Changeset 1537 for trunk/include/functions_search.inc.php
- Timestamp:
- Aug 15, 2006, 4:06:06 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/functions_search.inc.php
r1119 r1537 74 74 * need to be transformed into an SQL clause to be used in queries. 75 75 * 76 * @param int search_id76 * @param array search 77 77 * @return string 78 78 */ 79 function get_sql_search_clause($search_id) 80 { 81 $search = get_search_array($search_id); 82 79 function get_sql_search_clause($search) 80 { 83 81 // SQL where clauses are stored in $clauses array during query 84 82 // construction … … 213 211 214 212 /** 215 * returns the list of items corresponding to the search id216 * 217 * @param int search id213 * returns the list of items corresponding to the advanced search array 214 * 215 * @param array search 218 216 * @return array 219 217 */ 220 function get_ search_items($search_id)218 function get_regular_search_results($search) 221 219 { 222 220 $items = array(); 223 224 $search_clause = get_sql_search_clause($search _id);225 221 222 $search_clause = get_sql_search_clause($search); 223 226 224 if (!empty($search_clause)) 227 225 { … … 270 268 } 271 269 } 272 270 273 271 return $items; 274 272 } 273 274 275 if (!function_exists('array_intersect_key')) { 276 function array_intersect_key() 277 { 278 $arrs = func_get_args(); 279 $result = array_shift($arrs); 280 foreach ($arrs as $array) { 281 foreach ($result as $key => $v) { 282 if (!array_key_exists($key, $array)) { 283 unset($result[$key]); 284 } 285 } 286 } 287 return $result; 288 } 289 } 290 291 292 function get_qsearch_like_clause($q, $field) 293 { 294 $tokens = preg_split('/[\s,.;!\?]+/', $q); 295 for ($i=0; $i<count($tokens); $i++) 296 { 297 $tokens[$i]=str_replace('*','%', $tokens[$i]); 298 if (preg_match('/^[+<>]/',$tokens[$i]) ) 299 $tokens[$i]=substr($tokens[$i], 1); 300 else if (substr($tokens[$i], 0, 1)=='-') 301 { 302 unset($tokens[$i]); 303 $i--; 304 } 305 } 306 307 if (!empty($tokens)) 308 { 309 $query = '('; 310 for ($i=0; $i<count($tokens); $i++) 311 { 312 if ($i>0) $query .= 'OR '; 313 $query .= ' '.$field.' LIKE "%'.$tokens[$i].'%" '; 314 } 315 $query .= ')'; 316 return $query; 317 } 318 return null; 319 } 320 321 322 /** 323 * returns the search results corresponding to a quick search 324 * 325 * @param string q 326 * @return array 327 */ 328 function get_quick_search_results($q) 329 { 330 global $user, $page; 331 $search_results = array(); 332 333 $q_like_clause = get_qsearch_like_clause($q, 'CONVERT(name, CHAR)' ); 334 $by_tag_weights=array(); 335 if (!empty($q_like_clause)) 336 { 337 $query = ' 338 SELECT id 339 FROM '.TAGS_TABLE.' 340 WHERE '.$q_like_clause; 341 $tag_ids = array_from_query( $query, 'id'); 342 if (!empty($tag_ids)) 343 { 344 $query = ' 345 SELECT image_id, COUNT(tag_id) AS q 346 FROM '.IMAGE_TAG_TABLE.' 347 WHERE tag_id IN ('.implode(',',$tag_ids).') 348 GROUP BY image_id'; 349 $result = pwg_query($query); 350 while ($row = mysql_fetch_array($result)) 351 { 352 $by_tag_weights[(int)$row['image_id']] = $row['q']; 353 } 354 } 355 } 356 357 $query = ' 358 SELECT 359 i.id, i.file, CAST( CONCAT_WS(" ", 360 IFNULL(i.name,""), 361 IFNULL(i.comment,""), 362 IFNULL(GROUP_CONCAT(DISTINCT co.content),""), 363 IFNULL(GROUP_CONCAT(DISTINCT c.dir),""), 364 IFNULL(GROUP_CONCAT(DISTINCT c.name),""), 365 IFNULL(GROUP_CONCAT(DISTINCT c.comment),"") ) AS CHAR) AS ft 366 FROM ( 367 ( 368 '.IMAGES_TABLE.' i LEFT JOIN '.COMMENTS_TABLE.' co on i.id=co.image_id 369 ) 370 INNER JOIN 371 '.IMAGE_CATEGORY_TABLE.' ic on ic.image_id=i.id 372 ) 373 INNER JOIN 374 '.CATEGORIES_TABLE.' c on c.id=ic.category_id 375 WHERE category_id NOT IN ('.$user['forbidden_categories'].') 376 GROUP BY i.id'; 377 378 $query = 'SELECT id, MATCH(ft) AGAINST( "'.$q.'" IN BOOLEAN MODE) AS q FROM ('.$query.') AS Y 379 WHERE MATCH(ft) AGAINST( "'.$q.'" IN BOOLEAN MODE)'; 380 381 $q_like_clause = get_qsearch_like_clause($q, 'file' ); 382 if (! empty($q_like_clause) ) 383 { 384 $query .= ' OR '.$q_like_clause; 385 } 386 387 $by_weights=array(); 388 $result = pwg_query($query); 389 while ($row = mysql_fetch_array($result)) 390 { 391 $by_weights[(int)$row['id']] = $row['q'] ? $row['q'] : 0; 392 } 393 394 foreach ( $by_weights as $image=>$w ) 395 { 396 $by_tag_weights[$image] = 2*$w+ (isset($by_tag_weights[$image])?$by_tag_weights[$image]:0); 397 } 398 399 if ( empty($by_tag_weights) or isset($page['super_order_by']) ) 400 { 401 if (! isset($page['super_order_by']) ) 402 { 403 arsort($by_tag_weights, SORT_NUMERIC); 404 $search_results['as_is']=1; 405 } 406 $search_results['items'] = array_keys($by_tag_weights); 407 } 408 else 409 { 410 $query = ' 411 SELECT DISTINCT(id) 412 FROM '.IMAGES_TABLE.' 413 INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id 414 WHERE id IN ('.implode(',', array_keys($by_tag_weights) ).') 415 AND category_id NOT IN ('.$user['forbidden_categories'].')'; 416 417 $allowed_image_ids = array_from_query( $query, 'id'); 418 $by_tag_weights = array_intersect_key($by_tag_weights, array_flip($allowed_image_ids)); 419 arsort($by_tag_weights, SORT_NUMERIC); 420 $search_results = array( 421 'items'=>array_keys($by_tag_weights), 422 'as_is'=>1 423 ); 424 } 425 return $search_results; 426 } 427 428 /** 429 * returns an array of 'items' corresponding to the search id 430 * 431 * @param int search id 432 * @return array 433 */ 434 function get_search_results($search_id) 435 { 436 $search = get_search_array($search_id); 437 if ( !isset($search['q']) ) 438 { 439 $result['items'] = get_regular_search_results($search); 440 return $result; 441 } 442 else 443 { 444 return get_quick_search_results($search['q']); 445 } 446 } 275 447 ?>
Note: See TracChangeset
for help on using the changeset viewer.