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