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' );
211 $text = $params[
'text'];
212 $title = $params[
'title'];
213 if ( $title ===
null ) {
214 $titleProvided =
false;
218 $titleProvided =
true;
221 $page = $params[
'page'];
222 $pageid = $params[
'pageid'];
223 $oldid = $params[
'oldid'];
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
279 if ( $params[
'redirects'] ) {
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 ) ] );
332 $revid = $params[
'revid'];
334 if ( $revid !==
null ) {
335 $rev = $this->revisionLookup->getRevisionById( $revid );
337 $this->dieWithError( [
'apierror-nosuchrevid', $revid ] );
339 $pTitleObj = $titleObj;
340 $titleObj = Title::newFromPageIdentity( $rev->getPage() );
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() );
411 if ( $params[
'pst'] || $params[
'onlypst'] ) {
412 $this->pstContent = $this->contentTransformer->preSaveTransform(
415 $this->getUserForPreview(),
419 if ( $params[
'onlypst'] ) {
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 );
441 if ( $params[
'pst'] ) {
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'] ) ) {
460 $skin = $this->skinFactory->makeSkin( Skin::normalizeKey( $params[
'useskin'] ) );
468 $skin || isset( $prop[
'subtitle'] ) || isset( $prop[
'headhtml'] ) || isset( $prop[
'categorieshtml'] ) ||
469 isset( $params[
'mobileformat'] )
485 $context->setTitle( $titleObj );
488 $context->setWikiPage( $pageObj );
492 $context->setRequest(
new FauxRequest( [
'action' =>
'view' ] ) );
496 $context->setSkin( $skin );
498 $skin = $context->getSkin();
503 $context->setSkin( $context->getSkin() );
508 $outputPage->setArticleFlag(
true );
510 $outputPage->addParserOutputMetadata( $p_result );
511 if ( $this->content ) {
512 $outputPage->addContentOverride( $titleObj, $this->content );
514 $context->setOutput( $outputPage );
518 $outputPage->loadSkinModules( $skin );
521 $this->getHookRunner()->onApiParseMakeOutputPage( $this, $outputPage );
524 if ( $oldid !==
null ) {
525 $result_array[
'revid'] = (int)$oldid;
528 if ( $params[
'redirects'] && $redirValues !==
null ) {
529 $result_array[
'redirects'] = $redirValues;
532 if ( isset( $prop[
'text'] ) ) {
533 $skin = $context ? $context->getSkin() :
null;
534 $skinOptions = $skin ? $skin->getOptions() : [
541 $oldText = $p_result->getRawText();
542 $result_array[
'text'] = $p_result->runOutputPipeline( $popts, [
543 'allowClone' =>
false,
544 'allowTOC' => !$params[
'disabletoc'],
545 'injectTOC' => $skinOptions[
'toc'],
546 'enableSectionEditLinks' => !$params[
'disableeditsection'],
547 'wrapperDivClass' => $params[
'wrapoutputclass'],
548 'deduplicateStyles' => !$params[
'disablestylededuplication'],
549 'userLang' => $context ? $context->getLanguage() :
null,
551 'includeDebugInfo' => !$params[
'disablepp'] && !$params[
'disablelimitreport']
552 ] )->getContentHolderText();
553 $p_result->setRawText( $oldText );
556 $this->getHookRunner()->onOutputPageBeforeHTML( $context->getOutput(), $result_array[
'text'] );
560 if ( $params[
'summary'] !==
null ||
561 ( $params[
'sectiontitle'] !==
null && $this->section ===
'new' )
563 $result_array[
'parsedsummary'] = $this->formatSummary( $titleObj, $params );
567 if ( isset( $prop[
'langlinks'] ) ) {
569 $langlinks = $outputPage->getLanguageLinks();
571 $langlinks = array_map(
572 static fn ( $item ) => $item[
'link'],
573 $p_result->getLinkList( ParserOutputLinkTypes::LANGUAGE )
578 if ( $params[
'effectivelanglinks'] ) {
579 # for compatibility with old hook, convert to string[]
581 foreach ( $langlinks as $link ) {
582 $s = $link->getInterwiki() .
':' . $link->getText();
583 if ( $link->hasFragment() ) {
584 $s .=
'#' . $link->getFragment();
588 $langlinks = $compat;
590 $this->getHookRunner()->onLanguageLinks( $titleObj, $langlinks, $linkFlags );
594 $result_array[
'langlinks'] = $this->formatLangLinks( $langlinks );
596 if ( isset( $prop[
'categories'] ) ) {
597 $result_array[
'categories'] = $this->formatCategoryLinks( $p_result->getCategoryMap() );
599 if ( isset( $prop[
'categorieshtml'] ) ) {
600 $result_array[
'categorieshtml'] = $outputPage->getSkin()->getCategories();
603 if ( isset( $prop[
'links'] ) ) {
605 $result_array[
'links'] = $this->formatLinks( $p_result->getLinkList( ParserOutputLinkTypes::LOCAL ) );
607 if ( isset( $prop[
'templates'] ) ) {
608 $result_array[
'templates'] = $this->formatLinks(
609 $p_result->getLinkList( ParserOutputLinkTypes::TEMPLATE )
612 if ( isset( $prop[
'images'] ) ) {
613 $result_array[
'images'] = array_map(
614 static fn ( $item ) => $item[
'link']->getDBkey(),
615 $p_result->getLinkList( ParserOutputLinkTypes::MEDIA )
618 if ( isset( $prop[
'externallinks'] ) ) {
619 $result_array[
'externallinks'] = array_keys( $p_result->getExternalLinks() );
621 if ( isset( $prop[
'sections'] ) ) {
622 $result_array[
'sections'] = $p_result->getSections();
623 $result_array[
'showtoc'] = $p_result->getOutputFlag( ParserOutputFlags::SHOW_TOC );
625 if ( isset( $prop[
'parsewarnings'] ) ) {
626 $result_array[
'parsewarnings'] = $p_result->getWarnings();
628 if ( isset( $prop[
'parsewarningshtml'] ) ) {
629 $warnings = $p_result->getWarnings();
630 $warningsHtml = array_map(
static function ( $warning ) {
631 return (
new RawMessage(
'$1', [ $warning ] ) )->parse();
633 $result_array[
'parsewarningshtml'] = $warningsHtml;
636 if ( isset( $prop[
'displaytitle'] ) ) {
637 $result_array[
'displaytitle'] = $p_result->getDisplayTitle() !==
false
638 ? $p_result->getDisplayTitle()
639 : htmlspecialchars( $titleObj->getPrefixedText(), ENT_NOQUOTES );
642 if ( isset( $prop[
'subtitle'] ) ) {
644 $result_array[
'subtitle'] = $context->getSkin()->prepareSubtitle(
false );
647 if ( isset( $prop[
'headitems'] ) ) {
649 $result_array[
'headitems'] = $this->formatHeadItems( $outputPage->getHeadItemsArray() );
651 $result_array[
'headitems'] = $this->formatHeadItems( $p_result->getHeadItems() );
655 if ( isset( $prop[
'headhtml'] ) ) {
656 $result_array[
'headhtml'] = $outputPage->headElement( $context->getSkin() );
660 if ( isset( $prop[
'modules'] ) ) {
662 $result_array[
'modules'] = $outputPage->getModules();
664 $result_array[
'modulescripts'] = [];
665 $result_array[
'modulestyles'] = $outputPage->getModuleStyles();
667 $result_array[
'modules'] = array_values( array_unique( $p_result->getModules() ) );
669 $result_array[
'modulescripts'] = [];
670 $result_array[
'modulestyles'] = array_values( array_unique( $p_result->getModuleStyles() ) );
674 if ( isset( $prop[
'jsconfigvars'] ) ) {
675 $showStrategyKeys = (bool)( $params[
'showstrategykeys'] );
676 $jsconfigvars = $skin ? $outputPage->getJsConfigVars() : $p_result->getJsConfigVars( $showStrategyKeys );
680 if ( isset( $prop[
'encodedjsconfigvars'] ) ) {
681 $jsconfigvars = $skin ? $outputPage->getJsConfigVars() : $p_result->getJsConfigVars();
682 $result_array[
'encodedjsconfigvars'] = FormatJson::encode(
690 if ( isset( $prop[
'modules'] ) &&
691 !isset( $prop[
'jsconfigvars'] ) && !isset( $prop[
'encodedjsconfigvars'] ) ) {
692 $this->addWarning(
'apiwarn-moduleswithoutvars' );
695 if ( isset( $prop[
'indicators'] ) ) {
697 $result_array[
'indicators'] = $outputPage->getIndicators();
699 $result_array[
'indicators'] = $p_result->getIndicators();
704 if ( isset( $prop[
'iwlinks'] ) ) {
706 static fn ( $item ) => $item[
'link'],
707 $p_result->getLinkList( ParserOutputLinkTypes::INTERWIKI )
709 $result_array[
'iwlinks'] = $this->formatIWLinks( $links );
712 if ( isset( $prop[
'wikitext'] ) ) {
713 $result_array[
'wikitext'] = $this->content->serialize( $format );
716 if ( $this->pstContent !==
null ) {
717 $result_array[
'psttext'] = $this->pstContent->serialize( $format );
721 if ( isset( $prop[
'properties'] ) ) {
722 $result_array[
'properties'] = $p_result->getPageProperties();
726 if ( isset( $prop[
'limitreportdata'] ) ) {
727 $result_array[
'limitreportdata'] =
728 $this->formatLimitReportData( $p_result->getLimitReportData() );
730 if ( isset( $prop[
'limitreporthtml'] ) ) {
731 $result_array[
'limitreporthtml'] = EditPage::getPreviewLimitReport( $p_result );
735 if ( isset( $prop[
'parsetree'] ) || $params[
'generatexml'] ) {
737 $this->dieWithError(
'apierror-parsetree-notwikitext',
'notwikitext' );
740 $parser = $this->parserFactory->getInstance();
741 $parser->startExternalParse( $titleObj, $popts, Parser::OT_PREPROCESS );
743 $xml = $parser->preprocessToDom( $this->content->getText() )->__toString();
744 $result_array[
'parsetree'] = $xml;
751 'categories' =>
'cl',
755 'externallinks' =>
'el',
760 'indicators' =>
'ind',
761 'modulescripts' =>
'm',
762 'modulestyles' =>
'm',
763 'properties' =>
'pp',
764 'limitreportdata' =>
'lr',
765 'parsewarnings' =>
'pw',
766 'parsewarningshtml' =>
'pw',
768 $this->setIndexedTagNames( $result_array, $result_mapping );
769 $result->addValue(
null, $this->getModuleName(), $result_array );