Using Wikibooks/Scripting and the MediaWiki API

While this section is most useful for administrators, any user can make use of the MediaWiki API, and hence this section should benefit any Wikibookian.

The MediaWiki API
MediaWiki offers a powerful API tool that can allow you to perform virtually any task that you can do on-wiki using API calls. Consider the following example:

https://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=Pet_door&rvslots=*&rvprop=content&formatversion=2

Try it on your web browser. You'll get a page that looks like this: MediaWiki API result

This is the HTML representation of the JSON format. HTML is good for debugging, but is unsuitable for application use.

Specify the format parameter to change the output format. To see the non-HTML representation of the JSON format, set format=json.

See the complete documentation, or the API help for more information.

and a bunch of JSON text, which contains the content of the title "Pet door".

How does this help? Well, if you want to get the details of 100 articles, you do not have to manually visit all of them! Just use a simple bash script that would do this work for you.

Scripting
Now, how does this apply to a Wikibooks administrator? Suppose you're working on a large deletion request. If there are 500 of the pages, manually deleting each page is likely to take hours and cause frustration to you! Instead, use a Python 3 script! The MediaWiki API page on MediaWiki.org contains a list of all such API calls, and contains helpful example code from which the code in this page was derived from.

First, the script. Here it is. The annotations explain what's going on.

So how do you use it? Put this in a Python file, put all the pages to delete in, and run it. Watch the output - if there are errors, MediaWiki will let you know. Common issues include


 * cannot find the requests module - install it using
 * not adapting the script. For instance, if you're performing undeletions, you may want to put the new pages in a different location from the old ones. Make sure that they work as expected! You may want to perform a test run first.

If you're stuck at any point, just ask at WB:RR.

Now, this can be easily adapted. Suppose instead of deleting, you want to undelete these pages. Then all that is needed is replace  to

Even non-admins can benefit from the use of the script. Suppose you're trying to mass-move pages. In that case,  can be changed to

While unlikely, it is possible to run into ratelimit issues when running scripts such as these. In that case, if you're not yet a reviewer, the best choice would be to become one.