/[drupal]/contributions/modules/sparql/sparql.pages.inc
ViewVC logotype

Contents of /contributions/modules/sparql/sparql.pages.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Sat Jun 7 20:07:14 2008 UTC (17 months, 2 weeks ago) by arto
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +6 -3 lines
File MIME type: text/x-php
Imported latest 6.x version (r482) from SVN development repository.

Changelog:
- Implemented a proper database schema with a dedicated SQL table, and assorted schema migrations, for SPARQL node data.
- Improved error handling and caching logic for node-based queries.
- Implemented hook_delete() and hook_nodeapi() revision deletion.
- Added more MIME types for SPARQL resultset parsing (they are incorrect, but occasionally encountered in the wild).
- Made specifying the endpoint field mandatory until the Drupal-native engine is ready for prime time.
- Implemented more human-friendly display of query results (requires RDF API 6.x-1.0-alpha2).
- Added note about suppressing the display of previous query results when previewing a changed query.
- Implemented unit tests for querying the CIA Factbook's SPARQL endpoint.
1 <?php
2 // $Id$
3 /**
4 * sparql.pages.inc - SPARQL API menu callbacks.
5 *
6 * @author Arto Bendiken <http://bendiken.net/>
7 * @copyright Copyright (c) 2007-2008 Arto Bendiken. All rights reserved.
8 * @license GPL <http://creativecommons.org/licenses/GPL/2.0/>
9 * @package sparql.module
10 */
11
12 //////////////////////////////////////////////////////////////////////////////
13 // SPARQL query screen
14
15 function sparql_endpoint() {
16 if (!SPARQL_ENDPOINT) {
17 die(drupal_not_found());
18 }
19
20 drupal_add_css(drupal_get_path('module', 'sparql') .'/sparql.css');
21
22 $endpoint = isset($_GET['endpoint']) ? $_GET['endpoint'] : '';
23 $graph = isset($_GET['default-graph-uri']) ? $_GET['default-graph-uri'] : '';
24 $query = isset($_GET['query']) ? $_GET['query'] : '';
25 $debug = isset($_GET['debug']) ? !empty($_GET['debug']) : '';
26
27 $form = array();
28
29 $form['query'] = array('#type' => 'fieldset', '#title' => t('Query'), '#prefix' => '<div class="container-inline">', '#suffix' => '</div>');
30 $form['query']['query'] = array('#type' => 'textarea', '#title' => '', '#default_value' => $query, '#rows' => 8, '#prefix' => '<div class="sparql-query-text">', '#suffix' => '</div>');
31 //$form['query']['debug'] = array('#type' => 'checkbox', '#title' => 'Debug', '#default_value' => $debug, '#prefix' => '<div class="sparql-query-debug">', '#suffix' => '</div>');
32 $form['query']['submit'] = array('#type' => 'submit', '#value' => t('Query'), '#prefix' => '<div class="sparql-query-button">', '#suffix' => '</div>');
33
34 $form['options'] = array('#type' => 'fieldset', '#title' => t('Options'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#prefix' => '<div class="sparql-query-options">', '#suffix' => '</div>');
35 $form['options']['endpoint'] = array('#type' => 'textfield', '#title' => 'Endpoint URL', '#default_value' => $endpoint, '#required' => TRUE, '#description' => t(''));
36 $form['options']['default-graph-uri'] = array('#type' => 'textfield', '#title' => 'Default Graph URI', '#default_value' => $graph, '#description' => t(''));
37
38 return $form;
39 }
40
41 function sparql_endpoint_validate($form, &$form_state) {
42 extract($form_state['values'], EXTR_SKIP | EXTR_REFS);
43 $graph = $form_state['values']['default-graph-uri'];
44 }
45
46 function sparql_endpoint_submit($form, &$form_state) {
47 extract($form_state['values'], EXTR_SKIP | EXTR_REFS);
48 $graph = $form_state['values']['default-graph-uri'];
49
50 $query = array_filter(array('endpoint' => $endpoint, 'default-graph-uri' => $graph, 'query' => $query), 'strlen');
51 $form_state['redirect'] = array('sparql', http_build_query($query, NULL, '&'));
52 }
53
54 //////////////////////////////////////////////////////////////////////////////
55
56 function sparql_render_query_debug($query) {
57 $query = sparql_parse($query);
58 $form['debug'] = array('#value' => highlight_string(var_export($query, TRUE), TRUE));
59 return drupal_render($form['debug']);
60 }
61
62 function sparql_render_query_results($query, $options = NULL) {
63 $output = '';
64
65 if (empty($query)) {
66 if (isset($_GET['query'])) {
67 drupal_set_message(t('No query specified.'), 'error');
68 }
69 }
70 else {
71 timer_start('sparql_query');
72 $result = sparql_query($query, $options, $errors);
73 if (!empty($errors)) {
74 foreach ($errors as $error) {
75 drupal_set_message($error, 'error');
76 }
77 }
78 else {
79 list(, $time) = array_values(timer_stop('sparql_query'));
80
81 $form['time'] = array('#value' => t('Query execution took @time.', array('@time' => format_plural(round($time / 1000.0, 2), '1 second', '@count seconds'))), '#prefix' => '<div class="sparql-query-time">', '#suffix' => '</div>');
82 $output .= drupal_render($form['time']);
83 $output .= theme('sparql_results', $result);
84 }
85 }
86
87 return $output;
88 }
89
90 function theme_sparql_endpoint($form) {
91 $debug = !empty($form['query']['debug']['#value']);
92 $query = $form['query']['query']['#value'];
93 foreach (array('endpoint', 'graph') as $var) {
94 if (isset($form['options'][$var]['#value'])) {
95 $$var = $form['options'][$var]['#value'];
96 }
97 }
98 $options = compact('endpoint', 'graph');
99
100 $output = drupal_render($form['query']);
101 $output .= drupal_render($form['options']);
102 $output .= !$debug ? sparql_render_query_results($query, $options) : sparql_render_query_debug($query, $options);
103 $output .= drupal_render($form);
104 return $output;
105 }
106
107 function theme_sparql_results($result) {
108 switch (gettype($result)) {
109 case 'boolean': // ASK
110 $result = array(array('success' => $result ? t('yes') : t('no')));
111 // fall through to SELECT
112
113 case 'array': // SELECT
114 if (empty($result)) {
115 // TODO
116 $form['results'] = array('#value' => ' ' . t('No results were returned.'));
117 return drupal_render($form['results']);
118 }
119 else {
120 require_once drupal_get_path('module', 'rdf') . '/rdf.theme.inc';
121 $vars = array_keys($result[0]);
122 $rows = array_map('theme_sparql_results_row', $result);
123 return theme('table', $vars, $rows, array('class' => 'sparql-results'));
124 }
125
126 case 'object': // CONSTRUCT/DESCRIBE
127 return theme('rdf_triple_table', $result);
128 }
129 }
130
131 function theme_sparql_results_row($row) {
132 return array_map('theme_rdf_value', array_values($row));
133 }

  ViewVC Help
Powered by ViewVC 1.1.2