Fix use of GenderCache in ApiPageSet::processTitlesArray
[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 use MediaWiki\MediaWikiServices;
24
25 /**
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
28 * to the caller.
29 *
30 * @ingroup API
31 */
32 class ApiExpandTemplates extends ApiBase {
33
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' );
37
38 // Get parameters
39 $params = $this->extractRequestParams();
40 $this->requireMaxOneParameter( $params, 'prop', 'generatexml' );
41
42 $title = $params['title'];
43 if ( $title === null ) {
44 $titleProvided = false;
45 // A title is needed for parsing, so arbitrarily choose one
46 $title = 'API';
47 } else {
48 $titleProvided = true;
49 }
50
51 if ( $params['prop'] === null ) {
52 $this->addDeprecation(
53 [ 'apiwarn-deprecation-missingparam', 'prop' ], 'action=expandtemplates&!prop'
54 );
55 $prop = [];
56 } else {
57 $prop = array_flip( $params['prop'] );
58 }
59
60 $titleObj = Title::newFromText( $title );
61 if ( !$titleObj || $titleObj->isExternal() ) {
62 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
63 }
64
65 // Get title and revision ID for parser
66 $revid = $params['revid'];
67 if ( $revid !== null ) {
68 $rev = MediaWikiServices::getInstance()->getRevisionStore()->getRevisionById( $revid );
69 if ( !$rev ) {
70 $this->dieWithError( [ 'apierror-nosuchrevid', $revid ] );
71 }
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() ) ] );
78 }
79 } else {
80 // Consider the title derived from the revid as having
81 // been provided.
82 $titleProvided = true;
83 }
84 }
85
86 $result = $this->getResult();
87
88 // Parse text
89 $options = ParserOptions::newFromContext( $this->getContext() );
90
91 if ( $params['includecomments'] ) {
92 $options->setRemoveComments( false );
93 }
94
95 $reset = null;
96 $suppressCache = false;
97 Hooks::run( 'ApiMakeParserOptions',
98 [ $options, $titleObj, $params, $this, &$reset, &$suppressCache ] );
99
100 $retval = [];
101
102 $parser = MediaWikiServices::getInstance()->getParser();
103 if ( isset( $prop['parsetree'] ) || $params['generatexml'] ) {
104 $parser->startExternalParse( $titleObj, $options, Parser::OT_PREPROCESS );
105 $dom = $parser->preprocessToDom( $params['text'] );
106 if ( is_callable( [ $dom, 'saveXML' ] ) ) {
107 $xml = $dom->saveXML();
108 } else {
109 $xml = $dom->__toString();
110 }
111 if ( isset( $prop['parsetree'] ) ) {
112 unset( $prop['parsetree'] );
113 $retval['parsetree'] = $xml;
114 } else {
115 // the old way
116 $result->addValue( null, 'parsetree', $xml );
117 $result->addValue( null, ApiResult::META_BC_SUBELEMENTS, [ 'parsetree' ] );
118 }
119 }
120
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 $parser->startExternalParse( $titleObj, $options, Parser::OT_PREPROCESS );
125 $frame = $parser->getPreprocessor()->newFrame();
126 $wikitext = $parser->preprocess( $params['text'], $titleObj, $options, $revid, $frame );
127 if ( $params['prop'] === null ) {
128 // the old way
129 ApiResult::setContentValue( $retval, 'wikitext', $wikitext );
130 } else {
131 $p_output = $parser->getOutput();
132 if ( isset( $prop['categories'] ) ) {
133 $categories = $p_output->getCategories();
134 if ( $categories ) {
135 $categories_result = [];
136 foreach ( $categories as $category => $sortkey ) {
137 $entry = [];
138 $entry['sortkey'] = $sortkey;
139 ApiResult::setContentValue( $entry, 'category', (string)$category );
140 $categories_result[] = $entry;
141 }
142 ApiResult::setIndexedTagName( $categories_result, 'category' );
143 $retval['categories'] = $categories_result;
144 }
145 }
146 if ( isset( $prop['properties'] ) ) {
147 $properties = $p_output->getProperties();
148 if ( $properties ) {
149 ApiResult::setArrayType( $properties, 'BCkvp', 'name' );
150 ApiResult::setIndexedTagName( $properties, 'property' );
151 $retval['properties'] = $properties;
152 }
153 }
154 if ( isset( $prop['volatile'] ) ) {
155 $retval['volatile'] = $frame->isVolatile();
156 }
157 if ( isset( $prop['ttl'] ) && $frame->getTTL() !== null ) {
158 $retval['ttl'] = $frame->getTTL();
159 }
160 if ( isset( $prop['wikitext'] ) ) {
161 $retval['wikitext'] = $wikitext;
162 }
163 if ( isset( $prop['modules'] ) ) {
164 $retval['modules'] = array_values( array_unique( $p_output->getModules() ) );
165 // Deprecated since 1.32 (T188689)
166 $retval['modulescripts'] = [];
167 $retval['modulestyles'] = array_values( array_unique( $p_output->getModuleStyles() ) );
168 }
169 if ( isset( $prop['jsconfigvars'] ) ) {
170 $retval['jsconfigvars'] =
171 ApiResult::addMetadataToResultVars( $p_output->getJsConfigVars() );
172 }
173 if ( isset( $prop['encodedjsconfigvars'] ) ) {
174 $retval['encodedjsconfigvars'] = FormatJson::encode(
175 $p_output->getJsConfigVars(), false, FormatJson::ALL_OK
176 );
177 $retval[ApiResult::META_SUBELEMENTS][] = 'encodedjsconfigvars';
178 }
179 if ( isset( $prop['modules'] ) &&
180 !isset( $prop['jsconfigvars'] ) && !isset( $prop['encodedjsconfigvars'] ) ) {
181 $this->addWarning( 'apiwarn-moduleswithoutvars' );
182 }
183 }
184 }
185 ApiResult::setSubelementsList( $retval, [ 'wikitext', 'parsetree' ] );
186 $result->addValue( null, $this->getModuleName(), $retval );
187 }
188
189 public function getAllowedParams() {
190 return [
191 'title' => null,
192 'text' => [
193 ApiBase::PARAM_TYPE => 'text',
194 ApiBase::PARAM_REQUIRED => true,
195 ],
196 'revid' => [
197 ApiBase::PARAM_TYPE => 'integer',
198 ],
199 'prop' => [
200 ApiBase::PARAM_TYPE => [
201 'wikitext',
202 'categories',
203 'properties',
204 'volatile',
205 'ttl',
206 'modules',
207 'jsconfigvars',
208 'encodedjsconfigvars',
209 'parsetree',
210 ],
211 ApiBase::PARAM_ISMULTI => true,
212 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
213 ],
214 'includecomments' => false,
215 'generatexml' => [
216 ApiBase::PARAM_TYPE => 'boolean',
217 ApiBase::PARAM_DEPRECATED => true,
218 ],
219 ];
220 }
221
222 protected function getExamplesMessages() {
223 return [
224 'action=expandtemplates&text={{Project:Sandbox}}'
225 => 'apihelp-expandtemplates-example-simple',
226 ];
227 }
228
229 public function getHelpUrls() {
230 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Parsing_wikitext#expandtemplates';
231 }
232 }