Ignore:
Timestamp:
Apr 5, 2014, 8:26:20 AM (10 years ago)
Author:
rvelices
Message:

bug 3056: quick search - fixes and date range scopes (posted:2003 posted:>2003 posted:*2003 posted:2003-05..20050312 etc)

File:
1 edited

Legend:

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

    r28066 r28080  
    303303  var $aliases;
    304304  var $is_text;
    305   var $allow_empty;
    306 
    307   function __construct($id, $aliases, $allow_empty=false, $is_text=true)
     305  var $nullable;
     306
     307  function __construct($id, $aliases, $nullable=false, $is_text=true)
    308308  {
    309309    $this->id = $id;
    310310    $this->aliases = $aliases;
    311311    $this->is_text = $is_text;
    312     $this->allow_empty =$allow_empty;
     312    $this->nullable =$nullable;
     313  }
     314
     315  function parse($token)
     316  {
     317    if (!$this->nullable && 0==strlen($token->term))
     318      return false;
     319    return true;
    313320  }
    314321}
     
    317324{
    318325  private $epsilon;
    319   function __construct($id, $aliases, $allow_empty=false, $epsilon=0)
    320   {
    321     parent::__construct($id, $aliases, $allow_empty, false);
     326  function __construct($id, $aliases, $nullable=false, $epsilon=0)
     327  {
     328    parent::__construct($id, $aliases, $nullable, false);
    322329    $this->epsilon = $epsilon;
    323330  }
     
    328335    if ( ($pos = strpos($str, '..')) !== false)
    329336      $range = array( substr($str,0,$pos), substr($str, $pos+2));
     337    elseif ('>' == @$str[0])// ratio:>1
     338      $range = array( substr($str,1), '');
     339    elseif ('<' == @$str[0]) // size:<5mp
     340      $range = array('', substr($str,1));
     341    elseif( ($token->modifier & QST_WILDCARD_BEGIN) )
     342      $range = array('', $str);
     343    elseif( ($token->modifier & QST_WILDCARD_END) )
     344      $range = array($str, '');
    330345    else
    331     {
    332       if ('>' == @$str[0])// ratio:>1
    333         $range = array( substr($str,1), '');
    334       elseif ('<' == @$str[0]) // size:<5mp
    335         $range = array('', substr($str,1));
    336       else
    337         $range = array($str, $str);
    338     }
     346      $range = array($str, $str);
    339347
    340348    foreach ($range as $i =>&$val)
     
    372380    }
    373381
    374     if (!$this->allow_empty && $range[0]=='' && $range[1] == '')
     382    if (!$this->nullable && $range[0]=='' && $range[1] == '')
    375383      return false;
    376384    $token->scope_data = $range;
     
    387395
    388396    if (empty($clauses))
    389       return $field.' IS NULL';
     397    {
     398      if ($token->modifier & QST_WILDCARD)
     399        return $field.' IS NOT NULL';
     400      else
     401        return $field.' IS NULL';
     402    }
     403    return '('.implode(' AND ', $clauses).')';
     404  }
     405}
     406
     407
     408class QDateRangeScope extends QSearchScope
     409{
     410  function __construct($id, $aliases, $nullable=false)
     411  {
     412    parent::__construct($id, $aliases, $nullable, false);
     413  }
     414
     415  function parse($token)
     416  {
     417    $str = $token->term;
     418    if ( ($pos = strpos($str, '..')) !== false)
     419      $range = array( substr($str,0,$pos), substr($str, $pos+2));
     420    elseif ('>' == @$str[0])
     421      $range = array( substr($str,1), '');
     422    elseif ('<' == @$str[0])
     423      $range = array('', substr($str,1));
     424    elseif( ($token->modifier & QST_WILDCARD_BEGIN) )
     425      $range = array('', $str);
     426    elseif( ($token->modifier & QST_WILDCARD_END) )
     427      $range = array($str, '');
     428    else
     429      $range = array($str, $str);
     430
     431    foreach ($range as $i =>&$val)
     432    {
     433      if (preg_match('/([0-9]{4})-?((?:0?[0-9])|(?:1[0-2]))?-?(((?:0?[0-9])|(?:[1-3][0-9])))?/', $val, $matches))
     434      {
     435        array_shift($matches);
     436        if (!isset($matches[1]))
     437          $matches[1] = !$i ? 1 : 12;
     438        if (!isset($matches[2]))
     439          $matches[2] = !$i ? 1 : 31;
     440        $val = $matches;
     441      }
     442      elseif (strlen($val))
     443        return false;
     444    }
     445
     446    if (!$this->nullable && $range[0]=='' && $range[1] == '')
     447      return false;
     448
     449    $token->scope_data = $range;
     450    return true;
     451  }
     452
     453  function get_sql($field, $token)
     454  {
     455    $clauses = array();
     456    if ($token->scope_data[0]!=='')
     457      $clauses[] = $field.' >= \'' . implode('-',$token->scope_data[0]).'\'';
     458    if ($token->scope_data[1]!=='')
     459      $clauses[] = $field.' <= \'' . implode('-',$token->scope_data[1]).' 23:59:59\'';
     460
     461    if (empty($clauses))
     462    {
     463      if ($token->modifier & QST_WILDCARD)
     464        return $field.' IS NOT NULL';
     465      else
     466        return $field.' IS NULL';
     467    }
    390468    return '('.implode(' AND ', $clauses).')';
    391469  }
     
    472550  private function push(&$token, &$modifier, &$scope)
    473551  {
    474     if (strlen($token) || (isset($scope) && $scope->allow_empty))
     552    if (strlen($token) || (isset($scope) && $scope->nullable))
    475553    {
    476554      $this->tokens[] = new QSingleToken($token, $modifier, $scope);
     
    559637            if (preg_match('/[\s,.;!\?]+/', $ch))
    560638            { // white space
    561               if (strlen($crt_token))
    562                 $this->push($crt_token, $crt_modifier, $crt_scope);
    563               $crt_modifier = 0;
     639              $this->push($crt_token, $crt_modifier, $crt_scope);
    564640            }
    565641            else
     
    592668      if ($token->is_single)
    593669      {
    594         if (!isset($token->scope))
    595         {
    596           if ( ($token->modifier & QST_QUOTED)==0 )
     670        if ( ($token->modifier & QST_QUOTED)==0
     671          && substr($token->term, -1)=='*' )
     672        {
     673          $token->term = rtrim($token->term, '*');
     674          $token->modifier |= QST_WILDCARD_END;
     675        }
     676
     677        if ( !isset($token->scope)
     678          && ($token->modifier & (QST_QUOTED|QST_WILDCARD))==0 )
     679        {
     680          if ('not' == strtolower($token->term))
    597681          {
    598             if ('not' == strtolower($token->term))
    599             {
    600               if ($i+1 < count($this->tokens))
    601                 $this->tokens[$i+1]->modifier |= QST_NOT;
    602               $token->term = "";
    603             }
    604             if ('or' == strtolower($token->term))
    605             {
    606               if ($i+1 < count($this->tokens))
    607                 $this->tokens[$i+1]->modifier |= QST_OR;
    608               $token->term = "";
    609             }
    610             if ('and' == strtolower($token->term))
    611             {
    612               $token->term = "";
    613             }
    614             if ( substr($token->term, -1)=='*' )
    615             {
    616               $token->term = rtrim($token->term, '*');
    617               $token->modifier |= QST_WILDCARD_END;
    618             }
     682            if ($i+1 < count($this->tokens))
     683              $this->tokens[$i+1]->modifier |= QST_NOT;
     684            $token->term = "";
    619685          }
    620           if (!strlen($token->term))
    621             $remove = true;
    622         }
    623         elseif (!$token->scope->is_text)
    624         {
    625           if (!$token->scope->parse($token))
    626             $remove = true;
    627         }
    628       }
    629       else
    630       {
    631         if (!count($token->tokens))
     686          if ('or' == strtolower($token->term))
     687          {
     688            if ($i+1 < count($this->tokens))
     689              $this->tokens[$i+1]->modifier |= QST_OR;
     690            $token->term = "";
     691          }
     692          if ('and' == strtolower($token->term))
     693          {
     694            $token->term = "";
     695          }
     696        }
     697
     698        if (!strlen($token->term)
     699          && (!isset($token->scope) || !$token->scope->nullable) )
     700        {
     701          $remove = true;
     702        }
     703
     704        if ( isset($token->scope)
     705          && !$token->scope->parse($token))
     706          $remove = true;
     707      }
     708      elseif (!count($token->tokens))
     709      {
    632710          $remove = true;
    633711      }
     
    650728      }
    651729      else
    652         $this->tokens[$i]->aooky_scope($scope);
     730        $this->tokens[$i]->apply_scope($scope);
    653731    }
    654732  }
     
    830908      case 'width':
    831909      case 'height':
    832       case 'hits':
    833       case 'rating_score':
    834910        $clauses[] = $token->scope->get_sql($scope_id, $token);
    835911        break;
     
    840916        $clauses[] = $token->scope->get_sql('width*height', $token);
    841917        break;
     918      case 'hits':
     919        $clauses[] = $token->scope->get_sql('hit', $token);
     920        break;
     921      case 'score':
     922        $clauses[] = $token->scope->get_sql('rating_score', $token);
     923        break;
    842924      case 'filesize':
    843         $clauses[] = $token->scope->get_sql('filesize', $token);
     925        $clauses[] = $token->scope->get_sql('1024*filesize', $token);
     926        break;
     927      case 'created':
     928        $clauses[] = $token->scope->get_sql('date_creation', $token);
     929        break;
     930      case 'posted':
     931        $clauses[] = $token->scope->get_sql('date_available', $token);
    844932        break;
    845933
     
    11271215  $scopes[] = new QNumericRangeScope('filesize', array());
    11281216  $scopes[] = new QNumericRangeScope('hits', array('hit', 'visit', 'visits'));
    1129   $scopes[] = new QNumericRangeScope('rating_score', array('score'), true);
     1217  $scopes[] = new QNumericRangeScope('score', array('rating'), true);
     1218
     1219  $createdDateAliases = array('taken');
     1220  $postedDateAliases = array('added');
     1221  if ($conf['calendar_datefield'] == 'date_creation')
     1222    $createdDateAliases[] = 'date';
     1223  else
     1224    $postedDateAliases[] = 'date';
     1225  $scopes[] = new QDateRangeScope('created', $createdDateAliases, true);
     1226  $scopes[] = new QDateRangeScope('posted', $postedDateAliases);
     1227
    11301228  $expression = new QExpression($q, $scopes);
    11311229//var_export($expression);
Note: See TracChangeset for help on using the changeset viewer.