Fix-up for I2ac604d3c042d
[lhc/web/wiklou.git] / includes / api / ApiExpandTemplates.php
index 8a3b534..6112534 100644 (file)
@@ -52,10 +52,19 @@ class ApiExpandTemplates extends ApiBase {
                        $prop = array_flip( $params['prop'] );
                }
 
-               // Create title for parser
-               $title_obj = Title::newFromText( $params['title'] );
-               if ( !$title_obj || $title_obj->isExternal() ) {
-                       $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
+               // Get title and revision ID for parser
+               $revid = $params['revid'];
+               if ( $revid !== null ) {
+                       $rev = Revision::newFromId( $revid );
+                       if ( !$rev ) {
+                               $this->dieUsage( "There is no revision ID $revid", 'missingrev' );
+                       }
+                       $title_obj = $rev->getTitle();
+               } else {
+                       $title_obj = Title::newFromText( $params['title'] );
+                       if ( !$title_obj || $title_obj->isExternal() ) {
+                               $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
+                       }
                }
 
                $result = $this->getResult();
@@ -75,7 +84,7 @@ class ApiExpandTemplates extends ApiBase {
                                $this->logFeatureUsage( 'action=expandtemplates&generatexml' );
                        }
 
-                       $wgParser->startExternalParse( $title_obj, $options, OT_PREPROCESS );
+                       $wgParser->startExternalParse( $title_obj, $options, Parser::OT_PREPROCESS );
                        $dom = $wgParser->preprocessToDom( $params['text'] );
                        if ( is_callable( array( $dom, 'saveXML' ) ) ) {
                                $xml = $dom->saveXML();
@@ -87,38 +96,46 @@ class ApiExpandTemplates extends ApiBase {
                                $retval['parsetree'] = $xml;
                        } else {
                                // the old way
-                               $xml_result = array();
-                               ApiResult::setContent( $xml_result, $xml );
-                               $result->addValue( null, 'parsetree', $xml_result );
+                               $result->addValue( null, 'parsetree', $xml );
+                               $result->addValue( null, ApiResult::META_BC_SUBELEMENTS, array( 'parsetree' ) );
                        }
                }
 
                // if they didn't want any output except (probably) the parse tree,
                // then don't bother actually fully expanding it
                if ( $prop || $params['prop'] === null ) {
-                       $wgParser->startExternalParse( $title_obj, $options, OT_PREPROCESS );
+                       $wgParser->startExternalParse( $title_obj, $options, Parser::OT_PREPROCESS );
                        $frame = $wgParser->getPreprocessor()->newFrame();
-                       $wikitext = $wgParser->preprocess( $params['text'], $title_obj, $options, null, $frame );
+                       $wikitext = $wgParser->preprocess( $params['text'], $title_obj, $options, $revid, $frame );
                        if ( $params['prop'] === null ) {
                                // the old way
-                               ApiResult::setContent( $retval, $wikitext );
+                               ApiResult::setContentValue( $retval, 'wikitext', $wikitext );
                        } else {
+                               $p_output = $wgParser->getOutput();
                                if ( isset( $prop['categories'] ) ) {
-                                       $categories = $wgParser->getOutput()->getCategories();
-                                       if ( !empty( $categories ) ) {
+                                       $categories = $p_output->getCategories();
+                                       if ( $categories ) {
                                                $categories_result = array();
                                                foreach ( $categories as $category => $sortkey ) {
                                                        $entry = array();
                                                        $entry['sortkey'] = $sortkey;
-                                                       ApiResult::setContent( $entry, $category );
+                                                       ApiResult::setContentValue( $entry, 'category', $category );
                                                        $categories_result[] = $entry;
                                                }
-                                               $result->setIndexedTagName( $categories_result, 'category' );
+                                               ApiResult::setIndexedTagName( $categories_result, 'category' );
                                                $retval['categories'] = $categories_result;
                                        }
                                }
-                               if ( isset( $prop['volatile'] ) && $frame->isVolatile() ) {
-                                       $retval['volatile'] = '';
+                               if ( isset( $prop['properties'] ) ) {
+                                       $properties = $p_output->getProperties();
+                                       if ( $properties ) {
+                                               ApiResult::setArrayType( $properties, 'BCkvp', 'name' );
+                                               ApiResult::setIndexedTagName( $properties, 'property' );
+                                               $retval['properties'] = $properties;
+                                       }
+                               }
+                               if ( isset( $prop['volatile'] ) ) {
+                                       $retval['volatile'] = $frame->isVolatile();
                                }
                                if ( isset( $prop['ttl'] ) && $frame->getTTL() !== null ) {
                                        $retval['ttl'] = $frame->getTTL();
@@ -126,9 +143,30 @@ class ApiExpandTemplates extends ApiBase {
                                if ( isset( $prop['wikitext'] ) ) {
                                        $retval['wikitext'] = $wikitext;
                                }
+                               if ( isset( $prop['modules'] ) ) {
+                                       $retval['modules'] = array_values( array_unique( $p_output->getModules() ) );
+                                       $retval['modulescripts'] = array_values( array_unique( $p_output->getModuleScripts() ) );
+                                       $retval['modulestyles'] = array_values( array_unique( $p_output->getModuleStyles() ) );
+                               }
+                               if ( isset( $prop['jsconfigvars'] ) ) {
+                                       $retval['jsconfigvars'] =
+                                               ApiResult::addMetadataToResultVars( $p_output->getJsConfigVars() );
+                               }
+                               if ( isset( $prop['encodedjsconfigvars'] ) ) {
+                                       $retval['encodedjsconfigvars'] = FormatJson::encode(
+                                               $p_output->getJsConfigVars(), false, FormatJson::ALL_OK
+                                       );
+                                       $retval[ApiResult::META_SUBELEMENTS][] = 'encodedjsconfigvars';
+                               }
+                               if ( isset( $prop['modules'] ) &&
+                                       !isset( $prop['jsconfigvars'] ) && !isset( $prop['encodedjsconfigvars'] ) ) {
+                                       $this->setWarning( "Property 'modules' was set but not 'jsconfigvars' " .
+                                               "or 'encodedjsconfigvars'. Configuration variables are necessary " .
+                                               "for proper module usage." );
+                               }
                        }
                }
-               $result->setSubelements( $retval, array( 'wikitext', 'parsetree' ) );
+               ApiResult::setSubelementsList( $retval, array( 'wikitext', 'parsetree' ) );
                $result->addValue( null, $this->getModuleName(), $retval );
        }
 
@@ -138,18 +176,26 @@ class ApiExpandTemplates extends ApiBase {
                                ApiBase::PARAM_DFLT => 'API',
                        ),
                        'text' => array(
-                               ApiBase::PARAM_TYPE => 'string',
+                               ApiBase::PARAM_TYPE => 'text',
                                ApiBase::PARAM_REQUIRED => true,
                        ),
+                       'revid' => array(
+                               ApiBase::PARAM_TYPE => 'integer',
+                       ),
                        'prop' => array(
                                ApiBase::PARAM_TYPE => array(
                                        'wikitext',
                                        'categories',
+                                       'properties',
                                        'volatile',
                                        'ttl',
+                                       'modules',
+                                       'jsconfigvars',
+                                       'encodedjsconfigvars',
                                        'parsetree',
                                ),
                                ApiBase::PARAM_ISMULTI => true,
+                               ApiBase::PARAM_HELP_MSG_PER_VALUE => array(),
                        ),
                        'includecomments' => false,
                        'generatexml' => array(
@@ -159,35 +205,10 @@ class ApiExpandTemplates extends ApiBase {
                );
        }
 
-       public function getParamDescription() {
-               return array(
-                       'text' => 'Wikitext to convert',
-                       'title' => 'Title of page',
-                       'prop' => array(
-                               'Which pieces of information to get',
-                               ' wikitext   - The expanded wikitext',
-                               ' categories - Any categories present in the input that are not represented in ' .
-                                       'the wikitext output',
-                               ' volatile   - Whether the output is volatile and should not be reused ' .
-                                       'elsewhere within the page',
-                               ' ttl        - The maximum time after which caches of the result should be ' .
-                                       'invalidated',
-                               ' parsetree  - The XML parse tree of the input',
-                               'Note that if no values are selected, the result will contain the wikitext,',
-                               'but the output will be in a deprecated format.',
-                       ),
-                       'includecomments' => 'Whether to include HTML comments in the output',
-                       'generatexml' => 'Generate XML parse tree (replaced by prop=parsetree)',
-               );
-       }
-
-       public function getDescription() {
-               return 'Expands all templates in wikitext.';
-       }
-
-       public function getExamples() {
+       protected function getExamplesMessages() {
                return array(
-                       'api.php?action=expandtemplates&text={{Project:Sandbox}}'
+                       'action=expandtemplates&text={{Project:Sandbox}}'
+                               => 'apihelp-expandtemplates-example-simple',
                );
        }