Changeset 1837 for trunk/include/functions_search.inc.php
- Timestamp:
- Feb 19, 2007, 5:25:47 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/functions_search.inc.php
r1744 r1837 3 3 // | PhpWebGallery - a PHP based picture gallery | 4 4 // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | 5 // | Copyright (C) 2003-200 6PhpWebGallery Team - http://phpwebgallery.net |5 // | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net | 6 6 // +-----------------------------------------------------------------------+ 7 // | branch : BSF (Best So Far)8 7 // | file : $Id$ 9 8 // | last update : $Date$ … … 302 301 function get_quick_search_results($q) 303 302 { 304 global $ user, $page, $filter;303 global $page; 305 304 $search_results = array(); 306 307 // first search tag names corresponding to the query $q. we could also search 308 // tags later during the big join, but for the sake of the performance and 309 // because tags have only a simple name we do it separately 310 $q_like_clause = get_qsearch_like_clause($q, 'CONVERT(name, CHAR)' ); 311 $by_tag_weights=array(); 312 if (!empty($q_like_clause)) 313 { 314 $query = ' 315 SELECT id 316 FROM '.TAGS_TABLE.' 317 WHERE '.$q_like_clause; 318 $tag_ids = array_from_query( $query, 'id'); 319 if (!empty($tag_ids)) 320 { // we got some tags 321 $query = ' 322 SELECT image_id, COUNT(tag_id) AS q 323 FROM '.IMAGE_TAG_TABLE.' 324 WHERE tag_id IN ('.implode(',',$tag_ids).') 325 GROUP BY image_id'; 326 $result = pwg_query($query); 327 while ($row = mysql_fetch_assoc($result)) 328 { // weight is important when sorting images by relevance 329 $by_tag_weights[(int)$row['image_id']] = $row['q']; 330 } 331 } 332 } 333 305 $q = trim($q); 306 if (empty($q)) 307 { 308 $search_results['items'] = array(); 309 return $search_results; 310 } 334 311 // prepare the big join on images, comments and categories 335 312 $query = ' 336 313 SELECT 337 i.id, i.file,CAST( CONCAT_WS(" ",314 i.id, CAST( CONCAT_WS(" ", 338 315 IFNULL(i.name,""), 339 316 IFNULL(i.comment,""), … … 357 334 'forbidden_categories' => 'category_id', 358 335 'visible_categories' => 'category_id', 359 'visible_images' => 'i c.image_id'336 'visible_images' => 'i.id' 360 337 ), 361 338 'WHERE' … … 366 343 WHERE MATCH(ft) AGAINST( "'.$q.'" IN BOOLEAN MODE)'; 367 344 368 //also inlcude the file name (but avoid full text which is slower because369 //the filename in pwg doesn't have spaces so full text is meaningless anyway)370 $q_like_clause = get_qsearch_like_clause($q, 'file' );371 if (! empty($q_like_clause) )372 {373 $query .= ' OR '.$q_like_clause;374 }375 376 345 $by_weights=array(); 377 346 $result = pwg_query($query); 378 347 while ($row = mysql_fetch_array($result)) 379 { 380 $by_weights[(int)$row['id']] = $row['q'] ? $row['q'] : 0; 381 } 382 383 // finally merge the results (tags and big join) sorted by "relevance" 384 foreach ( $by_weights as $image=>$w ) 385 { 386 $by_tag_weights[$image] = 2*$w+ (isset($by_tag_weights[$image])?$by_tag_weights[$image]:0); 348 { // weight is important when sorting images by relevance 349 if ($row['q']) 350 { 351 $by_weights[(int)$row['id']] = 2*$row['q']; 352 } 353 } 354 355 $permissions_checked = true; 356 // now search the file name separately (not done in full text because slower 357 // and the filename in pwg doesn't have spaces so full text is meaningless ) 358 $q_like_clause = get_qsearch_like_clause($q, 'file' ); 359 if (!empty($q_like_clause)) 360 { 361 $query = ' 362 SELECT id 363 FROM '.IMAGES_TABLE.' 364 WHERE '.$q_like_clause. 365 get_sql_condition_FandF 366 ( 367 array 368 ( 369 'visible_images' => 'id' 370 ), 371 'AND' 372 ); 373 $result = pwg_query($query); 374 while ($row = mysql_fetch_assoc($result)) 375 { // weight is important when sorting images by relevance 376 $id=(int)$row['id']; 377 @$by_weights[$id] += 2; 378 $permissions_checked = false; 379 } 380 } 381 382 // now search tag names corresponding to the query $q. we could have searched 383 // tags earlier during the big join, but for the sake of the performance and 384 // because tags have only a simple name we do it separately 385 $q_like_clause = get_qsearch_like_clause($q, 'CONVERT(name, CHAR)' ); 386 if (!empty($q_like_clause)) 387 { 388 $query = ' 389 SELECT id 390 FROM '.TAGS_TABLE.' 391 WHERE '.$q_like_clause; 392 $tag_ids = array_from_query( $query, 'id'); 393 if (!empty($tag_ids)) 394 { // we got some tags 395 $query = ' 396 SELECT image_id, COUNT(tag_id) AS q 397 FROM '.IMAGE_TAG_TABLE.' 398 WHERE tag_id IN ('.implode(',',$tag_ids).') 399 GROUP BY image_id'; 400 $result = pwg_query($query); 401 while ($row = mysql_fetch_assoc($result)) 402 { // weight is important when sorting images by relevance 403 $image_id=(int)$row['image_id']; 404 @$by_weights[$image_id] += $row['q']; 405 $permissions_checked = false; 406 } 407 } 387 408 } 388 409 389 410 //at this point, found images might contain images not allowed for the user 390 if ( empty($by_tag_weights) or isset($page['super_order_by']) ) 391 { 392 // no aditionnal query here for permissions (will be done by section_init 393 // while sorting items as the user requested it) 394 $search_results['items'] = array_keys($by_tag_weights); 395 } 396 else 411 if ( !$permissions_checked 412 and !empty($by_weights) 413 and !isset($page['super_order_by']) ) 397 414 { 398 415 // before returning the result "as is", make sure the user has the … … 402 419 FROM '.IMAGES_TABLE.' 403 420 INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id 404 WHERE id IN ('.implode(',', array_keys($by_ tag_weights) ).')421 WHERE id IN ('.implode(',', array_keys($by_weights) ).') 405 422 '.get_sql_condition_FandF 406 423 ( … … 409 426 'forbidden_categories' => 'category_id', 410 427 'visible_categories' => 'category_id', 411 'visible_images' => 'i c.image_id'428 'visible_images' => 'id' 412 429 ), 413 430 'AND' 414 431 ); 415 432 $allowed_image_ids = array_from_query( $query, 'id'); 416 $by_tag_weights = array_intersect_key($by_tag_weights, array_flip($allowed_image_ids)); 417 arsort($by_tag_weights, SORT_NUMERIC); 418 $search_results = array( 419 'items'=>array_keys($by_tag_weights), 420 'as_is'=>1 421 ); 422 } 433 $by_weights = array_intersect_key($by_weights, array_flip($allowed_image_ids)); 434 $permissions_checked = true; 435 } 436 arsort($by_weights, SORT_NUMERIC); 437 if ( $permissions_checked ) 438 { 439 $search_results['as_is']=1; 440 } 441 442 $search_results['items'] = array_keys($by_weights); 423 443 return $search_results; 424 444 }
Note: See TracChangeset
for help on using the changeset viewer.