411 REFERENCE · Wikibooks
SPARQL/SERVICE - mwapi
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
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:
ServiceDocumentationInputsOutputsDescriptionGenerator see heregenerator, prop, pproptitle, item, pageid, lastrevid, timestampCall any generator API. Use "generator" parameter to specify, and specific generator parameters to further amend the search (see the example below).Categoriessee heretitles,cllimitcategory, titleGet a list of categories on the page.Searchsee heresrsearch,srwhat,srlimittitleFull-text search in wiki.EntitySearchsee heresearch,language,type,limititem,labelWikibase 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
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 "idstitletype" .
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 .
}
}
Depicts statements with Dutch labels, of files in one Commons category
sdc 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') }
}
}
References
Wikidata query service/User Manual/MWAPI