From be0878fd3ad2fcbfe7c070263caf22df1a449b14 Mon Sep 17 00:00:00 2001 From: Alexandre Emsenhuber Date: Sat, 11 Aug 2012 21:44:12 +0200 Subject: [PATCH] Use WikiPage::makeParserOptions() where possible. - This is needed to for I90965346 ((bug 37453) Move $wgDisable(Lang|Title)Conversion to ParserOptions) because that change sets an option based on the Title, and I don't want to duplicate that to all ParserOptions that need it. - Refactored ApiParse to have a WikiPage object available and changed some part to take advantage of having this object available. Also used ApiBase::getTitleOrPageId() to reduce code duplication. Change-Id: Iec98e472af9c43d940f77261367a796b0d7b4b54 --- includes/Article.php | 9 +++-- includes/EditPage.php | 6 ++-- includes/WikiPage.php | 20 ++++++++--- includes/api/ApiParse.php | 56 +++++++++++++----------------- includes/diff/DifferenceEngine.php | 4 +-- 5 files changed, 50 insertions(+), 45 deletions(-) diff --git a/includes/Article.php b/includes/Article.php index a3fb747176..d2828be741 100644 --- a/includes/Article.php +++ b/includes/Article.php @@ -1617,8 +1617,11 @@ class Article extends Page { * @return ParserOutput or false if the given revsion ID is not found */ public function getParserOutput( $oldid = null, User $user = null ) { - $user = is_null( $user ) ? $this->getContext()->getUser() : $user; - $parserOptions = $this->mPage->makeParserOptions( $user ); + if ( $user === null ) { + $parserOptions = $this->getParserOptions(); + } else { + $parserOptions = $this->mPage->makeParserOptions( $user ); + } return $this->mPage->getParserOutput( $parserOptions, $oldid ); } @@ -1629,7 +1632,7 @@ class Article extends Page { */ public function getParserOptions() { if ( !$this->mParserOptions ) { - $this->mParserOptions = $this->mPage->makeParserOptions( $this->getContext()->getUser() ); + $this->mParserOptions = $this->mPage->makeParserOptions( $this->getContext() ); } // Clone to allow modifications of the return value without affecting cache return clone $this->mParserOptions; diff --git a/includes/EditPage.php b/includes/EditPage.php index f9bba19040..077618df6d 100644 --- a/includes/EditPage.php +++ b/includes/EditPage.php @@ -2642,9 +2642,9 @@ HTML ' [[#' . self::EDITFORM_ID . '|' . $wgLang->getArrow() . ' ' . wfMessage( 'continue-editing' )->text() . ']]'; } - $parserOptions = ParserOptions::newFromUser( $wgUser ); + $parserOptions = $this->mArticle->makeParserOptions( $this->mArticle->getContext() ); + $parserOptions->setEditSection( false ); - $parserOptions->setTidy( true ); $parserOptions->setIsPreview( true ); $parserOptions->setIsSectionPreview( !is_null( $this->section ) && $this->section !== '' ); @@ -2689,8 +2689,6 @@ HTML wfRunHooks( 'EditPageGetPreviewText', array( $this, &$toparse ) ); - $parserOptions->enableLimitReport(); - $toparse = $wgParser->preSaveTransform( $toparse, $this->mTitle, $wgUser, $parserOptions ); $parserOutput = $wgParser->parse( $toparse, $this->mTitle, $parserOptions ); diff --git a/includes/WikiPage.php b/includes/WikiPage.php index 10217672a4..fb99d2e86f 100644 --- a/includes/WikiPage.php +++ b/includes/WikiPage.php @@ -1640,18 +1640,30 @@ class WikiPage extends Page implements IDBAccessObject { /** * Get parser options suitable for rendering the primary article wikitext - * @param User|string $user User object or 'canonical' + * + * @param IContextSource|User|string $context One of the following: + * - IContextSource: Use the User and the Language of the provided + * context + * - User: Use the provided User object and $wgLang for the language, + * so use an IContextSource object if possible. + * - 'canonical': Canonical options (anonymous user with default + * preferences and content language). * @return ParserOptions */ - public function makeParserOptions( $user ) { + public function makeParserOptions( $context ) { global $wgContLang; - if ( $user instanceof User ) { // settings per user (even anons) - $options = ParserOptions::newFromUser( $user ); + + if ( $context instanceof IContextSource ) { + $options = ParserOptions::newFromContext( $context ); + } elseif ( $context instanceof User ) { // settings per user (even anons) + $options = ParserOptions::newFromUser( $context ); } else { // canonical settings $options = ParserOptions::newFromUserAndLang( new User, $wgContLang ); } + $options->enableLimitReport(); // show inclusion/loop reports $options->setTidy( true ); // fix bad HTML + return $options; } diff --git a/includes/api/ApiParse.php b/includes/api/ApiParse.php index c63ae8ac67..db6e2bb8b0 100644 --- a/includes/api/ApiParse.php +++ b/includes/api/ApiParse.php @@ -68,10 +68,6 @@ class ApiParse extends ApiBase { $this->getContext()->setLanguage( Language::factory( $params['uselang'] ) ); } - $popts = ParserOptions::newFromContext( $this->getContext() ); - $popts->setTidy( true ); - $popts->enableLimitReport( !$params['disablepp'] ); - $redirValues = null; // Return result @@ -89,13 +85,15 @@ class ApiParse extends ApiBase { } $titleObj = $rev->getTitle(); - $wgTitle = $titleObj; + $pageObj = WikiPage::factory( $titleObj ); + $popts = $pageObj->makeParserOptions( $this->getContext() ); + $popts->enableLimitReport( !$params['disablepp'] ); // If for some reason the "oldid" is actually the current revision, it may be cached if ( $titleObj->getLatestRevID() === intval( $oldid ) ) { // May get from/save to parser cache - $p_result = $this->getParsedSectionOrText( $titleObj, $popts, $pageid, + $p_result = $this->getParsedSectionOrText( $pageObj, $popts, $pageid, isset( $prop['wikitext'] ) ) ; } else { // This is an old revision, so get the text differently $this->text = $rev->getText( Revision::FOR_THIS_USER, $this->getUser() ); @@ -129,32 +127,26 @@ class ApiParse extends ApiBase { foreach ( (array)$redirValues as $r ) { $to = $r['to']; } - $titleObj = Title::newFromText( $to ); - } else { - if ( !is_null ( $pageid ) ) { - $reqParams['pageids'] = $pageid; - $titleObj = Title::newFromID( $pageid ); - } else { // $page - $to = $page; - $titleObj = Title::newFromText( $to ); - } - } - if ( !is_null ( $pageid ) ) { - if ( !$titleObj ) { - // Still throw nosuchpageid error if pageid was provided - $this->dieUsageMsg( array( 'nosuchpageid', $pageid ) ); - } - } elseif ( !$titleObj || !$titleObj->exists() ) { - $this->dieUsage( "The page you specified doesn't exist", 'missingtitle' ); + $pageParams = array( 'title' => $to ); + } elseif ( !is_null( $pageid ) ) { + $pageParams = array( 'pageid' => $pageid ); + } else { // $page + $pageParams = array( 'title' => $page ); } + + $pageObj = $this->getTitleOrPageId( $pageParams, 'fromdb' ); + $titleObj = $pageObj->getTitle(); $wgTitle = $titleObj; if ( isset( $prop['revid'] ) ) { - $oldid = $titleObj->getLatestRevID(); + $oldid = $pageObj->getLatest(); } + $popts = $pageObj->makeParserOptions( $this->getContext() ); + $popts->enableLimitReport( !$params['disablepp'] ); + // Potentially cached - $p_result = $this->getParsedSectionOrText( $titleObj, $popts, $pageid, + $p_result = $this->getParsedSectionOrText( $pageObj, $popts, $pageid, isset( $prop['wikitext'] ) ) ; } } else { // Not $oldid, $pageid, $page. Hence based on $text @@ -168,6 +160,10 @@ class ApiParse extends ApiBase { $this->dieUsageMsg( array( 'invalidtitle', $title ) ); } $wgTitle = $titleObj; + $pageObj = WikiPage::factory( $titleObj ); + + $popts = $pageObj->makeParserOptions( $this->getContext() ); + $popts->enableLimitReport( !$params['disablepp'] ); if ( $this->section !== false ) { $this->text = $this->getSectionText( $this->text, $titleObj->getText() ); @@ -323,23 +319,21 @@ class ApiParse extends ApiBase { } /** - * @param $titleObj Title + * @param $page WikiPage * @param $popts ParserOptions * @param $pageId Int * @param $getWikitext Bool * @return ParserOutput */ - private function getParsedSectionOrText( $titleObj, $popts, $pageId = null, $getWikitext = false ) { + private function getParsedSectionOrText( $page, $popts, $pageId = null, $getWikitext = false ) { global $wgParser; - $page = WikiPage::factory( $titleObj ); - if ( $this->section !== false ) { $this->text = $this->getSectionText( $page->getRawText(), !is_null( $pageId ) - ? 'page id ' . $pageId : $titleObj->getText() ); + ? 'page id ' . $pageId : $page->getTitle()->getPrefixedText() ); // Not cached (save or load) - return $wgParser->parse( $this->text, $titleObj, $popts ); + return $wgParser->parse( $this->text, $page->getTitle(), $popts ); } else { // Try the parser cache first // getParserOutput will save to Parser cache if able diff --git a/includes/diff/DifferenceEngine.php b/includes/diff/DifferenceEngine.php index 3846473a59..c7156fb2f1 100644 --- a/includes/diff/DifferenceEngine.php +++ b/includes/diff/DifferenceEngine.php @@ -536,9 +536,7 @@ class DifferenceEngine extends ContextSource { $wikiPage = WikiPage::factory( $this->mNewPage ); } - $parserOptions = ParserOptions::newFromContext( $this->getContext() ); - $parserOptions->enableLimitReport(); - $parserOptions->setTidy( true ); + $parserOptions = $wikiPage->makeParserOptions( $this->getContext() ); if ( !$this->mNewRev->isCurrent() ) { $parserOptions->setEditSection( false ); -- 2.20.1