Complete dieUsage errors in 6 more Api files
[lhc/web/wiklou.git] / includes / api / ApiParse.php
1 <?php
2
3 /*
4 * Created on Dec 01, 2007
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once ( "ApiBase.php" );
29 }
30
31 /**
32 * @ingroup API
33 */
34 class ApiParse extends ApiBase {
35
36 public function __construct( $main, $action ) {
37 parent :: __construct( $main, $action );
38 }
39
40 public function execute() {
41 // Get parameters
42 $params = $this->extractRequestParams();
43 $text = $params['text'];
44 $title = $params['title'];
45 $page = $params['page'];
46 $oldid = $params['oldid'];
47 if ( !is_null( $page ) && ( !is_null( $text ) || $title != "API" ) )
48 $this->dieUsage( "The page parameter cannot be used together with the text and title parameters", 'params' );
49 $prop = array_flip( $params['prop'] );
50 $revid = false;
51
52 // The parser needs $wgTitle to be set, apparently the
53 // $title parameter in Parser::parse isn't enough *sigh*
54 global $wgParser, $wgUser, $wgTitle, $wgEnableParserCache;
55 $popts = new ParserOptions();
56 $popts->setTidy( true );
57 $popts->enableLimitReport();
58 $redirValues = null;
59 if ( !is_null( $oldid ) || !is_null( $page ) )
60 {
61 if ( !is_null( $oldid ) )
62 {
63 // Don't use the parser cache
64 $rev = Revision::newFromID( $oldid );
65 if ( !$rev )
66 $this->dieUsage( "There is no revision ID $oldid", 'missingrev' );
67 if ( !$rev->userCan( Revision::DELETED_TEXT ) )
68 $this->dieUsage( "You don't have permission to view deleted revisions", 'permissiondenied' );
69
70 $text = $rev->getText( Revision::FOR_THIS_USER );
71 $titleObj = $rev->getTitle();
72 $wgTitle = $titleObj;
73 $p_result = $wgParser->parse( $text, $titleObj, $popts );
74 }
75 else
76 {
77 if ( $params['redirects'] )
78 {
79 $req = new FauxRequest( array(
80 'action' => 'query',
81 'redirects' => '',
82 'titles' => $page
83 ) );
84 $main = new ApiMain( $req );
85 $main->execute();
86 $data = $main->getResultData();
87 $redirValues = @$data['query']['redirects'];
88 $to = $page;
89 foreach ( (array)$redirValues as $r )
90 $to = $r['to'];
91 }
92 else
93 $to = $page;
94 $titleObj = Title::newFromText( $to );
95 if ( !$titleObj )
96 $this->dieUsage( "The page you specified doesn't exist", 'missingtitle' );
97
98 $articleObj = new Article( $titleObj );
99 if ( isset( $prop['revid'] ) )
100 $oldid = $articleObj->getRevIdFetched();
101 // Try the parser cache first
102 $p_result = false;
103 $pcache = ParserCache::singleton();
104 if ( $wgEnableParserCache )
105 $p_result = $pcache->get( $articleObj, $wgUser );
106 if ( !$p_result )
107 {
108 $p_result = $wgParser->parse( $articleObj->getContent(), $titleObj, $popts );
109
110 if ( $wgEnableParserCache )
111 $pcache->save( $p_result, $articleObj, $popts );
112 }
113 }
114 }
115 else
116 {
117 $titleObj = Title::newFromText( $title );
118 if ( !$titleObj )
119 $titleObj = Title::newFromText( "API" );
120 $wgTitle = $titleObj;
121 if ( $params['pst'] || $params['onlypst'] )
122 $text = $wgParser->preSaveTransform( $text, $titleObj, $wgUser, $popts );
123 if ( $params['onlypst'] )
124 {
125 // Build a result and bail out
126 $result_array['text'] = array();
127 $this->getResult()->setContent( $result_array['text'], $text );
128 $this->getResult()->addValue( null, $this->getModuleName(), $result_array );
129 return;
130 }
131 $p_result = $wgParser->parse( $text, $titleObj, $popts );
132 }
133
134 // Return result
135 $result = $this->getResult();
136 $result_array = array();
137 if ( $params['redirects'] && !is_null( $redirValues ) )
138 $result_array['redirects'] = $redirValues;
139
140 if ( isset( $prop['text'] ) ) {
141 $result_array['text'] = array();
142 $result->setContent( $result_array['text'], $p_result->getText() );
143 }
144
145 if ( !is_null( $params['summary'] ) ) {
146 $result_array['parsedsummary'] = array();
147 $result->setContent( $result_array['parsedsummary'], $wgUser->getSkin()->formatComment( $params['summary'], $titleObj ) );
148 }
149
150 if ( isset( $prop['langlinks'] ) )
151 $result_array['langlinks'] = $this->formatLangLinks( $p_result->getLanguageLinks() );
152 if ( isset( $prop['categories'] ) )
153 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
154 if ( isset( $prop['links'] ) )
155 $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
156 if ( isset( $prop['templates'] ) )
157 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
158 if ( isset( $prop['images'] ) )
159 $result_array['images'] = array_keys( $p_result->getImages() );
160 if ( isset( $prop['externallinks'] ) )
161 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
162 if ( isset( $prop['sections'] ) )
163 $result_array['sections'] = $p_result->getSections();
164 if ( isset( $prop['displaytitle'] ) )
165 $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
166 $p_result->getDisplayTitle() :
167 $titleObj->getPrefixedText();
168
169 if ( isset( $prop['headitems'] ) )
170 $result_array['headitems'] = $this->formatHeadItems( $p_result->getHeadItems() );
171
172 if ( isset( $prop['headhtml'] ) ) {
173 $out = new OutputPage;
174 $out->addParserOutputNoText( $p_result );
175 $result_array['stylesheets'] = array();
176 $result->setContent( $result_array['headhtml'], $out->headElement( $wgUser->getSkin() ) );
177 }
178
179 if ( !is_null( $oldid ) )
180 $result_array['revid'] = intval( $oldid );
181
182 $result_mapping = array(
183 'redirects' => 'r',
184 'langlinks' => 'll',
185 'categories' => 'cl',
186 'links' => 'pl',
187 'templates' => 'tl',
188 'images' => 'img',
189 'externallinks' => 'el',
190 'sections' => 's',
191 'headitems' => 'hi'
192 );
193 $this->setIndexedTagNames( $result_array, $result_mapping );
194 $result->addValue( null, $this->getModuleName(), $result_array );
195 }
196
197 private function formatLangLinks( $links ) {
198 $result = array();
199 foreach ( $links as $link ) {
200 $entry = array();
201 $bits = explode( ':', $link, 2 );
202 $entry['lang'] = $bits[0];
203 $this->getResult()->setContent( $entry, $bits[1] );
204 $result[] = $entry;
205 }
206 return $result;
207 }
208
209 private function formatCategoryLinks( $links ) {
210 $result = array();
211 foreach ( $links as $link => $sortkey ) {
212 $entry = array();
213 $entry['sortkey'] = $sortkey;
214 $this->getResult()->setContent( $entry, $link );
215 $result[] = $entry;
216 }
217 return $result;
218 }
219
220 private function formatLinks( $links ) {
221 $result = array();
222 foreach ( $links as $ns => $nslinks ) {
223 foreach ( $nslinks as $title => $id ) {
224 $entry = array();
225 $entry['ns'] = $ns;
226 $this->getResult()->setContent( $entry, Title::makeTitle( $ns, $title )->getFullText() );
227 if ( $id != 0 )
228 $entry['exists'] = '';
229 $result[] = $entry;
230 }
231 }
232 return $result;
233 }
234
235 private function formatHeadItems( $headItems ) {
236 $result = array();
237 foreach ( $headItems as $tag => $content ) {
238 $entry = array();
239 $entry['tag'] = $tag;
240 $this->getResult()->setContent( $entry, $content );
241 $result[] = $entry;
242 }
243 return $result;
244 }
245
246 private function setIndexedTagNames( &$array, $mapping ) {
247 foreach ( $mapping as $key => $name ) {
248 if ( isset( $array[$key] ) )
249 $this->getResult()->setIndexedTagName( $array[$key], $name );
250 }
251 }
252
253 public function getAllowedParams() {
254 return array (
255 'title' => array(
256 ApiBase :: PARAM_DFLT => 'API',
257 ),
258 'text' => null,
259 'summary' => null,
260 'page' => null,
261 'redirects' => false,
262 'oldid' => null,
263 'prop' => array(
264 ApiBase :: PARAM_DFLT => 'text|langlinks|categories|links|templates|images|externallinks|sections|revid|displaytitle',
265 ApiBase :: PARAM_ISMULTI => true,
266 ApiBase :: PARAM_TYPE => array(
267 'text',
268 'langlinks',
269 'categories',
270 'links',
271 'templates',
272 'images',
273 'externallinks',
274 'sections',
275 'revid',
276 'displaytitle',
277 'headitems',
278 'headhtml'
279 )
280 ),
281 'pst' => false,
282 'onlypst' => false,
283 );
284 }
285
286 public function getParamDescription() {
287 return array (
288 'text' => 'Wikitext to parse',
289 'summary' => 'Summary to parse',
290 'redirects' => 'If the page parameter is set to a redirect, resolve it',
291 'title' => 'Title of page the text belongs to',
292 'page' => 'Parse the content of this page. Cannot be used together with text and title',
293 'oldid' => 'Parse the content of this revision. Overrides page',
294 'prop' => array( 'Which pieces of information to get.',
295 'NOTE: Section tree is only generated if there are more than 4 sections, or if the __TOC__ keyword is present'
296 ),
297 'pst' => array( 'Do a pre-save transform on the input before parsing it.',
298 'Ignored if page or oldid is used.'
299 ),
300 'onlypst' => array( 'Do a PST on the input, but don\'t parse it.',
301 'Returns PSTed wikitext. Ignored if page or oldid is used.'
302 ),
303 );
304 }
305
306 public function getDescription() {
307 return 'This module parses wikitext and returns parser output';
308 }
309
310 public function getPossibleErrors() {
311 return array_merge( parent::getPossibleErrors(), array(
312 array( 'code' => 'params', 'info' => 'The page parameter cannot be used together with the text and title parameters' ),
313 array( 'code' => 'missingrev', 'info' => 'There is no revision ID oldid' ),
314 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revisions' ),
315 array( 'code' => 'missingtitle', 'info' => 'The page you specified doesn\'t exist' ),
316 ) );
317 }
318
319 protected function getExamples() {
320 return array (
321 'api.php?action=parse&text={{Project:Sandbox}}'
322 );
323 }
324
325 public function getVersion() {
326 return __CLASS__ . ': $Id$';
327 }
328 }