From b856d3487e3912965bac425891ea570447efd8d5 Mon Sep 17 00:00:00 2001 From: Daniel Kraus Date: Thu, 31 Aug 2017 20:53:18 +0200 Subject: [PATCH] Add $wgLinkTitlesSameNamespace option. --- extension.json | 3 ++- includes/Config.php | 9 ++++++++- includes/Targets.php | 17 +++++++++++++---- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/extension.json b/extension.json index cdb3e3f..00057fd 100644 --- a/extension.json +++ b/extension.json @@ -30,7 +30,8 @@ "LinkTitlesWordEndOnly": true, "LinkTitlesSpecialPageReloadAfter": 1, "LinkTitlesSourceNamespaces": [], - "LinkTitlesTargetNamespaces": [] + "LinkTitlesTargetNamespaces": [], + "LinkTitlesSameNamespace": true }, "AutoloadClasses": { "LinkTitles\\Extension": "includes/Extension.php", diff --git a/includes/Config.php b/includes/Config.php index 28a70e5..0940cd2 100644 --- a/includes/Config.php +++ b/includes/Config.php @@ -163,6 +163,12 @@ class Config { */ public $specialPageReloadAfter; + /** + * Whether to link to pages in the same namespace (default is true). + * @var bool $sameNamespace; + */ + public $sameNamespace; + public $enableConsoleOutput; public $enableDebugConsoleOutput; @@ -180,6 +186,7 @@ class Config { global $wgLinkTitlesBlackList; global $wgLinkTitlesSourceNamespaces; global $wgLinkTitlesTargetNamespaces; + global $wgLinkTitlesSameNamespace; global $wgLinkTitlesFirstOnly; global $wgLinkTitlesSmartMode; global $wgCapitalLinks; @@ -197,6 +204,7 @@ class Config { $this->blackList = $wgLinkTitlesBlackList; $this->sourceNamespaces = $wgLinkTitlesSourceNamespaces ? $wgLinkTitlesSourceNamespaces : [ NS_MAIN ]; $this->targetNamespaces = $wgLinkTitlesTargetNamespaces; + $this->sameNamespace = $wgLinkTitlesSameNamespace; $this->firstOnly = $wgLinkTitlesFirstOnly; $this->smartMode = $wgLinkTitlesSmartMode; $this->capitalLinks = $wgCapitalLinks; // MediaWiki global variable @@ -210,5 +218,4 @@ class Config { $this->enableConsoleOutput = false; $this->enableDebugConsoleOutput = false; } - } diff --git a/includes/Targets.php b/includes/Targets.php index 3a37472..144c056 100644 --- a/includes/Targets.php +++ b/includes/Targets.php @@ -89,7 +89,6 @@ class Targets { * Fetches the page titles from the database. */ private function fetch() { - ( $this->config->preferShortTitles ) ? $sortOrder = 'ASC' : $sortOrder = 'DESC'; // Build a blacklist of pages that are not supposed to be link @@ -101,9 +100,19 @@ class Targets { $blackList = null; } - // Build our weight list. Make sure current namespace is first element - $namespaces = array_diff( $this->config->targetNamespaces, [ $this->sourceNamespace ] ); - array_unshift( $namespaces, $this->sourceNamespace ); + if ( $this->config->sameNamespace ) { + // Build our weight list. Make sure current namespace is first element + $namespaces = array_diff( $this->config->targetNamespaces, [ $this->sourceNamespace ] ); + array_unshift( $namespaces, $this->sourceNamespace ); + } else { + $namespaces = $this->config->targetNamespaces; + } + + if ( !$namespaces) { + // If there are absolutely no target namespaces (not even the one of the + // source page), we can just return. + return; + } // No need for sanitiy check. we are sure that we have at least one element in the array $weightSelect = "CASE page_namespace ";