Ignore:
Timestamp:
Feb 19, 2007, 5:25:47 PM (17 years ago)
Author:
rvelices
Message:

web service: added method to query search elements
picture: small correction on my last commit
monthly calendar nice view: always use getimagesize instead of guessing the size
query search: improved results (filename is searched separately) and sometimes less sql queries than before

File:
1 edited

Legend:

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

    r1744 r1837  
    33// | PhpWebGallery - a PHP based picture gallery                           |
    44// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
    5 // | Copyright (C) 2003-2006 PhpWebGallery Team - http://phpwebgallery.net |
     5// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
    66// +-----------------------------------------------------------------------+
    7 // | branch        : BSF (Best So Far)
    87// | file          : $Id$
    98// | last update   : $Date$
     
    302301function get_quick_search_results($q)
    303302{
    304   global $user, $page, $filter;
     303  global $page;
    305304  $search_results = array();
    306 
    307   // first search tag names corresponding to the query $q. we could also search
    308   // tags later during the big join, but for the sake of the performance and
    309   // because tags have only a simple name we do it separately
    310   $q_like_clause = get_qsearch_like_clause($q, 'CONVERT(name, CHAR)' );
    311   $by_tag_weights=array();
    312   if (!empty($q_like_clause))
    313   {
    314     $query = '
    315 SELECT id
    316   FROM '.TAGS_TABLE.'
    317   WHERE '.$q_like_clause;
    318     $tag_ids = array_from_query( $query, 'id');
    319     if (!empty($tag_ids))
    320     { // we got some tags
    321       $query = '
    322 SELECT image_id, COUNT(tag_id) AS q
    323   FROM '.IMAGE_TAG_TABLE.'
    324   WHERE tag_id IN ('.implode(',',$tag_ids).')
    325   GROUP BY image_id';
    326       $result = pwg_query($query);
    327       while ($row = mysql_fetch_assoc($result))
    328       { // weight is important when sorting images by relevance
    329         $by_tag_weights[(int)$row['image_id']] = $row['q'];
    330       }
    331     }
    332   }
    333 
     305  $q = trim($q);
     306  if (empty($q))
     307  {
     308    $search_results['items'] = array();
     309    return $search_results;
     310  }
    334311  // prepare the big join on images, comments and categories
    335312  $query = '
    336313SELECT
    337   i.id, i.file, CAST( CONCAT_WS(" ",
     314  i.id, CAST( CONCAT_WS(" ",
    338315    IFNULL(i.name,""),
    339316    IFNULL(i.comment,""),
     
    357334        'forbidden_categories' => 'category_id',
    358335        'visible_categories' => 'category_id',
    359         'visible_images' => 'ic.image_id'
     336        'visible_images' => 'i.id'
    360337      ),
    361338    'WHERE'
     
    366343WHERE MATCH(ft) AGAINST( "'.$q.'" IN BOOLEAN MODE)';
    367344
    368   //also inlcude the file name (but avoid full text which is slower because
    369   //the filename in pwg doesn't have spaces so full text is meaningless anyway)
    370   $q_like_clause = get_qsearch_like_clause($q, 'file' );
    371   if (! empty($q_like_clause) )
    372   {
    373     $query .= ' OR '.$q_like_clause;
    374   }
    375 
    376345  $by_weights=array();
    377346  $result = pwg_query($query);
    378347  while ($row = mysql_fetch_array($result))
    379   {
    380     $by_weights[(int)$row['id']] = $row['q'] ? $row['q'] : 0;
    381   }
    382 
    383   // finally merge the results (tags and big join) sorted by "relevance"
    384   foreach ( $by_weights as $image=>$w )
    385   {
    386     $by_tag_weights[$image] = 2*$w+ (isset($by_tag_weights[$image])?$by_tag_weights[$image]:0);
     348  { // weight is important when sorting images by relevance
     349    if ($row['q'])
     350    {
     351      $by_weights[(int)$row['id']] =  2*$row['q'];
     352    }
     353  }
     354
     355  $permissions_checked = true;
     356  // now search the file name separately (not done in full text because slower
     357  // and the filename in pwg doesn't have spaces so full text is meaningless )
     358  $q_like_clause = get_qsearch_like_clause($q, 'file' );
     359  if (!empty($q_like_clause))
     360  {
     361    $query = '
     362SELECT id
     363  FROM '.IMAGES_TABLE.'
     364  WHERE '.$q_like_clause.
     365      get_sql_condition_FandF
     366      (
     367        array
     368          (
     369            'visible_images' => 'id'
     370          ),
     371        'AND'
     372      );
     373    $result = pwg_query($query);
     374    while ($row = mysql_fetch_assoc($result))
     375    { // weight is important when sorting images by relevance
     376      $id=(int)$row['id'];
     377      @$by_weights[$id] += 2;
     378      $permissions_checked = false;
     379    }
     380  }
     381
     382  // now search tag names corresponding to the query $q. we could have searched
     383  // tags earlier during the big join, but for the sake of the performance and
     384  // because tags have only a simple name we do it separately
     385  $q_like_clause = get_qsearch_like_clause($q, 'CONVERT(name, CHAR)' );
     386  if (!empty($q_like_clause))
     387  {
     388    $query = '
     389SELECT id
     390  FROM '.TAGS_TABLE.'
     391  WHERE '.$q_like_clause;
     392    $tag_ids = array_from_query( $query, 'id');
     393    if (!empty($tag_ids))
     394    { // we got some tags
     395      $query = '
     396SELECT image_id, COUNT(tag_id) AS q
     397  FROM '.IMAGE_TAG_TABLE.'
     398  WHERE tag_id IN ('.implode(',',$tag_ids).')
     399  GROUP BY image_id';
     400      $result = pwg_query($query);
     401      while ($row = mysql_fetch_assoc($result))
     402      { // weight is important when sorting images by relevance
     403        $image_id=(int)$row['image_id'];
     404        @$by_weights[$image_id] += $row['q'];
     405        $permissions_checked = false;
     406      }
     407    }
    387408  }
    388409
    389410  //at this point, found images might contain images not allowed for the user
    390   if ( empty($by_tag_weights) or isset($page['super_order_by']) )
    391   {
    392     // no aditionnal query here for permissions (will be done by section_init
    393     // while sorting items as the user requested it)
    394     $search_results['items'] = array_keys($by_tag_weights);
    395   }
    396   else
     411  if ( !$permissions_checked
     412       and !empty($by_weights)
     413       and !isset($page['super_order_by']) )
    397414  {
    398415    // before returning the result "as is", make sure the user has the
     
    402419  FROM '.IMAGES_TABLE.'
    403420    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
    404   WHERE id IN ('.implode(',', array_keys($by_tag_weights) ).')
     421  WHERE id IN ('.implode(',', array_keys($by_weights) ).')
    405422'.get_sql_condition_FandF
    406423  (
     
    409426        'forbidden_categories' => 'category_id',
    410427        'visible_categories' => 'category_id',
    411         'visible_images' => 'ic.image_id'
     428        'visible_images' => 'id'
    412429      ),
    413430    'AND'
    414431  );
    415432    $allowed_image_ids = array_from_query( $query, 'id');
    416     $by_tag_weights = array_intersect_key($by_tag_weights, array_flip($allowed_image_ids));
    417     arsort($by_tag_weights, SORT_NUMERIC);
    418     $search_results = array(
    419           'items'=>array_keys($by_tag_weights),
    420           'as_is'=>1
    421         );
    422   }
     433    $by_weights = array_intersect_key($by_weights, array_flip($allowed_image_ids));
     434    $permissions_checked = true;
     435  }
     436  arsort($by_weights, SORT_NUMERIC);
     437  if ( $permissions_checked )
     438  {
     439    $search_results['as_is']=1;
     440  }
     441 
     442  $search_results['items'] = array_keys($by_weights);
    423443  return $search_results;
    424444}
Note: See TracChangeset for help on using the changeset viewer.