2

So i have this page id 12517871: https://fr.wikipedia.org/wiki?curid=12517871

I would like to get the identifiers from the bottom here https://www.wikidata.org/wiki/Q64007200

Using sparql, but i don't know how to do so.

I know i must use something like

    SELECT ?sitelink ?itemLabel ?sitelinkLabel ?article ?cid WHERE {
  ?sitelink schema:isPartOf <https://fr.wikipedia.org/>.
}

but then i don't know how to search by page id and how to get the identifiers (imdb, allocine ..)

thanks

edit: i'm using this query https://w.wiki/GD5 but it randomly return nothing. To test this randomness, change the number on "limit"

SELECT ?propertyclaim ?value ?item WHERE {
  hint:Query hint:optimizer "None" .
  SERVICE wikibase:mwapi {
    bd:serviceParam wikibase:endpoint "fr.wikipedia.org" .
    bd:serviceParam wikibase:api "Generator" .
    bd:serviceParam mwapi:generator "revisions" .
    bd:serviceParam mwapi:pageids "12148688" .
    ?item_ wikibase:apiOutputItem mwapi:item .
    bd:serviceParam wikibase:limit 3
  }
  BIND (COALESCE(?item_, " ") AS ?item) 
  ?item ?propertyclaim ?value .  
  ?property wikibase:directClaim ?propertyclaim . 
  ?property wikibase:propertyType wikibase:ExternalId .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "fr,en". }} 
logi-kal
  • 7,107
  • 6
  • 31
  • 43
user3416510
  • 83
  • 1
  • 8

2 Answers2

5

As you have a pageid and not an article title this makes the query a bit cumbersome.

You need to use the Mediawiki API Service to call out to Mediawiki API from SPARQL for getting the Wikidata item id of a Wikipedia page id.

Once you have the item id, you can query for all external identifiers on that item. Together, the query looks like this:

SELECT ?propertyLabel ?value WHERE {
  SERVICE wikibase:mwapi {
    bd:serviceParam wikibase:endpoint "fr.wikipedia.org" .
    bd:serviceParam wikibase:api "Generator" .
    bd:serviceParam mwapi:generator "revisions" .
    bd:serviceParam mwapi:pageids "12517871" .
    ?item wikibase:apiOutputItem mwapi:item .
  }
  ?property wikibase:propertyType wikibase:ExternalId .
  ?property wikibase:directClaim ?propertyclaim .
  ?item ?propertyclaim ?value   
  SERVICE wikibase:label { bd:serviceParam wikibase:language "fr,en". }        
}
Pascalco
  • 2,481
  • 2
  • 15
  • 31
  • thank you, i tried it and it gave a timeout there https://query.wikidata.org/ is there any away to improve or fix this ? – user3416510 Jan 25 '20 at 11:34
  • 2
    use query hints: `SELECT ?propertyLabel ?value WHERE {{SELECT ?item { SERVICE wikibase:mwapi { bd:serviceParam wikibase:endpoint "fr.wikipedia.org" . bd:serviceParam wikibase:api "Generator" . bd:serviceParam mwapi:generator "revisions" . bd:serviceParam mwapi:pageids "12517871" . ?item wikibase:apiOutputItem mwapi:item . }} limit 10 } hint:Prior hint:runFirst "true". ?property wikibase:propertyType wikibase:ExternalId . ?property wikibase:directClaim ?propertyclaim . ?item ?propertyclaim ?value SERVICE wikibase:label { bd:serviceParam wikibase:language "fr,en". } }` – UninformedUser Jan 25 '20 at 12:07
  • Interestingly, just removing the labels seems to solve the timeout problem as well, without needing to use the hint optimiser. https://w.wiki/G7t gets the ID (nothing else) in 1.4s, https://w.wiki/G7v gets the property claims in 1.6s, https://w.wiki/G7w adds labels and times out. So depending on what you're needing it for, this might work... – Andrew is gone Jan 25 '20 at 12:12
  • i just had the opposite, the query from AKSW was fine, then i removed the propertyLabel to propertyClaim (i don't need the text, only the "claim"), it was timeouting, i retried and it returned in 16ms. It randomly timeouting ?! – user3416510 Jan 25 '20 at 12:16
  • do you know how to also return the wikidata id on the same query ? – user3416510 Jan 25 '20 at 12:19
  • nevermind i found out : SELECT ?propertyclaim ?value ?item WHERE { {SELECT ?item { SERVICE wikibase:mwapi { bd:serviceParam wikibase:endpoint "fr.wikipedia.org" . bd:serviceParam wikibase:api "Generator" . bd:serviceParam mwapi:generator "revisions" . bd:serviceParam mwapi:pageids "5443325" . ?item wikibase:apiOutputItem mwapi:item . }} limit 1 } hint:Prior hint:runFirst "true". ?property wikibase:propertyType wikibase:ExternalId . ?property wikibase:directClaim ?propertyclaim . ?item ?propertyclaim ?value } – user3416510 Jan 25 '20 at 12:44
  • Perhaps `bd:serviceParam wikibase:limit 1` would be better than nested SELECT. Anyway, please edit your answer, if your query times out. – Stanislav Kralin Jan 25 '20 at 13:03
  • i have a problem now, on fr.wikipedia.org and using the id 12685148, the api is always timing out/crashing https://w.wiki/G8K – user3416510 Jan 25 '20 at 14:18
  • 1
    actually it crashes when a wikidata pages does not exist – user3416510 Jan 25 '20 at 14:27
  • 1
    @user3416510, https://w.wiki/G8Z . "Request timeout" actually means "MWAPI query failed". It seems that API is not stable on fr,wikipedia.org. – Stanislav Kralin Jan 25 '20 at 15:58
  • @StanislavKralin i have another problem this time with id 12148688. It returns nothing (https://w.wiki/G9F) but with a slightly different query it return data (https://w.wiki/G9G). WAOW now it's the opposite. So it's randomly not displaying the results. I think something is wrong with the query – user3416510 Jan 25 '20 at 20:02
  • @user3416510, I have added `SILENT` , because Mediawiki API at fr.wikipedia.org is not stable. – Stanislav Kralin Jan 25 '20 at 20:26
  • @StanislavKralin even if i remove the "silent", it seems to randomly return nothing. I changed limit 1 to limit 10, then the limit 1 return data, limit 10 return nothing. With limit 10 it should return the same thing than with limit 1 right ? You said it is not stable, i prefer to see an error than "empty date" while it is supposed to have data – user3416510 Jan 25 '20 at 20:36
2

If you're not restricted to just using SPARQL, then a two-step approach is to use the API directly to get the Wikidata ID:

https://fr.wikipedia.org/w/api.php?action=query&prop=pageprops&pageids=12517871

 "wikibase_item": "Q64007200"

and then feed that directly into SPARQL:

SELECT ?propertyLabel ?value WHERE {
  ?property wikibase:propertyType wikibase:ExternalId .
  ?property wikibase:directClaim ?propertyclaim .    
  wd:Q64007200 ?propertyclaim ?value   
  SERVICE wikibase:label { bd:serviceParam wikibase:language "fr,en". }        
}

This is much more efficient and doesn't time out, but is a bit more cumbersome to code.

Andrew is gone
  • 286
  • 1
  • 5
  • thanks it works, however i prefer to use a single request if it's possible. Do you have an idea why it's timeout, sparql is not optimised for this query or the query is not written in a way that it can use indexes ? – user3416510 Jan 25 '20 at 12:06
  • @user3416510 it is, but sometimes the query optimizer needs hints. see my comment below the other answer – UninformedUser Jan 25 '20 at 12:08
  • @user3416510 I am not very experienced with the API/query linkups, so not sure, sorry! – Andrew is gone Jan 25 '20 at 12:11