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 if ( $title === null ) {
48 $titleProvided = false;
49 // A title is needed for parsing, so arbitrarily choose one
52 $titleProvided = true;
55 $page = $params['page'];
56 $pageid = $params['pageid'];
57 $oldid = $params['oldid'];
59 $model = $params['contentmodel'];
60 $format = $params['contentformat'];
62 if ( !is_null( $page ) && ( !is_null( $text ) ||
$titleProvided ) ) {
64 'The page parameter cannot be used together with the text and title parameters',
69 $prop = array_flip( $params['prop'] );
71 if ( isset( $params['section'] ) ) {
72 $this->section
= $params['section'];
73 if ( !preg_match( '/^((T-)?\d+|new)$/', $this->section
) ) {
74 $this->dieUsage( "The section parameter must be a valid section id or 'new'", "invalidsection" );
77 $this->section
= false;
80 // The parser needs $wgTitle to be set, apparently the
81 // $title parameter in Parser::parse isn't enough *sigh*
82 // TODO: Does this still need $wgTitle?
83 global $wgParser, $wgTitle;
88 $result = $this->getResult();
90 if ( !is_null( $oldid ) ||
!is_null( $pageid ) ||
!is_null( $page ) ) {
91 if ( $this->section
=== 'new' ) {
92 $this->dieUsage( 'section=new cannot be combined with oldid, pageid or page parameters. Please use text', 'params' );
94 if ( !is_null( $oldid ) ) {
95 // Don't use the parser cache
96 $rev = Revision
::newFromId( $oldid );
98 $this->dieUsage( "There is no revision ID $oldid", 'missingrev' );
100 if ( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
101 $this->dieUsage( "You don't have permission to view deleted revisions", 'permissiondenied' );
104 $titleObj = $rev->getTitle();
105 $wgTitle = $titleObj;
106 $pageObj = WikiPage
::factory( $titleObj );
107 $popts = $this->makeParserOptions( $pageObj, $params );
109 // If for some reason the "oldid" is actually the current revision, it may be cached
110 // Deliberately comparing $pageObj->getLatest() with $rev->getId(), rather than
111 // checking $rev->isCurrent(), because $pageObj is what actually ends up being used,
112 // and if its ->getLatest() is outdated, $rev->isCurrent() won't tell us that.
113 if ( $rev->getId() == $pageObj->getLatest() ) {
114 // May get from/save to parser cache
115 $p_result = $this->getParsedContent( $pageObj, $popts,
116 $pageid, isset( $prop['wikitext'] ) );
117 } else { // This is an old revision, so get the text differently
118 $this->content
= $rev->getContent( Revision
::FOR_THIS_USER
, $this->getUser() );
120 if ( $this->section
!== false ) {
121 $this->content
= $this->getSectionContent( $this->content
, 'r' . $rev->getId() );
124 // Should we save old revision parses to the parser cache?
125 $p_result = $this->content
->getParserOutput( $titleObj, $rev->getId(), $popts );
127 } else { // Not $oldid, but $pageid or $page
128 if ( $params['redirects'] ) {
132 if ( !is_null( $pageid ) ) {
133 $reqParams['pageids'] = $pageid;
135 $reqParams['titles'] = $page;
137 $req = new FauxRequest( $reqParams );
138 $main = new ApiMain( $req );
139 $pageSet = new ApiPageSet( $main );
143 foreach ( $pageSet->getRedirectTitles() as $title ) {
144 $to = $title->getFullText();
146 $pageParams = array( 'title' => $to );
147 } elseif ( !is_null( $pageid ) ) {
148 $pageParams = array( 'pageid' => $pageid );
150 $pageParams = array( 'title' => $page );
153 $pageObj = $this->getTitleOrPageId( $pageParams, 'fromdb' );
154 $titleObj = $pageObj->getTitle();
155 if ( !$titleObj ||
!$titleObj->exists() ) {
156 $this->dieUsage( "The page you specified doesn't exist", 'missingtitle' );
158 $wgTitle = $titleObj;
160 if ( isset( $prop['revid'] ) ) {
161 $oldid = $pageObj->getLatest();
164 $popts = $this->makeParserOptions( $pageObj, $params );
166 // Potentially cached
167 $p_result = $this->getParsedContent( $pageObj, $popts, $pageid,
168 isset( $prop['wikitext'] ) );
170 } else { // Not $oldid, $pageid, $page. Hence based on $text
171 $titleObj = Title
::newFromText( $title );
172 if ( !$titleObj ||
$titleObj->isExternal() ) {
173 $this->dieUsageMsg( array( 'invalidtitle', $title ) );
175 $wgTitle = $titleObj;
176 if ( $titleObj->canExist() ) {
177 $pageObj = WikiPage
::factory( $titleObj );
179 // Do like MediaWiki::initializeArticle()
180 $article = Article
::newFromTitle( $titleObj, $this->getContext() );
181 $pageObj = $article->getPage();
184 $popts = $this->makeParserOptions( $pageObj, $params );
185 $textProvided = !is_null( $text );
187 if ( !$textProvided ) {
188 if ( $titleProvided && ( $prop ||
$params['generatexml'] ) ) {
190 "'title' used without 'text', and parsed page properties were requested " .
191 "(did you mean to use 'page' instead of 'title'?)"
194 // Prevent warning from ContentHandler::makeContent()
198 // If we are parsing text, do not use the content model of the default
199 // API title, but default to wikitext to keep BC.
200 if ( $textProvided && !$titleProvided && is_null( $model ) ) {
201 $model = CONTENT_MODEL_WIKITEXT
;
202 $this->setWarning( "No 'title' or 'contentmodel' was given, assuming $model." );
206 $this->content
= ContentHandler
::makeContent( $text, $titleObj, $model, $format );
207 } catch ( MWContentSerializationException
$ex ) {
208 $this->dieUsage( $ex->getMessage(), 'parseerror' );
211 if ( $this->section
!== false ) {
212 if ( $this->section
=== 'new' ) {
213 // Insert the section title above the content.
214 if ( !is_null( $params['sectiontitle'] ) && $params['sectiontitle'] !== '' ) {
215 $this->content
= $this->content
->addSectionHeader( $params['sectiontitle'] );
218 $this->content
= $this->getSectionContent( $this->content
, $titleObj->getPrefixedText() );
222 if ( $params['pst'] ||
$params['onlypst'] ) {
223 $this->pstContent
= $this->content
->preSaveTransform( $titleObj, $this->getUser(), $popts );
225 if ( $params['onlypst'] ) {
226 // Build a result and bail out
227 $result_array = array();
228 $result_array['text'] = array();
229 ApiResult
::setContentValue( $result_array['text'], 'text', $this->pstContent
->serialize( $format ) );
230 if ( isset( $prop['wikitext'] ) ) {
231 $result_array['wikitext'] = array();
232 ApiResult
::setContentValue( $result_array['wikitext'], 'wikitext', $this->content
->serialize( $format ) );
234 if ( !is_null( $params['summary'] ) ||
235 ( !is_null( $params['sectiontitle'] ) && $this->section
=== 'new' )
237 $result_array['parsedsummary'] = array();
238 ApiResult
::setContentValue(
239 $result_array['parsedsummary'],
241 $this->formatSummary( $titleObj, $params )
245 $result->addValue( null, $this->getModuleName(), $result_array );
250 // Not cached (save or load)
251 if ( $params['pst'] ) {
252 $p_result = $this->pstContent
->getParserOutput( $titleObj, null, $popts );
254 $p_result = $this->content
->getParserOutput( $titleObj, null, $popts );
258 $result_array = array();
260 $result_array['title'] = $titleObj->getPrefixedText();
262 if ( !is_null( $oldid ) ) {
263 $result_array['revid'] = intval( $oldid );
266 if ( $params['redirects'] && !is_null( $redirValues ) ) {
267 $result_array['redirects'] = $redirValues;
270 if ( $params['disabletoc'] ) {
271 $p_result->setTOCEnabled( false );
274 if ( isset( $prop['text'] ) ) {
275 $result_array['text'] = array();
276 ApiResult
::setContentValue( $result_array['text'], 'text', $p_result->getText() );
279 if ( !is_null( $params['summary'] ) ||
280 ( !is_null( $params['sectiontitle'] ) && $this->section
=== 'new' )
282 $result_array['parsedsummary'] = array();
283 ApiResult
::setContentValue(
284 $result_array['parsedsummary'],
286 $this->formatSummary( $titleObj, $params )
290 if ( isset( $prop['langlinks'] ) ) {
291 $langlinks = $p_result->getLanguageLinks();
293 if ( $params['effectivelanglinks'] ) {
294 // Link flags are ignored for now, but may in the future be
295 // included in the result.
296 $linkFlags = array();
297 Hooks
::run( 'LanguageLinks', array( $titleObj, &$langlinks, &$linkFlags ) );
303 if ( isset( $prop['langlinks'] ) ) {
304 $result_array['langlinks'] = $this->formatLangLinks( $langlinks );
306 if ( isset( $prop['categories'] ) ) {
307 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
309 if ( isset( $prop['categorieshtml'] ) ) {
310 $categoriesHtml = $this->categoriesHtml( $p_result->getCategories() );
311 $result_array['categorieshtml'] = array();
312 ApiResult
::setContentValue( $result_array['categorieshtml'], 'categorieshtml', $categoriesHtml );
314 if ( isset( $prop['links'] ) ) {
315 $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
317 if ( isset( $prop['templates'] ) ) {
318 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
320 if ( isset( $prop['images'] ) ) {
321 $result_array['images'] = array_keys( $p_result->getImages() );
323 if ( isset( $prop['externallinks'] ) ) {
324 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
326 if ( isset( $prop['sections'] ) ) {
327 $result_array['sections'] = $p_result->getSections();
330 if ( isset( $prop['displaytitle'] ) ) {
331 $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
332 $p_result->getDisplayTitle() :
333 $titleObj->getPrefixedText();
336 if ( isset( $prop['headitems'] ) ||
isset( $prop['headhtml'] ) ) {
337 $context = $this->getContext();
338 $context->setTitle( $titleObj );
339 $context->getOutput()->addParserOutputMetadata( $p_result );
341 if ( isset( $prop['headitems'] ) ) {
342 $headItems = $this->formatHeadItems( $p_result->getHeadItems() );
344 $css = $this->formatCss( $context->getOutput()->buildCssLinksArray() );
346 $scripts = array( $context->getOutput()->getHeadScripts() );
348 $result_array['headitems'] = array_merge( $headItems, $css, $scripts );
351 if ( isset( $prop['headhtml'] ) ) {
352 $result_array['headhtml'] = array();
353 ApiResult
::setContentValue(
354 $result_array['headhtml'],
356 $context->getOutput()->headElement( $context->getSkin() )
361 if ( isset( $prop['modules'] ) ) {
362 $result_array['modules'] = array_values( array_unique( $p_result->getModules() ) );
363 $result_array['modulescripts'] = array_values( array_unique( $p_result->getModuleScripts() ) );
364 $result_array['modulestyles'] = array_values( array_unique( $p_result->getModuleStyles() ) );
365 $result_array['modulemessages'] = array_values( array_unique( $p_result->getModuleMessages() ) );
368 if ( isset( $prop['indicators'] ) ) {
369 foreach ( $p_result->getIndicators() as $name => $content ) {
370 $indicator = array( 'name' => $name );
371 ApiResult
::setContentValue( $indicator, 'content', $content );
372 $result_array['indicators'][] = $indicator;
376 if ( isset( $prop['iwlinks'] ) ) {
377 $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
380 if ( isset( $prop['wikitext'] ) ) {
381 $result_array['wikitext'] = array();
382 ApiResult
::setContentValue( $result_array['wikitext'], 'wikitext', $this->content
->serialize( $format ) );
383 if ( !is_null( $this->pstContent
) ) {
384 $result_array['psttext'] = array();
385 ApiResult
::setContentValue( $result_array['psttext'], 'psttext', $this->pstContent
->serialize( $format ) );
388 if ( isset( $prop['properties'] ) ) {
389 $result_array['properties'] = $this->formatProperties( $p_result->getProperties() );
392 if ( isset( $prop['limitreportdata'] ) ) {
393 $result_array['limitreportdata'] =
394 $this->formatLimitReportData( $p_result->getLimitReportData() );
396 if ( isset( $prop['limitreporthtml'] ) ) {
397 $limitreportHtml = EditPage
::getPreviewLimitReport( $p_result );
398 $result_array['limitreporthtml'] = array();
399 ApiResult
::setContentValue( $result_array['limitreporthtml'], 'limitreporthtml', $limitreportHtml );
402 if ( $params['generatexml'] ) {
403 if ( $this->content
->getModel() != CONTENT_MODEL_WIKITEXT
) {
404 $this->dieUsage( "generatexml is only supported for wikitext content", "notwikitext" );
407 $wgParser->startExternalParse( $titleObj, $popts, Parser
::OT_PREPROCESS
);
408 $dom = $wgParser->preprocessToDom( $this->content
->getNativeData() );
409 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
410 $xml = $dom->saveXML();
412 $xml = $dom->__toString();
414 $result_array['parsetree'] = array();
415 ApiResult
::setContentValue( $result_array['parsetree'], 'parsetree', $xml );
418 $result_mapping = array(
421 'categories' => 'cl',
425 'externallinks' => 'el',
430 'indicators' => 'ind',
431 'modulescripts' => 'm',
432 'modulestyles' => 'm',
433 'modulemessages' => 'm',
434 'properties' => 'pp',
435 'limitreportdata' => 'lr',
437 $this->setIndexedTagNames( $result_array, $result_mapping );
438 $result->addValue( null, $this->getModuleName(), $result_array );
442 * Constructs a ParserOptions object
444 * @param WikiPage $pageObj
445 * @param array $params
447 * @return ParserOptions
449 protected function makeParserOptions( WikiPage
$pageObj, array $params ) {
451 $popts = $pageObj->makeParserOptions( $this->getContext() );
452 $popts->enableLimitReport( !$params['disablepp'] );
453 $popts->setIsPreview( $params['preview'] ||
$params['sectionpreview'] );
454 $popts->setIsSectionPreview( $params['sectionpreview'] );
455 $popts->setEditSection( !$params['disableeditsection'] );
461 * @param WikiPage $page
462 * @param ParserOptions $popts
464 * @param bool $getWikitext
465 * @return ParserOutput
467 private function getParsedContent( WikiPage
$page, $popts, $pageId = null, $getWikitext = false ) {
468 $this->content
= $page->getContent( Revision
::RAW
); //XXX: really raw?
470 if ( $this->section
!== false && $this->content
!== null ) {
471 $this->content
= $this->getSectionContent(
473 !is_null( $pageId ) ?
'page id ' . $pageId : $page->getTitle()->getPrefixedText()
476 // Not cached (save or load)
477 return $this->content
->getParserOutput( $page->getTitle(), null, $popts );
480 // Try the parser cache first
481 // getParserOutput will save to Parser cache if able
482 $pout = $page->getParserOutput( $popts );
484 $this->dieUsage( "There is no revision ID {$page->getLatest()}", 'missingrev' );
486 if ( $getWikitext ) {
487 $this->content
= $page->getContent( Revision
::RAW
);
494 * @param Content $content
495 * @param string $what Identifies the content in error messages, e.g. page title.
496 * @return Content|bool
498 private function getSectionContent( Content
$content, $what ) {
499 // Not cached (save or load)
500 $section = $content->getSection( $this->section
);
501 if ( $section === false ) {
502 $this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' );
504 if ( $section === null ) {
505 $this->dieUsage( "Sections are not supported by " . $what, 'nosuchsection' );
513 * This mimicks the behavior of EditPage in formatting a summary
515 * @param Title $title of the page being parsed
516 * @param Array $params the API parameters of the request
517 * @return Content|bool
519 private function formatSummary( $title, $params ) {
521 $summary = !is_null( $params['summary'] ) ?
$params['summary'] : '';
522 $sectionTitle = !is_null( $params['sectiontitle'] ) ?
$params['sectiontitle'] : '';
524 if ( $this->section
=== 'new' && ( $sectionTitle === '' ||
$summary === '' ) ) {
525 if( $sectionTitle !== '' ) {
526 $summary = $params['sectiontitle'];
528 if ( $summary !== '' ) {
529 $summary = wfMessage( 'newsectionsummary' )->rawParams( $wgParser->stripSectionName( $summary ) )
530 ->inContentLanguage()->text();
533 return Linker
::formatComment( $summary, $title, $this->section
=== 'new' );
536 private function formatLangLinks( $links ) {
538 foreach ( $links as $link ) {
540 $bits = explode( ':', $link, 2 );
541 $title = Title
::newFromText( $link );
543 $entry['lang'] = $bits[0];
545 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
);
546 // localised language name in 'uselang' language
547 $entry['langname'] = Language
::fetchLanguageName(
548 $title->getInterwiki(),
549 $this->getLanguage()->getCode()
552 // native language name
553 $entry['autonym'] = Language
::fetchLanguageName( $title->getInterwiki() );
555 ApiResult
::setContentValue( $entry, 'title', $bits[1] );
562 private function formatCategoryLinks( $links ) {
569 // Fetch hiddencat property
571 $lb->setArray( array( NS_CATEGORY
=> $links ) );
572 $db = $this->getDB();
573 $res = $db->select( array( 'page', 'page_props' ),
574 array( 'page_title', 'pp_propname' ),
575 $lb->constructSet( 'page', $db ),
578 array( 'page_props' => array(
579 'LEFT JOIN', array( 'pp_propname' => 'hiddencat', 'pp_page = page_id' )
582 $hiddencats = array();
583 foreach ( $res as $row ) {
584 $hiddencats[$row->page_title
] = isset( $row->pp_propname
);
587 foreach ( $links as $link => $sortkey ) {
589 $entry['sortkey'] = $sortkey;
590 ApiResult
::setContentValue( $entry, 'category', $link );
591 if ( !isset( $hiddencats[$link] ) ) {
592 $entry['missing'] = '';
593 } elseif ( $hiddencats[$link] ) {
594 $entry['hidden'] = '';
602 private function categoriesHtml( $categories ) {
603 $context = $this->getContext();
604 $context->getOutput()->addCategoryLinks( $categories );
606 return $context->getSkin()->getCategories();
609 private function formatLinks( $links ) {
611 foreach ( $links as $ns => $nslinks ) {
612 foreach ( $nslinks as $title => $id ) {
615 ApiResult
::setContentValue( $entry, 'title', Title
::makeTitle( $ns, $title )->getFullText() );
617 $entry['exists'] = '';
626 private function formatIWLinks( $iw ) {
628 foreach ( $iw as $prefix => $titles ) {
629 foreach ( array_keys( $titles ) as $title ) {
631 $entry['prefix'] = $prefix;
633 $title = Title
::newFromText( "{$prefix}:{$title}" );
635 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
);
638 ApiResult
::setContentValue( $entry, 'title', $title->getFullText() );
646 private function formatHeadItems( $headItems ) {
648 foreach ( $headItems as $tag => $content ) {
650 $entry['tag'] = $tag;
651 ApiResult
::setContentValue( $entry, 'content', $content );
658 private function formatProperties( $properties ) {
660 foreach ( $properties as $name => $value ) {
662 $entry['name'] = $name;
663 ApiResult
::setContentValue( $entry, 'value', $value );
670 private function formatCss( $css ) {
672 foreach ( $css as $file => $link ) {
674 $entry['file'] = $file;
675 ApiResult
::setContentValue( $entry, 'link', $link );
682 private function formatLimitReportData( $limitReportData ) {
684 $apiResult = $this->getResult();
686 foreach ( $limitReportData as $name => $value ) {
688 $entry['name'] = $name;
689 if ( !is_array( $value ) ) {
690 $value = array( $value );
692 ApiResult
::setIndexedTagName( $value, 'param' );
693 ApiResult
::setIndexedTagNameOnSubarrays( $value, 'param' );
694 $entry = array_merge( $entry, $value );
701 private function setIndexedTagNames( &$array, $mapping ) {
702 foreach ( $mapping as $key => $name ) {
703 if ( isset( $array[$key] ) ) {
704 ApiResult
::setIndexedTagName( $array[$key], $name );
709 public function getAllowedParams() {
716 ApiBase
::PARAM_TYPE
=> 'integer',
718 'redirects' => false,
720 ApiBase
::PARAM_TYPE
=> 'integer',
723 ApiBase
::PARAM_DFLT
=> 'text|langlinks|categories|links|templates|' .
724 'images|externallinks|sections|revid|displaytitle|iwlinks|properties',
725 ApiBase
::PARAM_ISMULTI
=> true,
726 ApiBase
::PARAM_TYPE
=> array(
751 'effectivelanglinks' => false,
753 'sectiontitle' => array(
754 ApiBase
::PARAM_TYPE
=> 'string',
756 'disablepp' => false,
757 'disableeditsection' => false,
758 'generatexml' => array(
759 ApiBase
::PARAM_DFLT
=> false,
760 ApiBase
::PARAM_HELP_MSG
=> array(
761 'apihelp-parse-param-generatexml', CONTENT_MODEL_WIKITEXT
765 'sectionpreview' => false,
766 'disabletoc' => false,
767 'contentformat' => array(
768 ApiBase
::PARAM_TYPE
=> ContentHandler
::getAllContentFormats(),
770 'contentmodel' => array(
771 ApiBase
::PARAM_TYPE
=> ContentHandler
::getContentModels(),
776 protected function getExamplesMessages() {
778 'action=parse&page=Project:Sandbox'
779 => 'apihelp-parse-example-page',
780 'action=parse&text={{Project:Sandbox}}&contentmodel=wikitext'
781 => 'apihelp-parse-example-text',
782 'action=parse&text={{PAGENAME}}&title=Test'
783 => 'apihelp-parse-example-texttitle',
784 'action=parse&summary=Some+[[link]]&prop='
785 => 'apihelp-parse-example-summary',
789 public function getHelpUrls() {
790 return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#parse';