comparison sites/all/modules/sparql/sparql.module @ 4:ce11bbd8f642

added modules
author danieleb <danielebarchiesi@me.com>
date Thu, 19 Sep 2013 10:38:44 +0100
parents
children
comparison
equal deleted inserted replaced
3:b28be78d8160 4:ce11bbd8f642
1 <?php
2 /*
3 * @file
4 * API function for running queries on external SPARQL endpoints.
5 */
6
7 define('SPARQL_ENDPOINT', 'endpoint');
8
9 /**
10 * Implements hook_help().
11 */
12 function sparql_help($path, $arg = NULL) {
13 switch ($path) {
14 case 'admin/settings/sparql':
15 return '<p>' . t('The sparql module provides an interface for other modules to perform sparql queries') . '</p>'; // TODO
16 }
17 }
18
19 /**
20 * Executes a SPARQL query.
21 *
22 * @param $query
23 * The SPARQL query to execute.
24 * @param $endpoint
25 * An endpoint object, as returned by SPARQL Registry.
26 * @return
27 * The SPARQL results.
28 */
29 function sparql_request($query, $endpoint) {
30 $rows = _sparql_request($query, $endpoint);
31 return $rows;
32 }
33
34 function sparql_get_store($name, $type = 'store') {
35 $db = $GLOBALS['databases']['default']['default'];
36 $name = "sparql_store_$name";
37
38 $config = array(
39 /* db */
40 'db_host' => $db['host'],
41 'db_name' => $db['database'],
42 'db_user' => $db['username'],
43 'db_pwd' => $db['password'],
44 /* store */
45 'store_name' => $name,
46
47 /* endpoint */
48 'endpoint_features' => array(
49 'select', 'construct', 'ask', 'describe',
50 'load', 'insert', 'delete',
51 'dump' /* dump is a special command for streaming SPOG export */
52 ),
53 'endpoint_timeout' => 60, /* not implemented in ARC2 preview */
54 'endpoint_read_key' => '', /* optional */
55 'endpoint_write_key' => 'somekey', /* optional */
56 'endpoint_max_limit' => 500, /* optional */
57 );
58
59 /* instantiation */
60 if ($type == SPARQL_ENDPOINT) {
61 $store = ARC2::getStoreEndpoint($config);
62 }
63 else {
64 $store = ARC2::getStore($config);
65 }
66 if (!$store->isSetUp()) {
67 $store->setUp();
68 }
69 return $store;
70 }
71
72 /**
73 * Runs a query against an endpoint.
74 *
75 * @param $query
76 * The SPARQL query to execute.
77 * @param $endpoint
78 * The endpoint to try the query against.
79 * @options
80 * Options to use with the remote or local store, as passed to
81 * sparql_request.
82 */
83 function _sparql_request($query, $endpoint) {
84 // Initialize connection with the endpoint.
85 $store = _sparql_init_remote_store($endpoint);
86
87 // Execute the query.
88 $rs = $store->query($query);
89 if ($errors = $store->getErrors()) {
90 // Log errors.
91 foreach ($errors as $error) {
92 trigger_error($error, E_USER_ERROR);
93 }
94 return NULL;
95 }
96 else {
97 // Success!
98 return $rs;
99 }
100 }
101
102 /**
103 * Sets up an ARC2 RDF local repository.
104 *
105 * @param $name
106 * The name of the local repository.
107 * @param $endpoint
108 * Set to TRUE if this store should also be setup as a SPARQL endpoint.
109 * @return
110 * An ARC2 store object.
111 */
112 function _sparql_init_store($name, $endpoint = FALSE) {
113 // @todo fix this. Error reporting is off because ARC2 throws strict warnings.
114 error_reporting(0);
115
116 $db_spec = $GLOBALS['databases']['default']['default'];
117
118 $config = array(
119 /* db */
120 'db_name' => $db_spec['database'],
121 'db_user' => $db_spec['username'],
122 'db_pwd' => isset($db_spec['password']) ? $db_spec['password'] : '',
123 /* store */
124 'store_name' => $name,
125
126 /* endpoint */
127 'endpoint_features' => array(
128 'select', 'construct', 'ask', 'describe',
129 //'load', 'insert', 'delete',
130 'dump' /* dump is a special command for streaming SPOG export */
131 ),
132 'endpoint_timeout' => 60, /* not implemented in ARC2 preview */
133 'endpoint_read_key' => '', /* optional */
134 'endpoint_write_key' => '', /* optional */
135 'endpoint_max_limit' => 500, /* optional */
136 );
137
138 // If this site is exposing a SPARQL endpoint, instantiate the endpoint with
139 // the endpoint class which can be used for HTTP-based data access.
140 if ($endpoint) {
141 $store = ARC2::getStoreEndpoint($config);
142 }
143 // Otherwise, instantiate a locally accessible store.
144 else {
145 $store = ARC2::getStore($config);
146 }
147 if (!$store->isSetUp()) {
148 $store->setUp();
149 }
150
151 return $store;
152 }
153
154 /**
155 * Sets up an ARC2 RDF remote store.
156 *
157 * @param $endpoint
158 * The remote SPARQL endpoint.
159 * @return
160 * An ARC2 store object.
161 */
162 function _sparql_init_remote_store($endpoint) {
163 $endpoint_url = $endpoint->uri;
164
165 // If there are query parameters that need to be added before ARC2 processing,
166 // add them here. Filter the array to remove empty values.
167 if (isset($endpoint->options['query_parameters'])) {
168 $endpoint_url = url($endpoint_url, array('query' => array_filter($endpoint->options['query_parameters'])));
169 }
170
171 $config = array(
172 /* remote endpoint */
173 'remote_store_endpoint' => $endpoint_url,
174 );
175
176 /* instantiation */
177 $store = ARC2::getRemoteStore($config);
178
179 return $store;
180 }