From 8a4c74a01cec7b1e1da6400a0e44f8f6467ba097 Mon Sep 17 00:00:00 2001 From: "James D. Forrester" Date: Fri, 7 Nov 2025 17:36:26 -0500 Subject: [PATCH] ApiParse: Introduce prop=tocdata as replacement for prop=sections Bug: T328605 Change-Id: I11aaee951af1654685bae6463fbb856e9e873789 (cherry picked from commit 62ceaf809dccde76d418e35c46468d54cd15021b) --- includes/api/ApiMain.php | 1 + includes/api/ApiParse.php | 18 ++++++++++++++++-- includes/api/i18n/en.json | 1 + includes/api/i18n/qqq.json | 1 + 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index 58810a91b89..3976ae0cb70 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -178,6 +178,7 @@ class ApiMain extends ApiBase { 'UserFactory', 'UrlUtils', 'TitleFormatter', + 'JsonCodec', ] ], 'stashedit' => [ diff --git a/includes/api/ApiParse.php b/includes/api/ApiParse.php index 0417a886b37..d62de867f60 100644 --- a/includes/api/ApiParse.php +++ b/includes/api/ApiParse.php @@ -33,6 +33,7 @@ use MediaWiki\Content\Transform\ContentTransformer; use MediaWiki\Context\DerivativeContext; use MediaWiki\EditPage\EditPage; use MediaWiki\Json\FormatJson; +use MediaWiki\Json\JsonCodec; use MediaWiki\Language\RawMessage; use MediaWiki\Languages\LanguageNameUtils; use MediaWiki\Message\Message; @@ -61,6 +62,7 @@ use Skin; use SkinFactory; use Wikimedia\ParamValidator\ParamValidator; use Wikimedia\ParamValidator\TypeDef\EnumDef; +use Wikimedia\Parsoid\Core\TOCData; use WikiPage; /** @@ -95,6 +97,7 @@ class ApiParse extends ApiBase { private UserFactory $userFactory; private UrlUtils $urlUtils; private TitleFormatter $titleFormatter; + private JsonCodec $jsonCodec; public function __construct( ApiMain $main, @@ -113,7 +116,8 @@ class ApiParse extends ApiBase { TempUserCreator $tempUserCreator, UserFactory $userFactory, UrlUtils $urlUtils, - TitleFormatter $titleFormatter + TitleFormatter $titleFormatter, + JsonCodec $jsonCodec ) { parent::__construct( $main, $action ); $this->revisionLookup = $revisionLookup; @@ -131,6 +135,7 @@ class ApiParse extends ApiBase { $this->userFactory = $userFactory; $this->urlUtils = $urlUtils; $this->titleFormatter = $titleFormatter; + $this->jsonCodec = $jsonCodec; } private function getPoolKey(): string { @@ -605,6 +610,13 @@ class ApiParse extends ApiBase { } if ( isset( $prop['sections'] ) ) { $result_array['sections'] = $p_result->getSections(); + } + if ( isset( $prop['tocdata'] ) ) { + $result_array['tocdata'] = $this->jsonCodec->toJsonArray( + $p_result->getTOCData(), TOCData::class + ); + } + if ( isset( $prop['sections'] ) || isset( $prop['tocdata'] ) ) { $result_array['showtoc'] = $p_result->getOutputFlag( ParserOutputFlags::SHOW_TOC ); } if ( isset( $prop['parsewarnings'] ) ) { @@ -736,6 +748,7 @@ class ApiParse extends ApiBase { 'externallinks' => 'el', 'iwlinks' => 'iw', 'sections' => 's', + 'tocdata' => 'toc', 'headitems' => 'hi', 'modules' => 'm', 'indicators' => 'ind', @@ -1081,7 +1094,7 @@ class ApiParse extends ApiBase { ], 'prop' => [ ParamValidator::PARAM_DEFAULT => 'text|langlinks|categories|links|templates|' . - 'images|externallinks|sections|revid|displaytitle|iwlinks|' . + 'images|externallinks|sections|tocdata|revid|displaytitle|iwlinks|' . 'properties|parsewarnings', ParamValidator::PARAM_ISMULTI => true, ParamValidator::PARAM_TYPE => [ @@ -1094,6 +1107,7 @@ class ApiParse extends ApiBase { 'images', 'externallinks', 'sections', + 'tocdata', 'revid', 'displaytitle', 'subtitle', diff --git a/includes/api/i18n/en.json b/includes/api/i18n/en.json index 699a0490e44..4d3b7e85511 100644 --- a/includes/api/i18n/en.json +++ b/includes/api/i18n/en.json @@ -408,6 +408,7 @@ "apihelp-parse-paramvalue-prop-images": "Gives the images in the parsed wikitext.", "apihelp-parse-paramvalue-prop-externallinks": "Gives the external links in the parsed wikitext.", "apihelp-parse-paramvalue-prop-sections": "Gives the sections in the parsed wikitext.", + "apihelp-parse-paramvalue-prop-tocdata": "Gives the table of contents information in the parsed wikitext.", "apihelp-parse-paramvalue-prop-revid": "Adds the revision ID of the parsed page.", "apihelp-parse-paramvalue-prop-displaytitle": "Adds the title of the parsed wikitext.", "apihelp-parse-paramvalue-prop-subtitle": "Adds the page subtitle for the parsed page.", diff --git a/includes/api/i18n/qqq.json b/includes/api/i18n/qqq.json index e5681d37604..ef7e4bcccdd 100644 --- a/includes/api/i18n/qqq.json +++ b/includes/api/i18n/qqq.json @@ -396,6 +396,7 @@ "apihelp-parse-paramvalue-prop-images": "{{doc-apihelp-paramvalue|parse|prop|images}}", "apihelp-parse-paramvalue-prop-externallinks": "{{doc-apihelp-paramvalue|parse|prop|externallinks}}", "apihelp-parse-paramvalue-prop-sections": "{{doc-apihelp-paramvalue|parse|prop|sections}}", + "apihelp-parse-paramvalue-prop-tocdata": "{{doc-apihelp-paramvalue|parse|prop|tocdata}}", "apihelp-parse-paramvalue-prop-revid": "{{doc-apihelp-paramvalue|parse|prop|revid}}", "apihelp-parse-paramvalue-prop-displaytitle": "{{doc-apihelp-paramvalue|parse|prop|displaytitle}}", "apihelp-parse-paramvalue-prop-subtitle": "{{doc-apihelp-paramvalue|parse|prop|subtitle}}",