1510e042bd7d6cb40deddc20748f808ef39ddb20
[lhc/web/wiklou.git] / includes / api / ApiParse.php
1 <?php
2 /**
3 * Created on Dec 01, 2007
4 *
5 * Copyright © 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 */
24
25 if ( !defined( 'MEDIAWIKI' ) ) {
26 // Eclipse helper - will be ignored in production
27 require_once( "ApiBase.php" );
28 }
29
30 /**
31 * @ingroup API
32 */
33 class ApiParse extends ApiBase {
34 private $section, $text, $pstText = null;
35
36 public function __construct( $main, $action ) {
37 parent::__construct( $main, $action );
38 }
39
40 public function execute() {
41 // The data is hot but user-dependent, like page views, so we set vary cookies
42 $this->getMain()->setCacheMode( 'anon-public-user-private' );
43
44 // Get parameters
45 $params = $this->extractRequestParams();
46 $text = $params['text'];
47 $title = $params['title'];
48 $page = $params['page'];
49 $pageid = $params['pageid'];
50 $oldid = $params['oldid'];
51
52 if ( !is_null( $page ) && ( !is_null( $text ) || $title != 'API' ) ) {
53 $this->dieUsage( 'The page parameter cannot be used together with the text and title parameters', 'params' );
54 }
55 $prop = array_flip( $params['prop'] );
56
57 if ( isset( $params['section'] ) ) {
58 $this->section = $params['section'];
59 } else {
60 $this->section = false;
61 }
62
63 // The parser needs $wgTitle to be set, apparently the
64 // $title parameter in Parser::parse isn't enough *sigh*
65 global $wgParser, $wgUser, $wgTitle, $wgLang;
66
67 // Currently unnecessary, code to act as a safeguard against any change in current behaviour of uselang breaks
68 $oldLang = null;
69 if ( isset( $params['uselang'] ) && $params['uselang'] != $wgLang->getCode() ) {
70 $oldLang = $wgLang; // Backup wgLang
71 $wgLang = Language::factory( $params['uselang'] );
72 }
73
74 $popts = new ParserOptions();
75 $popts->setTidy( true );
76 $popts->enableLimitReport( !$params['disablepp'] );
77
78 $redirValues = null;
79
80 if ( !is_null( $oldid ) || !is_null( $pageid ) || !is_null( $page ) ) {
81 if ( !is_null( $oldid ) ) {
82 // Don't use the parser cache
83 $rev = Revision::newFromID( $oldid );
84 if ( !$rev ) {
85 $this->dieUsage( "There is no revision ID $oldid", 'missingrev' );
86 }
87 if ( !$rev->userCan( Revision::DELETED_TEXT ) ) {
88 $this->dieUsage( "You don't have permission to view deleted revisions", 'permissiondenied' );
89 }
90
91 $titleObj = $rev->getTitle();
92
93 $wgTitle = $titleObj;
94
95 // If for some reason the "oldid" is actually the current revision, it may be cached
96 if ( $titleObj->getLatestRevID() === intval( $oldid ) ) {
97 $articleObj = new Article( $titleObj, 0 );
98
99 $p_result = $this->getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageid,
100 isset( $prop['wikitext'] ) ) ;
101 } else { // This is an old revision, so get the text differently
102 $this->text = $rev->getText( Revision::FOR_THIS_USER );
103
104 $wgTitle = $titleObj;
105
106 if ( $this->section !== false ) {
107 $this->text = $this->getSectionText( $this->text, 'r' . $rev->getId() );
108 }
109
110 $p_result = $wgParser->parse( $this->text, $titleObj, $popts );
111 }
112 } else { // Not $oldid
113 if ( !is_null ( $pageid ) ) {
114 $titleObj = Title::newFromID( $pageid );
115
116 if ( !$titleObj ) {
117 $this->dieUsageMsg( array( 'nosuchpageid', $pageid ) );
118 }
119 } else { // $page
120 if ( $params['redirects'] ) {
121 $req = new FauxRequest( array(
122 'action' => 'query',
123 'redirects' => '',
124 'titles' => $page
125 ) );
126 $main = new ApiMain( $req );
127 $main->execute();
128 $data = $main->getResultData();
129 $redirValues = isset( $data['query']['redirects'] )
130 ? $data['query']['redirects'] : array();
131 $to = $page;
132 foreach ( (array)$redirValues as $r ) {
133 $to = $r['to'];
134 }
135 } else {
136 $to = $page;
137 }
138 $titleObj = Title::newFromText( $to );
139 if ( !$titleObj || !$titleObj->exists() ) {
140 $this->dieUsage( "The page you specified doesn't exist", 'missingtitle' );
141 }
142 }
143 $wgTitle = $titleObj;
144
145 $articleObj = new Article( $titleObj, 0 );
146 if ( isset( $prop['revid'] ) ) {
147 $oldid = $articleObj->getRevIdFetched();
148 }
149
150 $p_result = $this->getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageid,
151 isset( $prop['wikitext'] ) ) ;
152 }
153 } else { // Not $oldid, $pageid, $page. Hence based on $text
154
155 $this->text = $text;
156 $titleObj = Title::newFromText( $title );
157 if ( !$titleObj ) {
158 $titleObj = Title::newFromText( 'API' );
159 }
160 $wgTitle = $titleObj;
161
162 if ( $this->section !== false ) {
163 $this->text = $this->getSectionText( $this->text, $titleObj->getText() );
164 }
165
166 if ( $params['pst'] || $params['onlypst'] ) {
167 $this->pstText = $wgParser->preSaveTransform( $this->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'], $this->pstText );
173 if ( isset( $prop['wikitext'] ) ) {
174 $result_array['wikitext'] = array();
175 $this->getResult()->setContent( $result_array['wikitext'], $this->text );
176 }
177 $this->getResult()->addValue( null, $this->getModuleName(), $result_array );
178 return;
179 }
180 $p_result = $wgParser->parse( $params['pst'] ? $this->pstText : $this->text, $titleObj, $popts );
181 }
182
183 // Return result
184 $result = $this->getResult();
185 $result_array = array();
186
187 $result_array['title'] = $titleObj->getPrefixedText();
188
189 if ( !is_null( $oldid ) ) {
190 $result_array['revid'] = intval( $oldid );
191 }
192
193 if ( $params['redirects'] && !is_null( $redirValues ) ) {
194 $result_array['redirects'] = $redirValues;
195 }
196
197 if ( isset( $prop['text'] ) ) {
198 $result_array['text'] = array();
199 $result->setContent( $result_array['text'], $p_result->getText() );
200 }
201
202 if ( !is_null( $params['summary'] ) ) {
203 $result_array['parsedsummary'] = array();
204 $result->setContent( $result_array['parsedsummary'], $wgUser->getSkin()->formatComment( $params['summary'], $titleObj ) );
205 }
206
207 if ( isset( $prop['langlinks'] ) ) {
208 $result_array['langlinks'] = $this->formatLangLinks( $p_result->getLanguageLinks() );
209 }
210 if ( isset( $prop['languageshtml'] ) ) {
211 $languagesHtml = $this->languagesHtml( $p_result->getLanguageLinks() );
212 $result_array['languageshtml'] = array();
213 $result->setContent( $result_array['languageshtml'], $languagesHtml );
214 }
215 if ( isset( $prop['categories'] ) ) {
216 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
217 }
218 if ( isset( $prop['categorieshtml'] ) ) {
219 $categoriesHtml = $this->categoriesHtml( $p_result->getCategories() );
220 $result_array['categorieshtml'] = array();
221 $result->setContent( $result_array['categorieshtml'], $categoriesHtml );
222 }
223 if ( isset( $prop['links'] ) ) {
224 $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
225 }
226 if ( isset( $prop['templates'] ) ) {
227 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
228 }
229 if ( isset( $prop['images'] ) ) {
230 $result_array['images'] = array_keys( $p_result->getImages() );
231 }
232 if ( isset( $prop['externallinks'] ) ) {
233 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
234 }
235 if ( isset( $prop['sections'] ) ) {
236 $result_array['sections'] = $p_result->getSections();
237 }
238
239 if ( isset( $prop['displaytitle'] ) ) {
240 $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
241 $p_result->getDisplayTitle() :
242 $titleObj->getPrefixedText();
243 }
244
245 if ( isset( $prop['headitems'] ) || isset( $prop['headhtml'] ) ) {
246 $context = new RequestContext;
247 $context->output->addParserOutputNoText( $p_result );
248
249 if ( isset( $prop['headitems'] ) ) {
250 $headItems = $this->formatHeadItems( $p_result->getHeadItems() );
251
252 $context->skin->setupUserCss( $context->output );
253 $css = $this->formatCss( $context->output->buildCssLinksArray() );
254
255 $scripts = array( $context->output->getHeadScripts( $context->skin ) );
256
257 $result_array['headitems'] = array_merge( $headItems, $css, $scripts );
258 }
259
260 if ( isset( $prop['headhtml'] ) ) {
261 $result_array['headhtml'] = array();
262 $result->setContent( $result_array['headhtml'], $context->output->headElement( $context->skin ) );
263 }
264 }
265
266 if ( isset( $prop['iwlinks'] ) ) {
267 $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
268 }
269
270 if ( isset( $prop['wikitext'] ) ) {
271 $result_array['wikitext'] = array();
272 $result->setContent( $result_array['wikitext'], $this->text );
273 if ( !is_null( $this->pstText ) ) {
274 $result_array['psttext'] = array();
275 $result->setContent( $result_array['psttext'], $this->pstText );
276 }
277 }
278
279 $result_mapping = array(
280 'redirects' => 'r',
281 'langlinks' => 'll',
282 'categories' => 'cl',
283 'links' => 'pl',
284 'templates' => 'tl',
285 'images' => 'img',
286 'externallinks' => 'el',
287 'iwlinks' => 'iw',
288 'sections' => 's',
289 'headitems' => 'hi',
290 );
291 $this->setIndexedTagNames( $result_array, $result_mapping );
292 $result->addValue( null, $this->getModuleName(), $result_array );
293
294 if ( !is_null( $oldLang ) ) {
295 $wgLang = $oldLang; // Reset $wgLang to $oldLang
296 }
297 }
298
299 /**
300 * @param $articleObj Article
301 * @param $titleObj Title
302 * @param $popts ParserOptions
303 * @param $pageId Int
304 * @param $getWikitext Bool
305 * @return ParserOutput
306 */
307 private function getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageId = null, $getWikitext = false ) {
308 if ( $this->section !== false ) {
309 global $wgParser;
310
311 $this->text = $this->getSectionText( $articleObj->getRawText(), !is_null ( $pageId )
312 ? 'page id ' . $pageId : $titleObj->getText() );
313
314 return $wgParser->parse( $this->text, $titleObj, $popts );
315 } else {
316 // Try the parser cache first
317 $pout = $articleObj->getParserOutput();
318 if ( $getWikitext ) {
319 $rev = Revision::newFromTitle( $titleObj );
320 if ( $rev ) {
321 $this->text = $rev->getText();
322 }
323 }
324 return $pout;
325 }
326 }
327
328 private function getSectionText( $text, $what ) {
329 global $wgParser;
330 $text = $wgParser->getSection( $text, $this->section, false );
331 if ( $text === false ) {
332 $this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' );
333 }
334 return $text;
335 }
336
337 private function formatLangLinks( $links ) {
338 $result = array();
339 foreach ( $links as $link ) {
340 $entry = array();
341 $bits = explode( ':', $link, 2 );
342 $title = Title::newFromText( $link );
343
344 $entry['lang'] = $bits[0];
345 if ( $title ) {
346 $entry['url'] = $title->getFullURL();
347 }
348 $this->getResult()->setContent( $entry, $bits[1] );
349 $result[] = $entry;
350 }
351 return $result;
352 }
353
354 private function formatCategoryLinks( $links ) {
355 $result = array();
356 foreach ( $links as $link => $sortkey ) {
357 $entry = array();
358 $entry['sortkey'] = $sortkey;
359 $this->getResult()->setContent( $entry, $link );
360 $result[] = $entry;
361 }
362 return $result;
363 }
364
365 private function categoriesHtml( $categories ) {
366 global $wgOut, $wgUser;
367 $wgOut->addCategoryLinks( $categories );
368 return $wgUser->getSkin()->getCategories();
369 }
370
371 /**
372 * @deprecated No modern skin generates langlinks this way, please use langlinks data to generate your own html
373 */
374 private function languagesHtml( $languages ) {
375 global $wgContLang, $wgHideInterlanguageLinks;
376
377 if ( $wgHideInterlanguageLinks || count( $languages ) == 0 ) {
378 return '';
379 }
380
381 $s = htmlspecialchars( wfMsg( 'otherlanguages' ) . wfMsg( 'colon-separator' ) );
382
383 $langs = array();
384 foreach ( $languages as $l ) {
385 $nt = Title::newFromText( $l );
386 $text = $wgContLang->getLanguageName( $nt->getInterwiki() );
387
388 $langs[] = Html::element( 'a',
389 array( 'href' => $nt->getFullURL(), 'title' => $nt->getText(), 'class' => "external" ),
390 $text == '' ? $l : $text );
391 }
392
393 $s .= implode( htmlspecialchars( wfMsgExt( 'pipe-separator', 'escapenoentities' ) ), $langs );
394
395 if ( $wgContLang->isRTL() ) {
396 $s = Html::rawElement( 'span', array( 'dir' => "LTR" ), $s );
397 }
398
399 return $s;
400 }
401
402 private function formatLinks( $links ) {
403 $result = array();
404 foreach ( $links as $ns => $nslinks ) {
405 foreach ( $nslinks as $title => $id ) {
406 $entry = array();
407 $entry['ns'] = $ns;
408 $this->getResult()->setContent( $entry, Title::makeTitle( $ns, $title )->getFullText() );
409 if ( $id != 0 ) {
410 $entry['exists'] = '';
411 }
412 $result[] = $entry;
413 }
414 }
415 return $result;
416 }
417
418 private function formatIWLinks( $iw ) {
419 $result = array();
420 foreach ( $iw as $prefix => $titles ) {
421 foreach ( array_keys( $titles ) as $title ) {
422 $entry = array();
423 $entry['prefix'] = $prefix;
424
425 $title = Title::newFromText( "{$prefix}:{$title}" );
426 if ( $title ) {
427 $entry['url'] = $title->getFullURL();
428 }
429
430 $this->getResult()->setContent( $entry, $title->getFullText() );
431 $result[] = $entry;
432 }
433 }
434 return $result;
435 }
436
437 private function formatHeadItems( $headItems ) {
438 $result = array();
439 foreach ( $headItems as $tag => $content ) {
440 $entry = array();
441 $entry['tag'] = $tag;
442 $this->getResult()->setContent( $entry, $content );
443 $result[] = $entry;
444 }
445 return $result;
446 }
447
448 private function formatCss( $css ) {
449 $result = array();
450 foreach ( $css as $file => $link ) {
451 $entry = array();
452 $entry['file'] = $file;
453 $this->getResult()->setContent( $entry, $link );
454 $result[] = $entry;
455 }
456 return $result;
457 }
458
459 private function setIndexedTagNames( &$array, $mapping ) {
460 foreach ( $mapping as $key => $name ) {
461 if ( isset( $array[$key] ) ) {
462 $this->getResult()->setIndexedTagName( $array[$key], $name );
463 }
464 }
465 }
466
467 public function getAllowedParams() {
468 return array(
469 'title' => array(
470 ApiBase::PARAM_DFLT => 'API',
471 ),
472 'text' => null,
473 'summary' => null,
474 'page' => null,
475 'pageid' => array(
476 ApiBase::PARAM_TYPE => 'integer',
477 ),
478 'redirects' => false,
479 'oldid' => array(
480 ApiBase::PARAM_TYPE => 'integer',
481 ),
482 'prop' => array(
483 ApiBase::PARAM_DFLT => 'text|langlinks|categories|links|templates|images|externallinks|sections|revid|displaytitle',
484 ApiBase::PARAM_ISMULTI => true,
485 ApiBase::PARAM_TYPE => array(
486 'text',
487 'langlinks',
488 'languageshtml',
489 'categories',
490 'categorieshtml',
491 'links',
492 'templates',
493 'images',
494 'externallinks',
495 'sections',
496 'revid',
497 'displaytitle',
498 'headitems',
499 'headhtml',
500 'iwlinks',
501 'wikitext',
502 )
503 ),
504 'pst' => false,
505 'onlypst' => false,
506 'uselang' => null,
507 'section' => null,
508 'disablepp' => false,
509 );
510 }
511
512 public function getParamDescription() {
513 $p = $this->getModulePrefix();
514 return array(
515 'text' => 'Wikitext to parse',
516 'summary' => 'Summary to parse',
517 'redirects' => "If the {$p}page parameter is set to a redirect, resolve it",
518 'title' => 'Title of page the text belongs to',
519 'page' => "Parse the content of this page. Cannot be used together with {$p}text and {$p}title",
520 'pageid' => "Parse the content of this page. Overrides {$p}page",
521 'oldid' => "Parse the content of this revision. Overrides {$p}page and {$p}pageid",
522 'prop' => array(
523 'Which pieces of information to get',
524 ' text - Gives the parsed text of the wikitext',
525 ' langlinks - Gives the language links in the parsed wikitext',
526 ' categories - Gives the categories in the parsed wikitext',
527 ' categorieshtml - Gives the HTML version of the categories',
528 ' languageshtml - Gives the HTML version of the language links',
529 ' links - Gives the internal links in the parsed wikitext',
530 ' templates - Gives the templates in the parsed wikitext',
531 ' images - Gives the images in the parsed wikitext',
532 ' externallinks - Gives the external links in the parsed wikitext',
533 ' sections - Gives the sections in the parsed wikitext',
534 ' revid - Adds the revision ID of the parsed page',
535 ' displaytitle - Adds the title of the parsed wikitext',
536 ' headitems - Gives items to put in the <head> of the page',
537 ' headhtml - Gives parsed <head> of the page',
538 ' iwlinks - Gives interwiki links in the parsed wikitext',
539 ' wikitext - Gives the original wikitext that was parsed',
540 ),
541 'pst' => array(
542 'Do a pre-save transform on the input before parsing it',
543 'Ignored if page, pageid or oldid is used'
544 ),
545 'onlypst' => array(
546 'Do a pre-save transform (PST) on the input, but don\'t parse it',
547 'Returns the same wikitext, after a PST has been applied. Ignored if page, pageid or oldid is used'
548 ),
549 'uselang' => 'Which language to parse the request in',
550 'section' => 'Only retrieve the content of this section number',
551 'disablepp' => 'Disable the PP Report from the parser output',
552 );
553 }
554
555 public function getDescription() {
556 return 'Parses wikitext and returns parser output';
557 }
558
559 public function getPossibleErrors() {
560 return array_merge( parent::getPossibleErrors(), array(
561 array( 'code' => 'params', 'info' => 'The page parameter cannot be used together with the text and title parameters' ),
562 array( 'code' => 'missingrev', 'info' => 'There is no revision ID oldid' ),
563 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revisions' ),
564 array( 'code' => 'missingtitle', 'info' => 'The page you specified doesn\'t exist' ),
565 array( 'code' => 'nosuchsection', 'info' => 'There is no section sectionnumber in page' ),
566 array( 'nosuchpageid' ),
567 ) );
568 }
569
570 protected function getExamples() {
571 return array(
572 'api.php?action=parse&text={{Project:Sandbox}}'
573 );
574 }
575
576 public function getVersion() {
577 return __CLASS__ . ': $Id$';
578 }
579 }