59 $this->
getMain()->setCacheMode(
'anon-public-user-private' );
66 if ( $title ===
null ) {
67 $titleProvided =
false;
71 $titleProvided =
true;
74 if (
$params[
'prop'] ===
null ) {
76 [
'apiwarn-deprecation-missingparam',
'prop' ],
'action=expandtemplates&!prop'
80 $prop = array_fill_keys(
$params[
'prop'],
true );
83 $titleObj = Title::newFromText( $title );
84 if ( !$titleObj || $titleObj->isExternal() ) {
90 if ( $revid !==
null ) {
91 $rev = $this->revisionStore->getRevisionById( $revid );
93 $this->
dieWithError( [
'apierror-nosuchrevid', $revid ] );
95 $pTitleObj = $titleObj;
96 $titleObj = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
97 if ( $titleProvided && !$titleObj->equals( $pTitleObj ) ) {
98 $this->
addWarning( [
'apierror-revwrongpage', $rev->getId(),
106 $options = ParserOptions::newFromContext( $this->
getContext() );
108 if (
$params[
'includecomments'] ) {
109 $options->setRemoveComments(
false );
113 $suppressCache =
false;
115 $options, $titleObj,
$params, $this, $reset, $suppressCache );
119 if ( isset( $prop[
'parsetree'] ) ||
$params[
'generatexml'] ) {
120 $parser = $this->parserFactory->getInstance();
121 $parser->startExternalParse( $titleObj, $options, Parser::OT_PREPROCESS );
122 $dom = $parser->preprocessToDom(
$params[
'text'] );
124 if ( is_callable( [ $dom,
'saveXML' ] ) ) {
126 $xml = $dom->saveXML();
129 $xml = $dom->__toString();
131 if ( isset( $prop[
'parsetree'] ) ) {
132 unset( $prop[
'parsetree'] );
133 $retval[
'parsetree'] = $xml;
136 $result->addValue(
null,
'parsetree', $xml );
137 $result->addValue(
null, ApiResult::META_BC_SUBELEMENTS, [
'parsetree' ] );
143 if ( $prop ||
$params[
'prop'] ===
null ) {
144 $parser = $this->parserFactory->getInstance();
145 $parser->startExternalParse( $titleObj, $options, Parser::OT_PREPROCESS );
146 $frame = $parser->getPreprocessor()->newFrame();
147 $wikitext = $parser->preprocess(
$params[
'text'], $titleObj, $options, $revid, $frame );
148 if (
$params[
'prop'] ===
null ) {
150 ApiResult::setContentValue( $retval,
'wikitext', $wikitext );
152 $p_output = $parser->getOutput();
153 if ( isset( $prop[
'categories'] ) ) {
154 $categories = $p_output->getCategoryNames();
156 $defaultSortKey = $p_output->getPageProperty(
'defaultsort' ) ??
'';
157 $categories_result = [];
158 foreach ( $categories as $category ) {
163 'sortkey' => $p_output->getCategorySortKey( $category ) ?: $defaultSortKey,
165 ApiResult::setContentValue( $entry,
'category', $category );
166 $categories_result[] = $entry;
168 ApiResult::setIndexedTagName( $categories_result,
'category' );
169 $retval[
'categories'] = $categories_result;
172 if ( isset( $prop[
'properties'] ) ) {
173 $properties = $p_output->getPageProperties();
175 ApiResult::setArrayType( $properties,
'BCkvp',
'name' );
176 ApiResult::setIndexedTagName( $properties,
'property' );
177 $retval[
'properties'] = $properties;
180 if ( isset( $prop[
'volatile'] ) ) {
181 $retval[
'volatile'] = $frame->isVolatile();
183 if ( isset( $prop[
'ttl'] ) && $frame->getTTL() !==
null ) {
184 $retval[
'ttl'] = $frame->getTTL();
186 if ( isset( $prop[
'wikitext'] ) ) {
187 $retval[
'wikitext'] = $wikitext;
189 if ( isset( $prop[
'modules'] ) ) {
190 $retval[
'modules'] = array_values( array_unique( $p_output->getModules() ) );
192 $retval[
'modulescripts'] = [];
193 $retval[
'modulestyles'] = array_values( array_unique( $p_output->getModuleStyles() ) );
195 if ( isset( $prop[
'jsconfigvars'] ) ) {
196 $showStrategyKeys = (bool)(
$params[
'showstrategykeys'] );
197 $retval[
'jsconfigvars'] =
198 ApiResult::addMetadataToResultVars( $p_output->getJsConfigVars( $showStrategyKeys ) );
200 if ( isset( $prop[
'encodedjsconfigvars'] ) ) {
201 $retval[
'encodedjsconfigvars'] = FormatJson::encode(
202 $p_output->getJsConfigVars(),
false, FormatJson::ALL_OK
204 $retval[ApiResult::META_SUBELEMENTS][] =
'encodedjsconfigvars';
206 if ( isset( $prop[
'modules'] ) &&
207 !isset( $prop[
'jsconfigvars'] ) && !isset( $prop[
'encodedjsconfigvars'] ) ) {
208 $this->
addWarning(
'apiwarn-moduleswithoutvars' );
212 ApiResult::setSubelementsList( $retval, [
'wikitext',
'parsetree' ] );