User:Smalyshev (WMF)/mwapi

Mediawiki API Query Service
Mediawiki API Service allows to call out to Mediawiki API from SPARQL, and receive the results from inside the SPARQL query.

Service
The query is initiated by SERVICE with URL.

Input
Query inputs are specified as service parameters, in clause looking like: bd:serviceParam mwapi:language "en". This defines parameter " " in the query.

There are two special parameters,  and , which are always required and identify the endpoint host (wiki) and the specific API used. The API endpoints are pre-defined in configuration, see below. Currently, only pre-configured endpoints are allowed.

Example: bd:serviceParam wikibase:api "EntitySearch". bd:serviceParam wikibase:endpoint "www.wikidata.org". The rest of the parameters use predicate starting with  and the name is the name of the parameter in the query, e.g.  . 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 happens.

It is permissible to add input parameters not specified in the configuration, they will be passed to the service query. Please refer to the API documentation for the lists of parameters each service has.

Output
Mediawiki API services are asked to return the results in XML, from which they are extracted to output parameters using XPath. There are two ways to specify output parameters: by referring to the configured parameter and by specifying XPath directly.

The configured output parameter would be used like this: ?title wikibase:apiOutput mwapi:title. This results in variable  bound to the XPath result defined by " " output variable.

The direct XPath expression is used like this: ?ns wikibase:apiOutput "@ns". In this case, the object of the triple is an XPath string and not  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 are,   and. The first form results in a string taken literally, the second interprets the string as Item ID and constructs full item URI and the third interprets the string as an URI and constructs an URI value.

Configuration
The service is configured by a JSON file listing particular API configurations and a list of endpoints. Please see the example configuration.

On the top level, there is a list of endpoint hostnames under " " and API configs under " ", with the keys being service names for use with.

Only endpoints listed in  are allowed. The list can also contain prefixes - i.e.  allows any hostname that ends with.

Individual service configuration
The service configuration has two required elements -  for input parameters and   for output parameters. All the rest are currently ignored by the code, but  and   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
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 the, 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
Output is defined by , which specifies the XPath for extracting specific result elements for the query, and set of output variables under. Each key in  is variable name (to be used with  ) 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.