Mediawiki API
download / API / ask
Script interfaces with the mw api to download files according to ask requests
API what is the API?
API
An API or application programming interface is an interface that allows interaction via other software. It allows things such as apps to be build on top of existing services, where the apps use the API to interact with the service.
Web APIs
Mediawiki API is a Web API.
Essentially it means that the API is accessed thought HTTP requests and responds using JSON or XML object.
Note: might be helpful to install a JSON add on to your browser, to pretty-prints JSON content.
Mediawiki API
Mediawiki API allows many and very intricate request for information (and editing) from Mediawiki installations.
A few examples will follow, you can request using your web browser. Including a Pretty-prints JSON extension on your browser, as it will help you read the API's responses.
Example: Page basic info
http://pzwiki.wdka.nl/mw-mediadesign/api.php?format=json&action=query&titles=Main_Page&prop=info PZI MD wiki Main Page
Example: Page content
http://pzwiki.wdka.nl/mw-mediadesign/api.php?format=json&action=query&titles=Wiki_publishing&prop=revisions&rvprop=content This page Page
action=render
action=render
Action render is a simple way of obtaining just the page content as HTML markup.
http://pzwiki.wdka.nl/mw-mediadesign/index.php?title=Wiki_publishing&action=render
Decomposing an API request
http://pzwiki.wdka.nl/mw-mediadesign/api.php? format=json & action=query & titles=Wiki_publishing & prop=revisions & rvprop=content
https://pzwiki.wdka.nl/mw-mediadesign/api.php?
- endpoint - the home page of the MediaWiki web service. It can be found by going to the Version:Special page of a wiki a searching for the API entry pointformat=json
- the format of the output. Can either be JSON of XML, although XML is being phased outaction=query
- what action to you want to performed. query is the most used, but there many more available options, such as editing and deleting pages.
Then comes the action specific parameters
titles=Wiki_publishing
- the page queriedprop=revisions
- what properties you want from of a page: revision. As no particular revision is specified the latest will be returnedrvprop=content
- what specific properties you want from the page revision in question: content. You could also ask for theuser
who created the last revision, or thecomment
left by the user. These properties can be combined in a single request.rvprop=content|user|comment
Example: Page revisions
Example: Images in a page
http://pzwiki.wdka.nl/mw-mediadesign/api.php?format=json&action=query&titles=Main_Page&prop=images
Example: categories a page belongs to
Example: Category members - Pages belonging to a category
http://pzwiki.wdka.nl/mw-mediadesign/api.php?format=json&action=query&list=categorymembers&cmtitle=Category:Special Issue
Documentation: https://www.mediawiki.org/wiki/API:Categorymembers
Examples: Contributors to a page
Example: Users of a wiki
http://pzwiki.wdka.nl/mw-mediadesign/api.php?format=json&action=query&list=allusers&aulimit=100
limit of 100 users
Semantic queries: ASK
Semantic MediaWiki includes a simple query language for Semantic search, so that users can directly request certain information from the wiki.[1].
Inside a wiki, with SMW extension installed,the page [[Special:Ask]] provides a interface for query the wiki. See Autonomous Archive's Special:Ask
Although there are many parameters possible to include, let's keep it simple and focus on syntax for Selecting pages
Selecting pages
Wiki pages
- select all pages under the File: namespace (all files)
File:+
- select all pages under the User: namespace (all users)
User:+
Semantic properties and values
- all pages/items with property Actor
Actor::+
- all pages/items with property Actor::Eetcafe
Actor::Eetcafe
Combining more than one query string
- all File: pages/items with property Production_Method::Typewriter
Files:+Production_Method::Typewriter
- all File: pages/items
Notes on selecting pages
Difference between Wiki pages and Semantic property and values:
- 1
:
wiki pagesUser:Zalán_Szakács
User:Zalán_Szakács - 2
::
semantic property valuesProduction_Method::Typewriter
Displaying information on a property can be done by include in the
?Property
to the Additional data to display window.
?Production Method
display the Production_Method values of the page?Actor|?Origin
display the Actor and Origin values of the page
Finding existing properties and their use:
- Visit the Special:Properties page
- Visit the Special:3ASearchByProperty page
Ask 2 more interfaces
Api
The same Ask requests, which we have been making the wiki Ask interface, can be made using the mediawiki API, through the ASK module[2]
Using: /api.php?action=ask&query=
https://aa.xpub.nl/api.php?action=ask&query=Production_Method::+&format=jsonfm
https://aa.xpub.nl/api.php?action=ask&query=Actor::Poortgebouw&format=jsonfm
https://aa.xpub.nl/api.php?action=ask&query=Actor::PoortgebouwProduction_Method::Typewriter&format=jsonfm
Note that:
- we start using the
[[ ]]
, which we were in the wiki ask interface - the
+
sign is encoded with URL percent encoding, becoming %2B - we are declaring the format
format=jsonfm
,format=xml
are possible
Script
With the wiki-download.py script from https://git.xpub.nl/scan-utils/ we can perform the same semantic queries, and dowload the resulting items, if they happen to be a file
Usage:
python wiki-download.py --help
ask for help
python wiki-download.py --download imgs --ask Production_Method::Typewriter
python wiki-download.py --download imgs --ask Production_Method::+Document_Type::Flyer