[1113] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[8728] | 3 | // | Piwigo - a PHP based photo gallery | |
---|
[2297] | 4 | // +-----------------------------------------------------------------------+ |
---|
[12922] | 5 | // | Copyright(C) 2008-2012 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 | |
---|
| 24 | |
---|
| 25 | /** |
---|
| 26 | * returns search rules stored into a serialized array in "search" |
---|
| 27 | * table. Each search rules set is numericaly identified. |
---|
| 28 | * |
---|
| 29 | * @param int search_id |
---|
| 30 | * @return array |
---|
| 31 | */ |
---|
| 32 | function get_search_array($search_id) |
---|
| 33 | { |
---|
| 34 | if (!is_numeric($search_id)) |
---|
| 35 | { |
---|
| 36 | die('Search id must be an integer'); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | $query = ' |
---|
| 40 | SELECT rules |
---|
| 41 | FROM '.SEARCH_TABLE.' |
---|
| 42 | WHERE id = '.$search_id.' |
---|
| 43 | ;'; |
---|
[4325] | 44 | list($serialized_rules) = pwg_db_fetch_row(pwg_query($query)); |
---|
[1113] | 45 | |
---|
| 46 | return unserialize($serialized_rules); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | /** |
---|
| 50 | * returns the SQL clause from a search identifier |
---|
| 51 | * |
---|
| 52 | * Search rules are stored in search table as a serialized array. This array |
---|
| 53 | * need to be transformed into an SQL clause to be used in queries. |
---|
| 54 | * |
---|
[1537] | 55 | * @param array search |
---|
[1113] | 56 | * @return string |
---|
| 57 | */ |
---|
[1537] | 58 | function get_sql_search_clause($search) |
---|
[1113] | 59 | { |
---|
| 60 | // SQL where clauses are stored in $clauses array during query |
---|
| 61 | // construction |
---|
| 62 | $clauses = array(); |
---|
| 63 | |
---|
[1119] | 64 | foreach (array('file','name','comment','author') as $textfield) |
---|
[1113] | 65 | { |
---|
| 66 | if (isset($search['fields'][$textfield])) |
---|
| 67 | { |
---|
| 68 | $local_clauses = array(); |
---|
| 69 | foreach ($search['fields'][$textfield]['words'] as $word) |
---|
| 70 | { |
---|
| 71 | array_push($local_clauses, $textfield." LIKE '%".$word."%'"); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | // adds brackets around where clauses |
---|
| 75 | $local_clauses = prepend_append_array_items($local_clauses, '(', ')'); |
---|
| 76 | |
---|
| 77 | array_push( |
---|
| 78 | $clauses, |
---|
| 79 | implode( |
---|
| 80 | ' '.$search['fields'][$textfield]['mode'].' ', |
---|
| 81 | $local_clauses |
---|
| 82 | ) |
---|
| 83 | ); |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | if (isset($search['fields']['allwords'])) |
---|
| 88 | { |
---|
[1119] | 89 | $fields = array('file', 'name', 'comment', 'author'); |
---|
[1113] | 90 | // in the OR mode, request bust be : |
---|
| 91 | // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%') |
---|
| 92 | // OR (field1 LIKE '%word2%' OR field2 LIKE '%word2%')) |
---|
| 93 | // |
---|
| 94 | // in the AND mode : |
---|
| 95 | // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%') |
---|
| 96 | // AND (field1 LIKE '%word2%' OR field2 LIKE '%word2%')) |
---|
| 97 | $word_clauses = array(); |
---|
| 98 | foreach ($search['fields']['allwords']['words'] as $word) |
---|
| 99 | { |
---|
| 100 | $field_clauses = array(); |
---|
| 101 | foreach ($fields as $field) |
---|
| 102 | { |
---|
| 103 | array_push($field_clauses, $field." LIKE '%".$word."%'"); |
---|
| 104 | } |
---|
| 105 | // adds brackets around where clauses |
---|
| 106 | array_push( |
---|
| 107 | $word_clauses, |
---|
| 108 | implode( |
---|
| 109 | "\n OR ", |
---|
| 110 | $field_clauses |
---|
| 111 | ) |
---|
| 112 | ); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | array_walk( |
---|
| 116 | $word_clauses, |
---|
| 117 | create_function('&$s','$s="(".$s.")";') |
---|
| 118 | ); |
---|
| 119 | |
---|
| 120 | array_push( |
---|
| 121 | $clauses, |
---|
| 122 | "\n ". |
---|
| 123 | implode( |
---|
| 124 | "\n ". |
---|
| 125 | $search['fields']['allwords']['mode']. |
---|
| 126 | "\n ", |
---|
| 127 | $word_clauses |
---|
| 128 | ) |
---|
| 129 | ); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | foreach (array('date_available', 'date_creation') as $datefield) |
---|
| 133 | { |
---|
| 134 | if (isset($search['fields'][$datefield])) |
---|
| 135 | { |
---|
| 136 | array_push( |
---|
| 137 | $clauses, |
---|
| 138 | $datefield." = '".$search['fields'][$datefield]['date']."'" |
---|
| 139 | ); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | foreach (array('after','before') as $suffix) |
---|
| 143 | { |
---|
| 144 | $key = $datefield.'-'.$suffix; |
---|
| 145 | |
---|
| 146 | if (isset($search['fields'][$key])) |
---|
| 147 | { |
---|
| 148 | array_push( |
---|
| 149 | $clauses, |
---|
| 150 | |
---|
| 151 | $datefield. |
---|
| 152 | ($suffix == 'after' ? ' >' : ' <'). |
---|
| 153 | ($search['fields'][$key]['inc'] ? '=' : ''). |
---|
| 154 | " '".$search['fields'][$key]['date']."'" |
---|
| 155 | |
---|
| 156 | ); |
---|
| 157 | } |
---|
| 158 | } |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | if (isset($search['fields']['cat'])) |
---|
| 162 | { |
---|
| 163 | if ($search['fields']['cat']['sub_inc']) |
---|
| 164 | { |
---|
| 165 | // searching all the categories id of sub-categories |
---|
| 166 | $cat_ids = get_subcat_ids($search['fields']['cat']['words']); |
---|
| 167 | } |
---|
| 168 | else |
---|
| 169 | { |
---|
| 170 | $cat_ids = $search['fields']['cat']['words']; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | $local_clause = 'category_id IN ('.implode(',', $cat_ids).')'; |
---|
| 174 | array_push($clauses, $local_clause); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | // adds brackets around where clauses |
---|
| 178 | $clauses = prepend_append_array_items($clauses, '(', ')'); |
---|
| 179 | |
---|
| 180 | $where_separator = |
---|
| 181 | implode( |
---|
| 182 | "\n ".$search['mode'].' ', |
---|
| 183 | $clauses |
---|
| 184 | ); |
---|
| 185 | |
---|
| 186 | $search_clause = $where_separator; |
---|
| 187 | |
---|
[1119] | 188 | return $search_clause; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | /** |
---|
[1537] | 192 | * returns the list of items corresponding to the advanced search array |
---|
[1119] | 193 | * |
---|
[1537] | 194 | * @param array search |
---|
[1119] | 195 | * @return array |
---|
| 196 | */ |
---|
[2451] | 197 | function get_regular_search_results($search, $images_where) |
---|
[1119] | 198 | { |
---|
[2451] | 199 | global $conf; |
---|
| 200 | $forbidden = get_sql_condition_FandF( |
---|
| 201 | array |
---|
| 202 | ( |
---|
| 203 | 'forbidden_categories' => 'category_id', |
---|
| 204 | 'visible_categories' => 'category_id', |
---|
| 205 | 'visible_images' => 'id' |
---|
| 206 | ), |
---|
| 207 | "\n AND" |
---|
| 208 | ); |
---|
| 209 | |
---|
[1119] | 210 | $items = array(); |
---|
[2451] | 211 | $tag_items = array(); |
---|
[1537] | 212 | |
---|
[2451] | 213 | if (isset($search['fields']['tags'])) |
---|
| 214 | { |
---|
| 215 | $tag_items = get_image_ids_for_tags( |
---|
| 216 | $search['fields']['tags']['words'], |
---|
| 217 | $search['fields']['tags']['mode'] |
---|
| 218 | ); |
---|
| 219 | } |
---|
| 220 | |
---|
[1537] | 221 | $search_clause = get_sql_search_clause($search); |
---|
| 222 | |
---|
[1119] | 223 | if (!empty($search_clause)) |
---|
[1113] | 224 | { |
---|
[1119] | 225 | $query = ' |
---|
[8611] | 226 | SELECT DISTINCT(id) |
---|
[2451] | 227 | FROM '.IMAGES_TABLE.' i |
---|
[1119] | 228 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id |
---|
[2451] | 229 | WHERE '.$search_clause; |
---|
| 230 | if (!empty($images_where)) |
---|
| 231 | { |
---|
| 232 | $query .= "\n AND ".$images_where; |
---|
| 233 | } |
---|
[8726] | 234 | $query .= $forbidden.' |
---|
[2451] | 235 | '.$conf['order_by']; |
---|
[1119] | 236 | $items = array_from_query($query, 'id'); |
---|
[1113] | 237 | } |
---|
| 238 | |
---|
[2451] | 239 | if ( !empty($tag_items) ) |
---|
[1119] | 240 | { |
---|
| 241 | switch ($search['mode']) |
---|
| 242 | { |
---|
| 243 | case 'AND': |
---|
| 244 | if (empty($search_clause)) |
---|
| 245 | { |
---|
| 246 | $items = $tag_items; |
---|
| 247 | } |
---|
| 248 | else |
---|
| 249 | { |
---|
[5691] | 250 | $items = array_values( array_intersect($items, $tag_items) ); |
---|
[1119] | 251 | } |
---|
| 252 | break; |
---|
| 253 | case 'OR': |
---|
[2451] | 254 | $before_count = count($items); |
---|
[1119] | 255 | $items = array_unique( |
---|
| 256 | array_merge( |
---|
| 257 | $items, |
---|
| 258 | $tag_items |
---|
| 259 | ) |
---|
| 260 | ); |
---|
| 261 | break; |
---|
[2451] | 262 | } |
---|
[1119] | 263 | } |
---|
[1537] | 264 | |
---|
[1119] | 265 | return $items; |
---|
[1113] | 266 | } |
---|
[1537] | 267 | |
---|
[10340] | 268 | |
---|
| 269 | function is_word_char($ch) |
---|
| 270 | { |
---|
| 271 | return ($ch>='0' && $ch<='9') || ($ch>='a' && $ch<='z') || ($ch>='A' && $ch<='Z') || ord($ch)>127; |
---|
| 272 | } |
---|
| 273 | |
---|
[1619] | 274 | /** |
---|
[10340] | 275 | * analyzes and splits the quick/query search query $q into tokens |
---|
| 276 | * q='john bill' => 2 tokens 'john' 'bill' |
---|
| 277 | * Special characters for MySql full text search (+,<,>,~) appear in the token modifiers. |
---|
| 278 | * The query can contain a phrase: 'Pierre "New York"' will return 'pierre' qnd 'new york'. |
---|
[1619] | 279 | */ |
---|
[10340] | 280 | function analyse_qsearch($q, &$qtokens, &$qtoken_modifiers) |
---|
[1537] | 281 | { |
---|
[2135] | 282 | $q = stripslashes($q); |
---|
| 283 | $tokens = array(); |
---|
| 284 | $token_modifiers = array(); |
---|
| 285 | $crt_token = ""; |
---|
| 286 | $crt_token_modifier = ""; |
---|
| 287 | $state = 0; |
---|
| 288 | |
---|
| 289 | for ($i=0; $i<strlen($q); $i++) |
---|
[1537] | 290 | { |
---|
[2135] | 291 | $ch = $q[$i]; |
---|
| 292 | switch ($state) |
---|
[1537] | 293 | { |
---|
[2135] | 294 | case 0: |
---|
| 295 | if ($ch=='"') |
---|
| 296 | { |
---|
[10340] | 297 | $tokens[] = $crt_token; $token_modifiers[] = $crt_token_modifier; |
---|
| 298 | $crt_token = ""; $crt_token_modifier = "q"; |
---|
[2135] | 299 | $state=1; |
---|
| 300 | } |
---|
| 301 | elseif ( $ch=='*' ) |
---|
| 302 | { // wild card |
---|
[10340] | 303 | if (strlen($crt_token)) |
---|
| 304 | { |
---|
| 305 | $crt_token .= $ch; |
---|
| 306 | } |
---|
| 307 | else |
---|
| 308 | { |
---|
| 309 | $crt_token_modifier .= '*'; |
---|
| 310 | } |
---|
[2135] | 311 | } |
---|
| 312 | elseif ( strcspn($ch, '+-><~')==0 ) |
---|
| 313 | { //special full text modifier |
---|
| 314 | if (strlen($crt_token)) |
---|
| 315 | { |
---|
[10340] | 316 | $tokens[] = $crt_token; $token_modifiers[] = $crt_token_modifier; |
---|
| 317 | $crt_token = ""; $crt_token_modifier = ""; |
---|
[2135] | 318 | } |
---|
| 319 | $crt_token_modifier .= $ch; |
---|
| 320 | } |
---|
| 321 | elseif (preg_match('/[\s,.;!\?]+/', $ch)) |
---|
| 322 | { // white space |
---|
| 323 | if (strlen($crt_token)) |
---|
| 324 | { |
---|
[10340] | 325 | $tokens[] = $crt_token; $token_modifiers[] = $crt_token_modifier; |
---|
| 326 | $crt_token = ""; $crt_token_modifier = ""; |
---|
[2135] | 327 | } |
---|
| 328 | } |
---|
| 329 | else |
---|
| 330 | { |
---|
| 331 | $crt_token .= $ch; |
---|
| 332 | } |
---|
| 333 | break; |
---|
| 334 | case 1: // qualified with quotes |
---|
| 335 | switch ($ch) |
---|
| 336 | { |
---|
| 337 | case '"': |
---|
[10340] | 338 | $tokens[] = $crt_token; $token_modifiers[] = $crt_token_modifier; |
---|
| 339 | $crt_token = ""; $crt_token_modifier = ""; |
---|
[2135] | 340 | $state=0; |
---|
| 341 | break; |
---|
| 342 | default: |
---|
| 343 | $crt_token .= $ch; |
---|
| 344 | } |
---|
| 345 | break; |
---|
[1537] | 346 | } |
---|
| 347 | } |
---|
[2135] | 348 | if (strlen($crt_token)) |
---|
| 349 | { |
---|
| 350 | $tokens[] = $crt_token; |
---|
| 351 | $token_modifiers[] = $crt_token_modifier; |
---|
| 352 | } |
---|
[1537] | 353 | |
---|
[10340] | 354 | $qtokens = array(); |
---|
| 355 | $qtoken_modifiers = array(); |
---|
| 356 | for ($i=0; $i<count($tokens); $i++) |
---|
| 357 | { |
---|
| 358 | if (strstr($token_modifiers[$i], 'q')===false) |
---|
| 359 | { |
---|
| 360 | if ( substr($tokens[$i], -1)=='*' ) |
---|
| 361 | { |
---|
| 362 | $tokens[$i] = rtrim($tokens[$i], '*'); |
---|
| 363 | $token_modifiers[$i] .= '*'; |
---|
| 364 | } |
---|
| 365 | } |
---|
| 366 | if ( strlen($tokens[$i])==0) |
---|
| 367 | continue; |
---|
| 368 | $qtokens[] = $tokens[$i]; |
---|
| 369 | $qtoken_modifiers[] = $token_modifiers[$i]; |
---|
| 370 | } |
---|
| 371 | } |
---|
| 372 | |
---|
| 373 | |
---|
| 374 | /** |
---|
| 375 | * returns the LIKE sql clause corresponding to the quick search query |
---|
| 376 | * that has been split into tokens |
---|
| 377 | * for example file LIKE '%john%' OR file LIKE '%bill%'. |
---|
| 378 | */ |
---|
| 379 | function get_qsearch_like_clause($tokens, $token_modifiers, $field) |
---|
| 380 | { |
---|
[2135] | 381 | $clauses = array(); |
---|
| 382 | for ($i=0; $i<count($tokens); $i++) |
---|
[1537] | 383 | { |
---|
[10340] | 384 | $token = trim($tokens[$i], '%'); |
---|
[2135] | 385 | if (strstr($token_modifiers[$i], '-')!==false) |
---|
| 386 | continue; |
---|
[11979] | 387 | if ( strlen($token)==0 ) |
---|
[2135] | 388 | continue; |
---|
[10340] | 389 | $token = addslashes($token); |
---|
| 390 | $token = str_replace( array('%','_'), array('\\%','\\_'), $token); // escape LIKE specials %_ |
---|
| 391 | $clauses[] = $field.' LIKE \'%'.$token.'%\''; |
---|
[1537] | 392 | } |
---|
[2135] | 393 | |
---|
| 394 | return count($clauses) ? '('.implode(' OR ', $clauses).')' : null; |
---|
[1537] | 395 | } |
---|
| 396 | |
---|
| 397 | /** |
---|
[2135] | 398 | * returns the search results corresponding to a quick/query search. |
---|
| 399 | * A quick/query search returns many items (search is not strict), but results |
---|
[2451] | 400 | * are sorted by relevance unless $super_order_by is true. Returns: |
---|
[2135] | 401 | * array ( |
---|
| 402 | * 'items' => array(85,68,79...) |
---|
| 403 | * 'qs' => array( |
---|
[2138] | 404 | * 'matching_tags' => array of matching tags |
---|
| 405 | * 'matching_cats' => array of matching categories |
---|
[2135] | 406 | * 'matching_cats_no_images' =>array(99) - matching categories without images |
---|
| 407 | * )) |
---|
[1537] | 408 | * |
---|
| 409 | * @param string q |
---|
[2451] | 410 | * @param bool super_order_by |
---|
[2135] | 411 | * @param string images_where optional aditional restriction on images table |
---|
[1537] | 412 | * @return array |
---|
| 413 | */ |
---|
[2451] | 414 | function get_quick_search_results($q, $super_order_by, $images_where='') |
---|
[1537] | 415 | { |
---|
[10340] | 416 | global $user, $conf; |
---|
| 417 | |
---|
[2135] | 418 | $search_results = |
---|
| 419 | array( |
---|
| 420 | 'items' => array(), |
---|
| 421 | 'qs' => array('q'=>stripslashes($q)), |
---|
| 422 | ); |
---|
[1837] | 423 | $q = trim($q); |
---|
| 424 | if (empty($q)) |
---|
[1537] | 425 | { |
---|
[1837] | 426 | return $search_results; |
---|
[1537] | 427 | } |
---|
[10340] | 428 | |
---|
| 429 | analyse_qsearch($q, $tokens, $token_modifiers); |
---|
| 430 | |
---|
[2135] | 431 | $q_like_field = '@@__db_field__@@'; //something never in a search |
---|
[10340] | 432 | $q_like_clause = get_qsearch_like_clause($tokens, $token_modifiers, $q_like_field ); |
---|
[2135] | 433 | |
---|
| 434 | // Step 1 - first we find matches in #images table =========================== |
---|
[6664] | 435 | $where_clauses='MATCH(i.name, i.comment) AGAINST( \''.$q.'\' IN BOOLEAN MODE)'; |
---|
[2135] | 436 | if (!empty($q_like_clause)) |
---|
| 437 | { |
---|
| 438 | $where_clauses .= ' |
---|
[4261] | 439 | OR '. str_replace($q_like_field, 'CONVERT(file, CHAR)', $q_like_clause); |
---|
[2135] | 440 | $where_clauses = '('.$where_clauses.')'; |
---|
| 441 | } |
---|
| 442 | $where_clauses = array($where_clauses); |
---|
| 443 | if (!empty($images_where)) |
---|
| 444 | { |
---|
| 445 | $where_clauses[]='('.$images_where.')'; |
---|
| 446 | } |
---|
| 447 | $where_clauses[] .= get_sql_condition_FandF |
---|
| 448 | ( |
---|
| 449 | array( 'visible_images' => 'i.id' ), null, true |
---|
| 450 | ); |
---|
[1537] | 451 | $query = ' |
---|
[2135] | 452 | SELECT i.id, |
---|
[6664] | 453 | MATCH(i.name, i.comment) AGAINST( \''.$q.'\' IN BOOLEAN MODE) AS weight |
---|
[2135] | 454 | FROM '.IMAGES_TABLE.' i |
---|
| 455 | WHERE '.implode("\n AND ", $where_clauses); |
---|
[1537] | 456 | |
---|
| 457 | $by_weights=array(); |
---|
| 458 | $result = pwg_query($query); |
---|
[4325] | 459 | while ($row = pwg_db_fetch_assoc($result)) |
---|
[1837] | 460 | { // weight is important when sorting images by relevance |
---|
[2135] | 461 | if ($row['weight']) |
---|
[1837] | 462 | { |
---|
[2135] | 463 | $by_weights[(int)$row['id']] = 2*$row['weight']; |
---|
[1837] | 464 | } |
---|
[2135] | 465 | else |
---|
| 466 | {//full text does not match but file name match |
---|
| 467 | $by_weights[(int)$row['id']] = 2; |
---|
[1837] | 468 | } |
---|
[1537] | 469 | } |
---|
| 470 | |
---|
[2135] | 471 | |
---|
| 472 | // Step 2 - search tags corresponding to the query $q ======================== |
---|
[10340] | 473 | $transliterated_tokens = array(); |
---|
| 474 | $token_tags = array(); |
---|
| 475 | foreach ($tokens as $token) |
---|
| 476 | { |
---|
| 477 | $transliterated_tokens[] = transliterate($token); |
---|
| 478 | $token_tags[] = array(); |
---|
| 479 | } |
---|
| 480 | |
---|
| 481 | // Step 2.1 - find match tags for every token in the query search |
---|
| 482 | $all_tags = array(); |
---|
| 483 | $query = ' |
---|
| 484 | SELECT id, name, url_name, COUNT(image_id) AS nb_images |
---|
[1837] | 485 | FROM '.TAGS_TABLE.' |
---|
[10340] | 486 | INNER JOIN '.IMAGE_TAG_TABLE.' ON id=tag_id |
---|
| 487 | GROUP BY id'; |
---|
| 488 | $result = pwg_query($query); |
---|
| 489 | while ($tag = pwg_db_fetch_assoc($result)) |
---|
| 490 | { |
---|
| 491 | $transliterated_tag = transliterate($tag['name']); |
---|
| 492 | |
---|
| 493 | // find how this tag matches query tokens |
---|
| 494 | for ($i=0; $i<count($tokens); $i++) |
---|
| 495 | { |
---|
| 496 | if (strstr($token_modifiers[$i], '-')!==false) |
---|
| 497 | continue;// ignore this NOT token |
---|
| 498 | $transliterated_token = $transliterated_tokens[$i]; |
---|
| 499 | |
---|
| 500 | $match = false; |
---|
| 501 | $pos = 0; |
---|
| 502 | while ( ($pos = strpos($transliterated_tag, $transliterated_token, $pos)) !== false) |
---|
| 503 | { |
---|
| 504 | if (strstr($token_modifiers[$i], '*')!==false) |
---|
| 505 | {// wildcard in this token |
---|
| 506 | $match = 1; |
---|
| 507 | break; |
---|
| 508 | } |
---|
| 509 | $token_len = strlen($transliterated_token); |
---|
| 510 | |
---|
| 511 | $word_begin = $pos; |
---|
| 512 | while ($word_begin>0) |
---|
| 513 | { |
---|
| 514 | if (! is_word_char($transliterated_tag[$word_begin-1]) ) |
---|
| 515 | break; |
---|
| 516 | $word_begin--; |
---|
| 517 | } |
---|
| 518 | |
---|
| 519 | $word_end = $pos + $token_len; |
---|
| 520 | while ($word_end<strlen($transliterated_tag) && is_word_char($transliterated_tag[$word_end]) ) |
---|
| 521 | $word_end++; |
---|
| 522 | |
---|
| 523 | $this_score = $token_len / ($word_end-$word_begin); |
---|
| 524 | if ($token_len <= 2) |
---|
| 525 | {// search for 1 or 2 characters must match exactly to avoid retrieving too much data |
---|
| 526 | if ($token_len != $word_end-$word_begin) |
---|
| 527 | $this_score = 0; |
---|
| 528 | } |
---|
| 529 | elseif ($token_len == 3) |
---|
| 530 | { |
---|
| 531 | if ($word_end-$word_begin > 4) |
---|
| 532 | $this_score = 0; |
---|
| 533 | } |
---|
| 534 | |
---|
| 535 | if ($this_score>0) |
---|
| 536 | $match = max($match, $this_score ); |
---|
| 537 | $pos++; |
---|
| 538 | } |
---|
| 539 | |
---|
| 540 | if ($match) |
---|
| 541 | { |
---|
| 542 | $tag_id = (int)$tag['id']; |
---|
| 543 | $all_tags[$tag_id] = $tag; |
---|
| 544 | $token_tags[$i][] = array('tag_id'=>$tag_id, 'score'=>$match); |
---|
| 545 | } |
---|
| 546 | } |
---|
| 547 | } |
---|
| 548 | $search_results['qs']['matching_tags']=$all_tags; |
---|
| 549 | |
---|
| 550 | // Step 2.2 - reduce matching tags for every token in the query search |
---|
| 551 | $score_cmp_fn = create_function('$a,$b', 'return 100*($b["score"]-$a["score"]);'); |
---|
| 552 | foreach ($token_tags as &$tt) |
---|
| 553 | { |
---|
| 554 | usort($tt, $score_cmp_fn); |
---|
| 555 | $nb_images = 0; |
---|
| 556 | $prev_score = 0; |
---|
| 557 | for ($j=0; $j<count($tt); $j++) |
---|
| 558 | { |
---|
| 559 | if ($nb_images > 200 && $prev_score > $tt[$j]['score'] ) |
---|
| 560 | {// "many" images in previous tags and starting from this tag is less relevent |
---|
| 561 | $tt = array_slice( $tt, 0, $j); |
---|
| 562 | break; |
---|
| 563 | } |
---|
| 564 | $nb_images += $all_tags[ $tt[$j]['tag_id'] ]['nb_images']; |
---|
| 565 | $prev_score = $tt[$j]['score']; |
---|
| 566 | } |
---|
| 567 | } |
---|
| 568 | |
---|
| 569 | // Step 2.3 - get the images for tags |
---|
| 570 | for ($i=0; $i<count($token_tags); $i++) |
---|
| 571 | { |
---|
| 572 | $tag_ids = array(); |
---|
| 573 | foreach($token_tags[$i] as $arr) |
---|
| 574 | $tag_ids[] = $arr['tag_id']; |
---|
| 575 | |
---|
| 576 | if (!empty($tag_ids)) |
---|
| 577 | { |
---|
[1837] | 578 | $query = ' |
---|
[10340] | 579 | SELECT image_id |
---|
[1837] | 580 | FROM '.IMAGE_TAG_TABLE.' |
---|
[10340] | 581 | WHERE tag_id IN ('.implode(',',$tag_ids).') |
---|
[1837] | 582 | GROUP BY image_id'; |
---|
| 583 | $result = pwg_query($query); |
---|
[4325] | 584 | while ($row = pwg_db_fetch_assoc($result)) |
---|
[1837] | 585 | { // weight is important when sorting images by relevance |
---|
| 586 | $image_id=(int)$row['image_id']; |
---|
[10340] | 587 | @$by_weights[$image_id] += 1; |
---|
[1837] | 588 | } |
---|
| 589 | } |
---|
[1537] | 590 | } |
---|
| 591 | |
---|
[2135] | 592 | // Step 3 - search categories corresponding to the query $q ================== |
---|
| 593 | $query = ' |
---|
[2138] | 594 | SELECT id, name, permalink, nb_images |
---|
[2135] | 595 | FROM '.CATEGORIES_TABLE.' |
---|
| 596 | INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' ON id=cat_id |
---|
| 597 | WHERE user_id='.$user['id'].' |
---|
[6664] | 598 | AND MATCH(name, comment) AGAINST( \''.$q.'\' IN BOOLEAN MODE)'. |
---|
[2135] | 599 | get_sql_condition_FandF ( |
---|
| 600 | array( 'visible_categories' => 'cat_id' ), "\n AND" |
---|
| 601 | ); |
---|
| 602 | $result = pwg_query($query); |
---|
[4325] | 603 | while ($row = pwg_db_fetch_assoc($result)) |
---|
[2135] | 604 | { // weight is important when sorting images by relevance |
---|
| 605 | if ($row['nb_images']==0) |
---|
| 606 | { |
---|
[2138] | 607 | $search_results['qs']['matching_cats_no_images'][] = $row; |
---|
[2135] | 608 | } |
---|
| 609 | else |
---|
| 610 | { |
---|
[2138] | 611 | $search_results['qs']['matching_cats'][$row['id']] = $row; |
---|
[2135] | 612 | } |
---|
| 613 | } |
---|
| 614 | |
---|
| 615 | if ( empty($by_weights) and empty($search_results['qs']['matching_cats']) ) |
---|
[1537] | 616 | { |
---|
[2135] | 617 | return $search_results; |
---|
| 618 | } |
---|
| 619 | |
---|
| 620 | // Step 4 - now we have $by_weights ( array image id => weight ) that need |
---|
| 621 | // permission checks and/or matching categories to get images from |
---|
| 622 | $where_clauses = array(); |
---|
| 623 | if ( !empty($by_weights) ) |
---|
| 624 | { |
---|
| 625 | $where_clauses[]='i.id IN (' |
---|
| 626 | . implode(',', array_keys($by_weights)) . ')'; |
---|
| 627 | } |
---|
| 628 | if ( !empty($search_results['qs']['matching_cats']) ) |
---|
| 629 | { |
---|
| 630 | $where_clauses[]='category_id IN ('. |
---|
[2138] | 631 | implode(',',array_keys($search_results['qs']['matching_cats'])).')'; |
---|
[2135] | 632 | } |
---|
| 633 | $where_clauses = array( '('.implode("\n OR ",$where_clauses).')' ); |
---|
| 634 | if (!empty($images_where)) |
---|
| 635 | { |
---|
| 636 | $where_clauses[]='('.$images_where.')'; |
---|
| 637 | } |
---|
| 638 | $where_clauses[] = get_sql_condition_FandF( |
---|
| 639 | array |
---|
| 640 | ( |
---|
| 641 | 'forbidden_categories' => 'category_id', |
---|
| 642 | 'visible_categories' => 'category_id', |
---|
| 643 | 'visible_images' => 'i.id' |
---|
| 644 | ), |
---|
| 645 | null,true |
---|
| 646 | ); |
---|
| 647 | |
---|
| 648 | $query = ' |
---|
[1537] | 649 | SELECT DISTINCT(id) |
---|
[2135] | 650 | FROM '.IMAGES_TABLE.' i |
---|
[1537] | 651 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id |
---|
[2135] | 652 | WHERE '.implode("\n AND ", $where_clauses)."\n". |
---|
| 653 | $conf['order_by']; |
---|
| 654 | |
---|
| 655 | $allowed_images = array_from_query( $query, 'id'); |
---|
| 656 | |
---|
[2451] | 657 | if ( $super_order_by or empty($by_weights) ) |
---|
[2135] | 658 | { |
---|
| 659 | $search_results['items'] = $allowed_images; |
---|
| 660 | return $search_results; |
---|
[1537] | 661 | } |
---|
[2135] | 662 | |
---|
| 663 | $allowed_images = array_flip( $allowed_images ); |
---|
| 664 | $divisor = 5.0 * count($allowed_images); |
---|
| 665 | foreach ($allowed_images as $id=>$rank ) |
---|
[1837] | 666 | { |
---|
[2135] | 667 | $weight = isset($by_weights[$id]) ? $by_weights[$id] : 1; |
---|
| 668 | $weight -= $rank/$divisor; |
---|
| 669 | $allowed_images[$id] = $weight; |
---|
[1837] | 670 | } |
---|
[2135] | 671 | arsort($allowed_images, SORT_NUMERIC); |
---|
| 672 | $search_results['items'] = array_keys($allowed_images); |
---|
[1537] | 673 | return $search_results; |
---|
| 674 | } |
---|
| 675 | |
---|
| 676 | /** |
---|
| 677 | * returns an array of 'items' corresponding to the search id |
---|
| 678 | * |
---|
| 679 | * @param int search id |
---|
[2135] | 680 | * @param string images_where optional aditional restriction on images table |
---|
[1537] | 681 | * @return array |
---|
| 682 | */ |
---|
[2451] | 683 | function get_search_results($search_id, $super_order_by, $images_where='') |
---|
[1537] | 684 | { |
---|
| 685 | $search = get_search_array($search_id); |
---|
| 686 | if ( !isset($search['q']) ) |
---|
| 687 | { |
---|
[2451] | 688 | $result['items'] = get_regular_search_results($search, $images_where); |
---|
[1537] | 689 | return $result; |
---|
| 690 | } |
---|
| 691 | else |
---|
| 692 | { |
---|
[2451] | 693 | return get_quick_search_results($search['q'], $super_order_by, $images_where); |
---|
[1537] | 694 | } |
---|
| 695 | } |
---|
[1113] | 696 | ?> |
---|