Followup e48ecbc5: fix fatal error due to undefined variable
[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->setWarning( 'Because no values have been specified for the prop parameter, a legacy format has been used for the output.'
46 . ' This format is deprecated, and in the future, a default value will be set for the prop parameter, causing the new format to always be used.' );
47 $prop = array();
48 } else {
49 $prop = array_flip( $params['prop'] );
50 }
51
52 // Create title for parser
53 $title_obj = Title::newFromText( $params['title'] );
54 if ( !$title_obj || $title_obj->isExternal() ) {
55 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
56 }
57
58 $result = $this->getResult();
59
60 // Parse text
61 global $wgParser;
62 $options = ParserOptions::newFromContext( $this->getContext() );
63
64 if ( $params['includecomments'] ) {
65 $options->setRemoveComments( false );
66 }
67
68 $retval = array();
69
70 if ( isset( $prop['parsetree'] ) || $params['generatexml'] ) {
71 $wgParser->startExternalParse( $title_obj, $options, OT_PREPROCESS );
72 $dom = $wgParser->preprocessToDom( $params['text'] );
73 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
74 $xml = $dom->saveXML();
75 } else {
76 $xml = $dom->__toString();
77 }
78 if ( isset( $prop['parsetree'] ) ) {
79 unset( $prop['parsetree'] );
80 $retval['parsetree'] = $xml;
81 } else {
82 // the old way
83 $xml_result = array();
84 ApiResult::setContent( $xml_result, $xml );
85 $result->addValue( null, 'parsetree', $xml_result );
86 }
87 }
88
89 // if they didn't want any output except (probably) the parse tree,
90 // then don't bother actually fully expanding it
91 if ( $prop || $params['prop'] === null ) {
92 $frame = $wgParser->getPreprocessor()->newFrame();
93 $wikitext = $wgParser->preprocess( $params['text'], $title_obj, $options, null, $frame );
94 if ( $params['prop'] === null ) {
95 // the old way
96 ApiResult::setContent( $retval, $wikitext );
97 } else {
98 if ( isset( $prop['categories'] ) ) {
99 $categories = $wgParser->getOutput()->getCategories();
100 if ( !empty( $categories ) ) {
101 $categories_result = array();
102 foreach ( $categories as $category => $sortkey ) {
103 $entry = array();
104 $entry['sortkey'] = $sortkey;
105 ApiResult::setContent( $entry, $category );
106 $categories_result[] = $entry;
107 }
108 $result->setIndexedTagName( $categories_result, 'category' );
109 $retval['categories'] = $categories_result;
110 }
111 }
112 if ( isset( $prop['volatile'] ) && $frame->isVolatile() ) {
113 $retval['volatile'] = '';
114 }
115 if ( isset( $prop['ttl'] ) && $frame->getTTL() !== null ) {
116 $retval['ttl'] = $frame->getTTL();
117 }
118 if ( isset( $prop['wikitext'] ) ) {
119 $retval['wikitext'] = $wikitext;
120 }
121 }
122 }
123 $result->setSubelements( $retval, array( 'wikitext', 'parsetree' ) );
124 $result->addValue( null, $this->getModuleName(), $retval );
125 }
126
127 public function getAllowedParams() {
128 return array(
129 'title' => array(
130 ApiBase::PARAM_DFLT => 'API',
131 ),
132 'text' => array(
133 ApiBase::PARAM_TYPE => 'string',
134 ApiBase::PARAM_REQUIRED => true,
135 ),
136 'prop' => array(
137 ApiBase::PARAM_TYPE => array(
138 'wikitext',
139 'categories',
140 'volatile',
141 'ttl',
142 'parsetree',
143 ),
144 ApiBase::PARAM_ISMULTI => true,
145 ),
146 'includecomments' => false,
147 'generatexml' => array(
148 ApiBase::PARAM_TYPE => 'boolean',
149 ApiBase::PARAM_DEPRECATED => true,
150 ),
151 );
152 }
153
154 public function getParamDescription() {
155 return array(
156 'text' => 'Wikitext to convert',
157 'title' => 'Title of page',
158 'prop' => array(
159 'Which pieces of information to get',
160 ' wikitext - The expanded wikitext',
161 ' categories - Any categories present in the input that are not represented in the wikitext output',
162 ' volatile - Whether the output is volatile and should not be reused elsewhere within the page',
163 ' ttl - The maximum time after which caches of the result should be invalidated',
164 ' parsetree - The XML parse tree of the input',
165 'Note that if no values are selected, the result will contain the wikitext,',
166 'but the output will be in a deprecated format.',
167 ),
168 'includecomments' => 'Whether to include HTML comments in the output',
169 'generatexml' => 'Generate XML parse tree (replaced by prop=parsetree)',
170 );
171 }
172
173 public function getResultProperties() {
174 return array(
175 'wikitext' => array(
176 'wikitext' => 'string',
177 ),
178 'categories' => array(
179 'categories' => array(
180 ApiBase::PROP_TYPE => 'array',
181 ApiBase::PROP_NULLABLE => true,
182 ),
183 ),
184 'volatile' => array(
185 'volatile' => array(
186 ApiBase::PROP_TYPE => 'boolean',
187 ApiBase::PROP_NULLABLE => true,
188 ),
189 ),
190 'ttl' => array(
191 'ttl' => array(
192 ApiBase::PROP_TYPE => 'integer',
193 ApiBase::PROP_NULLABLE => true,
194 ),
195 ),
196 'parsetree' => array(
197 'parsetree' => 'string',
198 ),
199 );
200 }
201
202 public function getDescription() {
203 return 'Expands all templates in wikitext.';
204 }
205
206 public function getPossibleErrors() {
207 return array_merge( parent::getPossibleErrors(), array(
208 array( 'invalidtitle', 'title' ),
209 ) );
210 }
211
212 public function getExamples() {
213 return array(
214 'api.php?action=expandtemplates&text={{Project:Sandbox}}'
215 );
216 }
217
218 public function getHelpUrls() {
219 return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#expandtemplates';
220 }
221 }