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