Jump to content

API:Expandtemplates

From mediawiki.org

GET request to expand all templates in the wikitext.

MediaWiki version:
1.12

API documentation

[edit]

action=expandtemplates

(main | expandtemplates)

Expands all templates within wikitext.

Specific parameters:
Other general parameters are available.
title

Title of the page.

text

Wikitext to convert.

This parameter is required.
revid

Revision ID, for {{REVISIONID}} and similar variables.

Type: integer
prop

Which pieces of information to get.

Note that if no values are selected, the result will contain the wikitext, but the output will be in a deprecated format.

wikitext
The expanded wikitext.
categories
Any categories present in the input that are not represented in the wikitext output.
properties
Page properties defined by expanded magic words in the wikitext.
volatile
Whether the output is volatile and should not be reused elsewhere within the page.
ttl
The maximum time after which caches of the result should be invalidated.
modules
Any ResourceLoader modules that parser functions have requested be added to the output. Either jsconfigvars or encodedjsconfigvars must be requested jointly with modules.
jsconfigvars
Gives the JavaScript configuration variables specific to the page.
encodedjsconfigvars
Gives the JavaScript configuration variables specific to the page as a JSON string.
parsetree
The XML parse tree of the input.
Values (separate with | or alternative): categories, encodedjsconfigvars, jsconfigvars, modules, parsetree, properties, ttl, volatile, wikitext
includecomments

Whether to include HTML comments in the output.

Type: boolean (details)
showstrategykeys

Whether to include internal merge strategy information in jsconfigvars.

Type: boolean (details)
generatexml
Deprecated.

Generate XML parse tree (replaced by prop=parsetree).

Type: boolean (details)
templatesandboxprefix

Template sandbox prefix, as with Special:TemplateSandbox.

Separate values with | or alternative.
Maximum number of values is 50 (500 for clients that are allowed higher limits).
templatesandboxtitle

Parse the page using templatesandboxtext in place of the contents of the page named here.

templatesandboxtext

Parse the page using this page content in place of the page named by templatesandboxtitle.

templatesandboxcontentmodel

Content model of templatesandboxtext.

One of the following values: GadgetDefinition, Json.JsonConfig, JsonSchema, Map.JsonConfig, MassMessageListContent, NewsletterContent, Scribunto, SecurePoll, Tabular.JsonConfig, css, flow-board, javascript, json, sanitized-css, text, translate-messagebundle, unknown, wikitext
templatesandboxcontentformat

Content format of templatesandboxtext.

One of the following values: application/json, application/octet-stream, application/unknown, application/x-binary, text/css, text/javascript, text/plain, text/unknown, text/x-wiki, unknown/unknown
Example:
Expand the wikitext {{Project:Sandbox}}.
api.php?action=expandtemplates&text={{Project:Sandbox}} [open in sandbox]


Example

[edit]

GET request

[edit]
A sample request to expand the Project:Sandbox template.

Response

[edit]
 "expandtemplates": {
        "wikitext": "\n<table class=\"plainlinks ombox ombox-notice\" role=\"presentation\" style=\"margin:auto;\"><tr><td class=\"mbox-image\">[[File:Sandbox.png|75px|alt=|link=]]</td><td class=\"mbox-text\">Welcome to this [[Wikipedia:About the Sandbox|sandbox page]]. Sandbox pages provide space to experiment with the process of editing Wikipedia pages.<br/>To edit this sandbox, click <span class=\"plainlinks\">'''[//en.wikipedia.org/w/index.php?title=API&action=edit here]'''</span> or the \"Edit\" tab along the top of this page..."
    }

Sample code

[edit]

Python

[edit]
#!/usr/bin/python3

"""
    expand_templates.py

    MediaWiki API Demos
    Demo of `Expandtemplates` module: Expand the Project:Sandbox template.

    MIT License
"""

import requests

S = requests.Session()

URL = "https://en.wikipedia.org/w/api.php"

PARAMS = {
    "action": "expandtemplates",
    "text": "{{Project:Sandbox}}",
    "prop": "wikitext",
    "format": "json"
}

R = S.get(url=URL, params=PARAMS)
DATA = R.json()

print(DATA)

PHP

[edit]
<?php

/*
    expand_templates.php

    MediaWiki API Demos
    Demo of `Expandtemplates` module: Expand the Project:Sandbox template.

    MIT License
*/

$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
    "action" => "expandtemplates",
    "text" => "{{Project:Sandbox}}",
    "prop" => "wikitext",
    "format" => "json"
];

$url = $endPoint . "?" . http_build_query( $params );

$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$output = curl_exec( $ch );
curl_close( $ch );

echo( $output );

JavaScript

[edit]
/*
    expand_templates.js

    MediaWiki API Demos
    Demo of `Expandtemplates` module: Expand the Project:Sandbox template.

    MIT License
*/

var url = "https://en.wikipedia.org/w/api.php"; 

var params = {
    action: "expandtemplates",
    text: "{{Project:Sandbox}}",
    prop: "wikitext",
    format: "json"
};
request.get({ url: url, qs: params }, function(error, res, body) {
    if (error) {
        return;
    }
    console.log(body);
});

MediaWiki JS

[edit]
/*
    expand_templates.js

    MediaWiki API Demos
    Demo of `Expandtemplates` module: Expand the Project:Sandbox template.

    MIT License
*/

var params = {
    action: "expandtemplates",
    text: "{{Project:Sandbox}}",
    prop: "wikitext",
    format: "json"
},
api = new mw.Api();

api.get( params ).done( function ( data ) {
console.log( data );
} );

Possible errors

[edit]
Code Info
invalidtitle Bad title "title".
nosuchrevid There is no revision with ID revid.
revwrongpage rrevid is not a revision of title.

Parameter history

[edit]
  • v1.26 - Introduced encodedjsconfigvars, jsconfigvars, modules
  • v1.25 - Introduced revid, properties
  • v1.24 - Introduced prop
  • v1.18 - Introduced includecomments
  • v1.13 - Introduced generatexml

Additional notes

[edit]
  • Special:ExpandTemplates - This is a special page that has two input boxes, one to enter wikitext, and another one to enter a page name. It produces in the Result window the expanded wikitext, i.e., templates, parser functions, and variables are expanded recursively; variables depending on page name are expanded based on the supplied page name. This is an intermediate result before a page is rendered after saving or pressing Preview (or the resulting wikitext is submitted to other processing), helpful in understanding and debugging wikitext expansion. More information about this page can be found here: Help:ExpandTemplates

See also

[edit]