Extension:Education Program/API deletion

From MediaWiki.org
Jump to: navigation, search

The API for this extension allows deletion of courses and organizations by ID instead of by name. This is useful in cases where the list of courses/orgs shows duplicates, because the same one has more than one ID. You can delete one of the IDs to remove the duplicate. This python script is one way to do that.

Instructions
  1. Find the ID of the course or org you want to delete. The IDs show up in the html source of Special:Courses and other lists of courses (if you are logged in and have the right to delete courses).
  2. Save this script (for example, as "api-deleteeducation.py") on a computer with python installed, and change the lines in the CONFIGURATION section.
  3. Run the script (for example, with the command "python api-deleteeducation.py") from the command line.
# This is a script for deleting [[mw:Extension:Education Program]] courses or organizations by ID, useful for when one course has multiple IDs and so shows up more than once in the list of courses. 
 
import urllib2
import urllib
import cookielib
import json
 
# CONFIGURATION: change these before you run the script.
 
username = 'YOUR USERNAME' # Your account must have the right to delete courses. 
password = 'YOUR PASSWORD'
idtype = 'course' # Change to 'org' if you want to delete an organization instead of a course
ids = 12345 # Specify the ID of the course/org you want to delete, or multiple IDs separated by pipes
url = 'https://test2.wikipedia.org/w/api.php' # Change this to the wiki you want
 
 
# cookie handling, so that the script will automatically return the cookie from the first API request
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
 
# API requires a user-agent header
agent = 'api-deleteeducation.py/0.1 (https://www.mediawiki.org/wiki/Extension:Education_Program/API_deletion)'
opener.addheaders = [
    ('User-Agent', agent ),
	]
 
 
# Login: first fetch a login token, then use it for login, per [[mw:API:Login]]
login_args = { 'action':'login', 'format':'json', 'lgname':username, 'lgpassword':password }
login_data = urllib.urlencode(login_args)
login_request = urllib2.Request(url, login_data)
login_response = opener.open(login_request)
login_info = login_response.read()
print login_info
loginfo = json.loads(login_info)
logintoken = loginfo['login']['token']
login_args2 = { 'action':'login', 'format':'json', 'lgname':username, 'lgpassword':password, 'lgtoken':logintoken }
login_data2 = urllib.urlencode(login_args2)
login_request2 = urllib2.Request(url, login_data2)
login_request2.add_header('User-Agent', agent)
login_response2 = opener.open(login_request2)
login_info2 = login_response2.read()
print login_info2
 
 
# Get an edit token, which is required for the 'deleteeducation' action
edit_token_args = { 'action':'tokens', 'format':'json' }
edit_token_data = urllib.urlencode(edit_token_args)
edit_token_request = urllib2.Request(url, edit_token_data)
edit_token_response = opener.open(edit_token_request)
edit_token_info = edit_token_response.read()
print edit_token_info
edittokenfo = json.loads(edit_token_info)
edittoken = edittokenfo['tokens']['edittoken']
 
 
# Perform the deletion
course_delete_args = { 'action':'deleteeducation', 'format':'json', 'ids':ids, 'type':idtype, 'comment':'testing api deletion', 'token':edittoken }
course_delete_data = urllib.urlencode(course_delete_args)
course_delete_request = urllib2.Request(url, course_delete_data)
course_delete_response = opener.open(course_delete_request)
result = course_delete_response.read()
 
 
print result