Merge "Add CollationFa"
[lhc/web/wiklou.git] / includes / api / ApiExpandTemplates.php
1 <?php
2 /**
3 *
4 *
5 * Created on Oct 05, 2007
6 *
7 * Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 /**
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
30 * to the caller.
31 *
32 * @ingroup API
33 */
34 class ApiExpandTemplates extends ApiBase {
35
36 public function execute() {
37 // Cache may vary on the user because ParserOptions gets data from it
38 $this->getMain()->setCacheMode( 'anon-public-user-private' );
39
40 // Get parameters
41 $params = $this->extractRequestParams();
42 $this->requireMaxOneParameter( $params, 'prop', 'generatexml' );
43
44 if ( $params['prop'] === null ) {
45 $this->addDeprecation(
46 'apiwarn-deprecation-expandtemplates-prop', 'action=expandtemplates&!prop'
47 );
48 $prop = [];
49 } else {
50 $prop = array_flip( $params['prop'] );
51 }
52
53 // Get title and revision ID for parser
54 $revid = $params['revid'];
55 if ( $revid !== null ) {
56 $rev = Revision::newFromId( $revid );
57 if ( !$rev ) {
58 $this->dieWithError( [ 'apierror-nosuchrevid', $revid ] );
59 }
60 $title_obj = $rev->getTitle();
61 } else {
62 $title_obj = Title::newFromText( $params['title'] );
63 if ( !$title_obj || $title_obj->isExternal() ) {
64 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
65 }
66 }
67
68 $result = $this->getResult();
69
70 // Parse text
71 global $wgParser;
72 $options = ParserOptions::newFromContext( $this->getContext() );
73
74 if ( $params['includecomments'] ) {
75 $options->setRemoveComments( false );
76 }
77
78 $reset = null;
79 $suppressCache = false;
80 Hooks::run( 'ApiMakeParserOptions',
81 [ $options, $title_obj, $params, $this, &$reset, &$suppressCache ] );
82
83 $retval = [];
84
85 if ( isset( $prop['parsetree'] ) || $params['generatexml'] ) {
86 $wgParser->startExternalParse( $title_obj, $options, Parser::OT_PREPROCESS );
87 $dom = $wgParser->preprocessToDom( $params['text'] );
88 if ( is_callable( [ $dom, 'saveXML' ] ) ) {
89 $xml = $dom->saveXML();
90 } else {
91 $xml = $dom->__toString();
92 }
93 if ( isset( $prop['parsetree'] ) ) {
94 unset( $prop['parsetree'] );
95 $retval['parsetree'] = $xml;
96 } else {
97 // the old way
98 $result->addValue( null, 'parsetree', $xml );
99 $result->addValue( null, ApiResult::META_BC_SUBELEMENTS, [ 'parsetree' ] );
100 }
101 }
102
103 // if they didn't want any output except (probably) the parse tree,
104 // then don't bother actually fully expanding it
105 if ( $prop || $params['prop'] === null ) {
106 $wgParser->startExternalParse( $title_obj, $options, Parser::OT_PREPROCESS );
107 $frame = $wgParser->getPreprocessor()->newFrame();
108 $wikitext = $wgParser->preprocess( $params['text'], $title_obj, $options, $revid, $frame );
109 if ( $params['prop'] === null ) {
110 // the old way
111 ApiResult::setContentValue( $retval, 'wikitext', $wikitext );
112 } else {
113 $p_output = $wgParser->getOutput();
114 if ( isset( $prop['categories'] ) ) {
115 $categories = $p_output->getCategories();
116 if ( $categories ) {
117 $categories_result = [];
118 foreach ( $categories as $category => $sortkey ) {
119 $entry = [];
120 $entry['sortkey'] = $sortkey;
121 ApiResult::setContentValue( $entry, 'category', (string)$category );
122 $categories_result[] = $entry;
123 }
124 ApiResult::setIndexedTagName( $categories_result, 'category' );
125 $retval['categories'] = $categories_result;
126 }
127 }
128 if ( isset( $prop['properties'] ) ) {
129 $properties = $p_output->getProperties();
130 if ( $properties ) {
131 ApiResult::setArrayType( $properties, 'BCkvp', 'name' );
132 ApiResult::setIndexedTagName( $properties, 'property' );
133 $retval['properties'] = $properties;
134 }
135 }
136 if ( isset( $prop['volatile'] ) ) {
137 $retval['volatile'] = $frame->isVolatile();
138 }
139 if ( isset( $prop['ttl'] ) && $frame->getTTL() !== null ) {
140 $retval['ttl'] = $frame->getTTL();
141 }
142 if ( isset( $prop['wikitext'] ) ) {
143 $retval['wikitext'] = $wikitext;
144 }
145 if ( isset( $prop['modules'] ) ) {
146 $retval['modules'] = array_values( array_unique( $p_output->getModules() ) );
147 $retval['modulescripts'] = array_values( array_unique( $p_output->getModuleScripts() ) );
148 $retval['modulestyles'] = array_values( array_unique( $p_output->getModuleStyles() ) );
149 }
150 if ( isset( $prop['jsconfigvars'] ) ) {
151 $retval['jsconfigvars'] =
152 ApiResult::addMetadataToResultVars( $p_output->getJsConfigVars() );
153 }
154 if ( isset( $prop['encodedjsconfigvars'] ) ) {
155 $retval['encodedjsconfigvars'] = FormatJson::encode(
156 $p_output->getJsConfigVars(), false, FormatJson::ALL_OK
157 );
158 $retval[ApiResult::META_SUBELEMENTS][] = 'encodedjsconfigvars';
159 }
160 if ( isset( $prop['modules'] ) &&
161 !isset( $prop['jsconfigvars'] ) && !isset( $prop['encodedjsconfigvars'] ) ) {
162 $this->addWarning( 'apiwarn-moduleswithoutvars' );
163 }
164 }
165 }
166 ApiResult::setSubelementsList( $retval, [ 'wikitext', 'parsetree' ] );
167 $result->addValue( null, $this->getModuleName(), $retval );
168 }
169
170 public function getAllowedParams() {
171 return [
172 'title' => [
173 ApiBase::PARAM_DFLT => 'API',
174 ],
175 'text' => [
176 ApiBase::PARAM_TYPE => 'text',
177 ApiBase::PARAM_REQUIRED => true,
178 ],
179 'revid' => [
180 ApiBase::PARAM_TYPE => 'integer',
181 ],
182 'prop' => [
183 ApiBase::PARAM_TYPE => [
184 'wikitext',
185 'categories',
186 'properties',
187 'volatile',
188 'ttl',
189 'modules',
190 'jsconfigvars',
191 'encodedjsconfigvars',
192 'parsetree',
193 ],
194 ApiBase::PARAM_ISMULTI => true,
195 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
196 ],
197 'includecomments' => false,
198 'generatexml' => [
199 ApiBase::PARAM_TYPE => 'boolean',
200 ApiBase::PARAM_DEPRECATED => true,
201 ],
202 ];
203 }
204
205 protected function getExamplesMessages() {
206 return [
207 'action=expandtemplates&text={{Project:Sandbox}}'
208 => 'apihelp-expandtemplates-example-simple',
209 ];
210 }
211
212 public function getHelpUrls() {
213 return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#expandtemplates';
214 }
215 }