Mediawiki API: Difference between revisions
Andre Castro (talk | contribs) |
Andre Castro (talk | contribs) |
||
Line 15: | Line 15: | ||
===Selecting pages=== | ===Selecting pages=== | ||
Wiki pages | Wiki pages | ||
* select all pages under the File: namespace (all files) < | * select all pages under the File: namespace (all files) <nowiki>[[File:+]]</nowiki> | ||
* select all pages under the User: namespace (all users) < | * select all pages under the User: namespace (all users) '''<nowiki>[[User:+]]</nowiki>''' | ||
Semantic properties and values | Semantic properties and values | ||
* all pages/items with property Actor < | * all pages/items with property Actor '''<nowiki>[[Actor::+]] </nowiki>''' | ||
* all pages/items with property <nowiki>Actor::Eetcafe</nowiki> < | * all pages/items with property <nowiki>Actor::Eetcafe</nowiki> '''<nowiki>[[Actor::Eetcafe]]</nowiki>''' | ||
Combining more than one query string | Combining more than one query string | ||
* all File: pages/items with property Production_Method::Typewriter < | * all File: pages/items with property Production_Method::Typewriter '''<nowiki>[[Files:+]][[Production_Method::Typewriter]]</nowiki>''' | ||
* all File: pages/items | * all File: pages/items | ||
===Notes on selecting pages=== | ===Notes on selecting pages=== | ||
'''Difference between Wiki pages and Semantic property and values:''' | '''Difference between Wiki pages and Semantic property and values:''' | ||
* 1 < | * 1 '''<nowiki>:</nowiki>''' wiki pages '''<nowiki>[[User:Zalán_Szakács]]</nowiki>'''[[User:Zalán_Szakács]] | ||
* 2 < | * 2 '''<nowiki>::</nowiki>''' semantic property values '''<nowiki>[[Production_Method::Typewriter]]</nowiki>''' | ||
Displaying information on a property can be done by include in the | Displaying information on a property can be done by include in the | ||
< | '''<nowiki>?Property</nowiki>''' to the ''Additional data to display'' window. | ||
* < | * '''<nowiki>?Production Method</nowiki>''' display the Production_Method values of the page | ||
* < | * '''<nowiki>?Actor|?Origin</nowiki>''' display the Actor and Origin values of the page | ||
Finding existing properties and their use: | Finding existing properties and their use: | ||
Line 46: | Line 46: | ||
The same Ask requests, which we have been making the wiki Ask interface, can be made using the mediawiki API, through the ASK module<ref>https://www.semantic-mediawiki.org/wiki/Help:API:ask</ref> | The same Ask requests, which we have been making the wiki Ask interface, can be made using the mediawiki API, through the ASK module<ref>https://www.semantic-mediawiki.org/wiki/Help:API:ask</ref> | ||
Using: < | Using: '''<nowiki>/api.php?action=ask&query=</nowiki>''' | ||
https://aa.xpub.nl/api.php?action=ask&query=[[Production_Method::%2B]]&format=jsonfm | https://aa.xpub.nl/api.php?action=ask&query=[[Production_Method::%2B]]&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::Poortgebouw]]&format=jsonfm | ||
https://aa.xpub.nl/api.php?action=ask&query=[[Actor::Poortgebouw]][[Production_Method::Typewriter]]&format=jsonfm | https://aa.xpub.nl/api.php?action=ask&query=[[Actor::Poortgebouw]][[Production_Method::Typewriter]]&format=jsonfm | ||
Note that: | Note that: | ||
* we start using the < | * we start using the '''<nowiki>[[ ]]</nowiki>''', which we were in the wiki ask interface | ||
* the < | * the '''<nowiki>+</nowiki>''' sign is encoded with [https://en.wikipedia.org/wiki/Percent-encoding URL percent encoding], becoming %2B | ||
* we are declaring the format < | * we are declaring the format '''<nowiki>format=jsonfm</nowiki>''', '''<nowiki>format=xml</nowiki>''' are possible | ||
===Script=== | ===Script=== | ||
Line 64: | Line 64: | ||
Usage: | Usage: | ||
< | '''<nowiki>python wiki-download.py --help</nowiki>''' ask for help | ||
< | '''<nowiki>python wiki-download.py --download imgs --ask [[Production_Method::Typewriter]] </nowiki>''' | ||
< | '''<nowiki>python wiki-download.py --download imgs --ask [[Production_Method::+]][[Document_Type::Flyer]] </nowiki>''' | ||
=References= | =References= |
Revision as of 20:00, 19 November 2017
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 pages [[User:Zalán_Szakács]]User:Zalán_Szakács
- 2 :: semantic property values [[Production_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]]