Wikidata api how to get some attributes based on wikipedia page id

695 Views Asked by At

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". }} 
2

There are 2 best solutions below

13
On

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". }        
}
3
On

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.