36 parent::__construct( $main, $action );
37 $this->revisionStore = $revisionStore;
38 $this->parserFactory = $parserFactory;
43 $this->
getMain()->setCacheMode(
'anon-public-user-private' );
49 $title = $params[
'title'];
50 if ( $title ===
null ) {
51 $titleProvided =
false;
55 $titleProvided =
true;
58 if ( $params[
'prop'] ===
null ) {
60 [
'apiwarn-deprecation-missingparam',
'prop' ],
'action=expandtemplates&!prop'
64 $prop = array_fill_keys( $params[
'prop'],
true );
67 $titleObj = Title::newFromText( $title );
68 if ( !$titleObj || $titleObj->isExternal() ) {
73 $revid = $params[
'revid'];
74 if ( $revid !==
null ) {
75 $rev = $this->revisionStore->getRevisionById( $revid );
77 $this->
dieWithError( [
'apierror-nosuchrevid', $revid ] );
79 $pTitleObj = $titleObj;
80 $titleObj = Title::newFromPageIdentity( $rev->getPage() );
81 if ( $titleProvided && !$titleObj->equals( $pTitleObj ) ) {
82 $this->
addWarning( [
'apierror-revwrongpage', $rev->getId(),
90 $options = ParserOptions::newFromContext( $this->
getContext() );
92 if ( $params[
'includecomments'] ) {
93 $options->setRemoveComments(
false );
97 $suppressCache =
false;
99 $options, $titleObj, $params, $this, $reset, $suppressCache );
103 if ( isset( $prop[
'parsetree'] ) || $params[
'generatexml'] ) {
104 $parser = $this->parserFactory->getInstance();
105 $parser->startExternalParse( $titleObj, $options, Parser::OT_PREPROCESS );
106 $dom = $parser->preprocessToDom( $params[
'text'] );
107 if ( is_callable( [ $dom,
'saveXML' ] ) ) {
109 $xml = $dom->saveXML();
112 $xml = $dom->__toString();
114 if ( isset( $prop[
'parsetree'] ) ) {
115 unset( $prop[
'parsetree'] );
116 $retval[
'parsetree'] = $xml;
119 $result->addValue(
null,
'parsetree', $xml );
126 if ( $prop || $params[
'prop'] ===
null ) {
127 $parser = $this->parserFactory->getInstance();
128 $parser->startExternalParse( $titleObj, $options, Parser::OT_PREPROCESS );
129 $frame = $parser->getPreprocessor()->newFrame();
130 $wikitext = $parser->preprocess( $params[
'text'], $titleObj, $options, $revid, $frame );
131 if ( $params[
'prop'] ===
null ) {
135 $p_output = $parser->getOutput();
136 if ( isset( $prop[
'categories'] ) ) {
137 $categories = $p_output->getCategoryNames();
139 $defaultSortKey = $p_output->getPageProperty(
'defaultsort' ) ??
'';
140 $categories_result = [];
141 foreach ( $categories as $category ) {
146 'sortkey' => $p_output->getCategorySortKey( $category ) ?: $defaultSortKey,
149 $categories_result[] = $entry;
152 $retval[
'categories'] = $categories_result;
155 if ( isset( $prop[
'properties'] ) ) {
156 $properties = $p_output->getPageProperties();
160 $retval[
'properties'] = $properties;
163 if ( isset( $prop[
'volatile'] ) ) {
164 $retval[
'volatile'] = $frame->isVolatile();
166 if ( isset( $prop[
'ttl'] ) && $p_output->hasReducedExpiry() ) {
167 $retval[
'ttl'] = $p_output->getCacheExpiry();
169 if ( isset( $prop[
'wikitext'] ) ) {
170 $retval[
'wikitext'] = $wikitext;
172 if ( isset( $prop[
'modules'] ) ) {
173 $retval[
'modules'] = array_values( array_unique( $p_output->getModules() ) );
175 $retval[
'modulescripts'] = [];
176 $retval[
'modulestyles'] = array_values( array_unique( $p_output->getModuleStyles() ) );
178 if ( isset( $prop[
'jsconfigvars'] ) ) {
179 $showStrategyKeys = (bool)( $params[
'showstrategykeys'] );
180 $retval[
'jsconfigvars'] =
183 if ( isset( $prop[
'encodedjsconfigvars'] ) ) {
184 $retval[
'encodedjsconfigvars'] = FormatJson::encode(
185 $p_output->getJsConfigVars(),
false, FormatJson::ALL_OK
189 if ( isset( $prop[
'modules'] ) &&
190 !isset( $prop[
'jsconfigvars'] ) && !isset( $prop[
'encodedjsconfigvars'] ) ) {
191 $this->
addWarning(
'apiwarn-moduleswithoutvars' );
204 ParamValidator::PARAM_TYPE =>
'text',
205 ParamValidator::PARAM_REQUIRED =>
true,
208 ParamValidator::PARAM_TYPE =>
'integer',
211 ParamValidator::PARAM_TYPE => [
219 'encodedjsconfigvars',
222 ParamValidator::PARAM_ISMULTI =>
true,
225 'includecomments' =>
false,
226 'showstrategykeys' =>
false,
228 ParamValidator::PARAM_TYPE =>
'boolean',
229 ParamValidator::PARAM_DEPRECATED =>
true,
237 'action=expandtemplates&text={{Project:Sandbox}}'
238 =>
'apihelp-expandtemplates-example-simple',
244 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Expandtemplates';
249class_alias( ApiExpandTemplates::class,
'ApiExpandTemplates' );
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
This is the main API class, used for both external and internal processing.