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 if ( $rev->isCurrent() ) {
111 // May get from/save to parser cache
112 $p_result = $this->getParsedContent( $pageObj, $popts,
113 $pageid, isset( $prop['wikitext'] ) );
114 } else { // This is an old revision, so get the text differently
115 $this->content
= $rev->getContent( Revision
::FOR_THIS_USER
, $this->getUser() );
117 if ( $this->section
!== false ) {
118 $this->content
= $this->getSectionContent( $this->content
, 'r' . $rev->getId() );
121 // Should we save old revision parses to the parser cache?
122 $p_result = $this->content
->getParserOutput( $titleObj, $rev->getId(), $popts );
124 } else { // Not $oldid, but $pageid or $page
125 if ( $params['redirects'] ) {
129 if ( !is_null( $pageid ) ) {
130 $reqParams['pageids'] = $pageid;
132 $reqParams['titles'] = $page;
134 $req = new FauxRequest( $reqParams );
135 $main = new ApiMain( $req );
136 $pageSet = new ApiPageSet( $main );
140 foreach ( $pageSet->getRedirectTitles() as $title ) {
141 $to = $title->getFullText();
143 $pageParams = array( 'title' => $to );
144 } elseif ( !is_null( $pageid ) ) {
145 $pageParams = array( 'pageid' => $pageid );
147 $pageParams = array( 'title' => $page );
150 $pageObj = $this->getTitleOrPageId( $pageParams, 'fromdb' );
151 $titleObj = $pageObj->getTitle();
152 if ( !$titleObj ||
!$titleObj->exists() ) {
153 $this->dieUsage( "The page you specified doesn't exist", 'missingtitle' );
155 $wgTitle = $titleObj;
157 if ( isset( $prop['revid'] ) ) {
158 $oldid = $pageObj->getLatest();
161 $popts = $this->makeParserOptions( $pageObj, $params );
163 // Potentially cached
164 $p_result = $this->getParsedContent( $pageObj, $popts, $pageid,
165 isset( $prop['wikitext'] ) );
167 } else { // Not $oldid, $pageid, $page. Hence based on $text
168 $titleObj = Title
::newFromText( $title );
169 if ( !$titleObj ||
$titleObj->isExternal() ) {
170 $this->dieUsageMsg( array( 'invalidtitle', $title ) );
172 $wgTitle = $titleObj;
173 if ( $titleObj->canExist() ) {
174 $pageObj = WikiPage
::factory( $titleObj );
176 // Do like MediaWiki::initializeArticle()
177 $article = Article
::newFromTitle( $titleObj, $this->getContext() );
178 $pageObj = $article->getPage();
181 $popts = $this->makeParserOptions( $pageObj, $params );
182 $textProvided = !is_null( $text );
184 if ( !$textProvided ) {
185 if ( $titleProvided && ( $prop ||
$params['generatexml'] ) ) {
187 "'title' used without 'text', and parsed page properties were requested " .
188 "(did you mean to use 'page' instead of 'title'?)"
191 // Prevent warning from ContentHandler::makeContent()
195 // If we are parsing text, do not use the content model of the default
196 // API title, but default to wikitext to keep BC.
197 if ( $textProvided && !$titleProvided && is_null( $model ) ) {
198 $model = CONTENT_MODEL_WIKITEXT
;
199 $this->setWarning( "No 'title' or 'contentmodel' was given, assuming $model." );
203 $this->content
= ContentHandler
::makeContent( $text, $titleObj, $model, $format );
204 } catch ( MWContentSerializationException
$ex ) {
205 $this->dieUsage( $ex->getMessage(), 'parseerror' );
208 if ( $this->section
!== false ) {
209 if ( $this->section
=== 'new' ) {
210 // Insert the section title above the content.
211 if ( !is_null( $params['sectiontitle'] ) && $params['sectiontitle'] !== '' ) {
212 $this->content
= $this->content
->addSectionHeader( $params['sectiontitle'] );
215 $this->content
= $this->getSectionContent( $this->content
, $titleObj->getPrefixedText() );
219 if ( $params['pst'] ||
$params['onlypst'] ) {
220 $this->pstContent
= $this->content
->preSaveTransform( $titleObj, $this->getUser(), $popts );
222 if ( $params['onlypst'] ) {
223 // Build a result and bail out
224 $result_array = array();
225 $result_array['text'] = array();
226 ApiResult
::setContentValue( $result_array['text'], 'text', $this->pstContent
->serialize( $format ) );
227 if ( isset( $prop['wikitext'] ) ) {
228 $result_array['wikitext'] = array();
229 ApiResult
::setContentValue( $result_array['wikitext'], 'wikitext', $this->content
->serialize( $format ) );
231 if ( !is_null( $params['summary'] ) ||
232 ( !is_null( $params['sectiontitle'] ) && $this->section
=== 'new' )
234 $result_array['parsedsummary'] = array();
235 ApiResult
::setContentValue(
236 $result_array['parsedsummary'],
238 $this->formatSummary( $titleObj, $params )
242 $result->addValue( null, $this->getModuleName(), $result_array );
247 // Not cached (save or load)
248 if ( $params['pst'] ) {
249 $p_result = $this->pstContent
->getParserOutput( $titleObj, null, $popts );
251 $p_result = $this->content
->getParserOutput( $titleObj, null, $popts );
255 $result_array = array();
257 $result_array['title'] = $titleObj->getPrefixedText();
259 if ( !is_null( $oldid ) ) {
260 $result_array['revid'] = intval( $oldid );
263 if ( $params['redirects'] && !is_null( $redirValues ) ) {
264 $result_array['redirects'] = $redirValues;
267 if ( $params['disabletoc'] ) {
268 $p_result->setTOCEnabled( false );
271 if ( isset( $prop['text'] ) ) {
272 $result_array['text'] = array();
273 ApiResult
::setContentValue( $result_array['text'], 'text', $p_result->getText() );
276 if ( !is_null( $params['summary'] ) ||
277 ( !is_null( $params['sectiontitle'] ) && $this->section
=== 'new' )
279 $result_array['parsedsummary'] = array();
280 ApiResult
::setContentValue(
281 $result_array['parsedsummary'],
283 $this->formatSummary( $titleObj, $params )
287 if ( isset( $prop['langlinks'] ) ) {
288 $langlinks = $p_result->getLanguageLinks();
290 if ( $params['effectivelanglinks'] ) {
291 // Link flags are ignored for now, but may in the future be
292 // included in the result.
293 $linkFlags = array();
294 Hooks
::run( 'LanguageLinks', array( $titleObj, &$langlinks, &$linkFlags ) );
300 if ( isset( $prop['langlinks'] ) ) {
301 $result_array['langlinks'] = $this->formatLangLinks( $langlinks );
303 if ( isset( $prop['categories'] ) ) {
304 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
306 if ( isset( $prop['categorieshtml'] ) ) {
307 $categoriesHtml = $this->categoriesHtml( $p_result->getCategories() );
308 $result_array['categorieshtml'] = array();
309 ApiResult
::setContentValue( $result_array['categorieshtml'], 'categorieshtml', $categoriesHtml );
311 if ( isset( $prop['links'] ) ) {
312 $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
314 if ( isset( $prop['templates'] ) ) {
315 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
317 if ( isset( $prop['images'] ) ) {
318 $result_array['images'] = array_keys( $p_result->getImages() );
320 if ( isset( $prop['externallinks'] ) ) {
321 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
323 if ( isset( $prop['sections'] ) ) {
324 $result_array['sections'] = $p_result->getSections();
327 if ( isset( $prop['displaytitle'] ) ) {
328 $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
329 $p_result->getDisplayTitle() :
330 $titleObj->getPrefixedText();
333 if ( isset( $prop['headitems'] ) ||
isset( $prop['headhtml'] ) ) {
334 $context = $this->getContext();
335 $context->setTitle( $titleObj );
336 $context->getOutput()->addParserOutputMetadata( $p_result );
338 if ( isset( $prop['headitems'] ) ) {
339 $headItems = $this->formatHeadItems( $p_result->getHeadItems() );
341 $css = $this->formatCss( $context->getOutput()->buildCssLinksArray() );
343 $scripts = array( $context->getOutput()->getHeadScripts() );
345 $result_array['headitems'] = array_merge( $headItems, $css, $scripts );
348 if ( isset( $prop['headhtml'] ) ) {
349 $result_array['headhtml'] = array();
350 ApiResult
::setContentValue(
351 $result_array['headhtml'],
353 $context->getOutput()->headElement( $context->getSkin() )
358 if ( isset( $prop['modules'] ) ) {
359 $result_array['modules'] = array_values( array_unique( $p_result->getModules() ) );
360 $result_array['modulescripts'] = array_values( array_unique( $p_result->getModuleScripts() ) );
361 $result_array['modulestyles'] = array_values( array_unique( $p_result->getModuleStyles() ) );
362 $result_array['modulemessages'] = array_values( array_unique( $p_result->getModuleMessages() ) );
365 if ( isset( $prop['indicators'] ) ) {
366 foreach ( $p_result->getIndicators() as $name => $content ) {
367 $indicator = array( 'name' => $name );
368 ApiResult
::setContentValue( $indicator, 'content', $content );
369 $result_array['indicators'][] = $indicator;
373 if ( isset( $prop['iwlinks'] ) ) {
374 $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
377 if ( isset( $prop['wikitext'] ) ) {
378 $result_array['wikitext'] = array();
379 ApiResult
::setContentValue( $result_array['wikitext'], 'wikitext', $this->content
->serialize( $format ) );
380 if ( !is_null( $this->pstContent
) ) {
381 $result_array['psttext'] = array();
382 ApiResult
::setContentValue( $result_array['psttext'], 'psttext', $this->pstContent
->serialize( $format ) );
385 if ( isset( $prop['properties'] ) ) {
386 $result_array['properties'] = $this->formatProperties( $p_result->getProperties() );
389 if ( isset( $prop['limitreportdata'] ) ) {
390 $result_array['limitreportdata'] =
391 $this->formatLimitReportData( $p_result->getLimitReportData() );
393 if ( isset( $prop['limitreporthtml'] ) ) {
394 $limitreportHtml = EditPage
::getPreviewLimitReport( $p_result );
395 $result_array['limitreporthtml'] = array();
396 ApiResult
::setContentValue( $result_array['limitreporthtml'], 'limitreporthtml', $limitreportHtml );
399 if ( $params['generatexml'] ) {
400 if ( $this->content
->getModel() != CONTENT_MODEL_WIKITEXT
) {
401 $this->dieUsage( "generatexml is only supported for wikitext content", "notwikitext" );
404 $wgParser->startExternalParse( $titleObj, $popts, Parser
::OT_PREPROCESS
);
405 $dom = $wgParser->preprocessToDom( $this->content
->getNativeData() );
406 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
407 $xml = $dom->saveXML();
409 $xml = $dom->__toString();
411 $result_array['parsetree'] = array();
412 ApiResult
::setContentValue( $result_array['parsetree'], 'parsetree', $xml );
415 $result_mapping = array(
418 'categories' => 'cl',
422 'externallinks' => 'el',
427 'indicators' => 'ind',
428 'modulescripts' => 'm',
429 'modulestyles' => 'm',
430 'modulemessages' => 'm',
431 'properties' => 'pp',
432 'limitreportdata' => 'lr',
434 $this->setIndexedTagNames( $result_array, $result_mapping );
435 $result->addValue( null, $this->getModuleName(), $result_array );
439 * Constructs a ParserOptions object
441 * @param WikiPage $pageObj
442 * @param array $params
444 * @return ParserOptions
446 protected function makeParserOptions( WikiPage
$pageObj, array $params ) {
448 $popts = $pageObj->makeParserOptions( $this->getContext() );
449 $popts->enableLimitReport( !$params['disablepp'] );
450 $popts->setIsPreview( $params['preview'] ||
$params['sectionpreview'] );
451 $popts->setIsSectionPreview( $params['sectionpreview'] );
452 $popts->setEditSection( !$params['disableeditsection'] );
458 * @param WikiPage $page
459 * @param ParserOptions $popts
461 * @param bool $getWikitext
462 * @return ParserOutput
464 private function getParsedContent( WikiPage
$page, $popts, $pageId = null, $getWikitext = false ) {
465 $this->content
= $page->getContent( Revision
::RAW
); //XXX: really raw?
467 if ( $this->section
!== false && $this->content
!== null ) {
468 $this->content
= $this->getSectionContent(
470 !is_null( $pageId ) ?
'page id ' . $pageId : $page->getTitle()->getPrefixedText()
473 // Not cached (save or load)
474 return $this->content
->getParserOutput( $page->getTitle(), null, $popts );
477 // Try the parser cache first
478 // getParserOutput will save to Parser cache if able
479 $pout = $page->getParserOutput( $popts );
481 $this->dieUsage( "There is no revision ID {$page->getLatest()}", 'missingrev' );
483 if ( $getWikitext ) {
484 $this->content
= $page->getContent( Revision
::RAW
);
491 * @param Content $content
492 * @param string $what Identifies the content in error messages, e.g. page title.
493 * @return Content|bool
495 private function getSectionContent( Content
$content, $what ) {
496 // Not cached (save or load)
497 $section = $content->getSection( $this->section
);
498 if ( $section === false ) {
499 $this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' );
501 if ( $section === null ) {
502 $this->dieUsage( "Sections are not supported by " . $what, 'nosuchsection' );
510 * This mimicks the behavior of EditPage in formatting a summary
512 * @param Title $title of the page being parsed
513 * @param Array $params the API parameters of the request
514 * @return Content|bool
516 private function formatSummary( $title, $params ) {
518 $summary = !is_null( $params['summary'] ) ?
$params['summary'] : '';
519 $sectionTitle = !is_null( $params['sectiontitle'] ) ?
$params['sectiontitle'] : '';
521 if ( $this->section
=== 'new' && ( $sectionTitle === '' ||
$summary === '' ) ) {
522 if( $sectionTitle !== '' ) {
523 $summary = $params['sectiontitle'];
525 if ( $summary !== '' ) {
526 $summary = wfMessage( 'newsectionsummary' )->rawParams( $wgParser->stripSectionName( $summary ) )
527 ->inContentLanguage()->text();
530 return Linker
::formatComment( $summary, $title, $this->section
=== 'new' );
533 private function formatLangLinks( $links ) {
535 foreach ( $links as $link ) {
537 $bits = explode( ':', $link, 2 );
538 $title = Title
::newFromText( $link );
540 $entry['lang'] = $bits[0];
542 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
);
543 // localised language name in 'uselang' language
544 $entry['langname'] = Language
::fetchLanguageName(
545 $title->getInterwiki(),
546 $this->getLanguage()->getCode()
549 // native language name
550 $entry['autonym'] = Language
::fetchLanguageName( $title->getInterwiki() );
552 ApiResult
::setContentValue( $entry, 'title', $bits[1] );
559 private function formatCategoryLinks( $links ) {
566 // Fetch hiddencat property
568 $lb->setArray( array( NS_CATEGORY
=> $links ) );
569 $db = $this->getDB();
570 $res = $db->select( array( 'page', 'page_props' ),
571 array( 'page_title', 'pp_propname' ),
572 $lb->constructSet( 'page', $db ),
575 array( 'page_props' => array(
576 'LEFT JOIN', array( 'pp_propname' => 'hiddencat', 'pp_page = page_id' )
579 $hiddencats = array();
580 foreach ( $res as $row ) {
581 $hiddencats[$row->page_title
] = isset( $row->pp_propname
);
584 foreach ( $links as $link => $sortkey ) {
586 $entry['sortkey'] = $sortkey;
587 ApiResult
::setContentValue( $entry, 'category', $link );
588 if ( !isset( $hiddencats[$link] ) ) {
589 $entry['missing'] = '';
590 } elseif ( $hiddencats[$link] ) {
591 $entry['hidden'] = '';
599 private function categoriesHtml( $categories ) {
600 $context = $this->getContext();
601 $context->getOutput()->addCategoryLinks( $categories );
603 return $context->getSkin()->getCategories();
606 private function formatLinks( $links ) {
608 foreach ( $links as $ns => $nslinks ) {
609 foreach ( $nslinks as $title => $id ) {
612 ApiResult
::setContentValue( $entry, 'title', Title
::makeTitle( $ns, $title )->getFullText() );
614 $entry['exists'] = '';
623 private function formatIWLinks( $iw ) {
625 foreach ( $iw as $prefix => $titles ) {
626 foreach ( array_keys( $titles ) as $title ) {
628 $entry['prefix'] = $prefix;
630 $title = Title
::newFromText( "{$prefix}:{$title}" );
632 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
);
635 ApiResult
::setContentValue( $entry, 'title', $title->getFullText() );
643 private function formatHeadItems( $headItems ) {
645 foreach ( $headItems as $tag => $content ) {
647 $entry['tag'] = $tag;
648 ApiResult
::setContentValue( $entry, 'content', $content );
655 private function formatProperties( $properties ) {
657 foreach ( $properties as $name => $value ) {
659 $entry['name'] = $name;
660 ApiResult
::setContentValue( $entry, 'value', $value );
667 private function formatCss( $css ) {
669 foreach ( $css as $file => $link ) {
671 $entry['file'] = $file;
672 ApiResult
::setContentValue( $entry, 'link', $link );
679 private function formatLimitReportData( $limitReportData ) {
681 $apiResult = $this->getResult();
683 foreach ( $limitReportData as $name => $value ) {
685 $entry['name'] = $name;
686 if ( !is_array( $value ) ) {
687 $value = array( $value );
689 ApiResult
::setIndexedTagName( $value, 'param' );
690 ApiResult
::setIndexedTagNameOnSubarrays( $value, 'param' );
691 $entry = array_merge( $entry, $value );
698 private function setIndexedTagNames( &$array, $mapping ) {
699 foreach ( $mapping as $key => $name ) {
700 if ( isset( $array[$key] ) ) {
701 ApiResult
::setIndexedTagName( $array[$key], $name );
706 public function getAllowedParams() {
713 ApiBase
::PARAM_TYPE
=> 'integer',
715 'redirects' => false,
717 ApiBase
::PARAM_TYPE
=> 'integer',
720 ApiBase
::PARAM_DFLT
=> 'text|langlinks|categories|links|templates|' .
721 'images|externallinks|sections|revid|displaytitle|iwlinks|properties',
722 ApiBase
::PARAM_ISMULTI
=> true,
723 ApiBase
::PARAM_TYPE
=> array(
748 'effectivelanglinks' => false,
750 'sectiontitle' => array(
751 ApiBase
::PARAM_TYPE
=> 'string',
753 'disablepp' => false,
754 'disableeditsection' => false,
755 'generatexml' => array(
756 ApiBase
::PARAM_DFLT
=> false,
757 ApiBase
::PARAM_HELP_MSG
=> array(
758 'apihelp-parse-param-generatexml', CONTENT_MODEL_WIKITEXT
762 'sectionpreview' => false,
763 'disabletoc' => false,
764 'contentformat' => array(
765 ApiBase
::PARAM_TYPE
=> ContentHandler
::getAllContentFormats(),
767 'contentmodel' => array(
768 ApiBase
::PARAM_TYPE
=> ContentHandler
::getContentModels(),
773 protected function getExamplesMessages() {
775 'action=parse&page=Project:Sandbox'
776 => 'apihelp-parse-example-page',
777 'action=parse&text={{Project:Sandbox}}&contentmodel=wikitext'
778 => 'apihelp-parse-example-text',
779 'action=parse&text={{PAGENAME}}&title=Test'
780 => 'apihelp-parse-example-texttitle',
781 'action=parse&summary=Some+[[link]]&prop='
782 => 'apihelp-parse-example-summary',
786 public function getHelpUrls() {
787 return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#parse';