50 parent::__construct( $main, $action );
51 $this->revisionStore = $revisionStore;
52 $this->parserFactory = $parserFactory;
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(),
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 );
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 ) {
150 $p_output = $parser->getOutput();
151 if ( isset( $prop[
'categories'] ) ) {
152 $categories = $p_output->getCategories();
154 $categories_result = [];
155 foreach ( $categories as $category => $sortkey ) {
157 $entry[
'sortkey'] = $sortkey;
159 $categories_result[] = $entry;
162 $retval[
'categories'] = $categories_result;
165 if ( isset( $prop[
'properties'] ) ) {
166 $properties = $p_output->getPageProperties();
170 $retval[
'properties'] = $properties;
173 if ( isset( $prop[
'volatile'] ) ) {
174 $retval[
'volatile'] = $frame->isVolatile();
176 if ( isset( $prop[
'ttl'] ) && $frame->getTTL() !==
null ) {
177 $retval[
'ttl'] = $frame->getTTL();
179 if ( isset( $prop[
'wikitext'] ) ) {
180 $retval[
'wikitext'] = $wikitext;
182 if ( isset( $prop[
'modules'] ) ) {
183 $retval[
'modules'] = array_values( array_unique( $p_output->getModules() ) );
185 $retval[
'modulescripts'] = [];
186 $retval[
'modulestyles'] = array_values( array_unique( $p_output->getModuleStyles() ) );
188 if ( isset( $prop[
'jsconfigvars'] ) ) {
189 $showStrategyKeys = (bool)( $params[
'showstrategykeys'] );
190 $retval[
'jsconfigvars'] =
193 if ( isset( $prop[
'encodedjsconfigvars'] ) ) {
199 if ( isset( $prop[
'modules'] ) &&
200 !isset( $prop[
'jsconfigvars'] ) && !isset( $prop[
'encodedjsconfigvars'] ) ) {
201 $this->
addWarning(
'apiwarn-moduleswithoutvars' );
213 ParamValidator::PARAM_TYPE =>
'text',
214 ParamValidator::PARAM_REQUIRED =>
true,
217 ParamValidator::PARAM_TYPE =>
'integer',
220 ParamValidator::PARAM_TYPE => [
228 'encodedjsconfigvars',
231 ParamValidator::PARAM_ISMULTI =>
true,
234 'includecomments' =>
false,
235 'showstrategykeys' =>
false,
237 ParamValidator::PARAM_TYPE =>
'boolean',
238 ParamValidator::PARAM_DEPRECATED =>
true,
245 'action=expandtemplates&text={{Project:Sandbox}}'
246 =>
'apihelp-expandtemplates-example-simple',
251 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Expandtemplates';
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
This abstract class implements many basic API functions, and is the base of all API classes.
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
getMain()
Get the main module.
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
addDeprecation( $msg, $feature, $data=[])
Add a deprecation warning for this module.
requireMaxOneParameter( $params,... $required)
Dies if more than one parameter from a certain set of parameters are set and not false.
getResult()
Get the result object.
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
getModuleName()
Get the name of the module being executed by this instance.
getHookRunner()
Get an ApiHookRunner for running core API hooks.
API module that functions as a shortcut to the wikitext preprocessor.
getExamplesMessages()
Returns usage examples for this module.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
__construct(ApiMain $main, $action, RevisionStore $revisionStore, ParserFactory $parserFactory)
getHelpUrls()
Return links to more detailed help pages about the module.
This is the main API class, used for both external and internal processing.
static setArrayType(array &$arr, $type, $kvpKeyName=null)
Set the array data type.
static addMetadataToResultVars( $vars, $forceHash=true)
Add the correct metadata to an array of vars we want to export through the API.
const META_SUBELEMENTS
Key for the 'subelements' metadata item.
static setSubelementsList(array &$arr, $names)
Causes the elements with the specified names to be output as subelements rather than attributes.
const META_BC_SUBELEMENTS
Key for the 'BC subelements' metadata item.
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
static setContentValue(array &$arr, $name, $value, $flags=0)
Add an output value to the array by name and mark as META_CONTENT.
getContext()
Get the base IContextSource object.
static newFromContext(IContextSource $context)
Get a ParserOptions object from a IContextSource object.
const OT_PREPROCESS
Output type: like Parser::preprocess()