Merge "Add validation of the content model edited by EditPage"
[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 ( $params['disabletoc'] ) {
253 $p_result->setTOCEnabled( false );
254 }
255
256 if ( isset( $prop['text'] ) ) {
257 $result_array['text'] = array();
258 ApiResult::setContent( $result_array['text'], $p_result->getText() );
259 }
260
261 if ( !is_null( $params['summary'] ) ) {
262 $result_array['parsedsummary'] = array();
263 ApiResult::setContent(
264 $result_array['parsedsummary'],
265 Linker::formatComment( $params['summary'], $titleObj )
266 );
267 }
268
269 if ( isset( $prop['langlinks'] ) || isset( $prop['languageshtml'] ) ) {
270 $langlinks = $p_result->getLanguageLinks();
271
272 if ( $params['effectivelanglinks'] ) {
273 // Link flags are ignored for now, but may in the future be
274 // included in the result.
275 $linkFlags = array();
276 wfRunHooks( 'LanguageLinks', array( $titleObj, &$langlinks, &$linkFlags ) );
277 }
278 } else {
279 $langlinks = false;
280 }
281
282 if ( isset( $prop['langlinks'] ) ) {
283 $result_array['langlinks'] = $this->formatLangLinks( $langlinks );
284 }
285 if ( isset( $prop['languageshtml'] ) ) {
286 $languagesHtml = $this->languagesHtml( $langlinks );
287
288 $result_array['languageshtml'] = array();
289 ApiResult::setContent( $result_array['languageshtml'], $languagesHtml );
290 }
291 if ( isset( $prop['categories'] ) ) {
292 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
293 }
294 if ( isset( $prop['categorieshtml'] ) ) {
295 $categoriesHtml = $this->categoriesHtml( $p_result->getCategories() );
296 $result_array['categorieshtml'] = array();
297 ApiResult::setContent( $result_array['categorieshtml'], $categoriesHtml );
298 }
299 if ( isset( $prop['links'] ) ) {
300 $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
301 }
302 if ( isset( $prop['templates'] ) ) {
303 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
304 }
305 if ( isset( $prop['images'] ) ) {
306 $result_array['images'] = array_keys( $p_result->getImages() );
307 }
308 if ( isset( $prop['externallinks'] ) ) {
309 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
310 }
311 if ( isset( $prop['sections'] ) ) {
312 $result_array['sections'] = $p_result->getSections();
313 }
314
315 if ( isset( $prop['displaytitle'] ) ) {
316 $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
317 $p_result->getDisplayTitle() :
318 $titleObj->getPrefixedText();
319 }
320
321 if ( isset( $prop['headitems'] ) || isset( $prop['headhtml'] ) ) {
322 $context = $this->getContext();
323 $context->setTitle( $titleObj );
324 $context->getOutput()->addParserOutputNoText( $p_result );
325
326 if ( isset( $prop['headitems'] ) ) {
327 $headItems = $this->formatHeadItems( $p_result->getHeadItems() );
328
329 $css = $this->formatCss( $context->getOutput()->buildCssLinksArray() );
330
331 $scripts = array( $context->getOutput()->getHeadScripts() );
332
333 $result_array['headitems'] = array_merge( $headItems, $css, $scripts );
334 }
335
336 if ( isset( $prop['headhtml'] ) ) {
337 $result_array['headhtml'] = array();
338 ApiResult::setContent(
339 $result_array['headhtml'],
340 $context->getOutput()->headElement( $context->getSkin() )
341 );
342 }
343 }
344
345 if ( isset( $prop['iwlinks'] ) ) {
346 $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
347 }
348
349 if ( isset( $prop['wikitext'] ) ) {
350 $result_array['wikitext'] = array();
351 ApiResult::setContent( $result_array['wikitext'], $this->content->serialize( $format ) );
352 if ( !is_null( $this->pstContent ) ) {
353 $result_array['psttext'] = array();
354 ApiResult::setContent( $result_array['psttext'], $this->pstContent->serialize( $format ) );
355 }
356 }
357 if ( isset( $prop['properties'] ) ) {
358 $result_array['properties'] = $this->formatProperties( $p_result->getProperties() );
359 }
360
361 if ( $params['generatexml'] ) {
362 if ( $this->content->getModel() != CONTENT_MODEL_WIKITEXT ) {
363 $this->dieUsage( "generatexml is only supported for wikitext content", "notwikitext" );
364 }
365
366 $wgParser->startExternalParse( $titleObj, $popts, OT_PREPROCESS );
367 $dom = $wgParser->preprocessToDom( $this->content->getNativeData() );
368 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
369 $xml = $dom->saveXML();
370 } else {
371 $xml = $dom->__toString();
372 }
373 $result_array['parsetree'] = array();
374 ApiResult::setContent( $result_array['parsetree'], $xml );
375 }
376
377 $result_mapping = array(
378 'redirects' => 'r',
379 'langlinks' => 'll',
380 'categories' => 'cl',
381 'links' => 'pl',
382 'templates' => 'tl',
383 'images' => 'img',
384 'externallinks' => 'el',
385 'iwlinks' => 'iw',
386 'sections' => 's',
387 'headitems' => 'hi',
388 'properties' => 'pp',
389 );
390 $this->setIndexedTagNames( $result_array, $result_mapping );
391 $result->addValue( null, $this->getModuleName(), $result_array );
392
393 if ( !is_null( $oldLang ) ) {
394 $this->getContext()->setLanguage( $oldLang ); // Reset language to $oldLang
395 }
396 }
397
398 /**
399 * Constructs a ParserOptions object
400 *
401 * @param WikiPage $pageObj
402 * @param array $params
403 *
404 * @return ParserOptions
405 */
406 protected function makeParserOptions( WikiPage $pageObj, array $params ) {
407 wfProfileIn( __METHOD__ );
408
409 $popts = $pageObj->makeParserOptions( $this->getContext() );
410 $popts->enableLimitReport( !$params['disablepp'] );
411 $popts->setIsPreview( $params['preview'] || $params['sectionpreview'] );
412 $popts->setIsSectionPreview( $params['sectionpreview'] );
413
414 wfProfileOut( __METHOD__ );
415
416 return $popts;
417 }
418
419 /**
420 * @param $page WikiPage
421 * @param $popts ParserOptions
422 * @param $pageId Int
423 * @param $getWikitext Bool
424 * @return ParserOutput
425 */
426 private function getParsedContent( WikiPage $page, $popts, $pageId = null, $getWikitext = false ) {
427 $this->content = $page->getContent( Revision::RAW ); //XXX: really raw?
428
429 if ( $this->section !== false && $this->content !== null ) {
430 $this->content = $this->getSectionContent(
431 $this->content,
432 !is_null( $pageId ) ? 'page id ' . $pageId : $page->getTitle()->getText()
433 );
434
435 // Not cached (save or load)
436 return $this->content->getParserOutput( $page->getTitle(), null, $popts );
437 }
438
439 // Try the parser cache first
440 // getParserOutput will save to Parser cache if able
441 $pout = $page->getParserOutput( $popts );
442 if ( !$pout ) {
443 $this->dieUsage( "There is no revision ID {$page->getLatest()}", 'missingrev' );
444 }
445 if ( $getWikitext ) {
446 $this->content = $page->getContent( Revision::RAW );
447 }
448
449 return $pout;
450 }
451
452 private function getSectionContent( Content $content, $what ) {
453 // Not cached (save or load)
454 $section = $content->getSection( $this->section );
455 if ( $section === false ) {
456 $this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' );
457 }
458 if ( $section === null ) {
459 $this->dieUsage( "Sections are not supported by " . $what, 'nosuchsection' );
460 $section = false;
461 }
462
463 return $section;
464 }
465
466 private function formatLangLinks( $links ) {
467 $result = array();
468 foreach ( $links as $link ) {
469 $entry = array();
470 $bits = explode( ':', $link, 2 );
471 $title = Title::newFromText( $link );
472
473 $entry['lang'] = $bits[0];
474 if ( $title ) {
475 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
476 }
477 ApiResult::setContent( $entry, $bits[1] );
478 $result[] = $entry;
479 }
480
481 return $result;
482 }
483
484 private function formatCategoryLinks( $links ) {
485 $result = array();
486
487 if ( !$links ) {
488 return $result;
489 }
490
491 // Fetch hiddencat property
492 $lb = new LinkBatch;
493 $lb->setArray( array( NS_CATEGORY => $links ) );
494 $db = $this->getDB();
495 $res = $db->select( array( 'page', 'page_props' ),
496 array( 'page_title', 'pp_propname' ),
497 $lb->constructSet( 'page', $db ),
498 __METHOD__,
499 array(),
500 array( 'page_props' => array(
501 'LEFT JOIN', array( 'pp_propname' => 'hiddencat', 'pp_page = page_id' )
502 ) )
503 );
504 $hiddencats = array();
505 foreach ( $res as $row ) {
506 $hiddencats[$row->page_title] = isset( $row->pp_propname );
507 }
508
509 foreach ( $links as $link => $sortkey ) {
510 $entry = array();
511 $entry['sortkey'] = $sortkey;
512 ApiResult::setContent( $entry, $link );
513 if ( !isset( $hiddencats[$link] ) ) {
514 $entry['missing'] = '';
515 } elseif ( $hiddencats[$link] ) {
516 $entry['hidden'] = '';
517 }
518 $result[] = $entry;
519 }
520
521 return $result;
522 }
523
524 private function categoriesHtml( $categories ) {
525 $context = $this->getContext();
526 $context->getOutput()->addCategoryLinks( $categories );
527
528 return $context->getSkin()->getCategories();
529 }
530
531 /**
532 * @deprecated since 1.18 No modern skin generates language links this way,
533 * please use language links data to generate your own HTML.
534 * @param $languages array
535 * @return string
536 */
537 private function languagesHtml( $languages ) {
538 wfDeprecated( __METHOD__, '1.18' );
539 $this->setWarning( '"action=parse&prop=languageshtml" is deprecated ' .
540 'and will be removed in MediaWiki 1.24. Use "prop=langlinks" ' .
541 'to generate your own HTML.' );
542
543 global $wgContLang, $wgHideInterlanguageLinks;
544
545 if ( $wgHideInterlanguageLinks || count( $languages ) == 0 ) {
546 return '';
547 }
548
549 $s = htmlspecialchars( wfMessage( 'otherlanguages' )->text() .
550 wfMessage( 'colon-separator' )->text() );
551
552 $langs = array();
553 foreach ( $languages as $l ) {
554 $nt = Title::newFromText( $l );
555 $text = Language::fetchLanguageName( $nt->getInterwiki() );
556
557 $langs[] = Html::element( 'a',
558 array( 'href' => $nt->getFullURL(), 'title' => $nt->getText(), 'class' => 'external' ),
559 $text == '' ? $l : $text );
560 }
561
562 $s .= implode( wfMessage( 'pipe-separator' )->escaped(), $langs );
563
564 if ( $wgContLang->isRTL() ) {
565 $s = Html::rawElement( 'span', array( 'dir' => 'LTR' ), $s );
566 }
567
568 return $s;
569 }
570
571 private function formatLinks( $links ) {
572 $result = array();
573 foreach ( $links as $ns => $nslinks ) {
574 foreach ( $nslinks as $title => $id ) {
575 $entry = array();
576 $entry['ns'] = $ns;
577 ApiResult::setContent( $entry, Title::makeTitle( $ns, $title )->getFullText() );
578 if ( $id != 0 ) {
579 $entry['exists'] = '';
580 }
581 $result[] = $entry;
582 }
583 }
584
585 return $result;
586 }
587
588 private function formatIWLinks( $iw ) {
589 $result = array();
590 foreach ( $iw as $prefix => $titles ) {
591 foreach ( array_keys( $titles ) as $title ) {
592 $entry = array();
593 $entry['prefix'] = $prefix;
594
595 $title = Title::newFromText( "{$prefix}:{$title}" );
596 if ( $title ) {
597 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
598 }
599
600 ApiResult::setContent( $entry, $title->getFullText() );
601 $result[] = $entry;
602 }
603 }
604
605 return $result;
606 }
607
608 private function formatHeadItems( $headItems ) {
609 $result = array();
610 foreach ( $headItems as $tag => $content ) {
611 $entry = array();
612 $entry['tag'] = $tag;
613 ApiResult::setContent( $entry, $content );
614 $result[] = $entry;
615 }
616
617 return $result;
618 }
619
620 private function formatProperties( $properties ) {
621 $result = array();
622 foreach ( $properties as $name => $value ) {
623 $entry = array();
624 $entry['name'] = $name;
625 ApiResult::setContent( $entry, $value );
626 $result[] = $entry;
627 }
628
629 return $result;
630 }
631
632 private function formatCss( $css ) {
633 $result = array();
634 foreach ( $css as $file => $link ) {
635 $entry = array();
636 $entry['file'] = $file;
637 ApiResult::setContent( $entry, $link );
638 $result[] = $entry;
639 }
640
641 return $result;
642 }
643
644 private function setIndexedTagNames( &$array, $mapping ) {
645 foreach ( $mapping as $key => $name ) {
646 if ( isset( $array[$key] ) ) {
647 $this->getResult()->setIndexedTagName( $array[$key], $name );
648 }
649 }
650 }
651
652 public function getAllowedParams() {
653 return array(
654 'title' => null,
655 'text' => null,
656 'summary' => null,
657 'page' => null,
658 'pageid' => array(
659 ApiBase::PARAM_TYPE => 'integer',
660 ),
661 'redirects' => false,
662 'oldid' => array(
663 ApiBase::PARAM_TYPE => 'integer',
664 ),
665 'prop' => array(
666 ApiBase::PARAM_DFLT => 'text|langlinks|categories|links|templates|' .
667 'images|externallinks|sections|revid|displaytitle|iwlinks|properties',
668 ApiBase::PARAM_ISMULTI => true,
669 ApiBase::PARAM_TYPE => array(
670 'text',
671 'langlinks',
672 'languageshtml',
673 'categories',
674 'categorieshtml',
675 'links',
676 'templates',
677 'images',
678 'externallinks',
679 'sections',
680 'revid',
681 'displaytitle',
682 'headitems',
683 'headhtml',
684 'iwlinks',
685 'wikitext',
686 'properties',
687 )
688 ),
689 'pst' => false,
690 'onlypst' => false,
691 'effectivelanglinks' => false,
692 'uselang' => null,
693 'section' => null,
694 'disablepp' => false,
695 'generatexml' => false,
696 'preview' => false,
697 'sectionpreview' => false,
698 'disabletoc' => false,
699 'contentformat' => array(
700 ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
701 ),
702 'contentmodel' => array(
703 ApiBase::PARAM_TYPE => ContentHandler::getContentModels(),
704 )
705 );
706 }
707
708 public function getParamDescription() {
709 $p = $this->getModulePrefix();
710 $wikitext = CONTENT_MODEL_WIKITEXT;
711
712 return array(
713 'text' => "Text to parse. Use {$p}title or {$p}contentmodel to control the content model",
714 'summary' => 'Summary to parse',
715 'redirects' => "If the {$p}page or the {$p}pageid parameter is set to a redirect, resolve it",
716 'title' => "Title of page the text belongs to. " .
717 "If omitted, \"API\" is used as the title with content model $wikitext",
718 'page' => "Parse the content of this page. Cannot be used together with {$p}text and {$p}title",
719 'pageid' => "Parse the content of this page. Overrides {$p}page",
720 'oldid' => "Parse the content of this revision. Overrides {$p}page and {$p}pageid",
721 'prop' => array(
722 'Which pieces of information to get',
723 ' text - Gives the parsed text of the wikitext',
724 ' langlinks - Gives the language links in the parsed wikitext',
725 ' categories - Gives the categories in the parsed wikitext',
726 ' categorieshtml - Gives the HTML version of the categories',
727 ' languageshtml - DEPRECATED. Will be removed in MediaWiki 1.24.',
728 ' Gives the HTML version of the language links',
729 ' links - Gives the internal links in the parsed wikitext',
730 ' templates - Gives the templates in the parsed wikitext',
731 ' images - Gives the images in the parsed wikitext',
732 ' externallinks - Gives the external links in the parsed wikitext',
733 ' sections - Gives the sections in the parsed wikitext',
734 ' revid - Adds the revision ID of the parsed page',
735 ' displaytitle - Adds the title of the parsed wikitext',
736 ' headitems - Gives items to put in the <head> of the page',
737 ' headhtml - Gives parsed <head> of the page',
738 ' iwlinks - Gives interwiki links in the parsed wikitext',
739 ' wikitext - Gives the original wikitext that was parsed',
740 ' properties - Gives various properties defined in the parsed wikitext',
741 ),
742 'effectivelanglinks' => array(
743 'Includes language links supplied by extensions',
744 '(for use with prop=langlinks|languageshtml)',
745 ),
746 'pst' => array(
747 'Do a pre-save transform on the input before parsing it',
748 "Only valid when used with {$p}text",
749 ),
750 'onlypst' => array(
751 'Do a pre-save transform (PST) on the input, but don\'t parse it',
752 'Returns the same wikitext, after a PST has been applied.',
753 "Only valid when used with {$p}text",
754 ),
755 'uselang' => 'Which language to parse the request in',
756 'section' => 'Only retrieve the content of this section number',
757 'disablepp' => 'Disable the PP Report from the parser output',
758 'generatexml' => "Generate XML parse tree (requires contentmodel=$wikitext)",
759 'preview' => 'Parse in preview mode',
760 'sectionpreview' => 'Parse in section preview mode (enables preview mode too)',
761 'disabletoc' => 'Disable table of contents in output',
762 'contentformat' => array(
763 'Content serialization format used for the input text',
764 "Only valid when used with {$p}text",
765 ),
766 'contentmodel' => array(
767 "Content model of the input text. Default is the model of the " .
768 "specified ${p}title, or $wikitext if ${p}title is not specified",
769 "Only valid when used with {$p}text",
770 ),
771 );
772 }
773
774 public function getDescription() {
775 $p = $this->getModulePrefix();
776
777 return array(
778 'Parses content and returns parser output',
779 'See the various prop-Modules of action=query to get information from the current' .
780 'version of a page',
781 'There are several ways to specify the text to parse:',
782 "1) Specify a page or revision, using {$p}page, {$p}pageid, or {$p}oldid.",
783 "2) Specify content explicitly, using {$p}text, {$p}title, and {$p}contentmodel.",
784 "3) Specify only a summary to parse. {$p}prop should be given an empty value.",
785 );
786 }
787
788 public function getPossibleErrors() {
789 return array_merge( parent::getPossibleErrors(), array(
790 array(
791 'code' => 'params',
792 'info' => 'The page parameter cannot be used together with the text and title parameters'
793 ),
794 array( 'code' => 'missingrev', 'info' => 'There is no revision ID oldid' ),
795 array(
796 'code' => 'permissiondenied',
797 'info' => 'You don\'t have permission to view deleted revisions'
798 ),
799 array( 'code' => 'missingtitle', 'info' => 'The page you specified doesn\'t exist' ),
800 array( 'code' => 'nosuchsection', 'info' => 'There is no section sectionnumber in page' ),
801 array( 'nosuchpageid' ),
802 array( 'invalidtitle', 'title' ),
803 array( 'code' => 'parseerror', 'info' => 'Failed to parse the given text.' ),
804 array(
805 'code' => 'notwikitext',
806 'info' => 'The requested operation is only supported on wikitext content.'
807 ),
808 array( 'code' => 'pagecannotexist', 'info' => "Namespace doesn't allow actual pages" ),
809 ) );
810 }
811
812 public function getExamples() {
813 return array(
814 'api.php?action=parse&page=Project:Sandbox' => 'Parse a page',
815 'api.php?action=parse&text={{Project:Sandbox}}' => 'Parse wikitext',
816 'api.php?action=parse&text={{PAGENAME}}&title=Test'
817 => 'Parse wikitext, specifying the page title',
818 'api.php?action=parse&summary=Some+[[link]]&prop=' => 'Parse a summary',
819 );
820 }
821
822 public function getHelpUrls() {
823 return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#parse';
824 }
825 }