Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all Property value's Labels of an Wikidata Item?

How would you request the set of all Properties that an Item has?

Specifically, I'm looking for the English Labels for each Item that is the Value of an Item's Property.

E.g. "Earth":

{
    "Challenger Deep",
    "Solar System",
    "oblate spheroid",
    "geoid",
    "World Ocean",
    "Afro-Eurasia",
    ...
}

I'm using the Wikidata Toolkit library for now, but I'd take recommendations.

like image 557
Ware Avatar asked Jan 15 '16 16:01

Ware


1 Answers

First you need to use MediaWiki API with action wbgetclaims to get all properties for each one claim. For example for Wikidata item Earth (Q2) the request will be:

https://www.wikidata.org/w/api.php?action=wbgetclaims&format=xml&props=value&entity=Q2

Then you need to parse the response and to take id from the value for each one property. In this example, for property P1589 the item id is Q459173.

The last step is to get all item labels in English by another request. You have two variants, in both of them we use the all item ids from the previous step, separated by pipe |:

  • First variant: by using action wbgetentities:

    https://www.wikidata.org/w/api.php?action=wbgetentities&props=labels&languages=en&ids=Q459173|Q544|Q3241540|Q185969|Q715269|Q27527

  • Second variant: by using action query:

    https://www.wikidata.org/w/api.php?action=query&prop=pageterms&wbptterms=label&titles=Q459173|Q544|Q3241540|Q185969|Q715269|Q27527

Update: I found how to get all item property value's labels by using only one request. The idea is to find all item links, and then to filter from them only these from namespace 0 which start with Q.

https://www.wikidata.org/w/api.php?action=query&titles=Q2&generator=links&gplnamespace=0&gpllimit=100&prop=pageterms&wbptterms=label
like image 199
Termininja Avatar answered Sep 18 '22 16:09

Termininja