Skip to main content

Using SPARQL and Linked Open Data For Content Blocks on Your Drupal 7 Website

Lehel Mátyus
Director, Front-End Engineering

The content that you display on your Drupal site doesn’t necessarily have to be content that you own or store in your own database. In addition to your own content, there could be various use cases and methods for dynamically displaying data from outside sources and displaying it on your site.

Why would you want to do this? Perhaps another website contains unique data that’s unavailable anywhere else. The content is highly relevant to what you publish. Although you don’t own or control the resource, displaying this data as supplementary content from this trusted source on your site would enrich your visitor’s experience on your site.

Based on our recent work with several museums, we have been working with Linked Open Data and a query tool called SPARQL. In this example, we’ll use public data from Wikipedia on our Drupal site to give a richer experience to our visitors.

Example Project

On our example site, we have a content type called ‘country’ set up that already populates pages with data about European countries. However, we want to display the population of each country in the sidebar but we don’t want to store this data on our server, since it will become out of date over time.

To help us get started, below is a quick terminology rundown of technologies we’ll be using.

Quick Terminology Recap

  • DB Pedia - A crowd-sourced community effort to extract structured information from Wikipedia
  • RDF - Arconym for Resource Description Framework, a way of organizing information in triplets
  • RDF triplet - A data model of graph of subjectpredicateobject triples
  • SPARQL - Specific query language to query RDF Triplets

The Plan

We are going to use JSONP SPARQL to query an endpoint, in this case DB Pedia, to gather data about an entity we already have in our database. We are going to create a block and add the necessary SARQL code to get the data from DB Pedia and display it on our site.

What You Need

  1. Drupal instalation ( this was tested on v7.37)
  2. Drupal Modules and dependencies:
    • JSONP SPARQL (jsonp_sparql)
      • Libraries API (libraries)
  3. Libraries to add to your libraries folder
    • jquery-jsonp v2.2.4 (https://github.com/jaubourg/jquery-jsonp/tree/v2.4.0)
    • Be sure to place it under sites\all\libraries\jquery.jsonp

Steps

These are the steps to create the block that will pull in and display the Linked Open Data.

1. Enable and configure JSONP SPARQL

Navigate to your websites /admin/config/search/jsonp_sparql and set up as many blocks as you need. In our example we only need one block.

2. Create an extra field for the content type

We already have country content type set up, but the title may not be clean enough to be used as a variable in our SPARQL query. We set up a new field in the content type called field_country_name_db we use this to store the name on the country in a clean format.

3. Build a SPARQL Query

We navigate to the DB Pedia SPARQL Endpoint and try out our Query with a specific details.
After some testing we come up with the following query:

PREFIX type: <http://dbpedia.org/class/yago/>
PREFIX prop: <http://dbpedia.org/property/>
SELECT ?country_name ?population
WHERE {
    ?country 
             rdfs:label ?country_name ;
             prop:populationEstimate ?population .
    FILTER (regex(?country_name, "Hungary") and
            langMatches(lang(?country_name), "EN")) .
}

 

We query for country_name as defined in http://dbpedia.org/class/yago/ ontology
and populationEstimate as defined in http://dbpedia.org/property/ ontology.

We filter the results by country_name using regular expression for a specific country. We use the langMatches function to filter for the country names only in English. The country name will become a variable provided from a drupal field.

4. Set up your block

Navigate to your websites block /admin/structure/blocks and find the JSONP SPARQL block that has been created by the module.

Fill out the 'field to use for input' field with the field name (note you could also use Tokens if you have a more complicated Query)

Add the query you built in the SPARQL Query to use field and place the variable using the [1] notation. In our case its the name of the country.

 

Set up SPARQL

 

Add the Endpoint with a ? question mark at the end of the URL. Set up your presentation putting the SPARQL Query variables between the pipe characters. Enter custom text for Intro Text, empty text and Error Text, just in case something goes wrong on the SPARQL Endpoint side.

 

Set up SPARQL

 

Place the block in the necessary region of your theme and Under the Visibility settings set up a rule so it only gets shown for the content types you need it, in our case it is country.

 

See the end result:

Set up SPARQL. End result.

 

I hope you found this example usefull, I would love to hear your opinion and use cases for using SPARQL blocks on your websites. Please leave a comment below.