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

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

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


Revision 1.2 - (show annotations) (download) (as text)
Fri Jun 6 10:09:07 2008 UTC (17 months, 3 weeks ago) by arto
Branch: MAIN
CVS Tags: DRUPAL-6--1-0-ALPHA1, HEAD
Changes since 1.1: +0 -0 lines
File MIME type: text/x-php
Imported latest 6.x version (r472) from SVN development repository.

Changelog:
- Update menu handling for Drupal 6.2 compatibility.
- Improved error handling in HTTP client.
- Implemented preliminary parsing of resultsets in XML format.
- Implemented more extensive datatype/xml:lang handling for resultset bindings.
- Added sponsors to README.txt.
1 <?php
2 // $Id$
3 /**
4 * sparql.inc - SPARQL API query engine implementation.
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 // Module settings
14
15 define('SPARQL_ENDPOINT', variable_get('sparql_endpoint', FALSE));
16
17 //////////////////////////////////////////////////////////////////////////////
18 // SPARQL API query constructors (DSL)
19
20 /**
21 * @see http://www.w3.org/TR/rdf-sparql-query/#ask
22 */
23 function sparql_ask() {
24 $args = func_get_args();
25 return call_user_func_array(array('SPARQL_Query', 'ask'), $args);
26 }
27
28 /**
29 * @see http://www.w3.org/TR/rdf-sparql-query/#select
30 */
31 function sparql_select() {
32 $args = func_get_args();
33 return call_user_func_array(array('SPARQL_Query', 'select'), $args);
34 }
35
36 /**
37 * @see http://www.w3.org/TR/rdf-sparql-query/#construct
38 */
39 function sparql_construct() {
40 $args = func_get_args();
41 return call_user_func_array(array('SPARQL_Query', 'construct'), $args);
42 }
43
44 /**
45 * @see http://www.w3.org/TR/rdf-sparql-query/#describe
46 */
47 function sparql_describe() {
48 $args = func_get_args();
49 return call_user_func_array(array('SPARQL_Query', 'describe'), $args);
50 }
51
52 //////////////////////////////////////////////////////////////////////////////
53 // SPARQL API query constructors (textual)
54
55 /**
56 * Executes a SPARQL query in text form.
57 */
58 function sparql_query($text, $options = array(), &$errors = NULL) {
59 if (!isset($options['prefixes']) || $options['prefixes']) {
60 $text = SPARQL_Query::prefixes() . "\n" . $text;
61 }
62
63 if (!empty($options['endpoint'])) {
64 require_once drupal_get_path('module', 'sparql') . '/sparql.client.inc';
65 return sparql_request($options['endpoint'], $text, $options, $errors);
66 }
67
68 return ($query = sparql_parse($text, $options, $errors)) ? $query->run() : NULL;
69 }
70
71 /**
72 * Parses a SPARQL query in text form, returning a SPARQL_Query object.
73 */
74 function sparql_parse($text, $options = array(), &$errors = NULL) {
75 $parser = ARC2::getSPARQLParser();
76 $parser->parse($text);
77
78 if ($parser->getErrors()) {
79 $errors = array();
80 foreach ($parser->getErrors() as $error) {
81 $errors[] = t('Malformed SPARQL query: %error.', array('%error' => $error));
82 }
83 return FALSE;
84 }
85
86 $ast = $parser->getQueryInfos();
87
88 $query = call_user_func(array('SPARQL_Query', $ast['query']['type']));
89 $query->distinct(!empty($ast['query']['distinct']));
90 $query->reduced(!empty($ast['query']['reduced']));
91
92 if (isset($ast['vars'])) {
93 $query->vars = array_values($ast['vars']);
94 }
95
96 if (isset($ast['query']['result_vars'])) {
97 foreach ($ast['query']['result_vars'] as $var) {
98 // $var->val
99 }
100 }
101
102 if (isset($ast['query']['result_iris'])) {
103 $query->uris = $ast['query']['result_iris'];
104 }
105
106 if (isset($ast['query']['pattern'])) {
107 $group = $ast['query']['pattern'];
108 foreach ($group['patterns'] as $subpattern) {
109 switch ($subpattern['type']) {
110 case 'triples':
111 foreach ($subpattern['patterns'] as $pattern) {
112 $query->pattern(_rdf_deconstruct_arc2_triple($pattern));
113 }
114 break;
115 case 'filter':
116 $constraint = $subpattern['constraint'];
117 //$query->filter($constraint['call']); // TODO
118 break;
119 }
120 }
121 }
122
123 if (isset($ast['query']['order_infos'])) {
124 foreach ($ast['query']['order_infos'] as $order_info) {
125 if ($order_info['type'] == 'var') { // FIXME
126 $query->order_by($order_info['val'], $order_info['direction']);
127 }
128 }
129 }
130
131 if (isset($ast['query']['offset'])) {
132 $query->offset((int)$ast['query']['offset']);
133 }
134
135 if (isset($ast['query']['limit'])) {
136 $query->limit((int)$ast['query']['limit']);
137 }
138
139 return $query;
140 }
141
142 //////////////////////////////////////////////////////////////////////////////
143 // SPARQL query builder
144
145 class SPARQL_Query implements IteratorAggregate {
146 public $type;
147 public $vars, $uris, $patterns, $filters;
148 public $distinct = FALSE, $reduced = FALSE;
149 public $order_by, $limit, $offset;
150
151 static function prefixes() {
152 $prefixes = array();
153 foreach (rdf_get_namespaces() as $prefix => $base_uri) {
154 if ($prefix != '_') { // FIXME
155 $prefixes[] = "PREFIX $prefix: <$base_uri>";
156 }
157 }
158 return implode("\n", $prefixes);
159 }
160
161 function __construct($type, array $options = array()) {
162 $this->type = $type;
163 foreach ($options as $k => $v) {
164 $this->$k = $v;
165 }
166 }
167
168 // Query forms
169 // @see http://www.w3.org/TR/rdf-sparql-query/#QueryForms
170
171 static function ask() {
172 return new self('ask');
173 }
174
175 static function select() {
176 $args = func_get_args();
177 return new self('select', array('vars' => $args));
178 }
179
180 static function construct() {
181 return new self('construct');
182 }
183
184 static function describe() {
185 return new self('describe');
186 }
187
188 // Instance methods
189
190 function distinct($value = TRUE) {
191 $this->distinct = $value;
192 return $this;
193 }
194
195 function reduced($value = TRUE) {
196 $this->reduced = $value;
197 return $this;
198 }
199
200 function pattern() {
201 $this->patterns[] = (func_num_args() == 1) && is_array(func_get_arg(0)) ? func_get_arg(0) : func_get_args();
202 return $this;
203 }
204
205 function filter() {
206 if (!is_array($this->filters)) {
207 $this->filters = array();
208 }
209 $this->filters[] = array();
210 return $this;
211 }
212
213 function order_by($var, $sort = 'asc') {
214 if (!is_array($this->order_by)) {
215 $this->order_by = array();
216 }
217 $this->order_by[$var] = $sort;
218 return $this;
219 }
220
221 function offset($offset) {
222 $this->offset = $offset;
223 return $this;
224 }
225
226 function limit($limit) {
227 $this->limit = $limit;
228 return $this;
229 }
230
231 function run() {
232 require_once drupal_get_path('module', 'sparql') . '/sparql.engine.inc';
233 return SPARQL_Engine::execute($this);
234 }
235
236 function getIterator() {
237 return is_array(($result = $this->run())) ? new ArrayIterator($result) : $result;
238 }
239
240 function __toString() {
241 // TODO
242 }
243 }

  ViewVC Help
Powered by ViewVC 1.1.2