Merge "Address errors and warnings in CodeSniffer in api/"
[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
30 /** @var String $section */
31 private $section = null;
32
33 /** @var Content $content */
34 private $content = null;
35
36 /** @var Content $pstContent */
37 private $pstContent = null;
38
39 public function execute() {
40 // The data is hot but user-dependent, like page views, so we set vary cookies
41 $this->getMain()->setCacheMode( 'anon-public-user-private' );
42
43 // Get parameters
44 $params = $this->extractRequestParams();
45 $text = $params['text'];
46 $title = $params['title'];
47 if ( $title === null ) {
48 $titleProvided = false;
49 // A title is needed for parsing, so arbitrarily choose one
50 $title = 'API';
51 } else {
52 $titleProvided = true;
53 }
54
55 $page = $params['page'];
56 $pageid = $params['pageid'];
57 $oldid = $params['oldid'];
58
59 $model = $params['contentmodel'];
60 $format = $params['contentformat'];
61
62 if ( !is_null( $page ) && ( !is_null( $text ) || $titleProvided ) ) {
63 $this->dieUsage(
64 'The page parameter cannot be used together with the text and title parameters',
65 'params'
66 );
67 }
68
69 $prop = array_flip( $params['prop'] );
70
71 if ( isset( $params['section'] ) ) {
72 $this->section = $params['section'];
73 } else {
74 $this->section = false;
75 }
76
77 // The parser needs $wgTitle to be set, apparently the
78 // $title parameter in Parser::parse isn't enough *sigh*
79 // TODO: Does this still need $wgTitle?
80 global $wgParser, $wgTitle;
81
82 // Currently unnecessary, code to act as a safeguard against any change
83 // in current behavior of uselang
84 $oldLang = null;
85 if ( isset( $params['uselang'] )
86 && $params['uselang'] != $this->getContext()->getLanguage()->getCode()
87 ) {
88 $oldLang = $this->getContext()->getLanguage(); // Backup language
89 $this->getContext()->setLanguage( Language::factory( $params['uselang'] ) );
90 }
91
92 $redirValues = null;
93
94 // Return result
95 $result = $this->getResult();
96
97 if ( !is_null( $oldid ) || !is_null( $pageid ) || !is_null( $page ) ) {
98 if ( !is_null( $oldid ) ) {
99 // Don't use the parser cache
100 $rev = Revision::newFromID( $oldid );
101 if ( !$rev ) {
102 $this->dieUsage( "There is no revision ID $oldid", 'missingrev' );
103 }
104 if ( !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
105 $this->dieUsage( "You don't have permission to view deleted revisions", 'permissiondenied' );
106 }
107
108 $titleObj = $rev->getTitle();
109 $wgTitle = $titleObj;
110 $pageObj = WikiPage::factory( $titleObj );
111 $popts = $this->makeParserOptions( $pageObj, $params );
112
113 // If for some reason the "oldid" is actually the current revision, it may be cached
114 if ( $rev->isCurrent() ) {
115 // May get from/save to parser cache
116 $p_result = $this->getParsedContent( $pageObj, $popts,
117 $pageid, isset( $prop['wikitext'] ) );
118 } else { // This is an old revision, so get the text differently
119 $this->content = $rev->getContent( Revision::FOR_THIS_USER, $this->getUser() );
120
121 if ( $this->section !== false ) {
122 $this->content = $this->getSectionContent( $this->content, 'r' . $rev->getId() );
123 }
124
125 // Should we save old revision parses to the parser cache?
126 $p_result = $this->content->getParserOutput( $titleObj, $rev->getId(), $popts );
127 }
128 } else { // Not $oldid, but $pageid or $page
129 if ( $params['redirects'] ) {
130 $reqParams = array(
131 'action' => 'query',
132 'redirects' => '',
133 );
134 if ( !is_null( $pageid ) ) {
135 $reqParams['pageids'] = $pageid;
136 } else { // $page
137 $reqParams['titles'] = $page;
138 }
139 $req = new FauxRequest( $reqParams );
140 $main = new ApiMain( $req );
141 $main->execute();
142 $data = $main->getResultData();
143 $redirValues = isset( $data['query']['redirects'] )
144 ? $data['query']['redirects']
145 : array();
146 $to = $page;
147 foreach ( (array)$redirValues as $r ) {
148 $to = $r['to'];
149 }
150 $pageParams = array( 'title' => $to );
151 } elseif ( !is_null( $pageid ) ) {
152 $pageParams = array( 'pageid' => $pageid );
153 } else { // $page
154 $pageParams = array( 'title' => $page );
155 }
156
157 $pageObj = $this->getTitleOrPageId( $pageParams, 'fromdb' );
158 $titleObj = $pageObj->getTitle();
159 if ( !$titleObj || !$titleObj->exists() ) {
160 $this->dieUsage( "The page you specified doesn't exist", 'missingtitle' );
161 }
162 $wgTitle = $titleObj;
163
164 if ( isset( $prop['revid'] ) ) {
165 $oldid = $pageObj->getLatest();
166 }
167
168 $popts = $this->makeParserOptions( $pageObj, $params );
169
170 // Potentially cached
171 $p_result = $this->getParsedContent( $pageObj, $popts, $pageid,
172 isset( $prop['wikitext'] ) );
173 }
174 } else { // Not $oldid, $pageid, $page. Hence based on $text
175 $titleObj = Title::newFromText( $title );
176 if ( !$titleObj || $titleObj->isExternal() ) {
177 $this->dieUsageMsg( array( 'invalidtitle', $title ) );
178 }
179 if ( !$titleObj->canExist() ) {
180 $this->dieUsage( "Namespace doesn't allow actual pages", 'pagecannotexist' );
181 }
182 $wgTitle = $titleObj;
183 $pageObj = WikiPage::factory( $titleObj );
184
185 $popts = $this->makeParserOptions( $pageObj, $params );
186
187 if ( is_null( $text ) ) {
188 if ( $titleProvided && ( $prop || $params['generatexml'] ) ) {
189 $this->setWarning(
190 "'title' used without 'text', and parsed page properties were requested " .
191 "(did you mean to use 'page' instead of 'title'?)"
192 );
193 }
194 // Prevent warning from ContentHandler::makeContent()
195 $text = '';
196 }
197
198 // If we are parsing text, do not use the content model of the default
199 // API title, but default to wikitext to keep BC.
200 if ( !$titleProvided && is_null( $model ) ) {
201 $model = CONTENT_MODEL_WIKITEXT;
202 $this->setWarning( "No 'title' or 'contentmodel' was given, assuming $model." );
203 }
204
205 try {
206 $this->content = ContentHandler::makeContent( $text, $titleObj, $model, $format );
207 } catch ( MWContentSerializationException $ex ) {
208 $this->dieUsage( $ex->getMessage(), 'parseerror' );
209 }
210
211 if ( $this->section !== false ) {
212 $this->content = $this->getSectionContent( $this->content, $titleObj->getText() );
213 }
214
215 if ( $params['pst'] || $params['onlypst'] ) {
216 $this->pstContent = $this->content->preSaveTransform( $titleObj, $this->getUser(), $popts );
217 }
218 if ( $params['onlypst'] ) {
219 // Build a result and bail out
220 $result_array = array();
221 $result_array['text'] = array();
222 ApiResult::setContent( $result_array['text'], $this->pstContent->serialize( $format ) );
223 if ( isset( $prop['wikitext'] ) ) {
224 $result_array['wikitext'] = array();
225 ApiResult::setContent( $result_array['wikitext'], $this->content->serialize( $format ) );
226 }
227 $result->addValue( null, $this->getModuleName(), $result_array );
228
229 return;
230 }
231
232 // Not cached (save or load)
233 if ( $params['pst'] ) {
234 $p_result = $this->pstContent->getParserOutput( $titleObj, null, $popts );
235 } else {
236 $p_result = $this->content->getParserOutput( $titleObj, null, $popts );
237 }
238 }
239
240 $result_array = array();
241
242 $result_array['title'] = $titleObj->getPrefixedText();
243
244 if ( !is_null( $oldid ) ) {
245 $result_array['revid'] = intval( $oldid );
246 }
247
248 if ( $params['redirects'] && !is_null( $redirValues ) ) {
249 $result_array['redirects'] = $redirValues;
250 }
251
252 if ( isset( $prop['text'] ) ) {
253 $result_array['text'] = array();
254 ApiResult::setContent( $result_array['text'], $p_result->getText() );
255 }
256
257 if ( !is_null( $params['summary'] ) ) {
258 $result_array['parsedsummary'] = array();
259 ApiResult::setContent(
260 $result_array['parsedsummary'],
261 Linker::formatComment( $params['summary'], $titleObj )
262 );
263 }
264
265 if ( isset( $prop['langlinks'] ) || isset( $prop['languageshtml'] ) ) {
266 $langlinks = $p_result->getLanguageLinks();
267
268 if ( $params['effectivelanglinks'] ) {
269 // Link flags are ignored for now, but may in the future be
270 // included in the result.
271 $linkFlags = array();
272 wfRunHooks( 'LanguageLinks', array( $titleObj, &$langlinks, &$linkFlags ) );
273 }
274 } else {
275 $langlinks = false;
276 }
277
278 if ( isset( $prop['langlinks'] ) ) {
279 $result_array['langlinks'] = $this->formatLangLinks( $langlinks );
280 }
281 if ( isset( $prop['languageshtml'] ) ) {
282 $languagesHtml = $this->languagesHtml( $langlinks );
283
284 $result_array['languageshtml'] = array();
285 ApiResult::setContent( $result_array['languageshtml'], $languagesHtml );
286 }
287 if ( isset( $prop['categories'] ) ) {
288 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
289 }
290 if ( isset( $prop['categorieshtml'] ) ) {
291 $categoriesHtml = $this->categoriesHtml( $p_result->getCategories() );
292 $result_array['categorieshtml'] = array();
293 ApiResult::setContent( $result_array['categorieshtml'], $categoriesHtml );
294 }
295 if ( isset( $prop['links'] ) ) {
296 $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
297 }
298 if ( isset( $prop['templates'] ) ) {
299 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
300 }
301 if ( isset( $prop['images'] ) ) {
302 $result_array['images'] = array_keys( $p_result->getImages() );
303 }
304 if ( isset( $prop['externallinks'] ) ) {
305 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
306 }
307 if ( isset( $prop['sections'] ) ) {
308 $result_array['sections'] = $p_result->getSections();
309 }
310
311 if ( isset( $prop['displaytitle'] ) ) {
312 $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
313 $p_result->getDisplayTitle() :
314 $titleObj->getPrefixedText();
315 }
316
317 if ( isset( $prop['headitems'] ) || isset( $prop['headhtml'] ) ) {
318 $context = $this->getContext();
319 $context->setTitle( $titleObj );
320 $context->getOutput()->addParserOutputNoText( $p_result );
321
322 if ( isset( $prop['headitems'] ) ) {
323 $headItems = $this->formatHeadItems( $p_result->getHeadItems() );
324
325 $css = $this->formatCss( $context->getOutput()->buildCssLinksArray() );
326
327 $scripts = array( $context->getOutput()->getHeadScripts() );
328
329 $result_array['headitems'] = array_merge( $headItems, $css, $scripts );
330 }
331
332 if ( isset( $prop['headhtml'] ) ) {
333 $result_array['headhtml'] = array();
334 ApiResult::setContent(
335 $result_array['headhtml'],
336 $context->getOutput()->headElement( $context->getSkin() )
337 );
338 }
339 }
340
341 if ( isset( $prop['iwlinks'] ) ) {
342 $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
343 }
344
345 if ( isset( $prop['wikitext'] ) ) {
346 $result_array['wikitext'] = array();
347 ApiResult::setContent( $result_array['wikitext'], $this->content->serialize( $format ) );
348 if ( !is_null( $this->pstContent ) ) {
349 $result_array['psttext'] = array();
350 ApiResult::setContent( $result_array['psttext'], $this->pstContent->serialize( $format ) );
351 }
352 }
353 if ( isset( $prop['properties'] ) ) {
354 $result_array['properties'] = $this->formatProperties( $p_result->getProperties() );
355 }
356
357 if ( $params['generatexml'] ) {
358 if ( $this->content->getModel() != CONTENT_MODEL_WIKITEXT ) {
359 $this->dieUsage( "generatexml is only supported for wikitext content", "notwikitext" );
360 }
361
362 $wgParser->startExternalParse( $titleObj, $popts, OT_PREPROCESS );
363 $dom = $wgParser->preprocessToDom( $this->content->getNativeData() );
364 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
365 $xml = $dom->saveXML();
366 } else {
367 $xml = $dom->__toString();
368 }
369 $result_array['parsetree'] = array();
370 ApiResult::setContent( $result_array['parsetree'], $xml );
371 }
372
373 $result_mapping = array(
374 'redirects' => 'r',
375 'langlinks' => 'll',
376 'categories' => 'cl',
377 'links' => 'pl',
378 'templates' => 'tl',
379 'images' => 'img',
380 'externallinks' => 'el',
381 'iwlinks' => 'iw',
382 'sections' => 's',
383 'headitems' => 'hi',
384 'properties' => 'pp',
385 );
386 $this->setIndexedTagNames( $result_array, $result_mapping );
387 $result->addValue( null, $this->getModuleName(), $result_array );
388
389 if ( !is_null( $oldLang ) ) {
390 $this->getContext()->setLanguage( $oldLang ); // Reset language to $oldLang
391 }
392 }
393
394 /**
395 * Constructs a ParserOptions object
396 *
397 * @param WikiPage $pageObj
398 * @param array $params
399 *
400 * @return ParserOptions
401 */
402 protected function makeParserOptions( WikiPage $pageObj, array $params ) {
403 wfProfileIn( __METHOD__ );
404
405 $popts = $pageObj->makeParserOptions( $this->getContext() );
406 $popts->enableLimitReport( !$params['disablepp'] );
407 $popts->setIsPreview( $params['preview'] || $params['sectionpreview'] );
408 $popts->setIsSectionPreview( $params['sectionpreview'] );
409
410 wfProfileOut( __METHOD__ );
411
412 return $popts;
413 }
414
415 /**
416 * @param $page WikiPage
417 * @param $popts ParserOptions
418 * @param $pageId Int
419 * @param $getWikitext Bool
420 * @return ParserOutput
421 */
422 private function getParsedContent( WikiPage $page, $popts, $pageId = null, $getWikitext = false ) {
423 $this->content = $page->getContent( Revision::RAW ); //XXX: really raw?
424
425 if ( $this->section !== false && $this->content !== null ) {
426 $this->content = $this->getSectionContent(
427 $this->content,
428 !is_null( $pageId ) ? 'page id ' . $pageId : $page->getTitle()->getText() );
429
430 // Not cached (save or load)
431 return $this->content->getParserOutput( $page->getTitle(), null, $popts );
432 } else {
433 // Try the parser cache first
434 // getParserOutput will save to Parser cache if able
435 $pout = $page->getParserOutput( $popts );
436 if ( !$pout ) {
437 $this->dieUsage( "There is no revision ID {$page->getLatest()}", 'missingrev' );
438 }
439 if ( $getWikitext ) {
440 $this->content = $page->getContent( Revision::RAW );
441 }
442
443 return $pout;
444 }
445 }
446
447 private function getSectionContent( Content $content, $what ) {
448 // Not cached (save or load)
449 $section = $content->getSection( $this->section );
450 if ( $section === false ) {
451 $this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' );
452 }
453 if ( $section === null ) {
454 $this->dieUsage( "Sections are not supported by " . $what, 'nosuchsection' );
455 $section = false;
456 }
457
458 return $section;
459 }
460
461 private function formatLangLinks( $links ) {
462 $result = array();
463 foreach ( $links as $link ) {
464 $entry = array();
465 $bits = explode( ':', $link, 2 );
466 $title = Title::newFromText( $link );
467
468 $entry['lang'] = $bits[0];
469 if ( $title ) {
470 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
471 }
472 ApiResult::setContent( $entry, $bits[1] );
473 $result[] = $entry;
474 }
475
476 return $result;
477 }
478
479 private function formatCategoryLinks( $links ) {
480 $result = array();
481
482 if ( !$links ) {
483 return $result;
484 }
485
486 // Fetch hiddencat property
487 $lb = new LinkBatch;
488 $lb->setArray( array( NS_CATEGORY => $links ) );
489 $db = $this->getDB();
490 $res = $db->select( array( 'page', 'page_props' ),
491 array( 'page_title', 'pp_propname' ),
492 $lb->constructSet( 'page', $db ),
493 __METHOD__,
494 array(),
495 array( 'page_props' => array(
496 'LEFT JOIN', array( 'pp_propname' => 'hiddencat', 'pp_page = page_id' )
497 ) )
498 );
499 $hiddencats = array();
500 foreach ( $res as $row ) {
501 $hiddencats[$row->page_title] = isset( $row->pp_propname );
502 }
503
504 foreach ( $links as $link => $sortkey ) {
505 $entry = array();
506 $entry['sortkey'] = $sortkey;
507 ApiResult::setContent( $entry, $link );
508 if ( !isset( $hiddencats[$link] ) ) {
509 $entry['missing'] = '';
510 } elseif ( $hiddencats[$link] ) {
511 $entry['hidden'] = '';
512 }
513 $result[] = $entry;
514 }
515
516 return $result;
517 }
518
519 private function categoriesHtml( $categories ) {
520 $context = $this->getContext();
521 $context->getOutput()->addCategoryLinks( $categories );
522
523 return $context->getSkin()->getCategories();
524 }
525
526 /**
527 * @deprecated since 1.18 No modern skin generates language links this way,
528 * please use language links data to generate your own HTML.
529 * @param $languages array
530 * @return string
531 */
532 private function languagesHtml( $languages ) {
533 wfDeprecated( __METHOD__, '1.18' );
534
535 global $wgContLang, $wgHideInterlanguageLinks;
536
537 if ( $wgHideInterlanguageLinks || count( $languages ) == 0 ) {
538 return '';
539 }
540
541 $s = htmlspecialchars( wfMessage( 'otherlanguages' )->text() .
542 wfMessage( 'colon-separator' )->text() );
543
544 $langs = array();
545 foreach ( $languages as $l ) {
546 $nt = Title::newFromText( $l );
547 $text = Language::fetchLanguageName( $nt->getInterwiki() );
548
549 $langs[] = Html::element( 'a',
550 array( 'href' => $nt->getFullURL(), 'title' => $nt->getText(), 'class' => 'external' ),
551 $text == '' ? $l : $text );
552 }
553
554 $s .= implode( wfMessage( 'pipe-separator' )->escaped(), $langs );
555
556 if ( $wgContLang->isRTL() ) {
557 $s = Html::rawElement( 'span', array( 'dir' => 'LTR' ), $s );
558 }
559
560 return $s;
561 }
562
563 private function formatLinks( $links ) {
564 $result = array();
565 foreach ( $links as $ns => $nslinks ) {
566 foreach ( $nslinks as $title => $id ) {
567 $entry = array();
568 $entry['ns'] = $ns;
569 ApiResult::setContent( $entry, Title::makeTitle( $ns, $title )->getFullText() );
570 if ( $id != 0 ) {
571 $entry['exists'] = '';
572 }
573 $result[] = $entry;
574 }
575 }
576
577 return $result;
578 }
579
580 private function formatIWLinks( $iw ) {
581 $result = array();
582 foreach ( $iw as $prefix => $titles ) {
583 foreach ( array_keys( $titles ) as $title ) {
584 $entry = array();
585 $entry['prefix'] = $prefix;
586
587 $title = Title::newFromText( "{$prefix}:{$title}" );
588 if ( $title ) {
589 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
590 }
591
592 ApiResult::setContent( $entry, $title->getFullText() );
593 $result[] = $entry;
594 }
595 }
596
597 return $result;
598 }
599
600 private function formatHeadItems( $headItems ) {
601 $result = array();
602 foreach ( $headItems as $tag => $content ) {
603 $entry = array();
604 $entry['tag'] = $tag;
605 ApiResult::setContent( $entry, $content );
606 $result[] = $entry;
607 }
608
609 return $result;
610 }
611
612 private function formatProperties( $properties ) {
613 $result = array();
614 foreach ( $properties as $name => $value ) {
615 $entry = array();
616 $entry['name'] = $name;
617 ApiResult::setContent( $entry, $value );
618 $result[] = $entry;
619 }
620
621 return $result;
622 }
623
624 private function formatCss( $css ) {
625 $result = array();
626 foreach ( $css as $file => $link ) {
627 $entry = array();
628 $entry['file'] = $file;
629 ApiResult::setContent( $entry, $link );
630 $result[] = $entry;
631 }
632
633 return $result;
634 }
635
636 private function setIndexedTagNames( &$array, $mapping ) {
637 foreach ( $mapping as $key => $name ) {
638 if ( isset( $array[$key] ) ) {
639 $this->getResult()->setIndexedTagName( $array[$key], $name );
640 }
641 }
642 }
643
644 public function getAllowedParams() {
645 return array(
646 'title' => null,
647 'text' => null,
648 'summary' => null,
649 'page' => null,
650 'pageid' => array(
651 ApiBase::PARAM_TYPE => 'integer',
652 ),
653 'redirects' => false,
654 'oldid' => array(
655 ApiBase::PARAM_TYPE => 'integer',
656 ),
657 'prop' => array(
658 ApiBase::PARAM_DFLT => 'text|langlinks|categories|links|templates|' .
659 'images|externallinks|sections|revid|displaytitle|iwlinks|properties',
660 ApiBase::PARAM_ISMULTI => true,
661 ApiBase::PARAM_TYPE => array(
662 'text',
663 'langlinks',
664 'languageshtml',
665 'categories',
666 'categorieshtml',
667 'links',
668 'templates',
669 'images',
670 'externallinks',
671 'sections',
672 'revid',
673 'displaytitle',
674 'headitems',
675 'headhtml',
676 'iwlinks',
677 'wikitext',
678 'properties',
679 )
680 ),
681 'pst' => false,
682 'onlypst' => false,
683 'effectivelanglinks' => false,
684 'uselang' => null,
685 'section' => null,
686 'disablepp' => false,
687 'generatexml' => false,
688 'preview' => false,
689 'sectionpreview' => false,
690 'contentformat' => array(
691 ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
692 ),
693 'contentmodel' => array(
694 ApiBase::PARAM_TYPE => ContentHandler::getContentModels(),
695 )
696 );
697 }
698
699 public function getParamDescription() {
700 $p = $this->getModulePrefix();
701 $wikitext = CONTENT_MODEL_WIKITEXT;
702
703 return array(
704 'text' => "Text to parse. Use {$p}title or {$p}contentmodel to control the content model",
705 'summary' => 'Summary to parse',
706 'redirects' => "If the {$p}page or the {$p}pageid parameter is set to a redirect, resolve it",
707 'title' => "Title of page the text belongs to. " .
708 "If omitted, \"API\" is used as the title with content model $wikitext",
709 'page' => "Parse the content of this page. Cannot be used together with {$p}text and {$p}title",
710 'pageid' => "Parse the content of this page. Overrides {$p}page",
711 'oldid' => "Parse the content of this revision. Overrides {$p}page and {$p}pageid",
712 'prop' => array(
713 'Which pieces of information to get',
714 ' text - Gives the parsed text of the wikitext',
715 ' langlinks - Gives the language links in the parsed wikitext',
716 ' categories - Gives the categories in the parsed wikitext',
717 ' categorieshtml - Gives the HTML version of the categories',
718 ' languageshtml - Gives the HTML version of the language links',
719 ' links - Gives the internal links in the parsed wikitext',
720 ' templates - Gives the templates in the parsed wikitext',
721 ' images - Gives the images in the parsed wikitext',
722 ' externallinks - Gives the external links in the parsed wikitext',
723 ' sections - Gives the sections in the parsed wikitext',
724 ' revid - Adds the revision ID of the parsed page',
725 ' displaytitle - Adds the title of the parsed wikitext',
726 ' headitems - Gives items to put in the <head> of the page',
727 ' headhtml - Gives parsed <head> of the page',
728 ' iwlinks - Gives interwiki links in the parsed wikitext',
729 ' wikitext - Gives the original wikitext that was parsed',
730 ' properties - Gives various properties defined in the parsed wikitext',
731 ),
732 'effectivelanglinks' => array(
733 'Includes language links supplied by extensions',
734 '(for use with prop=langlinks|languageshtml)',
735 ),
736 'pst' => array(
737 'Do a pre-save transform on the input before parsing it',
738 "Only valid when used with {$p}text",
739 ),
740 'onlypst' => array(
741 'Do a pre-save transform (PST) on the input, but don\'t parse it',
742 'Returns the same wikitext, after a PST has been applied.',
743 "Only valid when used with {$p}text",
744 ),
745 'uselang' => 'Which language to parse the request in',
746 'section' => 'Only retrieve the content of this section number',
747 'disablepp' => 'Disable the PP Report from the parser output',
748 'generatexml' => "Generate XML parse tree (requires contentmodel=$wikitext)",
749 'preview' => 'Parse in preview mode',
750 'sectionpreview' => 'Parse in section preview mode (enables preview mode too)',
751 'contentformat' => array(
752 'Content serialization format used for the input text',
753 "Only valid when used with {$p}text",
754 ),
755 'contentmodel' => array(
756 "Content model of the input text. Default is the model of the " .
757 "specified ${p}title, or $wikitext if ${p}title is not specified",
758 "Only valid when used with {$p}text",
759 ),
760 );
761 }
762
763 public function getDescription() {
764 $p = $this->getModulePrefix();
765
766 return array(
767 'Parses content and returns parser output',
768 'See the various prop-Modules of action=query to get information from the current' .
769 'version of a page',
770 'There are several ways to specify the text to parse:',
771 "1) Specify a page or revision, using {$p}page, {$p}pageid, or {$p}oldid.",
772 "2) Specify content explicitly, using {$p}text, {$p}title, and {$p}contentmodel.",
773 "3) Specify only a summary to parse. {$p}prop should be given an empty value.",
774 );
775 }
776
777 public function getPossibleErrors() {
778 return array_merge( parent::getPossibleErrors(), array(
779 array(
780 'code' => 'params',
781 'info' => 'The page parameter cannot be used together with the text and title parameters'
782 ),
783 array( 'code' => 'missingrev', 'info' => 'There is no revision ID oldid' ),
784 array(
785 'code' => 'permissiondenied',
786 'info' => 'You don\'t have permission to view deleted revisions'
787 ),
788 array( 'code' => 'missingtitle', 'info' => 'The page you specified doesn\'t exist' ),
789 array( 'code' => 'nosuchsection', 'info' => 'There is no section sectionnumber in page' ),
790 array( 'nosuchpageid' ),
791 array( 'invalidtitle', 'title' ),
792 array( 'code' => 'parseerror', 'info' => 'Failed to parse the given text.' ),
793 array(
794 'code' => 'notwikitext',
795 'info' => 'The requested operation is only supported on wikitext content.'
796 ),
797 array( 'code' => 'pagecannotexist', 'info' => "Namespace doesn't allow actual pages" ),
798 ) );
799 }
800
801 public function getExamples() {
802 return array(
803 'api.php?action=parse&page=Project:Sandbox' => 'Parse a page',
804 'api.php?action=parse&text={{Project:Sandbox}}' => 'Parse wikitext',
805 'api.php?action=parse&text={{PAGENAME}}&title=Test'
806 => 'Parse wikitext, specifying the page title',
807 'api.php?action=parse&summary=Some+[[link]]&prop=' => 'Parse a summary',
808 );
809 }
810
811 public function getHelpUrls() {
812 return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#parse';
813 }
814 }