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

Last change on this file since 2297 was 2297, checked in by plg, 16 years ago

Modification: new header on PHP files, PhpWebGallery renamed Piwigo.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 16.8 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008      Piwigo Team                  http://piwigo.org |
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// +-----------------------------------------------------------------------+
23// +-----------------------------------------------------------------------+
24// | PhpWebGallery - a PHP based picture gallery                           |
25// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
26// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
27// +-----------------------------------------------------------------------+
28// | file          : $Id: functions_search.inc.php 2297 2008-04-04 22:57:23Z plg $
29// | last update   : $Date: 2008-04-04 22:57:23 +0000 (Fri, 04 Apr 2008) $
30// | last modifier : $Author: plg $
31// | revision      : $Revision: 2297 $
32// +-----------------------------------------------------------------------+
33// | This program is free software; you can redistribute it and/or modify  |
34// | it under the terms of the GNU General Public License as published by  |
35// | the Free Software Foundation                                          |
36// |                                                                       |
37// | This program is distributed in the hope that it will be useful, but   |
38// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
39// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
40// | General Public License for more details.                              |
41// |                                                                       |
42// | You should have received a copy of the GNU General Public License     |
43// | along with this program; if not, write to the Free Software           |
44// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
45// | USA.                                                                  |
46// +-----------------------------------------------------------------------+
47
48
49/**
50 * returns search rules stored into a serialized array in "search"
51 * table. Each search rules set is numericaly identified.
52 *
53 * @param int search_id
54 * @return array
55 */
56function get_search_array($search_id)
57{
58  if (!is_numeric($search_id))
59  {
60    die('Search id must be an integer');
61  }
62
63  $query = '
64SELECT rules
65  FROM '.SEARCH_TABLE.'
66  WHERE id = '.$search_id.'
67;';
68  list($serialized_rules) = mysql_fetch_row(pwg_query($query));
69
70  return unserialize($serialized_rules);
71}
72
73/**
74 * returns the SQL clause from a search identifier
75 *
76 * Search rules are stored in search table as a serialized array. This array
77 * need to be transformed into an SQL clause to be used in queries.
78 *
79 * @param array search
80 * @return string
81 */
82function get_sql_search_clause($search)
83{
84  // SQL where clauses are stored in $clauses array during query
85  // construction
86  $clauses = array();
87
88  foreach (array('file','name','comment','author') as $textfield)
89  {
90    if (isset($search['fields'][$textfield]))
91    {
92      $local_clauses = array();
93      foreach ($search['fields'][$textfield]['words'] as $word)
94      {
95        array_push($local_clauses, $textfield." LIKE '%".$word."%'");
96      }
97
98      // adds brackets around where clauses
99      $local_clauses = prepend_append_array_items($local_clauses, '(', ')');
100
101      array_push(
102        $clauses,
103        implode(
104          ' '.$search['fields'][$textfield]['mode'].' ',
105          $local_clauses
106          )
107        );
108    }
109  }
110
111  if (isset($search['fields']['allwords']))
112  {
113    $fields = array('file', 'name', 'comment', 'author');
114    // in the OR mode, request bust be :
115    // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%')
116    // OR (field1 LIKE '%word2%' OR field2 LIKE '%word2%'))
117    //
118    // in the AND mode :
119    // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%')
120    // AND (field1 LIKE '%word2%' OR field2 LIKE '%word2%'))
121    $word_clauses = array();
122    foreach ($search['fields']['allwords']['words'] as $word)
123    {
124      $field_clauses = array();
125      foreach ($fields as $field)
126      {
127        array_push($field_clauses, $field." LIKE '%".$word."%'");
128      }
129      // adds brackets around where clauses
130      array_push(
131        $word_clauses,
132        implode(
133          "\n          OR ",
134          $field_clauses
135          )
136        );
137    }
138
139    array_walk(
140      $word_clauses,
141      create_function('&$s','$s="(".$s.")";')
142      );
143
144    array_push(
145      $clauses,
146      "\n         ".
147      implode(
148        "\n         ".
149              $search['fields']['allwords']['mode'].
150        "\n         ",
151        $word_clauses
152        )
153      );
154  }
155
156  foreach (array('date_available', 'date_creation') as $datefield)
157  {
158    if (isset($search['fields'][$datefield]))
159    {
160      array_push(
161        $clauses,
162        $datefield." = '".$search['fields'][$datefield]['date']."'"
163        );
164    }
165
166    foreach (array('after','before') as $suffix)
167    {
168      $key = $datefield.'-'.$suffix;
169
170      if (isset($search['fields'][$key]))
171      {
172        array_push(
173          $clauses,
174
175          $datefield.
176          ($suffix == 'after'             ? ' >' : ' <').
177          ($search['fields'][$key]['inc'] ? '='  : '').
178          " '".$search['fields'][$key]['date']."'"
179
180          );
181      }
182    }
183  }
184
185  if (isset($search['fields']['cat']))
186  {
187    if ($search['fields']['cat']['sub_inc'])
188    {
189      // searching all the categories id of sub-categories
190      $cat_ids = get_subcat_ids($search['fields']['cat']['words']);
191    }
192    else
193    {
194      $cat_ids = $search['fields']['cat']['words'];
195    }
196
197    $local_clause = 'category_id IN ('.implode(',', $cat_ids).')';
198    array_push($clauses, $local_clause);
199  }
200
201  // adds brackets around where clauses
202  $clauses = prepend_append_array_items($clauses, '(', ')');
203
204  $where_separator =
205    implode(
206      "\n    ".$search['mode'].' ',
207      $clauses
208      );
209
210  $search_clause = $where_separator;
211
212  return $search_clause;
213}
214
215/**
216 * returns the list of items corresponding to the advanced search array
217 *
218 * @param array search
219 * @return array
220 */
221function get_regular_search_results($search)
222{
223  $items = array();
224
225  $search_clause = get_sql_search_clause($search);
226
227  if (!empty($search_clause))
228  {
229    $query = '
230SELECT DISTINCT(id)
231  FROM '.IMAGES_TABLE.'
232    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
233  WHERE '.$search_clause.'
234;';
235    $items = array_from_query($query, 'id');
236  }
237
238  if (isset($search['fields']['tags']))
239  {
240    $tag_items = get_image_ids_for_tags(
241      $search['fields']['tags']['words'],
242      $search['fields']['tags']['mode']
243      );
244
245    switch ($search['mode'])
246    {
247      case 'AND':
248      {
249        if (empty($search_clause))
250        {
251          $items = $tag_items;
252        }
253        else
254        {
255          $items = array_intersect($items, $tag_items);
256        }
257        break;
258      }
259      case 'OR':
260      {
261        $items = array_unique(
262          array_merge(
263            $items,
264            $tag_items
265            )
266          );
267        break;
268      }
269    }
270  }
271
272  return $items;
273}
274
275/**
276 * returns the LIKE sql clause corresponding to the quick search query $q
277 * and the field $field. example q='john bill', field='file' will return
278 * file LIKE '%john%' OR file LIKE '%bill%'. Special characters for MySql full
279 * text search (+,<,>,~) are omitted. The query can contain a phrase:
280 * 'Pierre "New York"' will return LIKE '%Pierre%' OR LIKE '%New York%'.
281 * @param string q
282 * @param string field
283 * @return string
284 */
285function get_qsearch_like_clause($q, $field)
286{
287  $q = stripslashes($q);
288  $tokens = array();
289  $token_modifiers = array();
290  $crt_token = "";
291  $crt_token_modifier = "";
292  $state = 0;
293
294  for ($i=0; $i<strlen($q); $i++)
295  {
296    $ch = $q[$i];
297    switch ($state)
298    {
299      case 0:
300        if ($ch=='"')
301        {
302          if (strlen($crt_token))
303          {
304            $tokens[] = $crt_token;
305            $token_modifiers[] = $crt_token_modifier;
306            $crt_token = "";
307            $crt_token_modifier = "";
308          }
309          $state=1;
310        }
311        elseif ( $ch=='*' )
312        { // wild card
313          $crt_token .= '%';
314        }
315        elseif ( strcspn($ch, '+-><~')==0 )
316        { //special full text modifier
317          if (strlen($crt_token))
318          {
319            $tokens[] = $crt_token;
320            $token_modifiers[] = $crt_token_modifier;
321            $crt_token = "";
322            $crt_token_modifier = "";
323          }
324          $crt_token_modifier .= $ch;
325        }
326        elseif (preg_match('/[\s,.;!\?]+/', $ch))
327        { // white space
328          if (strlen($crt_token))
329          {
330            $tokens[] = $crt_token;
331            $token_modifiers[] = $crt_token_modifier;
332            $crt_token = "";
333            $crt_token_modifier = "";
334          }
335        }
336        else
337        {
338          $crt_token .= $ch;
339        }
340        break;
341      case 1: // qualified with quotes
342        switch ($ch)
343        {
344          case '"':
345            $tokens[] = $crt_token;
346            $token_modifiers[] = $crt_token_modifier;
347            $crt_token = "";
348            $crt_token_modifier = "";
349            $state=0;
350            break;
351          default:
352            $crt_token .= $ch;
353        }
354        break;
355    }
356  }
357  if (strlen($crt_token))
358  {
359    $tokens[] = $crt_token;
360    $token_modifiers[] = $crt_token_modifier;
361  }
362
363  $clauses = array();
364  for ($i=0; $i<count($tokens); $i++)
365  {
366    $tokens[$i] = trim($tokens[$i], '%');
367    if (strstr($token_modifiers[$i], '-')!==false)
368      continue;
369    if ( strlen($tokens[$i])==0)
370      continue;
371    $clauses[] = $field.' LIKE "%'.addslashes($tokens[$i]).'%"';
372  }
373
374  return count($clauses) ? '('.implode(' OR ', $clauses).')' : null;
375}
376
377
378/**
379 * returns the search results corresponding to a quick/query search.
380 * A quick/query search returns many items (search is not strict), but results
381 * are sorted by relevance unless $page['super_order_by'] is set. Returns:
382 * array (
383 * 'items' => array(85,68,79...)
384 * 'as_is' => 1 (indicates the caller that items are ordered and permissions checked
385 * 'qs'    => array(
386 *    'matching_tags' => array of matching tags
387 *    'matching_cats' => array of matching categories
388 *    'matching_cats_no_images' =>array(99) - matching categories without images
389 *      ))
390 *
391 * @param string q
392 * @param string images_where optional aditional restriction on images table
393 * @return array
394 */
395function get_quick_search_results($q, $images_where='')
396{
397  global $page;
398  $search_results =
399    array(
400      'items' => array(),
401      'as_is' => 1,
402      'qs' => array('q'=>stripslashes($q)),
403    );
404  $q = trim($q);
405  if (empty($q))
406  {
407    return $search_results;
408  }
409  $q_like_field = '@@__db_field__@@'; //something never in a search
410  $q_like_clause = get_qsearch_like_clause($q, $q_like_field );
411
412
413  // Step 1 - first we find matches in #images table ===========================
414  $where_clauses='MATCH(i.name, i.comment) AGAINST( "'.$q.'" IN BOOLEAN MODE)';
415  if (!empty($q_like_clause))
416  {
417    $where_clauses .= '
418    OR '. str_replace($q_like_field, 'file', $q_like_clause);
419    $where_clauses = '('.$where_clauses.')';
420  }
421  $where_clauses = array($where_clauses);
422  if (!empty($images_where))
423  {
424    $where_clauses[]='('.$images_where.')';
425  }
426  $where_clauses[] .= get_sql_condition_FandF
427      (
428        array( 'visible_images' => 'i.id' ), null, true
429      );
430  $query = '
431SELECT i.id,
432    MATCH(i.name, i.comment) AGAINST( "'.$q.'" IN BOOLEAN MODE) AS weight
433  FROM '.IMAGES_TABLE.' i
434  WHERE '.implode("\n AND ", $where_clauses);
435
436  $by_weights=array();
437  $result = pwg_query($query);
438  while ($row = mysql_fetch_array($result))
439  { // weight is important when sorting images by relevance
440    if ($row['weight'])
441    {
442      $by_weights[(int)$row['id']] =  2*$row['weight'];
443    }
444    else
445    {//full text does not match but file name match
446      $by_weights[(int)$row['id']] =  2;
447    }
448  }
449
450
451  // Step 2 - search tags corresponding to the query $q ========================
452  if (!empty($q_like_clause))
453  { // search name and url name (without accents)
454    $query = '
455SELECT id, name, url_name
456  FROM '.TAGS_TABLE.'
457  WHERE ('.str_replace($q_like_field, 'CONVERT(name, CHAR)', $q_like_clause).'
458    OR '.str_replace($q_like_field, 'url_name', $q_like_clause).')';
459    $tags = hash_from_query($query, 'id');
460    if ( !empty($tags) )
461    { // we got some tags; get the images
462      $search_results['qs']['matching_tags']=$tags;
463      $query = '
464SELECT image_id, COUNT(tag_id) AS weight
465  FROM '.IMAGE_TAG_TABLE.'
466  WHERE tag_id IN ('.implode(',',array_keys($tags)).')
467  GROUP BY image_id';
468      $result = pwg_query($query);
469      while ($row = mysql_fetch_assoc($result))
470      { // weight is important when sorting images by relevance
471        $image_id=(int)$row['image_id'];
472        @$by_weights[$image_id] += $row['weight'];
473      }
474    }
475  }
476
477
478  // Step 3 - search categories corresponding to the query $q ==================
479  global $user;
480  $query = '
481SELECT id, name, permalink, nb_images
482  FROM '.CATEGORIES_TABLE.'
483    INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' ON id=cat_id
484  WHERE user_id='.$user['id'].'
485    AND MATCH(name, comment) AGAINST( "'.$q.'" IN BOOLEAN MODE)'.
486  get_sql_condition_FandF (
487      array( 'visible_categories' => 'cat_id' ), "\n    AND"
488    );
489  $result = pwg_query($query);
490  while ($row = mysql_fetch_assoc($result))
491  { // weight is important when sorting images by relevance
492    if ($row['nb_images']==0)
493    {
494      $search_results['qs']['matching_cats_no_images'][] = $row;
495    }
496    else
497    {
498      $search_results['qs']['matching_cats'][$row['id']] = $row;
499    }
500  }
501
502  if ( empty($by_weights) and empty($search_results['qs']['matching_cats']) )
503  {
504    return $search_results;
505  }
506
507  // Step 4 - now we have $by_weights ( array image id => weight ) that need
508  // permission checks and/or matching categories to get images from
509  $where_clauses = array();
510  if ( !empty($by_weights) )
511  {
512    $where_clauses[]='i.id IN ('
513      . implode(',', array_keys($by_weights)) . ')';
514  }
515  if ( !empty($search_results['qs']['matching_cats']) )
516  {
517    $where_clauses[]='category_id IN ('.
518      implode(',',array_keys($search_results['qs']['matching_cats'])).')';
519  }
520  $where_clauses = array( '('.implode("\n    OR ",$where_clauses).')' );
521  if (!empty($images_where))
522  {
523    $where_clauses[]='('.$images_where.')';
524  }
525  $where_clauses[] = get_sql_condition_FandF(
526      array
527        (
528          'forbidden_categories' => 'category_id',
529          'visible_categories' => 'category_id',
530          'visible_images' => 'i.id'
531        ),
532      null,true
533    );
534
535  global $conf;
536  $query = '
537SELECT DISTINCT(id)
538  FROM '.IMAGES_TABLE.' i
539    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
540  WHERE '.implode("\n AND ", $where_clauses)."\n".
541  $conf['order_by'];
542
543  $allowed_images = array_from_query( $query, 'id');
544
545  if ( isset($page['super_order_by']) or empty($by_weights) )
546  {
547    $search_results['items'] = $allowed_images;
548    return $search_results;
549  }
550
551  $allowed_images = array_flip( $allowed_images );
552  $divisor = 5.0 * count($allowed_images);
553  foreach ($allowed_images as $id=>$rank )
554  {
555    $weight = isset($by_weights[$id]) ? $by_weights[$id] : 1;
556    $weight -= $rank/$divisor;
557    $allowed_images[$id] = $weight;
558  }
559  arsort($allowed_images, SORT_NUMERIC);
560  $search_results['items'] = array_keys($allowed_images);
561  return $search_results;
562}
563
564/**
565 * returns an array of 'items' corresponding to the search id
566 *
567 * @param int search id
568 * @param string images_where optional aditional restriction on images table
569 * @return array
570 */
571function get_search_results($search_id, $images_where='')
572{
573  $search = get_search_array($search_id);
574  if ( !isset($search['q']) )
575  {
576    $result['items'] = get_regular_search_results($search);
577    return $result;
578  }
579  else
580  {
581    return get_quick_search_results($search['q'], $images_where);
582  }
583}
584?>
Note: See TracBrowser for help on using the repository browser.