Merge "Fix sessionfailure i18n message during authentication"
[lhc/web/wiklou.git] / includes / api / ApiExpandTemplates.php
1 <?php
2 /**
3 * Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 /**
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
26 * to the caller.
27 *
28 * @ingroup API
29 */
30 class ApiExpandTemplates extends ApiBase {
31
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' );
35
36 // Get parameters
37 $params = $this->extractRequestParams();
38 $this->requireMaxOneParameter( $params, 'prop', 'generatexml' );
39
40 $title = $params['title'];
41 if ( $title === null ) {
42 $titleProvided = false;
43 // A title is needed for parsing, so arbitrarily choose one
44 $title = 'API';
45 } else {
46 $titleProvided = true;
47 }
48
49 if ( $params['prop'] === null ) {
50 $this->addDeprecation(
51 'apiwarn-deprecation-expandtemplates-prop', 'action=expandtemplates&!prop'
52 );
53 $prop = [];
54 } else {
55 $prop = array_flip( $params['prop'] );
56 }
57
58 $titleObj = Title::newFromText( $title );
59 if ( !$titleObj || $titleObj->isExternal() ) {
60 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
61 }
62
63 // Get title and revision ID for parser
64 $revid = $params['revid'];
65 if ( $revid !== null ) {
66 $rev = Revision::newFromId( $revid );
67 if ( !$rev ) {
68 $this->dieWithError( [ 'apierror-nosuchrevid', $revid ] );
69 }
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() ) ] );
76 }
77 } else {
78 // Consider the title derived from the revid as having
79 // been provided.
80 $titleProvided = true;
81 }
82 }
83
84 $result = $this->getResult();
85
86 // Parse text
87 global $wgParser;
88 $options = ParserOptions::newFromContext( $this->getContext() );
89
90 if ( $params['includecomments'] ) {
91 $options->setRemoveComments( false );
92 }
93
94 $reset = null;
95 $suppressCache = false;
96 Hooks::run( 'ApiMakeParserOptions',
97 [ $options, $titleObj, $params, $this, &$reset, &$suppressCache ] );
98
99 $retval = [];
100
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();
106 } else {
107 $xml = $dom->__toString();
108 }
109 if ( isset( $prop['parsetree'] ) ) {
110 unset( $prop['parsetree'] );
111 $retval['parsetree'] = $xml;
112 } else {
113 // the old way
114 $result->addValue( null, 'parsetree', $xml );
115 $result->addValue( null, ApiResult::META_BC_SUBELEMENTS, [ 'parsetree' ] );
116 }
117 }
118
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 ) {
126 // the old way
127 ApiResult::setContentValue( $retval, 'wikitext', $wikitext );
128 } else {
129 $p_output = $wgParser->getOutput();
130 if ( isset( $prop['categories'] ) ) {
131 $categories = $p_output->getCategories();
132 if ( $categories ) {
133 $categories_result = [];
134 foreach ( $categories as $category => $sortkey ) {
135 $entry = [];
136 $entry['sortkey'] = $sortkey;
137 ApiResult::setContentValue( $entry, 'category', (string)$category );
138 $categories_result[] = $entry;
139 }
140 ApiResult::setIndexedTagName( $categories_result, 'category' );
141 $retval['categories'] = $categories_result;
142 }
143 }
144 if ( isset( $prop['properties'] ) ) {
145 $properties = $p_output->getProperties();
146 if ( $properties ) {
147 ApiResult::setArrayType( $properties, 'BCkvp', 'name' );
148 ApiResult::setIndexedTagName( $properties, 'property' );
149 $retval['properties'] = $properties;
150 }
151 }
152 if ( isset( $prop['volatile'] ) ) {
153 $retval['volatile'] = $frame->isVolatile();
154 }
155 if ( isset( $prop['ttl'] ) && $frame->getTTL() !== null ) {
156 $retval['ttl'] = $frame->getTTL();
157 }
158 if ( isset( $prop['wikitext'] ) ) {
159 $retval['wikitext'] = $wikitext;
160 }
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() ) );
165 }
166 if ( isset( $prop['jsconfigvars'] ) ) {
167 $retval['jsconfigvars'] =
168 ApiResult::addMetadataToResultVars( $p_output->getJsConfigVars() );
169 }
170 if ( isset( $prop['encodedjsconfigvars'] ) ) {
171 $retval['encodedjsconfigvars'] = FormatJson::encode(
172 $p_output->getJsConfigVars(), false, FormatJson::ALL_OK
173 );
174 $retval[ApiResult::META_SUBELEMENTS][] = 'encodedjsconfigvars';
175 }
176 if ( isset( $prop['modules'] ) &&
177 !isset( $prop['jsconfigvars'] ) && !isset( $prop['encodedjsconfigvars'] ) ) {
178 $this->addWarning( 'apiwarn-moduleswithoutvars' );
179 }
180 }
181 }
182 ApiResult::setSubelementsList( $retval, [ 'wikitext', 'parsetree' ] );
183 $result->addValue( null, $this->getModuleName(), $retval );
184 }
185
186 public function getAllowedParams() {
187 return [
188 'title' => null,
189 'text' => [
190 ApiBase::PARAM_TYPE => 'text',
191 ApiBase::PARAM_REQUIRED => true,
192 ],
193 'revid' => [
194 ApiBase::PARAM_TYPE => 'integer',
195 ],
196 'prop' => [
197 ApiBase::PARAM_TYPE => [
198 'wikitext',
199 'categories',
200 'properties',
201 'volatile',
202 'ttl',
203 'modules',
204 'jsconfigvars',
205 'encodedjsconfigvars',
206 'parsetree',
207 ],
208 ApiBase::PARAM_ISMULTI => true,
209 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
210 ],
211 'includecomments' => false,
212 'generatexml' => [
213 ApiBase::PARAM_TYPE => 'boolean',
214 ApiBase::PARAM_DEPRECATED => true,
215 ],
216 ];
217 }
218
219 protected function getExamplesMessages() {
220 return [
221 'action=expandtemplates&text={{Project:Sandbox}}'
222 => 'apihelp-expandtemplates-example-simple',
223 ];
224 }
225
226 public function getHelpUrls() {
227 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Parsing_wikitext#expandtemplates';
228 }
229 }