From 34720765dab58bcef432cbd20fe1f95725428438 Mon Sep 17 00:00:00 2001 From: Daniel Kraus Date: Tue, 10 Jun 2014 13:41:35 +0200 Subject: [PATCH] Implement config for check-redirect and magic word. Added two new configuration variables, $wgLinkTitlesCheckRedirect and $wgLinkTitlesEnableNoTargetMagicWord, that can be used to increase performance. If both are overridden to be false, the target page content will not be fetched from the database to check for redirect or the occurrence of a magic word, which saves a lot of time. --- LinkTitles.body.php | 57 +++++++++++++++++++++++++++++++-------------- LinkTitles.php | 32 +++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 18 deletions(-) diff --git a/LinkTitles.body.php b/LinkTitles.body.php index 05c0729..350132a 100755 --- a/LinkTitles.body.php +++ b/LinkTitles.body.php @@ -91,9 +91,10 @@ global $wgLinkTitlesFirstOnly; global $wgLinkTitlesWordStartOnly; global $wgLinkTitlesWordEndOnly; - // global $wgLinkTitlesIgnoreCase; global $wgLinkTitlesSmartMode; global $wgCapitalLinks; + global $wgLinkTitlesEnableNoTargetMagicWord; + global $wgLinkTitlesCheckRedirect; ( $wgLinkTitlesWordStartOnly ) ? $wordStartDelim = '\b' : $wordStartDelim = ''; ( $wgLinkTitlesWordEndOnly ) ? $wordEndDelim = '\b' : $wordEndDelim = ''; @@ -180,28 +181,47 @@ // Iterate through the page titles foreach( $res as $row ) { - // Obtain an instance of a Title class for the current database - // row. + // Obtain an instance of a Title class for the current database row. $targetTitle = Title::makeTitle(NS_MAIN, $row->page_title); - $targetTitleText = $targetTitle->getText(); - // Obtain a page object for the current title, so we can check for - // the presence of the __NOAUTOLINKTARGET__ magic keyword. - $targetPage = WikiPage::factory($targetTitle); - $targetText = $targetPage->getText(); + if ( $wgLinkTitlesCheckRedirect || $wgLinkTitlesEnableNoTargetMagicWord ) { + // Obtain a page object for the current title, so we can check for + // the presence of the __NOAUTOLINKTARGET__ magic keyword. + $targetPageContent = WikiPage::factory($targetTitle)->getContent(); - // To prevent linking to pages that redirect to the current page, - // obtain the title that the target page redirects to. Will be null - // if there is no redirect. - $redirectTitle = $targetPage->getContent()->getUltimateRedirectTarget(); + // To prevent linking to pages that redirect to the current page, + // obtain the title that the target page redirects to. Will be null + // if there is no redirect. + if ( $wgLinkTitlesCheckRedirect ) { + $redirectTitle = $targetPageContent->getUltimateRedirectTarget(); + $redirectCheck = !( $redirectTitle && $redirectTitle->equals($myTitle) ); + } + else + { + $redirectCheck = true; + }; + + if ( $wgLinkTitlesEnableNoTargetMagicWord ) { + $magicWordCheck = ! $targetPageContent->matchMagicWord( + MagicWord::get('MAG_LINKTITLES_NOTARGET') ); + } + else + { + $magicWordCheck = true; + }; + } + else + { + $redirectCheck = true; + $magicWordCheck = true; + } // Proceed only if the currently examined page does not redirect to - // our page and does not contain the no-target magic word - if ( - !( $redirectTitle && $redirectTitle->equals($myTitle) ) && - !( $targetPage->getContent()->matchMagicWord( - MagicWord::get('MAG_LINKTITLES_NOTARGET') ) ) ) { - + // our page and does not contain the no-target magic word. + // If the corresponding configuration variables are set to false, + // both 'check' variables below will be set to true by the code + // above. + if ( $redirectCheck && $magicWordCheck ) { // split the page content by [[...]] groups // credits to inhan @ StackOverflow for suggesting preg_split // see http://stackoverflow.com/questions/10672286 @@ -209,6 +229,7 @@ // Escape certain special characters in the page title to prevent // regexp compilation errors + $targetTitleText = $targetTitle->getText(); $quotedTitle = preg_quote($targetTitleText, '/'); // Depending on the global configuration setting $wgCapitalLinks, diff --git a/LinkTitles.php b/LinkTitles.php index 970cf21..3392e40 100755 --- a/LinkTitles.php +++ b/LinkTitles.php @@ -143,6 +143,38 @@ /// @ingroup config $wgLinkTitlesSmartMode = true; + /// Determines whether or not to check if a page redirects to the current + /// page. Normally one would want to have the default behavior (true), but + /// this check requires a time-consuming database query for every page in + /// the wiki. + /// @note For maximum performance, set both $wgLinkTitlesCheckRedirect and + /// $wgLinkTitlesEnableNoTargetMagicWord to false in LocalSettings.php. + /// On the developer's machine, fetching the target page content increased + /// the run time from about 50 ms to about 500 ms on a wiki with 90 pages + /// (Intel Core i5-3320M, 2x 2.6 GHz, 16 GB RAM, PHP 5.5.9, Apache 2.4.7, + /// MySQL 5.5.37, MediaWiki 1.23.0). If any of the two mentioned variables + /// is set to true, a page content request will be performed for every + /// page in the wiki whenever a single page is edited or parsed in batch + /// mode. + /// @ingroup config + $wgLinkTitlesCheckRedirect = true; + + /// Determines whether or not the magic word __NOAUTOLINKTARGET__ is + /// enabled or not. If __NOAUTOLINKTARGET__ is included in a page, this + /// page will never be linked to by the extension. The default is enabled + /// (true), but it comes at the cost of performance. + /// @note For maximum performance, set both $wgLinkTitlesCheckRedirect and + /// $wgLinkTitlesEnableNoTargetMagicWord to false in LocalSettings.php. + /// On the developer's machine, fetching the target page content increased + /// the run time from about 50 ms to about 500 ms on a wiki with 90 pages + /// (Intel Core i5-3320M, 2x 2.6 GHz, 16 GB RAM, PHP 5.5.9, Apache 2.4.7, + /// MySQL 5.5.37, MediaWiki 1.23.0). If any of the two mentioned variables + /// is set to true, a page content request will be performed for every + /// page in the wiki whenever a single page is edited or parsed in batch + /// mode. + /// @ingroup config + $wgLinkTitlesEnableNoTargetMagicWord = true; + /// Time limit for online batch processing. This determines the maximum /// amount of time in seconds that page processing will take before a /// refresh of the special page is issued.