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
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 __construct( $main, $action ) {
40 parent
::__construct( $main, $action );
43 public function execute() {
44 // The data is hot but user-dependent, like page views, so we set vary cookies
45 $this->getMain()->setCacheMode( 'anon-public-user-private' );
48 $params = $this->extractRequestParams();
49 $text = $params['text'];
50 $title = $params['title'];
51 $page = $params['page'];
52 $pageid = $params['pageid'];
53 $oldid = $params['oldid'];
55 $model = $params['contentmodel'];
56 $format = $params['contentformat'];
58 if ( !is_null( $page ) && ( !is_null( $text ) ||
$title != 'API' ) ) {
59 $this->dieUsage( 'The page parameter cannot be used together with the text and title parameters', 'params' );
62 $prop = array_flip( $params['prop'] );
64 if ( isset( $params['section'] ) ) {
65 $this->section
= $params['section'];
67 $this->section
= false;
70 // The parser needs $wgTitle to be set, apparently the
71 // $title parameter in Parser::parse isn't enough *sigh*
72 // TODO: Does this still need $wgTitle?
73 global $wgParser, $wgTitle;
75 // Currently unnecessary, code to act as a safeguard against any change in current behaviour of uselang
77 if ( isset( $params['uselang'] ) && $params['uselang'] != $this->getContext()->getLanguage()->getCode() ) {
78 $oldLang = $this->getContext()->getLanguage(); // Backup language
79 $this->getContext()->setLanguage( Language
::factory( $params['uselang'] ) );
85 $result = $this->getResult();
87 if ( !is_null( $oldid ) ||
!is_null( $pageid ) ||
!is_null( $page ) ) {
88 if ( !is_null( $oldid ) ) {
89 // Don't use the parser cache
90 $rev = Revision
::newFromID( $oldid );
92 $this->dieUsage( "There is no revision ID $oldid", 'missingrev' );
94 if ( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
95 $this->dieUsage( "You don't have permission to view deleted revisions", 'permissiondenied' );
98 $titleObj = $rev->getTitle();
100 $pageObj = WikiPage
::factory( $titleObj );
101 $popts = $pageObj->makeParserOptions( $this->getContext() );
102 $popts->enableLimitReport( !$params['disablepp'] );
104 // If for some reason the "oldid" is actually the current revision, it may be cached
105 if ( $rev->isCurrent() ) {
106 // May get from/save to parser cache
107 $p_result = $this->getParsedContent( $pageObj, $popts,
108 $pageid, isset( $prop['wikitext'] ) ) ;
109 } else { // This is an old revision, so get the text differently
110 $this->content
= $rev->getContent( Revision
::FOR_THIS_USER
, $this->getUser() );
112 if ( $this->section
!== false ) {
113 $this->content
= $this->getSectionContent( $this->content
, 'r' . $rev->getId() );
116 // Should we save old revision parses to the parser cache?
117 $p_result = $this->content
->getParserOutput( $titleObj, $rev->getId(), $popts );
119 } else { // Not $oldid, but $pageid or $page
120 if ( $params['redirects'] ) {
125 if ( !is_null ( $pageid ) ) {
126 $reqParams['pageids'] = $pageid;
128 $reqParams['titles'] = $page;
130 $req = new FauxRequest( $reqParams );
131 $main = new ApiMain( $req );
133 $data = $main->getResultData();
134 $redirValues = isset( $data['query']['redirects'] )
135 ?
$data['query']['redirects']
138 foreach ( (array)$redirValues as $r ) {
141 $pageParams = array( 'title' => $to );
142 } elseif ( !is_null( $pageid ) ) {
143 $pageParams = array( 'pageid' => $pageid );
145 $pageParams = array( 'title' => $page );
148 $pageObj = $this->getTitleOrPageId( $pageParams, 'fromdb' );
149 $titleObj = $pageObj->getTitle();
150 if ( !$titleObj ||
!$titleObj->exists() ) {
151 $this->dieUsage( "The page you specified doesn't exist", 'missingtitle' );
153 $wgTitle = $titleObj;
155 if ( isset( $prop['revid'] ) ) {
156 $oldid = $pageObj->getLatest();
159 $popts = $pageObj->makeParserOptions( $this->getContext() );
160 $popts->enableLimitReport( !$params['disablepp'] );
162 // Potentially cached
163 $p_result = $this->getParsedContent( $pageObj, $popts, $pageid,
164 isset( $prop['wikitext'] ) ) ;
166 } else { // Not $oldid, $pageid, $page. Hence based on $text
167 $titleObj = Title
::newFromText( $title );
169 $this->dieUsageMsg( array( 'invalidtitle', $title ) );
171 if ( !$titleObj->canExist() ) {
172 $this->dieUsage( "Namespace doesn't allow actual pages", 'pagecannotexist' );
174 $wgTitle = $titleObj;
175 $pageObj = WikiPage
::factory( $titleObj );
177 $popts = $pageObj->makeParserOptions( $this->getContext() );
178 $popts->enableLimitReport( !$params['disablepp'] );
180 if ( is_null( $text ) ) {
181 $this->dieUsage( 'The text parameter should be passed with the title parameter. Should you be using the "page" parameter instead?', 'params' );
185 $this->content
= ContentHandler
::makeContent( $text, $titleObj, $model, $format );
186 } catch ( MWContentSerializationException
$ex ) {
187 $this->dieUsage( $ex->getMessage(), 'parseerror' );
190 if ( $this->section
!== false ) {
191 $this->content
= $this->getSectionContent( $this->content
, $titleObj->getText() );
194 if ( $params['pst'] ||
$params['onlypst'] ) {
195 $this->pstContent
= $this->content
->preSaveTransform( $titleObj, $this->getUser(), $popts );
197 if ( $params['onlypst'] ) {
198 // Build a result and bail out
199 $result_array = array();
200 $result_array['text'] = array();
201 $result->setContent( $result_array['text'], $this->pstContent
->serialize( $format ) );
202 if ( isset( $prop['wikitext'] ) ) {
203 $result_array['wikitext'] = array();
204 $result->setContent( $result_array['wikitext'], $this->content
->serialize( $format ) );
206 $result->addValue( null, $this->getModuleName(), $result_array );
210 // Not cached (save or load)
211 if ( $params['pst'] ) {
212 $p_result = $this->pstContent
->getParserOutput( $titleObj, null, $popts );
214 $p_result = $this->content
->getParserOutput( $titleObj, null, $popts );
218 $result_array = array();
220 $result_array['title'] = $titleObj->getPrefixedText();
222 if ( !is_null( $oldid ) ) {
223 $result_array['revid'] = intval( $oldid );
226 if ( $params['redirects'] && !is_null( $redirValues ) ) {
227 $result_array['redirects'] = $redirValues;
230 if ( isset( $prop['text'] ) ) {
231 $result_array['text'] = array();
232 $result->setContent( $result_array['text'], $p_result->getText() );
235 if ( !is_null( $params['summary'] ) ) {
236 $result_array['parsedsummary'] = array();
237 $result->setContent( $result_array['parsedsummary'], Linker
::formatComment( $params['summary'], $titleObj ) );
240 if ( isset( $prop['langlinks'] ) ) {
241 $result_array['langlinks'] = $this->formatLangLinks( $p_result->getLanguageLinks() );
243 if ( isset( $prop['languageshtml'] ) ) {
244 $languagesHtml = $this->languagesHtml( $p_result->getLanguageLinks() );
245 $result_array['languageshtml'] = array();
246 $result->setContent( $result_array['languageshtml'], $languagesHtml );
248 if ( isset( $prop['categories'] ) ) {
249 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
251 if ( isset( $prop['categorieshtml'] ) ) {
252 $categoriesHtml = $this->categoriesHtml( $p_result->getCategories() );
253 $result_array['categorieshtml'] = array();
254 $result->setContent( $result_array['categorieshtml'], $categoriesHtml );
256 if ( isset( $prop['links'] ) ) {
257 $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
259 if ( isset( $prop['templates'] ) ) {
260 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
262 if ( isset( $prop['images'] ) ) {
263 $result_array['images'] = array_keys( $p_result->getImages() );
265 if ( isset( $prop['externallinks'] ) ) {
266 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
268 if ( isset( $prop['sections'] ) ) {
269 $result_array['sections'] = $p_result->getSections();
272 if ( isset( $prop['displaytitle'] ) ) {
273 $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
274 $p_result->getDisplayTitle() :
275 $titleObj->getPrefixedText();
278 if ( isset( $prop['headitems'] ) ||
isset( $prop['headhtml'] ) ) {
279 $context = $this->getContext();
280 $context->setTitle( $titleObj );
281 $context->getOutput()->addParserOutputNoText( $p_result );
283 if ( isset( $prop['headitems'] ) ) {
284 $headItems = $this->formatHeadItems( $p_result->getHeadItems() );
286 $css = $this->formatCss( $context->getOutput()->buildCssLinksArray() );
288 $scripts = array( $context->getOutput()->getHeadScripts() );
290 $result_array['headitems'] = array_merge( $headItems, $css, $scripts );
293 if ( isset( $prop['headhtml'] ) ) {
294 $result_array['headhtml'] = array();
295 $result->setContent( $result_array['headhtml'], $context->getOutput()->headElement( $context->getSkin() ) );
299 if ( isset( $prop['iwlinks'] ) ) {
300 $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
303 if ( isset( $prop['wikitext'] ) ) {
304 $result_array['wikitext'] = array();
305 $result->setContent( $result_array['wikitext'], $this->content
->serialize( $format ) );
306 if ( !is_null( $this->pstContent
) ) {
307 $result_array['psttext'] = array();
308 $result->setContent( $result_array['psttext'], $this->pstContent
->serialize( $format ) );
311 if ( isset( $prop['properties'] ) ) {
312 $result_array['properties'] = $this->formatProperties( $p_result->getProperties() );
315 if ( $params['generatexml'] ) {
316 if ( $this->content
->getModel() != CONTENT_MODEL_WIKITEXT
) {
317 $this->dieUsage( "generatexml is only supported for wikitext content", "notwikitext" );
320 $wgParser->startExternalParse( $titleObj, $popts, OT_PREPROCESS
);
321 $dom = $wgParser->preprocessToDom( $this->content
->getNativeData() );
322 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
323 $xml = $dom->saveXML();
325 $xml = $dom->__toString();
327 $result_array['parsetree'] = array();
328 $result->setContent( $result_array['parsetree'], $xml );
331 $result_mapping = array(
334 'categories' => 'cl',
338 'externallinks' => 'el',
342 'properties' => 'pp',
344 $this->setIndexedTagNames( $result_array, $result_mapping );
345 $result->addValue( null, $this->getModuleName(), $result_array );
347 if ( !is_null( $oldLang ) ) {
348 $this->getContext()->setLanguage( $oldLang ); // Reset language to $oldLang
353 * @param $page WikiPage
354 * @param $popts ParserOptions
356 * @param $getWikitext Bool
357 * @return ParserOutput
359 private function getParsedContent( WikiPage
$page, $popts, $pageId = null, $getWikitext = false ) {
360 $this->content
= $page->getContent( Revision
::RAW
); //XXX: really raw?
362 if ( $this->section
!== false && $this->content
!== null ) {
363 $this->content
= $this->getSectionContent(
365 !is_null( $pageId ) ?
'page id ' . $pageId : $page->getTitle()->getText() );
367 // Not cached (save or load)
368 return $this->content
->getParserOutput( $page->getTitle(), null, $popts );
370 // Try the parser cache first
371 // getParserOutput will save to Parser cache if able
372 $pout = $page->getParserOutput( $popts );
374 $this->dieUsage( "There is no revision ID {$page->getLatest()}", 'missingrev' );
376 if ( $getWikitext ) {
377 $this->content
= $page->getContent( Revision
::RAW
);
383 private function getSectionContent( Content
$content, $what ) {
384 // Not cached (save or load)
385 $section = $content->getSection( $this->section
);
386 if ( $section === false ) {
387 $this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' );
389 if ( $section === null ) {
390 $this->dieUsage( "Sections are not supported by " . $what, 'nosuchsection' );
396 private function formatLangLinks( $links ) {
398 foreach ( $links as $link ) {
400 $bits = explode( ':', $link, 2 );
401 $title = Title
::newFromText( $link );
403 $entry['lang'] = $bits[0];
405 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
);
407 $this->getResult()->setContent( $entry, $bits[1] );
413 private function formatCategoryLinks( $links ) {
415 foreach ( $links as $link => $sortkey ) {
417 $entry['sortkey'] = $sortkey;
418 $this->getResult()->setContent( $entry, $link );
424 private function categoriesHtml( $categories ) {
425 $context = $this->getContext();
426 $context->getOutput()->addCategoryLinks( $categories );
427 return $context->getSkin()->getCategories();
431 * @deprecated since 1.18 No modern skin generates language links this way, please use language links
432 * data to generate your own HTML.
433 * @param $languages array
436 private function languagesHtml( $languages ) {
437 wfDeprecated( __METHOD__
, '1.18' );
439 global $wgContLang, $wgHideInterlanguageLinks;
441 if ( $wgHideInterlanguageLinks ||
count( $languages ) == 0 ) {
445 $s = htmlspecialchars( wfMessage( 'otherlanguages' )->text() . wfMessage( 'colon-separator' )->text() );
448 foreach ( $languages as $l ) {
449 $nt = Title
::newFromText( $l );
450 $text = Language
::fetchLanguageName( $nt->getInterwiki() );
452 $langs[] = Html
::element( 'a',
453 array( 'href' => $nt->getFullURL(), 'title' => $nt->getText(), 'class' => "external" ),
454 $text == '' ?
$l : $text );
457 $s .= implode( wfMessage( 'pipe-separator' )->escaped(), $langs );
459 if ( $wgContLang->isRTL() ) {
460 $s = Html
::rawElement( 'span', array( 'dir' => "LTR" ), $s );
466 private function formatLinks( $links ) {
468 foreach ( $links as $ns => $nslinks ) {
469 foreach ( $nslinks as $title => $id ) {
472 $this->getResult()->setContent( $entry, Title
::makeTitle( $ns, $title )->getFullText() );
474 $entry['exists'] = '';
482 private function formatIWLinks( $iw ) {
484 foreach ( $iw as $prefix => $titles ) {
485 foreach ( array_keys( $titles ) as $title ) {
487 $entry['prefix'] = $prefix;
489 $title = Title
::newFromText( "{$prefix}:{$title}" );
491 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
);
494 $this->getResult()->setContent( $entry, $title->getFullText() );
501 private function formatHeadItems( $headItems ) {
503 foreach ( $headItems as $tag => $content ) {
505 $entry['tag'] = $tag;
506 $this->getResult()->setContent( $entry, $content );
512 private function formatProperties( $properties ) {
514 foreach ( $properties as $name => $value ) {
516 $entry['name'] = $name;
517 $this->getResult()->setContent( $entry, $value );
523 private function formatCss( $css ) {
525 foreach ( $css as $file => $link ) {
527 $entry['file'] = $file;
528 $this->getResult()->setContent( $entry, $link );
534 private function setIndexedTagNames( &$array, $mapping ) {
535 foreach ( $mapping as $key => $name ) {
536 if ( isset( $array[$key] ) ) {
537 $this->getResult()->setIndexedTagName( $array[$key], $name );
542 public function getAllowedParams() {
545 ApiBase
::PARAM_DFLT
=> 'API',
551 ApiBase
::PARAM_TYPE
=> 'integer',
553 'redirects' => false,
555 ApiBase
::PARAM_TYPE
=> 'integer',
558 ApiBase
::PARAM_DFLT
=> 'text|langlinks|categories|links|templates|images|externallinks|sections|revid|displaytitle|iwlinks|properties',
559 ApiBase
::PARAM_ISMULTI
=> true,
560 ApiBase
::PARAM_TYPE
=> array(
584 'disablepp' => false,
585 'generatexml' => false,
586 'contentformat' => array(
587 ApiBase
::PARAM_TYPE
=> ContentHandler
::getAllContentFormats(),
589 'contentmodel' => array(
590 ApiBase
::PARAM_TYPE
=> ContentHandler
::getContentModels(),
595 public function getParamDescription() {
596 $p = $this->getModulePrefix();
598 'text' => 'Wikitext to parse',
599 'summary' => 'Summary to parse',
600 'redirects' => "If the {$p}page or the {$p}pageid parameter is set to a redirect, resolve it",
601 'title' => 'Title of page the text belongs to',
602 'page' => "Parse the content of this page. Cannot be used together with {$p}text and {$p}title",
603 'pageid' => "Parse the content of this page. Overrides {$p}page",
604 'oldid' => "Parse the content of this revision. Overrides {$p}page and {$p}pageid",
606 'Which pieces of information to get',
607 ' text - Gives the parsed text of the wikitext',
608 ' langlinks - Gives the language links in the parsed wikitext',
609 ' categories - Gives the categories in the parsed wikitext',
610 ' categorieshtml - Gives the HTML version of the categories',
611 ' languageshtml - Gives the HTML version of the language links',
612 ' links - Gives the internal links in the parsed wikitext',
613 ' templates - Gives the templates in the parsed wikitext',
614 ' images - Gives the images in the parsed wikitext',
615 ' externallinks - Gives the external links in the parsed wikitext',
616 ' sections - Gives the sections in the parsed wikitext',
617 ' revid - Adds the revision ID of the parsed page',
618 ' displaytitle - Adds the title of the parsed wikitext',
619 ' headitems - Gives items to put in the <head> of the page',
620 ' headhtml - Gives parsed <head> of the page',
621 ' iwlinks - Gives interwiki links in the parsed wikitext',
622 ' wikitext - Gives the original wikitext that was parsed',
623 ' properties - Gives various properties defined in the parsed wikitext',
626 'Do a pre-save transform on the input before parsing it',
627 'Ignored if page, pageid or oldid is used'
630 'Do a pre-save transform (PST) on the input, but don\'t parse it',
631 'Returns the same wikitext, after a PST has been applied. Ignored if page, pageid or oldid is used'
633 'uselang' => 'Which language to parse the request in',
634 'section' => 'Only retrieve the content of this section number',
635 'disablepp' => 'Disable the PP Report from the parser output',
636 'generatexml' => 'Generate XML parse tree (requires prop=wikitext)',
637 'contentformat' => 'Content serialization format used for the input text',
638 'contentmodel' => 'Content model of the new content',
642 public function getDescription() {
644 'Parses wikitext and returns parser output',
645 'See the various prop-Modules of action=query to get information from the current version of a page',
649 public function getPossibleErrors() {
650 return array_merge( parent
::getPossibleErrors(), array(
651 array( 'code' => 'params', 'info' => 'The page parameter cannot be used together with the text and title parameters' ),
652 array( 'code' => 'params', 'info' => 'The text parameter should be passed with the title parameter. Should you be using the "page" parameter instead?' ),
653 array( 'code' => 'missingrev', 'info' => 'There is no revision ID oldid' ),
654 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revisions' ),
655 array( 'code' => 'missingtitle', 'info' => 'The page you specified doesn\'t exist' ),
656 array( 'code' => 'nosuchsection', 'info' => 'There is no section sectionnumber in page' ),
657 array( 'nosuchpageid' ),
658 array( 'invalidtitle', 'title' ),
659 array( 'code' => 'parseerror', 'info' => 'Failed to parse the given text.' ),
660 array( 'code' => 'notwikitext', 'info' => 'The requested operation is only supported on wikitext content.' ),
661 array( 'code' => 'pagecannotexist', 'info' => "Namespace doesn't allow actual pages" ),
665 public function getExamples() {
667 'api.php?action=parse&text={{Project:Sandbox}}'
671 public function getHelpUrls() {
672 return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#parse';
675 public function getVersion() {
676 return __CLASS__
. ': $Id$';