34 parent::__construct( $main, $action );
39 $this->
getMain()->setCacheMode(
'anon-public-user-private' );
45 $title = $params[
'title'];
46 if ( $title ===
null ) {
47 $titleProvided =
false;
51 $titleProvided =
true;
54 if ( $params[
'prop'] ===
null ) {
56 [
'apiwarn-deprecation-missingparam',
'prop' ],
'action=expandtemplates&!prop'
60 $prop = array_fill_keys( $params[
'prop'],
true );
63 $titleObj = Title::newFromText( $title );
64 if ( !$titleObj || $titleObj->isExternal() ) {
69 $revid = $params[
'revid'];
70 if ( $revid !==
null ) {
71 $rev = $this->revisionStore->getRevisionById( $revid );
73 $this->
dieWithError( [
'apierror-nosuchrevid', $revid ] );
75 $pTitleObj = $titleObj;
76 $titleObj = Title::newFromPageIdentity( $rev->getPage() );
77 if ( $titleProvided && !$titleObj->equals( $pTitleObj ) ) {
78 $this->
addWarning( [
'apierror-revwrongpage', $rev->getId(),
86 $options = ParserOptions::newFromContext( $this->
getContext() );
88 if ( $params[
'includecomments'] ) {
89 $options->setRemoveComments(
false );
93 $suppressCache =
false;
95 $options, $titleObj, $params, $this, $reset, $suppressCache );
99 if ( isset( $prop[
'parsetree'] ) || $params[
'generatexml'] ) {
100 $parser = $this->parserFactory->getInstance();
101 $parser->startExternalParse( $titleObj, $options, Parser::OT_PREPROCESS );
102 $dom = $parser->preprocessToDom( $params[
'text'] );
103 if ( is_callable( [ $dom,
'saveXML' ] ) ) {
105 $xml = $dom->saveXML();
108 $xml = $dom->__toString();
110 if ( isset( $prop[
'parsetree'] ) ) {
111 unset( $prop[
'parsetree'] );
112 $retval[
'parsetree'] = $xml;
115 $result->addValue(
null,
'parsetree', $xml );
122 if ( $prop || $params[
'prop'] ===
null ) {
123 $parser = $this->parserFactory->getInstance();
124 $parser->startExternalParse( $titleObj, $options, Parser::OT_PREPROCESS );
125 $frame = $parser->getPreprocessor()->newFrame();
126 $wikitext = $parser->preprocess( $params[
'text'], $titleObj, $options, $revid, $frame );
127 if ( $params[
'prop'] ===
null ) {
131 $p_output = $parser->getOutput();
132 if ( isset( $prop[
'categories'] ) ) {
133 $categories = $p_output->getCategoryNames();
135 $defaultSortKey = $p_output->getPageProperty(
'defaultsort' ) ??
'';
136 $categories_result = [];
137 foreach ( $categories as $category ) {
142 'sortkey' => $p_output->getCategorySortKey( $category ) ?: $defaultSortKey,
145 $categories_result[] = $entry;
148 $retval[
'categories'] = $categories_result;
151 if ( isset( $prop[
'properties'] ) ) {
152 $properties = $p_output->getPageProperties();
156 $retval[
'properties'] = $properties;
159 if ( isset( $prop[
'volatile'] ) ) {
160 $retval[
'volatile'] = $frame->isVolatile();
162 if ( isset( $prop[
'ttl'] ) && $p_output->hasReducedExpiry() ) {
163 $retval[
'ttl'] = $p_output->getCacheExpiry();
165 if ( isset( $prop[
'wikitext'] ) ) {
166 $retval[
'wikitext'] = $wikitext;
168 if ( isset( $prop[
'modules'] ) ) {
169 $retval[
'modules'] = array_values( array_unique( $p_output->getModules() ) );
171 $retval[
'modulescripts'] = [];
172 $retval[
'modulestyles'] = array_values( array_unique( $p_output->getModuleStyles() ) );
174 if ( isset( $prop[
'jsconfigvars'] ) ) {
175 $showStrategyKeys = (bool)( $params[
'showstrategykeys'] );
176 $retval[
'jsconfigvars'] =
179 if ( isset( $prop[
'encodedjsconfigvars'] ) ) {
180 $retval[
'encodedjsconfigvars'] = FormatJson::encode(
181 $p_output->getJsConfigVars(),
false, FormatJson::ALL_OK
185 if ( isset( $prop[
'modules'] ) &&
186 !isset( $prop[
'jsconfigvars'] ) && !isset( $prop[
'encodedjsconfigvars'] ) ) {
187 $this->
addWarning(
'apiwarn-moduleswithoutvars' );
200 ParamValidator::PARAM_TYPE =>
'text',
201 ParamValidator::PARAM_REQUIRED =>
true,
204 ParamValidator::PARAM_TYPE =>
'integer',
207 ParamValidator::PARAM_TYPE => [
215 'encodedjsconfigvars',
218 ParamValidator::PARAM_ISMULTI =>
true,
221 'includecomments' =>
false,
222 'showstrategykeys' =>
false,
224 ParamValidator::PARAM_TYPE =>
'boolean',
225 ParamValidator::PARAM_DEPRECATED =>
true,
233 'action=expandtemplates&text={{Project:Sandbox}}'
234 =>
'apihelp-expandtemplates-example-simple',
240 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Expandtemplates';
245class_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.