Search code examples
sparqlwikidata

In a wikidata SPARQL query how to address a file?


How to address a commons Media File?

In wikidata want to find all objects, which have a commons media file attached through a specific property with a SPARQL query.

Example:

SELECT ?item
{
    # get all objects which have this file as a LocatorMap
    ?item wdt:P242 commons:LocationPeru.svg
}

This does not work and neither do all variants like wd:commons:LocationPeru.svg, p:P242 [ps:P242 psv:commons:LocationCuba.svg] or everything else I tried. I also tried a FILTER with the label but rdfs:label ?label did not work for me on the fileobject.

So, how is the correct syntax?

Thanks.

Actually, I want to filter out specific ones from a more complex query with MINUS, but reduced my problem to this.


Solution

  • You can use the image's full URL:

    SELECT ?item
    {
      ?item wdt:P242 <http://commons.wikimedia.org/wiki/Special:FilePath/LocationPeru.svg>
    }
    

    Actually, from the above query it seems that no item indicates LocationPeru.svg as locator map. This is because in Q419#P242 there is an higher-ranked locator map.

    For ignoring the statements' ranking, you can use:

    SELECT ?item
    {
      ?item p:P242 [ ps:P242 <http://commons.wikimedia.org/wiki/Special:FilePath/LocationPeru.svg> ]
    }
    

    For filtering only the normal rank, you can use:

    SELECT ?item
    {
      ?item p:P242 [
          ps:P242 <http://commons.wikimedia.org/wiki/Special:FilePath/LocationPeru.svg> ;
          wikibase:rank wikibase:NormalRank
        ]
    }