3 * Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use MediaWiki\MediaWikiServices
;
26 * API module that functions as a shortcut to the wikitext preprocessor. Expands
27 * any templates in a provided string, and returns the result of this expansion
32 class ApiExpandTemplates
extends ApiBase
{
34 public function execute() {
35 // Cache may vary on the user because ParserOptions gets data from it
36 $this->getMain()->setCacheMode( 'anon-public-user-private' );
39 $params = $this->extractRequestParams();
40 $this->requireMaxOneParameter( $params, 'prop', 'generatexml' );
42 $title = $params['title'];
43 if ( $title === null ) {
44 $titleProvided = false;
45 // A title is needed for parsing, so arbitrarily choose one
48 $titleProvided = true;
51 if ( $params['prop'] === null ) {
52 $this->addDeprecation(
53 [ 'apiwarn-deprecation-missingparam', 'prop' ], 'action=expandtemplates&!prop'
57 $prop = array_flip( $params['prop'] );
60 $titleObj = Title
::newFromText( $title );
61 if ( !$titleObj ||
$titleObj->isExternal() ) {
62 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
65 // Get title and revision ID for parser
66 $revid = $params['revid'];
67 if ( $revid !== null ) {
68 $rev = MediaWikiServices
::getInstance()->getRevisionStore()->getRevisionById( $revid );
70 $this->dieWithError( [ 'apierror-nosuchrevid', $revid ] );
72 $pTitleObj = $titleObj;
73 $titleObj = Title
::newFromLinkTarget( $rev->getPageAsLinkTarget() );
74 if ( $titleProvided ) {
75 if ( !$titleObj->equals( $pTitleObj ) ) {
76 $this->addWarning( [ 'apierror-revwrongpage', $rev->getId(),
77 wfEscapeWikiText( $pTitleObj->getPrefixedText() ) ] );
82 $result = $this->getResult();
85 $options = ParserOptions
::newFromContext( $this->getContext() );
87 if ( $params['includecomments'] ) {
88 $options->setRemoveComments( false );
92 $suppressCache = false;
93 Hooks
::run( 'ApiMakeParserOptions',
94 [ $options, $titleObj, $params, $this, &$reset, &$suppressCache ] );
98 $parser = MediaWikiServices
::getInstance()->getParser();
99 if ( isset( $prop['parsetree'] ) ||
$params['generatexml'] ) {
100 $parser->startExternalParse( $titleObj, $options, Parser
::OT_PREPROCESS
);
101 $dom = $parser->preprocessToDom( $params['text'] );
102 // @phan-suppress-next-line PhanUndeclaredMethodInCallable
103 if ( is_callable( [ $dom, 'saveXML' ] ) ) {
104 // @phan-suppress-next-line PhanUndeclaredMethod
105 $xml = $dom->saveXML();
107 // @phan-suppress-next-line PhanUndeclaredMethod
108 $xml = $dom->__toString();
110 if ( isset( $prop['parsetree'] ) ) {
111 unset( $prop['parsetree'] );
112 $retval['parsetree'] = $xml;
115 $result->addValue( null, 'parsetree', $xml );
116 $result->addValue( null, ApiResult
::META_BC_SUBELEMENTS
, [ 'parsetree' ] );
120 // if they didn't want any output except (probably) the parse tree,
121 // then don't bother actually fully expanding it
122 if ( $prop ||
$params['prop'] === null ) {
123 $parser->startExternalParse( $titleObj, $options, Parser
::OT_PREPROCESS
);
124 $frame = $parser->getPreprocessor()->newFrame();
125 $wikitext = $parser->preprocess( $params['text'], $titleObj, $options, $revid, $frame );
126 if ( $params['prop'] === null ) {
128 ApiResult
::setContentValue( $retval, 'wikitext', $wikitext );
130 $p_output = $parser->getOutput();
131 if ( isset( $prop['categories'] ) ) {
132 $categories = $p_output->getCategories();
134 $categories_result = [];
135 foreach ( $categories as $category => $sortkey ) {
137 $entry['sortkey'] = $sortkey;
138 ApiResult
::setContentValue( $entry, 'category', (string)$category );
139 $categories_result[] = $entry;
141 ApiResult
::setIndexedTagName( $categories_result, 'category' );
142 $retval['categories'] = $categories_result;
145 if ( isset( $prop['properties'] ) ) {
146 $properties = $p_output->getProperties();
148 ApiResult
::setArrayType( $properties, 'BCkvp', 'name' );
149 ApiResult
::setIndexedTagName( $properties, 'property' );
150 $retval['properties'] = $properties;
153 if ( isset( $prop['volatile'] ) ) {
154 $retval['volatile'] = $frame->isVolatile();
156 if ( isset( $prop['ttl'] ) && $frame->getTTL() !== null ) {
157 $retval['ttl'] = $frame->getTTL();
159 if ( isset( $prop['wikitext'] ) ) {
160 $retval['wikitext'] = $wikitext;
162 if ( isset( $prop['modules'] ) ) {
163 $retval['modules'] = array_values( array_unique( $p_output->getModules() ) );
164 // Deprecated since 1.32 (T188689)
165 $retval['modulescripts'] = [];
166 $retval['modulestyles'] = array_values( array_unique( $p_output->getModuleStyles() ) );
168 if ( isset( $prop['jsconfigvars'] ) ) {
169 $retval['jsconfigvars'] =
170 ApiResult
::addMetadataToResultVars( $p_output->getJsConfigVars() );
172 if ( isset( $prop['encodedjsconfigvars'] ) ) {
173 $retval['encodedjsconfigvars'] = FormatJson
::encode(
174 $p_output->getJsConfigVars(), false, FormatJson
::ALL_OK
176 $retval[ApiResult
::META_SUBELEMENTS
][] = 'encodedjsconfigvars';
178 if ( isset( $prop['modules'] ) &&
179 !isset( $prop['jsconfigvars'] ) && !isset( $prop['encodedjsconfigvars'] ) ) {
180 $this->addWarning( 'apiwarn-moduleswithoutvars' );
184 ApiResult
::setSubelementsList( $retval, [ 'wikitext', 'parsetree' ] );
185 $result->addValue( null, $this->getModuleName(), $retval );
188 public function getAllowedParams() {
192 ApiBase
::PARAM_TYPE
=> 'text',
193 ApiBase
::PARAM_REQUIRED
=> true,
196 ApiBase
::PARAM_TYPE
=> 'integer',
199 ApiBase
::PARAM_TYPE
=> [
207 'encodedjsconfigvars',
210 ApiBase
::PARAM_ISMULTI
=> true,
211 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
213 'includecomments' => false,
215 ApiBase
::PARAM_TYPE
=> 'boolean',
216 ApiBase
::PARAM_DEPRECATED
=> true,
221 protected function getExamplesMessages() {
223 'action=expandtemplates&text={{Project:Sandbox}}'
224 => 'apihelp-expandtemplates-example-simple',
228 public function getHelpUrls() {
229 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Parsing_wikitext#expandtemplates';