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
24 * API module that functions as a shortcut to the wikitext preprocessor. Expands
25 * any templates in a provided string, and returns the result of this expansion
30 class ApiExpandTemplates
extends ApiBase
{
32 public function execute() {
33 // Cache may vary on the user because ParserOptions gets data from it
34 $this->getMain()->setCacheMode( 'anon-public-user-private' );
37 $params = $this->extractRequestParams();
38 $this->requireMaxOneParameter( $params, 'prop', 'generatexml' );
40 $title = $params['title'];
41 if ( $title === null ) {
42 $titleProvided = false;
43 // A title is needed for parsing, so arbitrarily choose one
46 $titleProvided = true;
49 if ( $params['prop'] === null ) {
50 $this->addDeprecation(
51 'apiwarn-deprecation-expandtemplates-prop', 'action=expandtemplates&!prop'
55 $prop = array_flip( $params['prop'] );
58 $titleObj = Title
::newFromText( $title );
59 if ( !$titleObj ||
$titleObj->isExternal() ) {
60 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
63 // Get title and revision ID for parser
64 $revid = $params['revid'];
65 if ( $revid !== null ) {
66 $rev = Revision
::newFromId( $revid );
68 $this->dieWithError( [ 'apierror-nosuchrevid', $revid ] );
70 $pTitleObj = $titleObj;
71 $titleObj = $rev->getTitle();
72 if ( $titleProvided ) {
73 if ( !$titleObj->equals( $pTitleObj ) ) {
74 $this->addWarning( [ 'apierror-revwrongpage', $rev->getId(),
75 wfEscapeWikiText( $pTitleObj->getPrefixedText() ) ] );
78 // Consider the title derived from the revid as having
80 $titleProvided = true;
84 $result = $this->getResult();
88 $options = ParserOptions
::newFromContext( $this->getContext() );
90 if ( $params['includecomments'] ) {
91 $options->setRemoveComments( false );
95 $suppressCache = false;
96 Hooks
::run( 'ApiMakeParserOptions',
97 [ $options, $titleObj, $params, $this, &$reset, &$suppressCache ] );
101 if ( isset( $prop['parsetree'] ) ||
$params['generatexml'] ) {
102 $wgParser->startExternalParse( $titleObj, $options, Parser
::OT_PREPROCESS
);
103 $dom = $wgParser->preprocessToDom( $params['text'] );
104 if ( is_callable( [ $dom, 'saveXML' ] ) ) {
105 $xml = $dom->saveXML();
107 $xml = $dom->__toString();
109 if ( isset( $prop['parsetree'] ) ) {
110 unset( $prop['parsetree'] );
111 $retval['parsetree'] = $xml;
114 $result->addValue( null, 'parsetree', $xml );
115 $result->addValue( null, ApiResult
::META_BC_SUBELEMENTS
, [ 'parsetree' ] );
119 // if they didn't want any output except (probably) the parse tree,
120 // then don't bother actually fully expanding it
121 if ( $prop ||
$params['prop'] === null ) {
122 $wgParser->startExternalParse( $titleObj, $options, Parser
::OT_PREPROCESS
);
123 $frame = $wgParser->getPreprocessor()->newFrame();
124 $wikitext = $wgParser->preprocess( $params['text'], $titleObj, $options, $revid, $frame );
125 if ( $params['prop'] === null ) {
127 ApiResult
::setContentValue( $retval, 'wikitext', $wikitext );
129 $p_output = $wgParser->getOutput();
130 if ( isset( $prop['categories'] ) ) {
131 $categories = $p_output->getCategories();
133 $categories_result = [];
134 foreach ( $categories as $category => $sortkey ) {
136 $entry['sortkey'] = $sortkey;
137 ApiResult
::setContentValue( $entry, 'category', (string)$category );
138 $categories_result[] = $entry;
140 ApiResult
::setIndexedTagName( $categories_result, 'category' );
141 $retval['categories'] = $categories_result;
144 if ( isset( $prop['properties'] ) ) {
145 $properties = $p_output->getProperties();
147 ApiResult
::setArrayType( $properties, 'BCkvp', 'name' );
148 ApiResult
::setIndexedTagName( $properties, 'property' );
149 $retval['properties'] = $properties;
152 if ( isset( $prop['volatile'] ) ) {
153 $retval['volatile'] = $frame->isVolatile();
155 if ( isset( $prop['ttl'] ) && $frame->getTTL() !== null ) {
156 $retval['ttl'] = $frame->getTTL();
158 if ( isset( $prop['wikitext'] ) ) {
159 $retval['wikitext'] = $wikitext;
161 if ( isset( $prop['modules'] ) ) {
162 $retval['modules'] = array_values( array_unique( $p_output->getModules() ) );
163 $retval['modulescripts'] = array_values( array_unique( $p_output->getModuleScripts() ) );
164 $retval['modulestyles'] = array_values( array_unique( $p_output->getModuleStyles() ) );
166 if ( isset( $prop['jsconfigvars'] ) ) {
167 $retval['jsconfigvars'] =
168 ApiResult
::addMetadataToResultVars( $p_output->getJsConfigVars() );
170 if ( isset( $prop['encodedjsconfigvars'] ) ) {
171 $retval['encodedjsconfigvars'] = FormatJson
::encode(
172 $p_output->getJsConfigVars(), false, FormatJson
::ALL_OK
174 $retval[ApiResult
::META_SUBELEMENTS
][] = 'encodedjsconfigvars';
176 if ( isset( $prop['modules'] ) &&
177 !isset( $prop['jsconfigvars'] ) && !isset( $prop['encodedjsconfigvars'] ) ) {
178 $this->addWarning( 'apiwarn-moduleswithoutvars' );
182 ApiResult
::setSubelementsList( $retval, [ 'wikitext', 'parsetree' ] );
183 $result->addValue( null, $this->getModuleName(), $retval );
186 public function getAllowedParams() {
190 ApiBase
::PARAM_TYPE
=> 'text',
191 ApiBase
::PARAM_REQUIRED
=> true,
194 ApiBase
::PARAM_TYPE
=> 'integer',
197 ApiBase
::PARAM_TYPE
=> [
205 'encodedjsconfigvars',
208 ApiBase
::PARAM_ISMULTI
=> true,
209 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
211 'includecomments' => false,
213 ApiBase
::PARAM_TYPE
=> 'boolean',
214 ApiBase
::PARAM_DEPRECATED
=> true,
219 protected function getExamplesMessages() {
221 'action=expandtemplates&text={{Project:Sandbox}}'
222 => 'apihelp-expandtemplates-example-simple',
226 public function getHelpUrls() {
227 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Parsing_wikitext#expandtemplates';