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