Mediawiki API: Difference between revisions

From XPUB & Lens-Based wiki
Line 15: Line 15:
===Selecting pages===
===Selecting pages===
Wiki pages
Wiki pages
* select all pages under the File: namespace (all files) <code>[[File:+]]</code>
* select all pages under the File: namespace (all files) <nowiki>[[File:+]]</nowiki>
* select all pages under the User: namespace (all users) <code>[[User:+]]</code>
* 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 <code>[[Actor::+]] </code>
* all pages/items with property Actor '''<nowiki>[[Actor::+]] </nowiki>'''
* all pages/items with property <nowiki>Actor::Eetcafe</nowiki> <code>[[Actor::Eetcafe]]</code>
* 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 <code>[[Files:+]][[Production_Method::Typewriter]]</code>
* 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 <code>:</code> wiki pages <code>[[User:Zalán_Szakács]]</code>[[User:Zalán_Szakács]]  
* 1 '''<nowiki>:</nowiki>''' wiki pages '''<nowiki>[[User:Zalán_Szakács]]</nowiki>'''[[User:Zalán_Szakács]]  
* 2 <code>::</code> semantic property values <code>[[Production_Method::Typewriter]]</code>
* 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  
<code>?Property</code> to the ''Additional data to display'' window.
'''<nowiki>?Property</nowiki>''' to the ''Additional data to display'' window.
* <code>?Production Method</code> display the Production_Method values of the page
* '''<nowiki>?Production Method</nowiki>''' display the Production_Method values of the page
* <code>?Actor|?Origin</code> display the Actor and Origin 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: <code>/api.php?action=ask&query=</code>
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 <code>[[ ]]</code>, which we were in the wiki ask interface
* we start using the '''<nowiki>[[ ]]</nowiki>''', which we were in the wiki ask interface
* the <code>+</code> sign is encoded with [https://en.wikipedia.org/wiki/Percent-encoding URL percent encoding], becoming %2B
* the '''<nowiki>+</nowiki>''' sign is encoded with [https://en.wikipedia.org/wiki/Percent-encoding URL percent encoding], becoming %2B
* we are declaring the format <code>format=jsonfm</code>, <code>format=xml</code> are possible
* we are declaring the format '''<nowiki>format=jsonfm</nowiki>''', '''<nowiki>format=xml</nowiki>''' are possible


===Script===
===Script===
Line 64: Line 64:


Usage:
Usage:
<code>python wiki-download.py --help</code> ask for help
'''<nowiki>python wiki-download.py --help</nowiki>''' ask for help


<code>python wiki-download.py --download imgs --ask [[Production_Method::Typewriter]]  </code>
'''<nowiki>python wiki-download.py --download imgs --ask [[Production_Method::Typewriter]]  </nowiki>'''


<code>python wiki-download.py --download imgs --ask [[Production_Method::+]][[Document_Type::Flyer]]  </code>
'''<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

http://pzwiki.wdka.nl/mw-mediadesign/api.php?format=json&action=query&titles=Wiki_publishing&prop=info This 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 point
  • format=json - the format of the output. Can either be JSON of XML, although XML is being phased out
  • action=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 queried
  • prop=revisions - what properties you want from of a page: revision. As no particular revision is specified the latest will be returned
  • rvprop=content - what specific properties you want from the page revision in question: content. You could also ask for the user who created the last revision, or the comment 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

http://pzwiki.wdka.nl/mw-mediadesign/api.php?format=json&action=query&titles=Wiki_publishing&prop=categories

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

http://pzwiki.wdka.nl/mw-mediadesign/api.php?format=json&action=query&titles=Main_Page&prop=contributors

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:

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]]

References