200 $this->getMain()->setCacheMode(
'anon-public-user-private' );
203 $params = $this->extractRequestParams();
207 $this->requireMaxOneParameter(
$params,
'page',
'pageid',
'oldid',
'text' );
208 $this->requireMaxOneParameter(
$params,
'page',
'pageid',
'oldid',
'title' );
209 $this->requireMaxOneParameter(
$params,
'page',
'pageid',
'oldid',
'revid' );
213 if ( $title ===
null ) {
214 $titleProvided =
false;
218 $titleProvided =
true;
225 $prop = array_fill_keys(
$params[
'prop'],
true );
227 if ( isset(
$params[
'section'] ) ) {
228 $this->section =
$params[
'section'];
229 if ( !preg_match(
'/^((T-)?\d+|new)$/', $this->section ) ) {
230 $this->dieWithError(
'apierror-invalidsection' );
233 $this->section =
false;
245 $needContent = isset( $prop[
'wikitext'] ) ||
246 isset( $prop[
'parsetree'] ) ||
$params[
'generatexml'];
249 $result = $this->getResult();
251 if ( $oldid !==
null || $pageid !==
null || $page !==
null ) {
252 if ( $this->section ===
'new' ) {
253 $this->dieWithError(
'apierror-invalidparammix-parse-new-section',
'invalidparammix' );
255 if ( $oldid !==
null ) {
257 $rev = $this->revisionLookup->getRevisionById( $oldid );
259 $this->dieWithError( [
'apierror-nosuchrevid', $oldid ] );
262 $this->checkTitleUserPermissions( $rev->getPage(),
'read' );
264 if ( !$rev->userCan( RevisionRecord::DELETED_TEXT, $this->getAuthority() ) ) {
266 [
'apierror-permissiondenied', $this->msg(
'action-deletedtext' ) ]
270 $revLinkTarget = $rev->getPageAsLinkTarget();
271 $titleObj = Title::newFromLinkTarget( $revLinkTarget );
273 $pageObj = $this->wikiPageFactory->newFromTitle( $titleObj );
274 [ $popts, $reset, $suppressCache ] = $this->makeParserOptions( $pageObj,
$params );
275 $p_result = $this->getParsedContent(
276 $pageObj, $popts, $suppressCache, $pageid, $rev, $needContent
284 if ( $pageid !==
null ) {
285 $reqParams[
'pageids'] = $pageid;
286 $pageParams[
'pageid'] = $pageid;
288 $reqParams[
'titles'] = $page;
289 $pageParams[
'title'] = $page;
295 $redirValues = $pageSet->getRedirectTitlesAsResult( $this->getResult() );
297 foreach ( $pageSet->getRedirectTargets() as $redirectTarget ) {
298 $pageParams = [
'title' => $this->titleFormatter->getFullText( $redirectTarget ) ];
300 } elseif ( $pageid !==
null ) {
301 $pageParams = [
'pageid' => $pageid ];
303 $pageParams = [
'title' => $page ];
306 $pageObj = $this->getTitleOrPageId( $pageParams,
'fromdb' );
308 if ( !$titleObj->exists() ) {
309 $this->dieWithError(
'apierror-missingtitle' );
312 $this->checkTitleUserPermissions( $titleObj,
'read' );
315 if ( isset( $prop[
'revid'] ) ) {
319 [ $popts, $reset, $suppressCache ] = $this->makeParserOptions( $pageObj,
$params );
320 $p_result = $this->getParsedContent(
321 $pageObj, $popts, $suppressCache, $pageid,
null, $needContent
325 $model =
$params[
'contentmodel'];
326 $format =
$params[
'contentformat'];
328 $titleObj = Title::newFromText( $title );
329 if ( !$titleObj || $titleObj->isExternal() ) {
330 $this->dieWithError( [
'apierror-invalidtitle',
wfEscapeWikiText( $title ) ] );
334 if ( $revid !==
null ) {
335 $rev = $this->revisionLookup->getRevisionById( $revid );
337 $this->dieWithError( [
'apierror-nosuchrevid', $revid ] );
339 $pTitleObj = $titleObj;
340 $titleObj = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
341 if ( $titleProvided ) {
342 if ( !$titleObj->equals( $pTitleObj ) ) {
343 $this->addWarning( [
'apierror-revwrongpage', $rev->getId(),
349 $titleProvided =
true;
353 if ( $titleObj->canExist() ) {
354 $pageObj = $this->wikiPageFactory->newFromTitle( $titleObj );
355 [ $popts, $reset ] = $this->makeParserOptions( $pageObj,
$params );
359 $popts = ParserOptions::newFromContext( $this->
getContext() );
360 [ $popts, $reset ] = $this->tweakParserOptions( $popts, $titleObj,
$params );
363 $textProvided = $text !==
null;
365 if ( !$textProvided ) {
366 if ( $titleProvided && ( $prop ||
$params[
'generatexml'] ) ) {
367 if ( $revid !==
null ) {
368 $this->addWarning(
'apiwarn-parse-revidwithouttext' );
370 $this->addWarning(
'apiwarn-parse-titlewithouttext' );
379 if ( $textProvided && !$titleProvided && $model ===
null ) {
381 $this->addWarning( [
'apiwarn-parse-nocontentmodel', $model ] );
382 } elseif ( $model ===
null ) {
383 $model = $titleObj->getContentModel();
386 $contentHandler = $this->contentHandlerFactory->getContentHandler( $model );
388 if ( $format && !$contentHandler->isSupportedFormat( $format ) ) {
389 $this->dieWithError( [
'apierror-badformat-generic', $format, $model ] );
393 $this->content = $contentHandler->unserializeContent( $text, $format );
395 $this->dieWithException( $ex, [
396 'wrap' =>
ApiMessage::create(
'apierror-contentserializationexception',
'parseerror' )
400 if ( $this->section !==
false ) {
401 if ( $this->section ===
'new' ) {
403 if (
$params[
'sectiontitle'] !==
null ) {
404 $this->content = $this->content->addSectionHeader(
$params[
'sectiontitle'] );
407 $this->content = $this->getSectionContent( $this->content, $titleObj->getPrefixedText() );
412 $this->pstContent = $this->contentTransformer->preSaveTransform(
415 $this->getUserForPreview(),
422 $result_array[
'text'] = $this->pstContent->serialize( $format );
424 if ( isset( $prop[
'wikitext'] ) ) {
425 $result_array[
'wikitext'] = $this->content->serialize( $format );
428 if (
$params[
'summary'] !==
null ||
429 (
$params[
'sectiontitle'] !==
null && $this->section ===
'new' )
431 $result_array[
'parsedsummary'] = $this->formatSummary( $titleObj,
$params );
435 $result->addValue(
null, $this->getModuleName(), $result_array );
442 $p_result = $this->getContentParserOutput( $this->pstContent, $titleObj, $rev, $popts );
444 $p_result = $this->getContentParserOutput( $this->content, $titleObj, $rev, $popts );
450 $result_array[
'title'] = $titleObj->getPrefixedText();
451 $result_array[
'pageid'] = $pageid ?: $titleObj->getArticleID();
452 if ( $this->contentIsDeleted ) {
453 $result_array[
'textdeleted'] =
true;
455 if ( $this->contentIsSuppressed ) {
456 $result_array[
'textsuppressed'] =
true;
459 if ( isset(
$params[
'useskin'] ) ) {
468 $skin || isset( $prop[
'subtitle'] ) || isset( $prop[
'headhtml'] ) || isset( $prop[
'categorieshtml'] ) ||
469 isset(
$params[
'mobileformat'] )
486 $context->setTitle( $titleObj );
489 $context->setWikiPage( $pageObj );
493 $context->setRequest(
new FauxRequest( [
'action' =>
'view' ] ) );
497 $context->setSkin( $skin );
499 $skin = $context->getSkin();
504 $context->setSkin( $context->getSkin() );
509 $outputPage->setArticleFlag(
true );
511 $outputPage->addParserOutputMetadata( $p_result );
512 if ( $this->content ) {
513 $outputPage->addContentOverride( $titleObj, $this->content );
515 $context->setOutput( $outputPage );
519 $outputPage->loadSkinModules( $skin );
522 $this->getHookRunner()->onApiParseMakeOutputPage( $this, $outputPage );
525 if ( $oldid !==
null ) {
526 $result_array[
'revid'] = (int)$oldid;
529 if (
$params[
'redirects'] && $redirValues !==
null ) {
530 $result_array[
'redirects'] = $redirValues;
533 if ( isset( $prop[
'text'] ) ) {
534 $skin = $context ? $context->getSkin() :
null;
535 $skinOptions = $skin ? $skin->getOptions() : [
542 $oldText = $p_result->getRawText();
543 $result_array[
'text'] = $p_result->runOutputPipeline( $popts, [
544 'allowClone' =>
false,
545 'allowTOC' => !
$params[
'disabletoc'],
546 'injectTOC' => $skinOptions[
'toc'],
547 'enableSectionEditLinks' => !
$params[
'disableeditsection'],
548 'wrapperDivClass' =>
$params[
'wrapoutputclass'],
549 'deduplicateStyles' => !
$params[
'disablestylededuplication'],
550 'userLang' => $context ? $context->getLanguage() :
null,
552 'includeDebugInfo' => !
$params[
'disablepp'] && !
$params[
'disablelimitreport']
553 ] )->getContentHolderText();
554 $p_result->setRawText( $oldText );
557 $this->getHookRunner()->onOutputPageBeforeHTML( $context->getOutput(), $result_array[
'text'] );
561 if (
$params[
'summary'] !==
null ||
562 (
$params[
'sectiontitle'] !==
null && $this->section ===
'new' )
564 $result_array[
'parsedsummary'] = $this->formatSummary( $titleObj,
$params );
568 if ( isset( $prop[
'langlinks'] ) ) {
570 $langlinks = $outputPage->getLanguageLinks();
572 $langlinks = array_map(
573 fn ( $item ) => $item[
'link'],
574 $p_result->getLinkList( ParserOutputLinkTypes::LANGUAGE )
579 if (
$params[
'effectivelanglinks'] ) {
580 # for compatibility with old hook, convert to string[]
582 foreach ( $langlinks as $link ) {
583 $s = $link->getInterwiki() .
':' . $link->getText();
584 if ( $link->hasFragment() ) {
585 $s .=
'#' . $link->getFragment();
589 $langlinks = $compat;
591 $this->getHookRunner()->onLanguageLinks( $titleObj, $langlinks, $linkFlags );
595 $result_array[
'langlinks'] = $this->formatLangLinks( $langlinks );
597 if ( isset( $prop[
'categories'] ) ) {
598 $result_array[
'categories'] = $this->formatCategoryLinks( $p_result->getCategoryMap() );
600 if ( isset( $prop[
'categorieshtml'] ) ) {
601 $result_array[
'categorieshtml'] = $outputPage->getSkin()->getCategories();
604 if ( isset( $prop[
'links'] ) ) {
606 $result_array[
'links'] = $this->formatLinks( $p_result->getLinkList( ParserOutputLinkTypes::LOCAL ) );
608 if ( isset( $prop[
'templates'] ) ) {
609 $result_array[
'templates'] = $this->formatLinks(
610 $p_result->getLinkList( ParserOutputLinkTypes::TEMPLATE )
613 if ( isset( $prop[
'images'] ) ) {
614 $result_array[
'images'] = array_map(
615 fn ( $item ) => $item[
'link']->getDBkey(),
616 $p_result->getLinkList( ParserOutputLinkTypes::MEDIA )
619 if ( isset( $prop[
'externallinks'] ) ) {
620 $result_array[
'externallinks'] = array_keys( $p_result->getExternalLinks() );
622 if ( isset( $prop[
'sections'] ) ) {
623 $result_array[
'sections'] = $p_result->getSections();
624 $result_array[
'showtoc'] = $p_result->getOutputFlag( ParserOutputFlags::SHOW_TOC );
626 if ( isset( $prop[
'parsewarnings'] ) ) {
627 $result_array[
'parsewarnings'] = $p_result->getWarnings();
629 if ( isset( $prop[
'parsewarningshtml'] ) ) {
630 $warnings = $p_result->getWarnings();
631 $warningsHtml = array_map(
static function ( $warning ) {
632 return (
new RawMessage(
'$1', [ $warning ] ) )->parse();
634 $result_array[
'parsewarningshtml'] = $warningsHtml;
637 if ( isset( $prop[
'displaytitle'] ) ) {
638 $result_array[
'displaytitle'] = $p_result->getDisplayTitle() !==
false
639 ? $p_result->getDisplayTitle()
640 : htmlspecialchars( $titleObj->getPrefixedText(), ENT_NOQUOTES );
643 if ( isset( $prop[
'subtitle'] ) ) {
645 $result_array[
'subtitle'] = $context->getSkin()->prepareSubtitle(
false );
648 if ( isset( $prop[
'headitems'] ) ) {
650 $result_array[
'headitems'] = $this->formatHeadItems( $outputPage->getHeadItemsArray() );
652 $result_array[
'headitems'] = $this->formatHeadItems( $p_result->getHeadItems() );
656 if ( isset( $prop[
'headhtml'] ) ) {
657 $result_array[
'headhtml'] = $outputPage->headElement( $context->getSkin() );
661 if ( isset( $prop[
'modules'] ) ) {
663 $result_array[
'modules'] = $outputPage->getModules();
665 $result_array[
'modulescripts'] = [];
666 $result_array[
'modulestyles'] = $outputPage->getModuleStyles();
668 $result_array[
'modules'] = array_values( array_unique( $p_result->getModules() ) );
670 $result_array[
'modulescripts'] = [];
671 $result_array[
'modulestyles'] = array_values( array_unique( $p_result->getModuleStyles() ) );
675 if ( isset( $prop[
'jsconfigvars'] ) ) {
676 $showStrategyKeys = (bool)(
$params[
'showstrategykeys'] );
677 $jsconfigvars = $skin ? $outputPage->getJsConfigVars() : $p_result->getJsConfigVars( $showStrategyKeys );
681 if ( isset( $prop[
'encodedjsconfigvars'] ) ) {
682 $jsconfigvars = $skin ? $outputPage->getJsConfigVars() : $p_result->getJsConfigVars();
683 $result_array[
'encodedjsconfigvars'] = FormatJson::encode(
691 if ( isset( $prop[
'modules'] ) &&
692 !isset( $prop[
'jsconfigvars'] ) && !isset( $prop[
'encodedjsconfigvars'] ) ) {
693 $this->addWarning(
'apiwarn-moduleswithoutvars' );
696 if ( isset( $prop[
'indicators'] ) ) {
698 $result_array[
'indicators'] = $outputPage->getIndicators();
700 $result_array[
'indicators'] = $p_result->getIndicators();
705 if ( isset( $prop[
'iwlinks'] ) ) {
707 fn ( $item ) => $item[
'link'],
708 $p_result->getLinkList( ParserOutputLinkTypes::INTERWIKI )
710 $result_array[
'iwlinks'] = $this->formatIWLinks( $links );
713 if ( isset( $prop[
'wikitext'] ) ) {
714 $result_array[
'wikitext'] = $this->content->serialize( $format );
717 if ( $this->pstContent !==
null ) {
718 $result_array[
'psttext'] = $this->pstContent->serialize( $format );
722 if ( isset( $prop[
'properties'] ) ) {
723 $result_array[
'properties'] = $p_result->getPageProperties();
727 if ( isset( $prop[
'limitreportdata'] ) ) {
728 $result_array[
'limitreportdata'] =
729 $this->formatLimitReportData( $p_result->getLimitReportData() );
731 if ( isset( $prop[
'limitreporthtml'] ) ) {
732 $result_array[
'limitreporthtml'] = EditPage::getPreviewLimitReport( $p_result );
736 if ( isset( $prop[
'parsetree'] ) ||
$params[
'generatexml'] ) {
738 $this->dieWithError(
'apierror-parsetree-notwikitext',
'notwikitext' );
741 $parser = $this->parserFactory->getInstance();
742 $parser->startExternalParse( $titleObj, $popts, Parser::OT_PREPROCESS );
744 $xml = $parser->preprocessToDom( $this->content->getText() )->__toString();
745 $result_array[
'parsetree'] = $xml;
752 'categories' =>
'cl',
756 'externallinks' =>
'el',
761 'indicators' =>
'ind',
762 'modulescripts' =>
'm',
763 'modulestyles' =>
'm',
764 'properties' =>
'pp',
765 'limitreportdata' =>
'lr',
766 'parsewarnings' =>
'pw',
767 'parsewarningshtml' =>
'pw',
769 $this->setIndexedTagNames( $result_array, $result_mapping );
770 $result->addValue(
null, $this->getModuleName(), $result_array );