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 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' );
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'];
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
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'] ) );
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 );
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();
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'] ) {
121 if ( !is_null ( $pageid ) ) {
122 $reqParams['pageids'] = $pageid;
124 $reqParams['titles'] = $page;
126 $req = new FauxRequest( $reqParams );
127 $main = new ApiMain( $req );
129 $data = $main->getResultData();
130 $redirValues = isset( $data['query']['redirects'] )
131 ?
$data['query']['redirects']
134 foreach ( (array)$redirValues as $r ) {
137 $pageParams = array( 'title' => $to );
138 } elseif ( !is_null( $pageid ) ) {
139 $pageParams = array( 'pageid' => $pageid );
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' );
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 ApiResult
::setContent( $result_array['text'], $this->pstContent
->serialize( $format ) );
198 if ( isset( $prop['wikitext'] ) ) {
199 $result_array['wikitext'] = array();
200 ApiResult
::setContent( $result_array['wikitext'], $this->content
->serialize( $format ) );
202 $result->addValue( null, $this->getModuleName(), $result_array );
206 // Not cached (save or load)
207 if ( $params['pst'] ) {
208 $p_result = $this->pstContent
->getParserOutput( $titleObj, null, $popts );
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 ApiResult
::setContent( $result_array['text'], $p_result->getText() );
231 if ( !is_null( $params['summary'] ) ) {
232 $result_array['parsedsummary'] = array();
233 ApiResult
::setContent( $result_array['parsedsummary'], Linker
::formatComment( $params['summary'], $titleObj ) );
236 if ( isset( $prop['langlinks'] ) ||
isset( $prop['languageshtml'] ) ) {
237 $langlinks = $p_result->getLanguageLinks();
239 if ( $params['effectivelanglinks'] ) {
240 // Link flags are ignored for now, but may in the future be
241 // included in the result.
242 $linkFlags = array();
243 wfRunHooks( 'LanguageLinks', array( $titleObj, &$langlinks, &$linkFlags ) );
249 if ( isset( $prop['langlinks'] ) ) {
250 $result_array['langlinks'] = $this->formatLangLinks( $langlinks );
252 if ( isset( $prop['languageshtml'] ) ) {
253 $languagesHtml = $this->languagesHtml( $langlinks );
255 $result_array['languageshtml'] = array();
256 ApiResult
::setContent( $result_array['languageshtml'], $languagesHtml );
258 if ( isset( $prop['categories'] ) ) {
259 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
261 if ( isset( $prop['categorieshtml'] ) ) {
262 $categoriesHtml = $this->categoriesHtml( $p_result->getCategories() );
263 $result_array['categorieshtml'] = array();
264 ApiResult
::setContent( $result_array['categorieshtml'], $categoriesHtml );
266 if ( isset( $prop['links'] ) ) {
267 $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
269 if ( isset( $prop['templates'] ) ) {
270 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
272 if ( isset( $prop['images'] ) ) {
273 $result_array['images'] = array_keys( $p_result->getImages() );
275 if ( isset( $prop['externallinks'] ) ) {
276 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
278 if ( isset( $prop['sections'] ) ) {
279 $result_array['sections'] = $p_result->getSections();
282 if ( isset( $prop['displaytitle'] ) ) {
283 $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
284 $p_result->getDisplayTitle() :
285 $titleObj->getPrefixedText();
288 if ( isset( $prop['headitems'] ) ||
isset( $prop['headhtml'] ) ) {
289 $context = $this->getContext();
290 $context->setTitle( $titleObj );
291 $context->getOutput()->addParserOutputNoText( $p_result );
293 if ( isset( $prop['headitems'] ) ) {
294 $headItems = $this->formatHeadItems( $p_result->getHeadItems() );
296 $css = $this->formatCss( $context->getOutput()->buildCssLinksArray() );
298 $scripts = array( $context->getOutput()->getHeadScripts() );
300 $result_array['headitems'] = array_merge( $headItems, $css, $scripts );
303 if ( isset( $prop['headhtml'] ) ) {
304 $result_array['headhtml'] = array();
305 ApiResult
::setContent( $result_array['headhtml'], $context->getOutput()->headElement( $context->getSkin() ) );
309 if ( isset( $prop['iwlinks'] ) ) {
310 $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
313 if ( isset( $prop['wikitext'] ) ) {
314 $result_array['wikitext'] = array();
315 ApiResult
::setContent( $result_array['wikitext'], $this->content
->serialize( $format ) );
316 if ( !is_null( $this->pstContent
) ) {
317 $result_array['psttext'] = array();
318 ApiResult
::setContent( $result_array['psttext'], $this->pstContent
->serialize( $format ) );
321 if ( isset( $prop['properties'] ) ) {
322 $result_array['properties'] = $this->formatProperties( $p_result->getProperties() );
325 if ( $params['generatexml'] ) {
326 if ( $this->content
->getModel() != CONTENT_MODEL_WIKITEXT
) {
327 $this->dieUsage( "generatexml is only supported for wikitext content", "notwikitext" );
330 $wgParser->startExternalParse( $titleObj, $popts, OT_PREPROCESS
);
331 $dom = $wgParser->preprocessToDom( $this->content
->getNativeData() );
332 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
333 $xml = $dom->saveXML();
335 $xml = $dom->__toString();
337 $result_array['parsetree'] = array();
338 ApiResult
::setContent( $result_array['parsetree'], $xml );
341 $result_mapping = array(
344 'categories' => 'cl',
348 'externallinks' => 'el',
352 'properties' => 'pp',
354 $this->setIndexedTagNames( $result_array, $result_mapping );
355 $result->addValue( null, $this->getModuleName(), $result_array );
357 if ( !is_null( $oldLang ) ) {
358 $this->getContext()->setLanguage( $oldLang ); // Reset language to $oldLang
363 * @param $page WikiPage
364 * @param $popts ParserOptions
366 * @param $getWikitext Bool
367 * @return ParserOutput
369 private function getParsedContent( WikiPage
$page, $popts, $pageId = null, $getWikitext = false ) {
370 $this->content
= $page->getContent( Revision
::RAW
); //XXX: really raw?
372 if ( $this->section
!== false && $this->content
!== null ) {
373 $this->content
= $this->getSectionContent(
375 !is_null( $pageId ) ?
'page id ' . $pageId : $page->getTitle()->getText() );
377 // Not cached (save or load)
378 return $this->content
->getParserOutput( $page->getTitle(), null, $popts );
380 // Try the parser cache first
381 // getParserOutput will save to Parser cache if able
382 $pout = $page->getParserOutput( $popts );
384 $this->dieUsage( "There is no revision ID {$page->getLatest()}", 'missingrev' );
386 if ( $getWikitext ) {
387 $this->content
= $page->getContent( Revision
::RAW
);
393 private function getSectionContent( Content
$content, $what ) {
394 // Not cached (save or load)
395 $section = $content->getSection( $this->section
);
396 if ( $section === false ) {
397 $this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' );
399 if ( $section === null ) {
400 $this->dieUsage( "Sections are not supported by " . $what, 'nosuchsection' );
406 private function formatLangLinks( $links ) {
408 foreach ( $links as $link ) {
410 $bits = explode( ':', $link, 2 );
411 $title = Title
::newFromText( $link );
413 $entry['lang'] = $bits[0];
415 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
);
417 ApiResult
::setContent( $entry, $bits[1] );
423 private function formatCategoryLinks( $links ) {
425 foreach ( $links as $link => $sortkey ) {
427 $entry['sortkey'] = $sortkey;
428 ApiResult
::setContent( $entry, $link );
434 private function categoriesHtml( $categories ) {
435 $context = $this->getContext();
436 $context->getOutput()->addCategoryLinks( $categories );
437 return $context->getSkin()->getCategories();
441 * @deprecated since 1.18 No modern skin generates language links this way, please use language links
442 * data to generate your own HTML.
443 * @param $languages array
446 private function languagesHtml( $languages ) {
447 wfDeprecated( __METHOD__
, '1.18' );
449 global $wgContLang, $wgHideInterlanguageLinks;
451 if ( $wgHideInterlanguageLinks ||
count( $languages ) == 0 ) {
455 $s = htmlspecialchars( wfMessage( 'otherlanguages' )->text() . wfMessage( 'colon-separator' )->text() );
458 foreach ( $languages as $l ) {
459 $nt = Title
::newFromText( $l );
460 $text = Language
::fetchLanguageName( $nt->getInterwiki() );
462 $langs[] = Html
::element( 'a',
463 array( 'href' => $nt->getFullURL(), 'title' => $nt->getText(), 'class' => 'external' ),
464 $text == '' ?
$l : $text );
467 $s .= implode( wfMessage( 'pipe-separator' )->escaped(), $langs );
469 if ( $wgContLang->isRTL() ) {
470 $s = Html
::rawElement( 'span', array( 'dir' => 'LTR' ), $s );
476 private function formatLinks( $links ) {
478 foreach ( $links as $ns => $nslinks ) {
479 foreach ( $nslinks as $title => $id ) {
482 ApiResult
::setContent( $entry, Title
::makeTitle( $ns, $title )->getFullText() );
484 $entry['exists'] = '';
492 private function formatIWLinks( $iw ) {
494 foreach ( $iw as $prefix => $titles ) {
495 foreach ( array_keys( $titles ) as $title ) {
497 $entry['prefix'] = $prefix;
499 $title = Title
::newFromText( "{$prefix}:{$title}" );
501 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
);
504 ApiResult
::setContent( $entry, $title->getFullText() );
511 private function formatHeadItems( $headItems ) {
513 foreach ( $headItems as $tag => $content ) {
515 $entry['tag'] = $tag;
516 ApiResult
::setContent( $entry, $content );
522 private function formatProperties( $properties ) {
524 foreach ( $properties as $name => $value ) {
526 $entry['name'] = $name;
527 ApiResult
::setContent( $entry, $value );
533 private function formatCss( $css ) {
535 foreach ( $css as $file => $link ) {
537 $entry['file'] = $file;
538 ApiResult
::setContent( $entry, $link );
544 private function setIndexedTagNames( &$array, $mapping ) {
545 foreach ( $mapping as $key => $name ) {
546 if ( isset( $array[$key] ) ) {
547 $this->getResult()->setIndexedTagName( $array[$key], $name );
552 public function getAllowedParams() {
555 ApiBase
::PARAM_DFLT
=> 'API',
561 ApiBase
::PARAM_TYPE
=> 'integer',
563 'redirects' => false,
565 ApiBase
::PARAM_TYPE
=> 'integer',
568 ApiBase
::PARAM_DFLT
=> 'text|langlinks|categories|links|templates|images|externallinks|sections|revid|displaytitle|iwlinks|properties',
569 ApiBase
::PARAM_ISMULTI
=> true,
570 ApiBase
::PARAM_TYPE
=> array(
592 'effectivelanglinks' => false,
595 'disablepp' => false,
596 'generatexml' => false,
597 'contentformat' => array(
598 ApiBase
::PARAM_TYPE
=> ContentHandler
::getAllContentFormats(),
600 'contentmodel' => array(
601 ApiBase
::PARAM_TYPE
=> ContentHandler
::getContentModels(),
606 public function getParamDescription() {
607 $p = $this->getModulePrefix();
609 'text' => 'Wikitext to parse',
610 'summary' => 'Summary to parse',
611 'redirects' => "If the {$p}page or the {$p}pageid parameter is set to a redirect, resolve it",
612 'title' => 'Title of page the text belongs to',
613 'page' => "Parse the content of this page. Cannot be used together with {$p}text and {$p}title",
614 'pageid' => "Parse the content of this page. Overrides {$p}page",
615 'oldid' => "Parse the content of this revision. Overrides {$p}page and {$p}pageid",
617 'Which pieces of information to get',
618 ' text - Gives the parsed text of the wikitext',
619 ' langlinks - Gives the language links in the parsed wikitext',
620 ' categories - Gives the categories in the parsed wikitext',
621 ' categorieshtml - Gives the HTML version of the categories',
622 ' languageshtml - Gives the HTML version of the language links',
623 ' links - Gives the internal links in the parsed wikitext',
624 ' templates - Gives the templates in the parsed wikitext',
625 ' images - Gives the images in the parsed wikitext',
626 ' externallinks - Gives the external links in the parsed wikitext',
627 ' sections - Gives the sections in the parsed wikitext',
628 ' revid - Adds the revision ID of the parsed page',
629 ' displaytitle - Adds the title of the parsed wikitext',
630 ' headitems - Gives items to put in the <head> of the page',
631 ' headhtml - Gives parsed <head> of the page',
632 ' iwlinks - Gives interwiki links in the parsed wikitext',
633 ' wikitext - Gives the original wikitext that was parsed',
634 ' properties - Gives various properties defined in the parsed wikitext',
636 'effectivelanglinks' => array(
637 'Includes language links supplied by extensions',
638 '(for use with prop=langlinks|languageshtml)',
641 'Do a pre-save transform on the input before parsing it',
642 'Ignored if page, pageid or oldid is used'
645 'Do a pre-save transform (PST) on the input, but don\'t parse it',
646 'Returns the same wikitext, after a PST has been applied. Ignored if page, pageid or oldid is used'
648 'uselang' => 'Which language to parse the request in',
649 'section' => 'Only retrieve the content of this section number',
650 'disablepp' => 'Disable the PP Report from the parser output',
651 'generatexml' => 'Generate XML parse tree (requires prop=wikitext)',
652 'contentformat' => 'Content serialization format used for the input text',
653 'contentmodel' => 'Content model of the new content',
657 public function getDescription() {
659 'Parses wikitext and returns parser output',
660 'See the various prop-Modules of action=query to get information from the current version of a page',
664 public function getPossibleErrors() {
665 return array_merge( parent
::getPossibleErrors(), array(
666 array( 'code' => 'params', 'info' => 'The page parameter cannot be used together with the text and title parameters' ),
667 array( 'code' => 'params', 'info' => 'The text parameter should be passed with the title parameter. Should you be using the "page" parameter instead?' ),
668 array( 'code' => 'missingrev', 'info' => 'There is no revision ID oldid' ),
669 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revisions' ),
670 array( 'code' => 'missingtitle', 'info' => 'The page you specified doesn\'t exist' ),
671 array( 'code' => 'nosuchsection', 'info' => 'There is no section sectionnumber in page' ),
672 array( 'nosuchpageid' ),
673 array( 'invalidtitle', 'title' ),
674 array( 'code' => 'parseerror', 'info' => 'Failed to parse the given text.' ),
675 array( 'code' => 'notwikitext', 'info' => 'The requested operation is only supported on wikitext content.' ),
676 array( 'code' => 'pagecannotexist', 'info' => "Namespace doesn't allow actual pages" ),
680 public function getExamples() {
682 'api.php?action=parse&text={{Project:Sandbox}}'
686 public function getHelpUrls() {
687 return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#parse';