API:Purge
Appearance
This page is part of the MediaWiki Action API documentation. |
MediaWiki version: | ≥ 1.14 |
POST request to purge the cache for the given title.
API documentation
[edit]
Examples
[edit]Example 1: Purge one or two pages
[edit]POST request
[edit]Response
[edit]{
"batchcomplete": "",
"purge": [
{
"ns": 0,
"title": "NonexistentArticle",
"missing": ""
},
{
"ns": 0,
"title": "Main Page",
"purged": ""
}
],
"normalized": [
{
"from": "Main_Page",
"to": "Main Page"
}
]
}
Sample code
[edit]Python
[edit]#!/usr/bin/python3
"""
purge_two_pages.py
MediaWiki API Demos
Demo of `purge` module: Sending post request to purge two or more pages
MIT license
"""
import requests
S = requests.Session()
URL = "https://en.wikipedia.org/w/api.php"
PARAMS = {
"action": "purge",
"titles": "Main_Page|Nonexistent",
"format": "json"
}
R = S.post(url=URL, params=PARAMS)
DATA = R.text
print(DATA)
PHP
[edit]<?php
/*
purge_two_pages.php
MediaWiki API Demos
Demo of `purge` module: Sending post request to purge two or more pages
MIT license
*/
$endPoint = "https://en.wikipedia.org/w/api.php";
purge();
function purge() {
global $endPoint;
$params = [
"action" => "purge",
"titles" => "Main_Page|Nonexistent",
"format" => "json"
];
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $endPoint );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $params ) );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$output = curl_exec( $ch );
curl_close( $ch );
echo ( $output );
}
JavaScript
[edit]/*
purge_two_pages.js
MediaWiki API Demos
Demo of `purge` module: Sending post request to purge two or more pages
MIT license
*/
var request = require('request').defaults({jar: true}),
url = "https://en.wikipedia.org/w/api.php";
function purge() {
var params = {
action: "purge",
titles: "Main_Page|Nonexistent",
format: "json"
};
request.post({ url: url, form: params }, function (error, res, body) {
if (error) {
return;
}
console.log(body);
});
}
// Start the function
purge();
MediaWiki JS
[edit]/*
purge_two_pages.js
MediaWiki API Demos
Demo of `purge` module: Sending post request to purge two or more pages
MIT License
*/
var params = {
action: 'purge',
titles: 'Main_Page|Nonexistent',
format: 'json'
},
api = new mw.Api();
api.post( params ).done( function ( data ) {
console.log( data );
} );
Example 2: Purge first 10 pages in the main namespace
[edit]POST request
[edit]Response
[edit]{
"batchcomplete": "",
"continue": {
"gapcontinue": "!!Destroy-Oh-Boy!!",
"continue": "gapcontinue||"
},
"purge": [
{
"ns": 0,
"title": "!",
"purged": ""
},
{
"ns": 0,
"title": "!!",
"purged": ""
}
...
]
}
Sample code
[edit]Python
[edit]#!/usr/bin/python3
"""
purge_namespace_pages.py
MediaWiki API Demos
Demo of `purge` module: Sending post request to purge first 10 pages in the main namespace
MIT license
"""
import requests
S = requests.Session()
URL = "https://en.wikipedia.org/w/api.php"
PARAMS = {
"action": "purge",
"generator": "allpages",
"gapnamespace": "0",
"gaplimit": "10",
"format": "json"
}
R = S.post(url=URL, params=PARAMS)
DATA = R.text
print(DATA)
PHP
[edit]<?php
/*
purge_namespace_pages.php
MediaWiki API Demos
Demo of `purge` module: Sending post request to purge first 10 pages in the main namespace
MIT license
*/
$endPoint = "https://en.wikipedia.org/w/api.php";
purge();
function purge() {
global $endPoint;
$params = [
"action" => "purge",
"generator" => "allpages",
"gapnamespace" => "0",
"gaplimit" => "10",
"format" => "json"
];
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $endPoint );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $params ) );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$output = curl_exec( $ch );
curl_close( $ch );
echo ( $output );
}
JavaScript
[edit]/*
purge_namespace_pages.js
MediaWiki API Demos
Demo of `purge` module: Sending post request to purge first 10 pages in the main namespace
MIT license
*/
var request = require('request').defaults({jar: true}),
url = "https://test.wikipedia.org/w/api.php";
function purge() {
var params = {
action: "purge",
generator: "allpages",
gapnamespace: "0",
gaplimit: "10",
format: "json"
};
request.post({ url: url, form: params }, function (error, res, body) {
if (error) {
return;
}
console.log(body);
});
}
// Start the function
purge();
MediaWiki JS
[edit]/*
purge_namespace_pages.js
MediaWiki API Demos
Demo of `purge` module: Sending post request to purge first 10 pages in the main namespace
MIT License
*/
var params = {
action: 'purge',
generator: 'allpages',
gapnamespace: '0',
gaplimit: '10',
format: 'json'
},
api = new mw.Api();
api.post( params ).done( function ( data ) {
console.log( data );
} );
Possible errors
[edit]Code | Info |
---|---|
cantpurge | Only users with the 'purge' right can purge pages via the API |
mustbeposted | The purge module requires a POST request. |
invalidreason | The requested page title contains invalid characters: "title". |
assertuserfailed | You are no longer logged in, so the action could not be completed. |
readonly | Database locked |
Parameter history
[edit]- v1.20: Introduced
pageid