source: trunk/include/functions_search.inc.php @ 27868

Last change on this file since 27868 was 27868, checked in by rvelices, 10 years ago

bug 3056: Improve/rewrite quick search engine: by default AND is used to match all entered terms, OR operator, grouping using brackets ()
still work in progress

  • Property svn:eol-style set to LF
File size: 23.5 KB
RevLine 
[1113]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[26461]5// | Copyright(C) 2008-2014 Piwigo Team                  http://piwigo.org |
[2297]6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
[1113]23
[25658]24/**
25 * @package functions\search
26 */
[1113]27
[25658]28
[1113]29/**
[25658]30 * Returns search rules stored into a serialized array in "search"
[1113]31 * table. Each search rules set is numericaly identified.
32 *
[25658]33 * @param int $search_id
[1113]34 * @return array
35 */
36function get_search_array($search_id)
37{
38  if (!is_numeric($search_id))
39  {
40    die('Search id must be an integer');
41  }
42
43  $query = '
44SELECT rules
45  FROM '.SEARCH_TABLE.'
46  WHERE id = '.$search_id.'
47;';
[4325]48  list($serialized_rules) = pwg_db_fetch_row(pwg_query($query));
[1113]49
50  return unserialize($serialized_rules);
51}
52
53/**
[25658]54 * Returns the SQL clause for a search.
55 * Transforms the array returned by get_search_array() into SQL sub-query.
[1113]56 *
[25658]57 * @param array $search
[1113]58 * @return string
59 */
[1537]60function get_sql_search_clause($search)
[1113]61{
62  // SQL where clauses are stored in $clauses array during query
63  // construction
64  $clauses = array();
65
[1119]66  foreach (array('file','name','comment','author') as $textfield)
[1113]67  {
68    if (isset($search['fields'][$textfield]))
69    {
70      $local_clauses = array();
71      foreach ($search['fields'][$textfield]['words'] as $word)
72      {
[25018]73        $local_clauses[] = $textfield." LIKE '%".$word."%'";
[1113]74      }
75
76      // adds brackets around where clauses
77      $local_clauses = prepend_append_array_items($local_clauses, '(', ')');
78
[25018]79      $clauses[] = implode(
80        ' '.$search['fields'][$textfield]['mode'].' ',
81        $local_clauses
[1113]82        );
83    }
84  }
85
86  if (isset($search['fields']['allwords']))
87  {
[1119]88    $fields = array('file', 'name', 'comment', 'author');
[1113]89    // in the OR mode, request bust be :
90    // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%')
91    // OR (field1 LIKE '%word2%' OR field2 LIKE '%word2%'))
92    //
93    // in the AND mode :
94    // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%')
95    // AND (field1 LIKE '%word2%' OR field2 LIKE '%word2%'))
96    $word_clauses = array();
97    foreach ($search['fields']['allwords']['words'] as $word)
98    {
99      $field_clauses = array();
100      foreach ($fields as $field)
101      {
[25018]102        $field_clauses[] = $field." LIKE '%".$word."%'";
[1113]103      }
104      // adds brackets around where clauses
[25018]105      $word_clauses[] = implode(
106        "\n          OR ",
107        $field_clauses
[1113]108        );
109    }
110
111    array_walk(
112      $word_clauses,
113      create_function('&$s','$s="(".$s.")";')
114      );
115
[26825]116    // make sure the "mode" is either OR or AND
117    if ($search['fields']['allwords']['mode'] != 'AND' and $search['fields']['allwords']['mode'] != 'OR')
118    {
119      $search['fields']['allwords']['mode'] = 'AND';
120    }
121
[25018]122    $clauses[] = "\n         ".
[1113]123      implode(
[25018]124        "\n         ". $search['fields']['allwords']['mode']. "\n         ",
[1113]125        $word_clauses
[25018]126        );
[1113]127  }
128
129  foreach (array('date_available', 'date_creation') as $datefield)
130  {
131    if (isset($search['fields'][$datefield]))
132    {
[25026]133      $clauses[] = $datefield." = '".$search['fields'][$datefield]['date']."'";
[1113]134    }
135
136    foreach (array('after','before') as $suffix)
137    {
138      $key = $datefield.'-'.$suffix;
139
140      if (isset($search['fields'][$key]))
141      {
[25018]142        $clauses[] = $datefield.
[1113]143          ($suffix == 'after'             ? ' >' : ' <').
144          ($search['fields'][$key]['inc'] ? '='  : '').
[25018]145          " '".$search['fields'][$key]['date']."'";
[1113]146      }
147    }
148  }
149
150  if (isset($search['fields']['cat']))
151  {
152    if ($search['fields']['cat']['sub_inc'])
153    {
154      // searching all the categories id of sub-categories
155      $cat_ids = get_subcat_ids($search['fields']['cat']['words']);
156    }
157    else
158    {
159      $cat_ids = $search['fields']['cat']['words'];
160    }
161
162    $local_clause = 'category_id IN ('.implode(',', $cat_ids).')';
[25018]163    $clauses[] = $local_clause;
[1113]164  }
165
166  // adds brackets around where clauses
167  $clauses = prepend_append_array_items($clauses, '(', ')');
168
169  $where_separator =
170    implode(
171      "\n    ".$search['mode'].' ',
172      $clauses
173      );
174
175  $search_clause = $where_separator;
176
[1119]177  return $search_clause;
178}
179
180/**
[25658]181 * Returns the list of items corresponding to the advanced search array.
[1119]182 *
[25658]183 * @param array $search
184 * @param string $images_where optional additional restriction on images table
[1119]185 * @return array
186 */
[25658]187function get_regular_search_results($search, $images_where='')
[1119]188{
[2451]189  global $conf;
190  $forbidden = get_sql_condition_FandF(
191        array
192          (
193            'forbidden_categories' => 'category_id',
194            'visible_categories' => 'category_id',
195            'visible_images' => 'id'
196          ),
197        "\n  AND"
198    );
199
[1119]200  $items = array();
[2451]201  $tag_items = array();
[1537]202
[2451]203  if (isset($search['fields']['tags']))
204  {
205    $tag_items = get_image_ids_for_tags(
206      $search['fields']['tags']['words'],
207      $search['fields']['tags']['mode']
208      );
209  }
210
[1537]211  $search_clause = get_sql_search_clause($search);
212
[1119]213  if (!empty($search_clause))
[1113]214  {
[1119]215    $query = '
[8611]216SELECT DISTINCT(id)
[2451]217  FROM '.IMAGES_TABLE.' i
[1119]218    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
[2451]219  WHERE '.$search_clause;
220    if (!empty($images_where))
221    {
222      $query .= "\n  AND ".$images_where;
223    }
[8726]224    $query .= $forbidden.'
[2451]225  '.$conf['order_by'];
[1119]226    $items = array_from_query($query, 'id');
[1113]227  }
228
[2451]229  if ( !empty($tag_items) )
[1119]230  {
231    switch ($search['mode'])
232    {
233      case 'AND':
234        if (empty($search_clause))
235        {
236          $items = $tag_items;
237        }
238        else
239        {
[5691]240          $items = array_values( array_intersect($items, $tag_items) );
[1119]241        }
242        break;
243      case 'OR':
[2451]244        $before_count = count($items);
[1119]245        $items = array_unique(
246          array_merge(
247            $items,
248            $tag_items
249            )
250          );
251        break;
[2451]252    }
[1119]253  }
[1537]254
[1119]255  return $items;
[1113]256}
[1537]257
[25658]258/**
259 * Finds if a char is a letter, a figure or any char of the extended ASCII table (>127).
260 *
261 * @param char $ch
262 * @return bool
263 */
[10340]264function is_word_char($ch)
265{
266  return ($ch>='0' && $ch<='9') || ($ch>='a' && $ch<='z') || ($ch>='A' && $ch<='Z') || ord($ch)>127;
267}
268
[25658]269/**
270 * Finds if a char is a special token for word start: [{<=*+
271 *
272 * @param char $ch
273 * @return bool
274 */
[18207]275function is_odd_wbreak_begin($ch)
276{
277  return strpos('[{<=*+', $ch)===false ? false:true;
278}
279
[25658]280/**
281 * Finds if a char is a special token for word end: ]}>=*+
282 *
283 * @param char $ch
284 * @return bool
285 */
[18207]286function is_odd_wbreak_end($ch)
287{
288  return strpos(']}>=*+', $ch)===false ? false:true;
289}
290
[25658]291
292define('QST_QUOTED',         0x01);
293define('QST_NOT',            0x02);
[27868]294define('QST_OR',             0x04);
295define('QST_WILDCARD_BEGIN', 0x08);
296define('QST_WILDCARD_END',   0x10);
[18207]297define('QST_WILDCARD', QST_WILDCARD_BEGIN|QST_WILDCARD_END);
298
[1619]299/**
[25658]300 * Analyzes and splits the quick/query search query $q into tokens.
[10340]301 * q='john bill' => 2 tokens 'john' 'bill'
302 * Special characters for MySql full text search (+,<,>,~) appear in the token modifiers.
303 * The query can contain a phrase: 'Pierre "New York"' will return 'pierre' qnd 'new york'.
[25658]304 *
305 * @param string $q
[1619]306 */
[27868]307
308class QSingleToken
[1537]309{
[27868]310  var $is_single = true;
311  var $token;
312  var $idx;
[2135]313
[27868]314  function __construct($token)
[1537]315  {
[27868]316    $this->token = $token;
317  }
318}
319
320class QMultiToken
321{
322  var $is_single = false;
323  var $tokens = array();
324  var $token_modifiers = array();
325
326  function __toString()
327  {
328    $s = '';
329    for ($i=0; $i<count($this->tokens); $i++)
[1537]330    {
[27868]331      $modifier = $this->token_modifiers[$i];
332      if ($i)
333        $s .= ' ';
334      if ($modifier & QST_OR)
335        $s .= 'OR ';
336      if ($modifier & QST_NOT)
337        $s .= 'NOT ';
338      if ($modifier & QST_WILDCARD_BEGIN)
339        $s .= '*';
340      if ($modifier & QST_QUOTED)
341        $s .= '"';
342      if (! ($this->tokens[$i]->is_single) )
343      {
344        $s .= '(';
345        $s .= $this->tokens[$i];
346        $s .= ')';
347      }
348      else
349      {
350        $s .= $this->tokens[$i]->token;
351      }
352      if ($modifier & QST_QUOTED)
353        $s .= '"';
354      if ($modifier & QST_WILDCARD_END)
355        $s .= '*';
356
357    }
358    return $s;
359  }
360
361  function push(&$token, &$modifier)
362  {
363    $this->tokens[] = new QSingleToken($token);
364    $this->token_modifiers[] = $modifier;
365    $token = "";
366    $modifier = 0;
367  }
368
369  protected function parse_expression($q, &$qi, $level)
370  {
371    $crt_token = "";
372    $crt_modifier = 0;
373
374    for ($stop=false; !$stop && $qi<strlen($q); $qi++)
375    {
376      $ch = $q[$qi];
377      if ( ($crt_modifier&QST_QUOTED)==0)
378      {
379        switch ($ch)
380        {
381          case '(':
382            if (strlen($crt_token))
383              $this->push($crt_token, $crt_modifier);
384            $sub = new QMultiToken;
385            $qi++;
386            $sub->parse_expression($q, $qi, $level+1);
387            $this->tokens[] = $sub;
388            $this->token_modifiers[] = $crt_modifier;
389            $crt_modifier = 0;
390            break;
391          case ')':
392            if ($level>0)
393              $stop = true;
394            break;
395          case '"':
396            if (strlen($crt_token))
397              $this->push($crt_token, $crt_modifier);
398            $crt_modifier |= QST_QUOTED;
399            break;
400          case '-':
401            if (strlen($crt_token))
402              $crt_token .= $ch;
403            else
404              $crt_modifier |= QST_NOT;
405            break;
406          case '*':
407            if (strlen($crt_token))
408              $crt_token .= $ch; // wildcard end later
409            else
410              $crt_modifier |= QST_WILDCARD_BEGIN;
411            break;
412          default:
413            if (preg_match('/[\s,.;!\?]+/', $ch))
414            { // white space
415              if (strlen($crt_token))
416                $this->push($crt_token, $crt_modifier);
417              $crt_modifier = 0;
418            }
419            else
420              $crt_token .= $ch;
421            break;
422        }
423      }
424      else
425      {// quoted
[2135]426        if ($ch=='"')
427        {
[27868]428          if ($qi+1 < strlen($q) && $q[$qi+1]=='*')
[18207]429          {
[27868]430            $crt_modifier |= QST_WILDCARD_END;
431            $ai++;
[18207]432          }
[27868]433          $this->push($crt_token, $crt_modifier);
[2135]434        }
[27868]435        else
436          $crt_token .= $ch;
437      }
438    }
439
440    if (strlen($crt_token))
441      $this->push($crt_token, $crt_modifier);
442
443    for ($i=0; $i<count($this->tokens); $i++)
444    {
445      $token = $this->tokens[$i];
446      $remove = false;
447      if ($token->is_single)
448      {
449        if ( ($this->token_modifiers[$i]&QST_QUOTED)==0 )
450        {
451          if ('not' == strtolower($token->token))
[10340]452          {
[27868]453            if ($i+1 < count($this->tokens))
454              $this->token_modifiers[$i+1] |= QST_NOT;
455            $token->token = "";
[10340]456          }
[27868]457          if ('or' == strtolower($token->token))
[10340]458          {
[27868]459            if ($i+1 < count($this->tokens))
460              $this->token_modifiers[$i+1] |= QST_OR;
461            $token->token = "";
[10340]462          }
[27868]463          if ('and' == strtolower($token->token))
[2135]464          {
[27868]465            $token->token = "";
[2135]466          }
[27868]467          if ( substr($token->token, -1)=='*' )
468          {
469            $token->token = rtrim($token->token, '*');
470            $this->token_modifiers[$i] |= QST_WILDCARD_END;
471          }
[2135]472        }
[27868]473        if (!strlen($token->token))
474          $remove = true;
475      }
476      else
[18207]477      {
[27868]478        if (!count($token->tokens))
479          $remove = true;
[18207]480      }
[27868]481      if ($remove)
482      {
483        array_splice($this->tokens, $i, 1);
484        array_splice($this->token_modifiers, $i, 1);
485        $i--;
486      }
[1537]487    }
488  }
[27868]489}
[18636]490
[27868]491class QExpression extends QMultiToken
492{
493  var $stokens = array();
494  var $stoken_modifiers = array();
495
496  function __construct($q)
[2135]497  {
[27868]498    $i = 0;
499    $this->parse_expression($q, $i, 0);
500    //@TODO: manipulate the tree so that 'a OR b c' is the same as 'b c OR a'
501    $this->build_single_tokens($this);
[2135]502  }
[1537]503
[27868]504  private function build_single_tokens(QMultiToken $expr)
[10340]505  {
[27868]506    //@TODO: double negation results in no negation in token modifier
507    for ($i=0; $i<count($expr->tokens); $i++)
[10340]508    {
[27868]509      $token = $expr->tokens[$i];
510      if ($token->is_single)
[10340]511      {
[27868]512        $token->idx = count($this->stokens);
513        $this->stokens[] = $token->token;
514        $this->stoken_modifiers[] = $expr->token_modifiers[$i];
[10340]515      }
[27868]516      else
517        $this->build_single_tokens($token);
[10340]518    }
519  }
520}
521
[27868]522class QResults
[10340]523{
[27868]524  var $all_tags;
525  var $tag_ids;
526  var $tag_iids;
527  var $images_iids;
528  var $iids;
529}
530
531function qsearch_get_images(QExpression $expr, QResults $qsr)
532{
533  //@TODO: inflections for english / french
534  $qsr->images_iids = array_fill(0, count($expr->tokens), array());
535  $query_base = 'SELECT id from '.IMAGES_TABLE.' i WHERE ';
536  for ($i=0; $i<count($expr->stokens); $i++)
[1537]537  {
[27868]538    $token = $expr->stokens[$i];
539    $clauses = array();
540
541    $like = addslashes($token);
542    $like = str_replace( array('%','_'), array('\\%','\\_'), $like); // escape LIKE specials %_
543    $clauses[] = 'CONVERT(file, CHAR) LIKE \'%'.$like.'%\'';
544
545    if (strlen($token)>3) // default minimum full text index
546    {
547      $ft = $token;
548      if ($expr->stoken_modifiers[$i] & QST_QUOTED)
549        $ft = '"'.$ft.'"';
550      if ($expr->stoken_modifiers[$i] & QST_WILDCARD_END)
551        $ft .= '*';
552      $clauses[] = 'MATCH(i.name, i.comment) AGAINST( \''.addslashes($ft).'\' IN BOOLEAN MODE)';
553    }
554    else
555    {
556      foreach( array('i.name', 'i.comment') as $field)
557      {
558        $clauses[] = $field.' LIKE \''.$like.' %\'';
559        $clauses[] = $field.' LIKE \'% '.$like.'\'';
560        $clauses[] = $field.' LIKE \'% '.$like.' %\'';
561      }
562    }
563    $query = $query_base.'('.implode(' OR ', $clauses).')';
564    $qsr->images_iids[$i] = query2array($query,null,'id');
[1537]565  }
566}
567
[27868]568function qsearch_get_tags(QExpression $expr, QResults $qsr)
[1537]569{
[27868]570  $tokens = $expr->stokens;
571  $token_modifiers = $expr->stoken_modifiers;
572
[18636]573  $token_tag_ids = array_fill(0, count($tokens), array() );
[27868]574  $all_tags = array();
[10340]575
[18636]576  $token_tag_scores = $token_tag_ids;
[10340]577  $transliterated_tokens = array();
578  foreach ($tokens as $token)
579  {
580    $transliterated_tokens[] = transliterate($token);
581  }
582
583  $query = '
[18636]584SELECT t.*, COUNT(image_id) AS counter
585  FROM '.TAGS_TABLE.' t
[10340]586    INNER JOIN '.IMAGE_TAG_TABLE.' ON id=tag_id
587  GROUP BY id';
588  $result = pwg_query($query);
589  while ($tag = pwg_db_fetch_assoc($result))
590  {
591    $transliterated_tag = transliterate($tag['name']);
592
593    // find how this tag matches query tokens
594    for ($i=0; $i<count($tokens); $i++)
595    {
596      $transliterated_token = $transliterated_tokens[$i];
597
598      $match = false;
599      $pos = 0;
600      while ( ($pos = strpos($transliterated_tag, $transliterated_token, $pos)) !== false)
601      {
[18207]602        if ( ($token_modifiers[$i]&QST_WILDCARD)==QST_WILDCARD )
[10340]603        {// wildcard in this token
604          $match = 1;
605          break;
606        }
607        $token_len = strlen($transliterated_token);
608
[18207]609        // search begin of word
610        $wbegin_len=0; $wbegin_char=' ';
611        while ($pos-$wbegin_len > 0)
[10340]612        {
[18207]613          if (! is_word_char($transliterated_tag[$pos-$wbegin_len-1]) )
614          {
615            $wbegin_char = $transliterated_tag[$pos-$wbegin_len-1];
[10340]616            break;
[18207]617          }
618          $wbegin_len++;
[10340]619        }
620
[18207]621        // search end of word
622        $wend_len=0; $wend_char=' ';
623        while ($pos+$token_len+$wend_len < strlen($transliterated_tag))
[10340]624        {
[18207]625          if (! is_word_char($transliterated_tag[$pos+$token_len+$wend_len]) )
626          {
627            $wend_char = $transliterated_tag[$pos+$token_len+$wend_len];
628            break;
629          }
630          $wend_len++;
[10340]631        }
632
[18207]633        $this_score = 0;
634        if ( ($token_modifiers[$i]&QST_WILDCARD)==0 )
635        {// no wildcard begin or end
636          if ($token_len <= 2)
637          {// search for 1 or 2 characters must match exactly to avoid retrieving too much data
638            if ($wbegin_len==0 && $wend_len==0 && !is_odd_wbreak_begin($wbegin_char) && !is_odd_wbreak_end($wend_char) )
639              $this_score = 1;
640          }
641          elseif ($token_len == 3)
642          {
643            if ($wbegin_len==0)
644              $this_score = $token_len / ($token_len + $wend_len);
645          }
646          else
647          {
648            $this_score = $token_len / ($token_len + 1.1 * $wbegin_len + 0.9 * $wend_len);
649          }
650        }
651
[10340]652        if ($this_score>0)
653          $match = max($match, $this_score );
654        $pos++;
655      }
656
657      if ($match)
658      {
659        $tag_id = (int)$tag['id'];
660        $all_tags[$tag_id] = $tag;
[18636]661        $token_tag_ids[$i][] = $tag_id;
662        $token_tag_scores[$i][] = $match;
[10340]663      }
664    }
665  }
666
[27868]667  // process tags
668  $not_tag_ids = array();
[18636]669  for ($i=0; $i<count($tokens); $i++)
[10340]670  {
[18636]671    array_multisort($token_tag_scores[$i], SORT_DESC|SORT_NUMERIC, $token_tag_ids[$i]);
[27868]672    $is_not = $token_modifiers[$i]&QST_NOT;
673    $counter = 0;
[18636]674
675    for ($j=0; $j<count($token_tag_scores[$i]); $j++)
[10340]676    {
[27868]677      if ($is_not)
[18636]678      {
[27868]679        if ($token_tag_scores[$i][$j] < 0.8 ||
680              ($j>0 && $token_tag_scores[$i][$j] < $token_tag_scores[$i][0]) )
681        {
682          array_splice($token_tag_scores[$i], $j);
683          array_splice($token_tag_ids[$i], $j);
684        }
[18636]685      }
[27868]686      else
687      {
688        $tag_id = $token_tag_ids[$i][$j];
689        $counter += $all_tags[$tag_id]['counter'];
690        if ($counter > 200 && $j>0 && $token_tag_scores[$i][0] > $token_tag_scores[$i][$j] )
691        {// "many" images in previous tags and starting from this tag is less relevent
692          array_splice($token_tag_ids[$i], $j);
693          array_splice($token_tag_scores[$i], $j);
694          break;
695        }
696      }
[18636]697    }
[27868]698
699    if ($is_not)
700    {
701      $not_tag_ids = array_merge($not_tag_ids, $token_tag_ids[$i]);
702    }
[18636]703  }
704
[27868]705  $all_tags = array_diff_key($all_tags, array_flip($not_tag_ids));
706  usort($all_tags, 'tag_alpha_compare');
707  foreach ( $all_tags as &$tag )
708  {
709    $tag['name'] = trigger_event('render_tag_name', $tag['name'], $tag);
710  }
711  $qsr->all_tags = $all_tags;
712
713  $qsr->tag_ids = $token_tag_ids;
714  $qsr->tag_iids = array_fill(0, count($tokens), array() );
715
[18636]716  for ($i=0; $i<count($tokens); $i++)
717  {
[27868]718    $tag_ids = $token_tag_ids[$i];
[18636]719
[27868]720    if (!empty($tag_ids))
721    {
722      $query = '
723SELECT image_id FROM '.IMAGE_TAG_TABLE.'
724  WHERE tag_id IN ('.implode(',',$tag_ids).')
725  GROUP BY image_id';
726      $qsr->tag_iids[$i] = query2array($query, null, 'image_id');
727    }
728  }
729}
[18636]730
[27868]731
732function qsearch_eval(QExpression $expr, QResults $qsr, QMultiToken $crt_expr)
733{
734  $ids = $not_ids = array();
735  $first = true;
736  for ($i=0; $i<count($crt_expr->tokens); $i++)
737  {
738    $current = $crt_expr->tokens[$i];
739    if ($current->is_single)
[18636]740    {
[27868]741      $crt_ids = $qsr->iids[$current->idx] = array_unique( array_merge($qsr->images_iids[$current->idx], $qsr->tag_iids[$current->idx]) );
742    }
743    else
744      $crt_ids = qsearch_eval($expr, $qsr, $current);
745    $modifier = $crt_expr->token_modifiers[$i];
746
747    if ($modifier & QST_NOT)
748      $not_ids = array_unique( array_merge($not_ids, $crt_ids));
749    else
750    {
751      if ($modifier & QST_OR)
752        $ids = array_unique( array_merge($ids, $crt_ids) );
753      else
[18636]754      {
[27868]755        if ($current->is_single && empty($crt_ids))
756        {
757          //@TODO: mark this term as unmatched and tell users
758          //@TODO: if we don't find a term at all, maybe ignore it and produce some results
759        }
760        if ($first)
761          $ids = $crt_ids;
762        else
763          $ids = array_intersect($ids, $crt_ids);
764        $first= false;
[18636]765      }
[10340]766    }
767  }
[22175]768
[27868]769  if (count($not_ids))
770    $ids = array_diff($ids, $not_ids);
771  return $ids;
[18636]772}
[10340]773
[18636]774/**
[25658]775 * Returns the search results corresponding to a quick/query search.
[18636]776 * A quick/query search returns many items (search is not strict), but results
777 * are sorted by relevance unless $super_order_by is true. Returns:
[25658]778 *  array (
779 *    'items' => array of matching images
780 *    'qs'    => array(
781 *      'matching_tags' => array of matching tags
782 *      'matching_cats' => array of matching categories
783 *      'matching_cats_no_images' =>array(99) - matching categories without images
784 *      )
785 *    )
[18636]786 *
[25658]787 * @param string $q
788 * @param bool $super_order_by
789 * @param string $images_where optional additional restriction on images table
[18636]790 * @return array
791 */
792function get_quick_search_results($q, $super_order_by, $images_where='')
793{
794  global $user, $conf;
795
796  $search_results =
797    array(
798      'items' => array(),
799      'qs' => array('q'=>stripslashes($q)),
800    );
801  $q = trim($q);
[27868]802  $expression = new QExpression($q);
803//var_export($expression);
[18636]804
[27868]805  $qsr = new QResults;
806  qsearch_get_tags($expression, $qsr);
807  qsearch_get_images($expression, $qsr);
808//var_export($qsr->all_tags);
[18636]809
[27868]810  $ids = qsearch_eval($expression, $qsr, $expression);
[18636]811
[27868]812  $debug[] = "<!--\nparsed: ".$expression;
813  $debug[] = count($expression->stokens).' tokens';
814  for ($i=0; $i<count($expression->stokens); $i++)
[18636]815  {
[27868]816    $debug[] = $expression->stokens[$i].': '.count($qsr->tag_ids[$i]).' tags, '.count($qsr->tag_iids[$i]).' tiids, '.count($qsr->images_iids[$i]).' iiids, '.count($qsr->iids[$i]).' iids';
[18636]817  }
[27868]818  $debug[] = 'before perms '.count($ids);
[18636]819
[27868]820  $search_results['qs']['matching_tags'] = $qsr->all_tags;
821  global $template;
[18636]822
[27868]823  if (empty($ids))
[18636]824  {
[27868]825    $debug[] = '-->';
826    $template->append('footer_elements', implode("\n", $debug) );
[2135]827    return $search_results;
828  }
829
830  $where_clauses = array();
[27868]831  $where_clauses[]='i.id IN ('. implode(',', $ids) . ')';
[2135]832  if (!empty($images_where))
833  {
834    $where_clauses[]='('.$images_where.')';
835  }
836  $where_clauses[] = get_sql_condition_FandF(
837      array
838        (
839          'forbidden_categories' => 'category_id',
840          'visible_categories' => 'category_id',
841          'visible_images' => 'i.id'
842        ),
843      null,true
844    );
845
846  $query = '
[1537]847SELECT DISTINCT(id)
[2135]848  FROM '.IMAGES_TABLE.' i
[1537]849    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
[2135]850  WHERE '.implode("\n AND ", $where_clauses)."\n".
851  $conf['order_by'];
852
[27868]853  $ids = query2array($query, null, 'id');
[2135]854
[27868]855  $debug[] = count($ids).' final photo count -->';
856  $template->append('footer_elements', implode("\n", $debug) );
[18207]857
[27868]858  $search_results['items'] = $ids;
[1537]859  return $search_results;
860}
861
862/**
[25658]863 * Returns an array of 'items' corresponding to the search id.
864 * It can be either a quick search or a regular search.
[1537]865 *
[25658]866 * @param int $search_id
867 * @param bool $super_order_by
868 * @param string $images_where optional aditional restriction on images table
[1537]869 * @return array
870 */
[2451]871function get_search_results($search_id, $super_order_by, $images_where='')
[1537]872{
873  $search = get_search_array($search_id);
874  if ( !isset($search['q']) )
875  {
[2451]876    $result['items'] = get_regular_search_results($search, $images_where);
[1537]877    return $result;
878  }
879  else
880  {
[2451]881    return get_quick_search_results($search['q'], $super_order_by, $images_where);
[1537]882  }
883}
[25658]884
[1113]885?>
Note: See TracBrowser for help on using the repository browser.