203 $this->getMain()->setCacheMode(
'anon-public-user-private' );
206 $params = $this->extractRequestParams();
210 $this->requireMaxOneParameter(
$params,
'page',
'pageid',
'oldid',
'text' );
211 $this->requireMaxOneParameter(
$params,
'page',
'pageid',
'oldid',
'title' );
212 $this->requireMaxOneParameter(
$params,
'page',
'pageid',
'oldid',
'revid' );
216 if ( $title ===
null ) {
217 $titleProvided =
false;
221 $titleProvided =
true;
228 $prop = array_fill_keys(
$params[
'prop'],
true );
230 if ( isset(
$params[
'section'] ) ) {
231 $this->section =
$params[
'section'];
232 if ( !preg_match(
'/^((T-)?\d+|new)$/', $this->section ) ) {
233 $this->dieWithError(
'apierror-invalidsection' );
236 $this->section =
false;
247 $needContent = isset( $prop[
'wikitext'] ) ||
248 isset( $prop[
'parsetree'] ) ||
$params[
'generatexml'];
251 $result = $this->getResult();
253 if ( $oldid !==
null || $pageid !==
null || $page !==
null ) {
254 if ( $this->section ===
'new' ) {
255 $this->dieWithError(
'apierror-invalidparammix-parse-new-section',
'invalidparammix' );
257 if ( $oldid !==
null ) {
259 $rev = $this->revisionLookup->getRevisionById( $oldid );
261 $this->dieWithError( [
'apierror-nosuchrevid', $oldid ] );
264 $this->checkTitleUserPermissions( $rev->getPage(),
'read' );
266 if ( !$rev->userCan( RevisionRecord::DELETED_TEXT, $this->getAuthority() ) ) {
268 [
'apierror-permissiondenied', $this->msg(
'action-deletedtext' ) ]
272 $revLinkTarget = $rev->getPageAsLinkTarget();
273 $titleObj = Title::newFromLinkTarget( $revLinkTarget );
275 $pageObj = $this->wikiPageFactory->newFromTitle( $titleObj );
276 [ $popts, $reset, $suppressCache ] = $this->makeParserOptions( $pageObj,
$params );
277 $p_result = $this->getParsedContent(
278 $pageObj, $popts, $suppressCache, $pageid, $rev, $needContent
286 if ( $pageid !==
null ) {
287 $reqParams[
'pageids'] = $pageid;
288 $pageParams[
'pageid'] = $pageid;
290 $reqParams[
'titles'] = $page;
291 $pageParams[
'title'] = $page;
297 $redirValues = $pageSet->getRedirectTitlesAsResult( $this->getResult() );
299 foreach ( $pageSet->getRedirectTargets() as $redirectTarget ) {
300 $pageParams = [
'title' => $this->titleFormatter->getFullText( $redirectTarget ) ];
302 } elseif ( $pageid !==
null ) {
303 $pageParams = [
'pageid' => $pageid ];
305 $pageParams = [
'title' => $page ];
308 $pageObj = $this->getTitleOrPageId( $pageParams,
'fromdb' );
310 if ( !$titleObj->exists() ) {
311 $this->dieWithError(
'apierror-missingtitle' );
314 $this->checkTitleUserPermissions( $titleObj,
'read' );
317 if ( isset( $prop[
'revid'] ) ) {
321 [ $popts, $reset, $suppressCache ] = $this->makeParserOptions( $pageObj,
$params );
322 $p_result = $this->getParsedContent(
323 $pageObj, $popts, $suppressCache, $pageid,
null, $needContent
327 $model =
$params[
'contentmodel'];
328 $format =
$params[
'contentformat'];
330 $titleObj = Title::newFromText( $title );
331 if ( !$titleObj || $titleObj->isExternal() ) {
332 $this->dieWithError( [
'apierror-invalidtitle',
wfEscapeWikiText( $title ) ] );
336 if ( $revid !==
null ) {
337 $rev = $this->revisionLookup->getRevisionById( $revid );
339 $this->dieWithError( [
'apierror-nosuchrevid', $revid ] );
341 $pTitleObj = $titleObj;
342 $titleObj = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
343 if ( $titleProvided ) {
344 if ( !$titleObj->equals( $pTitleObj ) ) {
345 $this->addWarning( [
'apierror-revwrongpage', $rev->getId(),
351 $titleProvided =
true;
355 if ( $titleObj->canExist() ) {
356 $pageObj = $this->wikiPageFactory->newFromTitle( $titleObj );
357 [ $popts, $reset ] = $this->makeParserOptions( $pageObj,
$params );
361 $popts = ParserOptions::newFromContext( $this->
getContext() );
362 [ $popts, $reset ] = $this->tweakParserOptions( $popts, $titleObj,
$params );
365 $textProvided = $text !==
null;
367 if ( !$textProvided ) {
368 if ( $titleProvided && ( $prop ||
$params[
'generatexml'] ) ) {
369 if ( $revid !==
null ) {
370 $this->addWarning(
'apiwarn-parse-revidwithouttext' );
372 $this->addWarning(
'apiwarn-parse-titlewithouttext' );
381 if ( $textProvided && !$titleProvided && $model ===
null ) {
383 $this->addWarning( [
'apiwarn-parse-nocontentmodel', $model ] );
384 } elseif ( $model ===
null ) {
385 $model = $titleObj->getContentModel();
388 $contentHandler = $this->contentHandlerFactory->getContentHandler( $model );
390 if ( $format && !$contentHandler->isSupportedFormat( $format ) ) {
391 $this->dieWithError( [
'apierror-badformat-generic', $format, $model ] );
395 $this->content = $contentHandler->unserializeContent( $text, $format );
397 $this->dieWithException( $ex, [
398 'wrap' => ApiMessage::create(
'apierror-contentserializationexception',
'parseerror' )
402 if ( $this->section !==
false ) {
403 if ( $this->section ===
'new' ) {
405 if (
$params[
'sectiontitle'] !==
null ) {
406 $this->content = $this->content->addSectionHeader(
$params[
'sectiontitle'] );
409 $this->content = $this->getSectionContent( $this->content, $titleObj->getPrefixedText() );
414 $this->pstContent = $this->contentTransformer->preSaveTransform(
417 $this->getUserForPreview(),
424 $result_array[
'text'] = $this->pstContent->serialize( $format );
425 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'text';
426 if ( isset( $prop[
'wikitext'] ) ) {
427 $result_array[
'wikitext'] = $this->content->serialize( $format );
428 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'wikitext';
430 if (
$params[
'summary'] !==
null ||
431 (
$params[
'sectiontitle'] !==
null && $this->section ===
'new' )
433 $result_array[
'parsedsummary'] = $this->formatSummary( $titleObj,
$params );
434 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'parsedsummary';
437 $result->addValue(
null, $this->getModuleName(), $result_array );
444 $p_result = $this->getContentParserOutput( $this->pstContent, $titleObj, $rev, $popts );
446 $p_result = $this->getContentParserOutput( $this->content, $titleObj, $rev, $popts );
452 $result_array[
'title'] = $titleObj->getPrefixedText();
453 $result_array[
'pageid'] = $pageid ?: $titleObj->getArticleID();
454 if ( $this->contentIsDeleted ) {
455 $result_array[
'textdeleted'] =
true;
457 if ( $this->contentIsSuppressed ) {
458 $result_array[
'textsuppressed'] =
true;
461 if ( isset(
$params[
'useskin'] ) ) {
470 $skin || isset( $prop[
'subtitle'] ) || isset( $prop[
'headhtml'] ) || isset( $prop[
'categorieshtml'] ) ||
471 isset(
$params[
'mobileformat'] )
486 $context =
new DerivativeContext( $this->
getContext() );
487 $context->setTitle( $titleObj );
490 $context->setWikiPage( $pageObj );
494 $context->setRequest(
new FauxRequest( [
'action' =>
'view' ] ) );
498 $context->setSkin( $skin );
500 $skin = $context->getSkin();
505 $context->setSkin( $context->getSkin() );
510 $outputPage->setArticleFlag(
true );
512 $outputPage->addParserOutputMetadata( $p_result );
513 if ( $this->content ) {
514 $outputPage->addContentOverride( $titleObj, $this->content );
516 $context->setOutput( $outputPage );
520 $outputPage->loadSkinModules( $skin );
523 $this->getHookRunner()->onApiParseMakeOutputPage( $this, $outputPage );
526 if ( $oldid !==
null ) {
527 $result_array[
'revid'] = (int)$oldid;
530 if (
$params[
'redirects'] && $redirValues !==
null ) {
531 $result_array[
'redirects'] = $redirValues;
534 if ( isset( $prop[
'text'] ) ) {
535 $skin = $context ? $context->getSkin() :
null;
536 $skinOptions = $skin ? $skin->getOptions() : [
539 $result_array[
'text'] = $p_result->getText( [
540 'allowTOC' => !
$params[
'disabletoc'],
541 'injectTOC' => $skinOptions[
'toc'],
542 'enableSectionEditLinks' => !
$params[
'disableeditsection'],
543 'wrapperDivClass' =>
$params[
'wrapoutputclass'],
544 'deduplicateStyles' => !
$params[
'disablestylededuplication'],
545 'userLang' => $context ? $context->getLanguage() :
null,
547 'includeDebugInfo' => !
$params[
'disablepp'] && !
$params[
'disablelimitreport']
549 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'text';
551 $this->getHookRunner()->onOutputPageBeforeHTML( $context->getOutput(), $result_array[
'text'] );
555 if (
$params[
'summary'] !==
null ||
556 (
$params[
'sectiontitle'] !==
null && $this->section ===
'new' )
558 $result_array[
'parsedsummary'] = $this->formatSummary( $titleObj,
$params );
559 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'parsedsummary';
562 if ( isset( $prop[
'langlinks'] ) ) {
564 $langlinks = $outputPage->getLanguageLinks();
566 $langlinks = $p_result->getLanguageLinks();
570 if (
$params[
'effectivelanglinks'] ) {
572 $this->getHookRunner()->onLanguageLinks( $titleObj, $langlinks, $linkFlags );
576 $result_array[
'langlinks'] = $this->formatLangLinks( $langlinks );
578 if ( isset( $prop[
'categories'] ) ) {
579 $result_array[
'categories'] = $this->formatCategoryLinks( $p_result->getCategoryMap() );
581 if ( isset( $prop[
'categorieshtml'] ) ) {
582 $result_array[
'categorieshtml'] = $outputPage->getSkin()->getCategories();
583 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'categorieshtml';
585 if ( isset( $prop[
'links'] ) ) {
586 $result_array[
'links'] = $this->formatLinks( $p_result->getLinks() );
588 if ( isset( $prop[
'templates'] ) ) {
589 $result_array[
'templates'] = $this->formatLinks( $p_result->getTemplates() );
591 if ( isset( $prop[
'images'] ) ) {
594 $result_array[
'images'] = array_map(
595 fn ( $link ) => (
string)$link,
596 array_keys( $p_result->getImages() )
599 if ( isset( $prop[
'externallinks'] ) ) {
600 $result_array[
'externallinks'] = array_keys( $p_result->getExternalLinks() );
602 if ( isset( $prop[
'sections'] ) ) {
603 $result_array[
'sections'] = $p_result->getSections();
604 $result_array[
'showtoc'] = $p_result->getOutputFlag( ParserOutputFlags::SHOW_TOC );
606 if ( isset( $prop[
'parsewarnings'] ) ) {
607 $result_array[
'parsewarnings'] = $p_result->getWarnings();
609 if ( isset( $prop[
'parsewarningshtml'] ) ) {
610 $warnings = $p_result->getWarnings();
611 $warningsHtml = array_map(
static function ( $warning ) {
612 return (
new RawMessage(
'$1', [ $warning ] ) )->parse();
614 $result_array[
'parsewarningshtml'] = $warningsHtml;
617 if ( isset( $prop[
'displaytitle'] ) ) {
618 $result_array[
'displaytitle'] = $p_result->getDisplayTitle() !==
false
619 ? $p_result->getDisplayTitle()
620 : htmlspecialchars( $titleObj->getPrefixedText(), ENT_NOQUOTES );
623 if ( isset( $prop[
'subtitle'] ) ) {
625 $result_array[
'subtitle'] = $context->getSkin()->prepareSubtitle(
false );
628 if ( isset( $prop[
'headitems'] ) ) {
630 $result_array[
'headitems'] = $this->formatHeadItems( $outputPage->getHeadItemsArray() );
632 $result_array[
'headitems'] = $this->formatHeadItems( $p_result->getHeadItems() );
636 if ( isset( $prop[
'headhtml'] ) ) {
637 $result_array[
'headhtml'] = $outputPage->headElement( $context->getSkin() );
638 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'headhtml';
641 if ( isset( $prop[
'modules'] ) ) {
643 $result_array[
'modules'] = $outputPage->getModules();
645 $result_array[
'modulescripts'] = [];
646 $result_array[
'modulestyles'] = $outputPage->getModuleStyles();
648 $result_array[
'modules'] = array_values( array_unique( $p_result->getModules() ) );
650 $result_array[
'modulescripts'] = [];
651 $result_array[
'modulestyles'] = array_values( array_unique( $p_result->getModuleStyles() ) );
655 if ( isset( $prop[
'jsconfigvars'] ) ) {
656 $showStrategyKeys = (bool)(
$params[
'showstrategykeys'] );
657 $jsconfigvars = $skin ? $outputPage->getJsConfigVars() : $p_result->getJsConfigVars( $showStrategyKeys );
658 $result_array[
'jsconfigvars'] = ApiResult::addMetadataToResultVars( $jsconfigvars );
661 if ( isset( $prop[
'encodedjsconfigvars'] ) ) {
662 $jsconfigvars = $skin ? $outputPage->getJsConfigVars() : $p_result->getJsConfigVars();
663 $result_array[
'encodedjsconfigvars'] = FormatJson::encode(
668 $result_array[ApiResult::META_SUBELEMENTS][] =
'encodedjsconfigvars';
671 if ( isset( $prop[
'modules'] ) &&
672 !isset( $prop[
'jsconfigvars'] ) && !isset( $prop[
'encodedjsconfigvars'] ) ) {
673 $this->addWarning(
'apiwarn-moduleswithoutvars' );
676 if ( isset( $prop[
'indicators'] ) ) {
678 $result_array[
'indicators'] = $outputPage->getIndicators();
680 $result_array[
'indicators'] = $p_result->getIndicators();
682 ApiResult::setArrayType( $result_array[
'indicators'],
'BCkvp',
'name' );
685 if ( isset( $prop[
'iwlinks'] ) ) {
686 $result_array[
'iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
689 if ( isset( $prop[
'wikitext'] ) ) {
690 $result_array[
'wikitext'] = $this->content->serialize( $format );
691 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'wikitext';
693 if ( $this->pstContent !==
null ) {
694 $result_array[
'psttext'] = $this->pstContent->serialize( $format );
695 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'psttext';
698 if ( isset( $prop[
'properties'] ) ) {
699 $result_array[
'properties'] = $p_result->getPageProperties();
700 ApiResult::setArrayType( $result_array[
'properties'],
'BCkvp',
'name' );
703 if ( isset( $prop[
'limitreportdata'] ) ) {
704 $result_array[
'limitreportdata'] =
705 $this->formatLimitReportData( $p_result->getLimitReportData() );
707 if ( isset( $prop[
'limitreporthtml'] ) ) {
708 $result_array[
'limitreporthtml'] = EditPage::getPreviewLimitReport( $p_result );
709 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'limitreporthtml';
712 if ( isset( $prop[
'parsetree'] ) ||
$params[
'generatexml'] ) {
714 $this->dieWithError(
'apierror-parsetree-notwikitext',
'notwikitext' );
717 $parser = $this->parserFactory->getInstance();
718 $parser->startExternalParse( $titleObj, $popts, Parser::OT_PREPROCESS );
720 $xml = $parser->preprocessToDom( $this->content->getText() )->__toString();
721 $result_array[
'parsetree'] = $xml;
722 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'parsetree';
728 'categories' =>
'cl',
732 'externallinks' =>
'el',
737 'indicators' =>
'ind',
738 'modulescripts' =>
'm',
739 'modulestyles' =>
'm',
740 'properties' =>
'pp',
741 'limitreportdata' =>
'lr',
742 'parsewarnings' =>
'pw',
743 'parsewarningshtml' =>
'pw',
745 $this->setIndexedTagNames( $result_array, $result_mapping );
746 $result->addValue(
null, $this->getModuleName(), $result_array );