Merge "mw.loader: Use Object.create() instead of $.extend() where possible"
[lhc/web/wiklou.git] / includes / api / ApiParse.php
index 91e49ab..7cbd353 100644 (file)
@@ -38,6 +38,9 @@ class ApiParse extends ApiBase {
        /** @var Content $pstContent */
        private $pstContent = null;
 
+       /** @var bool */
+       private $contentIsDeleted = false, $contentIsSuppressed = false;
+
        public function execute() {
                // The data is hot but user-dependent, like page views, so we set vary cookies
                $this->getMain()->setCacheMode( 'anon-public-user-private' );
@@ -45,10 +48,11 @@ class ApiParse extends ApiBase {
                // Get parameters
                $params = $this->extractRequestParams();
 
-               // No easy way to say that text & title are allowed together while the
-               // rest aren't, so just do it in two calls.
+               // No easy way to say that text and title or revid are allowed together
+               // while the rest aren't, so just do it in three calls.
                $this->requireMaxOneParameter( $params, 'page', 'pageid', 'oldid', 'text' );
                $this->requireMaxOneParameter( $params, 'page', 'pageid', 'oldid', 'title' );
+               $this->requireMaxOneParameter( $params, 'page', 'pageid', 'oldid', 'revid' );
 
                $text = $params['text'];
                $title = $params['title'];
@@ -85,6 +89,9 @@ class ApiParse extends ApiBase {
 
                $redirValues = null;
 
+               $needContent = isset( $prop['wikitext'] ) ||
+                       isset( $prop['parsetree'] ) || $params['generatexml'];
+
                // Return result
                $result = $this->getResult();
 
@@ -110,27 +117,9 @@ class ApiParse extends ApiBase {
                                $wgTitle = $titleObj;
                                $pageObj = WikiPage::factory( $titleObj );
                                list( $popts, $reset, $suppressCache ) = $this->makeParserOptions( $pageObj, $params );
-
-                               // If for some reason the "oldid" is actually the current revision, it may be cached
-                               // Deliberately comparing $pageObj->getLatest() with $rev->getId(), rather than
-                               // checking $rev->isCurrent(), because $pageObj is what actually ends up being used,
-                               // and if its ->getLatest() is outdated, $rev->isCurrent() won't tell us that.
-                               if ( !$suppressCache && $rev->getId() == $pageObj->getLatest() ) {
-                                       // May get from/save to parser cache
-                                       $p_result = $this->getParsedContent( $pageObj, $popts,
-                                               $pageid, isset( $prop['wikitext'] ) );
-                               } else { // This is an old revision, so get the text differently
-                                       $this->content = $rev->getContent( Revision::FOR_THIS_USER, $this->getUser() );
-
-                                       if ( $this->section !== false ) {
-                                               $this->content = $this->getSectionContent(
-                                                       $this->content, $this->msg( 'revid', $rev->getId() )
-                                               );
-                                       }
-
-                                       // Should we save old revision parses to the parser cache?
-                                       $p_result = $this->content->getParserOutput( $titleObj, $rev->getId(), $popts );
-                               }
+                               $p_result = $this->getParsedContent(
+                                       $pageObj, $popts, $suppressCache, $pageid, $rev, $needContent
+                               );
                        } else { // Not $oldid, but $pageid or $page
                                if ( $params['redirects'] ) {
                                        $reqParams = [
@@ -172,31 +161,34 @@ class ApiParse extends ApiBase {
                                }
 
                                list( $popts, $reset, $suppressCache ) = $this->makeParserOptions( $pageObj, $params );
-
-                               // Don't pollute the parser cache when setting options that aren't
-                               // in ParserOptions::optionsHash()
-                               /// @todo: This should be handled closer to the actual cache instead of here, see T110269
-                               $suppressCache = $suppressCache ||
-                                       $params['disablepp'] ||
-                                       $params['disablelimitreport'] ||
-                                       $params['preview'] ||
-                                       $params['sectionpreview'] ||
-                                       $params['disabletidy'];
-
-                               if ( $suppressCache ) {
-                                       $this->content = $this->getContent( $pageObj, $pageid );
-                                       $p_result = $this->content->getParserOutput( $titleObj, null, $popts );
-                               } else {
-                                       // Potentially cached
-                                       $p_result = $this->getParsedContent( $pageObj, $popts, $pageid,
-                                               isset( $prop['wikitext'] ) );
-                               }
+                               $p_result = $this->getParsedContent(
+                                       $pageObj, $popts, $suppressCache, $pageid, null, $needContent
+                               );
                        }
                } else { // Not $oldid, $pageid, $page. Hence based on $text
                        $titleObj = Title::newFromText( $title );
                        if ( !$titleObj || $titleObj->isExternal() ) {
                                $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $title ) ] );
                        }
+                       $revid = $params['revid'];
+                       if ( $revid !== null ) {
+                               $rev = Revision::newFromId( $revid );
+                               if ( !$rev ) {
+                                       $this->dieWithError( [ 'apierror-nosuchrevid', $revid ] );
+                               }
+                               $pTitleObj = $titleObj;
+                               $titleObj = $rev->getTitle();
+                               if ( $titleProvided ) {
+                                       if ( !$titleObj->equals( $pTitleObj ) ) {
+                                               $this->addWarning( [ 'apierror-revwrongpage', $rev->getId(),
+                                                       wfEscapeWikiText( $pTitleObj->getPrefixedText() ) ] );
+                                       }
+                               } else {
+                                       // Consider the title derived from the revid as having
+                                       // been provided.
+                                       $titleProvided = true;
+                               }
+                       }
                        $wgTitle = $titleObj;
                        if ( $titleObj->canExist() ) {
                                $pageObj = WikiPage::factory( $titleObj );
@@ -211,7 +203,11 @@ class ApiParse extends ApiBase {
 
                        if ( !$textProvided ) {
                                if ( $titleProvided && ( $prop || $params['generatexml'] ) ) {
-                                       $this->addWarning( 'apiwarn-parse-titlewithouttext' );
+                                       if ( $revid !== null ) {
+                                               $this->addWarning( 'apiwarn-parse-revidwithouttext' );
+                                       } else {
+                                               $this->addWarning( 'apiwarn-parse-titlewithouttext' );
+                                       }
                                }
                                // Prevent warning from ContentHandler::makeContent()
                                $text = '';
@@ -249,6 +245,12 @@ class ApiParse extends ApiBase {
                        if ( $params['onlypst'] ) {
                                // Build a result and bail out
                                $result_array = [];
+                               if ( $this->contentIsDeleted ) {
+                                       $result_array['textdeleted'] = true;
+                               }
+                               if ( $this->contentIsSuppressed ) {
+                                       $result_array['textsuppressed'] = true;
+                               }
                                $result_array['text'] = $this->pstContent->serialize( $format );
                                $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'text';
                                if ( isset( $prop['wikitext'] ) ) {
@@ -269,9 +271,9 @@ class ApiParse extends ApiBase {
 
                        // Not cached (save or load)
                        if ( $params['pst'] ) {
-                               $p_result = $this->pstContent->getParserOutput( $titleObj, null, $popts );
+                               $p_result = $this->pstContent->getParserOutput( $titleObj, $revid, $popts );
                        } else {
-                               $p_result = $this->content->getParserOutput( $titleObj, null, $popts );
+                               $p_result = $this->content->getParserOutput( $titleObj, $revid, $popts );
                        }
                }
 
@@ -279,6 +281,12 @@ class ApiParse extends ApiBase {
 
                $result_array['title'] = $titleObj->getPrefixedText();
                $result_array['pageid'] = $pageid ?: $pageObj->getId();
+               if ( $this->contentIsDeleted ) {
+                       $result_array['textdeleted'] = true;
+               }
+               if ( $this->contentIsSuppressed ) {
+                       $result_array['textsuppressed'] = true;
+               }
 
                if ( $params['disabletoc'] ) {
                        $p_result->setTOCEnabled( false );
@@ -401,7 +409,6 @@ class ApiParse extends ApiBase {
                        } else {
                                $result_array['headitems'] = $this->formatHeadItems( $p_result->getHeadItems() );
                        }
-                       $this->addDeprecation( 'apiwarn-deprecation-parse-headitems', 'action=parse&prop=headitems' );
                }
 
                if ( isset( $prop['headhtml'] ) ) {
@@ -541,64 +548,72 @@ class ApiParse extends ApiBase {
                Hooks::run( 'ApiMakeParserOptions',
                        [ $popts, $pageObj->getTitle(), $params, $this, &$reset, &$suppressCache ] );
 
+               // Force cache suppression when $popts aren't cacheable.
+               $suppressCache = $suppressCache || !$popts->isSafeToCache();
+
                return [ $popts, $reset, $suppressCache ];
        }
 
        /**
         * @param WikiPage $page
         * @param ParserOptions $popts
+        * @param bool $suppressCache
         * @param int $pageId
-        * @param bool $getWikitext
+        * @param Revision|null $rev
+        * @param bool $getContent
         * @return ParserOutput
         */
-       private function getParsedContent( WikiPage $page, $popts, $pageId = null, $getWikitext = false ) {
-               $this->content = $this->getContent( $page, $pageId );
+       private function getParsedContent(
+               WikiPage $page, $popts, $suppressCache, $pageId, $rev, $getContent
+       ) {
+               $revId = $rev ? $rev->getId() : null;
+               $isDeleted = $rev && $rev->isDeleted( Revision::DELETED_TEXT );
+
+               if ( $getContent || $this->section !== false || $isDeleted ) {
+                       if ( $rev ) {
+                               $this->content = $rev->getContent( Revision::FOR_THIS_USER, $this->getUser() );
+                               if ( !$this->content ) {
+                                       $this->dieWithError( [ 'apierror-missingcontent-revid', $revId ] );
+                               }
+                       } else {
+                               $this->content = $page->getContent( Revision::FOR_THIS_USER, $this->getUser() );
+                               if ( !$this->content ) {
+                                       $this->dieWithError( [ 'apierror-missingcontent-pageid', $pageId ] );
+                               }
+                       }
+                       $this->contentIsDeleted = $isDeleted;
+                       $this->contentIsSuppressed = $rev &&
+                               $rev->isDeleted( Revision::DELETED_TEXT | Revision::DELETED_RESTRICTED );
+               }
 
-               if ( $this->section !== false && $this->content !== null ) {
-                       // Not cached (save or load)
-                       return $this->content->getParserOutput( $page->getTitle(), null, $popts );
+               if ( $this->section !== false ) {
+                       $this->content = $this->getSectionContent(
+                               $this->content,
+                               $pageId === null ? $page->getTitle()->getPrefixedText() : $this->msg( 'pageid', $pageId )
+                       );
+                       return $this->content->getParserOutput( $page->getTitle(), $revId, $popts );
                }
 
-               // Try the parser cache first
-               // getParserOutput will save to Parser cache if able
-               $pout = $page->getParserOutput( $popts );
-               if ( !$pout ) {
-                       $this->dieWithError( [ 'apierror-nosuchrevid', $page->getLatest() ] );
+               if ( $isDeleted ) {
+                       // getParserOutput can't do revdeled revisions
+                       $pout = $this->content->getParserOutput( $page->getTitle(), $revId, $popts );
+               } else {
+                       // getParserOutput will save to Parser cache if able
+                       $pout = $page->getParserOutput( $popts, $revId, $suppressCache );
                }
-               if ( $getWikitext ) {
-                       $this->content = $page->getContent( Revision::RAW );
+               if ( !$pout ) {
+                       $this->dieWithError( [ 'apierror-nosuchrevid', $revId ?: $page->getLatest() ] );
                }
 
                return $pout;
        }
 
-       /**
-        * Get the content for the given page and the requested section.
-        *
-        * @param WikiPage $page
-        * @param int $pageId
-        * @return Content
-        */
-       private function getContent( WikiPage $page, $pageId = null ) {
-               $content = $page->getContent( Revision::RAW ); // XXX: really raw?
-
-               if ( $this->section !== false && $content !== null ) {
-                       $content = $this->getSectionContent(
-                               $content,
-                               !is_null( $pageId )
-                                       ? $this->msg( 'pageid', $pageId )
-                                       : $page->getTitle()->getPrefixedText()
-                       );
-               }
-               return $content;
-       }
-
        /**
         * Extract the requested section from the given Content
         *
         * @param Content $content
         * @param string|Message $what Identifies the content in error messages, e.g. page title.
-        * @return Content|bool
+        * @return Content
         */
        private function getSectionContent( Content $content, $what ) {
                // Not cached (save or load)
@@ -794,6 +809,9 @@ class ApiParse extends ApiBase {
                        'text' => [
                                ApiBase::PARAM_TYPE => 'text',
                        ],
+                       'revid' => [
+                               ApiBase::PARAM_TYPE => 'integer',
+                       ],
                        'summary' => null,
                        'page' => null,
                        'pageid' => [
@@ -820,7 +838,6 @@ class ApiParse extends ApiBase {
                                        'sections',
                                        'revid',
                                        'displaytitle',
-                                       'headitems',
                                        'headhtml',
                                        'modules',
                                        'jsconfigvars',
@@ -832,11 +849,15 @@ class ApiParse extends ApiBase {
                                        'limitreportdata',
                                        'limitreporthtml',
                                        'parsetree',
-                                       'parsewarnings'
+                                       'parsewarnings',
+                                       'headitems',
                                ],
                                ApiBase::PARAM_HELP_MSG_PER_VALUE => [
                                        'parsetree' => [ 'apihelp-parse-paramvalue-prop-parsetree', CONTENT_MODEL_WIKITEXT ],
                                ],
+                               ApiBase::PARAM_DEPRECATED_VALUES => [
+                                       'headitems' => 'apiwarn-deprecation-parse-headitems',
+                               ],
                        ],
                        'wrapoutputclass' => 'mw-parser-output',
                        'pst' => false,