Search code examples
mediawiki-apiwikidatawikidata-api

Get VIAF identifier from a Wikidata Item


I've been playing around with the Wikidata API for a while. I know how to get item (entities) by name and by their Q number. but I can't seem to figure out how to get their properties right. What I'm looking for is P214 (viaf identifiers for authors)

For example, i'm already using this query to get an author by his full name

https://www.wikidata.org/w/api.php?action=wbsearchentities&search=Douglas%20Adams&language=en

returns an array of pages names and linked to pages that have the name "Douglas Adams" on them. but no properties.

then I can use the list of Q id to query for properties, like so

https://www.wikidata.org/w/api.php?action=wbgetentities&ids=Q42&language=en 

the viaf property and it's value for Q42 are there. but this is such an unwieldy approach, because I'm getting a list of results from the first query, then I have to iterate over them and query each for properties for that one I'm looking for.

tl;dr : is there a simpler way to get an item properties list by item name ?


Solution

  • I eventually figured out how to get an entity's property list by name using the wbgetentities action. I totally missed out that I can do a lookup by titles and sites parameters to get an entity rather than just searching it by it's QID using the same action.

    what pointed me towards this solution was this example (hidden at the bottom of the page):

    api.php?action=wbgetentities&sites=enwiki&titles=Berlin&languages=en

    a small down side is that I must also provide the sites parameter, but I can live with that

    EDIT:

    The SPARQL API was just what I needed all along!!!

    PREFIX wikibase: <http://wikiba.se/ontology#>
    PREFIX wd: <http://www.wikidata.org/entity/> 
    PREFIX wdt: <http://www.wikidata.org/prop/direct/>
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    
    SELECT ?viaf WHERE {
      wd:Q42 wdt:P214 ?viaf   
      SERVICE wikibase:label {
        bd:serviceParam wikibase:language "en" .
      }
    }
    

    now i'm getting all P214 (viaf) for Q42 (adams), replacing wd:Q42 with a plain object symbol ?o, will get me all entities with a VIAF ID property and it's value.