API:Parsing wikitext
From MediaWiki.org
![]() |
This page is part of the MediaWiki API documentation. |
Language: | English • Deutsch • español • 日本語 |
---|
Quick overview:
- Quick start guide
- FAQ
- Tutorial
- Formats
- Error reporting
- Restricting usage
- Authentication
- Queries
- Search suggestions
- Expanding templates and rendering
- Purging pages' caches
- Parameter information
- Changing wiki content
- Watchlist feed
- Extensions
- Using the API in MediaWiki and extensions
- Miscellaneous
- Implementation
- Client code
These API modules allow calling the parser for preprocessing and rendering.
Retrieving just the raw source wikitext can be done through prop=revisions.
Contents |
[edit] expandtemplates
MediaWiki version: | 1.12 |
Returns preprocessed wikitext (comments stripped, templates expanded, etc.)
[edit] Parameters
text
: Wikitext to preprocesstitle
: Act like the wikitext is on this page (default:API
)- This only really matters when parsing links to the page itself or links to subpages, or when using magic words like
{{PAGENAME}}
.
- This only really matters when parsing links to the page itself or links to subpages, or when using magic words like
[edit] Example
Expand wikitext {{Project:Sandbox}}
<?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>
<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>
[edit] Possible errors
None.
[edit] parse
![]() |
It has been suggested that this page or section be merged with API:Parse. (Discuss) |
MediaWiki version: | 1.12 |
Returns parsed wikitext
[edit] Parameters
text
: Wikitext to parsetitle
: Act like the wikitext is on this page (default:API
)- This only really matters when parsing links to the page itself or links to subpages, or when using magic words like
{{PAGENAME}}
. - If you are interested in simply getting the rendered content of a page, you can bypass the api and simply add
action=render
to your url, like so:/w/index.php?title=API:Parsing_wikitext&action=render
. However, this method has been deprecated, using the Parse API is better. More info here.
- This only really matters when parsing links to the page itself or links to subpages, or when using magic words like
page
: Parse the content of this page. Cannot be used together withtext
ortitle
prop
: Which properties to get (all by default)text
: The generated HTMLlanglinks
: A list of language links in the wikitext fragmentcategories
: A list of category links in the wikitext fragmentlinks
: A list of internal links in the wikitext fragmenttemplates
: A list of templates used in the wikitext fragmentimages
: A list of images used in the wikitext fragmentexternallinks
: A list of external links used in the wikitext fragmentsections
: A list of sections present in the wikitext fragmentrevid
: Ifpage
was used, specify the ID of the revision parsed
pst
: Do a pre-save-transform (makes{{subst:}}
work properly, expands tildes to signatures, etc.)uselang
: The language code ("uselang=en" for English, "uselang=fr" for French, and so on) 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.
- This is useful for language-dependent content, such as generated by the {{LangSwitch}} template at the Commons, or generally by things dependent on
[edit] Example
Parse wikitext [[foo]] [[API:Query|bar]] [http://www.example.com/ baz]
<?xml version="1.0" encoding="utf-8"?> <api> <parse> <text><p><a href="/wiki/Foo" title="Foo">foo</a> <a href="/w/index.php?title=API:Query&amp;action=edit&amp;redlink=1" class="new" title="API:Query (page does not exist)">bar</a> <a href="http://www.example.com/" class="external text" title="http://www.example.com/" rel="nofollow">baz</a> </p></text> <langlinks /> <categories /> <links> <pl ns="0" exists="">Foo</pl> <pl ns="0">API:Query</pl> </links> <templates /> <images /> <externallinks> <el>http://www.example.com/</el> </externallinks> <sections /> </parse> </api>
[edit] Possible errors
- code: params
- info: The page parameter cannot be used together with the text and title parameters
- code: missingtitle
- info: The page you specified doesn't exist.
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.