From 15d6be2c66e2556a46bd025e5c5f222f159cc7ef Mon Sep 17 00:00:00 2001 From: Daniel Kraus Date: Wed, 14 Dec 2016 21:15:51 +0100 Subject: [PATCH] Fix page option in maintenance script. - Fix: Maintenance script would crash if invoked with the --page option. Closes #13. --- LinkTitles_Maintenance.php | 17 ++++++++----- includes/LinkTitles_Extension.php | 40 ++++++++++++++++++------------- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/LinkTitles_Maintenance.php b/LinkTitles_Maintenance.php index 96b6f93..7132166 100755 --- a/LinkTitles_Maintenance.php +++ b/LinkTitles_Maintenance.php @@ -111,11 +111,16 @@ class Cli extends \Maintenance { $pagename = strval($this->getOption('page')); if ($pagename != null) { - - $curTitle = Title::newFromDBkey( $pagename ); - LinkTitles::processPage($curTitle,RequestContext::getMain() ); - $this->output("\nFinished parsing.\n"); - return; + $this->output( 'Processing single page: ' . $pagename); + $title = \Title::newFromText( $pagename ); + $success = Extension::processPage( $title, \RequestContext::getMain() ); + if ( $success ) { + $this->output( "\nFinished parsing.\n" ); + } + else { + $this->output( "\nError: There is no such page.\n" ); + } + return $success; } // get our Namespaces $namespacesClause = str_replace( '_', ' ','(' . implode( ', ',$wgLinkTitlesNamespaces ) . ')' ); @@ -150,7 +155,7 @@ class Cli extends \Maintenance { Extension::processPage($curTitle, $context); } - $this->output("\nFinished parsing.\n"); + $this->output("\rFinished parsing. \n"); } } diff --git a/includes/LinkTitles_Extension.php b/includes/LinkTitles_Extension.php index 9883d91..f4d7178 100644 --- a/includes/LinkTitles_Extension.php +++ b/includes/LinkTitles_Extension.php @@ -248,26 +248,32 @@ class Extension { /// Automatically processes a single page, given a $title Title object. /// This function is called by the SpecialLinkTitles class and the /// LinkTitlesJob class. - /// @param string $title Page title. - /// @param RequestContext $context Current context. + /// @param Title $title Title object. + /// @param RequestContext $context Current request context. /// If in doubt, call MediaWiki's `RequestContext::getMain()` /// to obtain such an object. - /// @returns undefined - public static function processPage( $title, \RequestContext $context ) { - $titleObj = \Title::makeTitle(0, $title); - self::ltLog('Processing '. $titleObj->getPrefixedText()); - $page = \WikiPage::factory($titleObj); + /// @returns boolean True if the page exists, false if the page does not exist + public static function processPage( \Title $title, \RequestContext $context ) { + self::ltLog('Processing '. $title->getPrefixedText()); + $page = \WikiPage::factory($title); $content = $page->getContent(); - $text = $content->getContentHandler()->serializeContent($content); - $newText = self::parseContent($titleObj, $text); - if ( $text != $newText ) { - $content = $content->getContentHandler()->unserializeContent( $newText ); - $page->doQuickEditContent($content, - $context->getUser(), - "Links to existing pages added by LinkTitles bot.", // TODO: i18n - true // minor modification - ); - }; + if ( $content != null ) { + $text = $content->getContentHandler()->serializeContent($content); + $newText = self::parseContent($title, $text); + if ( $text != $newText ) { + $content = $content->getContentHandler()->unserializeContent( $newText ); + $page->doQuickEditContent( + $content, + $context->getUser(), + "Links to existing pages added by LinkTitles bot.", // TODO: i18n + true // minor modification + ); + }; + return true; + } + else { + return false; + } } /// Adds the two magic words defined by this extension to the list of