You are here

function querypath_examples_show_ld in QueryPath 6

Same name and namespace in other branches
  1. 7.3 querypath_examples.module \querypath_examples_show_ld()
  2. 7.2 querypath_examples.module \querypath_examples_show_ld()

View Linked Data for a resource.

1 string reference to 'querypath_examples_show_ld'
querypath_examples_menu in ./querypath_examples.module
Implements hook_menu();

File

./querypath_examples.module, line 411
The main file for querypath_examples.

Code

function querypath_examples_show_ld() {
  $resource = urldecode($_GET['uri']);
  if (empty($resource)) {
    drupal_set_message('Using default resource: The Beatles.', 'status');
    $resource = 'http://dbpedia.org/data/The_Beatles.rdf';
  }
  else {
    if (preg_match('|^http[s]?://|', $resource) == 0) {
      drupal_set_message('Invalid URI', 'error');
      return '';
    }
  }
  $headers = array(
    'Accept: application/rdf,application/rdf+xml;q=0.9,*/*;q=0.8',
    'Accept-Language: en-us,en',
    'Accept-Charset: ISO-8859-1,utf-8',
    'User-Agent: QueryPath/1.2',
  );

  // The context options:
  $options = array(
    'http' => array(
      'method' => 'GET',
      'protocol_version' => 1.1,
      'header' => implode("\r\n", $headers),
    ),
  );

  // Create a stream context that will tell QueryPath how to
  // load the file.
  $cxt = stream_context_create($options);

  // Fetch the URL and select all rdf:Description elements.
  // (Note that | is the CSS 3 equiv of colons for namespacing.)
  // To add the context, we pass it in as an option to QueryPath.
  $qp = qp($resource, 'rdf|Description', array(
    'context' => $cxt,
  ));

  //$qp = qp('/Users/mbutcher/Code/QueryPath/examples/The_Beatles.rdf');

  // Normally this would be refactored into a theme function, but we leave
  // it here so that it is easier to see what is really happening.
  // Use CSS pseudoclasses with namespaced XML.
  $out = "<h1>" . $qp
    ->top()
    ->find('rdfs|label:first')
    ->text() . '</h1>';
  $out .= "<p>About: " . $qp
    ->top()
    ->find('foaf|name:first')
    ->text() . '</p>';

  // Namespaced attributes can be retrieved using the same sort of delimiting.
  $out .= '<p>' . $qp
    ->top()
    ->find('rdfs|comment[xml|lang="en"]')
    ->text() . '</p>';
  $out .= '<h2>Images</h2>';
  $qp
    ->top();
  foreach ($qp
    ->branch()
    ->find('foaf|img') as $img) {

    // Note that when we use attr() we are using the XML name, NOT
    // the CSS 3 name. So it is rdf:resource, not rdf|resource.
    // The same goes for the tag() function -- it will return
    // the full element name (e.g. rdf:Description).
    $out .= '<img src="' . $img
      ->attr('rdf:resource') . '"/>';
  }
  $out .= "<h2>Images Galleries</h2>";
  foreach ($qp
    ->branch()
    ->find('dbpprop|hasPhotoCollection') as $img) {
    $link = $img
      ->attr('rdf:resource');
    $out .= l($link, $link, array(
      'absolute' => TRUE,
    ));
  }
  $out .= "<h2>Other Sites</h2>";
  foreach ($qp
    ->branch()
    ->find('foaf|page') as $img) {
    $link = $img
      ->attr('rdf:resource') . PHP_EOL;
    $out .= l($link, $link, array(
      'absolute' => TRUE,
    ));
  }
  return $out;
}