58 $this->
getMain()->setCacheMode(
'anon-public-user-private' );
65 if ( $title ===
null ) {
66 $titleProvided =
false;
70 $titleProvided =
true;
73 if (
$params[
'prop'] ===
null ) {
75 [
'apiwarn-deprecation-missingparam',
'prop' ],
'action=expandtemplates&!prop'
79 $prop = array_fill_keys(
$params[
'prop'],
true );
82 $titleObj = Title::newFromText( $title );
83 if ( !$titleObj || $titleObj->isExternal() ) {
89 if ( $revid !==
null ) {
90 $rev = $this->revisionStore->getRevisionById( $revid );
92 $this->
dieWithError( [
'apierror-nosuchrevid', $revid ] );
94 $pTitleObj = $titleObj;
95 $titleObj = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
96 if ( $titleProvided && !$titleObj->equals( $pTitleObj ) ) {
97 $this->
addWarning( [
'apierror-revwrongpage', $rev->getId(),
105 $options = ParserOptions::newFromContext( $this->
getContext() );
107 if (
$params[
'includecomments'] ) {
108 $options->setRemoveComments(
false );
112 $suppressCache =
false;
114 $options, $titleObj,
$params, $this, $reset, $suppressCache );
118 if ( isset( $prop[
'parsetree'] ) ||
$params[
'generatexml'] ) {
119 $parser = $this->parserFactory->getInstance();
120 $parser->startExternalParse( $titleObj, $options, Parser::OT_PREPROCESS );
121 $dom = $parser->preprocessToDom(
$params[
'text'] );
123 if ( is_callable( [ $dom,
'saveXML' ] ) ) {
125 $xml = $dom->saveXML();
128 $xml = $dom->__toString();
130 if ( isset( $prop[
'parsetree'] ) ) {
131 unset( $prop[
'parsetree'] );
132 $retval[
'parsetree'] = $xml;
135 $result->addValue(
null,
'parsetree', $xml );
136 $result->addValue(
null, ApiResult::META_BC_SUBELEMENTS, [
'parsetree' ] );
142 if ( $prop ||
$params[
'prop'] ===
null ) {
143 $parser = $this->parserFactory->getInstance();
144 $parser->startExternalParse( $titleObj, $options, Parser::OT_PREPROCESS );
145 $frame = $parser->getPreprocessor()->newFrame();
146 $wikitext = $parser->preprocess(
$params[
'text'], $titleObj, $options, $revid, $frame );
147 if (
$params[
'prop'] ===
null ) {
149 ApiResult::setContentValue( $retval,
'wikitext', $wikitext );
151 $p_output = $parser->getOutput();
152 if ( isset( $prop[
'categories'] ) ) {
153 $categories = $p_output->getCategoryNames();
155 $defaultSortKey = $p_output->getPageProperty(
'defaultsort' ) ??
'';
156 $categories_result = [];
157 foreach ( $categories as $category ) {
162 'sortkey' => $p_output->getCategorySortKey( $category ) ?: $defaultSortKey,
164 ApiResult::setContentValue( $entry,
'category', $category );
165 $categories_result[] = $entry;
167 ApiResult::setIndexedTagName( $categories_result,
'category' );
168 $retval[
'categories'] = $categories_result;
171 if ( isset( $prop[
'properties'] ) ) {
172 $properties = $p_output->getPageProperties();
174 ApiResult::setArrayType( $properties,
'BCkvp',
'name' );
175 ApiResult::setIndexedTagName( $properties,
'property' );
176 $retval[
'properties'] = $properties;
179 if ( isset( $prop[
'volatile'] ) ) {
180 $retval[
'volatile'] = $frame->isVolatile();
182 if ( isset( $prop[
'ttl'] ) && $frame->getTTL() !==
null ) {
183 $retval[
'ttl'] = $frame->getTTL();
185 if ( isset( $prop[
'wikitext'] ) ) {
186 $retval[
'wikitext'] = $wikitext;
188 if ( isset( $prop[
'modules'] ) ) {
189 $retval[
'modules'] = array_values( array_unique( $p_output->getModules() ) );
191 $retval[
'modulescripts'] = [];
192 $retval[
'modulestyles'] = array_values( array_unique( $p_output->getModuleStyles() ) );
194 if ( isset( $prop[
'jsconfigvars'] ) ) {
195 $showStrategyKeys = (bool)(
$params[
'showstrategykeys'] );
196 $retval[
'jsconfigvars'] =
197 ApiResult::addMetadataToResultVars( $p_output->getJsConfigVars( $showStrategyKeys ) );
199 if ( isset( $prop[
'encodedjsconfigvars'] ) ) {
200 $retval[
'encodedjsconfigvars'] = FormatJson::encode(
201 $p_output->getJsConfigVars(),
false, FormatJson::ALL_OK
203 $retval[ApiResult::META_SUBELEMENTS][] =
'encodedjsconfigvars';
205 if ( isset( $prop[
'modules'] ) &&
206 !isset( $prop[
'jsconfigvars'] ) && !isset( $prop[
'encodedjsconfigvars'] ) ) {
207 $this->
addWarning(
'apiwarn-moduleswithoutvars' );
211 ApiResult::setSubelementsList( $retval, [
'wikitext',
'parsetree' ] );