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