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