Skip to content

Commit

Permalink
bug 3056 quick search - small fixes & improvements
Browse files Browse the repository at this point in the history
- scopes are case insesitive date:2013 and Date:2013 are the same thing
- use strict comparison in scopes when required e.g. date:<2013 is not matching year 2013
- allow scopes (in plugins) to overwrite behaviour of space characters ...

git-svn-id: http://piwigo.org/svn/trunk@28992 68402e56-0260-453c-a942-63ccdbb3a9ee
  • Loading branch information
rvelices committed Jul 7, 2014
1 parent 45887f5 commit d181101
Showing 1 changed file with 46 additions and 19 deletions.
65 changes: 46 additions & 19 deletions include/functions_search.inc.php
Expand Up @@ -295,6 +295,11 @@ function parse($token)
return false;
return true;
}

function process_char(&$ch, &$crt_token)
{
return false;
}
}

class QNumericRangeScope extends QSearchScope
Expand All @@ -309,12 +314,19 @@ function __construct($id, $aliases, $nullable=false, $epsilon=0)
function parse($token)
{
$str = $token->term;
$strict = array(0,0);
if ( ($pos = strpos($str, '..')) !== false)
$range = array( substr($str,0,$pos), substr($str, $pos+2));
elseif ('>' == @$str[0])// ratio:>1
{
$range = array( substr($str,1), '');
$strict[0] = 1;
}
elseif ('<' == @$str[0]) // size:<5mp
{
$range = array('', substr($str,1));
$strict[1] = 1;
}
elseif( ($token->modifier & QST_WILDCARD_BEGIN) )
$range = array('', $str);
elseif( ($token->modifier & QST_WILDCARD_END) )
Expand Down Expand Up @@ -349,7 +361,7 @@ function parse($token)
$val = '';
if (is_numeric($val))
{
if ($i)
if ($i ^ $strict[$i])
$val += $this->epsilon;
else
$val -= $this->epsilon;
Expand All @@ -358,17 +370,17 @@ function parse($token)

if (!$this->nullable && $range[0]=='' && $range[1] == '')
return false;
$token->scope_data = $range;
$token->scope_data = array( 'range'=>$range, 'strict'=>$strict );
return true;
}

function get_sql($field, $token)
{
$clauses = array();
if ($token->scope_data[0]!=='')
$clauses[] = $field.' >= ' .$token->scope_data[0].' ';
if ($token->scope_data[1]!=='')
$clauses[] = $field.' <= ' .$token->scope_data[1].' ';
if ($token->scope_data['range'][0]!=='')
$clauses[] = $field.' >'.($token->scope_data['strict'][0]?'':'=').$token->scope_data['range'][0].' ';
if ($token->scope_data['range'][1]!=='')
$clauses[] = $field.' <'.($token->scope_data['strict'][1]?'':'=').$token->scope_data['range'][1].' ';

if (empty($clauses))
{
Expand All @@ -392,12 +404,19 @@ function __construct($id, $aliases, $nullable=false)
function parse($token)
{
$str = $token->term;
$strict = array(0,0);
if ( ($pos = strpos($str, '..')) !== false)
$range = array( substr($str,0,$pos), substr($str, $pos+2));
elseif ('>' == @$str[0])
{
$range = array( substr($str,1), '');
$strict[0] = 1;
}
elseif ('<' == @$str[0])
{
$range = array('', substr($str,1));
$strict[1] = 1;
}
elseif( ($token->modifier & QST_WILDCARD_BEGIN) )
$range = array('', $str);
elseif( ($token->modifier & QST_WILDCARD_END) )
Expand All @@ -411,10 +430,12 @@ function parse($token)
{
array_shift($matches);
if (!isset($matches[1]))
$matches[1] = !$i ? 1 : 12;
$matches[1] = ($i ^ $strict[$i]) ? 12 : 1;
if (!isset($matches[2]))
$matches[2] = !$i ? 1 : 31;
$val = $matches;
$matches[2] = ($i ^ $strict[$i]) ? 31 : 1;
$val = implode('-', $matches);
if ($i ^ $strict[$i])
$val .= ' 23:59:59';
}
elseif (strlen($val))
return false;
Expand All @@ -431,9 +452,9 @@ function get_sql($field, $token)
{
$clauses = array();
if ($token->scope_data[0]!=='')
$clauses[] = $field.' >= \'' . implode('-',$token->scope_data[0]).'\'';
$clauses[] = $field.' >= \'' . $token->scope_data[0].'\'';
if ($token->scope_data[1]!=='')
$clauses[] = $field.' <= \'' . implode('-',$token->scope_data[1]).' 23:59:59\'';
$clauses[] = $field.' <= \'' . $token->scope_data[1].'\'';

if (empty($clauses))
{
Expand Down Expand Up @@ -578,7 +599,7 @@ protected function parse_expression($q, &$qi, $level, $root)
$stop = true;
break;
case ':':
$scope = @$root->scopes[$crt_token];
$scope = @$root->scopes[strtolower($crt_token)];
if (!isset($scope) || isset($crt_scope))
{ // white space
$this->push($crt_token, $crt_modifier, $crt_scope);
Expand Down Expand Up @@ -620,12 +641,15 @@ protected function parse_expression($q, &$qi, $level, $root)
}
// else white space go on..
default:
if (strpos(' ,.;!?', $ch)!==false)
{ // white space
$this->push($crt_token, $crt_modifier, $crt_scope);
if (!$crt_scope || !$crt_scope->process_char($ch, $crt_token))
{
if (strpos(' ,.;!?', $ch)!==false)
{ // white space
$this->push($crt_token, $crt_modifier, $crt_scope);
}
else
$crt_token .= $ch;
}
else
$crt_token .= $ch;
break;
}
}
Expand Down Expand Up @@ -1117,7 +1141,10 @@ function get_quick_search_results($q, $options)

$res = get_quick_search_results_no_cache($q, $options);

$persistent_cache->set($cache_key, $res, 300);
if ( count($res['items']) )
{// cache the results only if not empty - otherwise it is useless
$persistent_cache->set($cache_key, $res, 300);
}
return $res;
}

Expand All @@ -1127,7 +1154,7 @@ function get_quick_search_results($q, $options)
function get_quick_search_results_no_cache($q, $options)
{
global $conf;
//@TODO: maybe cache for 10 minutes the result set to avoid many expensive sql calls when navigating the pictures

$q = trim(stripslashes($q));
$search_results =
array(
Expand Down

0 comments on commit d181101

Please sign in to comment.