181 $this->getMain()->setCacheMode(
'anon-public-user-private' );
184 $params = $this->extractRequestParams();
188 $this->requireMaxOneParameter( $params,
'page',
'pageid',
'oldid',
'text' );
189 $this->requireMaxOneParameter( $params,
'page',
'pageid',
'oldid',
'title' );
190 $this->requireMaxOneParameter( $params,
'page',
'pageid',
'oldid',
'revid' );
192 $text = $params[
'text'];
193 $title = $params[
'title'];
195 $titleProvided =
false;
199 $titleProvided =
true;
202 $page = $params[
'page'];
203 $pageid = $params[
'pageid'];
204 $oldid = $params[
'oldid'];
206 $prop = array_fill_keys( $params[
'prop'],
true );
208 if ( isset( $params[
'section'] ) ) {
209 $this->section = $params[
'section'];
210 if ( !preg_match(
'/^((T-)?\d+|new)$/', $this->section ) ) {
211 $this->dieWithError(
'apierror-invalidsection' );
214 $this->section =
false;
225 $needContent = isset( $prop[
'wikitext'] ) ||
226 isset( $prop[
'parsetree'] ) || $params[
'generatexml'];
229 $result = $this->getResult();
231 if ( $oldid !==
null || $pageid !==
null || $page !==
null ) {
232 if ( $this->section ===
'new' ) {
233 $this->dieWithError(
'apierror-invalidparammix-parse-new-section',
'invalidparammix' );
235 if ( $oldid !==
null ) {
237 $rev = $this->revisionLookup->getRevisionById( $oldid );
239 $this->dieWithError( [
'apierror-nosuchrevid', $oldid ] );
242 $this->checkTitleUserPermissions( $rev->getPage(),
'read' );
244 if ( !$rev->userCan( RevisionRecord::DELETED_TEXT, $this->getAuthority() ) ) {
246 [
'apierror-permissiondenied', $this->msg(
'action-deletedtext' ) ]
250 $revLinkTarget = $rev->getPageAsLinkTarget();
251 $titleObj = Title::newFromLinkTarget( $revLinkTarget );
253 $pageObj = $this->wikiPageFactory->newFromTitle( $titleObj );
254 list( $popts, $reset, $suppressCache ) = $this->makeParserOptions( $pageObj, $params );
255 $p_result = $this->getParsedContent(
256 $pageObj, $popts, $suppressCache, $pageid, $rev, $needContent
259 if ( $params[
'redirects'] ) {
264 if ( $pageid !==
null ) {
265 $reqParams[
'pageids'] = $pageid;
266 $pageParams[
'pageid'] = $pageid;
268 $reqParams[
'titles'] = $page;
269 $pageParams[
'title'] = $page;
275 $redirValues = $pageSet->getRedirectTitlesAsResult( $this->getResult() );
277 foreach ( $pageSet->getRedirectTitles() as
$title ) {
278 $pageParams = [
'title' =>
$title->getFullText() ];
280 } elseif ( $pageid !==
null ) {
281 $pageParams = [
'pageid' => $pageid ];
283 $pageParams = [
'title' => $page ];
286 $pageObj = $this->getTitleOrPageId( $pageParams,
'fromdb' );
288 if ( !$titleObj->exists() ) {
289 $this->dieWithError(
'apierror-missingtitle' );
292 $this->checkTitleUserPermissions( $titleObj,
'read' );
295 if ( isset( $prop[
'revid'] ) ) {
299 list( $popts, $reset, $suppressCache ) = $this->makeParserOptions( $pageObj, $params );
300 $p_result = $this->getParsedContent(
301 $pageObj, $popts, $suppressCache, $pageid,
null, $needContent
305 $model = $params[
'contentmodel'];
306 $format = $params[
'contentformat'];
308 $titleObj = Title::newFromText(
$title );
309 if ( !$titleObj || $titleObj->isExternal() ) {
312 $revid = $params[
'revid'];
313 if ( $revid !==
null ) {
314 $rev = $this->revisionLookup->getRevisionById( $revid );
316 $this->dieWithError( [
'apierror-nosuchrevid', $revid ] );
318 $pTitleObj = $titleObj;
319 $titleObj = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
320 if ( $titleProvided ) {
321 if ( !$titleObj->equals( $pTitleObj ) ) {
322 $this->addWarning( [
'apierror-revwrongpage', $rev->getId(),
328 $titleProvided =
true;
332 if ( $titleObj->canExist() ) {
333 $pageObj = $this->wikiPageFactory->newFromTitle( $titleObj );
334 list( $popts, $reset ) = $this->makeParserOptions( $pageObj, $params );
338 $popts = ParserOptions::newCanonical( $this->
getContext() );
339 list( $popts, $reset ) = $this->tweakParserOptions( $popts, $titleObj, $params );
342 $textProvided = $text !==
null;
344 if ( !$textProvided ) {
345 if ( $titleProvided && ( $prop || $params[
'generatexml'] ) ) {
346 if ( $revid !==
null ) {
347 $this->addWarning(
'apiwarn-parse-revidwithouttext' );
349 $this->addWarning(
'apiwarn-parse-titlewithouttext' );
358 if ( $textProvided && !$titleProvided && $model ===
null ) {
360 $this->addWarning( [
'apiwarn-parse-nocontentmodel', $model ] );
361 } elseif ( $model ===
null ) {
362 $model = $titleObj->getContentModel();
365 $contentHandler = $this->contentHandlerFactory->getContentHandler( $model );
367 if ( $format && !$contentHandler->isSupportedFormat( $format ) ) {
368 $this->dieWithError( [
'apierror-badformat-generic', $format, $model ] );
372 $this->content = $contentHandler->unserializeContent( $text, $format );
374 $this->dieWithException( $ex, [
375 'wrap' => ApiMessage::create(
'apierror-contentserializationexception',
'parseerror' )
379 if ( $this->section !==
false ) {
380 if ( $this->section ===
'new' ) {
382 if ( $params[
'sectiontitle'] !==
null ) {
383 $this->content = $this->content->addSectionHeader( $params[
'sectiontitle'] );
386 $this->content = $this->getSectionContent( $this->content, $titleObj->getPrefixedText() );
390 if ( $params[
'pst'] || $params[
'onlypst'] ) {
391 $this->pstContent = $this->contentTransformer->preSaveTransform(
398 if ( $params[
'onlypst'] ) {
401 $result_array[
'text'] = $this->pstContent->serialize( $format );
402 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'text';
403 if ( isset( $prop[
'wikitext'] ) ) {
404 $result_array[
'wikitext'] = $this->content->serialize( $format );
405 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'wikitext';
407 if ( $params[
'summary'] !==
null ||
408 ( $params[
'sectiontitle'] !==
null && $this->section ===
'new' )
410 $result_array[
'parsedsummary'] = $this->formatSummary( $titleObj, $params );
411 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'parsedsummary';
414 $result->addValue(
null, $this->getModuleName(), $result_array );
420 if ( $params[
'pst'] ) {
421 $p_result = $this->getContentParserOutput( $this->pstContent, $titleObj, $revid, $popts );
423 $p_result = $this->getContentParserOutput( $this->content, $titleObj, $revid, $popts );
429 $result_array[
'title'] = $titleObj->getPrefixedText();
430 $result_array[
'pageid'] = $pageid ?: $titleObj->getArticleID();
431 if ( $this->contentIsDeleted ) {
432 $result_array[
'textdeleted'] =
true;
434 if ( $this->contentIsSuppressed ) {
435 $result_array[
'textsuppressed'] =
true;
438 if ( isset( $params[
'useskin'] ) ) {
447 $skin || isset( $prop[
'subtitle'] ) || isset( $prop[
'headhtml'] ) || isset( $prop[
'categorieshtml'] ) ||
448 isset( $params[
'mobileformat'] )
465 $context->setTitle( $titleObj );
468 $context->setWikiPage( $pageObj );
472 $context->setRequest(
new FauxRequest( [
'action' =>
'view' ] ) );
476 $context->setSkin( $skin );
478 $skin = $context->getSkin();
483 $context->setSkin( $context->getSkin() );
488 $outputPage->setArticleFlag(
true );
490 $outputPage->addParserOutputMetadata( $p_result );
491 if ( $this->content ) {
492 $outputPage->addContentOverride( $titleObj, $this->content );
494 $context->setOutput( $outputPage );
498 $outputPage->loadSkinModules( $skin );
501 $this->getHookRunner()->onApiParseMakeOutputPage( $this, $outputPage );
504 if ( $oldid !==
null ) {
505 $result_array[
'revid'] = (int)$oldid;
508 if ( $params[
'redirects'] && $redirValues !==
null ) {
509 $result_array[
'redirects'] = $redirValues;
512 if ( isset( $prop[
'text'] ) ) {
513 $skin = $context ? $context->getSkin() :
null;
514 $skinOptions = $skin ? $skin->getOptions() : [
517 $result_array[
'text'] = $p_result->getText( [
518 'allowTOC' => !$params[
'disabletoc'],
519 'injectTOC' => $skinOptions[
'toc'],
520 'enableSectionEditLinks' => !$params[
'disableeditsection'],
521 'wrapperDivClass' => $params[
'wrapoutputclass'],
522 'deduplicateStyles' => !$params[
'disablestylededuplication'],
524 'includeDebugInfo' => !$params[
'disablepp'] && !$params[
'disablelimitreport']
526 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'text';
528 $this->getHookRunner()->onOutputPageBeforeHTML( $context->getOutput(), $result_array[
'text'] );
532 if ( $params[
'summary'] !==
null ||
533 ( $params[
'sectiontitle'] !==
null && $this->section ===
'new' )
535 $result_array[
'parsedsummary'] = $this->formatSummary( $titleObj, $params );
536 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'parsedsummary';
539 if ( isset( $prop[
'langlinks'] ) ) {
541 $langlinks = $outputPage->getLanguageLinks();
543 $langlinks = $p_result->getLanguageLinks();
547 if ( $params[
'effectivelanglinks'] ) {
549 $this->getHookRunner()->onLanguageLinks( $titleObj, $langlinks, $linkFlags );
553 $result_array[
'langlinks'] = $this->formatLangLinks( $langlinks );
555 if ( isset( $prop[
'categories'] ) ) {
556 $result_array[
'categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
558 if ( isset( $prop[
'categorieshtml'] ) ) {
559 $result_array[
'categorieshtml'] = $outputPage->getSkin()->getCategories();
560 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'categorieshtml';
562 if ( isset( $prop[
'links'] ) ) {
563 $result_array[
'links'] = $this->formatLinks( $p_result->getLinks() );
565 if ( isset( $prop[
'templates'] ) ) {
566 $result_array[
'templates'] = $this->formatLinks( $p_result->getTemplates() );
568 if ( isset( $prop[
'images'] ) ) {
569 $result_array[
'images'] = array_keys( $p_result->getImages() );
571 if ( isset( $prop[
'externallinks'] ) ) {
572 $result_array[
'externallinks'] = array_keys( $p_result->getExternalLinks() );
574 if ( isset( $prop[
'sections'] ) ) {
575 $result_array[
'sections'] = $p_result->getSections();
576 $result_array[
'showtoc'] = (bool)$p_result->getTOCHTML();
578 if ( isset( $prop[
'parsewarnings'] ) ) {
579 $result_array[
'parsewarnings'] = $p_result->getWarnings();
581 if ( isset( $prop[
'parsewarningshtml'] ) ) {
582 $warnings = $p_result->getWarnings();
583 $warningsHtml = array_map(
static function ( $warning ) {
584 return (
new RawMessage(
'$1', [ $warning ] ) )->parse();
586 $result_array[
'parsewarningshtml'] = $warningsHtml;
589 if ( isset( $prop[
'displaytitle'] ) ) {
590 $result_array[
'displaytitle'] = $p_result->getDisplayTitle() !==
false
591 ? $p_result->getDisplayTitle()
592 : htmlspecialchars( $titleObj->getPrefixedText(), ENT_NOQUOTES );
595 if ( isset( $prop[
'subtitle'] ) ) {
596 $result_array[
'subtitle'] = $context->getSkin()->prepareSubtitle();
599 if ( isset( $prop[
'headitems'] ) ) {
601 $result_array[
'headitems'] = $this->formatHeadItems( $outputPage->getHeadItemsArray() );
603 $result_array[
'headitems'] = $this->formatHeadItems( $p_result->getHeadItems() );
607 if ( isset( $prop[
'headhtml'] ) ) {
608 $result_array[
'headhtml'] = $outputPage->headElement( $context->getSkin() );
609 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'headhtml';
612 if ( isset( $prop[
'modules'] ) ) {
614 $result_array[
'modules'] = $outputPage->getModules();
616 $result_array[
'modulescripts'] = [];
617 $result_array[
'modulestyles'] = $outputPage->getModuleStyles();
619 $result_array[
'modules'] = array_values( array_unique( $p_result->getModules() ) );
621 $result_array[
'modulescripts'] = [];
622 $result_array[
'modulestyles'] = array_values( array_unique( $p_result->getModuleStyles() ) );
626 if ( isset( $prop[
'jsconfigvars'] ) ) {
627 $showStrategyKeys = (bool)( $params[
'showstrategykeys'] );
628 $jsconfigvars = $skin ? $outputPage->getJsConfigVars() : $p_result->getJsConfigVars( $showStrategyKeys );
629 $result_array[
'jsconfigvars'] = ApiResult::addMetadataToResultVars( $jsconfigvars );
632 if ( isset( $prop[
'encodedjsconfigvars'] ) ) {
633 $jsconfigvars = $skin ? $outputPage->getJsConfigVars() : $p_result->getJsConfigVars();
634 $result_array[
'encodedjsconfigvars'] = FormatJson::encode(
639 $result_array[ApiResult::META_SUBELEMENTS][] =
'encodedjsconfigvars';
642 if ( isset( $prop[
'modules'] ) &&
643 !isset( $prop[
'jsconfigvars'] ) && !isset( $prop[
'encodedjsconfigvars'] ) ) {
644 $this->addWarning(
'apiwarn-moduleswithoutvars' );
647 if ( isset( $prop[
'indicators'] ) ) {
649 $result_array[
'indicators'] = (array)$outputPage->getIndicators();
651 $result_array[
'indicators'] = (array)$p_result->getIndicators();
653 ApiResult::setArrayType( $result_array[
'indicators'],
'BCkvp',
'name' );
656 if ( isset( $prop[
'iwlinks'] ) ) {
657 $result_array[
'iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
660 if ( isset( $prop[
'wikitext'] ) ) {
661 $result_array[
'wikitext'] = $this->content->serialize( $format );
662 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'wikitext';
664 if ( $this->pstContent !==
null ) {
665 $result_array[
'psttext'] = $this->pstContent->serialize( $format );
666 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'psttext';
669 if ( isset( $prop[
'properties'] ) ) {
670 $result_array[
'properties'] = $p_result->getPageProperties();
671 ApiResult::setArrayType( $result_array[
'properties'],
'BCkvp',
'name' );
674 if ( isset( $prop[
'limitreportdata'] ) ) {
675 $result_array[
'limitreportdata'] =
676 $this->formatLimitReportData( $p_result->getLimitReportData() );
678 if ( isset( $prop[
'limitreporthtml'] ) ) {
680 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'limitreporthtml';
683 if ( isset( $prop[
'parsetree'] ) || $params[
'generatexml'] ) {
685 $this->dieWithError(
'apierror-parsetree-notwikitext',
'notwikitext' );
690 $xml = $this->parser->preprocessToDom( $this->content->getText() )->__toString();
691 $result_array[
'parsetree'] = $xml;
692 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'parsetree';
698 'categories' =>
'cl',
702 'externallinks' =>
'el',
707 'indicators' =>
'ind',
708 'modulescripts' =>
'm',
709 'modulestyles' =>
'm',
710 'properties' =>
'pp',
711 'limitreportdata' =>
'lr',
712 'parsewarnings' =>
'pw',
713 'parsewarningshtml' =>
'pw',
715 $this->setIndexedTagNames( $result_array, $result_mapping );
716 $result->addValue(
null, $this->getModuleName(), $result_array );