Search code examples
sparqlmediawikiwikipediawikipedia-apiwikidata

Wikidata Query Service/Categories: number of pages/subcategories and HiddenCategory attributes


using gas:service or mediawiki:categoryTree services of Wikidata API is it possible somehow to include mediawiki:pages, mediawiki:subcategories and mediawiki:HiddenCategory attributes in query results? I see these attributes in dumps, but have no luck trying to access them programmatically (with SPARQL or some other API)...


Solution

  • You just need to add your conditions e.g for pages add:

      ?out  mediawiki:pages ?pages .
    

    Result

       {
          "out" : {
            "type" : "uri",
            "value" : "https://en.wikipedia.org/wiki/Category:Fictional_ducks"
          },
          "depth" : {
            "datatype" : "http://www.w3.org/2001/XMLSchema#int",
            "type" : "literal",
            "value" : "1"
          },
          "pages" : {
            "datatype" : "http://www.w3.org/2001/XMLSchema#integer",
            "type" : "literal",
            "value" : "113"
          }
    

    They warn that you can't access this through the UI, so you need to encode your query and pass it in the URL:https://query.wikidata.org/bigdata/namespace/categories/sparql?query=&format=json

    Full query:

    PREFIX gas: <http://www.bigdata.com/rdf/gas#>
    prefix mediawiki: <https://www.mediawiki.org/ontology#> 
    
    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 <https://en.wikipedia.org/wiki/Category:Ducks>. # one or more times, specifies the initial frontier.
         gas:program gas:out ?out . # exactly once - will be bound to the visited vertices.
         gas:program gas:out1 ?depth . # exactly once - will be bound to the depth of the visited vertices.
         gas:program gas:maxIterations 8 . # optional limit on breadth first expansion.
      }
      ?out  mediawiki:pages ?pages .
    } ORDER BY ASC(?depth)