Recently, I worked on adding more metadata from Wikimedia Commons about the uploads done during a course on the Programs & Events Dashboard using the MediaWiki API.
The MediaWiki action API can be used to monitor a MediaWiki installation, or create a bot to automatically maintain one. It provides direct, high-level access to the data contained in MediaWiki databases. Client programs can log in to a wiki, get data, and post changes automatically by making HTTP requests to the web service.
The endpoint
https://en.wikipedia.org/w/api.php
(or any other wiki)
All Wikimedia wikis have endpoints that follow this pattern:
https://www.mediawiki.org/w/api.php
# MediaWiki APIhttps://en.wikipedia.org/w/api.php
# English Wikipedia APIhttps://nl.wikipedia.org/w/api.php
# Dutch Wikipedia APIhttps://commons.wikimedia.org/w/api.php
# Wikimedia Commons API
Since, we were interested in working with the media uploaded on Commons, I used the Wikimedia Commons API and will be using this as the endpoint for the rest of this post.
Parameters
format — The format of the output
json: Output data in JSON format.
More format values can be: jsonfm, none, php, phpfm, rawfm, xml, xmlfm
action — Which action to perform
query: Fetch data from and about MediaWiki.
You can read about other values for action parameter here.
Action-specific parameters
titles= takes one or more titles for the query to operate on.
pageids= takes one or more page ids for the query to operate on. revids= takes a list of revision IDs to work on.
Separate the above values with | or alternative. Maximum number of values is 50 (500 for bots).
prop= which properties to get for the queried pages.
Read more here.
Example queries
To fetch basic info using titles with HTML representation of the JSON format:
To fetch information about multiple images using page IDs:
To fetch metadata and urls using iiprop
:
You can play with the API Sandbox to experiment with the MediaWiki API.