Ignore:
Timestamp:
Jul 23, 2008, 2:56:22 AM (16 years ago)
Author:
rvelices
Message:
  • normalize behaviour of query search versus std search (now both return items already sorted and permission checked); also more optimized sql queries (in some cases)
File:
1 edited

Legend:

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

    r2299 r2451  
    195195 * @return array
    196196 */
    197 function get_regular_search_results($search)
     197function get_regular_search_results($search, $images_where)
    198198{
     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
    199210  $items = array();
    200 
    201   $search_clause = get_sql_search_clause($search);
    202 
    203   if (!empty($search_clause))
    204   {
    205     $query = '
    206 SELECT DISTINCT(id)
    207   FROM '.IMAGES_TABLE.'
    208     INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
    209   WHERE '.$search_clause.'
    210 ;';
    211     $items = array_from_query($query, 'id');
    212   }
     211  $tag_items = array();
    213212
    214213  if (isset($search['fields']['tags']))
     
    218217      $search['fields']['tags']['mode']
    219218      );
    220 
     219  }
     220
     221  $search_clause = get_sql_search_clause($search);
     222
     223  if (!empty($search_clause))
     224  {
     225    $query = '
     226SELECT DISTINCT(id)
     227  FROM '.IMAGES_TABLE.' i
     228    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
     229  WHERE '.$search_clause;
     230    if (!empty($images_where))
     231    {
     232      $query .= "\n  AND ".$images_where;
     233    }
     234    if (empty($tag_items) or $search['mode']=='AND')
     235    { // directly use forbidden and order by
     236      $query .= $forbidden.'
     237  '.$conf['order_by'];
     238    }
     239    $items = array_from_query($query, 'id');
     240  }
     241
     242  if ( !empty($tag_items) )
     243  {
     244    $need_permission_check = false;
    221245    switch ($search['mode'])
    222246    {
    223247      case 'AND':
    224       {
    225248        if (empty($search_clause))
    226249        {
     250          $need_permission_check = true;
    227251          $items = $tag_items;
    228252        }
     
    232256        }
    233257        break;
    234       }
    235258      case 'OR':
    236       {
     259        $before_count = count($items);
    237260        $items = array_unique(
    238261          array_merge(
     
    241264            )
    242265          );
     266        if ( $before_count < count($items) )
     267        {
     268          $need_permission_check = true;
     269        }
    243270        break;
     271    }
     272    if ($need_permission_check and count($items) )
     273    {
     274      $query = '
     275SELECT DISTINCT(id)
     276  FROM '.IMAGES_TABLE.' i
     277    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
     278  WHERE id IN ('.implode(',', $items).') '.$forbidden;
     279      if (!empty($images_where))
     280      {
     281        $query .= "\n  AND ".$images_where;
    244282      }
     283      $query .= '
     284  '.$conf['order_by'];
     285      $items = array_from_query($query, 'id');
    245286    }
    246287  }
     
    355396 * returns the search results corresponding to a quick/query search.
    356397 * A quick/query search returns many items (search is not strict), but results
    357  * are sorted by relevance unless $page['super_order_by'] is set. Returns:
     398 * are sorted by relevance unless $super_order_by is true. Returns:
    358399 * array (
    359400 * 'items' => array(85,68,79...)
    360  * 'as_is' => 1 (indicates the caller that items are ordered and permissions checked
    361401 * 'qs'    => array(
    362402 *    'matching_tags' => array of matching tags
     
    366406 *
    367407 * @param string q
     408 * @param bool super_order_by
    368409 * @param string images_where optional aditional restriction on images table
    369410 * @return array
    370411 */
    371 function get_quick_search_results($q, $images_where='')
     412function get_quick_search_results($q, $super_order_by, $images_where='')
    372413{
    373   global $page;
    374414  $search_results =
    375415    array(
    376416      'items' => array(),
    377       'as_is' => 1,
    378417      'qs' => array('q'=>stripslashes($q)),
    379418    );
     
    519558  $allowed_images = array_from_query( $query, 'id');
    520559
    521   if ( isset($page['super_order_by']) or empty($by_weights) )
     560  if ( $super_order_by or empty($by_weights) )
    522561  {
    523562    $search_results['items'] = $allowed_images;
     
    545584 * @return array
    546585 */
    547 function get_search_results($search_id, $images_where='')
     586function get_search_results($search_id, $super_order_by, $images_where='')
    548587{
    549588  $search = get_search_array($search_id);
    550589  if ( !isset($search['q']) )
    551590  {
    552     $result['items'] = get_regular_search_results($search);
     591    $result['items'] = get_regular_search_results($search, $images_where);
    553592    return $result;
    554593  }
    555594  else
    556595  {
    557     return get_quick_search_results($search['q'], $images_where);
     596    return get_quick_search_results($search['q'], $super_order_by, $images_where);
    558597  }
    559598}
Note: See TracChangeset for help on using the changeset viewer.