Changeset 2451


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)
Location:
trunk/include
Files:
4 edited

Legend:

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

    r2371 r2451  
    667667    'popuphelp' => array('used' => false),
    668668    'profile' => array('used' => false),
    669     'web_service' => array('used' => false),
    670669    'ws' => array('used' => false),
    671670    'identification' => array('cancel' => true),
  • 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}
  • trunk/include/section_init.inc.php

    r2430 r2451  
    333333    include_once( PHPWG_ROOT_PATH .'include/functions_search.inc.php' );
    334334
    335     $search_result = get_search_results($page['search']);
    336     if ( !empty($search_result['items']) and !isset($search_result['as_is']) )
    337     {
    338       $query = '
    339 SELECT DISTINCT(id)
    340   FROM '.IMAGES_TABLE.'
    341     INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
    342   WHERE id IN ('.implode(',', $search_result['items']).')
    343     '.$forbidden.'
    344   '.$conf['order_by'].'
    345 ;';
    346       $page['items'] = array_from_query($query, 'id');
    347     }
    348     else
    349     {
    350       $page['items'] = $search_result['items'];
    351       if ( isset($search_result['qs']) )
    352       {//save the details of the query search
    353         $page['qsearch_details'] = $search_result['qs'];
    354       }
    355     }
    356 
    357     $page = array_merge(
    358       $page,
    359       array(
     335    $search_result = get_search_results($page['search'], @$page['super_order_by'] );
     336    if ( isset($search_result['qs']) )
     337    {//save the details of the query search
     338      $page['qsearch_details'] = $search_result['qs'];
     339    }
     340
     341    $page = array_merge(
     342      $page,
     343      array(
     344        'items' => $search_result['items'],
    360345        'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">'
    361346                  .l10n('search_result').'</a>',
     
    379364    array
    380365      (
    381         'visible_images' => 'image_id'
     366        'visible_images' => 'id'
    382367      ),
    383368    'AND'
  • trunk/include/ws_functions.inc.php

    r2435 r2451  
    824824  $order_by = ws_std_image_sql_order($params, 'i.');
    825825
     826  $super_order_by = false;
    826827  if ( !empty($order_by) )
    827828  {
    828829    global $conf;
    829830    $conf['order_by'] = 'ORDER BY '.$order_by;
    830     $page['super_order_by']=1; // quick_search_result might be faster
     831    $super_order_by=true; // quick_search_result might be faster
    831832  }
    832833
    833834  $search_result = get_quick_search_results($params['query'],
    834     implode(',', $where_clauses) );
    835   $image_ids = $search_result['items'];
    836 
    837   $image_ids = array_slice($image_ids,
    838     $params['page']*$params['per_page'],
    839     $params['per_page'] );
     835      $super_order_by,
     836      implode(',', $where_clauses)
     837    );
     838
     839  $image_ids = array_slice(
     840      $search_result['items'],
     841      $params['page']*$params['per_page'],
     842      $params['per_page']
     843    );
    840844
    841845  if ( count($image_ids) )
     
    843847    $query = '
    844848SELECT * FROM '.IMAGES_TABLE.'
    845   WHERE id IN ('
    846         .wordwrap(implode(', ', $image_ids), 80, "\n")
    847         .')';
    848 
     849  WHERE id IN ('.implode(',', $image_ids).')';
     850
     851    $image_ids = array_flip($image_ids);
    849852    $result = pwg_query($query);
    850853    while ($row = mysql_fetch_assoc($result))
     
    863866      }
    864867      $image = array_merge( $image, ws_std_get_urls($row) );
    865       array_push($images, $image);
    866     }
    867 
    868     $image_ids = array_flip($image_ids);
    869     usort(
    870         $images,
    871         create_function('$i1,$i2', 'global $image_ids; return $image_ids[$i1["id"]]-$image_ids[$i2["id"]];')
    872       );
     868      $images[$image_ids[$image['id']]] = $image;
     869    }
     870    ksort($images, SORT_NUMERIC);
     871    $images = array_values($images);
    873872  }
    874873
Note: See TracChangeset for help on using the changeset viewer.