From 600b5454778931490bf20f96e1454aa39535bb0f Mon Sep 17 00:00:00 2001 From: Daniel Kraus Date: Tue, 10 Jun 2014 19:24:58 +0200 Subject: [PATCH 1/5] Use typed parameters in functions. --- LinkTitles.body.php | 23 +++++++++++------------ SpecialLinkTitles.php | 8 ++++---- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/LinkTitles.body.php b/LinkTitles.body.php index 4d2b46a..a141a00 100755 --- a/LinkTitles.body.php +++ b/LinkTitles.body.php @@ -88,7 +88,7 @@ /// @param $article Article object /// @param $content Content object that holds the article content /// @returns true - static function parseContent( &$article, &$content ) { + static function parseContent( WikiPage &$wikiPage, Content &$content ) { // If the page contains the magic word '__NOAUTOLINKS__', do not parse // the content. if ( $content->matchMagicWord( @@ -121,7 +121,7 @@ $templatesDelimiter = '{{[^|]+?}}|{{.+\||'; }; - LinkTitles::$currentTitle = $article->getTitle(); + LinkTitles::$currentTitle = $wikiPage->getTitle(); $text = $content->getContentHandler()->serializeContent($content); $newText = $text; @@ -250,18 +250,17 @@ /// Automatically processes a single page, given a $title Title object. /// This function is called by the SpecialLinkTitles class and the /// LinkTitlesJob class. - /// @param $title `Title` object that identifies the page. - /// @param $context Object that implements IContextProvider. + /// @param string $title Page title. + /// @param RequestContext $context Current context. /// If in doubt, call MediaWiki's `RequestContext::getMain()` /// to obtain such an object. /// @returns undefined - public static function processPage($title, $context) { + public static function processPage($title, RequestContext $context) { // TODO: make this namespace-aware $titleObj = Title::makeTitle(0, $title); $page = WikiPage::factory($titleObj); $content = $page->getContent(); - $article = Article::newFromWikiPage($page, $context); - LinkTitles::parseContent($article, $content); + LinkTitles::parseContent($page, $content); $page->doQuickEditContent($content, $context->getUser(), "Links to existing pages added by LinkTitles bot.", @@ -274,14 +273,14 @@ /// page is displayed. /// @param $doubleUnderscoreIDs Array of magic word IDs. /// @returns true - public static function onGetDoubleUnderscoreIDs( &$doubleUnderscoreIDs ) { + public static function onGetDoubleUnderscoreIDs( array &$doubleUnderscoreIDs ) { $doubleUnderscoreIDs[] = 'MAG_LINKTITLES_NOTARGET'; $doubleUnderscoreIDs[] = 'MAG_LINKTITLES_NOAUTOLINKS'; return true; } // Build an anonymous callback function to be used in simple mode. - private static function simpleModeCallback( $matches ) { + private static function simpleModeCallback( array $matches ) { if ( LinkTitles::checkTargetPage() ) { return '[[' . $matches[0] . ']]'; } @@ -298,7 +297,7 @@ // If $wgCapitalLinks is set to true, the case of the first // letter is ignored by MediaWiki and we don't need to build a // piped link if only the case of the first letter is different. - private static function smartModeCallback( $matches ) { + private static function smartModeCallback( array $matches ) { global $wgCapitalLinks; if ( $wgCapitalLinks ) { @@ -339,9 +338,9 @@ } /// Sets member variables for the current target page. - private static function newTarget( $titleString ) { + private static function newTarget( $title ) { // @todo Make this wiki namespace aware. - LinkTitles::$targetTitle = Title::makeTitle( NS_MAIN, $titleString ); + LinkTitles::$targetTitle = Title::makeTitle( NS_MAIN, $title); LinkTitles::$targetContent = null; } diff --git a/SpecialLinkTitles.php b/SpecialLinkTitles.php index ce71271..6091da7 100644 --- a/SpecialLinkTitles.php +++ b/SpecialLinkTitles.php @@ -74,10 +74,10 @@ class SpecialLinkTitles extends SpecialPage { /// Processes wiki articles, starting at the page indicated by /// $startTitle. If $wgLinkTitlesTimeLimit is reached before all pages are /// processed, returns the title of the next page that needs processing. - /// @param $request WebRequest object that is associated with the special - /// page. - /// @param $output Output object that the special page is equipped with. - private function process(&$request, &$output) { + /// @param WebRequest $request WebRequest object that is associated with the special + /// page. + /// @param Output $output Output object that the special page is equipped with. + private function process( WebRequest &$request, Output &$output) { global $wgLinkTitlesTimeLimit; // Start the stopwatch From 35d1b57f381a4f5b650f1cbe039c7bd201b6d714 Mon Sep 17 00:00:00 2001 From: Daniel Kraus Date: Wed, 11 Jun 2014 08:02:50 +0200 Subject: [PATCH 2/5] Change signature of parseContent function. To be able to use the InternalParseBeforeLinks hook (to be implemented), a different signature was needed. This entailed subsequent changes to calling functions. --- LinkTitles.body.php | 51 ++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/LinkTitles.body.php b/LinkTitles.body.php index a141a00..ef0f081 100755 --- a/LinkTitles.body.php +++ b/LinkTitles.body.php @@ -63,10 +63,15 @@ public static function onPageContentSave( &$wikiPage, &$user, &$content, &$summary, $isMinor, $isWatch, $section, &$flags, &$status ) { - // To prevent time-consuming parsing of the page whenever - // it is edited and saved, we only parse it if the flag - // 'minor edits' is not set. - return $isMinor or self::parseContent( $wikiPage, $content ); + if ( ! $isMinor ) { + $title = $wikiPage->getTitle(); + $text = $content->getContentHandler()->serializeContent($content); + $newText = self::parseContent( $title, $text ); + if ( $newText != $text ) { + $content = $content->getContentHandler()->unserializeContent( $newText ); + } + }; + return true; } /// Event handler that is hooked to the ArticleAfterFetchContent event. @@ -85,14 +90,12 @@ } /// Core function of the extension, performs the actual parsing of the content. - /// @param $article Article object - /// @param $content Content object that holds the article content - /// @returns true - static function parseContent( WikiPage &$wikiPage, Content &$content ) { - // If the page contains the magic word '__NOAUTOLINKS__', do not parse - // the content. - if ( $content->matchMagicWord( - MagicWord::get('MAG_LINKTITLES_NOAUTOLINKS') ) ) { + /// @param Title $title Title of the page being parsed + /// @param $text String that holds the article content + /// @returns string: parsed text with links added if needed + private static function parseContent( Title &$title, &$text ) { + // If the page contains the magic word '__NOAUTOLINKS__', do not parse it. + if ( MagicWord::get('MAG_LINKTITLES_NOAUTOLINKS')->match( $text ) ) { return true; } @@ -121,8 +124,7 @@ $templatesDelimiter = '{{[^|]+?}}|{{.+\||'; }; - LinkTitles::$currentTitle = $wikiPage->getTitle(); - $text = $content->getContentHandler()->serializeContent($content); + LinkTitles::$currentTitle = $title; $newText = $text; // Build a regular expression that will capture existing wiki links ("[[...]]"), @@ -241,10 +243,7 @@ $newText = implode( '', $arr ); } // $wgLinkTitlesSmartMode }; // foreach $res as $row - if ( $newText != $text ) { - $content = $content->getContentHandler()->unserializeContent( $newText ); - } - return true; + return $newText; } /// Automatically processes a single page, given a $title Title object. @@ -260,12 +259,16 @@ $titleObj = Title::makeTitle(0, $title); $page = WikiPage::factory($titleObj); $content = $page->getContent(); - LinkTitles::parseContent($page, $content); - $page->doQuickEditContent($content, - $context->getUser(), - "Links to existing pages added by LinkTitles bot.", - true // minor modification - ); + $text = $content->getContentHandler()->serializeContent($content); + $newText = LinkTitles::parseContent($titleObj, $text); + if ( $text != $newText ) { + $content = $content->getContentHandler()->unserializeContent( $newText ); + $page->doQuickEditContent($content, + $context->getUser(), + "Links to existing pages added by LinkTitles bot.", + true // minor modification + ); + }; } /// Adds the two magic words defined by this extension to the list of From 330bc33cef7432bad87829ae1478cbc919f9b22f Mon Sep 17 00:00:00 2001 From: Daniel Kraus Date: Wed, 11 Jun 2014 11:58:39 +0200 Subject: [PATCH 3/5] Fix SpecialPage::process signature. --- SpecialLinkTitles.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SpecialLinkTitles.php b/SpecialLinkTitles.php index 6091da7..4d7ce89 100644 --- a/SpecialLinkTitles.php +++ b/SpecialLinkTitles.php @@ -76,8 +76,8 @@ class SpecialLinkTitles extends SpecialPage { /// processed, returns the title of the next page that needs processing. /// @param WebRequest $request WebRequest object that is associated with the special /// page. - /// @param Output $output Output object that the special page is equipped with. - private function process( WebRequest &$request, Output &$output) { + /// @param OutputPage $output Output page for the special page. + private function process( WebRequest &$request, OutputPage &$output) { global $wgLinkTitlesTimeLimit; // Start the stopwatch From 284b0695fe0301f7862c18509b44185a4f333e25 Mon Sep 17 00:00:00 2001 From: Daniel Kraus Date: Wed, 11 Jun 2014 12:00:06 +0200 Subject: [PATCH 4/5] Implement InternalParseBeforeLinks hook. This replaces the ArticleAfterFetchContent hook and enables the extension to parse output produced by templates for links. --- LinkTitles.body.php | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/LinkTitles.body.php b/LinkTitles.body.php index ef0f081..8bee8ae 100755 --- a/LinkTitles.body.php +++ b/LinkTitles.body.php @@ -53,13 +53,12 @@ $wgHooks['PageContentSave'][] = 'LinkTitles::onPageContentSave'; }; if ( $wgLinkTitlesParseOnRender ) { - $wgHooks['ArticleAfterFetchContentObject'][] = - 'LinkTitles::onArticleAfterFetchContentObject'; + $wgHooks['InternalParseBeforeLinks'][] = 'LinkTitles::onInternalParseBeforeLinks'; }; $wgHooks['GetDoubleUnderscoreIDs'][] = 'LinkTitles::onGetDoubleUnderscoreIDs'; } - /// Event handler that is hooked to the ArticleSave event. + /// Event handler that is hooked to the PageContentSave event. public static function onPageContentSave( &$wikiPage, &$user, &$content, &$summary, $isMinor, $isWatch, $section, &$flags, &$status ) { @@ -74,18 +73,12 @@ return true; } - /// Event handler that is hooked to the ArticleAfterFetchContent event. - /// @param $article Article object - /// @param $content Content object that holds the article content - public static function onArticleAfterFetchContentObject( &$article, &$content ) { - // The ArticleAfterFetchContentObject event is triggered whenever page - // content is retrieved from the database, i.e. also for editing etc. - // Therefore we access the global $action variabl to only parse the - // content when the page is viewed. - global $action; - if ( in_array( $action, array('view', 'render', 'purge') ) ) { - self::parseContent( $article, $content ); - }; + /// Event handler that is hooked to the InternalParseBeforeLinks event. + /// @param Parser $parser Parser that raised the event. + /// @param $text Preprocessed text of the page. + public static function onInternalParseBeforeLinks( Parser &$parser, &$text ) { + $title = $parser->getTitle(); + $text = self::parseContent( $title, $text ); return true; } From dd34719f0dc74b5afbe05e44381aae832f924ff2 Mon Sep 17 00:00:00 2001 From: Daniel Kraus Date: Wed, 11 Jun 2014 16:55:25 +0200 Subject: [PATCH 5/5] Fix page black list. --- LinkTitles.body.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/LinkTitles.body.php b/LinkTitles.body.php index 8bee8ae..b9b0d56 100755 --- a/LinkTitles.body.php +++ b/LinkTitles.body.php @@ -146,8 +146,8 @@ // Build a blacklist of pages that are not supposed to be link // targets. This includes the current page. - $black_list = str_replace( '_', ' ', - '("' . implode( '", "',$wgLinkTitlesBlackList ) . + $blackList = str_replace( '_', ' ', + '("' . implode( '", "',$wgLinkTitlesBlackList ) . '", "' . LinkTitles::$currentTitle->getDbKey() . '")' ); // Build an SQL query and fetch all page titles ordered by length from @@ -162,7 +162,7 @@ array( 'page_namespace = 0', 'CHAR_LENGTH(page_title) >= ' . $wgLinkTitlesMinimumTitleLength, - 'page_title NOT IN ' . $black_list, + 'page_title NOT IN ' . $blackList, ), __METHOD__, array( 'ORDER BY' => 'CHAR_LENGTH(page_title) ' . $sort_order ) @@ -174,7 +174,7 @@ array( 'page_namespace = 0', 'LENGTH(page_title) >= ' . $wgLinkTitlesMinimumTitleLength, - 'page_title NOT IN ' . $black_list, + 'page_title NOT IN ' . $blackList, ), __METHOD__, array( 'ORDER BY' => 'LENGTH(page_title) ' . $sort_order )