From a9840b77e8ea11c6fb2c16cfc37d8470995479f9 Mon Sep 17 00:00:00 2001 From: c0nnex Date: Wed, 30 Sep 2015 01:47:51 +0200 Subject: [PATCH 01/17] Added Namespace support --- LinkTitles.body.php | 35 +++++++++++++++++------- LinkTitles.php | 4 +++ LinkTitles.phpproj | 66 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 9 deletions(-) create mode 100644 LinkTitles.phpproj diff --git a/LinkTitles.body.php b/LinkTitles.body.php index 64744a4..2713707 100755 --- a/LinkTitles.body.php +++ b/LinkTitles.body.php @@ -108,6 +108,7 @@ global $wgLinkTitlesFirstOnly; global $wgLinkTitlesSmartMode; global $wgCapitalLinks; + global $wgLinkTitlesNamespaces; ( $wgLinkTitlesPreferShortTitles ) ? $sort_order = 'ASC' : $sort_order = 'DESC'; ( $wgLinkTitlesFirstOnly ) ? $limit = 1 : $limit = -1; @@ -121,6 +122,23 @@ '("' . implode( '", "',$wgLinkTitlesBlackList ) . '", "' . LinkTitles::$currentTitle->getDbKey() . '")' ); + $currentNamespace = $title->getNamespace(); + + // Build our weight list. Make sure current namespace is first element + $namespaces = array_unshift( array($currentNamespace), array_diff($wgLinkTitlesNamespaces, array($currentNamespace)) ); + + // No need for sanitiy check. we are sure that we have at least one element in the array + $weightSelect = "CASE page_namespace "; + $currentWeight = 0; + foreach ($namespaces as &$namspacevalue) { + $currentWeight = $currentWeight + 100; + $weightSelect = $weightSelect . " WHEN " . $namspacevalue . " THEN " . $currentWeight . PHP_EOL; + } + $weightSelect = $weightSelect . " END "; + + $namespacesClause = str_replace( '_', ' ', + '("' . implode( '", "',$namespaces ) . '")' ); + // Build an SQL query and fetch all page titles ordered by length from // shortest to longest. Only titles from 'normal' pages (namespace uid // = 0) are returned. Since the db may be sqlite, we need a try..catch @@ -129,21 +147,20 @@ try { $res = $dbr->select( 'page', - 'page_title', - array( - 'page_namespace = 0', + array( 'page_title', "page_namespace" , "weight" = > $weightSelect), + array( 'CHAR_LENGTH(page_title) >= ' . $wgLinkTitlesMinimumTitleLength, 'page_title NOT IN ' . $blackList, ), __METHOD__, - array( 'ORDER BY' => 'CHAR_LENGTH(page_title) ' . $sort_order ) + array( 'ORDER BY' => 'weight ASC, CHAR_LENGTH(page_title) ' . $sort_order ) ); } catch (Exception $e) { $res = $dbr->select( 'page', 'page_title', array( - 'page_namespace = 0', + 'page_namespace IN ' . $namespacesClause, 'LENGTH(page_title) >= ' . $wgLinkTitlesMinimumTitleLength, 'page_title NOT IN ' . $blackList, ), @@ -154,7 +171,7 @@ // Iterate through the page titles foreach( $res as $row ) { - LinkTitles::newTarget( $row->page_title ); + LinkTitles::newTarget($row->page_namespace, $row->page_title ); // split the page content by [[...]] groups // credits to inhan @ StackOverflow for suggesting preg_split @@ -249,7 +266,7 @@ // Build an anonymous callback function to be used in simple mode. private static function simpleModeCallback( array $matches ) { if ( LinkTitles::checkTargetPage() ) { - return '[[' . $matches[0] . ']]'; + return '[[' . LinkTitles::$targetTitle . "|" . $matches[0] . ']]'; } else { @@ -305,9 +322,9 @@ } /// Sets member variables for the current target page. - private static function newTarget( $title ) { + private static function newTarget($ns, $title ) { // @todo Make this wiki namespace aware. - LinkTitles::$targetTitle = Title::makeTitle( NS_MAIN, $title); + LinkTitles::$targetTitle = Title::makeTitle( $ns, $title); LinkTitles::$targetContent = null; } diff --git a/LinkTitles.php b/LinkTitles.php index 8746808..e27504c 100755 --- a/LinkTitles.php +++ b/LinkTitles.php @@ -189,6 +189,10 @@ /// @ingroup config $wgLinkTitlesTimeLimit = 0.2; + /// Namespaces to search in weighted order. + /// Namespace of the page will always to highest weight, rest in given order + $wgLinkTitlesNamespaces = array(NS_USER); + /// @cond $wgExtensionCredits['parserhook'][] = array( 'path' => __FILE__, diff --git a/LinkTitles.phpproj b/LinkTitles.phpproj new file mode 100644 index 0000000..652932b --- /dev/null +++ b/LinkTitles.phpproj @@ -0,0 +1,66 @@ + + + Debug + LinkTitles + {1c5e70d9-0077-48f3-9e7a-97e6e7967c2e} + LinkTitles + Library + {A0786B88-2ADB-4C21-ABE8-AA2D79766269} + + + true + + + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From 4c3c5e493164b815cb6a75a15f3228b1e32f9050 Mon Sep 17 00:00:00 2001 From: c0nnex Date: Wed, 30 Sep 2015 01:50:19 +0200 Subject: [PATCH 02/17] Revert "Added Namespace support" This reverts commit a9840b77e8ea11c6fb2c16cfc37d8470995479f9. --- LinkTitles.body.php | 35 +++++++----------------- LinkTitles.php | 4 --- LinkTitles.phpproj | 66 --------------------------------------------- 3 files changed, 9 insertions(+), 96 deletions(-) delete mode 100644 LinkTitles.phpproj diff --git a/LinkTitles.body.php b/LinkTitles.body.php index 2713707..64744a4 100755 --- a/LinkTitles.body.php +++ b/LinkTitles.body.php @@ -108,7 +108,6 @@ global $wgLinkTitlesFirstOnly; global $wgLinkTitlesSmartMode; global $wgCapitalLinks; - global $wgLinkTitlesNamespaces; ( $wgLinkTitlesPreferShortTitles ) ? $sort_order = 'ASC' : $sort_order = 'DESC'; ( $wgLinkTitlesFirstOnly ) ? $limit = 1 : $limit = -1; @@ -122,23 +121,6 @@ '("' . implode( '", "',$wgLinkTitlesBlackList ) . '", "' . LinkTitles::$currentTitle->getDbKey() . '")' ); - $currentNamespace = $title->getNamespace(); - - // Build our weight list. Make sure current namespace is first element - $namespaces = array_unshift( array($currentNamespace), array_diff($wgLinkTitlesNamespaces, array($currentNamespace)) ); - - // No need for sanitiy check. we are sure that we have at least one element in the array - $weightSelect = "CASE page_namespace "; - $currentWeight = 0; - foreach ($namespaces as &$namspacevalue) { - $currentWeight = $currentWeight + 100; - $weightSelect = $weightSelect . " WHEN " . $namspacevalue . " THEN " . $currentWeight . PHP_EOL; - } - $weightSelect = $weightSelect . " END "; - - $namespacesClause = str_replace( '_', ' ', - '("' . implode( '", "',$namespaces ) . '")' ); - // Build an SQL query and fetch all page titles ordered by length from // shortest to longest. Only titles from 'normal' pages (namespace uid // = 0) are returned. Since the db may be sqlite, we need a try..catch @@ -147,20 +129,21 @@ try { $res = $dbr->select( 'page', - array( 'page_title', "page_namespace" , "weight" = > $weightSelect), - array( + 'page_title', + array( + 'page_namespace = 0', 'CHAR_LENGTH(page_title) >= ' . $wgLinkTitlesMinimumTitleLength, 'page_title NOT IN ' . $blackList, ), __METHOD__, - array( 'ORDER BY' => 'weight ASC, CHAR_LENGTH(page_title) ' . $sort_order ) + array( 'ORDER BY' => 'CHAR_LENGTH(page_title) ' . $sort_order ) ); } catch (Exception $e) { $res = $dbr->select( 'page', 'page_title', array( - 'page_namespace IN ' . $namespacesClause, + 'page_namespace = 0', 'LENGTH(page_title) >= ' . $wgLinkTitlesMinimumTitleLength, 'page_title NOT IN ' . $blackList, ), @@ -171,7 +154,7 @@ // Iterate through the page titles foreach( $res as $row ) { - LinkTitles::newTarget($row->page_namespace, $row->page_title ); + LinkTitles::newTarget( $row->page_title ); // split the page content by [[...]] groups // credits to inhan @ StackOverflow for suggesting preg_split @@ -266,7 +249,7 @@ // Build an anonymous callback function to be used in simple mode. private static function simpleModeCallback( array $matches ) { if ( LinkTitles::checkTargetPage() ) { - return '[[' . LinkTitles::$targetTitle . "|" . $matches[0] . ']]'; + return '[[' . $matches[0] . ']]'; } else { @@ -322,9 +305,9 @@ } /// Sets member variables for the current target page. - private static function newTarget($ns, $title ) { + private static function newTarget( $title ) { // @todo Make this wiki namespace aware. - LinkTitles::$targetTitle = Title::makeTitle( $ns, $title); + LinkTitles::$targetTitle = Title::makeTitle( NS_MAIN, $title); LinkTitles::$targetContent = null; } diff --git a/LinkTitles.php b/LinkTitles.php index e27504c..8746808 100755 --- a/LinkTitles.php +++ b/LinkTitles.php @@ -189,10 +189,6 @@ /// @ingroup config $wgLinkTitlesTimeLimit = 0.2; - /// Namespaces to search in weighted order. - /// Namespace of the page will always to highest weight, rest in given order - $wgLinkTitlesNamespaces = array(NS_USER); - /// @cond $wgExtensionCredits['parserhook'][] = array( 'path' => __FILE__, diff --git a/LinkTitles.phpproj b/LinkTitles.phpproj deleted file mode 100644 index 652932b..0000000 --- a/LinkTitles.phpproj +++ /dev/null @@ -1,66 +0,0 @@ - - - Debug - LinkTitles - {1c5e70d9-0077-48f3-9e7a-97e6e7967c2e} - LinkTitles - Library - {A0786B88-2ADB-4C21-ABE8-AA2D79766269} - - - true - - - false - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file From 1e155be6c632aea1195439743f622975bbbf685d Mon Sep 17 00:00:00 2001 From: c0nnex Date: Wed, 30 Sep 2015 01:52:17 +0200 Subject: [PATCH 03/17] Added Namespace support --- .gitignore | 4 ++++ LinkTitles.body.php | 35 ++++++++++++++++++++++++++--------- LinkTitles.php | 4 ++++ 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 5caec79..0fc8781 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,7 @@ gpl-2.0.txt Maintenance.php doxygen_sqlite3.db +/LinkTitles.phpproj +/LinkTitles.phpproj.user +/LinkTitles.sln +/LinkTitles.v12.suo diff --git a/LinkTitles.body.php b/LinkTitles.body.php index 64744a4..0e7dd4c 100755 --- a/LinkTitles.body.php +++ b/LinkTitles.body.php @@ -108,6 +108,7 @@ global $wgLinkTitlesFirstOnly; global $wgLinkTitlesSmartMode; global $wgCapitalLinks; + global $wgLinkTitlesNamespaces; ( $wgLinkTitlesPreferShortTitles ) ? $sort_order = 'ASC' : $sort_order = 'DESC'; ( $wgLinkTitlesFirstOnly ) ? $limit = 1 : $limit = -1; @@ -121,6 +122,23 @@ '("' . implode( '", "',$wgLinkTitlesBlackList ) . '", "' . LinkTitles::$currentTitle->getDbKey() . '")' ); + $currentNamespace = $title->getNamespace(); + + // Build our weight list. Make sure current namespace is first element + $namespaces = array_unshift( array($currentNamespace), array_diff($wgLinkTitlesNamespaces, array($currentNamespace)) ); + + // No need for sanitiy check. we are sure that we have at least one element in the array + $weightSelect = "CASE page_namespace "; + $currentWeight = 0; + foreach ($namespaces as &$namspacevalue) { + $currentWeight = $currentWeight + 100; + $weightSelect = $weightSelect . " WHEN " . $namspacevalue . " THEN " . $currentWeight . PHP_EOL; + } + $weightSelect = $weightSelect . " END "; + + $namespacesClause = str_replace( '_', ' ', + '("' . implode( '", "',$namespaces ) . '")' ); + // Build an SQL query and fetch all page titles ordered by length from // shortest to longest. Only titles from 'normal' pages (namespace uid // = 0) are returned. Since the db may be sqlite, we need a try..catch @@ -129,21 +147,20 @@ try { $res = $dbr->select( 'page', - 'page_title', - array( - 'page_namespace = 0', + array( 'page_title', "page_namespace" , "weight" => $weightSelect), + array( 'CHAR_LENGTH(page_title) >= ' . $wgLinkTitlesMinimumTitleLength, 'page_title NOT IN ' . $blackList, ), __METHOD__, - array( 'ORDER BY' => 'CHAR_LENGTH(page_title) ' . $sort_order ) + array( 'ORDER BY' => 'weight ASC, CHAR_LENGTH(page_title) ' . $sort_order ) ); } catch (Exception $e) { $res = $dbr->select( 'page', 'page_title', array( - 'page_namespace = 0', + 'page_namespace IN ' . $namespacesClause, 'LENGTH(page_title) >= ' . $wgLinkTitlesMinimumTitleLength, 'page_title NOT IN ' . $blackList, ), @@ -154,7 +171,7 @@ // Iterate through the page titles foreach( $res as $row ) { - LinkTitles::newTarget( $row->page_title ); + LinkTitles::newTarget($row->page_namespace, $row->page_title ); // split the page content by [[...]] groups // credits to inhan @ StackOverflow for suggesting preg_split @@ -249,7 +266,7 @@ // Build an anonymous callback function to be used in simple mode. private static function simpleModeCallback( array $matches ) { if ( LinkTitles::checkTargetPage() ) { - return '[[' . $matches[0] . ']]'; + return '[[' . LinkTitles::$targetTitle . "|" . $matches[0] . ']]'; } else { @@ -305,9 +322,9 @@ } /// Sets member variables for the current target page. - private static function newTarget( $title ) { + private static function newTarget($ns, $title ) { // @todo Make this wiki namespace aware. - LinkTitles::$targetTitle = Title::makeTitle( NS_MAIN, $title); + LinkTitles::$targetTitle = Title::makeTitle( $ns, $title); LinkTitles::$targetContent = null; } diff --git a/LinkTitles.php b/LinkTitles.php index 8746808..e27504c 100755 --- a/LinkTitles.php +++ b/LinkTitles.php @@ -189,6 +189,10 @@ /// @ingroup config $wgLinkTitlesTimeLimit = 0.2; + /// Namespaces to search in weighted order. + /// Namespace of the page will always to highest weight, rest in given order + $wgLinkTitlesNamespaces = array(NS_USER); + /// @cond $wgExtensionCredits['parserhook'][] = array( 'path' => __FILE__, From 1c6692d9d0ce753d0f553b60fbe448fe5099dc5d Mon Sep 17 00:00:00 2001 From: c0nnex Date: Wed, 30 Sep 2015 02:00:03 +0200 Subject: [PATCH 04/17] Fix 1 --- LinkTitles.body.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LinkTitles.body.php b/LinkTitles.body.php index 0e7dd4c..f7d77d0 100755 --- a/LinkTitles.body.php +++ b/LinkTitles.body.php @@ -122,10 +122,10 @@ '("' . implode( '", "',$wgLinkTitlesBlackList ) . '", "' . LinkTitles::$currentTitle->getDbKey() . '")' ); - $currentNamespace = $title->getNamespace(); + $currentNamespace[] = $title->getNamespace(); // Build our weight list. Make sure current namespace is first element - $namespaces = array_unshift( array($currentNamespace), array_diff($wgLinkTitlesNamespaces, array($currentNamespace)) ); + $namespaces = array_unshift( $currentNamespace, array_diff($wgLinkTitlesNamespaces, $currentNamespace) ); // No need for sanitiy check. we are sure that we have at least one element in the array $weightSelect = "CASE page_namespace "; From 8d3ccce48cae2f332f79e9584ff38d04de7cf3b7 Mon Sep 17 00:00:00 2001 From: c0nnex Date: Wed, 30 Sep 2015 02:05:09 +0200 Subject: [PATCH 05/17] Fix 2 --- LinkTitles.body.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LinkTitles.body.php b/LinkTitles.body.php index f7d77d0..19ebb8c 100755 --- a/LinkTitles.body.php +++ b/LinkTitles.body.php @@ -125,7 +125,7 @@ $currentNamespace[] = $title->getNamespace(); // Build our weight list. Make sure current namespace is first element - $namespaces = array_unshift( $currentNamespace, array_diff($wgLinkTitlesNamespaces, $currentNamespace) ); + $namespaces = array_unshift(array_diff($wgLinkTitlesNamespaces, $currentNamespace), $currentNamespace ); // No need for sanitiy check. we are sure that we have at least one element in the array $weightSelect = "CASE page_namespace "; @@ -137,7 +137,7 @@ $weightSelect = $weightSelect . " END "; $namespacesClause = str_replace( '_', ' ', - '("' . implode( '", "',$namespaces ) . '")' ); + '(' . implode( ', ',$namespaces ) . ')' ); // Build an SQL query and fetch all page titles ordered by length from // shortest to longest. Only titles from 'normal' pages (namespace uid From bed28dd61cf949b96a8406f9227e81cd8490d455 Mon Sep 17 00:00:00 2001 From: c0nnex Date: Wed, 30 Sep 2015 02:15:26 +0200 Subject: [PATCH 06/17] Fix 3 --- LinkTitles.body.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/LinkTitles.body.php b/LinkTitles.body.php index 19ebb8c..5e602ce 100755 --- a/LinkTitles.body.php +++ b/LinkTitles.body.php @@ -123,10 +123,11 @@ LinkTitles::$currentTitle->getDbKey() . '")' ); $currentNamespace[] = $title->getNamespace(); - + wfDebugLog("LinkTitles","$wgLinkTitlesNamespaces = ". print_r($wgLinkTitlesNamespaces,true)); + wfDebugLog("LinkTitles","$currentNamespace = ". print_r($currentNamespace,true)); // Build our weight list. Make sure current namespace is first element $namespaces = array_unshift(array_diff($wgLinkTitlesNamespaces, $currentNamespace), $currentNamespace ); - + wfDebugLog("LinkTitles","$namespaces = ". print_r($namespaces,true)); // No need for sanitiy check. we are sure that we have at least one element in the array $weightSelect = "CASE page_namespace "; $currentWeight = 0; From 1b8ef249b6140bb327aab0bfe3fb8d80b4ce8958 Mon Sep 17 00:00:00 2001 From: c0nnex Date: Wed, 30 Sep 2015 02:18:53 +0200 Subject: [PATCH 07/17] Fix 4 --- LinkTitles.body.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/LinkTitles.body.php b/LinkTitles.body.php index 5e602ce..d435024 100755 --- a/LinkTitles.body.php +++ b/LinkTitles.body.php @@ -123,11 +123,11 @@ LinkTitles::$currentTitle->getDbKey() . '")' ); $currentNamespace[] = $title->getNamespace(); - wfDebugLog("LinkTitles","$wgLinkTitlesNamespaces = ". print_r($wgLinkTitlesNamespaces,true)); - wfDebugLog("LinkTitles","$currentNamespace = ". print_r($currentNamespace,true)); + wfDebugLog("LinkTitles",'$wgLinkTitlesNamespaces = '. print_r($wgLinkTitlesNamespaces,true)); + wfDebugLog("LinkTitles",'$currentNamespace = '. print_r($currentNamespace,true)); // Build our weight list. Make sure current namespace is first element $namespaces = array_unshift(array_diff($wgLinkTitlesNamespaces, $currentNamespace), $currentNamespace ); - wfDebugLog("LinkTitles","$namespaces = ". print_r($namespaces,true)); + wfDebugLog("LinkTitles",'$namespaces = '. print_r($namespaces,true)); // No need for sanitiy check. we are sure that we have at least one element in the array $weightSelect = "CASE page_namespace "; $currentWeight = 0; From 70f4d87bf7457f037f58bd235a5284a8bd5690c6 Mon Sep 17 00:00:00 2001 From: c0nnex Date: Wed, 30 Sep 2015 02:23:34 +0200 Subject: [PATCH 08/17] Fix 6 --- LinkTitles.body.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/LinkTitles.body.php b/LinkTitles.body.php index d435024..65e9905 100755 --- a/LinkTitles.body.php +++ b/LinkTitles.body.php @@ -125,8 +125,10 @@ $currentNamespace[] = $title->getNamespace(); wfDebugLog("LinkTitles",'$wgLinkTitlesNamespaces = '. print_r($wgLinkTitlesNamespaces,true)); wfDebugLog("LinkTitles",'$currentNamespace = '. print_r($currentNamespace,true)); + // Build our weight list. Make sure current namespace is first element - $namespaces = array_unshift(array_diff($wgLinkTitlesNamespaces, $currentNamespace), $currentNamespace ); + $namespaces = array_diff($wgLinkTitlesNamespaces, $currentNamespace); + array_unshift($namespaces, $currentNamespace ); wfDebugLog("LinkTitles",'$namespaces = '. print_r($namespaces,true)); // No need for sanitiy check. we are sure that we have at least one element in the array $weightSelect = "CASE page_namespace "; @@ -148,8 +150,9 @@ try { $res = $dbr->select( 'page', - array( 'page_title', "page_namespace" , "weight" => $weightSelect), - array( + array( 'page_title', 'page_namespace' , "weight" => $weightSelect), + array( + 'page_namespace IN ' . $namespacesClause, 'CHAR_LENGTH(page_title) >= ' . $wgLinkTitlesMinimumTitleLength, 'page_title NOT IN ' . $blackList, ), @@ -159,14 +162,14 @@ } catch (Exception $e) { $res = $dbr->select( 'page', - 'page_title', + array( 'page_title', 'page_namespace' , "weight" => $weightSelect ), array( 'page_namespace IN ' . $namespacesClause, 'LENGTH(page_title) >= ' . $wgLinkTitlesMinimumTitleLength, 'page_title NOT IN ' . $blackList, ), __METHOD__, - array( 'ORDER BY' => 'LENGTH(page_title) ' . $sort_order ) + array( 'ORDER BY' => 'weight ASC, LENGTH(page_title) ' . $sort_order ) ); } From b18350efddb45c0ea06cf52503ff3f04df6e275a Mon Sep 17 00:00:00 2001 From: c0nnex Date: Wed, 30 Sep 2015 02:25:05 +0200 Subject: [PATCH 09/17] Fix 7 --- LinkTitles.body.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/LinkTitles.body.php b/LinkTitles.body.php index 65e9905..d230b73 100755 --- a/LinkTitles.body.php +++ b/LinkTitles.body.php @@ -128,8 +128,9 @@ // Build our weight list. Make sure current namespace is first element $namespaces = array_diff($wgLinkTitlesNamespaces, $currentNamespace); - array_unshift($namespaces, $currentNamespace ); + array_unshift($namespaces, $currentNamespace[0] ); wfDebugLog("LinkTitles",'$namespaces = '. print_r($namespaces,true)); + // No need for sanitiy check. we are sure that we have at least one element in the array $weightSelect = "CASE page_namespace "; $currentWeight = 0; From c75fe391ce24f2cc42be0f978a5a5d07613131d2 Mon Sep 17 00:00:00 2001 From: c0nnex Date: Wed, 30 Sep 2015 02:28:01 +0200 Subject: [PATCH 10/17] Made debug logging private (explicit turn on). Final --- LinkTitles.body.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/LinkTitles.body.php b/LinkTitles.body.php index d230b73..20b6f5c 100755 --- a/LinkTitles.body.php +++ b/LinkTitles.body.php @@ -123,13 +123,13 @@ LinkTitles::$currentTitle->getDbKey() . '")' ); $currentNamespace[] = $title->getNamespace(); - wfDebugLog("LinkTitles",'$wgLinkTitlesNamespaces = '. print_r($wgLinkTitlesNamespaces,true)); - wfDebugLog("LinkTitles",'$currentNamespace = '. print_r($currentNamespace,true)); + wfDebugLog("LinkTitles",'$wgLinkTitlesNamespaces = '. print_r($wgLinkTitlesNamespaces,true),'private'); + wfDebugLog("LinkTitles",'$currentNamespace = '. print_r($currentNamespace,true),'private'); // Build our weight list. Make sure current namespace is first element $namespaces = array_diff($wgLinkTitlesNamespaces, $currentNamespace); array_unshift($namespaces, $currentNamespace[0] ); - wfDebugLog("LinkTitles",'$namespaces = '. print_r($namespaces,true)); + wfDebugLog("LinkTitles",'$namespaces = '. print_r($namespaces,true),'private'); // No need for sanitiy check. we are sure that we have at least one element in the array $weightSelect = "CASE page_namespace "; @@ -140,8 +140,7 @@ } $weightSelect = $weightSelect . " END "; - $namespacesClause = str_replace( '_', ' ', - '(' . implode( ', ',$namespaces ) . ')' ); + $namespacesClause = str_replace( '_', ' ','(' . implode( ', ',$namespaces ) . ')' ); // Build an SQL query and fetch all page titles ordered by length from // shortest to longest. Only titles from 'normal' pages (namespace uid From 5e58bc0dfc74eab9b841e475a98b7fb84a5733b6 Mon Sep 17 00:00:00 2001 From: c0nnex Date: Wed, 30 Sep 2015 02:48:10 +0200 Subject: [PATCH 11/17] Added namepsace-support to cli and specialpage --- LinkTitles.body.php | 9 ++++----- LinkTitles.cli.php | 11 ++++++++--- SpecialLinkTitles.php | 10 +++++++--- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/LinkTitles.body.php b/LinkTitles.body.php index 20b6f5c..b54417e 100755 --- a/LinkTitles.body.php +++ b/LinkTitles.body.php @@ -239,13 +239,12 @@ /// If in doubt, call MediaWiki's `RequestContext::getMain()` /// to obtain such an object. /// @returns undefined - public static function processPage($title, RequestContext $context) { - // TODO: make this namespace-aware - $titleObj = Title::makeTitle(0, $title); - $page = WikiPage::factory($titleObj); + public static function processPage(Title $title, RequestContext $context) { + // TODO: make this namespace-aware + $page = WikiPage::factory($title); $content = $page->getContent(); $text = $content->getContentHandler()->serializeContent($content); - $newText = LinkTitles::parseContent($titleObj, $text); + $newText = LinkTitles::parseContent($title, $text); if ( $text != $newText ) { $content = $content->getContentHandler()->unserializeContent( $newText ); $page->doQuickEditContent($content, diff --git a/LinkTitles.cli.php b/LinkTitles.cli.php index a7feded..bf734fc 100755 --- a/LinkTitles.cli.php +++ b/LinkTitles.cli.php @@ -71,20 +71,25 @@ class LinkTitlesCli extends Maintenance { /// if the `--start` option is given) and call LinkTitles::processPage() for /// each page. public function execute() { + global $wgLinkTitlesNamespaces; + $index = intval($this->getOption('start', 0)); if ( $index < 0 ) { $this->error('FATAL: Start index must be 0 or greater.', 1); }; + // get our Namespaces + $namespacesClause = str_replace( '_', ' ','(' . implode( ', ',$wgLinkTitlesNamespaces ) . ')' ); + // Connect to the database $dbr = $this->getDB( DB_SLAVE ); // Retrieve page names from the database. $res = $dbr->select( 'page', - 'page_title', + array('page_title', 'page_namespace'), array( - 'page_namespace = 0', + 'page_namespace IN ' . $namespacesClause, ), __METHOD__, array( @@ -99,7 +104,7 @@ class LinkTitlesCli extends Maintenance { // Iterate through the pages; break if a time limit is exceeded. foreach ( $res as $row ) { $index += 1; - $curTitle = $row->page_title; + $curTitle = Title::makeTitle( $row->page_namespace, $row->page_title); $this->output( sprintf("\rPage #%d (%02.0f%%)", $index, $index / $numPages * 100) ); diff --git a/SpecialLinkTitles.php b/SpecialLinkTitles.php index 4d7ce89..5d205f3 100644 --- a/SpecialLinkTitles.php +++ b/SpecialLinkTitles.php @@ -79,6 +79,10 @@ class SpecialLinkTitles extends SpecialPage { /// @param OutputPage $output Output page for the special page. private function process( WebRequest &$request, OutputPage &$output) { global $wgLinkTitlesTimeLimit; + global $wgLinkTitlesNamespaces; + + // get our Namespaces + $namespacesClause = str_replace( '_', ' ','(' . implode( ', ',$wgLinkTitlesNamespaces ) . ')' ); // Start the stopwatch $startTime = microtime(true); @@ -111,9 +115,9 @@ class SpecialLinkTitles extends SpecialPage { // Retrieve page names from the database. $res = $dbr->select( 'page', - 'page_title', + array('page_title', 'page_namespace'), array( - 'page_namespace = 0', + 'page_namespace IN ' . $namespacesClause, ), __METHOD__, array( @@ -124,7 +128,7 @@ class SpecialLinkTitles extends SpecialPage { // Iterate through the pages; break if a time limit is exceeded. foreach ( $res as $row ) { - $curTitle = $row->page_title; + $curTitle = Title::makeTitle( $row->page_namespace, $row->page_title); LinkTitles::processPage($curTitle, $this->getContext()); $start += 1; From d7464ac995250726d0c00ac08ce42f269c6884cc Mon Sep 17 00:00:00 2001 From: c0nnex Date: Thu, 1 Oct 2015 03:40:16 +0200 Subject: [PATCH 12/17] NS Check 1 --- LinkTitles.body.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/LinkTitles.body.php b/LinkTitles.body.php index b54417e..6880a29 100755 --- a/LinkTitles.body.php +++ b/LinkTitles.body.php @@ -186,6 +186,7 @@ // regexp compilation errors LinkTitles::$targetTitleText = LinkTitles::$targetTitle->getText(); $quotedTitle = preg_quote(LinkTitles::$targetTitleText, '/'); + wfDebugLog("LinkTitles",'TargetTitle='. LinkTitles::$targetTitleText,"private"); // Depending on the global configuration setting $wgCapitalLinks, // the title has to be searched for either in a strictly case-sensitive @@ -295,7 +296,7 @@ if ( LinkTitles::checkTargetPage() ) { if ( strcmp(substr(LinkTitles::$targetTitleText, 1), substr($matches[0], 1)) == 0 ) { // Case-sensitive match: no need to bulid piped link. - return '[[' . $matches[0] . ']]'; + return '[[' . LinkTitles::$targetTitle . "|" . $matches[0] . ']]'; } else { // Case-insensitive match: build piped link. return '[[' . LinkTitles::$targetTitleText . '|' . $matches[0] . ']]'; From 40c1e73818f02927e83ba8da8ca8b84fa095bb82 Mon Sep 17 00:00:00 2001 From: c0nnex Date: Thu, 1 Oct 2015 04:04:55 +0200 Subject: [PATCH 13/17] Namspeces 2 --- LinkTitles.body.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LinkTitles.body.php b/LinkTitles.body.php index 6880a29..940d1cd 100755 --- a/LinkTitles.body.php +++ b/LinkTitles.body.php @@ -184,8 +184,8 @@ // Escape certain special characters in the page title to prevent // regexp compilation errors - LinkTitles::$targetTitleText = LinkTitles::$targetTitle->getText(); - $quotedTitle = preg_quote(LinkTitles::$targetTitleText, '/'); + LinkTitles::$targetTitleText = LinkTitles::$targetTitle->getText(); // inlcudes namespace + $quotedTitle = preg_quote( LinkTitles::$targetTitle->getTitleValue()->getText(), '/'); wfDebugLog("LinkTitles",'TargetTitle='. LinkTitles::$targetTitleText,"private"); // Depending on the global configuration setting $wgCapitalLinks, From 70c679f7574be615bf9b4743038d74bf53469dab Mon Sep 17 00:00:00 2001 From: c0nnex Date: Thu, 1 Oct 2015 04:57:40 +0200 Subject: [PATCH 14/17] Debug Namespace clutches --- LinkTitles.body.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/LinkTitles.body.php b/LinkTitles.body.php index 940d1cd..16d0895 100755 --- a/LinkTitles.body.php +++ b/LinkTitles.body.php @@ -175,6 +175,7 @@ // Iterate through the page titles foreach( $res as $row ) { + LinkTitles::newTarget($row->page_namespace, $row->page_title ); // split the page content by [[...]] groups @@ -184,10 +185,10 @@ // Escape certain special characters in the page title to prevent // regexp compilation errors - LinkTitles::$targetTitleText = LinkTitles::$targetTitle->getText(); // inlcudes namespace + LinkTitles::$targetTitleText = LinkTitles::$targetTitle->getPrefixedText(); // containes Namespace ! $quotedTitle = preg_quote( LinkTitles::$targetTitle->getTitleValue()->getText(), '/'); wfDebugLog("LinkTitles",'TargetTitle='. LinkTitles::$targetTitleText,"private"); - + wfDebugLog("LinkTitles",'TargetTitleQuoted='. $quotedTitle,"private"); // Depending on the global configuration setting $wgCapitalLinks, // the title has to be searched for either in a strictly case-sensitive // way, or in a 'fuzzy' way where the first letter of the title may @@ -328,7 +329,10 @@ /// Sets member variables for the current target page. private static function newTarget($ns, $title ) { // @todo Make this wiki namespace aware. - LinkTitles::$targetTitle = Title::makeTitle( $ns, $title); + LinkTitles::$targetTitle = Title::newFromText( $title , $ns ); + wfDebugLog("LinkTitles",'newtarget='. print_r( LinkTitles::$targetTitle, true ) ,"private"); + + wfDebugLog("LinkTitles",'altTarget='. print_r( LinkTitles::$targetTitle->getTitleValue(), true ) ,"private"); LinkTitles::$targetContent = null; } From 124e92aafdc3d597f630a6a63644901b3ea692c8 Mon Sep 17 00:00:00 2001 From: c0nnex Date: Fri, 2 Oct 2015 02:35:47 +0200 Subject: [PATCH 15/17] * Fixed linking in Namespace pages * added paramters to cli interface --page={pagename} only process that page --log : show some output about the process --debug : show debug output in cli * changed pagecount on SpecialPage to use COUNT instead of selecting all rows --- LinkTitles.body.php | 78 +++++++++++++++++++++++++++---------------- LinkTitles.cli.php | 40 +++++++++++++++++++++- SpecialLinkTitles.php | 13 ++++---- 3 files changed, 95 insertions(+), 36 deletions(-) diff --git a/LinkTitles.body.php b/LinkTitles.body.php index 16d0895..c01748c 100755 --- a/LinkTitles.body.php +++ b/LinkTitles.body.php @@ -18,7 +18,7 @@ * MA 02110-1301, USA. */ /// @file - + /// Helper function for development and debugging. /// @param $var Any variable. Raw content will be dumped to stderr. /// @return undefined @@ -35,6 +35,9 @@ /// A Title object for the target page currently being examined. private static $targetTitle; + // The TiltleValue object of the target page + private static $targetTitleValue; + /// The content object for the currently processed target page. /// This variable is necessary to be able to prevent loading the target /// content twice. @@ -52,6 +55,9 @@ private static $wordStartDelim; private static $wordEndDelim; + public static $ltConsoleOutput; + public static $ltConsoleOutputDebug; + /// Setup function, hooks the extension's functions to MediaWiki events. public static function setup() { global $wgLinkTitlesParseOnEdit; @@ -91,6 +97,24 @@ return true; } + /// Local Debugging output function which can send output to console as well + public static function ltDebugLog($text) { + if (LinkTitles::$ltConsoleOutputDebug) + { + print $text . "\n"; + } + wfDebugLog('LinkTitles', $text , 'private'); + } + + /// Local Logging output function which can send output to console as well + public static function ltLog($text) { + if (LinkTitles::$ltConsoleOutput) + { + print $text . "\n"; + } + wfDebugLog('LinkTitles', $text , 'private'); + } + /// Core function of the extension, performs the actual parsing of the content. /// @param Title $title Title of the page being parsed /// @param $text String that holds the article content @@ -116,6 +140,8 @@ LinkTitles::$currentTitle = $title; $newText = $text; + + // Build a blacklist of pages that are not supposed to be link // targets. This includes the current page. $blackList = str_replace( '_', ' ', @@ -123,14 +149,12 @@ LinkTitles::$currentTitle->getDbKey() . '")' ); $currentNamespace[] = $title->getNamespace(); - wfDebugLog("LinkTitles",'$wgLinkTitlesNamespaces = '. print_r($wgLinkTitlesNamespaces,true),'private'); - wfDebugLog("LinkTitles",'$currentNamespace = '. print_r($currentNamespace,true),'private'); - + + // Build our weight list. Make sure current namespace is first element $namespaces = array_diff($wgLinkTitlesNamespaces, $currentNamespace); array_unshift($namespaces, $currentNamespace[0] ); - wfDebugLog("LinkTitles",'$namespaces = '. print_r($namespaces,true),'private'); - + // No need for sanitiy check. we are sure that we have at least one element in the array $weightSelect = "CASE page_namespace "; $currentWeight = 0; @@ -186,9 +210,11 @@ // Escape certain special characters in the page title to prevent // regexp compilation errors LinkTitles::$targetTitleText = LinkTitles::$targetTitle->getPrefixedText(); // containes Namespace ! - $quotedTitle = preg_quote( LinkTitles::$targetTitle->getTitleValue()->getText(), '/'); - wfDebugLog("LinkTitles",'TargetTitle='. LinkTitles::$targetTitleText,"private"); - wfDebugLog("LinkTitles",'TargetTitleQuoted='. $quotedTitle,"private"); + $quotedTitle = preg_quote( LinkTitles::$targetTitleValue->getText(), '/'); + + LinkTitles::ltDebugLog('TargetTitle='. LinkTitles::$targetTitleText,"private"); + LinkTitles::ltDebugLog('TargetTitleQuoted='. $quotedTitle,"private"); + // Depending on the global configuration setting $wgCapitalLinks, // the title has to be searched for either in a strictly case-sensitive // way, or in a 'fuzzy' way where the first letter of the title may @@ -242,7 +268,9 @@ /// to obtain such an object. /// @returns undefined public static function processPage(Title $title, RequestContext $context) { - // TODO: make this namespace-aware + + LinkTitles::ltLog('Processing '. $title->getPrefixedText()); + $page = WikiPage::factory($title); $content = $page->getContent(); $text = $content->getContentHandler()->serializeContent($content); @@ -270,7 +298,9 @@ // Build an anonymous callback function to be used in simple mode. private static function simpleModeCallback( array $matches ) { + if ( LinkTitles::checkTargetPage() ) { + LinkTitles::ltLog("Linking '$matches[0]' to '" . LinkTitles::$targetTitle . "'"); return '[[' . LinkTitles::$targetTitle . "|" . $matches[0] . ']]'; } else @@ -288,20 +318,15 @@ // piped link if only the case of the first letter is different. private static function smartModeCallback( array $matches ) { global $wgCapitalLinks; - + if ( $wgCapitalLinks ) { // With $wgCapitalLinks set to true we have a slightly more // complicated version of the callback than if it were false; // we need to ignore the first letter of the page titles, as // it does not matter for linking. if ( LinkTitles::checkTargetPage() ) { - if ( strcmp(substr(LinkTitles::$targetTitleText, 1), substr($matches[0], 1)) == 0 ) { - // Case-sensitive match: no need to bulid piped link. - return '[[' . LinkTitles::$targetTitle . "|" . $matches[0] . ']]'; - } else { - // Case-insensitive match: build piped link. - return '[[' . LinkTitles::$targetTitleText . '|' . $matches[0] . ']]'; - } + LinkTitles::ltLog("Linking (smart) '$matches[0]' to '" . LinkTitles::$targetTitle . "'"); + return '[[' . LinkTitles::$targetTitle . "|" . $matches[0] . ']]'; } else { @@ -311,13 +336,8 @@ // If $wgCapitalLinks is false, we can use the simple variant // of the callback function. if ( LinkTitles::checkTargetPage() ) { - if ( strcmp(LinkTitles::$targetTitleText, $matches[0]) == 0 ) { - // Case-sensitive match: no need to bulid piped link. - return '[[' . $matches[0] . ']]'; - } else { - // Case-insensitive match: build piped link. - return '[[' . LinkTitles::$targetTitleText . '|' . $matches[0] . ']]'; - } + LinkTitles::ltLog("Linking (smart) '$matches[0]' to '" . LinkTitles::$targetTitle . "'"); + return '[[' . LinkTitles::$targetTitle . '|' . $matches[0] . ']]'; } else { @@ -329,10 +349,10 @@ /// Sets member variables for the current target page. private static function newTarget($ns, $title ) { // @todo Make this wiki namespace aware. - LinkTitles::$targetTitle = Title::newFromText( $title , $ns ); - wfDebugLog("LinkTitles",'newtarget='. print_r( LinkTitles::$targetTitle, true ) ,"private"); - - wfDebugLog("LinkTitles",'altTarget='. print_r( LinkTitles::$targetTitle->getTitleValue(), true ) ,"private"); + LinkTitles::$targetTitle = Title::makeTitleSafe($ns,$title); + LinkTitles::ltDebugLog('newtarget='. LinkTitles::$targetTitle->getText() ,"private"); + LinkTitles::$targetTitleValue = LinkTitles::$targetTitle->getTitleValue(); + LinkTitles::ltDebugLog('altTarget='. LinkTitles::$targetTitleValue->getText() ,"private"); LinkTitles::$targetContent = null; } diff --git a/LinkTitles.cli.php b/LinkTitles.cli.php index bf734fc..fc198c4 100755 --- a/LinkTitles.cli.php +++ b/LinkTitles.cli.php @@ -64,6 +64,26 @@ class LinkTitlesCli extends Maintenance { true, // requires argument "s" ); + $this->addOption( + "page", + "page to process", + false, // not required + true, // requires argument + "p" + ); + $this->addOption( + "log", + "enables logging to console", + false, // not required + false, // requires no argument + "l" + ); + $this->addOption( + "debug", + "enables debug logging to console", + false, // not required + false // requires no argument + ); } /// Main function of the maintenance script. @@ -78,6 +98,24 @@ class LinkTitlesCli extends Maintenance { $this->error('FATAL: Start index must be 0 or greater.', 1); }; + if ($this->hasOption('log')) + { + LinkTitles::$ltConsoleOutput = true; + } + if ($this->hasOption('debug')) + { + LinkTitles::$ltConsoleOutputDebug = true; + } + + $pagename = strval($this->getOption('page')); + if ($pagename != null) + { + + $curTitle = Title::newFromDBkey( $pagename ); + LinkTitles::processPage($curTitle,RequestContext::getMain() ); + $this->output("\nFinished parsing.\n"); + return; + } // get our Namespaces $namespacesClause = str_replace( '_', ' ','(' . implode( ', ',$wgLinkTitlesNamespaces ) . ')' ); @@ -104,7 +142,7 @@ class LinkTitlesCli extends Maintenance { // Iterate through the pages; break if a time limit is exceeded. foreach ( $res as $row ) { $index += 1; - $curTitle = Title::makeTitle( $row->page_namespace, $row->page_title); + $curTitle = Title::makeTitleSafe( $row->page_namespace, $row->page_title); $this->output( sprintf("\rPage #%d (%02.0f%%)", $index, $index / $numPages * 100) ); diff --git a/SpecialLinkTitles.php b/SpecialLinkTitles.php index 5d205f3..e5b4949 100644 --- a/SpecialLinkTitles.php +++ b/SpecialLinkTitles.php @@ -105,7 +105,7 @@ class SpecialLinkTitles extends SpecialPage { else { // No end index was given. Therefore, count pages now. - $end = $this->countPages($dbr); + $end = $this->countPages($dbr, $namespacesClause ); }; array_key_exists('r', $postValues) ? @@ -128,7 +128,7 @@ class SpecialLinkTitles extends SpecialPage { // Iterate through the pages; break if a time limit is exceeded. foreach ( $res as $row ) { - $curTitle = Title::makeTitle( $row->page_namespace, $row->page_title); + $curTitle = Title::makeTitleSafe( $row->page_namespace, $row->page_title); LinkTitles::processPage($curTitle, $this->getContext()); $start += 1; @@ -279,16 +279,17 @@ EOF /// Counts the number of pages in a read-access wiki database ($dbr). /// @param $dbr Read-only `Database` object. /// @returns Number of pages in the default namespace (0) of the wiki. - private function countPages(&$dbr) { + private function countPages(&$dbr, $namespacesClause) { $res = $dbr->select( 'page', - 'page_id', + array('pagecount' => "COUNT(page_id)"), array( - 'page_namespace = 0', + 'page_namespace IN ' . $namespacesClause, ), __METHOD__ ); - return $res->numRows(); + + return $res->current()->pagecount; } } From dca10de21f00c563de49f4b256dc8d56d1950c92 Mon Sep 17 00:00:00 2001 From: c0nnex Date: Fri, 2 Oct 2015 02:44:42 +0200 Subject: [PATCH 16/17] Fixed Hooks only to process pags in wanted namespaces. Fixes ugly autolinking in Sidebar and breaking imagepages --- LinkTitles.body.php | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/LinkTitles.body.php b/LinkTitles.body.php index c01748c..f448f4f 100755 --- a/LinkTitles.body.php +++ b/LinkTitles.body.php @@ -79,11 +79,17 @@ if ( ! $isMinor ) { $title = $wikiPage->getTitle(); - $text = $content->getContentHandler()->serializeContent($content); - $newText = self::parseContent( $title, $text ); - if ( $newText != $text ) { - $content = $content->getContentHandler()->unserializeContent( $newText ); - } + + // Only process if page is in one of our namespaces we want to link + // Fixes ugly autolinking of sidebar pages + if (in_array($title->getNamespace(),$wgLinkTitlesNamespaces)) + { + $text = $content->getContentHandler()->serializeContent($content); + $newText = self::parseContent( $title, $text ); + if ( $newText != $text ) { + $content = $content->getContentHandler()->unserializeContent( $newText ); + } + } }; return true; } @@ -93,7 +99,13 @@ /// @param $text Preprocessed text of the page. public static function onInternalParseBeforeLinks( Parser &$parser, &$text ) { $title = $parser->getTitle(); - $text = self::parseContent( $title, $text ); + + // Only process if page is in one of our namespaces we want to link + // Fixes ugly autolinking of sidebar pages + if (in_array($title->getNamespace(),$wgLinkTitlesNamespaces)) + { + $text = self::parseContent( $title, $text ); + } return true; } From 085a4032f07ef9200370e7561b5b22b4c05e287c Mon Sep 17 00:00:00 2001 From: c0nnex Date: Fri, 9 Oct 2015 17:22:30 +0200 Subject: [PATCH 17/17] Fixed error because of global missing --- LinkTitles.body.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/LinkTitles.body.php b/LinkTitles.body.php index f448f4f..23a9084 100755 --- a/LinkTitles.body.php +++ b/LinkTitles.body.php @@ -76,7 +76,7 @@ /// Event handler that is hooked to the PageContentSave event. public static function onPageContentSave( &$wikiPage, &$user, &$content, &$summary, $isMinor, $isWatch, $section, &$flags, &$status ) { - + global $wgLinkTitlesNamespaces; if ( ! $isMinor ) { $title = $wikiPage->getTitle(); @@ -98,6 +98,7 @@ /// @param Parser $parser Parser that raised the event. /// @param $text Preprocessed text of the page. public static function onInternalParseBeforeLinks( Parser &$parser, &$text ) { + global $wgLinkTitlesNamespaces; $title = $parser->getTitle(); // Only process if page is in one of our namespaces we want to link