API:Parsing wikitext
| Language: | English • Deutsch • español • 日本語 • 中文 |
|---|
| This page is part of the MediaWiki action API documentation. |
MediaWiki action API
- Introduction and quick start
- FAQ
- Tutorial
- Formats
- Error reporting
- Restricting usage
- Cross-site requests
- Authentication
- Queries
- Searching (by title, content, coordinates...)
- Parsing wikitext and expanding templates
- Purging pages' caches
- Parameter information
- Changing wiki content
- Watchlist feed
- Wikidata
- Extensions
- Using the API in MediaWiki and extensions
- Miscellaneous
- Implementation
- Client code
- Asserting
These API modules allow calling the parser for preprocessing and rendering.
Retrieving just the raw source wikitext can be done through prop=revisions.
Contents
expandtemplates[edit]
| MediaWiki version: | 1.12 |
Returns preprocessed wikitext (comments stripped, templates expanded, etc.)
Parameters[edit]
text: Wikitext to preprocesstitle: Act like the wikitext is on the page specified. Note: This only really matters when parsing links to the page itself or links to subpages, or when using magic words like{{PAGENAME}}. (Default:API.)
Example[edit]
Expand wikitext {{Project:Sandbox}}
| Result |
|---|
<?xml version="1.0" encoding="utf-8"?>
<api>
<expandtemplates>
{|style="background:transparent;border:1px dotted #000000;padding:2px;margin:2px;"
|-
|
''Welcome to the '''Wikipedia Sandbox'''! This page allows you to carry out experiments. To edit, click
<span class="plainlinks">'''[http://en.wikipedia.org/w/index.php?title=API&action=edit here]'''</span>
or '''edit this page''' above (or the views section for obscure browsers),
make your changes and click the '''Save page''' button when finished. Content will '''not''' stay permanently;
this page is '''automatically cleaned''' every 12 hours,
although it tends to be overwritten by other testing users much faster than that.''
''Please do not place copyrighted, offensive, or libelous content in the sandbox(es).
If you have any questions regarding [[Wikipedia]], please see [[Wikipedia:Questions]]. Thanks!''
:''This page is a virtual sandbox on Wikipedia. For uses of sandboxes, see the article [[sandbox]].
''You may also use the templates''
<font style="font-size: 12px;">
[[Template:X1|X1]], [[Template:X2|X2]], [[Template:X3|X3]], [[Template:X4|X4]], [[Template:X5|X5]],
[[Template:X6|X6]], [[Template:X7|X7]], [[Template:X8|X8]], and [[Template:X9|X9]]</font>
for experimental purposes.<br />
''Try other sandboxes:'' <span style="font-size: 12px;">
[[Wikipedia:Sandbox|Main Sandbox]] |
[[Wikipedia:Tutorial (Editing)/sandbox|Tutorial Sandbox 1]] |
[[Wikipedia:Tutorial (Formatting)/sandbox|Tutorial Sandbox 2]] |
[[Wikipedia:Tutorial (Wikipedia links)/sandbox|Tutorial Sandbox 3]] |
[[Wikipedia:Tutorial (Related site links)/sandbox|Tutorial Sandbox 4]] |
[[Wikipedia:Tutorial (External links)/sandbox|Tutorial Sandbox 5]]</span><br />
''For a sandbox of your own, [[Wikipedia:User page#How do I create a user subpage?|create a user subpage]].''
|
{|style="background:transparent;"
|-
|
<table class="noprint Template-Shortcut"
style="float:right;margin:.5em 0;border:1px solid #999;background:#fff;padding:3px;">
<th class="noprint" align="center" valign="top">
<small>[[Wikipedia:Shortcut|Shortcuts]]:<br />
[[WP:SB]]<br />
[[WP:SAND]]<br />
[[WP:SANDBOX]]<br />
[[WP:TEST]]</small>
</th>
</table>
|-
|
<div style="background:#eeeeee;border:1px solid black;padding:5px;font-size:small;text-align:center;white-space:nowrap;">
More info:<br>
[[Wikipedia:Introduction|Introduction to Wikipedia]]<br>
[[Wikipedia:About the Sandbox|About the Sandbox]]<br>
[[Wikipedia:Tutorial|Editing tutorial]]
</div>
|}
|}
</sub>
This is a test</expandtemplates>
</api>
|
Possible errors[edit]
None.
parse[edit]
| parse | ||
|---|---|---|
This module cannot be used as a Generator. |
||
| Prefix | ||
| Required rights | none | |
| Post only? | No | |
| Generated help | Current | |
| Version added |
|
Returns parsed wikitext. Extension:TextExtracts provides additional APIs.
Parameters[edit]
title: Act like the wikitext is on this page. Note: This only really matters when parsing links to the page itself or subpages, or when using magic words like{{PAGENAME}}. Type: string (Default:API.)text: Wikitext to parse. Type: stringsummary: Summary to parse. Type: stringpage: Parse the content of this page. Cannot be used together withtextandtitle. Type: stringpageid: Parse the content of this page. Overridespage. Type: stringredirects: If thepageparameter is set to a redirect, resolve it. Type: boololdid: Parse the content of this revision. Overridespageandpageid. Type: stringprop: Which pieces of information to get. A pipe character|separated list containing one or more of the following options:text: Gives the parsed text of the wikitext.langlinks: Gives the langlinks the parsed wikitext.categories: Gives the categories of the parsed wikitext.categorieshtml: Gives the html version of the categories.languageshtml: Gives the html version of the language links. (removed in 1.24)links: Gives the internal links in the parsed wikitext.templates: Gives the templates in the parsed wikitext.images: Gives the images in the parsed wikitext.externallinks: Gives the external links in the parsed wikitext.sections: Gives the sections in the parsed wikitext (TOC, table of contents).revid: Ifpagewas used, specify the ID of the revision parsed.displaytitle: Adds the title of the parsed wikitext.headitems: Gives items to put in the <head> of the page.headhtml: Gives parsed <head> of the page.modules: Gives the modules, scripts, styles and messages used parsed wikitext. 1.24+indicators: Gives the HTML of page status indicators used on the page. 1.25+iwlinks: Gives interwiki links in the parsed wikitext. Note: Section tree is only generated if there are more than 4 sections, if the __TOC__ keyword is present, or ifsectionsis explicitly requested and the page contains headings/sub-headings (overrides __NOTOC__). Type: stringwikitext: Gives the original wikitext that was parsed. 1.25+properties: Gives various properties defined in the parsed wikitext. 1.25?+limitreportdata: Gives the limit report in a structured way. Gives no data, when disablepp is set. 1.23+limitreporthtml: Gives the html version of the PP report. Returns empty ifdisableppis true. 1.23+parsetree: Gives the XML parse tree of revision content (requires contentmodel=wikitext). 1.26+
pst: Do a pre-save transform (PST) on the input before parsing it (makes{{subst:}}work properly, expands tildes to signatures, etc.). Ignored ifpage,pageidoroldidis used. Type: boolonlypst: Only do a pre-save transform (PST) on the input, don't parse it. Returns the same wikitext, after a PST has been applied. Ignored ifpage,pageidoroldidis used. Type: boolsection: Only retrieve the content of this section number. When the value isnewadds a new section, but then requires thetextparam. Type: stringsectiontitle: The title for the new section Type: string 1.26+disablepp: Disable the PP Report from the parser output. Type: booldisableeditsection: Disable edit section links from the parser output. Type: bool 1.24+generatexml: Generate the XML parse tree (requires contentmodel=wikitext). Useprop=parsetreeinstead. Type: bool (deprecated in 1.26)preview: Parse in preview mode. Type: bool 1.22+sectionpreview: Parse in section preview mode (enables preview mode too). Type: bool 1.22+disabletoc: Disable table of contents in output. Type: bool 1.23+contentformat: Content serialization format used for the input text. Only valid when used with text. One value: text/x-wiki, text/javascript, application/json, text/css, text/plain.contentmodel: Content model of the input text. Default is the model of the specified title, orwikitextif title is not specified. Only valid when used with text. Can be one of the following: wikitext, javascript, css, text, JsonZeroConfig, Scribunto, JsonSchema.uselang: The language code ("en" for English, "fr" for French, etc.) of the language in which the text or the page should be parsed. This is useful for language-dependent content, such as generated by the {{LangSwitch}} template at the Commons, or generally by things dependent on{{int:Lang}}. Prior to MediaWiki 1.17, this worked by accident; since 1.17, it works by design. Type: string
Example[edit]
Parse wikitext: [[foo]] [[API:Query|bar]] [http://www.example.com/ baz]
| Result |
|---|
{
"parse": {
"title": "API",
"text": {
"*": "<p><a href=\"/wiki/Foo\" title=\"Foo\" class=\"mw-redirect\">foo</a> <a href=\"/w/index.php?title=API:Query&action=edit&redlink=1\" class=\"new\" title=\"API:Query (page does not exist)\">bar</a> <a rel=\"nofollow\" class=\"external text\" href=\"http://www.example.com/\">baz</a></p>\n\n\n<!-- \nNewPP limit report\nParsed by mw1193\nCPU time usage: 0.013 seconds\nReal time usage: 0.016 seconds\nPreprocessor visited node count: 1/1000000\nPreprocessor generated node count: 0/1500000\nPost\u2010expand include size: 0/2097152 bytes\nTemplate argument size: 0/2097152 bytes\nHighest expansion depth: 1/40\nExpensive parser function count: 0/500\n-->\n\n<!-- \nTransclusion expansion time report (%,ms,calls,template)\n100.00% 0.000 1 - -total\n-->\n"
},
"langlinks": [],
"categories": [],
"links": [
{
"ns": 0,
"*": "Foo",
"exists": ""
},
{
"ns": 0,
"*": "API:Query"
}
],
"templates": [],
"images": [],
"externallinks": [
"http://www.example.com/"
],
"sections": [],
"displaytitle": "API",
"iwlinks": [],
"properties": []
}
}
|
Possible errors[edit]
| Code | Info |
|---|---|
| invalidsection | The section parameter must be a valid section id or 'new' |
| missingrev | There is no revision ID oldid |
| missingtitle | The page you specified doesn't exist |
| nosuchpageid | There is no page with ID $1 |
| nosuchsection | There is no section sectionnumber in page |
| params | The page parameter cannot be used together with the text and title parameters |
| permissiondenied | You don't have permission to view deleted revisions |
| readapidenied | You need read permission to use this module |
Note: missingtitle is returned only for syntactically invalid page titles (eg. empty). Trying to fetching a missing page will return the rendered HTML of the "page missing" notice, not an error. However, you can check the revid attribute of <param>, which will always be 0 for missing pages.

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
