(bug 47070) check content model namespace on import.
[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
431 // Not cached (save or load)
432 return $this->content->getParserOutput( $page->getTitle(), null, $popts );
433 }
434
435 // Try the parser cache first
436 // getParserOutput will save to Parser cache if able
437 $pout = $page->getParserOutput( $popts );
438 if ( !$pout ) {
439 $this->dieUsage( "There is no revision ID {$page->getLatest()}", 'missingrev' );
440 }
441 if ( $getWikitext ) {
442 $this->content = $page->getContent( Revision::RAW );
443 }
444
445 return $pout;
446 }
447
448 private function getSectionContent( Content $content, $what ) {
449 // Not cached (save or load)
450 $section = $content->getSection( $this->section );
451 if ( $section === false ) {
452 $this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' );
453 }
454 if ( $section === null ) {
455 $this->dieUsage( "Sections are not supported by " . $what, 'nosuchsection' );
456 $section = false;
457 }
458
459 return $section;
460 }
461
462 private function formatLangLinks( $links ) {
463 $result = array();
464 foreach ( $links as $link ) {
465 $entry = array();
466 $bits = explode( ':', $link, 2 );
467 $title = Title::newFromText( $link );
468
469 $entry['lang'] = $bits[0];
470 if ( $title ) {
471 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
472 }
473 ApiResult::setContent( $entry, $bits[1] );
474 $result[] = $entry;
475 }
476
477 return $result;
478 }
479
480 private function formatCategoryLinks( $links ) {
481 $result = array();
482
483 if ( !$links ) {
484 return $result;
485 }
486
487 // Fetch hiddencat property
488 $lb = new LinkBatch;
489 $lb->setArray( array( NS_CATEGORY => $links ) );
490 $db = $this->getDB();
491 $res = $db->select( array( 'page', 'page_props' ),
492 array( 'page_title', 'pp_propname' ),
493 $lb->constructSet( 'page', $db ),
494 __METHOD__,
495 array(),
496 array( 'page_props' => array(
497 'LEFT JOIN', array( 'pp_propname' => 'hiddencat', 'pp_page = page_id' )
498 ) )
499 );
500 $hiddencats = array();
501 foreach ( $res as $row ) {
502 $hiddencats[$row->page_title] = isset( $row->pp_propname );
503 }
504
505 foreach ( $links as $link => $sortkey ) {
506 $entry = array();
507 $entry['sortkey'] = $sortkey;
508 ApiResult::setContent( $entry, $link );
509 if ( !isset( $hiddencats[$link] ) ) {
510 $entry['missing'] = '';
511 } elseif ( $hiddencats[$link] ) {
512 $entry['hidden'] = '';
513 }
514 $result[] = $entry;
515 }
516
517 return $result;
518 }
519
520 private function categoriesHtml( $categories ) {
521 $context = $this->getContext();
522 $context->getOutput()->addCategoryLinks( $categories );
523
524 return $context->getSkin()->getCategories();
525 }
526
527 /**
528 * @deprecated since 1.18 No modern skin generates language links this way,
529 * please use language links data to generate your own HTML.
530 * @param $languages array
531 * @return string
532 */
533 private function languagesHtml( $languages ) {
534 wfDeprecated( __METHOD__, '1.18' );
535 $this->setWarning( '"action=parse&prop=languageshtml" is deprecated ' .
536 'and will be removed in MediaWiki 1.24. Use "prop=langlinks" ' .
537 'to generate your own HTML.' );
538
539 global $wgContLang, $wgHideInterlanguageLinks;
540
541 if ( $wgHideInterlanguageLinks || count( $languages ) == 0 ) {
542 return '';
543 }
544
545 $s = htmlspecialchars( wfMessage( 'otherlanguages' )->text() .
546 wfMessage( 'colon-separator' )->text() );
547
548 $langs = array();
549 foreach ( $languages as $l ) {
550 $nt = Title::newFromText( $l );
551 $text = Language::fetchLanguageName( $nt->getInterwiki() );
552
553 $langs[] = Html::element( 'a',
554 array( 'href' => $nt->getFullURL(), 'title' => $nt->getText(), 'class' => 'external' ),
555 $text == '' ? $l : $text );
556 }
557
558 $s .= implode( wfMessage( 'pipe-separator' )->escaped(), $langs );
559
560 if ( $wgContLang->isRTL() ) {
561 $s = Html::rawElement( 'span', array( 'dir' => 'LTR' ), $s );
562 }
563
564 return $s;
565 }
566
567 private function formatLinks( $links ) {
568 $result = array();
569 foreach ( $links as $ns => $nslinks ) {
570 foreach ( $nslinks as $title => $id ) {
571 $entry = array();
572 $entry['ns'] = $ns;
573 ApiResult::setContent( $entry, Title::makeTitle( $ns, $title )->getFullText() );
574 if ( $id != 0 ) {
575 $entry['exists'] = '';
576 }
577 $result[] = $entry;
578 }
579 }
580
581 return $result;
582 }
583
584 private function formatIWLinks( $iw ) {
585 $result = array();
586 foreach ( $iw as $prefix => $titles ) {
587 foreach ( array_keys( $titles ) as $title ) {
588 $entry = array();
589 $entry['prefix'] = $prefix;
590
591 $title = Title::newFromText( "{$prefix}:{$title}" );
592 if ( $title ) {
593 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
594 }
595
596 ApiResult::setContent( $entry, $title->getFullText() );
597 $result[] = $entry;
598 }
599 }
600
601 return $result;
602 }
603
604 private function formatHeadItems( $headItems ) {
605 $result = array();
606 foreach ( $headItems as $tag => $content ) {
607 $entry = array();
608 $entry['tag'] = $tag;
609 ApiResult::setContent( $entry, $content );
610 $result[] = $entry;
611 }
612
613 return $result;
614 }
615
616 private function formatProperties( $properties ) {
617 $result = array();
618 foreach ( $properties as $name => $value ) {
619 $entry = array();
620 $entry['name'] = $name;
621 ApiResult::setContent( $entry, $value );
622 $result[] = $entry;
623 }
624
625 return $result;
626 }
627
628 private function formatCss( $css ) {
629 $result = array();
630 foreach ( $css as $file => $link ) {
631 $entry = array();
632 $entry['file'] = $file;
633 ApiResult::setContent( $entry, $link );
634 $result[] = $entry;
635 }
636
637 return $result;
638 }
639
640 private function setIndexedTagNames( &$array, $mapping ) {
641 foreach ( $mapping as $key => $name ) {
642 if ( isset( $array[$key] ) ) {
643 $this->getResult()->setIndexedTagName( $array[$key], $name );
644 }
645 }
646 }
647
648 public function getAllowedParams() {
649 return array(
650 'title' => null,
651 'text' => null,
652 'summary' => null,
653 'page' => null,
654 'pageid' => array(
655 ApiBase::PARAM_TYPE => 'integer',
656 ),
657 'redirects' => false,
658 'oldid' => array(
659 ApiBase::PARAM_TYPE => 'integer',
660 ),
661 'prop' => array(
662 ApiBase::PARAM_DFLT => 'text|langlinks|categories|links|templates|' .
663 'images|externallinks|sections|revid|displaytitle|iwlinks|properties',
664 ApiBase::PARAM_ISMULTI => true,
665 ApiBase::PARAM_TYPE => array(
666 'text',
667 'langlinks',
668 'languageshtml',
669 'categories',
670 'categorieshtml',
671 'links',
672 'templates',
673 'images',
674 'externallinks',
675 'sections',
676 'revid',
677 'displaytitle',
678 'headitems',
679 'headhtml',
680 'iwlinks',
681 'wikitext',
682 'properties',
683 )
684 ),
685 'pst' => false,
686 'onlypst' => false,
687 'effectivelanglinks' => false,
688 'uselang' => null,
689 'section' => null,
690 'disablepp' => false,
691 'generatexml' => false,
692 'preview' => false,
693 'sectionpreview' => false,
694 'contentformat' => array(
695 ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
696 ),
697 'contentmodel' => array(
698 ApiBase::PARAM_TYPE => ContentHandler::getContentModels(),
699 )
700 );
701 }
702
703 public function getParamDescription() {
704 $p = $this->getModulePrefix();
705 $wikitext = CONTENT_MODEL_WIKITEXT;
706
707 return array(
708 'text' => "Text to parse. Use {$p}title or {$p}contentmodel to control the content model",
709 'summary' => 'Summary to parse',
710 'redirects' => "If the {$p}page or the {$p}pageid parameter is set to a redirect, resolve it",
711 'title' => "Title of page the text belongs to. " .
712 "If omitted, \"API\" is used as the title with content model $wikitext",
713 'page' => "Parse the content of this page. Cannot be used together with {$p}text and {$p}title",
714 'pageid' => "Parse the content of this page. Overrides {$p}page",
715 'oldid' => "Parse the content of this revision. Overrides {$p}page and {$p}pageid",
716 'prop' => array(
717 'Which pieces of information to get',
718 ' text - Gives the parsed text of the wikitext',
719 ' langlinks - Gives the language links in the parsed wikitext',
720 ' categories - Gives the categories in the parsed wikitext',
721 ' categorieshtml - Gives the HTML version of the categories',
722 ' languageshtml - DEPRECATED. Will be removed in MediaWiki 1.24.',
723 ' Gives the HTML version of the language links',
724 ' links - Gives the internal links in the parsed wikitext',
725 ' templates - Gives the templates in the parsed wikitext',
726 ' images - Gives the images in the parsed wikitext',
727 ' externallinks - Gives the external links in the parsed wikitext',
728 ' sections - Gives the sections in the parsed wikitext',
729 ' revid - Adds the revision ID of the parsed page',
730 ' displaytitle - Adds the title of the parsed wikitext',
731 ' headitems - Gives items to put in the <head> of the page',
732 ' headhtml - Gives parsed <head> of the page',
733 ' iwlinks - Gives interwiki links in the parsed wikitext',
734 ' wikitext - Gives the original wikitext that was parsed',
735 ' properties - Gives various properties defined in the parsed wikitext',
736 ),
737 'effectivelanglinks' => array(
738 'Includes language links supplied by extensions',
739 '(for use with prop=langlinks|languageshtml)',
740 ),
741 'pst' => array(
742 'Do a pre-save transform on the input before parsing it',
743 "Only valid when used with {$p}text",
744 ),
745 'onlypst' => array(
746 'Do a pre-save transform (PST) on the input, but don\'t parse it',
747 'Returns the same wikitext, after a PST has been applied.',
748 "Only valid when used with {$p}text",
749 ),
750 'uselang' => 'Which language to parse the request in',
751 'section' => 'Only retrieve the content of this section number',
752 'disablepp' => 'Disable the PP Report from the parser output',
753 'generatexml' => "Generate XML parse tree (requires contentmodel=$wikitext)",
754 'preview' => 'Parse in preview mode',
755 'sectionpreview' => 'Parse in section preview mode (enables preview mode too)',
756 'contentformat' => array(
757 'Content serialization format used for the input text',
758 "Only valid when used with {$p}text",
759 ),
760 'contentmodel' => array(
761 "Content model of the input text. Default is the model of the " .
762 "specified ${p}title, or $wikitext if ${p}title is not specified",
763 "Only valid when used with {$p}text",
764 ),
765 );
766 }
767
768 public function getDescription() {
769 $p = $this->getModulePrefix();
770
771 return array(
772 'Parses content and returns parser output',
773 'See the various prop-Modules of action=query to get information from the current' .
774 'version of a page',
775 'There are several ways to specify the text to parse:',
776 "1) Specify a page or revision, using {$p}page, {$p}pageid, or {$p}oldid.",
777 "2) Specify content explicitly, using {$p}text, {$p}title, and {$p}contentmodel.",
778 "3) Specify only a summary to parse. {$p}prop should be given an empty value.",
779 );
780 }
781
782 public function getPossibleErrors() {
783 return array_merge( parent::getPossibleErrors(), array(
784 array(
785 'code' => 'params',
786 'info' => 'The page parameter cannot be used together with the text and title parameters'
787 ),
788 array( 'code' => 'missingrev', 'info' => 'There is no revision ID oldid' ),
789 array(
790 'code' => 'permissiondenied',
791 'info' => 'You don\'t have permission to view deleted revisions'
792 ),
793 array( 'code' => 'missingtitle', 'info' => 'The page you specified doesn\'t exist' ),
794 array( 'code' => 'nosuchsection', 'info' => 'There is no section sectionnumber in page' ),
795 array( 'nosuchpageid' ),
796 array( 'invalidtitle', 'title' ),
797 array( 'code' => 'parseerror', 'info' => 'Failed to parse the given text.' ),
798 array(
799 'code' => 'notwikitext',
800 'info' => 'The requested operation is only supported on wikitext content.'
801 ),
802 array( 'code' => 'pagecannotexist', 'info' => "Namespace doesn't allow actual pages" ),
803 ) );
804 }
805
806 public function getExamples() {
807 return array(
808 'api.php?action=parse&page=Project:Sandbox' => 'Parse a page',
809 'api.php?action=parse&text={{Project:Sandbox}}' => 'Parse wikitext',
810 'api.php?action=parse&text={{PAGENAME}}&title=Test'
811 => 'Parse wikitext, specifying the page title',
812 'api.php?action=parse&summary=Some+[[link]]&prop=' => 'Parse a summary',
813 );
814 }
815
816 public function getHelpUrls() {
817 return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#parse';
818 }
819 }