API:復元

From MediaWiki.org
Jump to navigation Jump to search
This page is a translated version of the page API:Undelete and the translation is 61% complete.

Other languages:
Deutsch • ‎English • ‎polski • ‎日本語 • ‎한국어
MediaWiki バージョン: 1.12

削除されたページの版を復元する POST リクエストです。

APIの説明文書


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.

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

  1. Log in using one of the methods described in API:ログイン .
  2. CSRF トークン を取得します。 このトークンはすべてのページで同じ値ですが、ログインするたびに変化します。
  3. Send a POST request with the CSRF token in order to undelete.

The sample code below covers the third step in detail.

POST リクエスト

Restore two revisions of Sandbox/Test.

レスポンス

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

サンプル コード

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)

起こりうるエラー

In addition to the standard error messages :

コード 情報
notitle パラメーター title を設定してください。
notoken パラメーター token を設定してください。
permissiondenied ページの復元に必要な権限がありません。
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.

パラメーターの履歴

  • v1.25: tags を導入しました
  • v1.24: fileids を導入しました
  • v1.16: watchlist を導入しました

追加的な注記

関連項目