Fixed spacing in actions/cache/filebackend/filerepo/job folder
[mediawiki.git] / includes / api / ApiParse.php
blob09b7a8828b61a98e24178f3bb1fbc564430b39ce
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 {
30 /** @var String $section */
31 private $section = null;
33 /** @var Content $content */
34 private $content = null;
36 /** @var Content $pstContent */
37 private $pstContent = null;
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' );
43 // Get parameters
44 $params = $this->extractRequestParams();
45 $text = $params['text'];
46 $title = $params['title'];
47 $page = $params['page'];
48 $pageid = $params['pageid'];
49 $oldid = $params['oldid'];
51 $model = $params['contentmodel'];
52 $format = $params['contentformat'];
54 if ( !is_null( $page ) && ( !is_null( $text ) || $title != 'API' ) ) {
55 $this->dieUsage( 'The page parameter cannot be used together with the text and title parameters', 'params' );
58 $prop = array_flip( $params['prop'] );
60 if ( isset( $params['section'] ) ) {
61 $this->section = $params['section'];
62 } else {
63 $this->section = false;
66 // The parser needs $wgTitle to be set, apparently the
67 // $title parameter in Parser::parse isn't enough *sigh*
68 // TODO: Does this still need $wgTitle?
69 global $wgParser, $wgTitle;
71 // Currently unnecessary, code to act as a safeguard against any change in current behavior of uselang
72 $oldLang = null;
73 if ( isset( $params['uselang'] ) && $params['uselang'] != $this->getContext()->getLanguage()->getCode() ) {
74 $oldLang = $this->getContext()->getLanguage(); // Backup language
75 $this->getContext()->setLanguage( Language::factory( $params['uselang'] ) );
78 $redirValues = null;
80 // Return result
81 $result = $this->getResult();
83 if ( !is_null( $oldid ) || !is_null( $pageid ) || !is_null( $page ) ) {
84 if ( !is_null( $oldid ) ) {
85 // Don't use the parser cache
86 $rev = Revision::newFromID( $oldid );
87 if ( !$rev ) {
88 $this->dieUsage( "There is no revision ID $oldid", 'missingrev' );
90 if ( !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
91 $this->dieUsage( "You don't have permission to view deleted revisions", 'permissiondenied' );
94 $titleObj = $rev->getTitle();
95 $wgTitle = $titleObj;
96 $pageObj = WikiPage::factory( $titleObj );
97 $popts = $pageObj->makeParserOptions( $this->getContext() );
98 $popts->enableLimitReport( !$params['disablepp'] );
100 // If for some reason the "oldid" is actually the current revision, it may be cached
101 if ( $rev->isCurrent() ) {
102 // May get from/save to parser cache
103 $p_result = $this->getParsedContent( $pageObj, $popts,
104 $pageid, isset( $prop['wikitext'] ) );
105 } else { // This is an old revision, so get the text differently
106 $this->content = $rev->getContent( Revision::FOR_THIS_USER, $this->getUser() );
108 if ( $this->section !== false ) {
109 $this->content = $this->getSectionContent( $this->content, 'r' . $rev->getId() );
112 // Should we save old revision parses to the parser cache?
113 $p_result = $this->content->getParserOutput( $titleObj, $rev->getId(), $popts );
115 } else { // Not $oldid, but $pageid or $page
116 if ( $params['redirects'] ) {
117 $reqParams = array(
118 'action' => 'query',
119 'redirects' => '',
121 if ( !is_null ( $pageid ) ) {
122 $reqParams['pageids'] = $pageid;
123 } else { // $page
124 $reqParams['titles'] = $page;
126 $req = new FauxRequest( $reqParams );
127 $main = new ApiMain( $req );
128 $main->execute();
129 $data = $main->getResultData();
130 $redirValues = isset( $data['query']['redirects'] )
131 ? $data['query']['redirects']
132 : array();
133 $to = $page;
134 foreach ( (array)$redirValues as $r ) {
135 $to = $r['to'];
137 $pageParams = array( 'title' => $to );
138 } elseif ( !is_null( $pageid ) ) {
139 $pageParams = array( 'pageid' => $pageid );
140 } else { // $page
141 $pageParams = array( 'title' => $page );
144 $pageObj = $this->getTitleOrPageId( $pageParams, 'fromdb' );
145 $titleObj = $pageObj->getTitle();
146 if ( !$titleObj || !$titleObj->exists() ) {
147 $this->dieUsage( "The page you specified doesn't exist", 'missingtitle' );
149 $wgTitle = $titleObj;
151 if ( isset( $prop['revid'] ) ) {
152 $oldid = $pageObj->getLatest();
155 $popts = $pageObj->makeParserOptions( $this->getContext() );
156 $popts->enableLimitReport( !$params['disablepp'] );
158 // Potentially cached
159 $p_result = $this->getParsedContent( $pageObj, $popts, $pageid,
160 isset( $prop['wikitext'] ) );
162 } else { // Not $oldid, $pageid, $page. Hence based on $text
163 $titleObj = Title::newFromText( $title );
164 if ( !$titleObj || $titleObj->isExternal() ) {
165 $this->dieUsageMsg( array( 'invalidtitle', $title ) );
167 if ( !$titleObj->canExist() ) {
168 $this->dieUsage( "Namespace doesn't allow actual pages", 'pagecannotexist' );
170 $wgTitle = $titleObj;
171 $pageObj = WikiPage::factory( $titleObj );
173 $popts = $pageObj->makeParserOptions( $this->getContext() );
174 $popts->enableLimitReport( !$params['disablepp'] );
176 if ( is_null( $text ) ) {
177 $this->dieUsage( 'The text parameter should be passed with the title parameter. Should you be using the "page" parameter instead?', 'params' );
180 try {
181 $this->content = ContentHandler::makeContent( $text, $titleObj, $model, $format );
182 } catch ( MWContentSerializationException $ex ) {
183 $this->dieUsage( $ex->getMessage(), 'parseerror' );
186 if ( $this->section !== false ) {
187 $this->content = $this->getSectionContent( $this->content, $titleObj->getText() );
190 if ( $params['pst'] || $params['onlypst'] ) {
191 $this->pstContent = $this->content->preSaveTransform( $titleObj, $this->getUser(), $popts );
193 if ( $params['onlypst'] ) {
194 // Build a result and bail out
195 $result_array = array();
196 $result_array['text'] = array();
197 $result->setContent( $result_array['text'], $this->pstContent->serialize( $format ) );
198 if ( isset( $prop['wikitext'] ) ) {
199 $result_array['wikitext'] = array();
200 $result->setContent( $result_array['wikitext'], $this->content->serialize( $format ) );
202 $result->addValue( null, $this->getModuleName(), $result_array );
203 return;
206 // Not cached (save or load)
207 if ( $params['pst'] ) {
208 $p_result = $this->pstContent->getParserOutput( $titleObj, null, $popts );
209 } else {
210 $p_result = $this->content->getParserOutput( $titleObj, null, $popts );
214 $result_array = array();
216 $result_array['title'] = $titleObj->getPrefixedText();
218 if ( !is_null( $oldid ) ) {
219 $result_array['revid'] = intval( $oldid );
222 if ( $params['redirects'] && !is_null( $redirValues ) ) {
223 $result_array['redirects'] = $redirValues;
226 if ( isset( $prop['text'] ) ) {
227 $result_array['text'] = array();
228 $result->setContent( $result_array['text'], $p_result->getText() );
231 if ( !is_null( $params['summary'] ) ) {
232 $result_array['parsedsummary'] = array();
233 $result->setContent( $result_array['parsedsummary'], Linker::formatComment( $params['summary'], $titleObj ) );
236 if ( isset( $prop['langlinks'] ) ) {
237 $result_array['langlinks'] = $this->formatLangLinks( $p_result->getLanguageLinks() );
239 if ( isset( $prop['languageshtml'] ) ) {
240 $languagesHtml = $this->languagesHtml( $p_result->getLanguageLinks() );
241 $result_array['languageshtml'] = array();
242 $result->setContent( $result_array['languageshtml'], $languagesHtml );
244 if ( isset( $prop['categories'] ) ) {
245 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
247 if ( isset( $prop['categorieshtml'] ) ) {
248 $categoriesHtml = $this->categoriesHtml( $p_result->getCategories() );
249 $result_array['categorieshtml'] = array();
250 $result->setContent( $result_array['categorieshtml'], $categoriesHtml );
252 if ( isset( $prop['links'] ) ) {
253 $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
255 if ( isset( $prop['templates'] ) ) {
256 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
258 if ( isset( $prop['images'] ) ) {
259 $result_array['images'] = array_keys( $p_result->getImages() );
261 if ( isset( $prop['externallinks'] ) ) {
262 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
264 if ( isset( $prop['sections'] ) ) {
265 $result_array['sections'] = $p_result->getSections();
268 if ( isset( $prop['displaytitle'] ) ) {
269 $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
270 $p_result->getDisplayTitle() :
271 $titleObj->getPrefixedText();
274 if ( isset( $prop['headitems'] ) || isset( $prop['headhtml'] ) ) {
275 $context = $this->getContext();
276 $context->setTitle( $titleObj );
277 $context->getOutput()->addParserOutputNoText( $p_result );
279 if ( isset( $prop['headitems'] ) ) {
280 $headItems = $this->formatHeadItems( $p_result->getHeadItems() );
282 $css = $this->formatCss( $context->getOutput()->buildCssLinksArray() );
284 $scripts = array( $context->getOutput()->getHeadScripts() );
286 $result_array['headitems'] = array_merge( $headItems, $css, $scripts );
289 if ( isset( $prop['headhtml'] ) ) {
290 $result_array['headhtml'] = array();
291 $result->setContent( $result_array['headhtml'], $context->getOutput()->headElement( $context->getSkin() ) );
295 if ( isset( $prop['iwlinks'] ) ) {
296 $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
299 if ( isset( $prop['wikitext'] ) ) {
300 $result_array['wikitext'] = array();
301 $result->setContent( $result_array['wikitext'], $this->content->serialize( $format ) );
302 if ( !is_null( $this->pstContent ) ) {
303 $result_array['psttext'] = array();
304 $result->setContent( $result_array['psttext'], $this->pstContent->serialize( $format ) );
307 if ( isset( $prop['properties'] ) ) {
308 $result_array['properties'] = $this->formatProperties( $p_result->getProperties() );
311 if ( $params['generatexml'] ) {
312 if ( $this->content->getModel() != CONTENT_MODEL_WIKITEXT ) {
313 $this->dieUsage( "generatexml is only supported for wikitext content", "notwikitext" );
316 $wgParser->startExternalParse( $titleObj, $popts, OT_PREPROCESS );
317 $dom = $wgParser->preprocessToDom( $this->content->getNativeData() );
318 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
319 $xml = $dom->saveXML();
320 } else {
321 $xml = $dom->__toString();
323 $result_array['parsetree'] = array();
324 $result->setContent( $result_array['parsetree'], $xml );
327 $result_mapping = array(
328 'redirects' => 'r',
329 'langlinks' => 'll',
330 'categories' => 'cl',
331 'links' => 'pl',
332 'templates' => 'tl',
333 'images' => 'img',
334 'externallinks' => 'el',
335 'iwlinks' => 'iw',
336 'sections' => 's',
337 'headitems' => 'hi',
338 'properties' => 'pp',
340 $this->setIndexedTagNames( $result_array, $result_mapping );
341 $result->addValue( null, $this->getModuleName(), $result_array );
343 if ( !is_null( $oldLang ) ) {
344 $this->getContext()->setLanguage( $oldLang ); // Reset language to $oldLang
349 * @param $page WikiPage
350 * @param $popts ParserOptions
351 * @param $pageId Int
352 * @param $getWikitext Bool
353 * @return ParserOutput
355 private function getParsedContent( WikiPage $page, $popts, $pageId = null, $getWikitext = false ) {
356 $this->content = $page->getContent( Revision::RAW ); //XXX: really raw?
358 if ( $this->section !== false && $this->content !== null ) {
359 $this->content = $this->getSectionContent(
360 $this->content,
361 !is_null( $pageId ) ? 'page id ' . $pageId : $page->getTitle()->getText() );
363 // Not cached (save or load)
364 return $this->content->getParserOutput( $page->getTitle(), null, $popts );
365 } else {
366 // Try the parser cache first
367 // getParserOutput will save to Parser cache if able
368 $pout = $page->getParserOutput( $popts );
369 if ( !$pout ) {
370 $this->dieUsage( "There is no revision ID {$page->getLatest()}", 'missingrev' );
372 if ( $getWikitext ) {
373 $this->content = $page->getContent( Revision::RAW );
375 return $pout;
379 private function getSectionContent( Content $content, $what ) {
380 // Not cached (save or load)
381 $section = $content->getSection( $this->section );
382 if ( $section === false ) {
383 $this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' );
385 if ( $section === null ) {
386 $this->dieUsage( "Sections are not supported by " . $what, 'nosuchsection' );
387 $section = false;
389 return $section;
392 private function formatLangLinks( $links ) {
393 $result = array();
394 foreach ( $links as $link ) {
395 $entry = array();
396 $bits = explode( ':', $link, 2 );
397 $title = Title::newFromText( $link );
399 $entry['lang'] = $bits[0];
400 if ( $title ) {
401 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
403 $this->getResult()->setContent( $entry, $bits[1] );
404 $result[] = $entry;
406 return $result;
409 private function formatCategoryLinks( $links ) {
410 $result = array();
411 foreach ( $links as $link => $sortkey ) {
412 $entry = array();
413 $entry['sortkey'] = $sortkey;
414 $this->getResult()->setContent( $entry, $link );
415 $result[] = $entry;
417 return $result;
420 private function categoriesHtml( $categories ) {
421 $context = $this->getContext();
422 $context->getOutput()->addCategoryLinks( $categories );
423 return $context->getSkin()->getCategories();
427 * @deprecated since 1.18 No modern skin generates language links this way, please use language links
428 * data to generate your own HTML.
429 * @param $languages array
430 * @return string
432 private function languagesHtml( $languages ) {
433 wfDeprecated( __METHOD__, '1.18' );
435 global $wgContLang, $wgHideInterlanguageLinks;
437 if ( $wgHideInterlanguageLinks || count( $languages ) == 0 ) {
438 return '';
441 $s = htmlspecialchars( wfMessage( 'otherlanguages' )->text() . wfMessage( 'colon-separator' )->text() );
443 $langs = array();
444 foreach ( $languages as $l ) {
445 $nt = Title::newFromText( $l );
446 $text = Language::fetchLanguageName( $nt->getInterwiki() );
448 $langs[] = Html::element( 'a',
449 array( 'href' => $nt->getFullURL(), 'title' => $nt->getText(), 'class' => 'external' ),
450 $text == '' ? $l : $text );
453 $s .= implode( wfMessage( 'pipe-separator' )->escaped(), $langs );
455 if ( $wgContLang->isRTL() ) {
456 $s = Html::rawElement( 'span', array( 'dir' => 'LTR' ), $s );
459 return $s;
462 private function formatLinks( $links ) {
463 $result = array();
464 foreach ( $links as $ns => $nslinks ) {
465 foreach ( $nslinks as $title => $id ) {
466 $entry = array();
467 $entry['ns'] = $ns;
468 $this->getResult()->setContent( $entry, Title::makeTitle( $ns, $title )->getFullText() );
469 if ( $id != 0 ) {
470 $entry['exists'] = '';
472 $result[] = $entry;
475 return $result;
478 private function formatIWLinks( $iw ) {
479 $result = array();
480 foreach ( $iw as $prefix => $titles ) {
481 foreach ( array_keys( $titles ) as $title ) {
482 $entry = array();
483 $entry['prefix'] = $prefix;
485 $title = Title::newFromText( "{$prefix}:{$title}" );
486 if ( $title ) {
487 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
490 $this->getResult()->setContent( $entry, $title->getFullText() );
491 $result[] = $entry;
494 return $result;
497 private function formatHeadItems( $headItems ) {
498 $result = array();
499 foreach ( $headItems as $tag => $content ) {
500 $entry = array();
501 $entry['tag'] = $tag;
502 $this->getResult()->setContent( $entry, $content );
503 $result[] = $entry;
505 return $result;
508 private function formatProperties( $properties ) {
509 $result = array();
510 foreach ( $properties as $name => $value ) {
511 $entry = array();
512 $entry['name'] = $name;
513 $this->getResult()->setContent( $entry, $value );
514 $result[] = $entry;
516 return $result;
519 private function formatCss( $css ) {
520 $result = array();
521 foreach ( $css as $file => $link ) {
522 $entry = array();
523 $entry['file'] = $file;
524 $this->getResult()->setContent( $entry, $link );
525 $result[] = $entry;
527 return $result;
530 private function setIndexedTagNames( &$array, $mapping ) {
531 foreach ( $mapping as $key => $name ) {
532 if ( isset( $array[$key] ) ) {
533 $this->getResult()->setIndexedTagName( $array[$key], $name );
538 public function getAllowedParams() {
539 return array(
540 'title' => array(
541 ApiBase::PARAM_DFLT => 'API',
543 'text' => null,
544 'summary' => null,
545 'page' => null,
546 'pageid' => array(
547 ApiBase::PARAM_TYPE => 'integer',
549 'redirects' => false,
550 'oldid' => array(
551 ApiBase::PARAM_TYPE => 'integer',
553 'prop' => array(
554 ApiBase::PARAM_DFLT => 'text|langlinks|categories|links|templates|images|externallinks|sections|revid|displaytitle|iwlinks|properties',
555 ApiBase::PARAM_ISMULTI => true,
556 ApiBase::PARAM_TYPE => array(
557 'text',
558 'langlinks',
559 'languageshtml',
560 'categories',
561 'categorieshtml',
562 'links',
563 'templates',
564 'images',
565 'externallinks',
566 'sections',
567 'revid',
568 'displaytitle',
569 'headitems',
570 'headhtml',
571 'iwlinks',
572 'wikitext',
573 'properties',
576 'pst' => false,
577 'onlypst' => false,
578 'uselang' => null,
579 'section' => null,
580 'disablepp' => false,
581 'generatexml' => false,
582 'contentformat' => array(
583 ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
585 'contentmodel' => array(
586 ApiBase::PARAM_TYPE => ContentHandler::getContentModels(),
591 public function getParamDescription() {
592 $p = $this->getModulePrefix();
593 return array(
594 'text' => 'Wikitext to parse',
595 'summary' => 'Summary to parse',
596 'redirects' => "If the {$p}page or the {$p}pageid parameter is set to a redirect, resolve it",
597 'title' => 'Title of page the text belongs to',
598 'page' => "Parse the content of this page. Cannot be used together with {$p}text and {$p}title",
599 'pageid' => "Parse the content of this page. Overrides {$p}page",
600 'oldid' => "Parse the content of this revision. Overrides {$p}page and {$p}pageid",
601 'prop' => array(
602 'Which pieces of information to get',
603 ' text - Gives the parsed text of the wikitext',
604 ' langlinks - Gives the language links in the parsed wikitext',
605 ' categories - Gives the categories in the parsed wikitext',
606 ' categorieshtml - Gives the HTML version of the categories',
607 ' languageshtml - Gives the HTML version of the language links',
608 ' links - Gives the internal links in the parsed wikitext',
609 ' templates - Gives the templates in the parsed wikitext',
610 ' images - Gives the images in the parsed wikitext',
611 ' externallinks - Gives the external links in the parsed wikitext',
612 ' sections - Gives the sections in the parsed wikitext',
613 ' revid - Adds the revision ID of the parsed page',
614 ' displaytitle - Adds the title of the parsed wikitext',
615 ' headitems - Gives items to put in the <head> of the page',
616 ' headhtml - Gives parsed <head> of the page',
617 ' iwlinks - Gives interwiki links in the parsed wikitext',
618 ' wikitext - Gives the original wikitext that was parsed',
619 ' properties - Gives various properties defined in the parsed wikitext',
621 'pst' => array(
622 'Do a pre-save transform on the input before parsing it',
623 'Ignored if page, pageid or oldid is used'
625 'onlypst' => array(
626 'Do a pre-save transform (PST) on the input, but don\'t parse it',
627 'Returns the same wikitext, after a PST has been applied. Ignored if page, pageid or oldid is used'
629 'uselang' => 'Which language to parse the request in',
630 'section' => 'Only retrieve the content of this section number',
631 'disablepp' => 'Disable the PP Report from the parser output',
632 'generatexml' => 'Generate XML parse tree (requires prop=wikitext)',
633 'contentformat' => 'Content serialization format used for the input text',
634 'contentmodel' => 'Content model of the new content',
638 public function getDescription() {
639 return array(
640 'Parses wikitext and returns parser output',
641 'See the various prop-Modules of action=query to get information from the current version of a page',
645 public function getPossibleErrors() {
646 return array_merge( parent::getPossibleErrors(), array(
647 array( 'code' => 'params', 'info' => 'The page parameter cannot be used together with the text and title parameters' ),
648 array( 'code' => 'params', 'info' => 'The text parameter should be passed with the title parameter. Should you be using the "page" parameter instead?' ),
649 array( 'code' => 'missingrev', 'info' => 'There is no revision ID oldid' ),
650 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revisions' ),
651 array( 'code' => 'missingtitle', 'info' => 'The page you specified doesn\'t exist' ),
652 array( 'code' => 'nosuchsection', 'info' => 'There is no section sectionnumber in page' ),
653 array( 'nosuchpageid' ),
654 array( 'invalidtitle', 'title' ),
655 array( 'code' => 'parseerror', 'info' => 'Failed to parse the given text.' ),
656 array( 'code' => 'notwikitext', 'info' => 'The requested operation is only supported on wikitext content.' ),
657 array( 'code' => 'pagecannotexist', 'info' => "Namespace doesn't allow actual pages" ),
658 ) );
661 public function getExamples() {
662 return array(
663 'api.php?action=parse&text={{Project:Sandbox}}'
667 public function getHelpUrls() {
668 return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#parse';