Changeset 1837 for trunk/include


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

Location:
trunk/include
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/calendar_monthly.class.php

    r1597 r1837  
    22// +-----------------------------------------------------------------------+
    33// | PhpWebGallery - a PHP based picture gallery                           |
    4 // | Copyright (C) 2003-2006 PhpWebGallery Team - http://phpwebgallery.net |
     4// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
    55// +-----------------------------------------------------------------------+
    66// | file          : $Id$
     
    439439      else
    440440      {
    441         // first try to guess thumbnail size
     441/*        // first try to guess thumbnail size
    442442        if ( !empty($items[$day]['width']) )
    443443        {
     
    446446                 $conf['tn_width'], $conf['tn_height'] );
    447447        }
    448         else
     448        else*/
    449449        {// item not an image (tn is either mime type or an image)
    450450          $thumb = get_thumbnail_path($items[$day]);
  • trunk/include/config_default.inc.php

    r1790 r1837  
    116116// given month. a value of 0 means that the pretty view is not shown.
    117117// a good suggestion would be to have the width and the height equal
    118 // and smaller than tn_width and tn_height. NOTE THAT tn_width AND tn_height
    119 // MUST CORRESPOND APPROXIMATIVELY TO YOUR REAL THUMBNAIL SIZE, OTHERWISE
    120 // THE IMAGES WILL NOT SHOW CORRECTLY
     118// and smaller than tn_width and tn_height.
    121119$conf['calendar_month_cell_width'] =80;
    122120$conf['calendar_month_cell_height']=80;
  • 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}
  • trunk/include/ws_functions.inc.php

    r1820 r1837  
    604604}
    605605
     606/**
     607 * returns a list of elements corresponding to a query search
     608 */
     609function ws_images_search($params, &$service)
     610{
     611  global $page;
     612  $images = array();
     613  include_once( PHPWG_ROOT_PATH .'include/functions_search.inc.php' );
     614  include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php');
     615
     616  $where_clauses = ws_std_image_sql_filter( $params );
     617  $order_by = ws_std_image_sql_order($params);
     618
     619  if ( !empty($where_clauses) and !empty($order_by) )
     620  {
     621    $page['super_order_by']=1; // quick_search_result might be faster
     622  }
     623  $search_result = get_quick_search_results($params['query']);
     624
     625  global $image_ids; //needed for sorting by rank (usort)
     626  if ( ( !isset($search_result['as_is'])
     627      or !empty($where_clauses)
     628      or !empty($order_by) )
     629      and !empty($search_result['items']) )
     630  {
     631    $where_clauses[] = 'id IN ('
     632        .wordwrap(implode(', ', $search_result['items']), 80, "\n")
     633        .')';
     634    $where_clauses[] = get_sql_condition_FandF(
     635        array
     636          (
     637            'forbidden_categories' => 'category_id',
     638            'visible_categories' => 'category_id',
     639            'visible_images' => 'id'
     640          ),
     641        '', true
     642      );
     643    $query = '
     644SELECT DISTINCT id FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id=image_id
     645  WHERE '.implode('
     646    AND ', $where_clauses);
     647    if (!empty($order_by))
     648    {
     649      $query .= '
     650  ORDER BY '.$order_by;
     651    }
     652    $image_ids = array_from_query($query, 'id');
     653    global $ranks;
     654    $ranks = array_flip( $search_result['items'] );
     655    usort(
     656      $image_ids,
     657      create_function('$i1,$i2', 'global $ranks; return $ranks[$i1]-$ranks[$i2];')
     658    );
     659    unset ($ranks);
     660  }
     661  else
     662  {
     663    $image_ids = $search_result['items'];
     664  }
     665 
     666  $image_ids = array_slice($image_ids,
     667    $params['page']*$params['per_page'],
     668    $params['per_page'] );
     669
     670  if ( count($image_ids) )
     671  {
     672    $query = '
     673SELECT * FROM '.IMAGES_TABLE.'
     674  WHERE id IN ('
     675        .wordwrap(implode(', ', $image_ids), 80, "\n")
     676        .')';
     677
     678    $result = pwg_query($query);
     679    while ($row = mysql_fetch_assoc($result))
     680    {
     681      $image = array();
     682      foreach ( array('id', 'width', 'height', 'hit') as $k )
     683      {
     684        if (isset($row[$k]))
     685        {
     686          $image[$k] = (int)$row[$k];
     687        }
     688      }
     689      foreach ( array('name', 'file') as $k )
     690      {
     691        $image[$k] = $row[$k];
     692      }
     693      $image = array_merge( $image, ws_std_get_urls($row) );
     694      array_push($images, $image);
     695    }
     696
     697    $image_ids = array_flip($image_ids);
     698    usort(
     699        $images,
     700        create_function('$i1,$i2', 'global $image_ids; return $image_ids[$i1["id"]]-$image_ids[$i2["id"]];')
     701      );
     702  }
     703
     704
     705  return array( 'images' =>
     706    array (
     707      WS_XML_ATTRIBUTES =>
     708        array(
     709            'page' => $params['page'],
     710            'per_page' => $params['per_page'],
     711            'count' => count($images)
     712          ),
     713       WS_XML_CONTENT => new PwgNamedArray($images, 'image',
     714          array('id', 'tn_url', 'element_url', 'file','width','height','hit') )
     715      )
     716    );
     717}
    606718
    607719/**
Note: See TracChangeset for help on using the changeset viewer.