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