Remove score display from search engine
[mediawiki.git] / includes / api / ApiParse.php
blob2e993e3648f022f2ff61ccdabcdddff020e4c506
1 <?php
2 /**
3 * Created on Dec 01, 2007
5 * Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
22 * @file
25 /**
26 * @ingroup API
28 class ApiParse extends ApiBase {
30 /** @var string $section */
31 private $section = null;
33 /** @var Content $content */
34 private $content = null;
36 /** @var Content $pstContent */
37 private $pstContent = null;
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' );
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;
55 $page = $params['page'];
56 $pageid = $params['pageid'];
57 $oldid = $params['oldid'];
59 $model = $params['contentmodel'];
60 $format = $params['contentformat'];
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'
69 $prop = array_flip( $params['prop'] );
71 if ( isset( $params['section'] ) ) {
72 $this->section = $params['section'];
73 } else {
74 $this->section = false;
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;
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'] ) );
92 $redirValues = null;
94 // Return result
95 $result = $this->getResult();
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' );
104 if ( !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
105 $this->dieUsage( "You don't have permission to view deleted revisions", 'permissiondenied' );
108 $titleObj = $rev->getTitle();
109 $wgTitle = $titleObj;
110 $pageObj = WikiPage::factory( $titleObj );
111 $popts = $this->makeParserOptions( $pageObj, $params );
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() );
121 if ( $this->section !== false ) {
122 $this->content = $this->getSectionContent( $this->content, 'r' . $rev->getId() );
125 // Should we save old revision parses to the parser cache?
126 $p_result = $this->content->getParserOutput( $titleObj, $rev->getId(), $popts );
128 } else { // Not $oldid, but $pageid or $page
129 if ( $params['redirects'] ) {
130 $reqParams = array(
131 'action' => 'query',
132 'redirects' => '',
134 if ( !is_null( $pageid ) ) {
135 $reqParams['pageids'] = $pageid;
136 } else { // $page
137 $reqParams['titles'] = $page;
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'];
150 $pageParams = array( 'title' => $to );
151 } elseif ( !is_null( $pageid ) ) {
152 $pageParams = array( 'pageid' => $pageid );
153 } else { // $page
154 $pageParams = array( 'title' => $page );
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' );
162 $wgTitle = $titleObj;
164 if ( isset( $prop['revid'] ) ) {
165 $oldid = $pageObj->getLatest();
168 $popts = $this->makeParserOptions( $pageObj, $params );
170 // Potentially cached
171 $p_result = $this->getParsedContent( $pageObj, $popts, $pageid,
172 isset( $prop['wikitext'] ) );
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 ) );
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();
188 $popts = $this->makeParserOptions( $pageObj, $params );
189 $textProvided = !is_null( $text );
191 if ( !$textProvided ) {
192 if ( $titleProvided && ( $prop || $params['generatexml'] ) ) {
193 $this->setWarning(
194 "'title' used without 'text', and parsed page properties were requested " .
195 "(did you mean to use 'page' instead of 'title'?)"
198 // Prevent warning from ContentHandler::makeContent()
199 $text = '';
202 // If we are parsing text, do not use the content model of the default
203 // API title, but default to wikitext to keep BC.
204 if ( $textProvided && !$titleProvided && is_null( $model ) ) {
205 $model = CONTENT_MODEL_WIKITEXT;
206 $this->setWarning( "No 'title' or 'contentmodel' was given, assuming $model." );
209 try {
210 $this->content = ContentHandler::makeContent( $text, $titleObj, $model, $format );
211 } catch ( MWContentSerializationException $ex ) {
212 $this->dieUsage( $ex->getMessage(), 'parseerror' );
215 if ( $this->section !== false ) {
216 $this->content = $this->getSectionContent( $this->content, $titleObj->getPrefixedText() );
219 if ( $params['pst'] || $params['onlypst'] ) {
220 $this->pstContent = $this->content->preSaveTransform( $titleObj, $this->getUser(), $popts );
222 if ( $params['onlypst'] ) {
223 // Build a result and bail out
224 $result_array = array();
225 $result_array['text'] = array();
226 ApiResult::setContent( $result_array['text'], $this->pstContent->serialize( $format ) );
227 if ( isset( $prop['wikitext'] ) ) {
228 $result_array['wikitext'] = array();
229 ApiResult::setContent( $result_array['wikitext'], $this->content->serialize( $format ) );
231 $result->addValue( null, $this->getModuleName(), $result_array );
233 return;
236 // Not cached (save or load)
237 if ( $params['pst'] ) {
238 $p_result = $this->pstContent->getParserOutput( $titleObj, null, $popts );
239 } else {
240 $p_result = $this->content->getParserOutput( $titleObj, null, $popts );
244 $result_array = array();
246 $result_array['title'] = $titleObj->getPrefixedText();
248 if ( !is_null( $oldid ) ) {
249 $result_array['revid'] = intval( $oldid );
252 if ( $params['redirects'] && !is_null( $redirValues ) ) {
253 $result_array['redirects'] = $redirValues;
256 if ( $params['disabletoc'] ) {
257 $p_result->setTOCEnabled( false );
260 if ( isset( $prop['text'] ) ) {
261 $result_array['text'] = array();
262 ApiResult::setContent( $result_array['text'], $p_result->getText() );
265 if ( !is_null( $params['summary'] ) ) {
266 $result_array['parsedsummary'] = array();
267 ApiResult::setContent(
268 $result_array['parsedsummary'],
269 Linker::formatComment( $params['summary'], $titleObj )
273 if ( isset( $prop['langlinks'] ) ) {
274 $langlinks = $p_result->getLanguageLinks();
276 if ( $params['effectivelanglinks'] ) {
277 // Link flags are ignored for now, but may in the future be
278 // included in the result.
279 $linkFlags = array();
280 wfRunHooks( 'LanguageLinks', array( $titleObj, &$langlinks, &$linkFlags ) );
282 } else {
283 $langlinks = false;
286 if ( isset( $prop['langlinks'] ) ) {
287 $result_array['langlinks'] = $this->formatLangLinks( $langlinks );
289 if ( isset( $prop['categories'] ) ) {
290 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
292 if ( isset( $prop['categorieshtml'] ) ) {
293 $categoriesHtml = $this->categoriesHtml( $p_result->getCategories() );
294 $result_array['categorieshtml'] = array();
295 ApiResult::setContent( $result_array['categorieshtml'], $categoriesHtml );
297 if ( isset( $prop['links'] ) ) {
298 $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
300 if ( isset( $prop['templates'] ) ) {
301 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
303 if ( isset( $prop['images'] ) ) {
304 $result_array['images'] = array_keys( $p_result->getImages() );
306 if ( isset( $prop['externallinks'] ) ) {
307 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
309 if ( isset( $prop['sections'] ) ) {
310 $result_array['sections'] = $p_result->getSections();
313 if ( isset( $prop['displaytitle'] ) ) {
314 $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
315 $p_result->getDisplayTitle() :
316 $titleObj->getPrefixedText();
319 if ( isset( $prop['headitems'] ) || isset( $prop['headhtml'] ) ) {
320 $context = $this->getContext();
321 $context->setTitle( $titleObj );
322 $context->getOutput()->addParserOutputMetadata( $p_result );
324 if ( isset( $prop['headitems'] ) ) {
325 $headItems = $this->formatHeadItems( $p_result->getHeadItems() );
327 $css = $this->formatCss( $context->getOutput()->buildCssLinksArray() );
329 $scripts = array( $context->getOutput()->getHeadScripts() );
331 $result_array['headitems'] = array_merge( $headItems, $css, $scripts );
334 if ( isset( $prop['headhtml'] ) ) {
335 $result_array['headhtml'] = array();
336 ApiResult::setContent(
337 $result_array['headhtml'],
338 $context->getOutput()->headElement( $context->getSkin() )
343 if ( isset( $prop['modules'] ) ) {
344 $result_array['modules'] = array_values( array_unique( $p_result->getModules() ) );
345 $result_array['modulescripts'] = array_values( array_unique( $p_result->getModuleScripts() ) );
346 $result_array['modulestyles'] = array_values( array_unique( $p_result->getModuleStyles() ) );
347 $result_array['modulemessages'] = array_values( array_unique( $p_result->getModuleMessages() ) );
350 if ( isset( $prop['iwlinks'] ) ) {
351 $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
354 if ( isset( $prop['wikitext'] ) ) {
355 $result_array['wikitext'] = array();
356 ApiResult::setContent( $result_array['wikitext'], $this->content->serialize( $format ) );
357 if ( !is_null( $this->pstContent ) ) {
358 $result_array['psttext'] = array();
359 ApiResult::setContent( $result_array['psttext'], $this->pstContent->serialize( $format ) );
362 if ( isset( $prop['properties'] ) ) {
363 $result_array['properties'] = $this->formatProperties( $p_result->getProperties() );
366 if ( isset( $prop['limitreportdata'] ) ) {
367 $result_array['limitreportdata'] =
368 $this->formatLimitReportData( $p_result->getLimitReportData() );
370 if ( isset( $prop['limitreporthtml'] ) ) {
371 $limitreportHtml = EditPage::getPreviewLimitReport( $p_result );
372 $result_array['limitreporthtml'] = array();
373 ApiResult::setContent( $result_array['limitreporthtml'], $limitreportHtml );
376 if ( $params['generatexml'] ) {
377 if ( $this->content->getModel() != CONTENT_MODEL_WIKITEXT ) {
378 $this->dieUsage( "generatexml is only supported for wikitext content", "notwikitext" );
381 $wgParser->startExternalParse( $titleObj, $popts, OT_PREPROCESS );
382 $dom = $wgParser->preprocessToDom( $this->content->getNativeData() );
383 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
384 $xml = $dom->saveXML();
385 } else {
386 $xml = $dom->__toString();
388 $result_array['parsetree'] = array();
389 ApiResult::setContent( $result_array['parsetree'], $xml );
392 $result_mapping = array(
393 'redirects' => 'r',
394 'langlinks' => 'll',
395 'categories' => 'cl',
396 'links' => 'pl',
397 'templates' => 'tl',
398 'images' => 'img',
399 'externallinks' => 'el',
400 'iwlinks' => 'iw',
401 'sections' => 's',
402 'headitems' => 'hi',
403 'modules' => 'm',
404 'modulescripts' => 'm',
405 'modulestyles' => 'm',
406 'modulemessages' => 'm',
407 'properties' => 'pp',
408 'limitreportdata' => 'lr',
410 $this->setIndexedTagNames( $result_array, $result_mapping );
411 $result->addValue( null, $this->getModuleName(), $result_array );
413 if ( !is_null( $oldLang ) ) {
414 $this->getContext()->setLanguage( $oldLang ); // Reset language to $oldLang
419 * Constructs a ParserOptions object
421 * @param WikiPage $pageObj
422 * @param array $params
424 * @return ParserOptions
426 protected function makeParserOptions( WikiPage $pageObj, array $params ) {
427 wfProfileIn( __METHOD__ );
429 $popts = $pageObj->makeParserOptions( $this->getContext() );
430 $popts->enableLimitReport( !$params['disablepp'] );
431 $popts->setIsPreview( $params['preview'] || $params['sectionpreview'] );
432 $popts->setIsSectionPreview( $params['sectionpreview'] );
433 $popts->setEditSection( !$params['disableeditsection'] );
435 wfProfileOut( __METHOD__ );
437 return $popts;
441 * @param WikiPage $page
442 * @param ParserOptions $popts
443 * @param int $pageId
444 * @param bool $getWikitext
445 * @return ParserOutput
447 private function getParsedContent( WikiPage $page, $popts, $pageId = null, $getWikitext = false ) {
448 $this->content = $page->getContent( Revision::RAW ); //XXX: really raw?
450 if ( $this->section !== false && $this->content !== null ) {
451 $this->content = $this->getSectionContent(
452 $this->content,
453 !is_null( $pageId ) ? 'page id ' . $pageId : $page->getTitle()->getPrefixedText()
456 // Not cached (save or load)
457 return $this->content->getParserOutput( $page->getTitle(), null, $popts );
460 // Try the parser cache first
461 // getParserOutput will save to Parser cache if able
462 $pout = $page->getParserOutput( $popts );
463 if ( !$pout ) {
464 $this->dieUsage( "There is no revision ID {$page->getLatest()}", 'missingrev' );
466 if ( $getWikitext ) {
467 $this->content = $page->getContent( Revision::RAW );
470 return $pout;
474 * @param Content $content
475 * @param string $what Identifies the content in error messages, e.g. page title.
477 private function getSectionContent( Content $content, $what ) {
478 // Not cached (save or load)
479 $section = $content->getSection( $this->section );
480 if ( $section === false ) {
481 $this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' );
483 if ( $section === null ) {
484 $this->dieUsage( "Sections are not supported by " . $what, 'nosuchsection' );
485 $section = false;
488 return $section;
491 private function formatLangLinks( $links ) {
492 $result = array();
493 foreach ( $links as $link ) {
494 $entry = array();
495 $bits = explode( ':', $link, 2 );
496 $title = Title::newFromText( $link );
498 $entry['lang'] = $bits[0];
499 if ( $title ) {
500 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
501 // localised language name in user language (maybe set by uselang=)
502 $entry['langname'] = Language::fetchLanguageName(
503 $title->getInterwiki(),
504 $this->getLanguage()->getCode()
507 // native language name
508 $entry['autonym'] = Language::fetchLanguageName( $title->getInterwiki() );
510 ApiResult::setContent( $entry, $bits[1] );
511 $result[] = $entry;
514 return $result;
517 private function formatCategoryLinks( $links ) {
518 $result = array();
520 if ( !$links ) {
521 return $result;
524 // Fetch hiddencat property
525 $lb = new LinkBatch;
526 $lb->setArray( array( NS_CATEGORY => $links ) );
527 $db = $this->getDB();
528 $res = $db->select( array( 'page', 'page_props' ),
529 array( 'page_title', 'pp_propname' ),
530 $lb->constructSet( 'page', $db ),
531 __METHOD__,
532 array(),
533 array( 'page_props' => array(
534 'LEFT JOIN', array( 'pp_propname' => 'hiddencat', 'pp_page = page_id' )
537 $hiddencats = array();
538 foreach ( $res as $row ) {
539 $hiddencats[$row->page_title] = isset( $row->pp_propname );
542 foreach ( $links as $link => $sortkey ) {
543 $entry = array();
544 $entry['sortkey'] = $sortkey;
545 ApiResult::setContent( $entry, $link );
546 if ( !isset( $hiddencats[$link] ) ) {
547 $entry['missing'] = '';
548 } elseif ( $hiddencats[$link] ) {
549 $entry['hidden'] = '';
551 $result[] = $entry;
554 return $result;
557 private function categoriesHtml( $categories ) {
558 $context = $this->getContext();
559 $context->getOutput()->addCategoryLinks( $categories );
561 return $context->getSkin()->getCategories();
564 private function formatLinks( $links ) {
565 $result = array();
566 foreach ( $links as $ns => $nslinks ) {
567 foreach ( $nslinks as $title => $id ) {
568 $entry = array();
569 $entry['ns'] = $ns;
570 ApiResult::setContent( $entry, Title::makeTitle( $ns, $title )->getFullText() );
571 if ( $id != 0 ) {
572 $entry['exists'] = '';
574 $result[] = $entry;
578 return $result;
581 private function formatIWLinks( $iw ) {
582 $result = array();
583 foreach ( $iw as $prefix => $titles ) {
584 foreach ( array_keys( $titles ) as $title ) {
585 $entry = array();
586 $entry['prefix'] = $prefix;
588 $title = Title::newFromText( "{$prefix}:{$title}" );
589 if ( $title ) {
590 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
593 ApiResult::setContent( $entry, $title->getFullText() );
594 $result[] = $entry;
598 return $result;
601 private function formatHeadItems( $headItems ) {
602 $result = array();
603 foreach ( $headItems as $tag => $content ) {
604 $entry = array();
605 $entry['tag'] = $tag;
606 ApiResult::setContent( $entry, $content );
607 $result[] = $entry;
610 return $result;
613 private function formatProperties( $properties ) {
614 $result = array();
615 foreach ( $properties as $name => $value ) {
616 $entry = array();
617 $entry['name'] = $name;
618 ApiResult::setContent( $entry, $value );
619 $result[] = $entry;
622 return $result;
625 private function formatCss( $css ) {
626 $result = array();
627 foreach ( $css as $file => $link ) {
628 $entry = array();
629 $entry['file'] = $file;
630 ApiResult::setContent( $entry, $link );
631 $result[] = $entry;
634 return $result;
637 private function formatLimitReportData( $limitReportData ) {
638 $result = array();
639 $apiResult = $this->getResult();
641 foreach ( $limitReportData as $name => $value ) {
642 $entry = array();
643 $entry['name'] = $name;
644 if ( !is_array( $value ) ) {
645 $value = array( $value );
647 $apiResult->setIndexedTagName( $value, 'param' );
648 $apiResult->setIndexedTagName_recursive( $value, 'param' );
649 $entry = array_merge( $entry, $value );
650 $result[] = $entry;
653 return $result;
656 private function setIndexedTagNames( &$array, $mapping ) {
657 foreach ( $mapping as $key => $name ) {
658 if ( isset( $array[$key] ) ) {
659 $this->getResult()->setIndexedTagName( $array[$key], $name );
664 public function getAllowedParams() {
665 return array(
666 'title' => null,
667 'text' => null,
668 'summary' => null,
669 'page' => null,
670 'pageid' => array(
671 ApiBase::PARAM_TYPE => 'integer',
673 'redirects' => false,
674 'oldid' => array(
675 ApiBase::PARAM_TYPE => 'integer',
677 'prop' => array(
678 ApiBase::PARAM_DFLT => 'text|langlinks|categories|links|templates|' .
679 'images|externallinks|sections|revid|displaytitle|iwlinks|properties',
680 ApiBase::PARAM_ISMULTI => true,
681 ApiBase::PARAM_TYPE => array(
682 'text',
683 'langlinks',
684 'categories',
685 'categorieshtml',
686 'links',
687 'templates',
688 'images',
689 'externallinks',
690 'sections',
691 'revid',
692 'displaytitle',
693 'headitems',
694 'headhtml',
695 'modules',
696 'iwlinks',
697 'wikitext',
698 'properties',
699 'limitreportdata',
700 'limitreporthtml',
703 'pst' => false,
704 'onlypst' => false,
705 'effectivelanglinks' => false,
706 'uselang' => null,
707 'section' => null,
708 'disablepp' => false,
709 'disableeditsection' => false,
710 'generatexml' => false,
711 'preview' => false,
712 'sectionpreview' => false,
713 'disabletoc' => false,
714 'contentformat' => array(
715 ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
717 'contentmodel' => array(
718 ApiBase::PARAM_TYPE => ContentHandler::getContentModels(),
723 public function getParamDescription() {
724 $p = $this->getModulePrefix();
725 $wikitext = CONTENT_MODEL_WIKITEXT;
727 return array(
728 'text' => "Text to parse. Use {$p}title or {$p}contentmodel to control the content model",
729 'summary' => 'Summary to parse',
730 'redirects' => "If the {$p}page or the {$p}pageid parameter is set to a redirect, resolve it",
731 'title' => "Title of page the text belongs to. " .
732 "If omitted, {$p}contentmodel must be specified, and \"API\" will be used as the title",
733 'page' => "Parse the content of this page. Cannot be used together with {$p}text and {$p}title",
734 'pageid' => "Parse the content of this page. Overrides {$p}page",
735 'oldid' => "Parse the content of this revision. Overrides {$p}page and {$p}pageid",
736 'prop' => array(
737 'Which pieces of information to get',
738 ' text - Gives the parsed text of the wikitext',
739 ' langlinks - Gives the language links in the parsed wikitext',
740 ' categories - Gives the categories in the parsed wikitext',
741 ' categorieshtml - Gives the HTML version of the categories',
742 ' links - Gives the internal links in the parsed wikitext',
743 ' templates - Gives the templates in the parsed wikitext',
744 ' images - Gives the images in the parsed wikitext',
745 ' externallinks - Gives the external links in the parsed wikitext',
746 ' sections - Gives the sections in the parsed wikitext',
747 ' revid - Adds the revision ID of the parsed page',
748 ' displaytitle - Adds the title of the parsed wikitext',
749 ' headitems - Gives items to put in the <head> of the page',
750 ' headhtml - Gives parsed <head> of the page',
751 ' modules - Gives the ResourceLoader modules used on the page',
752 ' iwlinks - Gives interwiki links in the parsed wikitext',
753 ' wikitext - Gives the original wikitext that was parsed',
754 ' properties - Gives various properties defined in the parsed wikitext',
755 ' limitreportdata - Gives the limit report in a structured way.',
756 " Gives no data, when {$p}disablepp is set.",
757 ' limitreporthtml - Gives the HTML version of the limit report.',
758 " Gives no data, when {$p}disablepp is set.",
760 'effectivelanglinks' => array(
761 'Includes language links supplied by extensions',
762 '(for use with prop=langlinks)',
764 'pst' => array(
765 'Do a pre-save transform on the input before parsing it',
766 "Only valid when used with {$p}text",
768 'onlypst' => array(
769 'Do a pre-save transform (PST) on the input, but don\'t parse it',
770 'Returns the same wikitext, after a PST has been applied.',
771 "Only valid when used with {$p}text",
773 'uselang' => 'Which language to parse the request in',
774 'section' => 'Only retrieve the content of this section number',
775 'disablepp' => 'Disable the PP Report from the parser output',
776 'disableeditsection' => 'Disable edit section links from the parser output',
777 'generatexml' => "Generate XML parse tree (requires contentmodel=$wikitext)",
778 'preview' => 'Parse in preview mode',
779 'sectionpreview' => 'Parse in section preview mode (enables preview mode too)',
780 'disabletoc' => 'Disable table of contents in output',
781 'contentformat' => array(
782 'Content serialization format used for the input text',
783 "Only valid when used with {$p}text",
785 'contentmodel' => array(
786 "Content model of the input text. If omitted, ${p}title must be specified, " .
787 "and default will be the model of the specified ${p}title",
788 "Only valid when used with {$p}text",
793 public function getDescription() {
794 $p = $this->getModulePrefix();
796 return array(
797 'Parses content and returns parser output.',
798 'See the various prop-Modules of action=query to get information from the current' .
799 'version of a page.',
800 'There are several ways to specify the text to parse:',
801 "1) Specify a page or revision, using {$p}page, {$p}pageid, or {$p}oldid.",
802 "2) Specify content explicitly, using {$p}text, {$p}title, and {$p}contentmodel.",
803 "3) Specify only a summary to parse. {$p}prop should be given an empty value.",
807 public function getPossibleErrors() {
808 return array_merge( parent::getPossibleErrors(), array(
809 array(
810 'code' => 'params',
811 'info' => 'The page parameter cannot be used together with the text and title parameters'
813 array( 'code' => 'missingrev', 'info' => 'There is no revision ID oldid' ),
814 array(
815 'code' => 'permissiondenied',
816 'info' => 'You don\'t have permission to view deleted revisions'
818 array( 'code' => 'missingtitle', 'info' => 'The page you specified doesn\'t exist' ),
819 array( 'code' => 'nosuchsection', 'info' => 'There is no section sectionnumber in page' ),
820 array( 'nosuchpageid' ),
821 array( 'invalidtitle', 'title' ),
822 array( 'code' => 'parseerror', 'info' => 'Failed to parse the given text.' ),
823 array(
824 'code' => 'notwikitext',
825 'info' => 'The requested operation is only supported on wikitext content.'
827 ) );
830 public function getExamples() {
831 return array(
832 'api.php?action=parse&page=Project:Sandbox' => 'Parse a page',
833 'api.php?action=parse&text={{Project:Sandbox}}&contentmodel=wikitext' => 'Parse wikitext',
834 'api.php?action=parse&text={{PAGENAME}}&title=Test'
835 => 'Parse wikitext, specifying the page title',
836 'api.php?action=parse&summary=Some+[[link]]&prop=' => 'Parse a summary',
840 public function getHelpUrls() {
841 return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#parse';