50 parent::__construct( $main, $action );
51 $this->revisionStore = $revisionStore;
52 $this->parserFactory = $parserFactory;
57 $this->
getMain()->setCacheMode(
'anon-public-user-private' );
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() ) {
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();
119 $parser->startExternalParse( $titleObj, $options, Parser::OT_PREPROCESS );
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 );
141 if ( $prop ||
$params[
'prop'] ===
null ) {
142 $parser = $this->parserFactory->getInstance();
143 $parser->startExternalParse( $titleObj, $options, Parser::OT_PREPROCESS );
144 $frame = $parser->getPreprocessor()->newFrame();
145 $wikitext = $parser->preprocess(
$params[
'text'], $titleObj, $options, $revid, $frame );
146 if (
$params[
'prop'] ===
null ) {
150 $p_output = $parser->getOutput();
151 if ( isset( $prop[
'categories'] ) ) {
152 $categories = $p_output->getCategoryNames();
154 $defaultSortKey = $p_output->getPageProperty(
'defaultsort' ) ??
'';
155 $categories_result = [];
156 foreach ( $categories as $category ) {
161 'sortkey' => $p_output->getCategorySortKey( $category ) ?: $defaultSortKey,
164 $categories_result[] = $entry;
167 $retval[
'categories'] = $categories_result;
170 if ( isset( $prop[
'properties'] ) ) {
171 $properties = $p_output->getPageProperties();
175 $retval[
'properties'] = $properties;
178 if ( isset( $prop[
'volatile'] ) ) {
179 $retval[
'volatile'] = $frame->isVolatile();
181 if ( isset( $prop[
'ttl'] ) && $frame->getTTL() !==
null ) {
182 $retval[
'ttl'] = $frame->getTTL();
184 if ( isset( $prop[
'wikitext'] ) ) {
185 $retval[
'wikitext'] = $wikitext;
187 if ( isset( $prop[
'modules'] ) ) {
188 $retval[
'modules'] = array_values( array_unique( $p_output->getModules() ) );
190 $retval[
'modulescripts'] = [];
191 $retval[
'modulestyles'] = array_values( array_unique( $p_output->getModuleStyles() ) );
193 if ( isset( $prop[
'jsconfigvars'] ) ) {
194 $showStrategyKeys = (bool)(
$params[
'showstrategykeys'] );
195 $retval[
'jsconfigvars'] =
198 if ( isset( $prop[
'encodedjsconfigvars'] ) ) {
199 $retval[
'encodedjsconfigvars'] = FormatJson::encode(
200 $p_output->getJsConfigVars(),
false, FormatJson::ALL_OK
204 if ( isset( $prop[
'modules'] ) &&
205 !isset( $prop[
'jsconfigvars'] ) && !isset( $prop[
'encodedjsconfigvars'] ) ) {
206 $this->
addWarning(
'apiwarn-moduleswithoutvars' );
218 ParamValidator::PARAM_TYPE =>
'text',
219 ParamValidator::PARAM_REQUIRED =>
true,
222 ParamValidator::PARAM_TYPE =>
'integer',
225 ParamValidator::PARAM_TYPE => [
233 'encodedjsconfigvars',
236 ParamValidator::PARAM_ISMULTI =>
true,
239 'includecomments' =>
false,
240 'showstrategykeys' =>
false,
242 ParamValidator::PARAM_TYPE =>
'boolean',
243 ParamValidator::PARAM_DEPRECATED =>
true,
250 'action=expandtemplates&text={{Project:Sandbox}}'
251 =>
'apihelp-expandtemplates-example-simple',
256 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Expandtemplates';
261class_alias( ApiExpandTemplates::class,
'ApiExpandTemplates' );
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
array $params
The job parameters.
This is the main API class, used for both external and internal processing.