-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocd.php
301 lines (260 loc) · 9.99 KB
/
ocd.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
class Ocd implements Iterator {
private $size = 20; // #results per API call; impact on memory
private $query; // hash for input
private $page; // the result (page)hash we provide during iteration n=0,..,n
private $current; // pointer to array n=0,1,...,n
private $limit; //limit for results
public function __construct() {
$this->api_url = 'http://api.opencultuurdata.nl/';
$this->api_version = 'v0';
}
// todo
public function resolve() {
}
// returns version of the object that is not iteratable
// GET /(source_id)/(object_id)/source
public function get_object($id, $getsrc = false) {
assert(!isset($this->query['query']) && !isset($this->query['similar']));
unset($this->query['query']); // but source = allowed
unset($this->query['similar']);
$this->query['object_id'] = $id;
if (!$this->get_source()) {
throw new Exception('Object($id, getsrc)->Query() requires ->source($src) param');
}
$op = $this->get_source() . $this->query['object_id'] . ( $getsrc ? '/source' : null );
return $this->rest($op, "GET");
}
// GET /similar/object-id returns similar objects
public function similar($id) {
unset($this->query['query']);
unset($this->query['object_id']);
$this->query['similar'] = $id;
return $this;
}
// sets the search query string
public function search($search_str) {
unset($this->query['similar']);
unset($this->query['object_id']);
$this->query['query'] = $search_str;
return $this;
}
// Loads Query So Iteration can take place
// Flushes pointers Throws Exceptions. Returns wat's working
public function query() {
$this->current = null;
if (!$this->get_results()) {
throw new Exception("QUERY FAILS");
}
return $this;
}
// sets the (sole) source of the Query (Rijksmuseum, Stedelijk). Null == Everything
public function source($source = null) {
$this->query['source'] = $source;
return $this;
}
// { "date": { "order": "desc" }},{ "_score": { "order": "desc" }}
// meta.source, meta.processing_started, meta.processing_finished, date, date_granularity, authors, _score
public function sort($sort) {
$this->query['sort'] = $sort;
return $this;
}
// adds array of facets to current query which are added to stack
public function add_facets($facets) {
foreach ($facets as $key => $value) {
$this->query['facets'][$key] = $value;
} return $this;
}
// assumes array of filters key values which are added to stack
public function add_filters($filters) {
foreach ($filters as $key => $value) {
$this->query['filters'][$key] = $value;
}
return $this;
}
// (re)sets facets with array of facets
public function facets($facets) {
$this->query['facets'] = $facets;
return $this;
}
// (re)sets facets with array of facets
public function filters($filters) {
$this->query['filters'] = $filters;
return $this;
}
// sets the maximum results
public function limit($results = null) {
$this->limit = $results;
return $this;
}
/* Sets the file position indicator for handle to the beginning of the file stream.
* Returns TRUE on success or FALSE on failure.
*/
public function rewind() {
if ($this->current > $this->size) {// we are not in the first page.
$this->current = 0;
if (!$this->get_results())
return FALSE;
} //other cases (null (error), or already on first page)
$this->current = 0;
if (!$this->validate($this->current)) {// further error handling
return FALSE;
}
return TRUE;
}
/* The current() function simply returns the value of the array element
* that's currently being pointed to by the internal pointer.
* It does not move the pointer in any way. If the internal pointer points
* beyond the end of the elements list or the array is empty, current() returns FALSE.
*/
public function current() {
// echo "current\n";
if ($this->validate($this->current)) {
return $this->page['hits']['hits'][($this->current % $this->size)];
}
return FALSE;
}
/* The key() function simply returns the key of the array element
* that's currently being pointed to by the internal pointer.
* It does not move the pointer in any way. If the internal pointer points
* beyond the end of the elements list or the array is empty, key() returns NULL.
*/
public function key() {
//echo "key\n";
if ($this->validate($this->current)) {
return $this->current;
}
return FALSE;
}
/* Returns the array value in the next place that's pointed to
* by the internal array pointer, or FALSE if there are no more elements.
*/
public function next() {
// echo "next\n";
$this->current++;
if (!$this->validate($this->current)) {
return FALSE;
}
if ($this->current % $this->size == 0 && $this->current / $this->size > 0 &&
!$this->get_results()) {
return FALSE;
}
return $this->page['hits']['hits'][($this->current % $this->size)];
}
/* Checks if current position is valid
* The return value will be casted to boolean and then evaluated.
* Returns TRUE on success or FALSE on failure.
*/
public function valid() {
// echo "valid\n";
return $this->validate($this->current);
}
// returns the uri consisting of the url and api version
public function api_uri() {
return $this->api_url . $this->api_version . "/";
}
// Returns total number of hits. We assume they are identical on every page (might be bold, ahem)
public function total() {
if (isset($this->page)) {
return $this->page['hits']['total'];
}
return NULL;
}
// Returns max_score. We assume they are identical on every page (might be bold, ahem)
public function max_score() {
if (isset($this->page)) {
return $this->page['hits']['max_score'];
}
return NULL;
}
// temporary function for debugging
public function get_page() {
if (isset($this->page)) {
return $this->page;
}
return NULL;
}
// Returns facets. We assume they are identical on every page (might be bold, ahem)
public function get_facets() {
if (isset($this->page)) {
return $this->page['facets'];
}
return NULL;
}
// sets the api url like 'http:server.org' no trailing slash
public function api($url) {
$this->query['api_url'] = $url;
return $this;
}
// sets the api version like '/v0'. no trailing slash
public function api_version($version) {
$this->query['api_version'] = $version;
return $this;
}
// private validation function
private function validate($pos) {
if (!isset($this->page['hits']['total'])) {
return FALSE;
}
if ($pos > ($this->page['hits']['total'] - 1)) {
return FALSE;
}
if (isset($this->limit) &&
$pos >= $this->limit) {
return FALSE;
}
return TRUE;
}
private function get_results() {
assert($this->page_num() >= 0 && !isset($this->query['object_id']));
if (isset($this->query['query'])) {//query
assert(!isset($this->query['object_id']) && assert(!isset($this->query['similar'])));
$op = $this->get_source() . "search";
} else if (isset($this->query['similar'])) {//handle similar
assert(!isset($this->query['query']));
$op = $this->get_source() . "similar/" . $this->query['similar'];
}
$data['from'] = $this->page_num() * $this->size;
$data['size'] = isset($this->limit) ? (
$this->limit < $data['from'] + $this->size ?
$this->limit % $this->size : $this->size ) : $this->size;
foreach ($this->query as $key => $value) {
$data[$key] = $value;
}
$result = $this->rest($op, "POST", json_encode($data));
// var_dump($result);
$json = json_decode($result, TRUE);
if (@$json['status'] == "error") {
throw new Exception($json['error']);
}
$this->page = $json;
return TRUE;
}
private function get_source() {
if (!isset($this->query['source'])) {
return null;
}
return $this->query['source'] . "/";
}
private function page_num() {
return (int) $this->current == 0 ? $this->current : $this->current / $this->size;
}
// performs the actual curl request
private function rest($op, $request, $post_fields = null) {
//print("rest() $request op " . $this->api_uri() . $op . " fields $post_fields\n");
$ch = curl_init($this->api_uri() . $op);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request);
if ($post_fields != null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($post_fields)));
$result = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) {
return $result;
}
throw new Exception("Http error " . curl_getinfo($ch, CURLINFO_HTTP_CODE));
}
}