API: Overhaul ApiResult, make format=xml not throw, and add json formatversion
[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 = array();
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( array( '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 = array();
81
82 if ( isset( $prop['parsetree'] ) || $params['generatexml'] ) {
83 if ( !isset( $prop['parsetree'] ) ) {
84 $this->logFeatureUsage( 'action=expandtemplates&generatexml' );
85 }
86
87 $wgParser->startExternalParse( $title_obj, $options, Parser::OT_PREPROCESS );
88 $dom = $wgParser->preprocessToDom( $params['text'] );
89 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
90 $xml = $dom->saveXML();
91 } else {
92 $xml = $dom->__toString();
93 }
94 if ( isset( $prop['parsetree'] ) ) {
95 unset( $prop['parsetree'] );
96 $retval['parsetree'] = $xml;
97 } else {
98 // the old way
99 $xml_result = array();
100 ApiResult::setContentValue( $xml_result, 'xml', $xml );
101 $result->addValue( null, 'parsetree', $xml_result );
102 }
103 }
104
105 // if they didn't want any output except (probably) the parse tree,
106 // then don't bother actually fully expanding it
107 if ( $prop || $params['prop'] === null ) {
108 $wgParser->startExternalParse( $title_obj, $options, Parser::OT_PREPROCESS );
109 $frame = $wgParser->getPreprocessor()->newFrame();
110 $wikitext = $wgParser->preprocess( $params['text'], $title_obj, $options, $revid, $frame );
111 if ( $params['prop'] === null ) {
112 // the old way
113 ApiResult::setContentValue( $retval, 'wikitext', $wikitext );
114 } else {
115 if ( isset( $prop['categories'] ) ) {
116 $categories = $wgParser->getOutput()->getCategories();
117 if ( $categories ) {
118 $categories_result = array();
119 foreach ( $categories as $category => $sortkey ) {
120 $entry = array();
121 $entry['sortkey'] = $sortkey;
122 ApiResult::setContentValue( $entry, 'category', $category );
123 $categories_result[] = $entry;
124 }
125 ApiResult::setIndexedTagName( $categories_result, 'category' );
126 $retval['categories'] = $categories_result;
127 }
128 }
129 if ( isset( $prop['properties'] ) ) {
130 $properties = $wgParser->getOutput()->getProperties();
131 if ( $properties ) {
132 $properties_result = array();
133 foreach ( $properties as $name => $value ) {
134 $entry = array();
135 $entry['name'] = $name;
136 ApiResult::setContentValue( $entry, 'value', $value );
137 $properties_result[] = $entry;
138 }
139 ApiResult::setIndexedTagName( $properties_result, 'property' );
140 $retval['properties'] = $properties_result;
141 }
142 }
143 if ( isset( $prop['volatile'] ) && $frame->isVolatile() ) {
144 $retval['volatile'] = '';
145 }
146 if ( isset( $prop['ttl'] ) && $frame->getTTL() !== null ) {
147 $retval['ttl'] = $frame->getTTL();
148 }
149 if ( isset( $prop['wikitext'] ) ) {
150 $retval['wikitext'] = $wikitext;
151 }
152 }
153 }
154 ApiResult::setSubelementsList( $retval, array( 'wikitext', 'parsetree' ) );
155 $result->addValue( null, $this->getModuleName(), $retval );
156 }
157
158 public function getAllowedParams() {
159 return array(
160 'title' => array(
161 ApiBase::PARAM_DFLT => 'API',
162 ),
163 'text' => array(
164 ApiBase::PARAM_TYPE => 'string',
165 ApiBase::PARAM_REQUIRED => true,
166 ),
167 'revid' => array(
168 ApiBase::PARAM_TYPE => 'integer',
169 ),
170 'prop' => array(
171 ApiBase::PARAM_TYPE => array(
172 'wikitext',
173 'categories',
174 'properties',
175 'volatile',
176 'ttl',
177 'parsetree',
178 ),
179 ApiBase::PARAM_ISMULTI => true,
180 ),
181 'includecomments' => false,
182 'generatexml' => array(
183 ApiBase::PARAM_TYPE => 'boolean',
184 ApiBase::PARAM_DEPRECATED => true,
185 ),
186 );
187 }
188
189 protected function getExamplesMessages() {
190 return array(
191 'action=expandtemplates&text={{Project:Sandbox}}'
192 => 'apihelp-expandtemplates-example-simple',
193 );
194 }
195
196 public function getHelpUrls() {
197 return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#expandtemplates';
198 }
199 }