MQL Monday is a new feature of the Freebase blog; every Monday we’ll post a MQL query drawn from documentation like the Schema Introspection Cookbook or the MQL Cookbook, or based on a question or submission from you all. In fact, we heartily encourage you to submit questions or solutions for MQL queries that have come from your own experience.
This week the query focuses on getting the Wikipedia article ID for a given topic, and is drawn from the help topic Attributing Topic Descriptions to Wikipedia. According to the Freebase terms of service, if you reproduce a Freebase topic description that originally came from Wikipedia in your own application, you are obligated to provide a link back to the original Wikipedia article. Fortunately, there’s an easy way to identify the source of a Freebase topic description by examining its /common/document/source_uri property for the topic description. If this property contains a wp, as in http://wp/en/1194195, then that description originated from Wikipedia. However, you should note that this value is a “pseudo-URI;” it won’t link you back to the original Wikipedia article, it’s simply a way of assigning a resource indicator to the article description.
With this in mind, the trick is to create a basic MQL query that will return the value for this property. Here’s a query that can be used to examine this property for a single topic:
{
"id" : "/en/hortense_ellis",
"/common/topic/article" : [{
"id":null,
"/common/document/source_uri" : []
}]
}
Note that this query is configured to return multiple results in case there’s both a Wikipedia and user-contributed topic description.
In most cases, however, you’ll want to run this query against a batch of results, and then, programmatically create links back to the Wikipedia articles where appropriate. Here’s how you could incorporate the same query into a larger one that looks for 25 albums with tracks containing the word “love,” and returns the artist name, track name, album name, and the GUID of the topic description, along with the /common/document/source_uri property value:
[
{
"album" : [{
"artist" : [ ],
"name" : null,
"/common/topic/article" : [{
"id":null,
"/common/document/source_uri" : [ ]
}]
}],
"limit" : 25,
"name" : null,
"name~=" : "Love*",
"type" : "/music/track"
}
]
Having retrieved the information you need, all you need to do is re-insert the number at the end of the /common/document/source_uri property, which identifies the article in Wikipedia, into a working Wikipedia URL, like this: http://en.wikipedia.org/w/index.php?curid=1194195. Again, this is something you can do programmically once you’ve gotten the information you need.
