No more undefined usage of rev{Start,End}Id warnings
[mediawiki.git] / includes / api / ApiParse.php
blob141f779372c3177b025febcbdb68576046439f0d
1 <?php
2 /**
3 * Created on Dec 01, 2007
5 * Copyright © 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
22 * @file
25 /**
26 * @ingroup API
28 class ApiParse extends ApiBase {
29 private $section, $text, $pstText = null;
31 public function __construct( $main, $action ) {
32 parent::__construct( $main, $action );
35 public function execute() {
36 // The data is hot but user-dependent, like page views, so we set vary cookies
37 $this->getMain()->setCacheMode( 'anon-public-user-private' );
39 // Get parameters
40 $params = $this->extractRequestParams();
41 $text = $params['text'];
42 $title = $params['title'];
43 $page = $params['page'];
44 $pageid = $params['pageid'];
45 $oldid = $params['oldid'];
47 if ( !is_null( $page ) && ( !is_null( $text ) || $title != 'API' ) ) {
48 $this->dieUsage( 'The page parameter cannot be used together with the text and title parameters', 'params' );
51 $prop = array_flip( $params['prop'] );
53 if ( isset( $params['section'] ) ) {
54 $this->section = $params['section'];
55 } else {
56 $this->section = false;
59 // The parser needs $wgTitle to be set, apparently the
60 // $title parameter in Parser::parse isn't enough *sigh*
61 // TODO: Does this still need $wgTitle?
62 global $wgParser, $wgTitle, $wgLang;
64 // Currently unnecessary, code to act as a safeguard against any change in current behaviour of uselang breaks
65 $oldLang = null;
66 if ( isset( $params['uselang'] ) && $params['uselang'] != $wgLang->getCode() ) {
67 $oldLang = $wgLang; // Backup wgLang
68 $wgLang = Language::factory( $params['uselang'] );
71 $popts = ParserOptions::newFromContext( $this->getContext() );
72 $popts->setTidy( true );
73 $popts->enableLimitReport( !$params['disablepp'] );
75 $redirValues = null;
77 // Return result
78 $result = $this->getResult();
80 if ( !is_null( $oldid ) || !is_null( $pageid ) || !is_null( $page ) ) {
81 if ( !is_null( $oldid ) ) {
82 // Don't use the parser cache
83 $rev = Revision::newFromID( $oldid );
84 if ( !$rev ) {
85 $this->dieUsage( "There is no revision ID $oldid", 'missingrev' );
87 if ( !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
88 $this->dieUsage( "You don't have permission to view deleted revisions", 'permissiondenied' );
91 $titleObj = $rev->getTitle();
93 $wgTitle = $titleObj;
95 // If for some reason the "oldid" is actually the current revision, it may be cached
96 if ( $titleObj->getLatestRevID() === intval( $oldid ) ) {
97 // May get from/save to parser cache
98 $p_result = $this->getParsedSectionOrText( $titleObj, $popts, $pageid,
99 isset( $prop['wikitext'] ) ) ;
100 } else { // This is an old revision, so get the text differently
101 $this->text = $rev->getText( Revision::FOR_THIS_USER, $this->getUser() );
103 if ( $this->section !== false ) {
104 $this->text = $this->getSectionText( $this->text, 'r' . $rev->getId() );
107 // Should we save old revision parses to the parser cache?
108 $p_result = $wgParser->parse( $this->text, $titleObj, $popts );
110 } else { // Not $oldid, but $pageid or $page
111 if ( $params['redirects'] ) {
112 $reqParams = array(
113 'action' => 'query',
114 'redirects' => '',
116 if ( !is_null ( $pageid ) ) {
117 $reqParams['pageids'] = $pageid;
118 } else { // $page
119 $reqParams['titles'] = $page;
121 $req = new FauxRequest( $reqParams );
122 $main = new ApiMain( $req );
123 $main->execute();
124 $data = $main->getResultData();
125 $redirValues = isset( $data['query']['redirects'] )
126 ? $data['query']['redirects']
127 : array();
128 $to = $page;
129 foreach ( (array)$redirValues as $r ) {
130 $to = $r['to'];
132 $titleObj = Title::newFromText( $to );
133 } else {
134 if ( !is_null ( $pageid ) ) {
135 $reqParams['pageids'] = $pageid;
136 $titleObj = Title::newFromID( $pageid );
137 } else { // $page
138 $to = $page;
139 $titleObj = Title::newFromText( $to );
142 if ( !is_null ( $pageid ) ) {
143 if ( !$titleObj ) {
144 // Still throw nosuchpageid error if pageid was provided
145 $this->dieUsageMsg( array( 'nosuchpageid', $pageid ) );
147 } elseif ( !$titleObj || !$titleObj->exists() ) {
148 $this->dieUsage( "The page you specified doesn't exist", 'missingtitle' );
150 $wgTitle = $titleObj;
152 if ( isset( $prop['revid'] ) ) {
153 $oldid = $titleObj->getLatestRevID();
156 // Potentially cached
157 $p_result = $this->getParsedSectionOrText( $titleObj, $popts, $pageid,
158 isset( $prop['wikitext'] ) ) ;
160 } else { // Not $oldid, $pageid, $page. Hence based on $text
162 if ( is_null( $text ) ) {
163 $this->dieUsage( 'The text parameter should be passed with the title parameter. Should you be using the "page" parameter instead?', 'params' );
165 $this->text = $text;
166 $titleObj = Title::newFromText( $title );
167 if ( !$titleObj ) {
168 $this->dieUsageMsg( array( 'invalidtitle', $title ) );
170 $wgTitle = $titleObj;
172 if ( $this->section !== false ) {
173 $this->text = $this->getSectionText( $this->text, $titleObj->getText() );
176 if ( $params['pst'] || $params['onlypst'] ) {
177 $this->pstText = $wgParser->preSaveTransform( $this->text, $titleObj, $this->getUser(), $popts );
179 if ( $params['onlypst'] ) {
180 // Build a result and bail out
181 $result_array = array();
182 $result_array['text'] = array();
183 $result->setContent( $result_array['text'], $this->pstText );
184 if ( isset( $prop['wikitext'] ) ) {
185 $result_array['wikitext'] = array();
186 $result->setContent( $result_array['wikitext'], $this->text );
188 $result->addValue( null, $this->getModuleName(), $result_array );
189 return;
191 // Not cached (save or load)
192 $p_result = $wgParser->parse( $params['pst'] ? $this->pstText : $this->text, $titleObj, $popts );
195 $result_array = array();
197 $result_array['title'] = $titleObj->getPrefixedText();
199 if ( !is_null( $oldid ) ) {
200 $result_array['revid'] = intval( $oldid );
203 if ( $params['redirects'] && !is_null( $redirValues ) ) {
204 $result_array['redirects'] = $redirValues;
207 if ( isset( $prop['text'] ) ) {
208 $result_array['text'] = array();
209 $result->setContent( $result_array['text'], $p_result->getText() );
212 if ( !is_null( $params['summary'] ) ) {
213 $result_array['parsedsummary'] = array();
214 $result->setContent( $result_array['parsedsummary'], Linker::formatComment( $params['summary'], $titleObj ) );
217 if ( isset( $prop['langlinks'] ) ) {
218 $result_array['langlinks'] = $this->formatLangLinks( $p_result->getLanguageLinks() );
220 if ( isset( $prop['languageshtml'] ) ) {
221 $languagesHtml = $this->languagesHtml( $p_result->getLanguageLinks() );
222 $result_array['languageshtml'] = array();
223 $result->setContent( $result_array['languageshtml'], $languagesHtml );
225 if ( isset( $prop['categories'] ) ) {
226 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
228 if ( isset( $prop['categorieshtml'] ) ) {
229 $categoriesHtml = $this->categoriesHtml( $p_result->getCategories() );
230 $result_array['categorieshtml'] = array();
231 $result->setContent( $result_array['categorieshtml'], $categoriesHtml );
233 if ( isset( $prop['links'] ) ) {
234 $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
236 if ( isset( $prop['templates'] ) ) {
237 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
239 if ( isset( $prop['images'] ) ) {
240 $result_array['images'] = array_keys( $p_result->getImages() );
242 if ( isset( $prop['externallinks'] ) ) {
243 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
245 if ( isset( $prop['sections'] ) ) {
246 $result_array['sections'] = $p_result->getSections();
249 if ( isset( $prop['displaytitle'] ) ) {
250 $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
251 $p_result->getDisplayTitle() :
252 $titleObj->getPrefixedText();
255 if ( isset( $prop['headitems'] ) || isset( $prop['headhtml'] ) ) {
256 $context = $this->getContext();
257 $context->setTitle( $titleObj );
258 $context->getOutput()->addParserOutputNoText( $p_result );
260 if ( isset( $prop['headitems'] ) ) {
261 $headItems = $this->formatHeadItems( $p_result->getHeadItems() );
263 $css = $this->formatCss( $context->getOutput()->buildCssLinksArray() );
265 $scripts = array( $context->getOutput()->getHeadScripts() );
267 $result_array['headitems'] = array_merge( $headItems, $css, $scripts );
270 if ( isset( $prop['headhtml'] ) ) {
271 $result_array['headhtml'] = array();
272 $result->setContent( $result_array['headhtml'], $context->getOutput()->headElement( $context->getSkin() ) );
276 if ( isset( $prop['iwlinks'] ) ) {
277 $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
280 if ( isset( $prop['wikitext'] ) ) {
281 $result_array['wikitext'] = array();
282 $result->setContent( $result_array['wikitext'], $this->text );
283 if ( !is_null( $this->pstText ) ) {
284 $result_array['psttext'] = array();
285 $result->setContent( $result_array['psttext'], $this->pstText );
289 $result_mapping = array(
290 'redirects' => 'r',
291 'langlinks' => 'll',
292 'categories' => 'cl',
293 'links' => 'pl',
294 'templates' => 'tl',
295 'images' => 'img',
296 'externallinks' => 'el',
297 'iwlinks' => 'iw',
298 'sections' => 's',
299 'headitems' => 'hi',
301 $this->setIndexedTagNames( $result_array, $result_mapping );
302 $result->addValue( null, $this->getModuleName(), $result_array );
304 if ( !is_null( $oldLang ) ) {
305 $wgLang = $oldLang; // Reset $wgLang to $oldLang
310 * @param $titleObj Title
311 * @param $popts ParserOptions
312 * @param $pageId Int
313 * @param $getWikitext Bool
314 * @return ParserOutput
316 private function getParsedSectionOrText( $titleObj, $popts, $pageId = null, $getWikitext = false ) {
317 global $wgParser;
319 $page = WikiPage::factory( $titleObj );
321 if ( $this->section !== false ) {
322 $this->text = $this->getSectionText( $page->getRawText(), !is_null( $pageId )
323 ? 'page id ' . $pageId : $titleObj->getText() );
325 // Not cached (save or load)
326 return $wgParser->parse( $this->text, $titleObj, $popts );
327 } else {
328 // Try the parser cache first
329 // getParserOutput will save to Parser cache if able
330 $pout = $page->getParserOutput( $popts );
331 if ( $getWikitext ) {
332 $this->text = $page->getRawText();
334 return $pout;
338 private function getSectionText( $text, $what ) {
339 global $wgParser;
340 // Not cached (save or load)
341 $text = $wgParser->getSection( $text, $this->section, false );
342 if ( $text === false ) {
343 $this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' );
345 return $text;
348 private function formatLangLinks( $links ) {
349 $result = array();
350 foreach ( $links as $link ) {
351 $entry = array();
352 $bits = explode( ':', $link, 2 );
353 $title = Title::newFromText( $link );
355 $entry['lang'] = $bits[0];
356 if ( $title ) {
357 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
359 $this->getResult()->setContent( $entry, $bits[1] );
360 $result[] = $entry;
362 return $result;
365 private function formatCategoryLinks( $links ) {
366 $result = array();
367 foreach ( $links as $link => $sortkey ) {
368 $entry = array();
369 $entry['sortkey'] = $sortkey;
370 $this->getResult()->setContent( $entry, $link );
371 $result[] = $entry;
373 return $result;
376 private function categoriesHtml( $categories ) {
377 $context = $this->getContext();
378 $context->getOutput()->addCategoryLinks( $categories );
379 return $context->getSkin()->getCategories();
383 * @deprecated since 1.18 No modern skin generates language links this way, please use language links
384 * data to generate your own HTML.
385 * @param $languages array
386 * @return string
388 private function languagesHtml( $languages ) {
389 wfDeprecated( __METHOD__, '1.18' );
391 global $wgContLang, $wgHideInterlanguageLinks;
393 if ( $wgHideInterlanguageLinks || count( $languages ) == 0 ) {
394 return '';
397 $s = htmlspecialchars( wfMsg( 'otherlanguages' ) . wfMsg( 'colon-separator' ) );
399 $langs = array();
400 foreach ( $languages as $l ) {
401 $nt = Title::newFromText( $l );
402 $text = Language::fetchLanguageName( $nt->getInterwiki() );
404 $langs[] = Html::element( 'a',
405 array( 'href' => $nt->getFullURL(), 'title' => $nt->getText(), 'class' => "external" ),
406 $text == '' ? $l : $text );
409 $s .= implode( htmlspecialchars( wfMsgExt( 'pipe-separator', 'escapenoentities' ) ), $langs );
411 if ( $wgContLang->isRTL() ) {
412 $s = Html::rawElement( 'span', array( 'dir' => "LTR" ), $s );
415 return $s;
418 private function formatLinks( $links ) {
419 $result = array();
420 foreach ( $links as $ns => $nslinks ) {
421 foreach ( $nslinks as $title => $id ) {
422 $entry = array();
423 $entry['ns'] = $ns;
424 $this->getResult()->setContent( $entry, Title::makeTitle( $ns, $title )->getFullText() );
425 if ( $id != 0 ) {
426 $entry['exists'] = '';
428 $result[] = $entry;
431 return $result;
434 private function formatIWLinks( $iw ) {
435 $result = array();
436 foreach ( $iw as $prefix => $titles ) {
437 foreach ( array_keys( $titles ) as $title ) {
438 $entry = array();
439 $entry['prefix'] = $prefix;
441 $title = Title::newFromText( "{$prefix}:{$title}" );
442 if ( $title ) {
443 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
446 $this->getResult()->setContent( $entry, $title->getFullText() );
447 $result[] = $entry;
450 return $result;
453 private function formatHeadItems( $headItems ) {
454 $result = array();
455 foreach ( $headItems as $tag => $content ) {
456 $entry = array();
457 $entry['tag'] = $tag;
458 $this->getResult()->setContent( $entry, $content );
459 $result[] = $entry;
461 return $result;
464 private function formatCss( $css ) {
465 $result = array();
466 foreach ( $css as $file => $link ) {
467 $entry = array();
468 $entry['file'] = $file;
469 $this->getResult()->setContent( $entry, $link );
470 $result[] = $entry;
472 return $result;
475 private function setIndexedTagNames( &$array, $mapping ) {
476 foreach ( $mapping as $key => $name ) {
477 if ( isset( $array[$key] ) ) {
478 $this->getResult()->setIndexedTagName( $array[$key], $name );
483 public function getAllowedParams() {
484 return array(
485 'title' => array(
486 ApiBase::PARAM_DFLT => 'API',
488 'text' => null,
489 'summary' => null,
490 'page' => null,
491 'pageid' => array(
492 ApiBase::PARAM_TYPE => 'integer',
494 'redirects' => false,
495 'oldid' => array(
496 ApiBase::PARAM_TYPE => 'integer',
498 'prop' => array(
499 ApiBase::PARAM_DFLT => 'text|langlinks|categories|links|templates|images|externallinks|sections|revid|displaytitle',
500 ApiBase::PARAM_ISMULTI => true,
501 ApiBase::PARAM_TYPE => array(
502 'text',
503 'langlinks',
504 'languageshtml',
505 'categories',
506 'categorieshtml',
507 'links',
508 'templates',
509 'images',
510 'externallinks',
511 'sections',
512 'revid',
513 'displaytitle',
514 'headitems',
515 'headhtml',
516 'iwlinks',
517 'wikitext',
520 'pst' => false,
521 'onlypst' => false,
522 'uselang' => null,
523 'section' => null,
524 'disablepp' => false,
528 public function getParamDescription() {
529 $p = $this->getModulePrefix();
530 return array(
531 'text' => 'Wikitext to parse',
532 'summary' => 'Summary to parse',
533 'redirects' => "If the {$p}page or the {$p}pageid parameter is set to a redirect, resolve it",
534 'title' => 'Title of page the text belongs to',
535 'page' => "Parse the content of this page. Cannot be used together with {$p}text and {$p}title",
536 'pageid' => "Parse the content of this page. Overrides {$p}page",
537 'oldid' => "Parse the content of this revision. Overrides {$p}page and {$p}pageid",
538 'prop' => array(
539 'Which pieces of information to get',
540 ' text - Gives the parsed text of the wikitext',
541 ' langlinks - Gives the language links in the parsed wikitext',
542 ' categories - Gives the categories in the parsed wikitext',
543 ' categorieshtml - Gives the HTML version of the categories',
544 ' languageshtml - Gives the HTML version of the language links',
545 ' links - Gives the internal links in the parsed wikitext',
546 ' templates - Gives the templates in the parsed wikitext',
547 ' images - Gives the images in the parsed wikitext',
548 ' externallinks - Gives the external links in the parsed wikitext',
549 ' sections - Gives the sections in the parsed wikitext',
550 ' revid - Adds the revision ID of the parsed page',
551 ' displaytitle - Adds the title of the parsed wikitext',
552 ' headitems - Gives items to put in the <head> of the page',
553 ' headhtml - Gives parsed <head> of the page',
554 ' iwlinks - Gives interwiki links in the parsed wikitext',
555 ' wikitext - Gives the original wikitext that was parsed',
557 'pst' => array(
558 'Do a pre-save transform on the input before parsing it',
559 'Ignored if page, pageid or oldid is used'
561 'onlypst' => array(
562 'Do a pre-save transform (PST) on the input, but don\'t parse it',
563 'Returns the same wikitext, after a PST has been applied. Ignored if page, pageid or oldid is used'
565 'uselang' => 'Which language to parse the request in',
566 'section' => 'Only retrieve the content of this section number',
567 'disablepp' => 'Disable the PP Report from the parser output',
571 public function getDescription() {
572 return 'Parses wikitext and returns parser output';
575 public function getPossibleErrors() {
576 return array_merge( parent::getPossibleErrors(), array(
577 array( 'code' => 'params', 'info' => 'The page parameter cannot be used together with the text and title parameters' ),
578 array( 'code' => 'params', 'info' => 'The text parameter should be passed with the title parameter. Should you be using the "page" parameter instead?' ),
579 array( 'code' => 'missingrev', 'info' => 'There is no revision ID oldid' ),
580 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revisions' ),
581 array( 'code' => 'missingtitle', 'info' => 'The page you specified doesn\'t exist' ),
582 array( 'code' => 'nosuchsection', 'info' => 'There is no section sectionnumber in page' ),
583 array( 'nosuchpageid' ),
584 array( 'invalidtitle', 'title' ),
585 ) );
588 public function getExamples() {
589 return array(
590 'api.php?action=parse&text={{Project:Sandbox}}'
594 public function getHelpUrls() {
595 return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#parse';
598 public function getVersion() {
599 return __CLASS__ . ': $Id$';