36 $this->
getMain()->setCacheMode(
'anon-public-user-private' );
44 $titleProvided =
false;
48 $titleProvided =
true;
51 if ( $params[
'prop'] ===
null ) {
53 [
'apiwarn-deprecation-missingparam',
'prop' ],
'action=expandtemplates&!prop'
57 $prop = array_flip( $params[
'prop'] );
60 $titleObj = Title::newFromText(
$title );
61 if ( !$titleObj || $titleObj->isExternal() ) {
66 $revid = $params[
'revid'];
67 if ( $revid !==
null ) {
68 $rev = MediaWikiServices::getInstance()->getRevisionStore()->getRevisionById( $revid );
70 $this->
dieWithError( [
'apierror-nosuchrevid', $revid ] );
72 $pTitleObj = $titleObj;
73 $titleObj = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
74 if ( $titleProvided ) {
75 if ( !$titleObj->equals( $pTitleObj ) ) {
76 $this->
addWarning( [
'apierror-revwrongpage', $rev->getId(),
85 $options = ParserOptions::newFromContext( $this->
getContext() );
87 if ( $params[
'includecomments'] ) {
88 $options->setRemoveComments(
false );
92 $suppressCache =
false;
94 $options, $titleObj, $params, $this, $reset, $suppressCache );
98 $parser = MediaWikiServices::getInstance()->getParser();
99 if ( isset( $prop[
'parsetree'] ) || $params[
'generatexml'] ) {
100 $parser->startExternalParse( $titleObj, $options, Parser::OT_PREPROCESS );
101 $dom = $parser->preprocessToDom( $params[
'text'] );
102 if ( is_callable( [ $dom,
'saveXML' ] ) ) {
104 $xml = $dom->saveXML();
107 $xml = $dom->__toString();
109 if ( isset( $prop[
'parsetree'] ) ) {
110 unset( $prop[
'parsetree'] );
111 $retval[
'parsetree'] = $xml;
114 $result->addValue(
null,
'parsetree', $xml );
115 $result->addValue(
null, ApiResult::META_BC_SUBELEMENTS, [
'parsetree' ] );
121 if ( $prop || $params[
'prop'] ===
null ) {
122 $parser->startExternalParse( $titleObj, $options, Parser::OT_PREPROCESS );
123 $frame = $parser->getPreprocessor()->newFrame();
124 $wikitext = $parser->preprocess( $params[
'text'], $titleObj, $options, $revid, $frame );
125 if ( $params[
'prop'] ===
null ) {
127 ApiResult::setContentValue( $retval,
'wikitext', $wikitext );
129 $p_output = $parser->getOutput();
130 if ( isset( $prop[
'categories'] ) ) {
131 $categories = $p_output->getCategories();
133 $categories_result = [];
134 foreach ( $categories as $category => $sortkey ) {
136 $entry[
'sortkey'] = $sortkey;
137 ApiResult::setContentValue( $entry,
'category', (
string)$category );
138 $categories_result[] = $entry;
140 ApiResult::setIndexedTagName( $categories_result,
'category' );
141 $retval[
'categories'] = $categories_result;
144 if ( isset( $prop[
'properties'] ) ) {
145 $properties = $p_output->getProperties();
147 ApiResult::setArrayType( $properties,
'BCkvp',
'name' );
148 ApiResult::setIndexedTagName( $properties,
'property' );
149 $retval[
'properties'] = $properties;
152 if ( isset( $prop[
'volatile'] ) ) {
153 $retval[
'volatile'] = $frame->isVolatile();
155 if ( isset( $prop[
'ttl'] ) && $frame->getTTL() !==
null ) {
156 $retval[
'ttl'] = $frame->getTTL();
158 if ( isset( $prop[
'wikitext'] ) ) {
159 $retval[
'wikitext'] = $wikitext;
161 if ( isset( $prop[
'modules'] ) ) {
162 $retval[
'modules'] = array_values( array_unique( $p_output->getModules() ) );
164 $retval[
'modulescripts'] = [];
165 $retval[
'modulestyles'] = array_values( array_unique( $p_output->getModuleStyles() ) );
167 if ( isset( $prop[
'jsconfigvars'] ) ) {
168 $retval[
'jsconfigvars'] =
169 ApiResult::addMetadataToResultVars( $p_output->getJsConfigVars() );
171 if ( isset( $prop[
'encodedjsconfigvars'] ) ) {
172 $retval[
'encodedjsconfigvars'] = FormatJson::encode(
173 $p_output->getJsConfigVars(),
false, FormatJson::ALL_OK
175 $retval[ApiResult::META_SUBELEMENTS][] =
'encodedjsconfigvars';
177 if ( isset( $prop[
'modules'] ) &&
178 !isset( $prop[
'jsconfigvars'] ) && !isset( $prop[
'encodedjsconfigvars'] ) ) {
179 $this->
addWarning(
'apiwarn-moduleswithoutvars' );
183 ApiResult::setSubelementsList( $retval, [
'wikitext',
'parsetree' ] );