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 );
141 $redirValues = $pageSet->getRedirectTitlesAsResult( $this->getResult() );
144 foreach ( $pageSet->getRedirectTitles() as $title ) {
145 $to = $title->getFullText();
147 $pageParams = array( 'title' => $to );
148 } elseif ( !is_null( $pageid ) ) {
149 $pageParams = array( 'pageid' => $pageid );
151 $pageParams = array( 'title' => $page );
154 $pageObj = $this->getTitleOrPageId( $pageParams, 'fromdb' );
155 $titleObj = $pageObj->getTitle();
156 if ( !$titleObj ||
!$titleObj->exists() ) {
157 $this->dieUsage( "The page you specified doesn't exist", 'missingtitle' );
159 $wgTitle = $titleObj;
161 if ( isset( $prop['revid'] ) ) {
162 $oldid = $pageObj->getLatest();
165 $popts = $this->makeParserOptions( $pageObj, $params );
167 // Potentially cached
168 $p_result = $this->getParsedContent( $pageObj, $popts, $pageid,
169 isset( $prop['wikitext'] ) );
171 } else { // Not $oldid, $pageid, $page. Hence based on $text
172 $titleObj = Title
::newFromText( $title );
173 if ( !$titleObj ||
$titleObj->isExternal() ) {
174 $this->dieUsageMsg( array( 'invalidtitle', $title ) );
176 $wgTitle = $titleObj;
177 if ( $titleObj->canExist() ) {
178 $pageObj = WikiPage
::factory( $titleObj );
180 // Do like MediaWiki::initializeArticle()
181 $article = Article
::newFromTitle( $titleObj, $this->getContext() );
182 $pageObj = $article->getPage();
185 $popts = $this->makeParserOptions( $pageObj, $params );
186 $textProvided = !is_null( $text );
188 if ( !$textProvided ) {
189 if ( $titleProvided && ( $prop ||
$params['generatexml'] ) ) {
191 "'title' used without 'text', and parsed page properties were requested " .
192 "(did you mean to use 'page' instead of 'title'?)"
195 // Prevent warning from ContentHandler::makeContent()
199 // If we are parsing text, do not use the content model of the default
200 // API title, but default to wikitext to keep BC.
201 if ( $textProvided && !$titleProvided && is_null( $model ) ) {
202 $model = CONTENT_MODEL_WIKITEXT
;
203 $this->setWarning( "No 'title' or 'contentmodel' was given, assuming $model." );
207 $this->content
= ContentHandler
::makeContent( $text, $titleObj, $model, $format );
208 } catch ( MWContentSerializationException
$ex ) {
209 $this->dieUsage( $ex->getMessage(), 'parseerror' );
212 if ( $this->section
!== false ) {
213 if ( $this->section
=== 'new' ) {
214 // Insert the section title above the content.
215 if ( !is_null( $params['sectiontitle'] ) && $params['sectiontitle'] !== '' ) {
216 $this->content
= $this->content
->addSectionHeader( $params['sectiontitle'] );
219 $this->content
= $this->getSectionContent( $this->content
, $titleObj->getPrefixedText() );
223 if ( $params['pst'] ||
$params['onlypst'] ) {
224 $this->pstContent
= $this->content
->preSaveTransform( $titleObj, $this->getUser(), $popts );
226 if ( $params['onlypst'] ) {
227 // Build a result and bail out
228 $result_array = array();
229 $result_array['text'] = $this->pstContent
->serialize( $format );
230 $result_array[ApiResult
::META_BC_SUBELEMENTS
][] = 'text';
231 if ( isset( $prop['wikitext'] ) ) {
232 $result_array['wikitext'] = $this->content
->serialize( $format );
233 $result_array[ApiResult
::META_BC_SUBELEMENTS
][] = 'wikitext';
235 if ( !is_null( $params['summary'] ) ||
236 ( !is_null( $params['sectiontitle'] ) && $this->section
=== 'new' )
238 $result_array['parsedsummary'] = $this->formatSummary( $titleObj, $params );
239 $result_array[ApiResult
::META_BC_SUBELEMENTS
][] = 'parsedsummary';
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'] = $p_result->getText();
273 $result_array[ApiResult
::META_BC_SUBELEMENTS
][] = 'text';
276 if ( !is_null( $params['summary'] ) ||
277 ( !is_null( $params['sectiontitle'] ) && $this->section
=== 'new' )
279 $result_array['parsedsummary'] = $this->formatSummary( $titleObj, $params );
280 $result_array[ApiResult
::META_BC_SUBELEMENTS
][] = 'parsedsummary';
283 if ( isset( $prop['langlinks'] ) ) {
284 $langlinks = $p_result->getLanguageLinks();
286 if ( $params['effectivelanglinks'] ) {
287 // Link flags are ignored for now, but may in the future be
288 // included in the result.
289 $linkFlags = array();
290 Hooks
::run( 'LanguageLinks', array( $titleObj, &$langlinks, &$linkFlags ) );
296 if ( isset( $prop['langlinks'] ) ) {
297 $result_array['langlinks'] = $this->formatLangLinks( $langlinks );
299 if ( isset( $prop['categories'] ) ) {
300 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
302 if ( isset( $prop['categorieshtml'] ) ) {
303 $result_array['categorieshtml'] = $this->categoriesHtml( $p_result->getCategories() );
304 $result_array[ApiResult
::META_BC_SUBELEMENTS
][] = 'categorieshtml';
306 if ( isset( $prop['links'] ) ) {
307 $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
309 if ( isset( $prop['templates'] ) ) {
310 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
312 if ( isset( $prop['images'] ) ) {
313 $result_array['images'] = array_keys( $p_result->getImages() );
315 if ( isset( $prop['externallinks'] ) ) {
316 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
318 if ( isset( $prop['sections'] ) ) {
319 $result_array['sections'] = $p_result->getSections();
322 if ( isset( $prop['displaytitle'] ) ) {
323 $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
324 $p_result->getDisplayTitle() :
325 $titleObj->getPrefixedText();
328 if ( isset( $prop['headitems'] ) ||
isset( $prop['headhtml'] ) ) {
329 $context = $this->getContext();
330 $context->setTitle( $titleObj );
331 $context->getOutput()->addParserOutputMetadata( $p_result );
333 if ( isset( $prop['headitems'] ) ) {
334 $headItems = $this->formatHeadItems( $p_result->getHeadItems() );
336 $css = $this->formatCss( $context->getOutput()->buildCssLinksArray() );
338 $scripts = array( $context->getOutput()->getHeadScripts() );
340 $result_array['headitems'] = array_merge( $headItems, $css, $scripts );
343 if ( isset( $prop['headhtml'] ) ) {
344 $result_array['headhtml'] = $context->getOutput()->headElement( $context->getSkin() );
345 $result_array[ApiResult
::META_BC_SUBELEMENTS
][] = 'headhtml';
349 if ( isset( $prop['modules'] ) ) {
350 $result_array['modules'] = array_values( array_unique( $p_result->getModules() ) );
351 $result_array['modulescripts'] = array_values( array_unique( $p_result->getModuleScripts() ) );
352 $result_array['modulestyles'] = array_values( array_unique( $p_result->getModuleStyles() ) );
353 $result_array['modulemessages'] = array_values( array_unique( $p_result->getModuleMessages() ) );
356 if ( isset( $prop['indicators'] ) ) {
357 $result_array['indicators'] = (array)$p_result->getIndicators();
358 ApiResult
::setArrayType( $result_array['indicators'], 'BCkvp', 'name' );
361 if ( isset( $prop['iwlinks'] ) ) {
362 $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
365 if ( isset( $prop['wikitext'] ) ) {
366 $result_array['wikitext'] = $this->content
->serialize( $format );
367 $result_array[ApiResult
::META_BC_SUBELEMENTS
][] = 'wikitext';
368 if ( !is_null( $this->pstContent
) ) {
369 $result_array['psttext'] = $this->pstContent
->serialize( $format );
370 $result_array[ApiResult
::META_BC_SUBELEMENTS
][] = 'psttext';
373 if ( isset( $prop['properties'] ) ) {
374 $result_array['properties'] = (array)$p_result->getProperties();
375 ApiResult
::setArrayType( $result_array['properties'], 'BCkvp', 'name' );
378 if ( isset( $prop['limitreportdata'] ) ) {
379 $result_array['limitreportdata'] =
380 $this->formatLimitReportData( $p_result->getLimitReportData() );
382 if ( isset( $prop['limitreporthtml'] ) ) {
383 $result_array['limitreporthtml'] = EditPage
::getPreviewLimitReport( $p_result );
384 $result_array[ApiResult
::META_BC_SUBELEMENTS
][] = 'limitreporthtml';
387 if ( $params['generatexml'] ) {
388 if ( $this->content
->getModel() != CONTENT_MODEL_WIKITEXT
) {
389 $this->dieUsage( "generatexml is only supported for wikitext content", "notwikitext" );
392 $wgParser->startExternalParse( $titleObj, $popts, Parser
::OT_PREPROCESS
);
393 $dom = $wgParser->preprocessToDom( $this->content
->getNativeData() );
394 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
395 $xml = $dom->saveXML();
397 $xml = $dom->__toString();
399 $result_array['parsetree'] = $xml;
400 $result_array[ApiResult
::META_BC_SUBELEMENTS
][] = 'parsetree';
403 $result_mapping = array(
406 'categories' => 'cl',
410 'externallinks' => 'el',
415 'indicators' => 'ind',
416 'modulescripts' => 'm',
417 'modulestyles' => 'm',
418 'modulemessages' => 'm',
419 'properties' => 'pp',
420 'limitreportdata' => 'lr',
422 $this->setIndexedTagNames( $result_array, $result_mapping );
423 $result->addValue( null, $this->getModuleName(), $result_array );
427 * Constructs a ParserOptions object
429 * @param WikiPage $pageObj
430 * @param array $params
432 * @return ParserOptions
434 protected function makeParserOptions( WikiPage
$pageObj, array $params ) {
436 $popts = $pageObj->makeParserOptions( $this->getContext() );
437 $popts->enableLimitReport( !$params['disablepp'] );
438 $popts->setIsPreview( $params['preview'] ||
$params['sectionpreview'] );
439 $popts->setIsSectionPreview( $params['sectionpreview'] );
440 $popts->setEditSection( !$params['disableeditsection'] );
446 * @param WikiPage $page
447 * @param ParserOptions $popts
449 * @param bool $getWikitext
450 * @return ParserOutput
452 private function getParsedContent( WikiPage
$page, $popts, $pageId = null, $getWikitext = false ) {
453 $this->content
= $page->getContent( Revision
::RAW
); //XXX: really raw?
455 if ( $this->section
!== false && $this->content
!== null ) {
456 $this->content
= $this->getSectionContent(
458 !is_null( $pageId ) ?
'page id ' . $pageId : $page->getTitle()->getPrefixedText()
461 // Not cached (save or load)
462 return $this->content
->getParserOutput( $page->getTitle(), null, $popts );
465 // Try the parser cache first
466 // getParserOutput will save to Parser cache if able
467 $pout = $page->getParserOutput( $popts );
469 $this->dieUsage( "There is no revision ID {$page->getLatest()}", 'missingrev' );
471 if ( $getWikitext ) {
472 $this->content
= $page->getContent( Revision
::RAW
);
479 * @param Content $content
480 * @param string $what Identifies the content in error messages, e.g. page title.
481 * @return Content|bool
483 private function getSectionContent( Content
$content, $what ) {
484 // Not cached (save or load)
485 $section = $content->getSection( $this->section
);
486 if ( $section === false ) {
487 $this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' );
489 if ( $section === null ) {
490 $this->dieUsage( "Sections are not supported by " . $what, 'nosuchsection' );
498 * This mimicks the behavior of EditPage in formatting a summary
500 * @param Title $title of the page being parsed
501 * @param Array $params the API parameters of the request
502 * @return Content|bool
504 private function formatSummary( $title, $params ) {
506 $summary = !is_null( $params['summary'] ) ?
$params['summary'] : '';
507 $sectionTitle = !is_null( $params['sectiontitle'] ) ?
$params['sectiontitle'] : '';
509 if ( $this->section
=== 'new' && ( $sectionTitle === '' ||
$summary === '' ) ) {
510 if( $sectionTitle !== '' ) {
511 $summary = $params['sectiontitle'];
513 if ( $summary !== '' ) {
514 $summary = wfMessage( 'newsectionsummary' )->rawParams( $wgParser->stripSectionName( $summary ) )
515 ->inContentLanguage()->text();
518 return Linker
::formatComment( $summary, $title, $this->section
=== 'new' );
521 private function formatLangLinks( $links ) {
523 foreach ( $links as $link ) {
525 $bits = explode( ':', $link, 2 );
526 $title = Title
::newFromText( $link );
528 $entry['lang'] = $bits[0];
530 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
);
531 // localised language name in 'uselang' language
532 $entry['langname'] = Language
::fetchLanguageName(
533 $title->getInterwiki(),
534 $this->getLanguage()->getCode()
537 // native language name
538 $entry['autonym'] = Language
::fetchLanguageName( $title->getInterwiki() );
540 ApiResult
::setContentValue( $entry, 'title', $bits[1] );
547 private function formatCategoryLinks( $links ) {
554 // Fetch hiddencat property
556 $lb->setArray( array( NS_CATEGORY
=> $links ) );
557 $db = $this->getDB();
558 $res = $db->select( array( 'page', 'page_props' ),
559 array( 'page_title', 'pp_propname' ),
560 $lb->constructSet( 'page', $db ),
563 array( 'page_props' => array(
564 'LEFT JOIN', array( 'pp_propname' => 'hiddencat', 'pp_page = page_id' )
567 $hiddencats = array();
568 foreach ( $res as $row ) {
569 $hiddencats[$row->page_title
] = isset( $row->pp_propname
);
572 foreach ( $links as $link => $sortkey ) {
574 $entry['sortkey'] = $sortkey;
575 ApiResult
::setContentValue( $entry, 'category', $link );
576 if ( !isset( $hiddencats[$link] ) ) {
577 $entry['missing'] = true;
578 } elseif ( $hiddencats[$link] ) {
579 $entry['hidden'] = true;
587 private function categoriesHtml( $categories ) {
588 $context = $this->getContext();
589 $context->getOutput()->addCategoryLinks( $categories );
591 return $context->getSkin()->getCategories();
594 private function formatLinks( $links ) {
596 foreach ( $links as $ns => $nslinks ) {
597 foreach ( $nslinks as $title => $id ) {
600 ApiResult
::setContentValue( $entry, 'title', Title
::makeTitle( $ns, $title )->getFullText() );
601 $entry['exists'] = $id != 0;
609 private function formatIWLinks( $iw ) {
611 foreach ( $iw as $prefix => $titles ) {
612 foreach ( array_keys( $titles ) as $title ) {
614 $entry['prefix'] = $prefix;
616 $title = Title
::newFromText( "{$prefix}:{$title}" );
618 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
);
621 ApiResult
::setContentValue( $entry, 'title', $title->getFullText() );
629 private function formatHeadItems( $headItems ) {
631 foreach ( $headItems as $tag => $content ) {
633 $entry['tag'] = $tag;
634 ApiResult
::setContentValue( $entry, 'content', $content );
641 private function formatCss( $css ) {
643 foreach ( $css as $file => $link ) {
645 $entry['file'] = $file;
646 ApiResult
::setContentValue( $entry, 'link', $link );
653 private function formatLimitReportData( $limitReportData ) {
655 $apiResult = $this->getResult();
657 foreach ( $limitReportData as $name => $value ) {
659 $entry['name'] = $name;
660 if ( !is_array( $value ) ) {
661 $value = array( $value );
663 ApiResult
::setIndexedTagNameRecursive( $value, 'param' );
664 $entry = array_merge( $entry, $value );
671 private function setIndexedTagNames( &$array, $mapping ) {
672 foreach ( $mapping as $key => $name ) {
673 if ( isset( $array[$key] ) ) {
674 ApiResult
::setIndexedTagName( $array[$key], $name );
679 public function getAllowedParams() {
686 ApiBase
::PARAM_TYPE
=> 'integer',
688 'redirects' => false,
690 ApiBase
::PARAM_TYPE
=> 'integer',
693 ApiBase
::PARAM_DFLT
=> 'text|langlinks|categories|links|templates|' .
694 'images|externallinks|sections|revid|displaytitle|iwlinks|properties',
695 ApiBase
::PARAM_ISMULTI
=> true,
696 ApiBase
::PARAM_TYPE
=> array(
721 'effectivelanglinks' => false,
723 'sectiontitle' => array(
724 ApiBase
::PARAM_TYPE
=> 'string',
726 'disablepp' => false,
727 'disableeditsection' => false,
728 'generatexml' => array(
729 ApiBase
::PARAM_DFLT
=> false,
730 ApiBase
::PARAM_HELP_MSG
=> array(
731 'apihelp-parse-param-generatexml', CONTENT_MODEL_WIKITEXT
735 'sectionpreview' => false,
736 'disabletoc' => false,
737 'contentformat' => array(
738 ApiBase
::PARAM_TYPE
=> ContentHandler
::getAllContentFormats(),
740 'contentmodel' => array(
741 ApiBase
::PARAM_TYPE
=> ContentHandler
::getContentModels(),
746 protected function getExamplesMessages() {
748 'action=parse&page=Project:Sandbox'
749 => 'apihelp-parse-example-page',
750 'action=parse&text={{Project:Sandbox}}&contentmodel=wikitext'
751 => 'apihelp-parse-example-text',
752 'action=parse&text={{PAGENAME}}&title=Test'
753 => 'apihelp-parse-example-texttitle',
754 'action=parse&summary=Some+[[link]]&prop='
755 => 'apihelp-parse-example-summary',
759 public function getHelpUrls() {
760 return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#parse';