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