MediaWiki API Service allows to call out toMediaWiki API from SPARQL, and receive the results from inside the SPARQL query.
The query is initiated by SERVICE with URLwikibase:mwapi.
Query inputs are specified as service parameters, in a clause such as this:
bd:serviceParammwapi:language"en".
This defines the parameter "language" in the query.
There are two special parameters,api andendpoint, which are mandatory and identify the endpoint host (wiki) and the specific API used. The API endpoints are pre-defined in configuration; see below.
On the Wikidata Query Service, all Wikimedia sites are allowed (all Wikipedia language editions, Wikimedia Commons, Wikidata itself, etc.).
Example:
bd:serviceParamwikibase:api"EntitySearch". bd:serviceParamwikibase:endpoint"www.wikidata.org".
The rest of the parameters use predicates starting withmwapi: and the name is the name of the parameter in the query, e.g.mwapi:srsearch. The value is the value that will be used in the query; it can be a constant or a variable. If the variable is not bound when the query is run, the parameter is skipped or, if it is required (as defined by the configuration), an error occurs.
It is permissible to add input parameters not specified in the configuration; they will be passed to the service query. Please refer to theAPI documentation for the parameters each service has. The configuration also defines some default parameters. If you override them in the service parameters, you may want to merge the default value with your desired value to ensure the output looks as expected by MWAPI (e. g. usemwapi:ppprop "wikibase_item|page_image_free" instead of justmwapi:ppprop "page_image_free").
By default, WDQS will try to fetch all the results of the query, with continuations. You can set the page size by using service-specific limit parameters, but the service will use continuation to fetch the following pages. This can be controlled by thewikibase:limit parameter, which will limit total number of rows fetched in one API request:
bd:serviceParamwikibase:limit50.
This will fetch only 50 rows, regardless of pagination settings. Note that SPARQLLIMIT clause will cause the same effect, but will be applied only after the API call has finished - all the data has been fetched and then the extra data has been discarded. It is more efficient to use the internal limit.
A special value of "once" disables the continuation mechanism:
bd:serviceParamwikibase:limit"once".
This makes the service to stop after the first API call and not use continuations. The number of results depends on service parameters.
There is a hard-coded limit of 10000 rows which always applies.
Two additional limit parameters exist to fine-tune some requests (see the originalGerrit patch):
bd:serviceParamwikibase:limitContinuations"3". to limit the number of internally requested continuations (by default 1000 continuations, hard-coded limit of 1000);bd:serviceParamwikibase:limitEmptyContinuations"200". to limit the number of empty consecutive continuations (when N empty consecutive continuations is reached, the global request to MediaWiki API is ended) (by default 25 empty continuations, hard-coded limit of 1000).MediaWiki API services are asked to return the results in XML, from which they are extracted to output parameters usingXPath. There are two ways to specify output parameters: by referring to the configured parameter and by specifying XPath directly.
The configured output parameter could be used like this:
?titlewikibase:apiOutputmwapi:title.
This results in the variable?title being bound to the XPath result defined by the "title" output variable. Note that the configured output parameters expect certain input parameters to be present, and if you override the configured defaults the output parameters may break: for example, the configuration specifies a default parameter ofprop=info|pageprops, and if you override it withmwapi:prop "revisions", then themwapi:lastrevid output parameter will no longer be able to get thelastrevid from the response. (You would fix this by looking up the defaultprop in the configuration and then changing your query to usemwapi:prop "info|pageprops|revisions".)
The direct XPath expression is used like this:
?nswikibase:apiOutput"@ns".
In this case, the object of the triple is an XPath string and notmwapi: URI.
In both cases, XPath is evaluated relative to item path, specified in service configuration. There's no difference in XPath evaluation for both forms.
The predicates that can be used arewikibase:apiOutput,wikibase:apiOutputItem andwikibase:apiOutputURI. The first form results in a string taken literally; the second interprets the string as Item ID and constructs a full item URI; and the third interprets the string as an URI and constructs an URI value.
Predicatewikibase:apiOrdinal allows to define variable that would be set to the number of the result in the original API output. Note that due to the multithreaded nature of the SPARQL engine the results are not necessarily returned in the order in which they were received by the API, which may be useful for queries like search where result order is important. Usingwikibase:apiOrdinal will allow to recover the original order. The object of the triple is currently ignored.
?numwikibase:apiOrdinaltrue.
Currently the following services are supported:
| Service | Inputs | Outputs | Description |
|---|---|---|---|
| Generator | generator, prop, pprop | title, item, pageid, lastrevid, timestamp | Call any generator API. Use "generator" parameter to specify, and specific generator parameters to further amend the search (see the example below). |
| Categories | titles,cllimit | category, title | Get a list of categories on the page. |
| Search | srsearch,srwhat,srlimit | title | Full-text search in wiki. |
| EntitySearch | search,language,type,limit | item,label | Wikibase entity search, by title. |
Required parameters are in bold. Other input parameters may also be supported – please refer to the service documentation (linked in Service column) for supported API parameters and their meaning.
The full list of services can be seen in theproduction config file.
The service is configured by a JSON file listing particular API configurations and a list of endpoints. Please see theexample configuration.
On the top level, there is a list of endpoint hostnames under "endpoints" and API configs under "services", with the keys being service names for use withwikibase:api.
Only endpoints listed inendpoints are allowed. The list can also contain prefixes - i.e..wikipedia.org allows any hostname that ends with.wikipedia.org.
The service configuration has two required elements -params for input parameters andoutput for output parameters. All the rest are currently ignored by the code, but@note and@docs are used to store description and link to documentation about particular API. It is safe to assume that config entries starting with @ will be always ignored by the code.
Input parameters are keyed by parameter name, and the value can be either constant - in which case the parameter is fixed, always present and its value can not be changed, or object, which contains description of variable parameter.
Variable parameter is bound by the query template or variable. The configuration specifies its type, which is currently ignored but may be enforced later. The configuration can also specify thedefault, which will be used in case the parameter is not specified or not bound. If the default is"",the parameter will be omitted in case it is not specified or not bound. If no default is specified, the parameter is treated as required and omitting it will result in an error.
Output is defined byitems , which specifies the XPath for extracting specific result elements for the query, and set of output variables undervars. Each key invars is variable name (to be used withwikibase:apiOutput) and the value is XPath to this particular value, calculated relative to the items path. Note that it is possible to also specify absolute paths for specific variables, but in that case you will get copies of that value for each result set.
SELECT*WHERE{SERVICEwikibase:mwapi{bd:serviceParamwikibase:endpoint"www.wikidata.org";wikibase:api"EntitySearch";mwapi:search"cheese";mwapi:language"en".?itemwikibase:apiOutputItemmwapi:item.?numwikibase:apiOrdinaltrue.}?item(wdt:P279|wdt:P31)?type}ORDER BYASC(?num)LIMIT20
SELECT*WHERE{SERVICEwikibase:mwapi{bd:serviceParamwikibase:endpoint"en.wikipedia.org";wikibase:api"Search";mwapi:srsearch"cheese".?titlewikibase:apiOutputmwapi:title.}}LIMIT20
SELECT?item?itemLabel?type?typeLabelWHERE{{SELECT?itemWHERE{SERVICEwikibase:mwapi{bd:serviceParamwikibase:endpoint"en.wikipedia.org";wikibase:api"Generator";mwapi:generator"search";mwapi:gsrsearch"cheese";mwapi:gsrlimit"max".?itemwikibase:apiOutputItemmwapi:item.}}LIMIT100}hint:Priorhint:runFirst"true".?itemwdt:P31|wdt:P279?type.SERVICEwikibase:label{bd:serviceParamwikibase:language"en".}}LIMIT100
SELECT*WHERE{wd:Q6501349wdt:P910?category.?linkschema:about?category;schema:isPartOf<https://commons.wikimedia.org/>;schema:name?title.SERVICEwikibase:mwapi{# inbd:serviceParamwikibase:endpoint"commons.wikimedia.org";wikibase:api"Generator";mwapi:generator"categorymembers";mwapi:gcmtitle?title;mwapi:gcmprop"ids|title|type";mwapi:gcmlimit"max".# out?memberwikibase:apiOutputmwapi:title.?nswikibase:apiOutput"@ns".?itemwikibase:apiOutputItemmwapi:item.}# FILTER (?ns = "14")}
SELECT*WHERE{BIND(wd:Q23040282AS?item)?itemwdt:P18?image.BIND(CONCAT("File:",STRAFTER(wikibase:decodeUri(STR(?image)),"http://commons.wikimedia.org/wiki/Special:FilePath/"))AS?fileTitle)# Query the MediaWiki API Query Service forSERVICEwikibase:mwapi{# Categories that contain these pagesbd:serviceParamwikibase:api"Categories";wikibase:endpoint"commons.wikimedia.org";wikibase:limit50;mwapi:titles?fileTitle.# Output the page title and category?titlewikibase:apiOutputmwapi:title.?categorywikibase:apiOutputmwapi:category.}}
SELECT*WHERE{BIND(wd:Q23040282AS?item)?itemwdt:P18?image.BIND(CONCAT("File:",STRAFTER(wikibase:decodeUri(STR(?image)),"http://commons.wikimedia.org/wiki/Special:FilePath/"))AS?fileTitle)# Query the MediaWiki API Query Service forSERVICEwikibase:mwapi{bd:serviceParamwikibase:endpoint"fi.wikipedia.org";wikibase:api"Generator";mwapi:generator"imageusage";mwapi:giutitle?fileTitle;mwapi:gsrlimit"max".?image_used_in_fiwikiwikibase:apiOutputItemmwapi:item.}}
Copied fromphab:T157798#5919648
SELECT*WHERE{BIND(wd:Q42AS?item)?itemwdt:P18?image.BIND(STRAFTER(wikibase:decodeUri(STR(?image)),"http://commons.wikimedia.org/wiki/Special:FilePath/")AS?fileTitle)SERVICEwikibase:mwapi{bd:serviceParamwikibase:endpoint"commons.wikimedia.org";wikibase:api"Generator";wikibase:limit"once";mwapi:generator"allpages";mwapi:gapfrom?fileTitle;mwapi:gapnamespace6;# NS_FILEmwapi:gaplimit1;mwapi:prop"imageinfo";mwapi:iiprop"dimensions".?sizewikibase:apiOutput"imageinfo/ii/@size".?widthwikibase:apiOutput"imageinfo/ii/@width".?heightwikibase:apiOutput"imageinfo/ii/@height".}}
SELECT*WHERE{SERVICEwikibase:mwapi{bd:serviceParamwikibase:endpoint"commons.wikimedia.org";wikibase:api"Generator";mwapi:generator"allimages";mwapi:gailimit1000;mwapi:gaiuser"zache";mwapi:gaisort"timestamp";mwapi:prop"url".?titlewikibase:apiOutputmwapi:title.?pageidwikibase:apiOutputmwapi:pageid.}BIND(URI(CONCAT("https://commons.wikimedia.org/entity/M",STR(?pageid)))as?sdc_item)}
SELECT*WHERE{BIND(wd:Q42AS?item)?itemwdt:P18?image.BIND(STRAFTER(wikibase:decodeUri(STR(?image)),"http://commons.wikimedia.org/wiki/Special:FilePath/")AS?fileTitle)SERVICEwikibase:mwapi{bd:serviceParamwikibase:endpoint"commons.wikimedia.org";wikibase:api"Generator";wikibase:limit"once";mwapi:generator"allpages";mwapi:gapfrom?fileTitle;mwapi:gapnamespace6;# NS_FILEmwapi:gaplimit1;mwapi:prop"revisions";mwapi:rvprop"content".?contentmodelwikibase:apiOutput'revisions/rev/@contentmodel'.?contentformatwikibase:apiOutput'revisions/rev/@contentformat'.?contentwikibase:apiOutput'revisions/rev/text()'.}}
SELECTDISTINCT?item?titleWHERE{SERVICEwikibase:mwapi{bd:serviceParamwikibase:endpoint"en.wikipedia.org";wikibase:api"Generator";mwapi:generator"links";mwapi:titles"List of bird species described in the 2020s";mwapi:gplnamespace"0";mwapi:redirects"1";mwapi:gpllimit"max".?itemwikibase:apiOutputItemmwapi:item.?titlewikibase:apiOutputmwapi:title.}}