202 $this->getMain()->setCacheMode(
'anon-public-user-private' );
205 $params = $this->extractRequestParams();
209 $this->requireMaxOneParameter( $params,
'page',
'pageid',
'oldid',
'text' );
210 $this->requireMaxOneParameter( $params,
'page',
'pageid',
'oldid',
'title' );
211 $this->requireMaxOneParameter( $params,
'page',
'pageid',
'oldid',
'revid' );
213 $text = $params[
'text'];
214 $title = $params[
'title'];
215 if ( $title ===
null ) {
216 $titleProvided =
false;
220 $titleProvided =
true;
223 $page = $params[
'page'];
224 $pageid = $params[
'pageid'];
225 $oldid = $params[
'oldid'];
227 $prop = array_fill_keys( $params[
'prop'],
true );
229 if ( isset( $params[
'section'] ) ) {
230 $this->section = $params[
'section'];
231 if ( !preg_match(
'/^((T-)?\d+|new)$/', $this->section ) ) {
232 $this->dieWithError(
'apierror-invalidsection' );
235 $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
281 if ( $params[
'redirects'] ) {
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 ) ] );
334 $revid = $params[
'revid'];
336 if ( $revid !==
null ) {
337 $rev = $this->revisionLookup->getRevisionById( $revid );
339 $this->dieWithError( [
'apierror-nosuchrevid', $revid ] );
341 $pTitleObj = $titleObj;
342 $titleObj = Title::newFromPageIdentity( $rev->getPage() );
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() );
413 if ( $params[
'pst'] || $params[
'onlypst'] ) {
414 $this->pstContent = $this->contentTransformer->preSaveTransform(
417 $this->getUserForPreview(),
421 if ( $params[
'onlypst'] ) {
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 );
443 if ( $params[
'pst'] ) {
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'] ) ) {
462 $skin = $this->skinFactory->makeSkin( Skin::normalizeKey( $params[
'useskin'] ) );
470 $skin || isset( $prop[
'subtitle'] ) || isset( $prop[
'headhtml'] ) || isset( $prop[
'categorieshtml'] ) ||
471 isset( $params[
'mobileformat'] )
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 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 );
555 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'text';
557 $this->getHookRunner()->onOutputPageBeforeHTML( $context->getOutput(), $result_array[
'text'] );
564 $outputPage->addParserOutputMetadata( $p_result );
567 if ( $params[
'summary'] !==
null ||
568 ( $params[
'sectiontitle'] !==
null && $this->section ===
'new' )
570 $result_array[
'parsedsummary'] = $this->formatSummary( $titleObj, $params );
571 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'parsedsummary';
574 if ( isset( $prop[
'langlinks'] ) ) {
576 $langlinks = $outputPage->getLanguageLinks();
578 $langlinks = array_map(
579 static fn ( $item ) => $item[
'link'],
580 $p_result->getLinkList( ParserOutputLinkTypes::LANGUAGE )
585 if ( $params[
'effectivelanglinks'] ) {
586 # for compatibility with old hook, convert to string[]
588 foreach ( $langlinks as $link ) {
589 $s = $link->getInterwiki() .
':' . $link->getText();
590 if ( $link->hasFragment() ) {
591 $s .=
'#' . $link->getFragment();
595 $langlinks = $compat;
597 $this->getHookRunner()->onLanguageLinks( $titleObj, $langlinks, $linkFlags );
601 $result_array[
'langlinks'] = $this->formatLangLinks( $langlinks );
603 if ( isset( $prop[
'categories'] ) ) {
604 $result_array[
'categories'] = $this->formatCategoryLinks( $p_result->getCategoryMap() );
606 if ( isset( $prop[
'categorieshtml'] ) ) {
607 $result_array[
'categorieshtml'] = $outputPage->getSkin()->getCategories();
608 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'categorieshtml';
610 if ( isset( $prop[
'links'] ) ) {
612 $result_array[
'links'] = $this->formatLinks( $p_result->getLinkList( ParserOutputLinkTypes::LOCAL ) );
614 if ( isset( $prop[
'templates'] ) ) {
615 $result_array[
'templates'] = $this->formatLinks(
616 $p_result->getLinkList( ParserOutputLinkTypes::TEMPLATE )
619 if ( isset( $prop[
'images'] ) ) {
620 $result_array[
'images'] = array_map(
621 static fn ( $item ) => $item[
'link']->getDBkey(),
622 $p_result->getLinkList( ParserOutputLinkTypes::MEDIA )
625 if ( isset( $prop[
'externallinks'] ) ) {
626 $result_array[
'externallinks'] = array_keys( $p_result->getExternalLinks() );
628 if ( isset( $prop[
'sections'] ) ) {
629 $result_array[
'sections'] = $p_result->getSections();
630 $result_array[
'showtoc'] = $p_result->getOutputFlag( ParserOutputFlags::SHOW_TOC );
632 if ( isset( $prop[
'parsewarnings'] ) ) {
633 $result_array[
'parsewarnings'] = $p_result->getWarnings();
635 if ( isset( $prop[
'parsewarningshtml'] ) ) {
636 $warnings = $p_result->getWarnings();
637 $warningsHtml = array_map(
static function ( $warning ) {
638 return (
new RawMessage(
'$1', [ $warning ] ) )->parse();
640 $result_array[
'parsewarningshtml'] = $warningsHtml;
643 if ( isset( $prop[
'displaytitle'] ) ) {
644 $result_array[
'displaytitle'] = $p_result->getDisplayTitle() !==
false
645 ? $p_result->getDisplayTitle()
646 : htmlspecialchars( $titleObj->getPrefixedText(), ENT_NOQUOTES );
649 if ( isset( $prop[
'subtitle'] ) ) {
651 $result_array[
'subtitle'] = $context->getSkin()->prepareSubtitle(
false );
654 if ( isset( $prop[
'headitems'] ) ) {
656 $result_array[
'headitems'] = $this->formatHeadItems( $outputPage->getHeadItemsArray() );
658 $result_array[
'headitems'] = $this->formatHeadItems( $p_result->getHeadItems() );
662 if ( isset( $prop[
'headhtml'] ) ) {
663 $result_array[
'headhtml'] = $outputPage->headElement( $context->getSkin() );
664 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'headhtml';
667 if ( isset( $prop[
'modules'] ) ) {
669 $result_array[
'modules'] = $outputPage->getModules();
671 $result_array[
'modulescripts'] = [];
672 $result_array[
'modulestyles'] = $outputPage->getModuleStyles();
674 $result_array[
'modules'] = array_values( array_unique( $p_result->getModules() ) );
676 $result_array[
'modulescripts'] = [];
677 $result_array[
'modulestyles'] = array_values( array_unique( $p_result->getModuleStyles() ) );
681 if ( isset( $prop[
'jsconfigvars'] ) ) {
682 $showStrategyKeys = (bool)( $params[
'showstrategykeys'] );
683 $jsconfigvars = $skin ? $outputPage->getJsConfigVars() : $p_result->getJsConfigVars( $showStrategyKeys );
684 $result_array[
'jsconfigvars'] = ApiResult::addMetadataToResultVars( $jsconfigvars );
687 if ( isset( $prop[
'encodedjsconfigvars'] ) ) {
688 $jsconfigvars = $skin ? $outputPage->getJsConfigVars() : $p_result->getJsConfigVars();
689 $result_array[
'encodedjsconfigvars'] = FormatJson::encode(
694 $result_array[ApiResult::META_SUBELEMENTS][] =
'encodedjsconfigvars';
697 if ( isset( $prop[
'modules'] ) &&
698 !isset( $prop[
'jsconfigvars'] ) && !isset( $prop[
'encodedjsconfigvars'] ) ) {
699 $this->addWarning(
'apiwarn-moduleswithoutvars' );
702 if ( isset( $prop[
'indicators'] ) ) {
704 $result_array[
'indicators'] = $outputPage->getIndicators();
706 $result_array[
'indicators'] = $p_result->getIndicators();
708 ApiResult::setArrayType( $result_array[
'indicators'],
'BCkvp',
'name' );
711 if ( isset( $prop[
'iwlinks'] ) ) {
713 static fn ( $item ) => $item[
'link'],
714 $p_result->getLinkList( ParserOutputLinkTypes::INTERWIKI )
716 $result_array[
'iwlinks'] = $this->formatIWLinks( $links );
719 if ( isset( $prop[
'wikitext'] ) ) {
720 $result_array[
'wikitext'] = $this->content->serialize( $format );
721 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'wikitext';
723 if ( $this->pstContent !==
null ) {
724 $result_array[
'psttext'] = $this->pstContent->serialize( $format );
725 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'psttext';
728 if ( isset( $prop[
'properties'] ) ) {
729 $result_array[
'properties'] = $p_result->getPageProperties();
730 ApiResult::setArrayType( $result_array[
'properties'],
'BCkvp',
'name' );
733 if ( isset( $prop[
'limitreportdata'] ) ) {
734 $result_array[
'limitreportdata'] =
735 $this->formatLimitReportData( $p_result->getLimitReportData() );
737 if ( isset( $prop[
'limitreporthtml'] ) ) {
738 $result_array[
'limitreporthtml'] = EditPage::getPreviewLimitReport( $p_result );
739 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'limitreporthtml';
742 if ( isset( $prop[
'parsetree'] ) || $params[
'generatexml'] ) {
744 $this->dieWithError(
'apierror-parsetree-notwikitext',
'notwikitext' );
747 $parser = $this->parserFactory->getInstance();
748 $parser->startExternalParse( $titleObj, $popts, Parser::OT_PREPROCESS );
750 $xml = $parser->preprocessToDom( $this->content->getText() )->__toString();
751 $result_array[
'parsetree'] = $xml;
752 $result_array[ApiResult::META_BC_SUBELEMENTS][] =
'parsetree';
758 'categories' =>
'cl',
762 'externallinks' =>
'el',
767 'indicators' =>
'ind',
768 'modulescripts' =>
'm',
769 'modulestyles' =>
'm',
770 'properties' =>
'pp',
771 'limitreportdata' =>
'lr',
772 'parsewarnings' =>
'pw',
773 'parsewarningshtml' =>
'pw',
775 $this->setIndexedTagNames( $result_array, $result_mapping );
776 $result->addValue(
null, $this->getModuleName(), $result_array );