5 * Created on Oct 05, 2007
7 * Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
28 * API module that functions as a shortcut to the wikitext preprocessor. Expands
29 * any templates in a provided string, and returns the result of this expansion
34 class ApiExpandTemplates
extends ApiBase
{
36 public function execute() {
37 // Cache may vary on $wgUser because ParserOptions gets data from it
38 $this->getMain()->setCacheMode( 'anon-public-user-private' );
41 $params = $this->extractRequestParams();
42 $this->requireMaxOneParameter( $params, 'prop', 'generatexml' );
44 if ( $params['prop'] === null ) {
45 $this->logFeatureUsage( 'action=expandtemplates&!prop' );
46 $this->setWarning( 'Because no values have been specified for the prop parameter, a ' .
47 'legacy format has been used for the output. This format is deprecated, and in ' .
48 'the future, a default value will be set for the prop parameter, causing the new' .
49 'format to always be used.' );
52 $prop = array_flip( $params['prop'] );
55 // Get title and revision ID for parser
56 $revid = $params['revid'];
57 if ( $revid !== null ) {
58 $rev = Revision
::newFromId( $revid );
60 $this->dieUsage( "There is no revision ID $revid", 'missingrev' );
62 $title_obj = $rev->getTitle();
64 $title_obj = Title
::newFromText( $params['title'] );
65 if ( !$title_obj ||
$title_obj->isExternal() ) {
66 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
70 $result = $this->getResult();
74 $options = ParserOptions
::newFromContext( $this->getContext() );
76 if ( $params['includecomments'] ) {
77 $options->setRemoveComments( false );
82 if ( isset( $prop['parsetree'] ) ||
$params['generatexml'] ) {
83 if ( !isset( $prop['parsetree'] ) ) {
84 $this->logFeatureUsage( 'action=expandtemplates&generatexml' );
87 $wgParser->startExternalParse( $title_obj, $options, Parser
::OT_PREPROCESS
);
88 $dom = $wgParser->preprocessToDom( $params['text'] );
89 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
90 $xml = $dom->saveXML();
92 $xml = $dom->__toString();
94 if ( isset( $prop['parsetree'] ) ) {
95 unset( $prop['parsetree'] );
96 $retval['parsetree'] = $xml;
99 $result->addValue( null, 'parsetree', $xml );
100 $result->addValue( null, ApiResult
::META_BC_SUBELEMENTS
, array( 'parsetree' ) );
104 // if they didn't want any output except (probably) the parse tree,
105 // then don't bother actually fully expanding it
106 if ( $prop ||
$params['prop'] === null ) {
107 $wgParser->startExternalParse( $title_obj, $options, Parser
::OT_PREPROCESS
);
108 $frame = $wgParser->getPreprocessor()->newFrame();
109 $wikitext = $wgParser->preprocess( $params['text'], $title_obj, $options, $revid, $frame );
110 if ( $params['prop'] === null ) {
112 ApiResult
::setContentValue( $retval, 'wikitext', $wikitext );
114 $p_output = $wgParser->getOutput();
115 if ( isset( $prop['categories'] ) ) {
116 $categories = $p_output->getCategories();
118 $categories_result = array();
119 foreach ( $categories as $category => $sortkey ) {
121 $entry['sortkey'] = $sortkey;
122 ApiResult
::setContentValue( $entry, 'category', $category );
123 $categories_result[] = $entry;
125 ApiResult
::setIndexedTagName( $categories_result, 'category' );
126 $retval['categories'] = $categories_result;
129 if ( isset( $prop['properties'] ) ) {
130 $properties = $p_output->getProperties();
132 ApiResult
::setArrayType( $properties, 'BCkvp', 'name' );
133 ApiResult
::setIndexedTagName( $properties, 'property' );
134 $retval['properties'] = $properties;
137 if ( isset( $prop['volatile'] ) ) {
138 $retval['volatile'] = $frame->isVolatile();
140 if ( isset( $prop['ttl'] ) && $frame->getTTL() !== null ) {
141 $retval['ttl'] = $frame->getTTL();
143 if ( isset( $prop['wikitext'] ) ) {
144 $retval['wikitext'] = $wikitext;
146 if ( isset( $prop['modules'] ) ) {
147 $retval['modules'] = array_values( array_unique( $p_output->getModules() ) );
148 $retval['modulescripts'] = array_values( array_unique( $p_output->getModuleScripts() ) );
149 $retval['modulestyles'] = array_values( array_unique( $p_output->getModuleStyles() ) );
151 if ( isset( $prop['jsconfigvars'] ) ) {
152 $retval['jsconfigvars'] =
153 ApiResult
::addMetadataToResultVars( $p_output->getJsConfigVars() );
155 if ( isset( $prop['encodedjsconfigvars'] ) ) {
156 $retval['encodedjsconfigvars'] = FormatJson
::encode(
157 $p_output->getJsConfigVars(), false, FormatJson
::ALL_OK
159 $retval[ApiResult
::META_SUBELEMENTS
][] = 'encodedjsconfigvars';
161 if ( isset( $prop['modules'] ) &&
162 !isset( $prop['jsconfigvars'] ) && !isset( $prop['encodedjsconfigvars'] ) ) {
163 $this->setWarning( "Property 'modules' was set but not 'jsconfigvars' " .
164 "or 'encodedjsconfigvars'. Configuration variables are necessary " .
165 "for proper module usage." );
169 ApiResult
::setSubelementsList( $retval, array( 'wikitext', 'parsetree' ) );
170 $result->addValue( null, $this->getModuleName(), $retval );
173 public function getAllowedParams() {
176 ApiBase
::PARAM_DFLT
=> 'API',
179 ApiBase
::PARAM_TYPE
=> 'text',
180 ApiBase
::PARAM_REQUIRED
=> true,
183 ApiBase
::PARAM_TYPE
=> 'integer',
186 ApiBase
::PARAM_TYPE
=> array(
194 'encodedjsconfigvars',
197 ApiBase
::PARAM_ISMULTI
=> true,
198 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> array(),
200 'includecomments' => false,
201 'generatexml' => array(
202 ApiBase
::PARAM_TYPE
=> 'boolean',
203 ApiBase
::PARAM_DEPRECATED
=> true,
208 protected function getExamplesMessages() {
210 'action=expandtemplates&text={{Project:Sandbox}}'
211 => 'apihelp-expandtemplates-example-simple',
215 public function getHelpUrls() {
216 return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#expandtemplates';