t('Transform an RSS feed'), 'description' => t('Grab a remote RSS feed and transform it into HTML.'), 'page callback' => 'querypath_examples_show_rss', 'page arguments' => array(), 'type' => MENU_NORMAL_ITEM, 'access arguments' => array('access content'), ); $items['qp/sticky'] = array( 'title' => t('Show sticky nodes'), 'description' => t('Grab a list of sticky nodes and display it.'), 'page callback' => 'querypath_examples_show_sticky_nodes', 'page arguments' => array(), 'type' => MENU_NORMAL_ITEM, 'access arguments' => array('access content'), ); $items['qp/twitter'] = array( 'title' => t('Show Twitters'), 'description' => t('Show public timeline from Twitter3.'), 'page callback' => 'querypath_examples_show_twitter', 'page arguments' => array(), 'type' => MENU_NORMAL_ITEM, 'access arguments' => array('access content'), ); $items['qp/musicbrainz'] = array( 'title' => t('Show MusicBrainz data'), 'description' => t('Query the MusicBrainz library.'), 'page callback' => 'querypath_examples_show_musicbrainz', 'page arguments' => array(), 'type' => MENU_NORMAL_ITEM, 'access arguments' => array('access content'), ); $items['qp/sparql'] = array( 'title' => t('Run SPARQL query'), 'description' => t('Query a SPARQL endpoint and display the results.'), 'page callback' => 'querypath_examples_show_sparql', 'page arguments' => array(), 'type' => MENU_NORMAL_ITEM, 'access arguments' => array('access content'), ); $items['qp/ld'] = array( 'title' => t('Related References'), 'description' => t('View linked data for a particular path.'), 'page callback' => 'querypath_examples_show_ld', 'page arguments' => array(), 'type' => MENU_NORMAL_ITEM, 'access arguments' => array('access content'), ); $items['qp/multiplerss'] = array( 'title' => t('Show Multiple RSS Feeds'), 'description' => t('Show multiple RSS feeds.'), 'page callback' => 'querypath_examples_show_multiplerss', 'page arguments' => array(), 'type' => MENU_NORMAL_ITEM, 'access arguments' => array('access content'), ); return $items; } /** * Display the titles of each item in the RSS feed. */ function querypath_examples_show_rss() { $out = '
' . t('Display a linked title to each item in the RSS feed') . '
'; $out .= '' . t('Behind the scenes, this is fetching the RSS feed over HTTP, parsing it, and then creating a list of linked items.') . '
'; $url = url('rss.xml', array('absolute' => TRUE)); foreach (qp($url, 'item') as $item) { $title = $item->find('title')->text(); $link = $item->next('link')->text(); $out .= '' . l($title, $link) . '
'; } $code = ' TRUE)); foreach (qp($url, \'item\') as $item) { $title = $item->find(\'title\')->text(); $link = $item->next(\'link\')->text(); $out .= \'\' . l($title, $link) . \'
\'; } ?>'; $out .= '' . t('Display a list of sticky nodes. This combines a database query with some basic HTML generation.') . '
'; $sql = 'SELECT title FROM {node} WHERE sticky = 1'; $ul = qp('' . t('Display the most recent entries on the Twitter public timeline.') . '
'; try { $ul = qp('The best match we found was for ' . $artist->children('name')->text() . '
'; $out .= 'Artist ID: ' . $id . '
'; $out .= 'Albums for this artist' . '
'; $out .= htmlentities($xml); $out .= ''; $_SESSION['musicbrainz_data'] = $out; } else { drupal_set_message('No artists found for ' . $artist_name, 'status'); } } catch (Exception $e) { drupal_set_message('Error: ' . $e->getMessage(), 'status'); } } function querypath_examples_show_sparql() { $out = drupal_get_form('querypath_examples_show_sparql_form'); if ($_SESSION['out']) { $out .= $_SESSION['out']; } return $out; } function querypath_examples_show_sparql_form($edit) { $form['sparql_name'] = array( '#type' => 'textfield', '#title' => t('Search'), '#description' => t('Enter a term. (Case-sensitive)'), '#default_value' => t('The Beatles'), '#size' => 60, '#maxlength' => 256, '#required' => FALSE, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Go'), ); return $form; } function querypath_examples_show_sparql_form_validate($form, &$form_state) { $query = $form_state['values']['sparql_name']; $query = check_plain($query); // Tags can goober things up. $query = addslashes($query); // Escape quotes. try { $out = querypath_examples_show_sparql_lookup($query); $_SESSION['out'] = $out; } catch (QueryPathException $qpe) { form_set_error('sparql_name', 'Could not submit query. This is likely due to a network problem.'); } } function querypath_examples_show_sparql_lookup($term) { // URL to DB Pedia's SPARQL endpoint. $base_url = 'http://dbpedia.org/sparql'; // The SPARQL query to run. $query = ' PREFIX foaf:
' . $sparql . ''; $out .= theme('table', $headers, $rows, array(), $caption); $out .= htmlentities($qp->top()->xml()); return $out; } function querypath_examples_format_ld_link($text, $uri) { // Switch to linked data URI: $uri = preg_replace('|\/resource\/|', '/data/', $uri) . '.rdf'; return l($text, 'qp/ld', array('query' => array('uri' => $uri))); } /** * View Linked Data for a resource. */ 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 = "
About: " . $qp->top()->find('foaf|name:first')->text() . '
'; // Namespaced attributes can be retrieved using the same sort of delimiting. $out .= '' . $qp->top()->find('rdfs|comment[xml|lang="en"]')->text() . '
'; $out .= '' . t('Display a linked title to each item in the RSS feed') . '
'; $out .= '' . t('Behind the scenes, this is fetching the RSS feed over HTTP, parsing it, and then creating a list of linked items.') . '
'; $urls = array("http://newsrss.bbc.co.uk/rss/newsonline_world_edition/front_page/rss.xml", "http://newsrss.bbc.co.uk/rss/newsonline_world_edition/africa/rss.xml", "http://newsrss.bbc.co.uk/rss/newsonline_world_edition/americas/rss.xml"); $feeds = array(); $feeds = _querypath_examples_show_multiplerss($urls, 10); foreach($feeds as $date => $item) { $out .= '' . l($item['title'], $item['link'])
.'
From: ' . $item['url']
.'
' . $item['desc']
.'
Published: ' . date("M d, Y H:m:s", $date) . '
\' . l($item[\'title\'], $item[\'link\'])
.\'
From: \' . $item[\'url\']
.\'
\' . $item[\'desc\']
.\'
Published: \' . date("M d, Y H:m:s", $date) . \'