58 $this->
getMain()->setCacheMode(
'anon-public-user-private' );
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() ) {
88 $revid = $params[
'revid'];
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 ) {
97 if ( !$titleObj->equals( $pTitleObj ) ) {
98 $this->
addWarning( [
'apierror-revwrongpage', $rev->getId(),
107 $options = ParserOptions::newFromContext( $this->
getContext() );
109 if ( $params[
'includecomments'] ) {
110 $options->setRemoveComments(
false );
114 $suppressCache =
false;
116 $options, $titleObj, $params, $this, $reset, $suppressCache );
120 if ( isset( $prop[
'parsetree'] ) || $params[
'generatexml'] ) {
122 $dom = $this->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 ) {
144 $frame = $this->parser->getPreprocessor()->newFrame();
145 $wikitext = $this->parser->preprocess( $params[
'text'], $titleObj, $options, $revid, $frame );
146 if ( $params[
'prop'] ===
null ) {
148 ApiResult::setContentValue( $retval,
'wikitext', $wikitext );
150 $p_output = $this->parser->getOutput();
151 if ( isset( $prop[
'categories'] ) ) {
152 $categories = $p_output->getCategories();
154 $categories_result = [];
155 foreach ( $categories as $category => $sortkey ) {
157 $entry[
'sortkey'] = $sortkey;
158 ApiResult::setContentValue( $entry,
'category', (
string)$category );
159 $categories_result[] = $entry;
161 ApiResult::setIndexedTagName( $categories_result,
'category' );
162 $retval[
'categories'] = $categories_result;
165 if ( isset( $prop[
'properties'] ) ) {
166 $properties = $p_output->getProperties();
168 ApiResult::setArrayType( $properties,
'BCkvp',
'name' );
169 ApiResult::setIndexedTagName( $properties,
'property' );
170 $retval[
'properties'] = $properties;
173 if ( isset( $prop[
'volatile'] ) ) {
174 $retval[
'volatile'] = $frame->isVolatile();
176 if ( isset( $prop[
'ttl'] ) && $frame->getTTL() !==
null ) {
177 $retval[
'ttl'] = $frame->getTTL();
179 if ( isset( $prop[
'wikitext'] ) ) {
180 $retval[
'wikitext'] = $wikitext;
182 if ( isset( $prop[
'modules'] ) ) {
183 $retval[
'modules'] = array_values( array_unique( $p_output->getModules() ) );
185 $retval[
'modulescripts'] = [];
186 $retval[
'modulestyles'] = array_values( array_unique( $p_output->getModuleStyles() ) );
188 if ( isset( $prop[
'jsconfigvars'] ) ) {
189 $retval[
'jsconfigvars'] =
190 ApiResult::addMetadataToResultVars( $p_output->getJsConfigVars() );
192 if ( isset( $prop[
'encodedjsconfigvars'] ) ) {
193 $retval[
'encodedjsconfigvars'] = FormatJson::encode(
194 $p_output->getJsConfigVars(),
false, FormatJson::ALL_OK
196 $retval[ApiResult::META_SUBELEMENTS][] =
'encodedjsconfigvars';
198 if ( isset( $prop[
'modules'] ) &&
199 !isset( $prop[
'jsconfigvars'] ) && !isset( $prop[
'encodedjsconfigvars'] ) ) {
200 $this->
addWarning(
'apiwarn-moduleswithoutvars' );
204 ApiResult::setSubelementsList( $retval, [
'wikitext',
'parsetree' ] );