Merge "ParserTest: clear Language namespaces cache"
[lhc/web/wiklou.git] / includes / api / ApiParse.php
index 031fbf7..cbd62a9 100644 (file)
@@ -1,7 +1,5 @@
 <?php
 /**
- * Created on Dec 01, 2007
- *
  * Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
  *
  * This program is free software; you can redistribute it and/or modify
@@ -48,10 +46,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'];
@@ -169,6 +168,25 @@ class ApiParse extends ApiBase {
                        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 );
@@ -183,7 +201,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 = '';
@@ -247,9 +269,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 );
                        }
                }
 
@@ -264,10 +286,6 @@ class ApiParse extends ApiBase {
                        $result_array['textsuppressed'] = true;
                }
 
-               if ( $params['disabletoc'] ) {
-                       $p_result->setTOCEnabled( false );
-               }
-
                if ( isset( $params['useskin'] ) ) {
                        $factory = MediaWikiServices::getInstance()->getSkinFactory();
                        $skin = $factory->makeSkin( Skin::normalizeKey( $params['useskin'] ) );
@@ -305,6 +323,8 @@ class ApiParse extends ApiBase {
                        $context->setOutput( $outputPage );
 
                        if ( $skin ) {
+                               // Based on OutputPage::headElement()
+                               $skin->setupSkinUserCss( $outputPage );
                                // Based on OutputPage::output()
                                foreach ( $skin->getDefaultModules() as $group ) {
                                        $outputPage->addModules( $group );
@@ -321,7 +341,12 @@ class ApiParse extends ApiBase {
                }
 
                if ( isset( $prop['text'] ) ) {
-                       $result_array['text'] = $p_result->getText();
+                       $result_array['text'] = $p_result->getText( [
+                               'allowTOC' => !$params['disabletoc'],
+                               'enableSectionEditLinks' => !$params['disableeditsection'],
+                               'unwrap' => $params['wrapoutputclass'] === '',
+                               'deduplicateStyles' => !$params['disablestylededuplication'],
+                       ] );
                        $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'text';
                }
 
@@ -511,13 +536,12 @@ class ApiParse extends ApiBase {
                $popts->enableLimitReport( !$params['disablepp'] && !$params['disablelimitreport'] );
                $popts->setIsPreview( $params['preview'] || $params['sectionpreview'] );
                $popts->setIsSectionPreview( $params['sectionpreview'] );
-               $popts->setEditSection( !$params['disableeditsection'] );
                if ( $params['disabletidy'] ) {
                        $popts->setTidy( false );
                }
-               $popts->setWrapOutputClass(
-                       $params['wrapoutputclass'] === '' ? false : $params['wrapoutputclass']
-               );
+               if ( $params['wrapoutputclass'] !== '' ) {
+                       $popts->setWrapOutputClass( $params['wrapoutputclass'] );
+               }
 
                $reset = null;
                $suppressCache = false;
@@ -785,6 +809,9 @@ class ApiParse extends ApiBase {
                        'text' => [
                                ApiBase::PARAM_TYPE => 'text',
                        ],
+                       'revid' => [
+                               ApiBase::PARAM_TYPE => 'integer',
+                       ],
                        'summary' => null,
                        'page' => null,
                        'pageid' => [
@@ -850,6 +877,7 @@ class ApiParse extends ApiBase {
                        'disablelimitreport' => false,
                        'disableeditsection' => false,
                        'disabletidy' => false,
+                       'disablestylededuplication' => false,
                        'generatexml' => [
                                ApiBase::PARAM_DFLT => false,
                                ApiBase::PARAM_HELP_MSG => [