Merge "Http::getProxy() method to get proxy configuration"
[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 $wgUser 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->logFeatureUsage( 'action=expandtemplates&!prop' );
46 $this->setWarning( 'Because no values have been specified for the prop parameter, a ' .
47 'legacy format has been used for the output. This format is deprecated, and in ' .
48 'the future, a default value will be set for the prop parameter, causing the new' .
49 'format to always be used.' );
50 $prop = [];
51 } else {
52 $prop = array_flip( $params['prop'] );
53 }
54
55 // Get title and revision ID for parser
56 $revid = $params['revid'];
57 if ( $revid !== null ) {
58 $rev = Revision::newFromId( $revid );
59 if ( !$rev ) {
60 $this->dieUsage( "There is no revision ID $revid", 'missingrev' );
61 }
62 $title_obj = $rev->getTitle();
63 } else {
64 $title_obj = Title::newFromText( $params['title'] );
65 if ( !$title_obj || $title_obj->isExternal() ) {
66 $this->dieUsageMsg( [ 'invalidtitle', $params['title'] ] );
67 }
68 }
69
70 $result = $this->getResult();
71
72 // Parse text
73 global $wgParser;
74 $options = ParserOptions::newFromContext( $this->getContext() );
75
76 if ( $params['includecomments'] ) {
77 $options->setRemoveComments( false );
78 }
79
80 $retval = [];
81
82 if ( isset( $prop['parsetree'] ) || $params['generatexml'] ) {
83 $wgParser->startExternalParse( $title_obj, $options, Parser::OT_PREPROCESS );
84 $dom = $wgParser->preprocessToDom( $params['text'] );
85 if ( is_callable( [ $dom, 'saveXML' ] ) ) {
86 $xml = $dom->saveXML();
87 } else {
88 $xml = $dom->__toString();
89 }
90 if ( isset( $prop['parsetree'] ) ) {
91 unset( $prop['parsetree'] );
92 $retval['parsetree'] = $xml;
93 } else {
94 // the old way
95 $result->addValue( null, 'parsetree', $xml );
96 $result->addValue( null, ApiResult::META_BC_SUBELEMENTS, [ 'parsetree' ] );
97 }
98 }
99
100 // if they didn't want any output except (probably) the parse tree,
101 // then don't bother actually fully expanding it
102 if ( $prop || $params['prop'] === null ) {
103 $wgParser->startExternalParse( $title_obj, $options, Parser::OT_PREPROCESS );
104 $frame = $wgParser->getPreprocessor()->newFrame();
105 $wikitext = $wgParser->preprocess( $params['text'], $title_obj, $options, $revid, $frame );
106 if ( $params['prop'] === null ) {
107 // the old way
108 ApiResult::setContentValue( $retval, 'wikitext', $wikitext );
109 } else {
110 $p_output = $wgParser->getOutput();
111 if ( isset( $prop['categories'] ) ) {
112 $categories = $p_output->getCategories();
113 if ( $categories ) {
114 $categories_result = [];
115 foreach ( $categories as $category => $sortkey ) {
116 $entry = [];
117 $entry['sortkey'] = $sortkey;
118 ApiResult::setContentValue( $entry, 'category', (string)$category );
119 $categories_result[] = $entry;
120 }
121 ApiResult::setIndexedTagName( $categories_result, 'category' );
122 $retval['categories'] = $categories_result;
123 }
124 }
125 if ( isset( $prop['properties'] ) ) {
126 $properties = $p_output->getProperties();
127 if ( $properties ) {
128 ApiResult::setArrayType( $properties, 'BCkvp', 'name' );
129 ApiResult::setIndexedTagName( $properties, 'property' );
130 $retval['properties'] = $properties;
131 }
132 }
133 if ( isset( $prop['volatile'] ) ) {
134 $retval['volatile'] = $frame->isVolatile();
135 }
136 if ( isset( $prop['ttl'] ) && $frame->getTTL() !== null ) {
137 $retval['ttl'] = $frame->getTTL();
138 }
139 if ( isset( $prop['wikitext'] ) ) {
140 $retval['wikitext'] = $wikitext;
141 }
142 if ( isset( $prop['modules'] ) ) {
143 $retval['modules'] = array_values( array_unique( $p_output->getModules() ) );
144 $retval['modulescripts'] = array_values( array_unique( $p_output->getModuleScripts() ) );
145 $retval['modulestyles'] = array_values( array_unique( $p_output->getModuleStyles() ) );
146 }
147 if ( isset( $prop['jsconfigvars'] ) ) {
148 $retval['jsconfigvars'] =
149 ApiResult::addMetadataToResultVars( $p_output->getJsConfigVars() );
150 }
151 if ( isset( $prop['encodedjsconfigvars'] ) ) {
152 $retval['encodedjsconfigvars'] = FormatJson::encode(
153 $p_output->getJsConfigVars(), false, FormatJson::ALL_OK
154 );
155 $retval[ApiResult::META_SUBELEMENTS][] = 'encodedjsconfigvars';
156 }
157 if ( isset( $prop['modules'] ) &&
158 !isset( $prop['jsconfigvars'] ) && !isset( $prop['encodedjsconfigvars'] ) ) {
159 $this->setWarning( "Property 'modules' was set but not 'jsconfigvars' " .
160 "or 'encodedjsconfigvars'. Configuration variables are necessary " .
161 'for proper module usage.' );
162 }
163 }
164 }
165 ApiResult::setSubelementsList( $retval, [ 'wikitext', 'parsetree' ] );
166 $result->addValue( null, $this->getModuleName(), $retval );
167 }
168
169 public function getAllowedParams() {
170 return [
171 'title' => [
172 ApiBase::PARAM_DFLT => 'API',
173 ],
174 'text' => [
175 ApiBase::PARAM_TYPE => 'text',
176 ApiBase::PARAM_REQUIRED => true,
177 ],
178 'revid' => [
179 ApiBase::PARAM_TYPE => 'integer',
180 ],
181 'prop' => [
182 ApiBase::PARAM_TYPE => [
183 'wikitext',
184 'categories',
185 'properties',
186 'volatile',
187 'ttl',
188 'modules',
189 'jsconfigvars',
190 'encodedjsconfigvars',
191 'parsetree',
192 ],
193 ApiBase::PARAM_ISMULTI => true,
194 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
195 ],
196 'includecomments' => false,
197 'generatexml' => [
198 ApiBase::PARAM_TYPE => 'boolean',
199 ApiBase::PARAM_DEPRECATED => true,
200 ],
201 ];
202 }
203
204 protected function getExamplesMessages() {
205 return [
206 'action=expandtemplates&text={{Project:Sandbox}}'
207 => 'apihelp-expandtemplates-example-simple',
208 ];
209 }
210
211 public function getHelpUrls() {
212 return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#expandtemplates';
213 }
214 }