forked from Islandora/tuque
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RepositoryQuery.php
198 lines (182 loc) · 5.95 KB
/
RepositoryQuery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
/**
* @file
* This file provides some methods for doing RDF queries.
*
* The essance of this file was taken from some commits that Adam Vessy made to
* Islandora 6.x, so I'd like to give him some credit here.
*/
class RepositoryQuery {
public $connection;
const SIMPLE_XML_NAMESPACE = "http://www.w3.org/2001/sw/DataAccess/rf1/result";
/**
* Construct a new RI object.
*
* @param RepositoryConnection $connection
* The connection to connect to the RI with.
*/
public function __construct(RepositoryConnection $connection) {
$this->connection = $connection;
}
/**
* Parse the passed in Sparql XML string into a more easily usable format.
*
* @param string $sparql
* A string containing Sparql result XML.
*
* @return array
* Indexed (numerical) array, containing a number of associative arrays,
* with keys being the same as the variable names in the query.
* URIs beginning with 'info:fedora/' will have this beginning stripped
* off, to facilitate their use as PIDs.
*/
public static function parseSparqlResults($sparql) {
// Load the results into a XMLReader Object.
$xmlReader = new XMLReader();
$xmlReader->xml($sparql);
// Storage.
$results = array();
// Build the results.
while ($xmlReader->read()) {
if ($xmlReader->localName === 'result') {
if ($xmlReader->nodeType == XMLReader::ELEMENT) {
// Initialize a single result.
$r = array();
}
elseif ($xmlReader->nodeType == XMLReader::END_ELEMENT) {
// Add result to results
$results[] = $r;
}
}
elseif ($xmlReader->nodeType == XMLReader::ELEMENT && $xmlReader->depth == 3) {
$val = array();
$uri = $xmlReader->getAttribute('uri');
if ($uri !== NULL) {
$val['value'] = self::pidUriToBarePid($uri);
$val['uri'] = (string) $uri;
$val['type'] = 'pid';
}
else {
//deal with any other types
$val['type'] = 'literal';
$val['value'] = (string) $xmlReader->readString();
}
$r[$xmlReader->localName] = $val;
}
}
$xmlReader->close();
return $results;
}
/**
* Performs the given Resource Index query and return the results.
*
* @param string $query
* A string containing the RI query to perform.
* @param string $type
* The type of query to perform, as used by the risearch interface.
* @param int $limit
* An integer, used to limit the number of results to return.
* @param string $format
* A string indicating the type format desired, as supported by the
* underlying triple store.
*
* @return string
* The contents returned, in the $format specified.
*/
protected function internalQuery($query, $type = 'itql', $limit = -1, $format = 'Sparql') {
// Construct the query URL.
$url = '/risearch';
$seperator = '?';
$this->connection->addParam($url, $seperator, 'type', 'tuples');
$this->connection->addParam($url, $seperator, 'flush', TRUE);
$this->connection->addParam($url, $seperator, 'format', $format);
$this->connection->addParam($url, $seperator, 'lang', $type);
$this->connection->addParam($url, $seperator, 'query', $query);
// Add limit if provided.
if ($limit > 0) {
$this->connection->addParam($url, $seperator, 'limit', $limit);
}
$result = $this->connection->getRequest($url);
return $result['content'];
}
/**
* Performs the given Resource Index query and return the results.
*
* @param string $query
* A string containing the RI query to perform.
* @param string $type
* The type of query to perform, as used by the risearch interface.
* @param int $limit
* An integer, used to limit the number of results to return.
*
* @return array
* Indexed (numerical) array, containing a number of associative arrays,
* with keys being the same as the variable names in the query.
* URIs beginning with 'info:fedora/' will have this beginning stripped
* off, to facilitate their use as PIDs.
*/
public function query($query, $type = 'itql', $limit = -1) {
// Pass the query's results off to a decent parser.
return self::parseSparqlResults($this->internalQuery($query, $type, $limit));
}
/**
* Thin wrapper for self::query().
*
* @see self::query()
*/
public function itqlQuery($query, $limit = -1) {
return $this->query($query, 'itql', $limit);
}
/**
* Thin wrapper for self::query().
*
* This function once took a 3rd parameter for an offset that did not work.
* It has been eliminated. If you wish to use an offset include it in the
* query.
*
* @see self::query()
*/
public function sparqlQuery($query, $limit = -1) {
return $this->query($query, 'sparql', $limit);
}
/**
* Utility function used in self::query().
*
* Strips off the 'info:fedora/' prefix from the passed in string.
*
* @param string $uri
* A string containing a URI.
*
* @return string
* The input string less the 'info:fedora/' prefix (if it has it).
* The original string otherwise.
*/
protected static function pidUriToBarePid($uri) {
$chunk = 'info:fedora/';
$pos = strpos($uri, $chunk);
// Remove info:fedora/ chunk.
if ($pos === 0) {
return substr($uri, strlen($chunk));
}
// Doesn't start with info:fedora/ chunk...
else {
return $uri;
}
}
/**
* Get the count of tuples a query selects.
*
* Given that some langauges do not have a built-in construct for performing
* counting/aggregation, a method to help with this is desirable.
*
* @param string $query
* A query for which to count the number tuples returned.
*
* @return int
* The number of tuples which were selected.
*/
public function countQuery($query, $type='itql') {
$content = $this->internalQuery($query, $type, -1, 'count');
return intval($content);
}
}