1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based photo gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2014 Piwigo Team http://piwigo.org | |
---|
6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
---|
7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
---|
8 | // +-----------------------------------------------------------------------+ |
---|
9 | // | This program is free software; you can redistribute it and/or modify | |
---|
10 | // | it under the terms of the GNU General Public License as published by | |
---|
11 | // | the Free Software Foundation | |
---|
12 | // | | |
---|
13 | // | This program is distributed in the hope that it will be useful, but | |
---|
14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
16 | // | General Public License for more details. | |
---|
17 | // | | |
---|
18 | // | You should have received a copy of the GNU General Public License | |
---|
19 | // | along with this program; if not, write to the Free Software | |
---|
20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
21 | // | USA. | |
---|
22 | // +-----------------------------------------------------------------------+ |
---|
23 | |
---|
24 | /** |
---|
25 | * @package functions\search |
---|
26 | */ |
---|
27 | |
---|
28 | |
---|
29 | /** |
---|
30 | * Returns search rules stored into a serialized array in "search" |
---|
31 | * table. Each search rules set is numericaly identified. |
---|
32 | * |
---|
33 | * @param int $search_id |
---|
34 | * @return array |
---|
35 | */ |
---|
36 | function get_search_array($search_id) |
---|
37 | { |
---|
38 | if (!is_numeric($search_id)) |
---|
39 | { |
---|
40 | die('Search id must be an integer'); |
---|
41 | } |
---|
42 | |
---|
43 | $query = ' |
---|
44 | SELECT rules |
---|
45 | FROM '.SEARCH_TABLE.' |
---|
46 | WHERE id = '.$search_id.' |
---|
47 | ;'; |
---|
48 | list($serialized_rules) = pwg_db_fetch_row(pwg_query($query)); |
---|
49 | |
---|
50 | return unserialize($serialized_rules); |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | * Returns the SQL clause for a search. |
---|
55 | * Transforms the array returned by get_search_array() into SQL sub-query. |
---|
56 | * |
---|
57 | * @param array $search |
---|
58 | * @return string |
---|
59 | */ |
---|
60 | function get_sql_search_clause($search) |
---|
61 | { |
---|
62 | // SQL where clauses are stored in $clauses array during query |
---|
63 | // construction |
---|
64 | $clauses = array(); |
---|
65 | |
---|
66 | foreach (array('file','name','comment','author') as $textfield) |
---|
67 | { |
---|
68 | if (isset($search['fields'][$textfield])) |
---|
69 | { |
---|
70 | $local_clauses = array(); |
---|
71 | foreach ($search['fields'][$textfield]['words'] as $word) |
---|
72 | { |
---|
73 | $local_clauses[] = $textfield." LIKE '%".$word."%'"; |
---|
74 | } |
---|
75 | |
---|
76 | // adds brackets around where clauses |
---|
77 | $local_clauses = prepend_append_array_items($local_clauses, '(', ')'); |
---|
78 | |
---|
79 | $clauses[] = implode( |
---|
80 | ' '.$search['fields'][$textfield]['mode'].' ', |
---|
81 | $local_clauses |
---|
82 | ); |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | if (isset($search['fields']['allwords'])) |
---|
87 | { |
---|
88 | $fields = array('file', 'name', 'comment', 'author'); |
---|
89 | // in the OR mode, request bust be : |
---|
90 | // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%') |
---|
91 | // OR (field1 LIKE '%word2%' OR field2 LIKE '%word2%')) |
---|
92 | // |
---|
93 | // in the AND mode : |
---|
94 | // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%') |
---|
95 | // AND (field1 LIKE '%word2%' OR field2 LIKE '%word2%')) |
---|
96 | $word_clauses = array(); |
---|
97 | foreach ($search['fields']['allwords']['words'] as $word) |
---|
98 | { |
---|
99 | $field_clauses = array(); |
---|
100 | foreach ($fields as $field) |
---|
101 | { |
---|
102 | $field_clauses[] = $field." LIKE '%".$word."%'"; |
---|
103 | } |
---|
104 | // adds brackets around where clauses |
---|
105 | $word_clauses[] = implode( |
---|
106 | "\n OR ", |
---|
107 | $field_clauses |
---|
108 | ); |
---|
109 | } |
---|
110 | |
---|
111 | array_walk( |
---|
112 | $word_clauses, |
---|
113 | create_function('&$s','$s="(".$s.")";') |
---|
114 | ); |
---|
115 | |
---|
116 | // make sure the "mode" is either OR or AND |
---|
117 | if ($search['fields']['allwords']['mode'] != 'AND' and $search['fields']['allwords']['mode'] != 'OR') |
---|
118 | { |
---|
119 | $search['fields']['allwords']['mode'] = 'AND'; |
---|
120 | } |
---|
121 | |
---|
122 | $clauses[] = "\n ". |
---|
123 | implode( |
---|
124 | "\n ". $search['fields']['allwords']['mode']. "\n ", |
---|
125 | $word_clauses |
---|
126 | ); |
---|
127 | } |
---|
128 | |
---|
129 | foreach (array('date_available', 'date_creation') as $datefield) |
---|
130 | { |
---|
131 | if (isset($search['fields'][$datefield])) |
---|
132 | { |
---|
133 | $clauses[] = $datefield." = '".$search['fields'][$datefield]['date']."'"; |
---|
134 | } |
---|
135 | |
---|
136 | foreach (array('after','before') as $suffix) |
---|
137 | { |
---|
138 | $key = $datefield.'-'.$suffix; |
---|
139 | |
---|
140 | if (isset($search['fields'][$key])) |
---|
141 | { |
---|
142 | $clauses[] = $datefield. |
---|
143 | ($suffix == 'after' ? ' >' : ' <'). |
---|
144 | ($search['fields'][$key]['inc'] ? '=' : ''). |
---|
145 | " '".$search['fields'][$key]['date']."'"; |
---|
146 | } |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | if (isset($search['fields']['cat'])) |
---|
151 | { |
---|
152 | if ($search['fields']['cat']['sub_inc']) |
---|
153 | { |
---|
154 | // searching all the categories id of sub-categories |
---|
155 | $cat_ids = get_subcat_ids($search['fields']['cat']['words']); |
---|
156 | } |
---|
157 | else |
---|
158 | { |
---|
159 | $cat_ids = $search['fields']['cat']['words']; |
---|
160 | } |
---|
161 | |
---|
162 | $local_clause = 'category_id IN ('.implode(',', $cat_ids).')'; |
---|
163 | $clauses[] = $local_clause; |
---|
164 | } |
---|
165 | |
---|
166 | // adds brackets around where clauses |
---|
167 | $clauses = prepend_append_array_items($clauses, '(', ')'); |
---|
168 | |
---|
169 | $where_separator = |
---|
170 | implode( |
---|
171 | "\n ".$search['mode'].' ', |
---|
172 | $clauses |
---|
173 | ); |
---|
174 | |
---|
175 | $search_clause = $where_separator; |
---|
176 | |
---|
177 | return $search_clause; |
---|
178 | } |
---|
179 | |
---|
180 | /** |
---|
181 | * Returns the list of items corresponding to the advanced search array. |
---|
182 | * |
---|
183 | * @param array $search |
---|
184 | * @param string $images_where optional additional restriction on images table |
---|
185 | * @return array |
---|
186 | */ |
---|
187 | function get_regular_search_results($search, $images_where='') |
---|
188 | { |
---|
189 | global $conf; |
---|
190 | $forbidden = get_sql_condition_FandF( |
---|
191 | array |
---|
192 | ( |
---|
193 | 'forbidden_categories' => 'category_id', |
---|
194 | 'visible_categories' => 'category_id', |
---|
195 | 'visible_images' => 'id' |
---|
196 | ), |
---|
197 | "\n AND" |
---|
198 | ); |
---|
199 | |
---|
200 | $items = array(); |
---|
201 | $tag_items = array(); |
---|
202 | |
---|
203 | if (isset($search['fields']['tags'])) |
---|
204 | { |
---|
205 | $tag_items = get_image_ids_for_tags( |
---|
206 | $search['fields']['tags']['words'], |
---|
207 | $search['fields']['tags']['mode'] |
---|
208 | ); |
---|
209 | } |
---|
210 | |
---|
211 | $search_clause = get_sql_search_clause($search); |
---|
212 | |
---|
213 | if (!empty($search_clause)) |
---|
214 | { |
---|
215 | $query = ' |
---|
216 | SELECT DISTINCT(id) |
---|
217 | FROM '.IMAGES_TABLE.' i |
---|
218 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id |
---|
219 | WHERE '.$search_clause; |
---|
220 | if (!empty($images_where)) |
---|
221 | { |
---|
222 | $query .= "\n AND ".$images_where; |
---|
223 | } |
---|
224 | $query .= $forbidden.' |
---|
225 | '.$conf['order_by']; |
---|
226 | $items = array_from_query($query, 'id'); |
---|
227 | } |
---|
228 | |
---|
229 | if ( !empty($tag_items) ) |
---|
230 | { |
---|
231 | switch ($search['mode']) |
---|
232 | { |
---|
233 | case 'AND': |
---|
234 | if (empty($search_clause)) |
---|
235 | { |
---|
236 | $items = $tag_items; |
---|
237 | } |
---|
238 | else |
---|
239 | { |
---|
240 | $items = array_values( array_intersect($items, $tag_items) ); |
---|
241 | } |
---|
242 | break; |
---|
243 | case 'OR': |
---|
244 | $before_count = count($items); |
---|
245 | $items = array_unique( |
---|
246 | array_merge( |
---|
247 | $items, |
---|
248 | $tag_items |
---|
249 | ) |
---|
250 | ); |
---|
251 | break; |
---|
252 | } |
---|
253 | } |
---|
254 | |
---|
255 | return $items; |
---|
256 | } |
---|
257 | |
---|
258 | /** |
---|
259 | * Finds if a char is a letter, a figure or any char of the extended ASCII table (>127). |
---|
260 | * |
---|
261 | * @param char $ch |
---|
262 | * @return bool |
---|
263 | */ |
---|
264 | function is_word_char($ch) |
---|
265 | { |
---|
266 | return ($ch>='0' && $ch<='9') || ($ch>='a' && $ch<='z') || ($ch>='A' && $ch<='Z') || ord($ch)>127; |
---|
267 | } |
---|
268 | |
---|
269 | /** |
---|
270 | * Finds if a char is a special token for word start: [{<=*+ |
---|
271 | * |
---|
272 | * @param char $ch |
---|
273 | * @return bool |
---|
274 | */ |
---|
275 | function is_odd_wbreak_begin($ch) |
---|
276 | { |
---|
277 | return strpos('[{<=*+', $ch)===false ? false:true; |
---|
278 | } |
---|
279 | |
---|
280 | /** |
---|
281 | * Finds if a char is a special token for word end: ]}>=*+ |
---|
282 | * |
---|
283 | * @param char $ch |
---|
284 | * @return bool |
---|
285 | */ |
---|
286 | function is_odd_wbreak_end($ch) |
---|
287 | { |
---|
288 | return strpos(']}>=*+', $ch)===false ? false:true; |
---|
289 | } |
---|
290 | |
---|
291 | |
---|
292 | define('QST_QUOTED', 0x01); |
---|
293 | define('QST_NOT', 0x02); |
---|
294 | define('QST_OR', 0x04); |
---|
295 | define('QST_WILDCARD_BEGIN', 0x08); |
---|
296 | define('QST_WILDCARD_END', 0x10); |
---|
297 | define('QST_WILDCARD', QST_WILDCARD_BEGIN|QST_WILDCARD_END); |
---|
298 | |
---|
299 | |
---|
300 | class QSearchScope |
---|
301 | { |
---|
302 | var $id; |
---|
303 | var $aliases; |
---|
304 | var $is_text; |
---|
305 | var $nullable; |
---|
306 | |
---|
307 | function __construct($id, $aliases, $nullable=false, $is_text=true) |
---|
308 | { |
---|
309 | $this->id = $id; |
---|
310 | $this->aliases = $aliases; |
---|
311 | $this->is_text = $is_text; |
---|
312 | $this->nullable =$nullable; |
---|
313 | } |
---|
314 | |
---|
315 | function parse($token) |
---|
316 | { |
---|
317 | if (!$this->nullable && 0==strlen($token->term)) |
---|
318 | return false; |
---|
319 | return true; |
---|
320 | } |
---|
321 | } |
---|
322 | |
---|
323 | class QNumericRangeScope extends QSearchScope |
---|
324 | { |
---|
325 | private $epsilon; |
---|
326 | function __construct($id, $aliases, $nullable=false, $epsilon=0) |
---|
327 | { |
---|
328 | parent::__construct($id, $aliases, $nullable, false); |
---|
329 | $this->epsilon = $epsilon; |
---|
330 | } |
---|
331 | |
---|
332 | function parse($token) |
---|
333 | { |
---|
334 | $str = $token->term; |
---|
335 | if ( ($pos = strpos($str, '..')) !== false) |
---|
336 | $range = array( substr($str,0,$pos), substr($str, $pos+2)); |
---|
337 | elseif ('>' == @$str[0])// ratio:>1 |
---|
338 | $range = array( substr($str,1), ''); |
---|
339 | elseif ('<' == @$str[0]) // size:<5mp |
---|
340 | $range = array('', substr($str,1)); |
---|
341 | elseif( ($token->modifier & QST_WILDCARD_BEGIN) ) |
---|
342 | $range = array('', $str); |
---|
343 | elseif( ($token->modifier & QST_WILDCARD_END) ) |
---|
344 | $range = array($str, ''); |
---|
345 | else |
---|
346 | $range = array($str, $str); |
---|
347 | |
---|
348 | foreach ($range as $i =>&$val) |
---|
349 | { |
---|
350 | if (preg_match('#^([0-9.]+)/([0-9.]+)$#i', $val, $matches)) |
---|
351 | { |
---|
352 | $val = floatval($matches[1]/$matches[2]); |
---|
353 | } |
---|
354 | elseif (preg_match('/^([0-9.]+)([km])?/i', $val, $matches)) |
---|
355 | { |
---|
356 | $val = floatval($matches[1]); |
---|
357 | if (isset($matches[2])) |
---|
358 | { |
---|
359 | if ($matches[2]=='k' || $matches[2]=='K') |
---|
360 | { |
---|
361 | $val *= 1000; |
---|
362 | if ($i) $val += 999; |
---|
363 | } |
---|
364 | if ($matches[2]=='m' || $matches[2]=='M') |
---|
365 | { |
---|
366 | $val *= 1000000; |
---|
367 | if ($i) $val += 999999; |
---|
368 | } |
---|
369 | } |
---|
370 | } |
---|
371 | else |
---|
372 | $val = ''; |
---|
373 | if (is_numeric($val)) |
---|
374 | { |
---|
375 | if ($i) |
---|
376 | $val += $this->epsilon; |
---|
377 | else |
---|
378 | $val -= $this->epsilon; |
---|
379 | } |
---|
380 | } |
---|
381 | |
---|
382 | if (!$this->nullable && $range[0]=='' && $range[1] == '') |
---|
383 | return false; |
---|
384 | $token->scope_data = $range; |
---|
385 | return true; |
---|
386 | } |
---|
387 | |
---|
388 | function get_sql($field, $token) |
---|
389 | { |
---|
390 | $clauses = array(); |
---|
391 | if ($token->scope_data[0]!=='') |
---|
392 | $clauses[] = $field.' >= ' .$token->scope_data[0].' '; |
---|
393 | if ($token->scope_data[1]!=='') |
---|
394 | $clauses[] = $field.' <= ' .$token->scope_data[1].' '; |
---|
395 | |
---|
396 | if (empty($clauses)) |
---|
397 | { |
---|
398 | if ($token->modifier & QST_WILDCARD) |
---|
399 | return $field.' IS NOT NULL'; |
---|
400 | else |
---|
401 | return $field.' IS NULL'; |
---|
402 | } |
---|
403 | return '('.implode(' AND ', $clauses).')'; |
---|
404 | } |
---|
405 | } |
---|
406 | |
---|
407 | |
---|
408 | class QDateRangeScope extends QSearchScope |
---|
409 | { |
---|
410 | function __construct($id, $aliases, $nullable=false) |
---|
411 | { |
---|
412 | parent::__construct($id, $aliases, $nullable, false); |
---|
413 | } |
---|
414 | |
---|
415 | function parse($token) |
---|
416 | { |
---|
417 | $str = $token->term; |
---|
418 | if ( ($pos = strpos($str, '..')) !== false) |
---|
419 | $range = array( substr($str,0,$pos), substr($str, $pos+2)); |
---|
420 | elseif ('>' == @$str[0]) |
---|
421 | $range = array( substr($str,1), ''); |
---|
422 | elseif ('<' == @$str[0]) |
---|
423 | $range = array('', substr($str,1)); |
---|
424 | elseif( ($token->modifier & QST_WILDCARD_BEGIN) ) |
---|
425 | $range = array('', $str); |
---|
426 | elseif( ($token->modifier & QST_WILDCARD_END) ) |
---|
427 | $range = array($str, ''); |
---|
428 | else |
---|
429 | $range = array($str, $str); |
---|
430 | |
---|
431 | foreach ($range as $i =>&$val) |
---|
432 | { |
---|
433 | if (preg_match('/([0-9]{4})-?((?:0?[0-9])|(?:1[0-2]))?-?(((?:0?[0-9])|(?:[1-3][0-9])))?/', $val, $matches)) |
---|
434 | { |
---|
435 | array_shift($matches); |
---|
436 | if (!isset($matches[1])) |
---|
437 | $matches[1] = !$i ? 1 : 12; |
---|
438 | if (!isset($matches[2])) |
---|
439 | $matches[2] = !$i ? 1 : 31; |
---|
440 | $val = $matches; |
---|
441 | } |
---|
442 | elseif (strlen($val)) |
---|
443 | return false; |
---|
444 | } |
---|
445 | |
---|
446 | if (!$this->nullable && $range[0]=='' && $range[1] == '') |
---|
447 | return false; |
---|
448 | |
---|
449 | $token->scope_data = $range; |
---|
450 | return true; |
---|
451 | } |
---|
452 | |
---|
453 | function get_sql($field, $token) |
---|
454 | { |
---|
455 | $clauses = array(); |
---|
456 | if ($token->scope_data[0]!=='') |
---|
457 | $clauses[] = $field.' >= \'' . implode('-',$token->scope_data[0]).'\''; |
---|
458 | if ($token->scope_data[1]!=='') |
---|
459 | $clauses[] = $field.' <= \'' . implode('-',$token->scope_data[1]).' 23:59:59\''; |
---|
460 | |
---|
461 | if (empty($clauses)) |
---|
462 | { |
---|
463 | if ($token->modifier & QST_WILDCARD) |
---|
464 | return $field.' IS NOT NULL'; |
---|
465 | else |
---|
466 | return $field.' IS NULL'; |
---|
467 | } |
---|
468 | return '('.implode(' AND ', $clauses).')'; |
---|
469 | } |
---|
470 | } |
---|
471 | |
---|
472 | /** |
---|
473 | * Analyzes and splits the quick/query search query $q into tokens. |
---|
474 | * q='john bill' => 2 tokens 'john' 'bill' |
---|
475 | * Special characters for MySql full text search (+,<,>,~) appear in the token modifiers. |
---|
476 | * The query can contain a phrase: 'Pierre "New York"' will return 'pierre' qnd 'new york'. |
---|
477 | * |
---|
478 | * @param string $q |
---|
479 | */ |
---|
480 | |
---|
481 | /** Represents a single word or quoted phrase to be searched.*/ |
---|
482 | class QSingleToken |
---|
483 | { |
---|
484 | var $is_single = true; |
---|
485 | var $modifier; |
---|
486 | var $term; /* the actual word/phrase string*/ |
---|
487 | var $scope; |
---|
488 | |
---|
489 | var $scope_data; |
---|
490 | var $idx; |
---|
491 | |
---|
492 | function __construct($term, $modifier, $scope) |
---|
493 | { |
---|
494 | $this->term = $term; |
---|
495 | $this->modifier = $modifier; |
---|
496 | $this->scope = $scope; |
---|
497 | } |
---|
498 | |
---|
499 | function __toString() |
---|
500 | { |
---|
501 | $s = ''; |
---|
502 | if (isset($this->scope)) |
---|
503 | $s .= $this->scope->id .':'; |
---|
504 | if ($this->modifier & QST_WILDCARD_BEGIN) |
---|
505 | $s .= '*'; |
---|
506 | if ($this->modifier & QST_QUOTED) |
---|
507 | $s .= '"'; |
---|
508 | $s .= $this->term; |
---|
509 | if ($this->modifier & QST_QUOTED) |
---|
510 | $s .= '"'; |
---|
511 | if ($this->modifier & QST_WILDCARD_END) |
---|
512 | $s .= '*'; |
---|
513 | return $s; |
---|
514 | } |
---|
515 | } |
---|
516 | |
---|
517 | /** Represents an expression of several words or sub expressions to be searched.*/ |
---|
518 | class QMultiToken |
---|
519 | { |
---|
520 | var $is_single = false; |
---|
521 | var $modifier; |
---|
522 | var $tokens = array(); // the actual array of QSingleToken or QMultiToken |
---|
523 | |
---|
524 | function __toString() |
---|
525 | { |
---|
526 | $s = ''; |
---|
527 | for ($i=0; $i<count($this->tokens); $i++) |
---|
528 | { |
---|
529 | $modifier = $this->tokens[$i]->modifier; |
---|
530 | if ($i) |
---|
531 | $s .= ' '; |
---|
532 | if ($modifier & QST_OR) |
---|
533 | $s .= 'OR '; |
---|
534 | if ($modifier & QST_NOT) |
---|
535 | $s .= 'NOT '; |
---|
536 | if (! ($this->tokens[$i]->is_single) ) |
---|
537 | { |
---|
538 | $s .= '('; |
---|
539 | $s .= $this->tokens[$i]; |
---|
540 | $s .= ')'; |
---|
541 | } |
---|
542 | else |
---|
543 | { |
---|
544 | $s .= $this->tokens[$i]; |
---|
545 | } |
---|
546 | } |
---|
547 | return $s; |
---|
548 | } |
---|
549 | |
---|
550 | private function push(&$token, &$modifier, &$scope) |
---|
551 | { |
---|
552 | if (strlen($token) || (isset($scope) && $scope->nullable)) |
---|
553 | { |
---|
554 | $this->tokens[] = new QSingleToken($token, $modifier, $scope); |
---|
555 | } |
---|
556 | $token = ""; |
---|
557 | $modifier = 0; |
---|
558 | $scope = null; |
---|
559 | } |
---|
560 | |
---|
561 | /** |
---|
562 | * Parses the input query string by tokenizing the input, generating the modifiers (and/or/not/quotation/wildcards...). |
---|
563 | * Recursivity occurs when parsing () |
---|
564 | * @param string $q the actual query to be parsed |
---|
565 | * @param int $qi the character index in $q where to start parsing |
---|
566 | * @param int $level the depth from root in the tree (number of opened and unclosed opening brackets) |
---|
567 | */ |
---|
568 | protected function parse_expression($q, &$qi, $level, $root) |
---|
569 | { |
---|
570 | $crt_token = ""; |
---|
571 | $crt_modifier = 0; |
---|
572 | $crt_scope = null; |
---|
573 | |
---|
574 | for ($stop=false; !$stop && $qi<strlen($q); $qi++) |
---|
575 | { |
---|
576 | $ch = $q[$qi]; |
---|
577 | if ( ($crt_modifier&QST_QUOTED)==0) |
---|
578 | { |
---|
579 | switch ($ch) |
---|
580 | { |
---|
581 | case '(': |
---|
582 | if (strlen($crt_token)) |
---|
583 | $this->push($crt_token, $crt_modifier, $crt_scope); |
---|
584 | $sub = new QMultiToken; |
---|
585 | $qi++; |
---|
586 | $sub->parse_expression($q, $qi, $level+1, $root); |
---|
587 | $sub->modifier = $crt_modifier; |
---|
588 | if (isset($crt_scope) && $crt_scope->is_text) |
---|
589 | { |
---|
590 | $sub->apply_scope($crt_scope); // eg. 'tag:(John OR Bill)' |
---|
591 | } |
---|
592 | $this->tokens[] = $sub; |
---|
593 | $crt_modifier = 0; |
---|
594 | $crt_scope = null; |
---|
595 | break; |
---|
596 | case ')': |
---|
597 | if ($level>0) |
---|
598 | $stop = true; |
---|
599 | break; |
---|
600 | case ':': |
---|
601 | $scope = @$root->scopes[$crt_token]; |
---|
602 | if (!isset($scope) || isset($crt_scope)) |
---|
603 | { // white space |
---|
604 | $this->push($crt_token, $crt_modifier, $crt_scope); |
---|
605 | } |
---|
606 | else |
---|
607 | { |
---|
608 | $crt_token = ""; |
---|
609 | $crt_scope = $scope; |
---|
610 | } |
---|
611 | break; |
---|
612 | case '"': |
---|
613 | if (strlen($crt_token)) |
---|
614 | $this->push($crt_token, $crt_modifier, $crt_scope); |
---|
615 | $crt_modifier |= QST_QUOTED; |
---|
616 | break; |
---|
617 | case '-': |
---|
618 | if (strlen($crt_token) || isset($crt_scope)) |
---|
619 | $crt_token .= $ch; |
---|
620 | else |
---|
621 | $crt_modifier |= QST_NOT; |
---|
622 | break; |
---|
623 | case '*': |
---|
624 | if (strlen($crt_token)) |
---|
625 | $crt_token .= $ch; // wildcard end later |
---|
626 | else |
---|
627 | $crt_modifier |= QST_WILDCARD_BEGIN; |
---|
628 | break; |
---|
629 | case '.': |
---|
630 | if (isset($crt_scope) && !$crt_scope->is_text) |
---|
631 | { |
---|
632 | $crt_token .= $ch; |
---|
633 | break; |
---|
634 | } |
---|
635 | // else white space go on.. |
---|
636 | default: |
---|
637 | if (preg_match('/[\s,.;!\?]+/', $ch)) |
---|
638 | { // white space |
---|
639 | $this->push($crt_token, $crt_modifier, $crt_scope); |
---|
640 | } |
---|
641 | else |
---|
642 | $crt_token .= $ch; |
---|
643 | break; |
---|
644 | } |
---|
645 | } |
---|
646 | else |
---|
647 | {// quoted |
---|
648 | if ($ch=='"') |
---|
649 | { |
---|
650 | if ($qi+1 < strlen($q) && $q[$qi+1]=='*') |
---|
651 | { |
---|
652 | $crt_modifier |= QST_WILDCARD_END; |
---|
653 | $qi++; |
---|
654 | } |
---|
655 | $this->push($crt_token, $crt_modifier, $crt_scope); |
---|
656 | } |
---|
657 | else |
---|
658 | $crt_token .= $ch; |
---|
659 | } |
---|
660 | } |
---|
661 | |
---|
662 | $this->push($crt_token, $crt_modifier, $crt_scope); |
---|
663 | |
---|
664 | for ($i=0; $i<count($this->tokens); $i++) |
---|
665 | { |
---|
666 | $token = $this->tokens[$i]; |
---|
667 | $remove = false; |
---|
668 | if ($token->is_single) |
---|
669 | { |
---|
670 | if ( ($token->modifier & QST_QUOTED)==0 |
---|
671 | && substr($token->term, -1)=='*' ) |
---|
672 | { |
---|
673 | $token->term = rtrim($token->term, '*'); |
---|
674 | $token->modifier |= QST_WILDCARD_END; |
---|
675 | } |
---|
676 | |
---|
677 | if ( !isset($token->scope) |
---|
678 | && ($token->modifier & (QST_QUOTED|QST_WILDCARD))==0 ) |
---|
679 | { |
---|
680 | if ('not' == strtolower($token->term)) |
---|
681 | { |
---|
682 | if ($i+1 < count($this->tokens)) |
---|
683 | $this->tokens[$i+1]->modifier |= QST_NOT; |
---|
684 | $token->term = ""; |
---|
685 | } |
---|
686 | if ('or' == strtolower($token->term)) |
---|
687 | { |
---|
688 | if ($i+1 < count($this->tokens)) |
---|
689 | $this->tokens[$i+1]->modifier |= QST_OR; |
---|
690 | $token->term = ""; |
---|
691 | } |
---|
692 | if ('and' == strtolower($token->term)) |
---|
693 | { |
---|
694 | $token->term = ""; |
---|
695 | } |
---|
696 | } |
---|
697 | |
---|
698 | if (!strlen($token->term) |
---|
699 | && (!isset($token->scope) || !$token->scope->nullable) ) |
---|
700 | { |
---|
701 | $remove = true; |
---|
702 | } |
---|
703 | |
---|
704 | if ( isset($token->scope) |
---|
705 | && !$token->scope->parse($token)) |
---|
706 | $remove = true; |
---|
707 | } |
---|
708 | elseif (!count($token->tokens)) |
---|
709 | { |
---|
710 | $remove = true; |
---|
711 | } |
---|
712 | if ($remove) |
---|
713 | { |
---|
714 | array_splice($this->tokens, $i, 1); |
---|
715 | $i--; |
---|
716 | } |
---|
717 | } |
---|
718 | } |
---|
719 | |
---|
720 | private function apply_scope(QSearchScope $scope) |
---|
721 | { |
---|
722 | for ($i=0; $i<count($this->tokens); $i++) |
---|
723 | { |
---|
724 | if ($this->tokens[$i]->is_single) |
---|
725 | { |
---|
726 | if (!isset($this->tokens[$i]->scope)) |
---|
727 | $this->tokens[$i]->scope = $scope; |
---|
728 | } |
---|
729 | else |
---|
730 | $this->tokens[$i]->apply_scope($scope); |
---|
731 | } |
---|
732 | } |
---|
733 | |
---|
734 | private static function priority($modifier) |
---|
735 | { |
---|
736 | return $modifier & QST_OR ? 0 :1; |
---|
737 | } |
---|
738 | |
---|
739 | /* because evaluations occur left to right, we ensure that 'a OR b c d' is interpreted as 'a OR (b c d)'*/ |
---|
740 | protected function check_operator_priority() |
---|
741 | { |
---|
742 | for ($i=0; $i<count($this->tokens); $i++) |
---|
743 | { |
---|
744 | if (!$this->tokens[$i]->is_single) |
---|
745 | $this->tokens[$i]->check_operator_priority(); |
---|
746 | if ($i==1) |
---|
747 | $crt_prio = self::priority($this->tokens[$i]->modifier); |
---|
748 | if ($i<=1) |
---|
749 | continue; |
---|
750 | $prio = self::priority($this->tokens[$i]->modifier); |
---|
751 | if ($prio > $crt_prio) |
---|
752 | {// e.g. 'a OR b c d' i=2, operator(c)=AND -> prio(AND) > prio(OR) = operator(b) |
---|
753 | $term_count = 2; // at least b and c to be regrouped |
---|
754 | for ($j=$i+1; $j<count($this->tokens); $j++) |
---|
755 | { |
---|
756 | if (self::priority($this->tokens[$j]->modifier) >= $prio) |
---|
757 | $term_count++; // also take d |
---|
758 | else |
---|
759 | break; |
---|
760 | } |
---|
761 | |
---|
762 | $i--; // move pointer to b |
---|
763 | // crate sub expression (b c d) |
---|
764 | $sub = new QMultiToken; |
---|
765 | $sub->tokens = array_splice($this->tokens, $i, $term_count); |
---|
766 | |
---|
767 | // rewrite ourseleves as a (b c d) |
---|
768 | array_splice($this->tokens, $i, 0, array($sub)); |
---|
769 | $sub->modifier = $sub->tokens[0]->modifier & QST_OR; |
---|
770 | $sub->tokens[0]->modifier &= ~QST_OR; |
---|
771 | |
---|
772 | $sub->check_operator_priority(); |
---|
773 | } |
---|
774 | else |
---|
775 | $crt_prio = $prio; |
---|
776 | } |
---|
777 | } |
---|
778 | } |
---|
779 | |
---|
780 | class QExpression extends QMultiToken |
---|
781 | { |
---|
782 | var $scopes = array(); |
---|
783 | var $stokens = array(); |
---|
784 | var $stoken_modifiers = array(); |
---|
785 | |
---|
786 | function __construct($q, $scopes) |
---|
787 | { |
---|
788 | foreach ($scopes as $scope) |
---|
789 | { |
---|
790 | $this->scopes[$scope->id] = $scope; |
---|
791 | foreach ($scope->aliases as $alias) |
---|
792 | $this->scopes[strtolower($alias)] = $scope; |
---|
793 | } |
---|
794 | $i = 0; |
---|
795 | $this->parse_expression($q, $i, 0, $this); |
---|
796 | //manipulate the tree so that 'a OR b c' is the same as 'b c OR a' |
---|
797 | $this->check_operator_priority(); |
---|
798 | $this->build_single_tokens($this, 0); |
---|
799 | } |
---|
800 | |
---|
801 | private function build_single_tokens(QMultiToken $expr, $this_is_not) |
---|
802 | { |
---|
803 | for ($i=0; $i<count($expr->tokens); $i++) |
---|
804 | { |
---|
805 | $token = $expr->tokens[$i]; |
---|
806 | $crt_is_not = ($token->modifier ^ $this_is_not) & QST_NOT; // no negation OR double negation -> no negation; |
---|
807 | |
---|
808 | if ($token->is_single) |
---|
809 | { |
---|
810 | $token->idx = count($this->stokens); |
---|
811 | $this->stokens[] = $token; |
---|
812 | |
---|
813 | $modifier = $token->modifier; |
---|
814 | if ($crt_is_not) |
---|
815 | $modifier |= QST_NOT; |
---|
816 | else |
---|
817 | $modifier &= ~QST_NOT; |
---|
818 | $this->stoken_modifiers[] = $modifier; |
---|
819 | } |
---|
820 | else |
---|
821 | $this->build_single_tokens($token, $crt_is_not); |
---|
822 | } |
---|
823 | } |
---|
824 | } |
---|
825 | |
---|
826 | /** |
---|
827 | Structure of results being filled from different tables |
---|
828 | */ |
---|
829 | class QResults |
---|
830 | { |
---|
831 | var $all_tags; |
---|
832 | var $tag_ids; |
---|
833 | var $tag_iids; |
---|
834 | var $images_iids; |
---|
835 | var $iids; |
---|
836 | |
---|
837 | var $variants; |
---|
838 | } |
---|
839 | |
---|
840 | function qsearch_get_images(QExpression $expr, QResults $qsr) |
---|
841 | { |
---|
842 | $qsr->images_iids = array_fill(0, count($expr->stokens), array()); |
---|
843 | |
---|
844 | $inflector = null; |
---|
845 | $lang_code = substr(get_default_language(),0,2); |
---|
846 | include_once(PHPWG_ROOT_PATH.'include/inflectors/'.$lang_code.'.php'); |
---|
847 | $class_name = 'Inflector_'.$lang_code; |
---|
848 | if (class_exists($class_name)) |
---|
849 | { |
---|
850 | $inflector = new $class_name; |
---|
851 | } |
---|
852 | |
---|
853 | $query_base = 'SELECT id from '.IMAGES_TABLE.' i WHERE '; |
---|
854 | for ($i=0; $i<count($expr->stokens); $i++) |
---|
855 | { |
---|
856 | $token = $expr->stokens[$i]; |
---|
857 | $term = $token->term; |
---|
858 | $scope_id = isset($token->scope) ? $token->scope->id : 'photo'; |
---|
859 | $clauses = array(); |
---|
860 | |
---|
861 | $like = addslashes($term); |
---|
862 | $like = str_replace( array('%','_'), array('\\%','\\_'), $like); // escape LIKE specials %_ |
---|
863 | $file_like = 'CONVERT(file, CHAR) LIKE \'%'.$like.'%\''; |
---|
864 | |
---|
865 | switch ($scope_id) |
---|
866 | { |
---|
867 | case 'photo': |
---|
868 | $clauses[] = $file_like; |
---|
869 | |
---|
870 | if ($inflector!=null && strlen($term)>2 |
---|
871 | && ($expr->stoken_modifiers[$i] & (QST_QUOTED|QST_WILDCARD))==0 |
---|
872 | && strcspn($term, '\'0123456789') == strlen($term) |
---|
873 | ) |
---|
874 | { |
---|
875 | $variants = array_unique( array_diff( $inflector->get_variants($term), array($term) ) ); |
---|
876 | $qsr->variants[$term] = $variants; |
---|
877 | } |
---|
878 | else |
---|
879 | { |
---|
880 | $variants = array(); |
---|
881 | } |
---|
882 | |
---|
883 | if (strlen($term)>3) // default minimum full text index |
---|
884 | { |
---|
885 | $ft = $term; |
---|
886 | if ($expr->stoken_modifiers[$i] & QST_QUOTED) |
---|
887 | $ft = '"'.$ft.'"'; |
---|
888 | if ($expr->stoken_modifiers[$i] & QST_WILDCARD_END) |
---|
889 | $ft .= '*'; |
---|
890 | foreach ($variants as $variant) |
---|
891 | { |
---|
892 | $ft.=' '.$variant; |
---|
893 | } |
---|
894 | $clauses[] = 'MATCH(i.name, i.comment) AGAINST( \''.addslashes($ft).'\' IN BOOLEAN MODE)'; |
---|
895 | } |
---|
896 | else |
---|
897 | { |
---|
898 | foreach( array('i.name', 'i.comment') as $field) |
---|
899 | { |
---|
900 | $clauses[] = $field.' REGEXP \'[[:<:]]'.addslashes(preg_quote($term)).'[[:>:]]\''; |
---|
901 | } |
---|
902 | } |
---|
903 | break; |
---|
904 | |
---|
905 | case 'file': |
---|
906 | $clauses[] = $file_like; |
---|
907 | break; |
---|
908 | case 'width': |
---|
909 | case 'height': |
---|
910 | $clauses[] = $token->scope->get_sql($scope_id, $token); |
---|
911 | break; |
---|
912 | case 'ratio': |
---|
913 | $clauses[] = $token->scope->get_sql('width/height', $token); |
---|
914 | break; |
---|
915 | case 'size': |
---|
916 | $clauses[] = $token->scope->get_sql('width*height', $token); |
---|
917 | break; |
---|
918 | case 'hits': |
---|
919 | $clauses[] = $token->scope->get_sql('hit', $token); |
---|
920 | break; |
---|
921 | case 'score': |
---|
922 | $clauses[] = $token->scope->get_sql('rating_score', $token); |
---|
923 | break; |
---|
924 | case 'filesize': |
---|
925 | $clauses[] = $token->scope->get_sql('1024*filesize', $token); |
---|
926 | break; |
---|
927 | case 'created': |
---|
928 | $clauses[] = $token->scope->get_sql('date_creation', $token); |
---|
929 | break; |
---|
930 | case 'posted': |
---|
931 | $clauses[] = $token->scope->get_sql('date_available', $token); |
---|
932 | break; |
---|
933 | |
---|
934 | } |
---|
935 | if (!empty($clauses)) |
---|
936 | { |
---|
937 | $query = $query_base.'('.implode(' OR ', $clauses).')'; |
---|
938 | $qsr->images_iids[$i] = query2array($query,null,'id'); |
---|
939 | } |
---|
940 | } |
---|
941 | } |
---|
942 | |
---|
943 | function qsearch_get_tags(QExpression $expr, QResults $qsr) |
---|
944 | { |
---|
945 | $tokens = $expr->stokens; |
---|
946 | $token_modifiers = $expr->stoken_modifiers; |
---|
947 | |
---|
948 | $token_tag_ids = array_fill(0, count($tokens), array() ); |
---|
949 | $all_tags = array(); |
---|
950 | |
---|
951 | $token_tag_scores = $token_tag_ids; |
---|
952 | $transliterated_tokens = array(); |
---|
953 | foreach ($tokens as $token) |
---|
954 | { |
---|
955 | if (!isset($token->scope) || 'tag' == $token->scope->id) |
---|
956 | { |
---|
957 | $transliterated_tokens[] = transliterate($token->term); |
---|
958 | } |
---|
959 | else |
---|
960 | { |
---|
961 | $transliterated_tokens[] = ''; |
---|
962 | } |
---|
963 | } |
---|
964 | |
---|
965 | $query = ' |
---|
966 | SELECT t.*, COUNT(image_id) AS counter |
---|
967 | FROM '.TAGS_TABLE.' t |
---|
968 | INNER JOIN '.IMAGE_TAG_TABLE.' ON id=tag_id |
---|
969 | GROUP BY id'; |
---|
970 | $result = pwg_query($query); |
---|
971 | while ($tag = pwg_db_fetch_assoc($result)) |
---|
972 | { |
---|
973 | $transliterated_tag = transliterate($tag['name']); |
---|
974 | |
---|
975 | // find how this tag matches query tokens |
---|
976 | for ($i=0; $i<count($tokens); $i++) |
---|
977 | { |
---|
978 | $transliterated_token = $transliterated_tokens[$i]; |
---|
979 | if (strlen($transliterated_token)==0) |
---|
980 | continue; |
---|
981 | |
---|
982 | $match = false; |
---|
983 | $pos = 0; |
---|
984 | while ( ($pos = strpos($transliterated_tag, $transliterated_token, $pos)) !== false) |
---|
985 | { |
---|
986 | if ( ($token_modifiers[$i]&QST_WILDCARD)==QST_WILDCARD ) |
---|
987 | {// wildcard in this token |
---|
988 | $match = 1; |
---|
989 | break; |
---|
990 | } |
---|
991 | $token_len = strlen($transliterated_token); |
---|
992 | |
---|
993 | // search begin of word |
---|
994 | $wbegin_len=0; $wbegin_char=' '; |
---|
995 | while ($pos-$wbegin_len > 0) |
---|
996 | { |
---|
997 | if (! is_word_char($transliterated_tag[$pos-$wbegin_len-1]) ) |
---|
998 | { |
---|
999 | $wbegin_char = $transliterated_tag[$pos-$wbegin_len-1]; |
---|
1000 | break; |
---|
1001 | } |
---|
1002 | $wbegin_len++; |
---|
1003 | } |
---|
1004 | |
---|
1005 | // search end of word |
---|
1006 | $wend_len=0; $wend_char=' '; |
---|
1007 | while ($pos+$token_len+$wend_len < strlen($transliterated_tag)) |
---|
1008 | { |
---|
1009 | if (! is_word_char($transliterated_tag[$pos+$token_len+$wend_len]) ) |
---|
1010 | { |
---|
1011 | $wend_char = $transliterated_tag[$pos+$token_len+$wend_len]; |
---|
1012 | break; |
---|
1013 | } |
---|
1014 | $wend_len++; |
---|
1015 | } |
---|
1016 | |
---|
1017 | $this_score = 0; |
---|
1018 | if ( ($token_modifiers[$i]&QST_WILDCARD)==0 ) |
---|
1019 | {// no wildcard begin or end |
---|
1020 | if ($token_len <= 2) |
---|
1021 | {// search for 1 or 2 characters must match exactly to avoid retrieving too much data |
---|
1022 | if ($wbegin_len==0 && $wend_len==0 && !is_odd_wbreak_begin($wbegin_char) && !is_odd_wbreak_end($wend_char) ) |
---|
1023 | $this_score = 1; |
---|
1024 | } |
---|
1025 | elseif ($token_len == 3) |
---|
1026 | { |
---|
1027 | if ($wbegin_len==0) |
---|
1028 | $this_score = $token_len / ($token_len + $wend_len); |
---|
1029 | } |
---|
1030 | else |
---|
1031 | { |
---|
1032 | $this_score = $token_len / ($token_len + 1.1 * $wbegin_len + 0.9 * $wend_len); |
---|
1033 | } |
---|
1034 | } |
---|
1035 | |
---|
1036 | if ($this_score>0) |
---|
1037 | $match = max($match, $this_score ); |
---|
1038 | $pos++; |
---|
1039 | } |
---|
1040 | |
---|
1041 | if ($match) |
---|
1042 | { |
---|
1043 | $tag_id = (int)$tag['id']; |
---|
1044 | $all_tags[$tag_id] = $tag; |
---|
1045 | $token_tag_ids[$i][] = $tag_id; |
---|
1046 | $token_tag_scores[$i][] = $match; |
---|
1047 | } |
---|
1048 | } |
---|
1049 | } |
---|
1050 | |
---|
1051 | // process tags |
---|
1052 | $not_tag_ids = array(); |
---|
1053 | for ($i=0; $i<count($tokens); $i++) |
---|
1054 | { |
---|
1055 | array_multisort($token_tag_scores[$i], SORT_DESC|SORT_NUMERIC, $token_tag_ids[$i]); |
---|
1056 | $is_not = $token_modifiers[$i]&QST_NOT; |
---|
1057 | $counter = 0; |
---|
1058 | |
---|
1059 | for ($j=0; $j<count($token_tag_scores[$i]); $j++) |
---|
1060 | { |
---|
1061 | if ($is_not) |
---|
1062 | { |
---|
1063 | if ($token_tag_scores[$i][$j] < 0.8 || |
---|
1064 | ($j>0 && $token_tag_scores[$i][$j] < $token_tag_scores[$i][0]) ) |
---|
1065 | { |
---|
1066 | array_splice($token_tag_scores[$i], $j); |
---|
1067 | array_splice($token_tag_ids[$i], $j); |
---|
1068 | } |
---|
1069 | } |
---|
1070 | else |
---|
1071 | { |
---|
1072 | $tag_id = $token_tag_ids[$i][$j]; |
---|
1073 | $counter += $all_tags[$tag_id]['counter']; |
---|
1074 | if ( $j>0 && ( |
---|
1075 | ($counter > 100 && $token_tag_scores[$i][0] > $token_tag_scores[$i][$j]) // "many" images in previous tags and starting from this tag is less relevant |
---|
1076 | || ($token_tag_scores[$i][0]==1 && $token_tag_scores[$i][$j]<0.8) |
---|
1077 | || ($token_tag_scores[$i][0]>0.8 && $token_tag_scores[$i][$j]<0.5) |
---|
1078 | )) |
---|
1079 | {// we remove this tag from the results, but we still leave it in all_tags list so that if we are wrong, the user chooses it |
---|
1080 | array_splice($token_tag_ids[$i], $j); |
---|
1081 | array_splice($token_tag_scores[$i], $j); |
---|
1082 | break; |
---|
1083 | } |
---|
1084 | } |
---|
1085 | } |
---|
1086 | |
---|
1087 | if ($is_not) |
---|
1088 | { |
---|
1089 | $not_tag_ids = array_merge($not_tag_ids, $token_tag_ids[$i]); |
---|
1090 | } |
---|
1091 | } |
---|
1092 | |
---|
1093 | $all_tags = array_diff_key($all_tags, array_flip($not_tag_ids)); |
---|
1094 | usort($all_tags, 'tag_alpha_compare'); |
---|
1095 | foreach ( $all_tags as &$tag ) |
---|
1096 | { |
---|
1097 | $tag['name'] = trigger_event('render_tag_name', $tag['name'], $tag); |
---|
1098 | } |
---|
1099 | $qsr->all_tags = $all_tags; |
---|
1100 | |
---|
1101 | $qsr->tag_ids = $token_tag_ids; |
---|
1102 | $qsr->tag_iids = array_fill(0, count($tokens), array() ); |
---|
1103 | |
---|
1104 | for ($i=0; $i<count($tokens); $i++) |
---|
1105 | { |
---|
1106 | $tag_ids = $token_tag_ids[$i]; |
---|
1107 | |
---|
1108 | if (!empty($tag_ids)) |
---|
1109 | { |
---|
1110 | $query = ' |
---|
1111 | SELECT image_id FROM '.IMAGE_TAG_TABLE.' |
---|
1112 | WHERE tag_id IN ('.implode(',',$tag_ids).') |
---|
1113 | GROUP BY image_id'; |
---|
1114 | $qsr->tag_iids[$i] = query2array($query, null, 'image_id'); |
---|
1115 | } |
---|
1116 | elseif (isset($tokens[$i]->scope) && 'tag' == $tokens[$i]->scope->id && strlen($token->term)==0) |
---|
1117 | { |
---|
1118 | if ($tokens[$i]->modifier & QST_WILDCARD) |
---|
1119 | {// eg. 'tag:*' returns all tagged images |
---|
1120 | $qsr->tag_iids[$i] = query2array('SELECT DISTINCT image_id FROM '.IMAGE_TAG_TABLE, null, 'image_id'); |
---|
1121 | } |
---|
1122 | else |
---|
1123 | {// eg. 'tag:' returns all untagged images |
---|
1124 | $qsr->tag_iids[$i] = query2array('SELECT id FROM '.IMAGES_TABLE.' LEFT JOIN '.IMAGE_TAG_TABLE.' ON id=image_id WHERE image_id IS NULL', null, 'id'); |
---|
1125 | } |
---|
1126 | } |
---|
1127 | } |
---|
1128 | } |
---|
1129 | |
---|
1130 | |
---|
1131 | function qsearch_eval(QMultiToken $expr, QResults $qsr, &$qualifies, &$ignored_terms) |
---|
1132 | { |
---|
1133 | $qualifies = false; // until we find at least one positive term |
---|
1134 | $ignored_terms = array(); |
---|
1135 | |
---|
1136 | $ids = $not_ids = array(); |
---|
1137 | |
---|
1138 | for ($i=0; $i<count($expr->tokens); $i++) |
---|
1139 | { |
---|
1140 | $crt = $expr->tokens[$i]; |
---|
1141 | if ($crt->is_single) |
---|
1142 | { |
---|
1143 | $crt_ids = $qsr->iids[$crt->idx] = array_unique( array_merge($qsr->images_iids[$crt->idx], $qsr->tag_iids[$crt->idx]) ); |
---|
1144 | $crt_qualifies = count($crt_ids)>0 || count($qsr->tag_ids[$crt->idx])>0; |
---|
1145 | $crt_ignored_terms = $crt_qualifies ? array() : array($crt->term); |
---|
1146 | } |
---|
1147 | else |
---|
1148 | $crt_ids = qsearch_eval($crt, $qsr, $crt_qualifies, $crt_ignored_terms); |
---|
1149 | |
---|
1150 | $modifier = $crt->modifier; |
---|
1151 | if ($modifier & QST_NOT) |
---|
1152 | $not_ids = array_unique( array_merge($not_ids, $crt_ids)); |
---|
1153 | else |
---|
1154 | { |
---|
1155 | $ignored_terms = array_merge($ignored_terms, $crt_ignored_terms); |
---|
1156 | if ($modifier & QST_OR) |
---|
1157 | { |
---|
1158 | $ids = array_unique( array_merge($ids, $crt_ids) ); |
---|
1159 | $qualifies |= $crt_qualifies; |
---|
1160 | } |
---|
1161 | elseif ($crt_qualifies) |
---|
1162 | { |
---|
1163 | if ($qualifies) |
---|
1164 | $ids = array_intersect($ids, $crt_ids); |
---|
1165 | else |
---|
1166 | $ids = $crt_ids; |
---|
1167 | $qualifies = true; |
---|
1168 | } |
---|
1169 | } |
---|
1170 | } |
---|
1171 | |
---|
1172 | if (count($not_ids)) |
---|
1173 | $ids = array_diff($ids, $not_ids); |
---|
1174 | return $ids; |
---|
1175 | } |
---|
1176 | |
---|
1177 | /** |
---|
1178 | * Returns the search results corresponding to a quick/query search. |
---|
1179 | * A quick/query search returns many items (search is not strict), but results |
---|
1180 | * are sorted by relevance unless $super_order_by is true. Returns: |
---|
1181 | * array ( |
---|
1182 | * 'items' => array of matching images |
---|
1183 | * 'qs' => array( |
---|
1184 | * 'unmatched_terms' => array of terms from the input string that were not matched |
---|
1185 | * 'matching_tags' => array of matching tags |
---|
1186 | * 'matching_cats' => array of matching categories |
---|
1187 | * 'matching_cats_no_images' =>array(99) - matching categories without images |
---|
1188 | * ) |
---|
1189 | * ) |
---|
1190 | * |
---|
1191 | * @param string $q |
---|
1192 | * @param bool $super_order_by |
---|
1193 | * @param string $images_where optional additional restriction on images table |
---|
1194 | * @return array |
---|
1195 | */ |
---|
1196 | function get_quick_search_results($q, $options) |
---|
1197 | { |
---|
1198 | global $conf; |
---|
1199 | //@TODO: maybe cache for 10 minutes the result set to avoid many expensive sql calls when navigating the pictures |
---|
1200 | $q = trim(stripslashes($q)); |
---|
1201 | $search_results = |
---|
1202 | array( |
---|
1203 | 'items' => array(), |
---|
1204 | 'qs' => array('q'=>$q), |
---|
1205 | ); |
---|
1206 | |
---|
1207 | $scopes = array(); |
---|
1208 | $scopes[] = new QSearchScope('tag', array('tags')); |
---|
1209 | $scopes[] = new QSearchScope('photo', array('photos')); |
---|
1210 | $scopes[] = new QSearchScope('file', array('filename')); |
---|
1211 | $scopes[] = new QNumericRangeScope('width', array()); |
---|
1212 | $scopes[] = new QNumericRangeScope('height', array()); |
---|
1213 | $scopes[] = new QNumericRangeScope('ratio', array(), false, 0.001); |
---|
1214 | $scopes[] = new QNumericRangeScope('size', array()); |
---|
1215 | $scopes[] = new QNumericRangeScope('filesize', array()); |
---|
1216 | $scopes[] = new QNumericRangeScope('hits', array('hit', 'visit', 'visits')); |
---|
1217 | $scopes[] = new QNumericRangeScope('score', array('rating'), true); |
---|
1218 | |
---|
1219 | $createdDateAliases = array('taken'); |
---|
1220 | $postedDateAliases = array('added'); |
---|
1221 | if ($conf['calendar_datefield'] == 'date_creation') |
---|
1222 | $createdDateAliases[] = 'date'; |
---|
1223 | else |
---|
1224 | $postedDateAliases[] = 'date'; |
---|
1225 | $scopes[] = new QDateRangeScope('created', $createdDateAliases, true); |
---|
1226 | $scopes[] = new QDateRangeScope('posted', $postedDateAliases); |
---|
1227 | |
---|
1228 | $expression = new QExpression($q, $scopes); |
---|
1229 | //var_export($expression); |
---|
1230 | |
---|
1231 | $qsr = new QResults; |
---|
1232 | qsearch_get_tags($expression, $qsr); |
---|
1233 | qsearch_get_images($expression, $qsr); |
---|
1234 | //var_export($qsr->all_tags); |
---|
1235 | |
---|
1236 | $ids = qsearch_eval($expression, $qsr, $tmp, $search_results['qs']['unmatched_terms']); |
---|
1237 | |
---|
1238 | $debug[] = "<!--\nparsed: ".$expression; |
---|
1239 | $debug[] = count($expression->stokens).' tokens'; |
---|
1240 | for ($i=0; $i<count($expression->stokens); $i++) |
---|
1241 | { |
---|
1242 | $debug[] = $expression->stokens[$i].': '.count($qsr->tag_ids[$i]).' tags, '.count($qsr->tag_iids[$i]).' tiids, '.count($qsr->images_iids[$i]).' iiids, '.count($qsr->iids[$i]).' iids' |
---|
1243 | .( !empty($qsr->variants[$expression->stokens[$i]->term]) ? ' variants: '.implode(', ',$qsr->variants[$expression->stokens[$i]->term]): ''); |
---|
1244 | } |
---|
1245 | $debug[] = 'before perms '.count($ids); |
---|
1246 | |
---|
1247 | $search_results['qs']['matching_tags'] = $qsr->all_tags; |
---|
1248 | global $template; |
---|
1249 | |
---|
1250 | if (empty($ids)) |
---|
1251 | { |
---|
1252 | $debug[] = '-->'; |
---|
1253 | $template->append('footer_elements', implode("\n", $debug) ); |
---|
1254 | return $search_results; |
---|
1255 | } |
---|
1256 | |
---|
1257 | $permissions = !isset($options['permissions']) ? true : $options['permissions']; |
---|
1258 | |
---|
1259 | $where_clauses = array(); |
---|
1260 | $where_clauses[]='i.id IN ('. implode(',', $ids) . ')'; |
---|
1261 | if (!empty($options['images_where'])) |
---|
1262 | { |
---|
1263 | $where_clauses[]='('.$images_where.')'; |
---|
1264 | } |
---|
1265 | if ($permissions) |
---|
1266 | { |
---|
1267 | $where_clauses[] = get_sql_condition_FandF( |
---|
1268 | array |
---|
1269 | ( |
---|
1270 | 'forbidden_categories' => 'category_id', |
---|
1271 | 'visible_categories' => 'category_id', |
---|
1272 | 'visible_images' => 'i.id' |
---|
1273 | ), |
---|
1274 | null,true |
---|
1275 | ); |
---|
1276 | } |
---|
1277 | |
---|
1278 | $query = ' |
---|
1279 | SELECT DISTINCT(id) FROM '.IMAGES_TABLE.' i'; |
---|
1280 | if ($permissions) |
---|
1281 | { |
---|
1282 | $query .= ' |
---|
1283 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id'; |
---|
1284 | } |
---|
1285 | $query .= ' |
---|
1286 | WHERE '.implode("\n AND ", $where_clauses)."\n". |
---|
1287 | $conf['order_by']; |
---|
1288 | |
---|
1289 | $ids = query2array($query, null, 'id'); |
---|
1290 | |
---|
1291 | $debug[] = count($ids).' final photo count -->'; |
---|
1292 | $template->append('footer_elements', implode("\n", $debug) ); |
---|
1293 | |
---|
1294 | $search_results['items'] = $ids; |
---|
1295 | return $search_results; |
---|
1296 | } |
---|
1297 | |
---|
1298 | /** |
---|
1299 | * Returns an array of 'items' corresponding to the search id. |
---|
1300 | * It can be either a quick search or a regular search. |
---|
1301 | * |
---|
1302 | * @param int $search_id |
---|
1303 | * @param bool $super_order_by |
---|
1304 | * @param string $images_where optional aditional restriction on images table |
---|
1305 | * @return array |
---|
1306 | */ |
---|
1307 | function get_search_results($search_id, $super_order_by, $images_where='') |
---|
1308 | { |
---|
1309 | $search = get_search_array($search_id); |
---|
1310 | if ( !isset($search['q']) ) |
---|
1311 | { |
---|
1312 | $result['items'] = get_regular_search_results($search, $images_where); |
---|
1313 | return $result; |
---|
1314 | } |
---|
1315 | else |
---|
1316 | { |
---|
1317 | return get_quick_search_results($search['q'], array('super_order_by'=>$super_order_by, 'images_where'=>$images_where) ); |
---|
1318 | } |
---|
1319 | } |
---|
1320 | |
---|
1321 | ?> |
---|