ApiParse: Introduce prop=tocdata as replacement for prop=sections

Bug: T328605
Change-Id: I11aaee951af1654685bae6463fbb856e9e873789
(cherry picked from commit 62ceaf809dccde76d418e35c46468d54cd15021b)
This commit is contained in:
James D. Forrester 2025-11-07 17:36:26 -05:00 committed by jenkins-bot
parent e8e8d5ed3d
commit 8a4c74a01c
4 changed files with 19 additions and 2 deletions

View file

@ -178,6 +178,7 @@ class ApiMain extends ApiBase {
'UserFactory',
'UrlUtils',
'TitleFormatter',
'JsonCodec',
]
],
'stashedit' => [

View file

@ -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',

View file

@ -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.",

View file

@ -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}}",