198 $this->getMain()->setCacheMode(
'anon-public-user-private' );
201 $params = $this->extractRequestParams();
205 $this->requireMaxOneParameter(
$params,
'page',
'pageid',
'oldid',
'text' );
206 $this->requireMaxOneParameter(
$params,
'page',
'pageid',
'oldid',
'title' );
207 $this->requireMaxOneParameter(
$params,
'page',
'pageid',
'oldid',
'revid' );
211 if ( $title ===
null ) {
212 $titleProvided =
false;
216 $titleProvided =
true;
223 $prop = array_fill_keys(
$params[
'prop'],
true );
225 if ( isset(
$params[
'section'] ) ) {
226 $this->section =
$params[
'section'];
227 if ( !preg_match(
'/^((T-)?\d+|new)$/', $this->section ) ) {
228 $this->dieWithError(
'apierror-invalidsection' );
231 $this->section =
false;
243 $needContent = isset( $prop[
'wikitext'] ) ||
244 isset( $prop[
'parsetree'] ) ||
$params[
'generatexml'];
247 $result = $this->getResult();
249 if ( $oldid !==
null || $pageid !==
null || $page !==
null ) {
250 if ( $this->section ===
'new' ) {
251 $this->dieWithError(
'apierror-invalidparammix-parse-new-section',
'invalidparammix' );
253 if ( $oldid !==
null ) {
255 $rev = $this->revisionLookup->getRevisionById( $oldid );
257 $this->dieWithError( [
'apierror-nosuchrevid', $oldid ] );
260 $this->checkTitleUserPermissions( $rev->getPage(),
'read' );
262 if ( !$rev->userCan( RevisionRecord::DELETED_TEXT, $this->getAuthority() ) ) {
264 [
'apierror-permissiondenied', $this->msg(
'action-deletedtext' ) ]
268 $revLinkTarget = $rev->getPageAsLinkTarget();
269 $titleObj = Title::newFromLinkTarget( $revLinkTarget );
271 $pageObj = $this->wikiPageFactory->newFromTitle( $titleObj );
272 [ $popts, $reset, $suppressCache ] = $this->makeParserOptions( $pageObj,
$params );
273 $p_result = $this->getParsedContent(
274 $pageObj, $popts, $suppressCache, $pageid, $rev, $needContent
282 if ( $pageid !==
null ) {
283 $reqParams[
'pageids'] = $pageid;
284 $pageParams[
'pageid'] = $pageid;
286 $reqParams[
'titles'] = $page;
287 $pageParams[
'title'] = $page;
293 $redirValues = $pageSet->getRedirectTitlesAsResult( $this->getResult() );
295 foreach ( $pageSet->getRedirectTargets() as $redirectTarget ) {
296 $pageParams = [
'title' => $this->titleFormatter->getFullText( $redirectTarget ) ];
298 } elseif ( $pageid !==
null ) {
299 $pageParams = [
'pageid' => $pageid ];
301 $pageParams = [
'title' => $page ];
304 $pageObj = $this->getTitleOrPageId( $pageParams,
'fromdb' );
306 if ( !$titleObj->exists() ) {
307 $this->dieWithError(
'apierror-missingtitle' );
310 $this->checkTitleUserPermissions( $titleObj,
'read' );
313 if ( isset( $prop[
'revid'] ) ) {
317 [ $popts, $reset, $suppressCache ] = $this->makeParserOptions( $pageObj,
$params );
318 $p_result = $this->getParsedContent(
319 $pageObj, $popts, $suppressCache, $pageid,
null, $needContent
323 $model =
$params[
'contentmodel'];
324 $format =
$params[
'contentformat'];
326 $titleObj = Title::newFromText( $title );
327 if ( !$titleObj || $titleObj->isExternal() ) {
328 $this->dieWithError( [
'apierror-invalidtitle',
wfEscapeWikiText( $title ) ] );
332 if ( $revid !==
null ) {
333 $rev = $this->revisionLookup->getRevisionById( $revid );
335 $this->dieWithError( [
'apierror-nosuchrevid', $revid ] );
337 $pTitleObj = $titleObj;
338 $titleObj = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
339 if ( $titleProvided ) {
340 if ( !$titleObj->equals( $pTitleObj ) ) {
341 $this->addWarning( [
'apierror-revwrongpage', $rev->getId(),
347 $titleProvided =
true;
351 if ( $titleObj->canExist() ) {
352 $pageObj = $this->wikiPageFactory->newFromTitle( $titleObj );
353 [ $popts, $reset ] = $this->makeParserOptions( $pageObj,
$params );
357 $popts = ParserOptions::newFromContext( $this->
getContext() );
358 [ $popts, $reset ] = $this->tweakParserOptions( $popts, $titleObj,
$params );
361 $textProvided = $text !==
null;
363 if ( !$textProvided ) {
364 if ( $titleProvided && ( $prop ||
$params[
'generatexml'] ) ) {
365 if ( $revid !==
null ) {
366 $this->addWarning(
'apiwarn-parse-revidwithouttext' );
368 $this->addWarning(
'apiwarn-parse-titlewithouttext' );
377 if ( $textProvided && !$titleProvided && $model ===
null ) {
379 $this->addWarning( [
'apiwarn-parse-nocontentmodel', $model ] );
380 } elseif ( $model ===
null ) {
381 $model = $titleObj->getContentModel();
384 $contentHandler = $this->contentHandlerFactory->getContentHandler( $model );
386 if ( $format && !$contentHandler->isSupportedFormat( $format ) ) {
387 $this->dieWithError( [
'apierror-badformat-generic', $format, $model ] );
391 $this->content = $contentHandler->unserializeContent( $text, $format );
393 $this->dieWithException( $ex, [
394 'wrap' =>
ApiMessage::create(
'apierror-contentserializationexception',
'parseerror' )
398 if ( $this->section !==
false ) {
399 if ( $this->section ===
'new' ) {
401 if (
$params[
'sectiontitle'] !==
null ) {
402 $this->content = $this->content->addSectionHeader(
$params[
'sectiontitle'] );
405 $this->content = $this->getSectionContent( $this->content, $titleObj->getPrefixedText() );
410 $this->pstContent = $this->contentTransformer->preSaveTransform(
413 $this->getUserForPreview(),
420 $result_array[
'text'] = $this->pstContent->serialize( $format );
422 if ( isset( $prop[
'wikitext'] ) ) {
423 $result_array[
'wikitext'] = $this->content->serialize( $format );
426 if (
$params[
'summary'] !==
null ||
427 (
$params[
'sectiontitle'] !==
null && $this->section ===
'new' )
429 $result_array[
'parsedsummary'] = $this->formatSummary( $titleObj,
$params );
433 $result->addValue(
null, $this->getModuleName(), $result_array );
440 $p_result = $this->getContentParserOutput( $this->pstContent, $titleObj, $rev, $popts );
442 $p_result = $this->getContentParserOutput( $this->content, $titleObj, $rev, $popts );
448 $result_array[
'title'] = $titleObj->getPrefixedText();
449 $result_array[
'pageid'] = $pageid ?: $titleObj->getArticleID();
450 if ( $this->contentIsDeleted ) {
451 $result_array[
'textdeleted'] =
true;
453 if ( $this->contentIsSuppressed ) {
454 $result_array[
'textsuppressed'] =
true;
457 if ( isset(
$params[
'useskin'] ) ) {
466 $skin || isset( $prop[
'subtitle'] ) || isset( $prop[
'headhtml'] ) || isset( $prop[
'categorieshtml'] ) ||
467 isset(
$params[
'mobileformat'] )
484 $context->setTitle( $titleObj );
487 $context->setWikiPage( $pageObj );
491 $context->setRequest(
new FauxRequest( [
'action' =>
'view' ] ) );
495 $context->setSkin( $skin );
497 $skin = $context->getSkin();
502 $context->setSkin( $context->getSkin() );
507 $outputPage->setArticleFlag(
true );
509 $outputPage->addParserOutputMetadata( $p_result );
510 if ( $this->content ) {
511 $outputPage->addContentOverride( $titleObj, $this->content );
513 $context->setOutput( $outputPage );
517 $outputPage->loadSkinModules( $skin );
520 $this->getHookRunner()->onApiParseMakeOutputPage( $this, $outputPage );
523 if ( $oldid !==
null ) {
524 $result_array[
'revid'] = (int)$oldid;
527 if (
$params[
'redirects'] && $redirValues !==
null ) {
528 $result_array[
'redirects'] = $redirValues;
531 if ( isset( $prop[
'text'] ) ) {
532 $skin = $context ? $context->getSkin() :
null;
533 $skinOptions = $skin ? $skin->getOptions() : [
540 $oldText = $p_result->getRawText();
541 $result_array[
'text'] = $p_result->runOutputPipeline( $popts, [
542 'allowClone' =>
false,
543 'allowTOC' => !
$params[
'disabletoc'],
544 'injectTOC' => $skinOptions[
'toc'],
545 'enableSectionEditLinks' => !
$params[
'disableeditsection'],
546 'wrapperDivClass' =>
$params[
'wrapoutputclass'],
547 'deduplicateStyles' => !
$params[
'disablestylededuplication'],
548 'userLang' => $context ? $context->getLanguage() :
null,
550 'includeDebugInfo' => !
$params[
'disablepp'] && !
$params[
'disablelimitreport']
551 ] )->getContentHolderText();
552 $p_result->setRawText( $oldText );
555 $this->getHookRunner()->onOutputPageBeforeHTML( $context->getOutput(), $result_array[
'text'] );
559 if (
$params[
'summary'] !==
null ||
560 (
$params[
'sectiontitle'] !==
null && $this->section ===
'new' )
562 $result_array[
'parsedsummary'] = $this->formatSummary( $titleObj,
$params );
566 if ( isset( $prop[
'langlinks'] ) ) {
568 $langlinks = $outputPage->getLanguageLinks();
570 $langlinks = $p_result->getLanguageLinks();
574 if (
$params[
'effectivelanglinks'] ) {
576 $this->getHookRunner()->onLanguageLinks( $titleObj, $langlinks, $linkFlags );
580 $result_array[
'langlinks'] = $this->formatLangLinks( $langlinks );
582 if ( isset( $prop[
'categories'] ) ) {
583 $result_array[
'categories'] = $this->formatCategoryLinks( $p_result->getCategoryMap() );
585 if ( isset( $prop[
'categorieshtml'] ) ) {
586 $result_array[
'categorieshtml'] = $outputPage->getSkin()->getCategories();
589 if ( isset( $prop[
'links'] ) ) {
590 $result_array[
'links'] = $this->formatLinks( $p_result->getLinks() );
592 if ( isset( $prop[
'templates'] ) ) {
593 $result_array[
'templates'] = $this->formatLinks( $p_result->getTemplates() );
595 if ( isset( $prop[
'images'] ) ) {
598 $result_array[
'images'] = array_map(
599 fn ( $link ) => (
string)$link,
600 array_keys( $p_result->getImages() )
603 if ( isset( $prop[
'externallinks'] ) ) {
604 $result_array[
'externallinks'] = array_keys( $p_result->getExternalLinks() );
606 if ( isset( $prop[
'sections'] ) ) {
607 $result_array[
'sections'] = $p_result->getSections();
608 $result_array[
'showtoc'] = $p_result->getOutputFlag( ParserOutputFlags::SHOW_TOC );
610 if ( isset( $prop[
'parsewarnings'] ) ) {
611 $result_array[
'parsewarnings'] = $p_result->getWarnings();
613 if ( isset( $prop[
'parsewarningshtml'] ) ) {
614 $warnings = $p_result->getWarnings();
615 $warningsHtml = array_map(
static function ( $warning ) {
616 return (
new RawMessage(
'$1', [ $warning ] ) )->parse();
618 $result_array[
'parsewarningshtml'] = $warningsHtml;
621 if ( isset( $prop[
'displaytitle'] ) ) {
622 $result_array[
'displaytitle'] = $p_result->getDisplayTitle() !==
false
623 ? $p_result->getDisplayTitle()
624 : htmlspecialchars( $titleObj->getPrefixedText(), ENT_NOQUOTES );
627 if ( isset( $prop[
'subtitle'] ) ) {
629 $result_array[
'subtitle'] = $context->getSkin()->prepareSubtitle(
false );
632 if ( isset( $prop[
'headitems'] ) ) {
634 $result_array[
'headitems'] = $this->formatHeadItems( $outputPage->getHeadItemsArray() );
636 $result_array[
'headitems'] = $this->formatHeadItems( $p_result->getHeadItems() );
640 if ( isset( $prop[
'headhtml'] ) ) {
641 $result_array[
'headhtml'] = $outputPage->headElement( $context->getSkin() );
645 if ( isset( $prop[
'modules'] ) ) {
647 $result_array[
'modules'] = $outputPage->getModules();
649 $result_array[
'modulescripts'] = [];
650 $result_array[
'modulestyles'] = $outputPage->getModuleStyles();
652 $result_array[
'modules'] = array_values( array_unique( $p_result->getModules() ) );
654 $result_array[
'modulescripts'] = [];
655 $result_array[
'modulestyles'] = array_values( array_unique( $p_result->getModuleStyles() ) );
659 if ( isset( $prop[
'jsconfigvars'] ) ) {
660 $showStrategyKeys = (bool)(
$params[
'showstrategykeys'] );
661 $jsconfigvars = $skin ? $outputPage->getJsConfigVars() : $p_result->getJsConfigVars( $showStrategyKeys );
665 if ( isset( $prop[
'encodedjsconfigvars'] ) ) {
666 $jsconfigvars = $skin ? $outputPage->getJsConfigVars() : $p_result->getJsConfigVars();
667 $result_array[
'encodedjsconfigvars'] = FormatJson::encode(
675 if ( isset( $prop[
'modules'] ) &&
676 !isset( $prop[
'jsconfigvars'] ) && !isset( $prop[
'encodedjsconfigvars'] ) ) {
677 $this->addWarning(
'apiwarn-moduleswithoutvars' );
680 if ( isset( $prop[
'indicators'] ) ) {
682 $result_array[
'indicators'] = $outputPage->getIndicators();
684 $result_array[
'indicators'] = $p_result->getIndicators();
689 if ( isset( $prop[
'iwlinks'] ) ) {
690 $result_array[
'iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
693 if ( isset( $prop[
'wikitext'] ) ) {
694 $result_array[
'wikitext'] = $this->content->serialize( $format );
697 if ( $this->pstContent !==
null ) {
698 $result_array[
'psttext'] = $this->pstContent->serialize( $format );
702 if ( isset( $prop[
'properties'] ) ) {
703 $result_array[
'properties'] = $p_result->getPageProperties();
707 if ( isset( $prop[
'limitreportdata'] ) ) {
708 $result_array[
'limitreportdata'] =
709 $this->formatLimitReportData( $p_result->getLimitReportData() );
711 if ( isset( $prop[
'limitreporthtml'] ) ) {
712 $result_array[
'limitreporthtml'] = EditPage::getPreviewLimitReport( $p_result );
716 if ( isset( $prop[
'parsetree'] ) ||
$params[
'generatexml'] ) {
718 $this->dieWithError(
'apierror-parsetree-notwikitext',
'notwikitext' );
721 $parser = $this->parserFactory->getInstance();
722 $parser->startExternalParse( $titleObj, $popts, Parser::OT_PREPROCESS );
724 $xml = $parser->preprocessToDom( $this->content->getText() )->__toString();
725 $result_array[
'parsetree'] = $xml;
732 'categories' =>
'cl',
736 'externallinks' =>
'el',
741 'indicators' =>
'ind',
742 'modulescripts' =>
'm',
743 'modulestyles' =>
'm',
744 'properties' =>
'pp',
745 'limitreportdata' =>
'lr',
746 'parsewarnings' =>
'pw',
747 'parsewarningshtml' =>
'pw',
749 $this->setIndexedTagNames( $result_array, $result_mapping );
750 $result->addValue(
null, $this->getModuleName(), $result_array );