Get all Wikidata items that are an instance of a given item
Asked Answered
R

3

12

Wikidata has an item called smartphone model.
I want to get all instances of it.

QUESTION: How to get the identifiers of the instances programmatically, using the live server?

Preferably not including false positives that show up in WhatLinksHere but are in the "Wikidata:" namespace rather than the main namespace.

Rarebit answered 27/4, 2015 at 2:48 Comment(0)
P
20

Your question specifies the "Mediawiki API" but this is not possible.

Wikidata has a SPARQL query service at https://query.wikidata.org

The query that you want is:

PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

SELECT DISTINCT ?item
WHERE {
    ?item wdt:P31/wdt:P279* wd:Q19723451
}

This will list the Items that are an 'instance of'(P31) 'smartphone model'(Q19723451) or an 'instance of' a 'subclass of'(P279) 'smartphone model'(Q19723451).

Process answered 31/1, 2016 at 19:9 Comment(3)
For easier reading, can you clarify what P31, Q19723451, P279, and Q19723451 stand for ?Foppish
Direct link to query: query.wikidata.org/…Grower
Hugo LOPEZ - All Wikidata wiki pages that correspond to properties start with "P". All Wikidata wikipages that correspond to "Q" are items. Most pages on Wikidata are items.Jegger
E
9

It is not possible yet. (The task for adding it was declined due to the new SPARQL interface solving such queries.)

The best you could to today in Mediawiki API is to use action=query and props=linkshere and filter on namespace like this:

https://www.wikidata.org/w/api.php?action=query&prop=linkshere&format=json&lhprop=title&lhnamespace=0&lhlimit=500&titles=Q19723451

However, there is an external query tool, Wikidata query, that would do exactly what you are asking for with this query:

https://query.wikidata.org/#SELECT%20%3Fitem%20WHERE%20%7B%0A%3Fitem%20wdt%3AP31%20wd%3AQ19723451%20.%0A%20%20%7D

After running it, there is a code tab where you can get eamples on how to use the results in various platforms.

EDIT: updated status of task and changed to new query tool.

Ease answered 27/4, 2015 at 5:30 Comment(3)
You may want to change the links to "https" links rather than "http" links. Also, the task that you linked to (T54385) was declined by the developers in 2018.Jegger
Why isn't using haswbstatement a valid solution? For example: https://www.wikidata.org/w/api.php?action=query&list=search&srsearch=haswbstatement:P31=Q744691Amytal
@S.D It is now, but not when the answer was originally written. Feel free to submit as a new answer.Ease
J
0

A lot of the answers here are going to involve using Wikimedia's SparQL service. Mine won't. The answer is going to be: "it depends on the topic". If you're an English-language speaker, then the best thing to do is find the category for associated with the topic you want to query.

Let's take the "smartphone model" example. That (as of this writing in February 2021) is associated with Q19723451. It appears as though it has something under "Topic's main category", which is "Q20195396". That appears to be associated with "Kategorie:Smartphone (Produkt)" on German Wikipedia, so we seem to be on the right track. Weirdly, as of this writing, that seems to be marked as being "Different from (P1889)" the Wikimedia category "Category:Smartphones (Q6483807)". No matter; let's see what the English Wikipedia link gives us.

Looks like a wonderfully curated list of smartphone models: https://en.wikipedia.org/wiki/Category:Smartphones. Each page in Category:Smartphones usually has a Wikidata item associated with it (e.g. the "Fairphone 1" is associated with Q21041244). You'll get a few extra results (e.g. Fairphone is a company, not a model of smartphone), but it's usually better than trying to do a generalized query on Wikidata.

Some categories of items are mature on Wikidata, and some aren't. That's the reason I said "it depends on the topic". For many things, as of February 2021, it may still be easier to use the MediaWiki API to scrape categories on English Wikipedia rather than using Wikidata. That may change in 2022, 2023, or beyond (or even by the end of the year).

Note that Wikimedia Foundation provides regular data dumps in JSON and RDF. Rather than trying real-time queries, some use cases may be better served by downloading a data dump, and performing text processing on the text file. The text files are very large, so the use cases may be limited, but still, it's worth considering.

Hopefully, this answer is obsolete soon. Happy Valentines Day! When clicking around on Wikidata, be mindful not to accidentally stumble into the events in Chicago on Valentine's Day, 1929 (<<< Trigger warning: that link is to details about a violent crime.)

Jegger answered 14/2, 2021 at 9:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.