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