Merge "Use WikiPage::makeParserOptions() where possible."
authorAaron Schulz <aschulz@wikimedia.org>
Sun, 16 Sep 2012 18:02:56 +0000 (18:02 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Sun, 16 Sep 2012 18:02:56 +0000 (18:02 +0000)
includes/Article.php
includes/EditPage.php
includes/WikiPage.php
includes/api/ApiParse.php
includes/diff/DifferenceEngine.php

index bec5f14..9ab4b6b 100644 (file)
@@ -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;
index 291e3fe..2115eb0 100644 (file)
@@ -2613,9 +2613,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 !== '' );
 
@@ -2660,8 +2660,6 @@ HTML
 
                        wfRunHooks( 'EditPageGetPreviewText', array( $this, &$toparse ) );
 
-                       $parserOptions->enableLimitReport();
-
                        $toparse = $wgParser->preSaveTransform( $toparse, $this->mTitle, $wgUser, $parserOptions );
                        $parserOutput = $wgParser->parse( $toparse, $this->mTitle, $parserOptions );
 
index 74772f2..ff2d5bb 100644 (file)
@@ -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;
        }
 
index c63ae8a..db6e2bb 100644 (file)
@@ -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
index 3846473..c7156fb 100644 (file)
@@ -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 );