I am migrating our old wiki to MediaWiki 1.28.0. To that end, I have exported all of the old wiki pages and am now trying to import them to MediaWiki via the API. But when I try, I get the following response back from the server (in JSON):
{'error': {'code': 'cantimport-upload', '*': 'See http://localhost/api.php for API usage', 'info': "You don't have permission to import uploaded pages"}}
I am trying to do this in Python, but will happily accept solutions or examples using curl.
From an account in the Administrator group, I created a bot password. Here's is what I have so far:
import json
import os
import requests
bot_endpoint = 'http://localhost/api.php'
bot_username = 'Admin@Migration'
bot_password = '<censored>'
session = requests.Session()
with open('/temp/test.xml') as f:
xml_out = f.read()
# get a login token
login_token_params = {'format':'json', 'action':'query', 'meta':'tokens',
'type':'login'}
resp = session.get(bot['endpoint'], params=login_token_params)
login_token = json.loads(resp.text)['query']['tokens']['logintoken']
# login to api
login_params = {'format':'json', 'action':'login', 'lgname':bot['username'],
'lgpassword':bot['password'], 'lgtoken':login_token}
resp = session.post(bot['endpoint'], data=login_params)
# get a csrf token
csrf_token_params = {'format':'json', 'action':'query', 'meta':'tokens',
'type':'csrf'}
resp = session.get(bot['endpoint'], params=csrf_token_params)
csrf_token = json.loads(resp.text)['query']['tokens']['csrftoken']
# try to upload a file
import_params = {'format':'json', 'action':'import', 'token':csrf_token }
resp = session.post(bot['endpoint'], data=import_params,
files={'file': ('dummy.xml', xml_out)})
print(json.loads(resp.text))
When I run this, I get the error above. It's possible that I'm not uploading/importing the XML correctly, but good examples of this in python are hard to come by.
I have verified that the user is in the Administrator group (which contains the "importupload" permission). However, when I check the bot in Special:BotPasswords, I don't see any particular grants for importing files. Furthermore, following the link to Special:ListGrants, neither "import" or "importupload" appear there either.
I am certain I am barking up some kind of tree here, just don't know which is the wrong one.