[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 | |
---|
[25658] | 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 | */ |
---|
[10340] | 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 | |
---|
[25658] | 269 | /** |
---|
| 270 | * Finds if a char is a special token for word start: [{<=*+ |
---|
| 271 | * |
---|
| 272 | * @param char $ch |
---|
| 273 | * @return bool |
---|
| 274 | */ |
---|
[18207] | 275 | function is_odd_wbreak_begin($ch) |
---|
| 276 | { |
---|
| 277 | return strpos('[{<=*+', $ch)===false ? false:true; |
---|
| 278 | } |
---|
| 279 | |
---|
[25658] | 280 | /** |
---|
| 281 | * Finds if a char is a special token for word end: ]}>=*+ |
---|
| 282 | * |
---|
| 283 | * @param char $ch |
---|
| 284 | * @return bool |
---|
| 285 | */ |
---|
[18207] | 286 | function is_odd_wbreak_end($ch) |
---|
| 287 | { |
---|
| 288 | return strpos(']}>=*+', $ch)===false ? false:true; |
---|
| 289 | } |
---|
| 290 | |
---|
[25658] | 291 | |
---|
| 292 | define('QST_QUOTED', 0x01); |
---|
| 293 | define('QST_NOT', 0x02); |
---|
[27868] | 294 | define('QST_OR', 0x04); |
---|
| 295 | define('QST_WILDCARD_BEGIN', 0x08); |
---|
| 296 | define('QST_WILDCARD_END', 0x10); |
---|
[18207] | 297 | define('QST_WILDCARD', QST_WILDCARD_BEGIN|QST_WILDCARD_END); |
---|
| 298 | |
---|
[1619] | 299 | /** |
---|
[25658] | 300 | * Analyzes and splits the quick/query search query $q into tokens. |
---|
[10340] | 301 | * q='john bill' => 2 tokens 'john' 'bill' |
---|
| 302 | * Special characters for MySql full text search (+,<,>,~) appear in the token modifiers. |
---|
| 303 | * The query can contain a phrase: 'Pierre "New York"' will return 'pierre' qnd 'new york'. |
---|
[25658] | 304 | * |
---|
| 305 | * @param string $q |
---|
[1619] | 306 | */ |
---|
[27868] | 307 | |
---|
[27882] | 308 | /** Represents a single word or quoted phrase to be searched.*/ |
---|
[27868] | 309 | class QSingleToken |
---|
[1537] | 310 | { |
---|
[27868] | 311 | var $is_single = true; |
---|
[27882] | 312 | var $token; /* the actual word/phrase string*/ |
---|
[27868] | 313 | var $idx; |
---|
[2135] | 314 | |
---|
[27868] | 315 | function __construct($token) |
---|
[1537] | 316 | { |
---|
[27868] | 317 | $this->token = $token; |
---|
| 318 | } |
---|
| 319 | } |
---|
| 320 | |
---|
[27882] | 321 | /** Represents an expression of several words or sub expressions to be searched.*/ |
---|
[27868] | 322 | class QMultiToken |
---|
| 323 | { |
---|
| 324 | var $is_single = false; |
---|
[27882] | 325 | var $tokens = array(); // the actual array of QSingleToken or QMultiToken |
---|
| 326 | var $token_modifiers = array(); // modifiers (OR,NOT,...) for every token |
---|
[27868] | 327 | |
---|
| 328 | function __toString() |
---|
| 329 | { |
---|
| 330 | $s = ''; |
---|
| 331 | for ($i=0; $i<count($this->tokens); $i++) |
---|
[1537] | 332 | { |
---|
[27868] | 333 | $modifier = $this->token_modifiers[$i]; |
---|
| 334 | if ($i) |
---|
| 335 | $s .= ' '; |
---|
| 336 | if ($modifier & QST_OR) |
---|
| 337 | $s .= 'OR '; |
---|
| 338 | if ($modifier & QST_NOT) |
---|
| 339 | $s .= 'NOT '; |
---|
| 340 | if ($modifier & QST_WILDCARD_BEGIN) |
---|
| 341 | $s .= '*'; |
---|
| 342 | if ($modifier & QST_QUOTED) |
---|
| 343 | $s .= '"'; |
---|
| 344 | if (! ($this->tokens[$i]->is_single) ) |
---|
| 345 | { |
---|
| 346 | $s .= '('; |
---|
| 347 | $s .= $this->tokens[$i]; |
---|
| 348 | $s .= ')'; |
---|
| 349 | } |
---|
| 350 | else |
---|
| 351 | { |
---|
| 352 | $s .= $this->tokens[$i]->token; |
---|
| 353 | } |
---|
| 354 | if ($modifier & QST_QUOTED) |
---|
| 355 | $s .= '"'; |
---|
| 356 | if ($modifier & QST_WILDCARD_END) |
---|
| 357 | $s .= '*'; |
---|
| 358 | |
---|
| 359 | } |
---|
| 360 | return $s; |
---|
| 361 | } |
---|
| 362 | |
---|
[27882] | 363 | private function push(&$token, &$modifier) |
---|
[27868] | 364 | { |
---|
| 365 | $this->tokens[] = new QSingleToken($token); |
---|
| 366 | $this->token_modifiers[] = $modifier; |
---|
| 367 | $token = ""; |
---|
| 368 | $modifier = 0; |
---|
| 369 | } |
---|
| 370 | |
---|
[27882] | 371 | /** |
---|
| 372 | * Parses the input query string by tokenizing the input, generating the modifiers (and/or/not/quotation/wildcards...). |
---|
| 373 | * Recursivity occurs when parsing () |
---|
| 374 | * @param string $q the actual query to be parsed |
---|
| 375 | * @param int $qi the character index in $q where to start parsing |
---|
| 376 | * @param int $level the depth from root in the tree (number of opened and unclosed opening brackets) |
---|
| 377 | */ |
---|
[27868] | 378 | protected function parse_expression($q, &$qi, $level) |
---|
| 379 | { |
---|
| 380 | $crt_token = ""; |
---|
| 381 | $crt_modifier = 0; |
---|
| 382 | |
---|
| 383 | for ($stop=false; !$stop && $qi<strlen($q); $qi++) |
---|
| 384 | { |
---|
| 385 | $ch = $q[$qi]; |
---|
| 386 | if ( ($crt_modifier&QST_QUOTED)==0) |
---|
| 387 | { |
---|
| 388 | switch ($ch) |
---|
| 389 | { |
---|
| 390 | case '(': |
---|
| 391 | if (strlen($crt_token)) |
---|
| 392 | $this->push($crt_token, $crt_modifier); |
---|
| 393 | $sub = new QMultiToken; |
---|
| 394 | $qi++; |
---|
| 395 | $sub->parse_expression($q, $qi, $level+1); |
---|
| 396 | $this->tokens[] = $sub; |
---|
| 397 | $this->token_modifiers[] = $crt_modifier; |
---|
| 398 | $crt_modifier = 0; |
---|
| 399 | break; |
---|
| 400 | case ')': |
---|
| 401 | if ($level>0) |
---|
| 402 | $stop = true; |
---|
| 403 | break; |
---|
| 404 | case '"': |
---|
| 405 | if (strlen($crt_token)) |
---|
| 406 | $this->push($crt_token, $crt_modifier); |
---|
| 407 | $crt_modifier |= QST_QUOTED; |
---|
| 408 | break; |
---|
| 409 | case '-': |
---|
| 410 | if (strlen($crt_token)) |
---|
| 411 | $crt_token .= $ch; |
---|
| 412 | else |
---|
| 413 | $crt_modifier |= QST_NOT; |
---|
| 414 | break; |
---|
| 415 | case '*': |
---|
| 416 | if (strlen($crt_token)) |
---|
| 417 | $crt_token .= $ch; // wildcard end later |
---|
| 418 | else |
---|
| 419 | $crt_modifier |= QST_WILDCARD_BEGIN; |
---|
| 420 | break; |
---|
| 421 | default: |
---|
| 422 | if (preg_match('/[\s,.;!\?]+/', $ch)) |
---|
| 423 | { // white space |
---|
| 424 | if (strlen($crt_token)) |
---|
| 425 | $this->push($crt_token, $crt_modifier); |
---|
| 426 | $crt_modifier = 0; |
---|
| 427 | } |
---|
| 428 | else |
---|
| 429 | $crt_token .= $ch; |
---|
| 430 | break; |
---|
| 431 | } |
---|
| 432 | } |
---|
| 433 | else |
---|
| 434 | {// quoted |
---|
[2135] | 435 | if ($ch=='"') |
---|
| 436 | { |
---|
[27868] | 437 | if ($qi+1 < strlen($q) && $q[$qi+1]=='*') |
---|
[18207] | 438 | { |
---|
[27868] | 439 | $crt_modifier |= QST_WILDCARD_END; |
---|
| 440 | $ai++; |
---|
[18207] | 441 | } |
---|
[27868] | 442 | $this->push($crt_token, $crt_modifier); |
---|
[2135] | 443 | } |
---|
[27868] | 444 | else |
---|
| 445 | $crt_token .= $ch; |
---|
| 446 | } |
---|
| 447 | } |
---|
| 448 | |
---|
| 449 | if (strlen($crt_token)) |
---|
| 450 | $this->push($crt_token, $crt_modifier); |
---|
| 451 | |
---|
| 452 | for ($i=0; $i<count($this->tokens); $i++) |
---|
| 453 | { |
---|
| 454 | $token = $this->tokens[$i]; |
---|
| 455 | $remove = false; |
---|
| 456 | if ($token->is_single) |
---|
| 457 | { |
---|
| 458 | if ( ($this->token_modifiers[$i]&QST_QUOTED)==0 ) |
---|
| 459 | { |
---|
| 460 | if ('not' == strtolower($token->token)) |
---|
[10340] | 461 | { |
---|
[27868] | 462 | if ($i+1 < count($this->tokens)) |
---|
| 463 | $this->token_modifiers[$i+1] |= QST_NOT; |
---|
| 464 | $token->token = ""; |
---|
[10340] | 465 | } |
---|
[27868] | 466 | if ('or' == strtolower($token->token)) |
---|
[10340] | 467 | { |
---|
[27868] | 468 | if ($i+1 < count($this->tokens)) |
---|
| 469 | $this->token_modifiers[$i+1] |= QST_OR; |
---|
| 470 | $token->token = ""; |
---|
[10340] | 471 | } |
---|
[27868] | 472 | if ('and' == strtolower($token->token)) |
---|
[2135] | 473 | { |
---|
[27868] | 474 | $token->token = ""; |
---|
[2135] | 475 | } |
---|
[27868] | 476 | if ( substr($token->token, -1)=='*' ) |
---|
| 477 | { |
---|
| 478 | $token->token = rtrim($token->token, '*'); |
---|
| 479 | $this->token_modifiers[$i] |= QST_WILDCARD_END; |
---|
| 480 | } |
---|
[2135] | 481 | } |
---|
[27868] | 482 | if (!strlen($token->token)) |
---|
| 483 | $remove = true; |
---|
| 484 | } |
---|
| 485 | else |
---|
[18207] | 486 | { |
---|
[27868] | 487 | if (!count($token->tokens)) |
---|
| 488 | $remove = true; |
---|
[18207] | 489 | } |
---|
[27868] | 490 | if ($remove) |
---|
| 491 | { |
---|
| 492 | array_splice($this->tokens, $i, 1); |
---|
| 493 | array_splice($this->token_modifiers, $i, 1); |
---|
| 494 | $i--; |
---|
| 495 | } |
---|
[1537] | 496 | } |
---|
| 497 | } |
---|
[27882] | 498 | |
---|
| 499 | private static function priority($modifier) |
---|
| 500 | { |
---|
| 501 | return $modifier & QST_OR ? 0 :1; |
---|
| 502 | } |
---|
| 503 | |
---|
| 504 | /* because evaluations occur left to right, we ensure that 'a OR b c d' is interpreted as 'a OR (b c d)'*/ |
---|
| 505 | protected function check_operator_priority() |
---|
| 506 | { |
---|
| 507 | for ($i=0; $i<count($this->tokens); $i++) |
---|
| 508 | { |
---|
| 509 | if (!$this->tokens[$i]->is_single) |
---|
| 510 | $this->tokens[$i]->check_operator_priority(); |
---|
| 511 | if ($i==1) |
---|
| 512 | $crt_prio = self::priority($this->token_modifiers[$i]); |
---|
| 513 | if ($i<=1) |
---|
| 514 | continue; |
---|
| 515 | $prio = self::priority($this->token_modifiers[$i]); |
---|
| 516 | if ($prio > $crt_prio) |
---|
| 517 | {// e.g. 'a OR b c d' i=2, operator(c)=AND -> prio(AND) > prio(OR) = operator(b) |
---|
| 518 | $term_count = 2; // at least b and c to be regrouped |
---|
| 519 | for ($j=$i+1; $j<count($this->tokens); $j++) |
---|
| 520 | { |
---|
| 521 | if (self::priority($this->token_modifiers[$j]) >= $prio) |
---|
| 522 | $term_count++; // also take d |
---|
| 523 | else |
---|
| 524 | break; |
---|
| 525 | } |
---|
| 526 | |
---|
| 527 | $i--; // move pointer to b |
---|
| 528 | // crate sub expression (b c d) |
---|
| 529 | $sub = new QMultiToken; |
---|
| 530 | $sub->tokens = array_splice($this->tokens, $i, $term_count); |
---|
| 531 | $sub->token_modifiers = array_splice($this->token_modifiers, $i, $term_count); |
---|
| 532 | |
---|
| 533 | // rewrite ourseleves as a (b c d) |
---|
| 534 | array_splice($this->tokens, $i, 0, array($sub)); |
---|
| 535 | array_splice($this->token_modifiers, $i, 0, array($sub->token_modifiers[0]&QST_OR)); |
---|
| 536 | $sub->token_modifiers[0] &= ~QST_OR; |
---|
| 537 | |
---|
| 538 | $sub->check_operator_priority(); |
---|
| 539 | } |
---|
| 540 | else |
---|
| 541 | $crt_prio = $prio; |
---|
| 542 | } |
---|
| 543 | } |
---|
[27868] | 544 | } |
---|
[18636] | 545 | |
---|
[27868] | 546 | class QExpression extends QMultiToken |
---|
| 547 | { |
---|
| 548 | var $stokens = array(); |
---|
| 549 | var $stoken_modifiers = array(); |
---|
| 550 | |
---|
| 551 | function __construct($q) |
---|
[2135] | 552 | { |
---|
[27868] | 553 | $i = 0; |
---|
| 554 | $this->parse_expression($q, $i, 0); |
---|
[27882] | 555 | //manipulate the tree so that 'a OR b c' is the same as 'b c OR a' |
---|
| 556 | $this->check_operator_priority(); |
---|
| 557 | $this->build_single_tokens($this, 0); |
---|
[2135] | 558 | } |
---|
[1537] | 559 | |
---|
[27882] | 560 | private function build_single_tokens(QMultiToken $expr, $this_is_not) |
---|
[10340] | 561 | { |
---|
[27868] | 562 | for ($i=0; $i<count($expr->tokens); $i++) |
---|
[10340] | 563 | { |
---|
[27868] | 564 | $token = $expr->tokens[$i]; |
---|
[27882] | 565 | $crt_is_not = ($expr->token_modifiers[$i] ^ $this_is_not) & QST_NOT; // no negation OR double negation -> no negation; |
---|
| 566 | |
---|
[27868] | 567 | if ($token->is_single) |
---|
[10340] | 568 | { |
---|
[27868] | 569 | $token->idx = count($this->stokens); |
---|
| 570 | $this->stokens[] = $token->token; |
---|
[27882] | 571 | |
---|
| 572 | $modifier = $expr->token_modifiers[$i]; |
---|
| 573 | if ($crt_is_not) |
---|
| 574 | $modifier |= QST_NOT; |
---|
| 575 | else |
---|
| 576 | $modifier &= ~QST_NOT; |
---|
| 577 | $this->stoken_modifiers[] = $modifier; |
---|
[10340] | 578 | } |
---|
[27868] | 579 | else |
---|
[27882] | 580 | $this->build_single_tokens($token, $crt_is_not); |
---|
[10340] | 581 | } |
---|
| 582 | } |
---|
| 583 | } |
---|
| 584 | |
---|
[27882] | 585 | /** |
---|
| 586 | Structure of results being filled from different tables |
---|
| 587 | */ |
---|
[27868] | 588 | class QResults |
---|
[10340] | 589 | { |
---|
[27868] | 590 | var $all_tags; |
---|
| 591 | var $tag_ids; |
---|
| 592 | var $tag_iids; |
---|
| 593 | var $images_iids; |
---|
| 594 | var $iids; |
---|
[27884] | 595 | |
---|
| 596 | var $variants; |
---|
[27868] | 597 | } |
---|
| 598 | |
---|
| 599 | function qsearch_get_images(QExpression $expr, QResults $qsr) |
---|
| 600 | { |
---|
| 601 | //@TODO: inflections for english / french |
---|
| 602 | $qsr->images_iids = array_fill(0, count($expr->tokens), array()); |
---|
[27884] | 603 | |
---|
| 604 | $inflector = null; |
---|
| 605 | $lang_code = substr(get_default_language(),0,2); |
---|
| 606 | include_once(PHPWG_ROOT_PATH.'include/inflectors/'.$lang_code.'.php'); |
---|
| 607 | $class_name = 'Inflector_'.$lang_code; |
---|
| 608 | if (class_exists($class_name)) |
---|
| 609 | { |
---|
| 610 | $inflector = new $class_name; |
---|
| 611 | } |
---|
| 612 | |
---|
[27868] | 613 | $query_base = 'SELECT id from '.IMAGES_TABLE.' i WHERE '; |
---|
| 614 | for ($i=0; $i<count($expr->stokens); $i++) |
---|
[1537] | 615 | { |
---|
[27868] | 616 | $token = $expr->stokens[$i]; |
---|
| 617 | $clauses = array(); |
---|
| 618 | |
---|
| 619 | $like = addslashes($token); |
---|
| 620 | $like = str_replace( array('%','_'), array('\\%','\\_'), $like); // escape LIKE specials %_ |
---|
| 621 | $clauses[] = 'CONVERT(file, CHAR) LIKE \'%'.$like.'%\''; |
---|
| 622 | |
---|
[27884] | 623 | if ($inflector!=null && strlen($token)>2 |
---|
| 624 | && ($expr->stoken_modifiers[$i] & (QST_QUOTED|QST_WILDCARD))==0 |
---|
| 625 | && strcspn($token, '\'0123456789') == strlen($token) |
---|
| 626 | ) |
---|
| 627 | { |
---|
| 628 | $variants = array_unique( array_diff( $inflector->get_variants($token), array($token) ) ); |
---|
| 629 | $qsr->variants[$token] = $variants; |
---|
| 630 | } |
---|
| 631 | else |
---|
| 632 | { |
---|
| 633 | $variants = array(); |
---|
| 634 | } |
---|
| 635 | |
---|
[27868] | 636 | if (strlen($token)>3) // default minimum full text index |
---|
| 637 | { |
---|
| 638 | $ft = $token; |
---|
| 639 | if ($expr->stoken_modifiers[$i] & QST_QUOTED) |
---|
| 640 | $ft = '"'.$ft.'"'; |
---|
| 641 | if ($expr->stoken_modifiers[$i] & QST_WILDCARD_END) |
---|
| 642 | $ft .= '*'; |
---|
[27884] | 643 | foreach ($variants as $variant) |
---|
| 644 | { |
---|
| 645 | $ft.=' '.$variant; |
---|
| 646 | } |
---|
[27868] | 647 | $clauses[] = 'MATCH(i.name, i.comment) AGAINST( \''.addslashes($ft).'\' IN BOOLEAN MODE)'; |
---|
| 648 | } |
---|
| 649 | else |
---|
| 650 | { |
---|
| 651 | foreach( array('i.name', 'i.comment') as $field) |
---|
| 652 | { |
---|
[27884] | 653 | /*$clauses[] = $field.' LIKE \''.$like.' %\''; |
---|
[27868] | 654 | $clauses[] = $field.' LIKE \'% '.$like.'\''; |
---|
[27884] | 655 | $clauses[] = $field.' LIKE \'% '.$like.' %\'';*/ |
---|
| 656 | $clauses[] = $field.' REGEXP \'[[:<:]]'.addslashes(preg_quote($token)).'[[:>:]]\''; |
---|
[27868] | 657 | } |
---|
| 658 | } |
---|
| 659 | $query = $query_base.'('.implode(' OR ', $clauses).')'; |
---|
| 660 | $qsr->images_iids[$i] = query2array($query,null,'id'); |
---|
[1537] | 661 | } |
---|
| 662 | } |
---|
| 663 | |
---|
[27868] | 664 | function qsearch_get_tags(QExpression $expr, QResults $qsr) |
---|
[1537] | 665 | { |
---|
[27868] | 666 | $tokens = $expr->stokens; |
---|
| 667 | $token_modifiers = $expr->stoken_modifiers; |
---|
| 668 | |
---|
[18636] | 669 | $token_tag_ids = array_fill(0, count($tokens), array() ); |
---|
[27868] | 670 | $all_tags = array(); |
---|
[10340] | 671 | |
---|
[18636] | 672 | $token_tag_scores = $token_tag_ids; |
---|
[10340] | 673 | $transliterated_tokens = array(); |
---|
| 674 | foreach ($tokens as $token) |
---|
| 675 | { |
---|
| 676 | $transliterated_tokens[] = transliterate($token); |
---|
| 677 | } |
---|
| 678 | |
---|
| 679 | $query = ' |
---|
[18636] | 680 | SELECT t.*, COUNT(image_id) AS counter |
---|
| 681 | FROM '.TAGS_TABLE.' t |
---|
[10340] | 682 | INNER JOIN '.IMAGE_TAG_TABLE.' ON id=tag_id |
---|
| 683 | GROUP BY id'; |
---|
| 684 | $result = pwg_query($query); |
---|
| 685 | while ($tag = pwg_db_fetch_assoc($result)) |
---|
| 686 | { |
---|
| 687 | $transliterated_tag = transliterate($tag['name']); |
---|
| 688 | |
---|
| 689 | // find how this tag matches query tokens |
---|
| 690 | for ($i=0; $i<count($tokens); $i++) |
---|
| 691 | { |
---|
| 692 | $transliterated_token = $transliterated_tokens[$i]; |
---|
| 693 | |
---|
| 694 | $match = false; |
---|
| 695 | $pos = 0; |
---|
| 696 | while ( ($pos = strpos($transliterated_tag, $transliterated_token, $pos)) !== false) |
---|
| 697 | { |
---|
[18207] | 698 | if ( ($token_modifiers[$i]&QST_WILDCARD)==QST_WILDCARD ) |
---|
[10340] | 699 | {// wildcard in this token |
---|
| 700 | $match = 1; |
---|
| 701 | break; |
---|
| 702 | } |
---|
| 703 | $token_len = strlen($transliterated_token); |
---|
| 704 | |
---|
[18207] | 705 | // search begin of word |
---|
| 706 | $wbegin_len=0; $wbegin_char=' '; |
---|
| 707 | while ($pos-$wbegin_len > 0) |
---|
[10340] | 708 | { |
---|
[18207] | 709 | if (! is_word_char($transliterated_tag[$pos-$wbegin_len-1]) ) |
---|
| 710 | { |
---|
| 711 | $wbegin_char = $transliterated_tag[$pos-$wbegin_len-1]; |
---|
[10340] | 712 | break; |
---|
[18207] | 713 | } |
---|
| 714 | $wbegin_len++; |
---|
[10340] | 715 | } |
---|
| 716 | |
---|
[18207] | 717 | // search end of word |
---|
| 718 | $wend_len=0; $wend_char=' '; |
---|
| 719 | while ($pos+$token_len+$wend_len < strlen($transliterated_tag)) |
---|
[10340] | 720 | { |
---|
[18207] | 721 | if (! is_word_char($transliterated_tag[$pos+$token_len+$wend_len]) ) |
---|
| 722 | { |
---|
| 723 | $wend_char = $transliterated_tag[$pos+$token_len+$wend_len]; |
---|
| 724 | break; |
---|
| 725 | } |
---|
| 726 | $wend_len++; |
---|
[10340] | 727 | } |
---|
| 728 | |
---|
[18207] | 729 | $this_score = 0; |
---|
| 730 | if ( ($token_modifiers[$i]&QST_WILDCARD)==0 ) |
---|
| 731 | {// no wildcard begin or end |
---|
| 732 | if ($token_len <= 2) |
---|
| 733 | {// search for 1 or 2 characters must match exactly to avoid retrieving too much data |
---|
| 734 | if ($wbegin_len==0 && $wend_len==0 && !is_odd_wbreak_begin($wbegin_char) && !is_odd_wbreak_end($wend_char) ) |
---|
| 735 | $this_score = 1; |
---|
| 736 | } |
---|
| 737 | elseif ($token_len == 3) |
---|
| 738 | { |
---|
| 739 | if ($wbegin_len==0) |
---|
| 740 | $this_score = $token_len / ($token_len + $wend_len); |
---|
| 741 | } |
---|
| 742 | else |
---|
| 743 | { |
---|
| 744 | $this_score = $token_len / ($token_len + 1.1 * $wbegin_len + 0.9 * $wend_len); |
---|
| 745 | } |
---|
| 746 | } |
---|
| 747 | |
---|
[10340] | 748 | if ($this_score>0) |
---|
| 749 | $match = max($match, $this_score ); |
---|
| 750 | $pos++; |
---|
| 751 | } |
---|
| 752 | |
---|
| 753 | if ($match) |
---|
| 754 | { |
---|
| 755 | $tag_id = (int)$tag['id']; |
---|
| 756 | $all_tags[$tag_id] = $tag; |
---|
[18636] | 757 | $token_tag_ids[$i][] = $tag_id; |
---|
| 758 | $token_tag_scores[$i][] = $match; |
---|
[10340] | 759 | } |
---|
| 760 | } |
---|
| 761 | } |
---|
| 762 | |
---|
[27868] | 763 | // process tags |
---|
| 764 | $not_tag_ids = array(); |
---|
[18636] | 765 | for ($i=0; $i<count($tokens); $i++) |
---|
[10340] | 766 | { |
---|
[18636] | 767 | array_multisort($token_tag_scores[$i], SORT_DESC|SORT_NUMERIC, $token_tag_ids[$i]); |
---|
[27868] | 768 | $is_not = $token_modifiers[$i]&QST_NOT; |
---|
| 769 | $counter = 0; |
---|
[18636] | 770 | |
---|
| 771 | for ($j=0; $j<count($token_tag_scores[$i]); $j++) |
---|
[10340] | 772 | { |
---|
[27868] | 773 | if ($is_not) |
---|
[18636] | 774 | { |
---|
[27868] | 775 | if ($token_tag_scores[$i][$j] < 0.8 || |
---|
| 776 | ($j>0 && $token_tag_scores[$i][$j] < $token_tag_scores[$i][0]) ) |
---|
| 777 | { |
---|
| 778 | array_splice($token_tag_scores[$i], $j); |
---|
| 779 | array_splice($token_tag_ids[$i], $j); |
---|
| 780 | } |
---|
[18636] | 781 | } |
---|
[27868] | 782 | else |
---|
| 783 | { |
---|
| 784 | $tag_id = $token_tag_ids[$i][$j]; |
---|
| 785 | $counter += $all_tags[$tag_id]['counter']; |
---|
[27884] | 786 | if ( $j>0 && ( |
---|
| 787 | ($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 |
---|
| 788 | || ($token_tag_scores[$i][0]==1 && $token_tag_scores[$i][$j]<0.8) |
---|
| 789 | || ($token_tag_scores[$i][0]>0.8 && $token_tag_scores[$i][$j]<0.5) |
---|
| 790 | )) |
---|
| 791 | {// 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 |
---|
[27868] | 792 | array_splice($token_tag_ids[$i], $j); |
---|
| 793 | array_splice($token_tag_scores[$i], $j); |
---|
| 794 | break; |
---|
| 795 | } |
---|
| 796 | } |
---|
[18636] | 797 | } |
---|
[27868] | 798 | |
---|
| 799 | if ($is_not) |
---|
| 800 | { |
---|
| 801 | $not_tag_ids = array_merge($not_tag_ids, $token_tag_ids[$i]); |
---|
| 802 | } |
---|
[18636] | 803 | } |
---|
| 804 | |
---|
[27868] | 805 | $all_tags = array_diff_key($all_tags, array_flip($not_tag_ids)); |
---|
| 806 | usort($all_tags, 'tag_alpha_compare'); |
---|
| 807 | foreach ( $all_tags as &$tag ) |
---|
| 808 | { |
---|
| 809 | $tag['name'] = trigger_event('render_tag_name', $tag['name'], $tag); |
---|
| 810 | } |
---|
| 811 | $qsr->all_tags = $all_tags; |
---|
| 812 | |
---|
| 813 | $qsr->tag_ids = $token_tag_ids; |
---|
| 814 | $qsr->tag_iids = array_fill(0, count($tokens), array() ); |
---|
| 815 | |
---|
[18636] | 816 | for ($i=0; $i<count($tokens); $i++) |
---|
| 817 | { |
---|
[27868] | 818 | $tag_ids = $token_tag_ids[$i]; |
---|
[18636] | 819 | |
---|
[27868] | 820 | if (!empty($tag_ids)) |
---|
| 821 | { |
---|
| 822 | $query = ' |
---|
| 823 | SELECT image_id FROM '.IMAGE_TAG_TABLE.' |
---|
| 824 | WHERE tag_id IN ('.implode(',',$tag_ids).') |
---|
| 825 | GROUP BY image_id'; |
---|
| 826 | $qsr->tag_iids[$i] = query2array($query, null, 'image_id'); |
---|
| 827 | } |
---|
| 828 | } |
---|
| 829 | } |
---|
[18636] | 830 | |
---|
[27868] | 831 | |
---|
[27882] | 832 | function qsearch_eval(QMultiToken $expr, QResults $qsr, &$qualifies, &$ignored_terms) |
---|
[27868] | 833 | { |
---|
[27882] | 834 | $qualifies = false; // until we find at least one positive term |
---|
| 835 | $ignored_terms = array(); |
---|
| 836 | |
---|
[27868] | 837 | $ids = $not_ids = array(); |
---|
[27882] | 838 | |
---|
| 839 | for ($i=0; $i<count($expr->tokens); $i++) |
---|
[27868] | 840 | { |
---|
[27882] | 841 | $crt = $expr->tokens[$i]; |
---|
| 842 | if ($crt->is_single) |
---|
[18636] | 843 | { |
---|
[27882] | 844 | $crt_ids = $qsr->iids[$crt->idx] = array_unique( array_merge($qsr->images_iids[$crt->idx], $qsr->tag_iids[$crt->idx]) ); |
---|
| 845 | $crt_qualifies = count($crt_ids)>0 || count($qsr->tag_ids[$crt->idx])>0; |
---|
| 846 | $crt_ignored_terms = $crt_qualifies ? array() : array($crt->token); |
---|
[27868] | 847 | } |
---|
| 848 | else |
---|
[27882] | 849 | $crt_ids = qsearch_eval($crt, $qsr, $crt_qualifies, $crt_ignored_terms); |
---|
[27868] | 850 | |
---|
[27882] | 851 | $modifier = $expr->token_modifiers[$i]; |
---|
[27868] | 852 | if ($modifier & QST_NOT) |
---|
| 853 | $not_ids = array_unique( array_merge($not_ids, $crt_ids)); |
---|
| 854 | else |
---|
| 855 | { |
---|
[27882] | 856 | $ignored_terms = array_merge($ignored_terms, $crt_ignored_terms); |
---|
[27868] | 857 | if ($modifier & QST_OR) |
---|
[27882] | 858 | { |
---|
[27868] | 859 | $ids = array_unique( array_merge($ids, $crt_ids) ); |
---|
[27882] | 860 | $qualifies |= $crt_qualifies; |
---|
| 861 | } |
---|
| 862 | elseif ($crt_qualifies) |
---|
[18636] | 863 | { |
---|
[27882] | 864 | if ($qualifies) |
---|
| 865 | $ids = array_intersect($ids, $crt_ids); |
---|
| 866 | else |
---|
[27868] | 867 | $ids = $crt_ids; |
---|
[27882] | 868 | $qualifies = true; |
---|
[18636] | 869 | } |
---|
[10340] | 870 | } |
---|
| 871 | } |
---|
[22175] | 872 | |
---|
[27868] | 873 | if (count($not_ids)) |
---|
| 874 | $ids = array_diff($ids, $not_ids); |
---|
| 875 | return $ids; |
---|
[18636] | 876 | } |
---|
[10340] | 877 | |
---|
[18636] | 878 | /** |
---|
[25658] | 879 | * Returns the search results corresponding to a quick/query search. |
---|
[18636] | 880 | * A quick/query search returns many items (search is not strict), but results |
---|
| 881 | * are sorted by relevance unless $super_order_by is true. Returns: |
---|
[25658] | 882 | * array ( |
---|
| 883 | * 'items' => array of matching images |
---|
| 884 | * 'qs' => array( |
---|
[27882] | 885 | * 'unmatched_terms' => array of terms from the input string that were not matched |
---|
[25658] | 886 | * 'matching_tags' => array of matching tags |
---|
| 887 | * 'matching_cats' => array of matching categories |
---|
| 888 | * 'matching_cats_no_images' =>array(99) - matching categories without images |
---|
| 889 | * ) |
---|
| 890 | * ) |
---|
[18636] | 891 | * |
---|
[25658] | 892 | * @param string $q |
---|
| 893 | * @param bool $super_order_by |
---|
| 894 | * @param string $images_where optional additional restriction on images table |
---|
[18636] | 895 | * @return array |
---|
| 896 | */ |
---|
| 897 | function get_quick_search_results($q, $super_order_by, $images_where='') |
---|
| 898 | { |
---|
[27882] | 899 | global $conf; |
---|
| 900 | //@TODO: maybe cache for 10 minutes the result set to avoid many expensive sql calls when navigating the pictures |
---|
[27884] | 901 | $q = trim(stripslashes($q)); |
---|
[18636] | 902 | $search_results = |
---|
| 903 | array( |
---|
| 904 | 'items' => array(), |
---|
[27884] | 905 | 'qs' => array('q'=>$q), |
---|
[18636] | 906 | ); |
---|
[27884] | 907 | |
---|
[27868] | 908 | $expression = new QExpression($q); |
---|
| 909 | //var_export($expression); |
---|
[18636] | 910 | |
---|
[27868] | 911 | $qsr = new QResults; |
---|
| 912 | qsearch_get_tags($expression, $qsr); |
---|
| 913 | qsearch_get_images($expression, $qsr); |
---|
| 914 | //var_export($qsr->all_tags); |
---|
[18636] | 915 | |
---|
[27882] | 916 | $ids = qsearch_eval($expression, $qsr, $tmp, $search_results['qs']['unmatched_terms']); |
---|
[18636] | 917 | |
---|
[27868] | 918 | $debug[] = "<!--\nparsed: ".$expression; |
---|
| 919 | $debug[] = count($expression->stokens).' tokens'; |
---|
| 920 | for ($i=0; $i<count($expression->stokens); $i++) |
---|
[18636] | 921 | { |
---|
[27884] | 922 | $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' |
---|
| 923 | .( !empty($qsr->variants[$expression->stokens[$i]]) ? ' variants: '.implode(', ',$qsr->variants[$expression->stokens[$i]]): ''); |
---|
[18636] | 924 | } |
---|
[27868] | 925 | $debug[] = 'before perms '.count($ids); |
---|
[18636] | 926 | |
---|
[27868] | 927 | $search_results['qs']['matching_tags'] = $qsr->all_tags; |
---|
| 928 | global $template; |
---|
[18636] | 929 | |
---|
[27868] | 930 | if (empty($ids)) |
---|
[18636] | 931 | { |
---|
[27868] | 932 | $debug[] = '-->'; |
---|
| 933 | $template->append('footer_elements', implode("\n", $debug) ); |
---|
[2135] | 934 | return $search_results; |
---|
| 935 | } |
---|
| 936 | |
---|
| 937 | $where_clauses = array(); |
---|
[27868] | 938 | $where_clauses[]='i.id IN ('. implode(',', $ids) . ')'; |
---|
[2135] | 939 | if (!empty($images_where)) |
---|
| 940 | { |
---|
| 941 | $where_clauses[]='('.$images_where.')'; |
---|
| 942 | } |
---|
| 943 | $where_clauses[] = get_sql_condition_FandF( |
---|
| 944 | array |
---|
| 945 | ( |
---|
| 946 | 'forbidden_categories' => 'category_id', |
---|
| 947 | 'visible_categories' => 'category_id', |
---|
| 948 | 'visible_images' => 'i.id' |
---|
| 949 | ), |
---|
| 950 | null,true |
---|
| 951 | ); |
---|
| 952 | |
---|
| 953 | $query = ' |
---|
[1537] | 954 | SELECT DISTINCT(id) |
---|
[2135] | 955 | FROM '.IMAGES_TABLE.' i |
---|
[1537] | 956 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id |
---|
[2135] | 957 | WHERE '.implode("\n AND ", $where_clauses)."\n". |
---|
| 958 | $conf['order_by']; |
---|
| 959 | |
---|
[27868] | 960 | $ids = query2array($query, null, 'id'); |
---|
[2135] | 961 | |
---|
[27868] | 962 | $debug[] = count($ids).' final photo count -->'; |
---|
| 963 | $template->append('footer_elements', implode("\n", $debug) ); |
---|
[18207] | 964 | |
---|
[27868] | 965 | $search_results['items'] = $ids; |
---|
[1537] | 966 | return $search_results; |
---|
| 967 | } |
---|
| 968 | |
---|
| 969 | /** |
---|
[25658] | 970 | * Returns an array of 'items' corresponding to the search id. |
---|
| 971 | * It can be either a quick search or a regular search. |
---|
[1537] | 972 | * |
---|
[25658] | 973 | * @param int $search_id |
---|
| 974 | * @param bool $super_order_by |
---|
| 975 | * @param string $images_where optional aditional restriction on images table |
---|
[1537] | 976 | * @return array |
---|
| 977 | */ |
---|
[2451] | 978 | function get_search_results($search_id, $super_order_by, $images_where='') |
---|
[1537] | 979 | { |
---|
| 980 | $search = get_search_array($search_id); |
---|
| 981 | if ( !isset($search['q']) ) |
---|
| 982 | { |
---|
[2451] | 983 | $result['items'] = get_regular_search_results($search, $images_where); |
---|
[1537] | 984 | return $result; |
---|
| 985 | } |
---|
| 986 | else |
---|
| 987 | { |
---|
[2451] | 988 | return get_quick_search_results($search['q'], $super_order_by, $images_where); |
---|
[1537] | 989 | } |
---|
| 990 | } |
---|
[25658] | 991 | |
---|
[1113] | 992 | ?> |
---|