-
Notifications
You must be signed in to change notification settings - Fork 1
/
query.php
394 lines (366 loc) · 10 KB
/
query.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
/*
This is a derived work.
Derived from: Calibre, licensed under GPL v3
http://calibre-ebook.com
Changes:
license: GPL v3
copyright: 2010, Charles Haley
http://charles.haleys.org
*/
define('AND_TOKEN', 1);
define('OR_TOKEN', 2);
define('NOT_TOKEN', 3);
define('VALUE_TOKEN', 4);
define('EOF_TOKEN', 5);
/*
* A node in the parse tree. The language is an expression language,
* so all nodes can be equal. Unary nodes simply have the right side NULL.
*/
class Node {
function __construct($node_type, $left, $right, $leaf) {
$this->node_type = $node_type;
$this->left = $left;
$this->right = $right;
$this->leaf = $leaf;
}
}
/*
* The leaf (value) node. This node knows how to test a field/value pair
* against the test specified in the search.
*/
class Leaf {
function __construct($prefix, $match_type, $test) {
$this->field = $prefix;
$this->match_type = $match_type;
$this->test = $test;
$this->value = false;
}
function set($val) {
$this->value = $val;
}
function match_value($val) {
// test for field exists
if ($this->test == 'true') {
// special case test for empty strings, which match false
if (!isset($val) || (is_string($val) && strlen($val) == 0))
return false;
// field is defined, so true test is true
return true;
}
if ($this->test == 'false') {
// special case test for empty strings, which match false
if (!isset($val) || (is_string($val) && strlen($val) == 0))
return true;
// field is defined, so false test is false
return false;
}
// Turn bools into strings. Yes, these should be translatable
if (is_bool($val))
$val = $val ? 'Yes' : 'No';
if ($this->match_type == '=') {
if (strcasecmp($val, $this->test) == 0)
return true;
} else if ($this->match_type == '~') {
if (substr($this->test, 0, 1) == '/') {
if (preg_match("$this->test", $val))
return true;
} else {
if (preg_match("/$this->test/i", $val))
return true;
}
} else if (stristr($val, $this->test)) {
return true;
}
return NULL;
}
function match($key, $val) {
if ($this->field)
if ($this->field != $key)
return NULL;
if (is_array($val)) {
foreach ($val as $v) {
if ($key == 'formats')
$a = $this->match_value($v['format']);
else
$a = $this->match_value($v);
if (isset($a))
return $a;
}
return NULL;
}
return $this->match_value($val);
}
}
/*
* Representation of a compiled query. 'Compiles' the query + restriction
* into a parse tree and a compact node-match specification.
*/
class Query {
function __construct($restriction, $query_string, $field_metadata) {
$this->fm = $field_metadata;
$this->token_value = '';
$this->leaves = array();
$this->error_message = '';
if ($query_string) {
$this->query = $query_string;
try {
$query_tree = $this->expression();
} catch (Exception $e) {
$this->error_message = 'Query error: ' . $e->getMessage();
$query_tree = NULL;
}
} else
$query_tree = NULL;
if ($restriction) {
$this->query = $restriction;
try {
$restriction_tree = $this->expression();
} catch (Exception $e) {
$this->error_message = 'Restriction error: ' . $e->getMessage();
$restriction_tree = NULL;
}
} else
$restriction_tree = NULL;
if ($query_tree && $restriction_tree)
return $this->parse_tree = new Node(AND_TOKEN, $restriction_tree, $query_tree, NULL);
if ($query_tree)
return $this->parse_tree = $query_tree;
if ($restriction_tree)
return $this->parse_tree = $restriction_tree;
return $this->parse_tree = NULL;
}
function is_empty() {
return !isset($this->parse_tree);
}
/*
* Prepare to evaluate a query for a book. The main work is to create
* an array of value nodes that we need to look for. The search routine
* must call this method, then eval_field on each book field, then
* evaluate.
*/
function prepare() {
$this->leaves_to_eval = array();
foreach ($this->leaves as $leaf) {
$leaf->set(false);
$this->leaves_to_eval[] = $leaf;
}
}
/*
* Check if a field + value match one of the search's value tests.
* Return true if no tests remain (small optimization).
*/
function eval_fields($book) {
/*
* First check all named fields.
*/
foreach ($this->leaves as $leaf) {
if (!$leaf->field)
continue;
if (!array_key_exists($leaf->field, $book))
continue;
$ans = $leaf->match($leaf->field, $book[$leaf->field]);
if (isset($ans)) {
$leaf->set($ans);
unset($this->leaves_to_eval[$leaf->field]);
continue;
}
}
// No fields left? Return
if (count($this->leaves_to_eval) == 0)
return;
/*
* Now check unnamed fields.
*/
foreach ($book as $k => $v) {
// Don't bother to test NULL values
if (!isset($v))
continue;
foreach ($this->leaves as $dex =>$leaf) {
if (!$leaf->value) {
$ans = $leaf->match($k, $v);
if (isset($ans)) {
/*
* We have an answer for this field test. Don't
* check it against any more fields.
*/
$leaf->set($ans);
unset($this->leaves_to_eval[$dex]);
continue;
}
}
}
// we are done if no fields left to check
if (count($this->leaves_to_eval) == 0)
return;
}
}
/*
* Evaluate the parse tree. This assumes that the leaf (value) nodes
* have already been set by eval_field.
*/
function evaluate() {
/*
* First loop through the tests, looking for :false tests that
* were not removed. If one is there, it means that the field was
* not defined in the book and therefore has a value of true.
*/
foreach ($this->leaves_to_eval as $key => $leaf) {
if ($leaf->test == 'false') {
$leaf->set(true);
unset($this->leaves_to_eval[$key]);
}
}
return $this->_eval($this->parse_tree);
}
/*
* Recursively walk the parse tree, computing the true/false value for
* this book.
*/
function _eval($node) {
if ($node->node_type == AND_TOKEN)
return $this->_eval($node->left) & $this->_eval($node->right);
if ($node->node_type == OR_TOKEN)
return $this->_eval($node->left) | $this->_eval($node->right);
if ($node->node_type == NOT_TOKEN)
return !$this->_eval($node->left);
if ($node->node_type == VALUE_TOKEN){
return $node->leaf->value;
}
throw new Exception('Unknown node in query eval');
}
/*
* A basic lexical analyser for the query language.
*/
function lex($pop_token) {
$this->query = trim ($this->query);
if (strlen($this->query) == 0)
return EOF_TOKEN;
if (substr($this->query, 0, 1) == '(') {
if ($pop_token)
$this->query = substr($this->query, 1);
return '(';
}
if (substr($this->query, 0, 1) == ')') {
if ($pop_token)
$this->query = substr($this->query, 1);
return ')';
}
if (preg_match('/^and(\s|\()/i', $this->query)) {
if ($pop_token)
$this->query = substr($this->query, 3);
return AND_TOKEN;
}
if (preg_match('/^or(\s|\()/i', $this->query)) {
if ($pop_token)
$this->query = substr($this->query, 2);
return OR_TOKEN;
}
if (preg_match('/^not(\s|\()/i', $this->query)) {
if ($pop_token)
$this->query = substr($this->query, 3);
return NOT_TOKEN;
}
if (preg_match('/^(\S*?".+?")([ \(\)].*|$)/i', $this->query, $m)) {
$this->token_value = $m[1];
if ($pop_token)
$this->query = $m[2];
return VALUE_TOKEN;
}
if (preg_match('/^(.+?)([ \(\)].*|$)/i', $this->query, $m)) {
$this->token_value = $m[1];
if ($pop_token)
$this->query = $m[2];
return VALUE_TOKEN;
}
throw new Exception('Lex didn\'t find something useful');
}
/*
* Recursive descent parser. Top-most node is OR.
*/
function expression() {
$left = $this->term();
while ($this->lex(false) == OR_TOKEN) {
$this->lex(true);
$right = $this->term();
$left = new Node(OR_TOKEN, $left, $right, NULL);
}
return $left;
}
/*
* Deal with AND.
*/
function term() {
$left = $this->factor();
for (;;) {
$token = $this->lex(false);
if ($token != AND_TOKEN & $token != VALUE_TOKEN)
return $left;
if ($token == AND_TOKEN)
$this->lex(true);
$right = $this->factor();
$left = new Node(AND_TOKEN, $left, $right, NULL);
}
return $left;
}
/*
* Deal with NOT and value tests
*/
function factor() {
$token = $this->lex(false);
if ($token == '(') {
$token = $this->lex(true);
$tree = $this->expression();
if ($this->lex(true) != ')')
throw new Exception('Parse error: closing parenthesis expected');
return $tree;
}
$token = $this->lex(true);
if ($token == NOT_TOKEN)
return new Node(NOT_TOKEN, $this->factor(), NULL, NULL);
if ($token != VALUE_TOKEN)
throw new Exception('Parse error: value token expected');
$t = $this->token_value;
if (preg_match('/((.*?):)?([=~]?)("?)(.*?)(\4)$/', $t, $m)) {
$prefix = $this->fm->search_term_to_field_key(strtolower($m[2]));
if ($prefix && !$this->fm->key_exists($prefix)) {
throw new Exception("Unknown metadata field: $prefix");
}
$match_type = $m[3];
$value = $m[5];
} else
throw new Exception('value specifier $t not understood');
$leaf = new Leaf($prefix, $match_type, $value);
$left = new Node(VALUE_TOKEN, NULL, NULL, $leaf);
$this->leaves[] = $leaf;
return $left;
}
/*
* For debugging
*/
function print_tree() {
$this->_print_tree('', $this->parse_tree);
}
function _print_tree($indent, $node) {
if (!isset($node) || $node == NULL)
return;
print "$indent$node->node_type: ";
switch ($node->node_type) {
case AND_TOKEN:
print 'AND';
break;
case OR_TOKEN:
print 'OR';
break;
case NOT_TOKEN:
print 'NOT';
break;
}
if ($node->leaf)
print $node->leaf->field . ':' . $node->leaf->test;
print "<br>";
$this->_print_tree($indent . ' ', $node->left);
$this->_print_tree($indent . ' ', $node->right);
}
}
?>