57 $this->
getMain()->setCacheMode(
'anon-public-user-private' );
63 $title = $params[
'title'];
64 if ( $title ===
null ) {
65 $titleProvided =
false;
69 $titleProvided =
true;
72 if ( $params[
'prop'] ===
null ) {
74 [
'apiwarn-deprecation-missingparam',
'prop' ],
'action=expandtemplates&!prop'
78 $prop = array_fill_keys( $params[
'prop'],
true );
81 $titleObj = Title::newFromText( $title );
82 if ( !$titleObj || $titleObj->isExternal() ) {
87 $revid = $params[
'revid'];
88 if ( $revid !==
null ) {
89 $rev = $this->revisionStore->getRevisionById( $revid );
91 $this->
dieWithError( [
'apierror-nosuchrevid', $revid ] );
93 $pTitleObj = $titleObj;
94 $titleObj = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
95 if ( $titleProvided && !$titleObj->equals( $pTitleObj ) ) {
96 $this->
addWarning( [
'apierror-revwrongpage', $rev->getId(),
104 $options = ParserOptions::newFromContext( $this->
getContext() );
106 if ( $params[
'includecomments'] ) {
107 $options->setRemoveComments(
false );
111 $suppressCache =
false;
113 $options, $titleObj, $params, $this, $reset, $suppressCache );
117 if ( isset( $prop[
'parsetree'] ) || $params[
'generatexml'] ) {
118 $parser = $this->parserFactory->getInstance();
120 $dom = $parser->preprocessToDom( $params[
'text'] );
122 if ( is_callable( [ $dom,
'saveXML' ] ) ) {
124 $xml = $dom->saveXML();
127 $xml = $dom->__toString();
129 if ( isset( $prop[
'parsetree'] ) ) {
130 unset( $prop[
'parsetree'] );
131 $retval[
'parsetree'] = $xml;
134 $result->addValue(
null,
'parsetree', $xml );
135 $result->addValue(
null, ApiResult::META_BC_SUBELEMENTS, [
'parsetree' ] );
141 if ( $prop || $params[
'prop'] ===
null ) {
142 $parser = $this->parserFactory->getInstance();
144 $frame = $parser->getPreprocessor()->newFrame();
145 $wikitext = $parser->preprocess( $params[
'text'], $titleObj, $options, $revid, $frame );
146 if ( $params[
'prop'] ===
null ) {
148 ApiResult::setContentValue( $retval,
'wikitext', $wikitext );
150 $p_output = $parser->getOutput();
151 if ( isset( $prop[
'categories'] ) ) {
152 $categories = $p_output->getCategoryNames();
154 $categories_result = [];
155 foreach ( $categories as $category ) {
157 'sortkey' => $p_output->getCategorySortKey( $category ),
159 ApiResult::setContentValue( $entry,
'category', $category );
160 $categories_result[] = $entry;
162 ApiResult::setIndexedTagName( $categories_result,
'category' );
163 $retval[
'categories'] = $categories_result;
166 if ( isset( $prop[
'properties'] ) ) {
167 $properties = $p_output->getPageProperties();
169 ApiResult::setArrayType( $properties,
'BCkvp',
'name' );
170 ApiResult::setIndexedTagName( $properties,
'property' );
171 $retval[
'properties'] = $properties;
174 if ( isset( $prop[
'volatile'] ) ) {
175 $retval[
'volatile'] = $frame->isVolatile();
177 if ( isset( $prop[
'ttl'] ) && $frame->getTTL() !==
null ) {
178 $retval[
'ttl'] = $frame->getTTL();
180 if ( isset( $prop[
'wikitext'] ) ) {
181 $retval[
'wikitext'] = $wikitext;
183 if ( isset( $prop[
'modules'] ) ) {
184 $retval[
'modules'] = array_values( array_unique( $p_output->getModules() ) );
186 $retval[
'modulescripts'] = [];
187 $retval[
'modulestyles'] = array_values( array_unique( $p_output->getModuleStyles() ) );
189 if ( isset( $prop[
'jsconfigvars'] ) ) {
190 $showStrategyKeys = (bool)( $params[
'showstrategykeys'] );
191 $retval[
'jsconfigvars'] =
192 ApiResult::addMetadataToResultVars( $p_output->getJsConfigVars( $showStrategyKeys ) );
194 if ( isset( $prop[
'encodedjsconfigvars'] ) ) {
195 $retval[
'encodedjsconfigvars'] = FormatJson::encode(
196 $p_output->getJsConfigVars(),
false, FormatJson::ALL_OK
198 $retval[ApiResult::META_SUBELEMENTS][] =
'encodedjsconfigvars';
200 if ( isset( $prop[
'modules'] ) &&
201 !isset( $prop[
'jsconfigvars'] ) && !isset( $prop[
'encodedjsconfigvars'] ) ) {
202 $this->
addWarning(
'apiwarn-moduleswithoutvars' );
206 ApiResult::setSubelementsList( $retval, [
'wikitext',
'parsetree' ] );