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() ) ] );
80 // Consider the title derived from the revid as having
82 $titleProvided = true;
86 $result = $this->getResult();
90 $options = ParserOptions
::newFromContext( $this->getContext() );
92 if ( $params['includecomments'] ) {
93 $options->setRemoveComments( false );
97 $suppressCache = false;
98 Hooks
::run( 'ApiMakeParserOptions',
99 [ $options, $titleObj, $params, $this, &$reset, &$suppressCache ] );
103 if ( isset( $prop['parsetree'] ) ||
$params['generatexml'] ) {
104 $wgParser->startExternalParse( $titleObj, $options, Parser
::OT_PREPROCESS
);
105 $dom = $wgParser->preprocessToDom( $params['text'] );
106 if ( is_callable( [ $dom, 'saveXML' ] ) ) {
107 $xml = $dom->saveXML();
109 $xml = $dom->__toString();
111 if ( isset( $prop['parsetree'] ) ) {
112 unset( $prop['parsetree'] );
113 $retval['parsetree'] = $xml;
116 $result->addValue( null, 'parsetree', $xml );
117 $result->addValue( null, ApiResult
::META_BC_SUBELEMENTS
, [ 'parsetree' ] );
121 // if they didn't want any output except (probably) the parse tree,
122 // then don't bother actually fully expanding it
123 if ( $prop ||
$params['prop'] === null ) {
124 $wgParser->startExternalParse( $titleObj, $options, Parser
::OT_PREPROCESS
);
125 $frame = $wgParser->getPreprocessor()->newFrame();
126 $wikitext = $wgParser->preprocess( $params['text'], $titleObj, $options, $revid, $frame );
127 if ( $params['prop'] === null ) {
129 ApiResult
::setContentValue( $retval, 'wikitext', $wikitext );
131 $p_output = $wgParser->getOutput();
132 if ( isset( $prop['categories'] ) ) {
133 $categories = $p_output->getCategories();
135 $categories_result = [];
136 foreach ( $categories as $category => $sortkey ) {
138 $entry['sortkey'] = $sortkey;
139 ApiResult
::setContentValue( $entry, 'category', (string)$category );
140 $categories_result[] = $entry;
142 ApiResult
::setIndexedTagName( $categories_result, 'category' );
143 $retval['categories'] = $categories_result;
146 if ( isset( $prop['properties'] ) ) {
147 $properties = $p_output->getProperties();
149 ApiResult
::setArrayType( $properties, 'BCkvp', 'name' );
150 ApiResult
::setIndexedTagName( $properties, 'property' );
151 $retval['properties'] = $properties;
154 if ( isset( $prop['volatile'] ) ) {
155 $retval['volatile'] = $frame->isVolatile();
157 if ( isset( $prop['ttl'] ) && $frame->getTTL() !== null ) {
158 $retval['ttl'] = $frame->getTTL();
160 if ( isset( $prop['wikitext'] ) ) {
161 $retval['wikitext'] = $wikitext;
163 if ( isset( $prop['modules'] ) ) {
164 $retval['modules'] = array_values( array_unique( $p_output->getModules() ) );
165 $retval['modulescripts'] = array_values( array_unique( $p_output->getModuleScripts() ) );
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';