| This page is part of theMediaWiki Action API documentation. |
| MediaWiki Action API |
|---|
| Basics |
| Authentication |
| Accounts and Users |
| Page Operations |
|
| Search |
| Developer Utilities |
| Tutorials |
| v · d · e |
| MediaWiki version: | ≥ 1.15 |
POST request to import a page from another wiki (transwikiing) or from an xml file.
| The following documentation is the output ofSpecial: |
Import a page from another wiki, or from an XML file.
Note that the HTTP POST must be done as a file upload (i.e. using multipart/form-data) when sending a file for thexml parameter.
Log entry import summary.
Uploaded XML file.
For uploaded imports: interwiki prefix to apply to unknown usernames (and known users ifassignknownusers is set).
For interwiki imports: wiki to import from.
For interwiki imports: page to import.
For interwiki imports: import the full history, not just the current version.
For interwiki imports: import all included templates as well.
Import to this namespace. Cannot be used together withrootpage.
Assign edits to local users where the named user exists locally.
Import as subpage of this page. Cannot be used together withnamespace.
Change tags to apply to the entry in the import log and to the dummy revision on the imported pages.
A "csrf" token retrieved fromaction=query&meta=tokens
Importing a page is a multi-step process:
The code samples below cover the third step in detail.
{"import":[{"ns":12,"revisions":639,"title":"Help:ParserFunctions"}]}
#!/usr/bin/python3""" import_interwiki.py MediaWiki Action API Code Samples Demo of `Import` module: Import a page from another wiki by specifying its title MIT license"""importrequestsS=requests.Session()URL="https://test.wikipedia.org/w/api.php"# Step 1: Retrieve a login tokenPARAMS_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 log in using the clientlogin method.# import rights can't be granted using Special:BotPasswords# hence using bot passwords may not work.# See https://www.mediawiki.org/wiki/API:Login for more# information on log in methods.PARAMS_2={"action":"clientlogin","username":"username","password":"password",'loginreturnurl':'http://127.0.0.1:5000/',"format":"json","logintoken":LOGIN_TOKEN}R=S.post(URL,data=PARAMS_2)# Step 3: While logged in, retrieve a CSRF tokenPARAMS_3={"action":"query","meta":"tokens","format":"json"}R=S.get(url=URL,params=PARAMS_3)DATA=R.json()CSRF_TOKEN=DATA['query']['tokens']['csrftoken']# Step 4: Post request to import page from another wikiPARAMS_4={"action":"import","format":"json","interwikisource":"meta","interwikipage":"Help:ParserFunctions","fullhistory":"true","namespace":"100","token":CSRF_TOKEN}R=S.post(url=URL,data=PARAMS_4)DATA=R.json()print(DATA)
<?php/* import_interwiki.php MediaWiki API Demos Demo of `Import` module: Import a page from another wiki byspecifying its title MIT license*/$endPoint="http://dev.wiki.local.wmftest.net:8080/w/api.php";$login_Token=getLoginToken();// Step 1loginRequest($login_Token);// Step 2$csrf_Token=getCSRFToken();// Step 3import($csrf_Token);// Step 4// Step 1: GET request to fetch login tokenfunctiongetLoginToken(){global$endPoint;$params1=["action"=>"query","meta"=>"tokens","type"=>"login","format"=>"json"];$url=$endPoint."?".http_build_query($params1);$ch=curl_init($url);curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);curl_setopt($ch,CURLOPT_COOKIEJAR,"cookie.txt");curl_setopt($ch,CURLOPT_COOKIEFILE,"cookie.txt");$output=curl_exec($ch);curl_close($ch);$result=json_decode($output,true);return$result["query"]["tokens"]["logintoken"];}// Step 2: POST request to log in. Use of main account for login is not// supported. Obtain credentials via Special:BotPasswords// (https://www.mediawiki.org/wiki/Special:BotPasswords) for lgname & lgpasswordfunctionloginRequest($logintoken){global$endPoint;$params2=["action"=>"clientlogin","username"=>"username","password"=>"password",'loginreturnurl'=>'http://127.0.0.1:5000/',"logintoken"=>$logintoken,"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($params2));curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);curl_setopt($ch,CURLOPT_COOKIEJAR,"cookie.txt");curl_setopt($ch,CURLOPT_COOKIEFILE,"cookie.txt");$output=curl_exec($ch);curl_close($ch);}// Step 3: GET request to fetch CSRF tokenfunctiongetCSRFToken(){global$endPoint;$params3=["action"=>"query","meta"=>"tokens","format"=>"json"];$url=$endPoint."?".http_build_query($params3);$ch=curl_init($url);curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);curl_setopt($ch,CURLOPT_COOKIEJAR,"cookie.txt");curl_setopt($ch,CURLOPT_COOKIEFILE,"cookie.txt");$output=curl_exec($ch);curl_close($ch);$result=json_decode($output,true);return$result["query"]["tokens"]["csrftoken"];}// Step 4: POST request to import page from another wikifunctionimport($csrftoken){global$endPoint;$params4=["action"=>"import","interwikisource"=>"wikipedia:en","interwikipage"=>"Pragyan (rover)","namespace"=>"0","fullhistory"=>"true","token"=>$csrftoken,"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($params4));curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);curl_setopt($ch,CURLOPT_COOKIEJAR,"cookie.txt");curl_setopt($ch,CURLOPT_COOKIEFILE,"cookie.txt");$output=curl_exec($ch);curl_close($ch);echo($output);}
/* import_interwiki.js MediaWiki API Demos Demo of `Import` module: Import a page from another wiki byspecifying its title MIT license*/varrequest=require('request').defaults({jar:true}),url="http://dev.wiki.local.wmftest.net:8080/w/api.php";// Step 1: GET request to fetch login tokenfunctiongetLoginToken(){varparams_0={action:"query",meta:"tokens",type:"login",format:"json"};request.get({url:url,qs:params_0},function(error,res,body){if(error){return;}vardata=JSON.parse(body);loginRequest(data.query.tokens.logintoken);});}// Step 2: POST request to log in.// Use of main account for login is not// supported. Obtain credentials via Special:BotPasswords// (https://www.mediawiki.org/wiki/Special:BotPasswords) for lgname & lgpasswordfunctionloginRequest(login_token){varparams_1={action:"clientlogin",username:"username",password:"password",loginreturnurl:"http://127.0.0.1:5000/",logintoken:login_token,format:"json"};request.post({url:url,form:params_1},function(error,res,body){if(error){return;}getCsrfToken();});}// Step 3: GET request to fetch CSRF tokenfunctiongetCsrfToken(){varparams_2={action:"query",meta:"tokens",format:"json"};request.get({url:url,qs:params_2},function(error,res,body){if(error){return;}vardata=JSON.parse(body);import_interwiki(data.query.tokens.csrftoken);});}// Step 4: POST request to import page from another wikifunctionimport_interwiki(csrf_token){varparams_3={action:"import",interwikisource:"wikipedia:en",interwikipage:"Pragyan (rover)",namespace:"0",fullhistory:"true",token:csrf_token,format:"json"};request.post({url:url,form:params_3},function(error,res,body){if(error){return;}console.log(body);});}// Start From Step 1getLoginToken();
/*import_interwiki.jsMediaWiki API DemosDemo of `Import` module: Import a page from another wiki by specifying its titleMIT License*/varparams={action:'import',interwikisource:'en:w',interwikipage:'Template:!-',fullhistory:'true',namespace:'100',format:'json'},api=newmw.Api();api.postWithToken('csrf',params).done(function(data){console.log(data);});
sourcelib.sh# includes the get-token function from API:TokensMW_URL="https://test.wikipedia.org/w/"API_URL="api.php"MW_PREF="en:w"PAGE_NAME=""# need login first via the code in API:Loginfunctionpage-install(){page_name="$1"API_URL="api.php?action=import&format=json"RESULT=$(curl-fsSL-XPOST\-d"summary=$BOT_INFO$MW_PREF"\-d"interwikisource=$MW_PREF"\-d"interwikipage=$1"\-d"assignknownusers=1"\-d"templates=1"\--data-urlencode"token=$(get-tokencsrf)"\-ccookie.txt\-bcookie.txt\"${MW_URL}${API_URL}")echo$RESULT}page-install"$PAGE_NAME"
ImportHelp:Extension:ParserFunctions by uploading its xml dump obtained fromSpecial:Export.
When uploading a file, you need to usemultipart/form-data as Content-Type or enctype,application/x-www-form-urlencoded will not work.
xml is not a file name, but the actual content of a file.| Response |
|---|
{"import":[{"ns":12,"title":"Help:ParserFunctions","revisions":639}]} |
#!/usr/bin/python3""" import_xml.py MediaWiki Action API Code Samples Demo of `Import` module: Import a page from another wiki by uploading its xml dump MIT license"""importrequestsS=requests.Session()URL="https://test.wikipedia.org/w/api.php"FILE_PATH='/path/to/your/file.xml'# Step 1: Retrieve a login tokenPARAMS_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 log in using the clientlogin method.# importupload rights can't be granted using Special:BotPasswords# hence using bot passwords may not work.# See https://www.mediawiki.org/wiki/API:Login for more# information on log in methods.PARAMS_2={"action":"clientlogin","username":"username","password":"password",'loginreturnurl':'http://127.0.0.1:5000/',"format":"json","logintoken":LOGIN_TOKEN}R=S.post(URL,data=PARAMS_2)# Step 3: While logged in, retrieve a CSRF tokenPARAMS_3={"action":"query","meta":"tokens","format":"json"}R=S.get(url=URL,params=PARAMS_3)DATA=R.json()CSRF_TOKEN=DATA['query']['tokens']['csrftoken']# Step 4: Post request to upload xml dump.# xml dumps can be downloaded through Special:Export# See https://www.mediawiki.org/wiki/Special:ExportPARAMS_4={"action":"import","format":"json","token":CSRF_TOKEN,"interwikiprefix":"meta"}FILE={'xml':('file.xml',open(FILE_PATH))}R=S.post(url=URL,files=FILE,data=PARAMS_4)DATA=R.json()print(DATA)
/* import_xml.js MediaWiki API Demos Demo of `Import` module: Import a page from another wiki by uploading its xml dump MIT license*/varfs=require('fs'),request=require('request').defaults({jar:true}),url="http://dev.wiki.local.wmftest.net:8080/w/api.php";// Step 1: GET request to fetch login tokenfunctiongetLoginToken(){varparams_0={action:"query",meta:"tokens",type:"login",format:"json"};request.get({url:url,qs:params_0},function(error,res,body){if(error){return;}vardata=JSON.parse(body);loginRequest(data.query.tokens.logintoken);});}// Step 2: POST request to log in.// Use of main account for login is not// supported. Obtain credentials via Special:BotPasswords// (https://www.mediawiki.org/wiki/Special:BotPasswords) for lgname & lgpasswordfunctionloginRequest(login_token){varparams_1={action:"clientlogin",username:"username",password:"password",loginreturnurl:"http://127.0.0.1:5000/",logintoken:login_token,format:"json"};request.post({url:url,form:params_1},function(error,res,body){if(error){return;}getCsrfToken();});}// Step 3: GET request to fetch CSRF tokenfunctiongetCsrfToken(){varparams_2={action:"query",meta:"tokens",format:"json"};request.get({url:url,qs:params_2},function(error,res,body){if(error){return;}vardata=JSON.parse(body);import_xml(data.query.tokens.csrftoken);});}// Step 4: POST request to upload xml dump.// xml dumps can be downloaded through Special:Export// See https://www.mediawiki.org/wiki/Special:Exportfunctionimport_xml(csrf_token){varparams_3={action:"import",interwikiprefix:"en",token:csrf_token,format:"json"};varfile={xml:fs.createReadStream('a.xml')};varformData=Object.assign({},params_3,file);request.post({url:url,formData:formData},function(error,res,body){if(error){return;}console.log(body);});}// Start From Step 1getLoginToken();
In addition tothe standard error messages:
| Code | Info |
|---|---|
| notoken | Thetoken parameter must be set. |
| cantimport | You don't have permission to import pages. |
| cantimport-upload | You don't have permission to import uploaded pages. |
| nointerwikipage | Theinterwikipage parameter must be set. |
| nofile | You didn't upload a file |
| filetoobig | The file you uploaded is bigger than the maximum upload size |
| partialupload | The file was only partially uploaded |
| notempdir | The temporary upload directory is missing This generally means the server is broken or misconfigured |
| cantopenfile | Couldn't open the uploaded file This generally means the server is broken or misconfigured |
| badinterwiki | Invalid interwiki title specified |
| import-unknownerror | Unknown error on import:error. |
tagsrootpageimportupload rights are required in order to upload an xml file, whileimport rights are required for interwiki imports.Missing boundary in multipart/form-data POST data error, it is because you sent it url-encoded but claimed it would be multipart/form-data. MediaWiki is looking for a boundary in the header but cannot find it.upload are only used when importing an uploaded XML file. Similarly, parameters marked withinterwiki are only used when importing from another wiki (transwiki).interwikisource parameter differ per wiki, seeManual:$wgImportSources. If the list of possible values for this parameter is empty, interwiki imports are disabled.