| 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 |
}
|