/[drupal]/contributions/modules/sparql/sparql.module
ViewVC logotype

Contents of /contributions/modules/sparql/sparql.module

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


Revision 1.4 - (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.3: +48 -13 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.module - Enables the use of SPARQL queries with the RDF API.
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 // Core API hooks
14
15 /**
16 * Implementation of hook_init().
17 */
18 function sparql_init() {
19 require_once drupal_get_path('module', 'sparql') . '/sparql.inc';
20
21 // Add a SPARQL autodiscovery link to the front page's <head> tag
22 if (SPARQL_ENDPOINT && drupal_is_front_page()) {
23 drupal_add_link(array('rel' => 'sparql', 'href' => url('sparql', array('absolute' => TRUE))));
24 }
25 }
26
27 /**
28 * Implementation of hook_help().
29 */
30 function sparql_help($path, $arg = NULL) {
31 switch ($path) {
32 case 'admin/settings/sparql':
33 return '<p>' . t('') . '</p>'; // TODO
34 }
35 }
36
37 /**
38 * Implementation of hook_perm().
39 */
40 function sparql_perm() {
41 return array(
42 'access SPARQL endpoint',
43 );
44 }
45
46 /**
47 * Implementation of hook_menu().
48 */
49 function sparql_menu() {
50 return array(
51 // SPARQL endpoint
52 'sparql' => array(
53 'title' => 'SPARQL query',
54 'description' => '',
55 'access arguments' => array('access SPARQL endpoint'),
56 'page callback' => 'drupal_get_form',
57 'page arguments' => array('sparql_endpoint'),
58 'file' => 'sparql.pages.inc',
59 ),
60 'sparql/query' => array(
61 'title' => 'Query',
62 'type' => MENU_DEFAULT_LOCAL_TASK,
63 ),
64 // Administer >> Site configuration >> SPARQL settings
65 'admin/settings/sparql' => array(
66 'title' => 'SPARQL settings',
67 'description' => 'Settings for the SPARQL API.',
68 'access arguments' => array('administer site configuration'),
69 'page callback' => 'drupal_get_form',
70 'page arguments' => array('sparql_admin_settings'),
71 'file' => 'sparql.admin.inc',
72 ),
73 );
74 }
75
76 /**
77 * Implementation of hook_theme()
78 */
79 function sparql_theme() {
80 return array(
81 'sparql_endpoint' => array(
82 'arguments' => array('form' => NULL),
83 'file' => 'sparql.pages.inc',
84 ),
85 'sparql_results' => array(
86 'arguments' => array('result' => NULL),
87 'file' => 'sparql.pages.inc',
88 ),
89 );
90 }
91
92 //////////////////////////////////////////////////////////////////////////////
93 // Node API hooks
94
95 /**
96 * Implementation of hook_node_info().
97 */
98 function sparql_node_info() {
99 return array(
100 'sparql' => array(
101 'module' => 'sparql_node',
102 'name' => t('SPARQL query'),
103 'description' => t('...'),
104 'title_label' => t('Title'),
105 'has_title' => TRUE,
106 'body_label' => t('Description'),
107 'has_body' => FALSE,
108 ),
109 );
110 }
111
112 /**
113 * Implementation of hook_form().
114 */
115 function sparql_node_form(&$node, $form_state) {
116 // TODO: This node_form() craziness here is needed because when
117 // drupal_retrieve_form('file_node_form') is performed, it will
118 // incorrectly ignore callback information defined in hook_forms() and
119 // call this function directly. We need to either rename our Node API
120 // prefix in hook_node_info(), or else submit a core patch.
121 if (is_array($node)) {
122 return node_form($node, $form_state);
123 }
124
125 $form = array();
126 $type = node_get_types('type', $node);
127
128 if ($type->has_title) {
129 $form['title'] = array('#type' => 'textfield', '#title' => check_plain($type->title_label), '#required' => TRUE, '#default_value' => $node->title, '#weight' => -5);
130 }
131
132 if ($type->has_body) {
133 $form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
134 $form['body_field']['body']['#rows'] = 3;
135 }
136
137 $form['sparql_query'] = array('#type' => 'textarea', '#title' => t('Query'), '#default_value' => @$node->sparql_query, '#rows' => 15, '#required' => TRUE);
138 $form['sparql_endpoint'] = array('#type' => 'textfield', '#title' => t('Endpoint URL'), '#default_value' => @$node->sparql_endpoint, '#required' => TRUE);
139
140 return $form;
141 }
142
143 /**
144 * Implementation of hook_view().
145 */
146 function sparql_node_view($node, $teaser = FALSE, $page = FALSE) {
147 $node->format = FALSE; // prevent PHP notice from node_prepare()
148 $node = node_prepare($node, $teaser);
149
150 if (!$teaser && !empty($node->sparql_query)) {
151 $errors = array();
152 $result = sparql_node_result($node, $errors);
153
154 foreach ($errors as $error) {
155 drupal_set_message($error, 'error', FALSE);
156 }
157
158 if (!is_null($result)) {
159 $node->content['sparql_results'] = array('#value' => theme('sparql_results', $result), '#weight' => 10);
160 }
161 }
162
163 return $node;
164 }
165
166 /**
167 * Implementation of hook_load().
168 */
169 function sparql_node_load($node) {
170 return (object)array(
171 'sparql_query' => (string)db_result(db_query("SELECT query FROM {sparql_nodes} WHERE vid = %d", $node->vid)),
172 'sparql_endpoint' => (string)db_result(db_query("SELECT endpoint FROM {sparql_nodes} WHERE vid = %d", $node->vid)),
173 );
174 }
175
176 /**
177 * Implementation of hook_validate().
178 */
179 function sparql_node_validate($node, &$form) {
180 if (!sparql_parse($node->sparql_query, NULL, $errors)) {
181 // TODO: suppress the display of previous query results when previewing a changed query.
182
183 foreach ($errors as $error) {
184 form_set_error('sparql_query', $error);
185 }
186 }
187 }
188
189 /**
190 * Implementation of hook_insert().
191 */
192 function sparql_node_insert($node) {
193 db_query("INSERT INTO {sparql_nodes} (nid, vid, endpoint, query) VALUES (%d, %d, '%s', '%s')", $node->nid, $node->vid, $node->sparql_endpoint, $node->sparql_query);
194 }
195
196 /**
197 * Implementation of hook_update().
198 */
199 function sparql_node_update($node) {
200 if ($node->revision) {
201 return sparql_node_insert($node);
202 }
203
204 db_query("UPDATE {sparql_nodes} SET endpoint = '%s', query = '%s' WHERE vid = %d", $node->sparql_endpoint, $node->sparql_query, $node->vid);
205 cache_clear_all('sparql:' . $node->nid, 'cache');
206 }
207
208 /**
209 * Implementation of hook_delete().
210 */
211 function sparql_node_delete($node) {
212 db_query('DELETE FROM {sparql_nodes} WHERE nid = %d', $node->nid);
213 }
214
215 /**
216 * Implementation of hook_nodeapi().
217 */
218 function sparql_nodeapi(&$node, $op, $teaser, $page) {
219 switch ($op) {
220 case 'delete revision':
221 if ($node->type == 'sparql') {
222 db_query('DELETE FROM {sparql_nodes} WHERE vid = %d', $node->vid);
223 }
224 break;
225 }
226 }
227
228 function sparql_node_result($node, &$errors = array()) {
229 $result = ($result = cache_get('sparql:' . $node->nid, 'cache')) ? $result->data : NULL;
230 if (is_null($result) || $result == '') {
231 $result = sparql_query($node->sparql_query, array('endpoint' => $node->sparql_endpoint), $errors);
232 cache_set('sparql:' . $node->nid, $result, 'cache', CACHE_PERMANENT);
233 }
234 return $result;
235 }
236
237 //////////////////////////////////////////////////////////////////////////////
238 // RDF API hooks
239
240 /**
241 * Implementation of hook_rdf_namespaces().
242 */
243 function sparql_rdf_namespaces() {
244 return array(
245 'rs' => 'http://www.w3.org/2005/sparql-results#',
246 );
247 }

  ViewVC Help
Powered by ViewVC 1.1.2