18

I can use the following query to list all sovereign states via the Wikidata query interface:

PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

SELECT ?cid ?country WHERE {
    ?cid wdt:P31 wd:Q3624078 .
    OPTIONAL {
        ?cid rdfs:label ?country filter (lang(?country) = "en") .
    }
}

The result listing contains the Wikidata entity ID and the English label, if available. I'd like to also query the URL of the corresponding English Wikipedia article if it exists. As far as I can tell, Wikipedia URLs are not properties of a Wikidata entity and I have no idea how I can express a relation between a Wikidata entity and Wikipedia URL in the SPARQL query.

16

Try this one:

prefix schema: <http://schema.org/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

SELECT ?cid ?country ?article WHERE {
    ?cid wdt:P31 wd:Q3624078 .
    OPTIONAL {
        ?cid rdfs:label ?country filter (lang(?country) = "en") .
    }
    OPTIONAL {
      ?article schema:about ?cid .
      ?article schema:inLanguage "en" .
      FILTER (SUBSTR(str(?article), 1, 25) = "https://en.wikipedia.org/")
    }
} 

More details about sitelinks are in the RDF docs. Note that right now the only way to distinguish between Wikipedia links and other English links (like sources, news, etc.) is by URL match, thus a somewhat ugly filter.

  • Awesome, thanks a lot! Just saw and tried your suggestion. – ramiro Oct 8 '15 at 23:20
  • How to use it directly by clain at https://wdq.wmflabs.org/api?q=claim[P:Q]? Example: a query for Q155 (Brazil) wdq.wmflabs.org/api?q=claim[31:155]`, but I need all sitelinks, and 31 is not a correct property. – Peter Krauss Jan 22 '16 at 1:28
  • @PeterKrauss I don't think WDQ (which is different from SPARQL engine) returns sitelinks. You can match for presence of sitelinks with e.g. link[dewiki] but I didn't find a way to return links. – StasM Jan 22 '16 at 19:23
20

On 18. April 2016 the ticket "More efficient SPARQL queries for sitelinks" brought us a better way: schema:isPartOf.

?sitelink schema:about ?item .
?sitelink schema:inLanguage "en" .     
?sitelink schema:isPartOf <https://en.wikipedia.org/> .

Your full query now looks like this:

PREFIX schema: <http://schema.org/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

SELECT ?cid ?country ?article WHERE {
    ?cid wdt:P31 wd:Q3624078 .
    OPTIONAL {
      ?cid rdfs:label ?country filter (lang(?country) = "en") .
    }
    OPTIONAL {
      ?article schema:about ?cid .
      ?article schema:inLanguage "en" .
      ?article schema:isPartOf <https://en.wikipedia.org/> .
    }
} 
  • That's great news and thanks for your answer! – ramiro May 2 '16 at 19:20
  • Is there a way to filter articles that are not in a given language? Etc., something like article schema:isNotPartOf ? – zygimantus Mar 29 '18 at 12:34
  • yep, sparql provides the filter syntax for that. add FILTER (!BOUND(?article)) below the last optional block above. – robbi5 Mar 29 '18 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.