SPARQL/SERVICE - mwapi

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Mediawiki API Service allows to call out to Mediawiki API from SPARQL, and receive the results from inside the SPARQL query. The query is initiated by SERVICE with URL wikibase:mwapi. Currently supported Mediawiki endpoints are: *.wikipedia.org, commons.wikimedia.org, www.mediawiki.org, www.wikidata.org, test.wikidata.org.

Currently the following services are supported:

Service Documentation Inputs Outputs Description
Generator see here 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 see here titles,cllimit category, title Get a list of categories on the page.
Search see here srsearch,srwhat,srlimit title Full-text search in wiki.
EntitySearch see here search,language,type,limit item,label Wikibase entity search, by title.

Required parameters are in bold. Please refer to the service documentation (linked in Documentation column) for the meaning of input parameters.

Please see full description on Mediawiki API Service documentation page.

Example finding members of wikipedia category[edit | edit source]

SELECT * WHERE {
  wd:Q6501349 wdt:P910 ?category . # Parking lot - Main category
  ?link schema:about ?category; schema:isPartOf <https://en.wikipedia.org/>; schema:name ?title .
  SERVICE wikibase:mwapi {
     bd:serviceParam wikibase:api "Generator" .
     bd:serviceParam wikibase:endpoint "en.wikipedia.org" .
     bd:serviceParam mwapi:gcmtitle ?title .
     bd:serviceParam mwapi:generator "categorymembers" .
     bd:serviceParam mwapi:gcmprop "ids|title|type" .
     bd:serviceParam mwapi:gcmlimit "max" .
     bd:serviceParam wikibase:limit 50 .
    # out
    ?subcat wikibase:apiOutput mwapi:title  .
    ?ns wikibase:apiOutput "@ns" .
    ?item wikibase:apiOutputItem mwapi:item .
  }
}

Try it!

Depicts statements with Dutch labels, of files in one Commons category[edit | edit source]

SELECT ?file ?title ?depicts ?depicts_label
WITH
{ SELECT ?file ?title
  WHERE
  { SERVICE wikibase:mwapi
    {
      bd:serviceParam wikibase:api "Generator" .
      bd:serviceParam wikibase:endpoint "commons.wikimedia.org" .
      bd:serviceParam mwapi:gcmtitle "Category:Historia Naturalis van Rudolf II" .
      bd:serviceParam mwapi:generator "categorymembers" .
      bd:serviceParam mwapi:gcmtype "file" .
      bd:serviceParam mwapi:gcmlimit "max" .
      ?title wikibase:apiOutput mwapi:title .
      ?pageid wikibase:apiOutput "@pageid" .
    }
    BIND (URI(CONCAT('https://commons.wikimedia.org/entity/M', ?pageid)) AS ?file)
  }
} AS %get_files
WHERE
{  INCLUDE %get_files
  ?file wdt:P180 ?depicts .
  service <https://query.wikidata.org/sparql> {
    OPTIONAL {?depicts rdfs:label ?depicts_label FILTER (lang(?depicts_label) = 'nl') } 
    }
}

Try it!

References[edit | edit source]