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