Remove some unneeded whitespace
[lhc/web/wiklou.git] / includes / api / ApiParse.php
1 <?php
2 /**
3 * API for MediaWiki 1.8+
4 *
5 * Created on Dec 01, 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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiBase.php" );
30 }
31
32 /**
33 * @ingroup API
34 */
35 class ApiParse extends ApiBase {
36
37 public function __construct( $main, $action ) {
38 parent::__construct( $main, $action );
39 }
40
41 public function execute() {
42 // The data is hot but user-dependent, like page views, so we set vary cookies
43 $this->getMain()->setCacheMode( 'anon-public-user-private' );
44
45 // Get parameters
46 $params = $this->extractRequestParams();
47 $text = $params['text'];
48 $title = $params['title'];
49 $page = $params['page'];
50 $pageid = $params['pageid'];
51 $oldid = $params['oldid'];
52
53 if ( !is_null( $page ) && ( !is_null( $text ) || $title != 'API' ) ) {
54 $this->dieUsage( 'The page parameter cannot be used together with the text and title parameters', 'params' );
55 }
56 $prop = array_flip( $params['prop'] );
57
58 if ( isset( $params['section'] ) ) {
59 $this->section = $params['section'];
60 } else {
61 $this->section = false;
62 }
63
64 // The parser needs $wgTitle to be set, apparently the
65 // $title parameter in Parser::parse isn't enough *sigh*
66 global $wgParser, $wgUser, $wgTitle, $wgEnableParserCache, $wgLang;
67
68 // Currently unnecessary, code to act as a safeguard against any change in current behaviour of uselang breaks
69 $oldLang = null;
70 if ( isset( $params['uselang'] ) && $params['uselang'] != $wgLang->getCode() ) {
71 $oldLang = $wgLang; // Backup wgLang
72 $wgLang = Language::factory( $params['uselang'] );
73 }
74
75 $popts = new ParserOptions();
76 $popts->setTidy( true );
77 $popts->enableLimitReport( !$params['disablepp'] );
78 $redirValues = null;
79 if ( !is_null( $oldid ) || !is_null( $pageid ) || !is_null( $page ) ) {
80 if ( !is_null( $oldid ) ) {
81 // Don't use the parser cache
82 $rev = Revision::newFromID( $oldid );
83 if ( !$rev ) {
84 $this->dieUsage( "There is no revision ID $oldid", 'missingrev' );
85 }
86 if ( !$rev->userCan( Revision::DELETED_TEXT ) ) {
87 $this->dieUsage( "You don't have permission to view deleted revisions", 'permissiondenied' );
88 }
89
90 $text = $rev->getText( Revision::FOR_THIS_USER );
91 $titleObj = $rev->getTitle();
92 $wgTitle = $titleObj;
93
94 if ( $this->section !== false ) {
95 $text = $this->getSectionText( $text, 'r' . $rev );
96 }
97
98 $p_result = $wgParser->parse( $text, $titleObj, $popts );
99 } else {
100 if ( !is_null ( $pageid ) ) {
101 $titleObj = Title::newFromID( $pageid );
102
103 if ( !$titleObj ) {
104 $this->dieUsageMsg( array( 'nosuchpageid', $pageid ) );
105 }
106 } else {
107 if ( $params['redirects'] ) {
108 $req = new FauxRequest( array(
109 'action' => 'query',
110 'redirects' => '',
111 'titles' => $page
112 ) );
113 $main = new ApiMain( $req );
114 $main->execute();
115 $data = $main->getResultData();
116 $redirValues = @$data['query']['redirects'];
117 $to = $page;
118 foreach ( (array)$redirValues as $r ) {
119 $to = $r['to'];
120 }
121 } else {
122 $to = $page;
123 }
124 $titleObj = Title::newFromText( $to );
125 if ( !$titleObj ) {
126 $this->dieUsage( "The page you specified doesn't exist", 'missingtitle' );
127 }
128 }
129 $wgTitle = $titleObj;
130
131 $articleObj = new Article( $titleObj );
132 if ( isset( $prop['revid'] ) ) {
133 $oldid = $articleObj->getRevIdFetched();
134 }
135
136 if ( $this->section !== false ) {
137 $text = $this->getSectionText( $text, !is_null ( $pageid ) ? 'page id ' . $pageid : $titleObj->getText() );
138 $p_result = $wgParser->parse( $text, $titleObj, $popts );
139 } else {
140 // Try the parser cache first
141 $p_result = false;
142 $pcache = ParserCache::singleton();
143 if ( $wgEnableParserCache ) {
144 $p_result = $pcache->get( $articleObj, $popts );
145 }
146 if ( !$p_result ) {
147 $p_result = $wgParser->parse( $articleObj->getContent(), $titleObj, $popts );
148
149 if ( $wgEnableParserCache ) {
150 $pcache->save( $p_result, $articleObj, $popts );
151 }
152 }
153 }
154 }
155 } else {
156 $titleObj = Title::newFromText( $title );
157 if ( !$titleObj ) {
158 $titleObj = Title::newFromText( 'API' );
159 }
160 $wgTitle = $titleObj;
161
162 if ( $this->section !== false ) {
163 $text = $this->getSectionText( $text, $titleObj->getText() );
164 }
165
166 if ( $params['pst'] || $params['onlypst'] ) {
167 $text = $wgParser->preSaveTransform( $text, $titleObj, $wgUser, $popts );
168 }
169 if ( $params['onlypst'] ) {
170 // Build a result and bail out
171 $result_array['text'] = array();
172 $this->getResult()->setContent( $result_array['text'], $text );
173 $this->getResult()->addValue( null, $this->getModuleName(), $result_array );
174 return;
175 }
176 $p_result = $wgParser->parse( $text, $titleObj, $popts );
177 }
178
179 // Return result
180 $result = $this->getResult();
181 $result_array = array();
182 if ( $params['redirects'] && !is_null( $redirValues ) ) {
183 $result_array['redirects'] = $redirValues;
184 }
185
186 if ( isset( $prop['text'] ) ) {
187 $result_array['text'] = array();
188 $result->setContent( $result_array['text'], $p_result->getText() );
189 }
190
191 if ( !is_null( $params['summary'] ) ) {
192 $result_array['parsedsummary'] = array();
193 $result->setContent( $result_array['parsedsummary'], $wgUser->getSkin()->formatComment( $params['summary'], $titleObj ) );
194 }
195
196 if ( isset( $prop['langlinks'] ) ) {
197 $result_array['langlinks'] = $this->formatLangLinks( $p_result->getLanguageLinks() );
198 }
199 if ( isset( $prop['categories'] ) ) {
200 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
201 }
202 if ( isset( $prop['links'] ) ) {
203 $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
204 }
205 if ( isset( $prop['templates'] ) ) {
206 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
207 }
208 if ( isset( $prop['images'] ) ) {
209 $result_array['images'] = array_keys( $p_result->getImages() );
210 }
211 if ( isset( $prop['externallinks'] ) ) {
212 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
213 }
214 if ( isset( $prop['sections'] ) ) {
215 $result_array['sections'] = $p_result->getSections();
216 }
217
218 if ( isset( $prop['displaytitle'] ) ) {
219 $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
220 $p_result->getDisplayTitle() :
221 $titleObj->getPrefixedText();
222 }
223
224 if ( isset( $prop['headitems'] ) || isset( $prop['headhtml'] ) ) {
225 $out = new OutputPage;
226 $out->addParserOutputNoText( $p_result );
227 $userSkin = $wgUser->getSkin();
228 }
229
230 if ( isset( $prop['headitems'] ) ) {
231 $headItems = $this->formatHeadItems( $p_result->getHeadItems() );
232
233 $userSkin->setupUserCss( $out );
234 $css = $this->formatCss( $out->buildCssLinksArray() );
235
236 $scripts = array( $out->getHeadScripts( $userSkin ) );
237
238 $result_array['headitems'] = array_merge( $headItems, $css, $scripts );
239 }
240
241 if ( isset( $prop['headhtml'] ) ) {
242 $result_array['headhtml'] = array();
243 $result->setContent( $result_array['headhtml'], $out->headElement( $userSkin ) );
244 }
245
246 if ( isset( $prop['iwlinks'] ) ) {
247 $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
248 }
249
250 if ( !is_null( $oldid ) ) {
251 $result_array['revid'] = intval( $oldid );
252 }
253
254 $result_mapping = array(
255 'redirects' => 'r',
256 'langlinks' => 'll',
257 'categories' => 'cl',
258 'links' => 'pl',
259 'templates' => 'tl',
260 'images' => 'img',
261 'externallinks' => 'el',
262 'iwlinks' => 'iw',
263 'sections' => 's',
264 'headitems' => 'hi',
265 );
266 $this->setIndexedTagNames( $result_array, $result_mapping );
267 $result->addValue( null, $this->getModuleName(), $result_array );
268
269 if ( !is_null( $oldLang ) ) {
270 $wgLang = $oldLang; // Reset $wgLang to $oldLang
271 }
272 }
273
274 private function getSectionText( $text, $what ) {
275 global $wgParser;
276 $text = $wgParser->getSection( $text, $this->section, false );
277 if ( $text === false ) {
278 $this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' );
279 }
280 return $text;
281 }
282
283 private function formatLangLinks( $links ) {
284 $result = array();
285 foreach ( $links as $link ) {
286 $entry = array();
287 $bits = explode( ':', $link, 2 );
288 $entry['lang'] = $bits[0];
289 $this->getResult()->setContent( $entry, $bits[1] );
290 $result[] = $entry;
291 }
292 return $result;
293 }
294
295 private function formatCategoryLinks( $links ) {
296 $result = array();
297 foreach ( $links as $link => $sortkey ) {
298 $entry = array();
299 $entry['sortkey'] = $sortkey;
300 $this->getResult()->setContent( $entry, $link );
301 $result[] = $entry;
302 }
303 return $result;
304 }
305
306 private function formatLinks( $links ) {
307 $result = array();
308 foreach ( $links as $ns => $nslinks ) {
309 foreach ( $nslinks as $title => $id ) {
310 $entry = array();
311 $entry['ns'] = $ns;
312 $this->getResult()->setContent( $entry, Title::makeTitle( $ns, $title )->getFullText() );
313 if ( $id != 0 ) {
314 $entry['exists'] = '';
315 }
316 $result[] = $entry;
317 }
318 }
319 return $result;
320 }
321
322 private function formatIWLinks( $iw ) {
323 $result = array();
324 foreach ( $iw as $prefix => $titles ) {
325 foreach ( $titles as $title => $id ) {
326 $entry = array();
327 $entry['prefix'] = $prefix;
328
329 $title = Title::newFromText( "{$prefix}:{$title}" );
330 if ( $title ) {
331 $entry['url'] = $title->getFullURL();
332 }
333
334 $this->getResult()->setContent( $entry, $title->getFullText() );
335 $result[] = $entry;
336 }
337 }
338 return $result;
339 }
340
341 private function formatHeadItems( $headItems ) {
342 $result = array();
343 foreach ( $headItems as $tag => $content ) {
344 $entry = array();
345 $entry['tag'] = $tag;
346 $this->getResult()->setContent( $entry, $content );
347 $result[] = $entry;
348 }
349 return $result;
350 }
351
352 private function formatCss( $css ) {
353 $result = array();
354 foreach ( $css as $file => $link ) {
355 $entry = array();
356 $entry['file'] = $file;
357 $this->getResult()->setContent( $entry, $link );
358 $result[] = $entry;
359 }
360 return $result;
361 }
362
363 private function setIndexedTagNames( &$array, $mapping ) {
364 foreach ( $mapping as $key => $name ) {
365 if ( isset( $array[$key] ) ) {
366 $this->getResult()->setIndexedTagName( $array[$key], $name );
367 }
368 }
369 }
370
371 public function getAllowedParams() {
372 return array(
373 'title' => array(
374 ApiBase::PARAM_DFLT => 'API',
375 ),
376 'text' => null,
377 'summary' => null,
378 'page' => null,
379 'pageid' => null,
380 'redirects' => false,
381 'oldid' => null,
382 'prop' => array(
383 ApiBase::PARAM_DFLT => 'text|langlinks|categories|links|templates|images|externallinks|sections|revid|displaytitle',
384 ApiBase::PARAM_ISMULTI => true,
385 ApiBase::PARAM_TYPE => array(
386 'text',
387 'langlinks',
388 'categories',
389 'links',
390 'templates',
391 'images',
392 'externallinks',
393 'sections',
394 'revid',
395 'displaytitle',
396 'headitems',
397 'headhtml',
398 'iwlinks',
399 )
400 ),
401 'pst' => false,
402 'onlypst' => false,
403 'uselang' => null,
404 'section' => null,
405 'disablepp' => false,
406 );
407 }
408
409 public function getParamDescription() {
410 $p = $this->getModulePrefix();
411 return array(
412 'text' => 'Wikitext to parse',
413 'summary' => 'Summary to parse',
414 'redirects' => "If the {$p}page parameter is set to a redirect, resolve it",
415 'title' => 'Title of page the text belongs to',
416 'page' => "Parse the content of this page. Cannot be used together with {$p}text and {$p}title",
417 'pageid' => "Parse the content of this page. Overrides {$p}page",
418 'oldid' => "Parse the content of this revision. Overrides {$p}page and {$p}pageid",
419 'prop' => array(
420 'Which pieces of information to get',
421 ' text - Gives the parsed text of the wikitext',
422 ' langlinks - Gives the langlinks the parsed wikitext',
423 ' categories - Gives the categories of the parsed wikitext',
424 ' links - Gives the internal links in the parsed wikitext',
425 ' templates - Gives the templates in the parsed wikitext',
426 ' images - Gives the images in the parsed wikitext',
427 ' externallinks - Gives the external links in the parsed wikitext',
428 ' sections - Gives the sections in the parsed wikitext',
429 ' revid - Adds the revision id of the parsed page',
430 ' displaytitle - Adds the title of the parsed wikitext',
431 ' headitems - Gives items to put in the <head> of the page',
432 ' headhtml - Gives parsed <head> of the page',
433 ' iwlinks - Gives interwiki links in the parsed wikitext',
434 'NOTE: Section tree is only generated if there are more than 4 sections, or if the __TOC__ keyword is present'
435 ),
436 'pst' => array(
437 'Do a pre-save transform on the input before parsing it',
438 'Ignored if page, pageid or oldid is used'
439 ),
440 'onlypst' => array(
441 'Do a pre-save transform (PST) on the input, but don\'t parse it',
442 'Returns the same wikitext, after a PST has been applied. Ignored if page, pageid or oldid is used'
443 ),
444 'uselang' => 'Which language to parse the request in',
445 'section' => 'Only retrieve the content of this section number',
446 'disablepp' => 'Disable the PP Report from the parser output',
447 );
448 }
449
450 public function getDescription() {
451 return 'This module parses wikitext and returns parser output';
452 }
453
454 public function getPossibleErrors() {
455 return array_merge( parent::getPossibleErrors(), array(
456 array( 'code' => 'params', 'info' => 'The page parameter cannot be used together with the text and title parameters' ),
457 array( 'code' => 'missingrev', 'info' => 'There is no revision ID oldid' ),
458 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revisions' ),
459 array( 'code' => 'missingtitle', 'info' => 'The page you specified doesn\'t exist' ),
460 array( 'code' => 'nosuchsection', 'info' => 'There is no section sectionnumber in page' ),
461 array( 'nosuchpageid' ),
462 ) );
463 }
464
465 protected function getExamples() {
466 return array(
467 'api.php?action=parse&text={{Project:Sandbox}}'
468 );
469 }
470
471 public function getVersion() {
472 return __CLASS__ . ': $Id$';
473 }
474 }