Changeset 1619


Ignore:
Timestamp:
Nov 29, 2006, 2:50:39 AM (17 years ago)
Author:
rvelices
Message:

added some code comments for quick/query search

File:
1 edited

Legend:

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

    r1618 r1619  
    287287}
    288288
    289 
     289/**
     290 * returns the LIKE sql clause corresponding to the quick search query $q
     291 * and the field $field. example q="john bill", field="file" will return
     292 * file LIKE "%john%" OR file LIKE "%bill%". Special characters for MySql
     293 * full text search (+,<,>) are omitted.
     294 * @param string q
     295 * @param string field
     296 * @return string
     297 */
    290298function get_qsearch_like_clause($q, $field)
    291299{
     
    319327
    320328/**
    321  * returns the search results corresponding to a quick search
     329 * returns the search results (array of image ids) corresponding to a
     330 * quick/query search. A quick/query search returns many items (search is
     331 * not strict), but results are sorted by relevance.
    322332 *
    323333 * @param string q
     
    329339  $search_results = array();
    330340
     341  // first search tag names corresponding to the query $q. we could also search
     342  // tags later during the big join, but for the sake of the performance and
     343  // because tags have only a simple name we do it separately
    331344  $q_like_clause = get_qsearch_like_clause($q, 'CONVERT(name, CHAR)' );
    332345  $by_tag_weights=array();
     
    339352    $tag_ids = array_from_query( $query, 'id');
    340353    if (!empty($tag_ids))
    341     {
     354    { // we got some tags
    342355      $query = '
    343356SELECT image_id, COUNT(tag_id) AS q
     
    346359  GROUP BY image_id';
    347360      $result = pwg_query($query);
    348       while ($row = mysql_fetch_array($result))
    349       {
     361      while ($row = mysql_fetch_assoc($result))
     362      { // weight is important when sorting images by relevance
    350363        $by_tag_weights[(int)$row['image_id']] = $row['q'];
    351364      }
     
    353366  }
    354367
     368  // prepare the big join on images, comments and categories
    355369  $query = '
    356370SELECT
     
    377391WHERE MATCH(ft) AGAINST( "'.$q.'" IN BOOLEAN MODE)';
    378392
     393  //also inlcude the file name (but avoid full text which is slower because
     394  //the filename in pwg doesn't have spaces so full text is meaningless anyway)
    379395  $q_like_clause = get_qsearch_like_clause($q, 'file' );
    380396  if (! empty($q_like_clause) )
     
    390406  }
    391407
     408  // finally merge the results (tags and big join) sorted by "relevance"
    392409  foreach ( $by_weights as $image=>$w )
    393410  {
     
    395412  }
    396413
     414  //at this point, found images might contain images not allowed for the user
    397415  if ( empty($by_tag_weights) or isset($page['super_order_by']) )
    398416  {
    399     if (! isset($page['super_order_by']) )
    400     {
    401       arsort($by_tag_weights, SORT_NUMERIC);
    402       $search_results['as_is']=1;
    403     }
     417    // no aditionnal query here for permissions (will be done by section_init
     418    // while sorting items as the user requested it)
    404419    $search_results['items'] = array_keys($by_tag_weights);
    405420  }
    406421  else
    407422  {
     423    // before returning the result "as is", make sure the user has the
     424    // permissions for every item
    408425    $query = '
    409426SELECT DISTINCT(id)
Note: See TracChangeset for help on using the changeset viewer.