SPARQL/Wikimedia Commons Query Service

Wikimedia Commons images use structured data too, see Structured Data on Commons (SDoC) . With Wikimedia Commons Query Service (WCQS) the data can be queried. This is available per 1/2/2022 at https://commons-query.wikimedia.org/ It was previously available in beta phase at https://wcqs-beta.wmflabs.org/.

Find below an example of Depictions of Douglas Adams shown as image grid.

#Show images of Douglas Adams in an image grid
#defaultView:ImageGrid
SELECT ?file ?image WHERE {
  ?file wdt:P180 wd:Q42 .
  ?file schema:url ?image.
}

Try it!

Mind: use |project=sdc where sdc =Structured Data on Commons, the default is |project=wd for Wikidata.

See more examples on WCQS itself.

The query below shows all properties of an image:

# all properties of an image 
SELECT DISTINCT ?file ?predicate ?pLabel ?o ?oLabel
WHERE {
  VALUES ?file { sdc:M107651852 }    # an example image
{ ?file ?predicate ?o.
  BIND( IRI(REPLACE( STR(?predicate),"prop/direct/","entity/" )) AS ?p). 
}
UNION
{ ?file ?predicate1 [ ?predicate ?o ].  # qualifiers   
  BIND( IRI((REPLACE( REPLACE( STR(?predicate), "(direct/|statement/|value/|value-normalized/|qualifier/|reference/)", ""),"prop/","entity/"))) AS ?p).  
}
  FILTER( CONTAINS( STR(?predicate), "/prop/direct/") || CONTAINS( STR(?predicate), "/prop/qualifier/") || CONTAINS( STR(?predicate), "/prop/reference/") 
       || CONTAINS( STR(?predicate), "schema.org") || CONTAINS( STR(?predicate), "w3.org") ) 
  SERVICE <https://query.wikidata.org/sparql> {
    SERVICE wikibase:label {
        bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" .
        ?p rdfs:label ?pLabel .
        ?o rdfs:label ?oLabel .
    }
  }
}
LIMIT 100

Try it!

This reveals some hidden statements like: date Modified / width / height / contentSize (bytes). The query below gives an example how to use it.

# show hidden statements
SELECT ?file ?instance_of ?instance_ofLabel ?inception_date ?dateModified ?width ?height ?contentSize
WHERE { 
  VALUES ?file { sdc:M107651852 }    # an example image
  OPTIONAL{ ?file wdt:P31 ?instance_of. }
  OPTIONAL{ ?file wdt:P571 ?inception_date. }
  OPTIONAL{ ?file schema:dateModified ?dateModified. }
  OPTIONAL{ ?file schema:width ?width. }
  OPTIONAL{ ?file schema:height ?height. }
  OPTIONAL{ ?file schema:contentSize ?contentSize. }
  SERVICE <https://query.wikidata.org/sparql> {
    SERVICE wikibase:label {
        bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" . 
           ?instance_of rdfs:label ?instance_ofLabel .
    }
  }
}

Try it!

This example with combination of SERVICE - mwapi shows Depicts statements with Dutch labels, of files in one Commons category

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!

ReferencesEdit