Fetching data with the MediaWiki API

Submitted by urvashi on Fri, 07/20/2018 - 08:26

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 API
https://en.wikipedia.org/w/api.php # English Wikipedia API
https://nl.wikipedia.org/w/api.php # Dutch Wikipedia API
https://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: jsonfmnonephpphpfmrawfmxmlxmlfm

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:

https://commons.wikimedia.org/w/api.php?action=query&titles=File:Sala%20de%20Conciertos,%20Berl%C3%ADn,%20Alemania,%202016-04-22,%20DD%2022-24%20HDR.jpg&prop=info

To fetch information about multiple images using page IDs:

https://commons.wikimedia.org/w/api.php?action=query&pageids=69796122|69582727|51186303&prop=imageinfo&format=json

To fetch metadata and urls using iiprop:

https://commons.wikimedia.org/w/api.php?action=query&pageids=69796122|69582727|51186303&prop=imageinfo&iiprop=extmetadata|url

You can play with the API Sandbox to experiment with the MediaWiki API.

 

Add new comment

Tags