API:Undelete

From MediaWiki.org
Jump to navigation Jump to search
Other languages: English  • Deutsch • 日本語 • 한국어
MediaWiki version: 1.12

POST request to restore the revisions of a deleted page.

API documentation[edit]


action=undelete

(main | undelete)
  • This module requires read rights.
  • This module requires write rights.
  • This module only accepts POST requests.
  • Source: MediaWiki
  • License: GPL-2.0-or-later

Restore revisions of a deleted page.

A list of deleted revisions (including timestamps) can be retrieved through prop=deletedrevisions, and a list of deleted file IDs can be retrieved through list=filearchive.

Parameters:
title

Title of the page to restore.

This parameter is required.
reason

Reason for restoring.

Default: (empty)
tags

Change tags to apply to the entry in the deletion log.

Values (separate with | or alternative):
timestamps

Timestamps of the revisions to restore. If both timestamps and fileids are empty, all will be restored.

Type: list of timestamps (allowed formats)
Separate values with | or alternative. Maximum number of values is 50 (500 for bots).
fileids

IDs of the file revisions to restore. If both timestamps and fileids are empty, all will be restored.

Type: list of integers
Separate values with | or alternative. Maximum number of values is 50 (500 for bots).
watchlist

Unconditionally add or remove the page from the current user's watchlist, use preferences or do not change watch.

One of the following values: watch, unwatch, preferences, nochange
Default: preferences
token

A "csrf" token retrieved from action=query&meta=tokens

This parameter is required.

Example[edit]

Undeleting the revisions of a page is a multi-step process:

  1. Log in using one of the methods described in API:Login .
  2. GET a CSRF token . This token is the same for all pages but changes at every login.
  3. Send a POST request with the CSRF token in order to undelete.

The sample code below covers the third step in detail.

POST request[edit]

Restore two revisions of Sandbox/Test.

Response[edit]

{
  "undelete": {
    "fileversions": 0,
    "reason": "Test undeleting via the API",
    "revisions": 2,
    "title": "Sandbox/Test"
  }
}

Sample code[edit]

undelete.py

#!/usr/bin/python3

"""
    undelete.py

    MediaWiki API Demos
    Demo of `Undelete` module: Restore the revisions of a deleted page

    MIT License
"""

import requests

S = requests.Session()

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

# Step 1: Retrieve a login token
PARAMS_1 = {
    "action": "query",
    "meta": "tokens",
    "type": "login",
    "format": "json"
}

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

LOGIN_TOKEN = DATA['query']['tokens']['logintoken']

# Step 2: Send a post request to login.
# Obtain credentials via Special:BotPasswords
# (https://www.mediawiki.org/wiki/Special:BotPasswords) for lgname &
# lgpassword
PARAMS_2 = {
    "action": "login",
    "lgname": "username",
    "lgpassword": "password",
    "lgtoken": LOGIN_TOKEN,
    "format": "json"
}

R = S.post(URL, data=PARAMS_2)

# Step 3: While logged in, get an CSRF token
PARAMS_3 = {
    "action": "query",
    "meta": "tokens",
    "type": "csrf",
    "format": "json"
}

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

CSRF_TOKEN = DATA["query"]["tokens"]["csrftoken"]

# Step 4: Send a post request to undelete revisions of a page
PARAMS_4 = {
    "action": "undelete",
    "format": "json",
    "token": CSRF_TOKEN,
    "title": "Sandbox/Test",
    "timestamps":"20190605072148|20190605074313",
    "reason": "Test undeleting via the API"
}

R = S.post(URL, data=PARAMS_4)
DATA = R.json()

print(DATA)

Possible errors[edit]

In addition to the standard error messages :

Code Info
notitle The title parameter must be set.
notoken The token parameter must be set.
permissiondenied You don't have permission to restore deleted revisions
Note: On most wikis, restoring deleted revisions is restricted to sysops, but other wikis may have stricter rules.
cantundelete Couldn't undelete: the requested revisions may not exist, or may have been undeleted already.

Parameter history[edit]

  • v1.25: Introduced tags
  • v1.24: Introduced fileids
  • v1.16: Introduced watchlist

Additional notes[edit]

See also[edit]