Ignore:
Timestamp:
Aug 15, 2006, 4:06:06 AM (18 years ago)
Author:
rvelices
Message:

feature 519: quick search (first version)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/functions_search.inc.php

    r1119 r1537  
    7474 * need to be transformed into an SQL clause to be used in queries.
    7575 *
    76  * @param int search_id
     76 * @param array search
    7777 * @return string
    7878 */
    79 function get_sql_search_clause($search_id)
    80 {
    81   $search = get_search_array($search_id);
    82 
     79function get_sql_search_clause($search)
     80{
    8381  // SQL where clauses are stored in $clauses array during query
    8482  // construction
     
    213211
    214212/**
    215  * returns the list of items corresponding to the search id
    216  *
    217  * @param int search id
     213 * returns the list of items corresponding to the advanced search array
     214 *
     215 * @param array search
    218216 * @return array
    219217 */
    220 function get_search_items($search_id)
     218function get_regular_search_results($search)
    221219{
    222220  $items = array();
    223  
    224   $search_clause = get_sql_search_clause($search_id);
    225  
     221
     222  $search_clause = get_sql_search_clause($search);
     223
    226224  if (!empty($search_clause))
    227225  {
     
    270268    }
    271269  }
    272  
     270
    273271  return $items;
    274272}
     273
     274
     275if (!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
     292function 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 */
     328function 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 = '
     338SELECT 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 = '
     345SELECT 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 = '
     358SELECT
     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
     366FROM (
     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
     375WHERE category_id NOT IN ('.$user['forbidden_categories'].')
     376GROUP BY i.id';
     377
     378  $query = 'SELECT id, MATCH(ft) AGAINST( "'.$q.'" IN BOOLEAN MODE) AS q FROM ('.$query.') AS Y
     379WHERE 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 = '
     411SELECT 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 */
     434function 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}
    275447?>
Note: See TracChangeset for help on using the changeset viewer.