Last night I noticed Darius Kazemi post about a little web page that displays a random sandwich from a list of Sandwiches on Wikipedia. It tickled me, probably because the page is so simple (view the source), and also because the results were so delightfully varied, within such a narrow domain. Also, I guess I was hungry…

It just so happens that for some things at work I’ve been thinking about Wikidata a bit more recently. For the past year or so I’ve been lurking on the mailing list and in various get togethers put on by the LD4-Wikidata Affinity Group–who are doing some great work connecting Linked Data to the actual work of libraries, museums and archives. I also attended a really inspiring event from Rhizome this week about the relaunch of their Artbase which uses Wikibase, which is the open source software that powers Wikidata. They are usually really good about putting their online events up on their Vimeo channel so keep an eye out for it there if you are interested.

So, naturally, it seemed like a fun little Friday evening project to adapt Darius’ JavaScript to pull the sandwiches from Wikidata using their query service. You can see the result here.

https://edsu.github.io/sandwich/

My initial motivation was to see if the query service’s CORS headers were set up properly to allow HTTP traffic from the browser (they are) and to test my ability to craft a good enough SPARQL query. But it also turned out to be an interesting exercise in how to send people over to Wikidata to improve things, since the descriptions of sandwiches in Wikidata are not as good as the ones found in Wikipedia proper, and you can link directly to a page to edit them in Wikidata.

Crafting the SPARQL query was quite a bit easier than I thought it would be because they have a useful Examples tab, where I typed sandwiches and up came the start of a query I could work with:

There are currently 345 of these example queries and you can even add your own by editing this wiki page. After a few minutes of noodling around I arrived at this query:

SELECT ?item ?itemLabel ?itemDescription ?countryLabel ?image
WHERE
{
  ?item wdt:P279 wd:Q28803.
  ?item wdt:P495 ?country.
  ?item wdt:P18 ?image.
  SERVICE wikibase:label {bd:serviceParam wikibase:language "en".}
}
LIMIT 1000

This wasn’t my first dance with SPARQL, so I won’t pretend this was easy. But it’s really nice to be able to be able to tweak the query examples and see what happens. It seems obvious that a query by example query language would have, well, examples. But Wikidata have done this in a very thoughtful way. It is really nice to be able to query information like this live from the client, so that users of your app can see the latest data that’s available, not some static snapshot of it.

But of course that comes with risks too, if the service is offline, if the metadata structure changes, if the entity is defaced or deleted. None of these things really matter for this toy page, but could for something more solid. I’ve got a bit more to say about this, but I’m going to save it for another post.