2

Given a specific category (i.e. https://commons.wikimedia.org/wiki/Category:Motorcycles) I want to get names of all sub-categories recursively, either in SPARQL:

SELECT ?category ?entityLabel WHERE {
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
  #get sub categories of category wd:Q7025402
}
LIMIT 10000

or using MediaWiki API:

https://commons.wikimedia.org/w/api.php?{get all subcategories of Category:Motorcycles}

Is there a way to do this?

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
Samantha
  • 23
  • 2
  • 2
    Yes, there are ways to do this. What have you tried? And where do you plan to execute this query (because endpoints matter to useful answers)? (Note that there is no such thing as "SparQL". It's "[SPARQL](http://dbpedia.org/page/SPARQL)" — the "SPARQL Protocol and RDF Query Language".) – TallTed Jul 20 '18 at 15:06
  • 1
    the category graph is maintained under namespace `categories`, thus, you have to use `https://query.wikidata.org/bigdata/namespace/categories/sparql` as SPARQL endpoint. – UninformedUser Jul 20 '18 at 19:40
  • 1
    complex query: `PREFIX gas: prefix mediawiki: SELECT * WHERE {SERVICE gas:service { gas:program gas:gasClass "com.bigdata.rdf.graph.analytics.BFS" . gas:program gas:linkType mediawiki:isInCategory . gas:program gas:traversalDirection "Reverse" . gas:program gas:in . # one or more times, specifies the initial frontier. gas:program gas:out ?out . gas:program gas:out1 ?depth . gas:program gas:maxIterations 8 . } } ORDER BY ASC(?depth)` – UninformedUser Jul 20 '18 at 19:42
  • 1
    simpler query: `SELECT ?out ?depth WHERE { SERVICE mediawiki:categoryTree { bd:serviceParam mediawiki:start . bd:serviceParam mediawiki:direction "Reverse" . bd:serviceParam mediawiki:depth 5 . } } ORDER BY ASC(?depth)` – UninformedUser Jul 20 '18 at 19:42

2 Answers2

1

API:

https://commons.wikimedia.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Motorcycles&cmtype=subcat&utf8=1&format=json

SQL:

https://quarry.wmflabs.org/query/28793

(via Quarry tool or directly if you have an account on Toolforge)

But recursive only via PetScan or manually via API/SQL through chain of queries (query for each category, where subcats is not 0):

https://commons.wikimedia.org/w/api.php?action=query&generator=categorymembers&gcmtitle=Category:Motorcycles&gcmtype=subcat&prop=categoryinfo&utf8=1&format=json

https://quarry.wmflabs.org/query/28794
Siarhei
  • 214
  • 1
  • 4
0

SPARQL

As of July 2018, the structure of Wikimedia Commons categories is not covered by Wikidata:

Exception is Commons, which has by far the largest set of categories and thus we decided not to cover it for now, until we ensure everything works as planned with smaller data sets.

MediaWiki API

Not possible, see T37402.

Alternatives

Use the PetScan tool:

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
  • 1
    The PetScan tool worked perfectly.. Thanks alot :) Besides, I used this tool also to retrieve the titles of media related to those categories. Thank you also for correcting the question and for the given explanations. – Samantha Jul 26 '18 at 21:12