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

Last change on this file since 1744 was 1744, checked in by rvelices, 17 years ago
  • revert feature 564: log the login of each user; but add the possibility to be

done by a plugin

  • create a "standard" way to define PHP functions that we use but might not be

available in the current php version

  • when a comment is rejected (spam, anti-flood etc), put the content back to the

browser in case there is a real user behind it

  • now a comment can be entered only if the page was retrieved between 2 seconds

ago and 1 hour ago

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.9 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2006 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $Id: functions_search.inc.php 1744 2007-01-23 01:22:52Z rvelices $
9// | last update   : $Date: 2007-01-23 01:22:52 +0000 (Tue, 23 Jan 2007) $
10// | last modifier : $Author: rvelices $
11// | revision      : $Revision: 1744 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
27
28
29/**
30 * returns search rules stored into a serialized array in "search"
31 * table. Each search rules set is numericaly identified.
32 *
33 * @param int search_id
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;';
48  list($serialized_rules) = mysql_fetch_row(pwg_query($query));
49
50  return unserialize($serialized_rules);
51}
52
53/**
54 * returns the SQL clause from a search identifier
55 *
56 * Search rules are stored in search table as a serialized array. This array
57 * need to be transformed into an SQL clause to be used in queries.
58 *
59 * @param array search
60 * @return string
61 */
62function get_sql_search_clause($search)
63{
64  // SQL where clauses are stored in $clauses array during query
65  // construction
66  $clauses = array();
67
68  foreach (array('file','name','comment','author') as $textfield)
69  {
70    if (isset($search['fields'][$textfield]))
71    {
72      $local_clauses = array();
73      foreach ($search['fields'][$textfield]['words'] as $word)
74      {
75        array_push($local_clauses, $textfield." LIKE '%".$word."%'");
76      }
77
78      // adds brackets around where clauses
79      $local_clauses = prepend_append_array_items($local_clauses, '(', ')');
80
81      array_push(
82        $clauses,
83        implode(
84          ' '.$search['fields'][$textfield]['mode'].' ',
85          $local_clauses
86          )
87        );
88    }
89  }
90
91  if (isset($search['fields']['allwords']))
92  {
93    $fields = array('file', 'name', 'comment', 'author');
94    // in the OR mode, request bust be :
95    // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%')
96    // OR (field1 LIKE '%word2%' OR field2 LIKE '%word2%'))
97    //
98    // in the AND mode :
99    // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%')
100    // AND (field1 LIKE '%word2%' OR field2 LIKE '%word2%'))
101    $word_clauses = array();
102    foreach ($search['fields']['allwords']['words'] as $word)
103    {
104      $field_clauses = array();
105      foreach ($fields as $field)
106      {
107        array_push($field_clauses, $field." LIKE '%".$word."%'");
108      }
109      // adds brackets around where clauses
110      array_push(
111        $word_clauses,
112        implode(
113          "\n          OR ",
114          $field_clauses
115          )
116        );
117    }
118
119    array_walk(
120      $word_clauses,
121      create_function('&$s','$s="(".$s.")";')
122      );
123
124    array_push(
125      $clauses,
126      "\n         ".
127      implode(
128        "\n         ".
129              $search['fields']['allwords']['mode'].
130        "\n         ",
131        $word_clauses
132        )
133      );
134  }
135
136  foreach (array('date_available', 'date_creation') as $datefield)
137  {
138    if (isset($search['fields'][$datefield]))
139    {
140      array_push(
141        $clauses,
142        $datefield." = '".$search['fields'][$datefield]['date']."'"
143        );
144    }
145
146    foreach (array('after','before') as $suffix)
147    {
148      $key = $datefield.'-'.$suffix;
149
150      if (isset($search['fields'][$key]))
151      {
152        array_push(
153          $clauses,
154
155          $datefield.
156          ($suffix == 'after'             ? ' >' : ' <').
157          ($search['fields'][$key]['inc'] ? '='  : '').
158          " '".$search['fields'][$key]['date']."'"
159
160          );
161      }
162    }
163  }
164
165  if (isset($search['fields']['cat']))
166  {
167    if ($search['fields']['cat']['sub_inc'])
168    {
169      // searching all the categories id of sub-categories
170      $cat_ids = get_subcat_ids($search['fields']['cat']['words']);
171    }
172    else
173    {
174      $cat_ids = $search['fields']['cat']['words'];
175    }
176
177    $local_clause = 'category_id IN ('.implode(',', $cat_ids).')';
178    array_push($clauses, $local_clause);
179  }
180
181  // adds brackets around where clauses
182  $clauses = prepend_append_array_items($clauses, '(', ')');
183
184  $where_separator =
185    implode(
186      "\n    ".$search['mode'].' ',
187      $clauses
188      );
189
190  $search_clause = $where_separator;
191
192  return $search_clause;
193}
194
195/**
196 * returns the list of items corresponding to the advanced search array
197 *
198 * @param array search
199 * @return array
200 */
201function get_regular_search_results($search)
202{
203  $items = array();
204
205  $search_clause = get_sql_search_clause($search);
206
207  if (!empty($search_clause))
208  {
209    $query = '
210SELECT DISTINCT(id)
211  FROM '.IMAGES_TABLE.'
212    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
213  WHERE '.$search_clause.'
214;';
215    $items = array_from_query($query, 'id');
216  }
217
218  if (isset($search['fields']['tags']))
219  {
220    $tag_items = get_image_ids_for_tags(
221      $search['fields']['tags']['words'],
222      $search['fields']['tags']['mode']
223      );
224
225    switch ($search['mode'])
226    {
227      case 'AND':
228      {
229        if (empty($search_clause))
230        {
231          $items = $tag_items;
232        }
233        else
234        {
235          $items = array_intersect($items, $tag_items);
236        }
237        break;
238      }
239      case 'OR':
240      {
241        $items = array_unique(
242          array_merge(
243            $items,
244            $tag_items
245            )
246          );
247        break;
248      }
249    }
250  }
251
252  return $items;
253}
254
255/**
256 * returns the LIKE sql clause corresponding to the quick search query $q
257 * and the field $field. example q="john bill", field="file" will return
258 * file LIKE "%john%" OR file LIKE "%bill%". Special characters for MySql
259 * full text search (+,<,>) are omitted.
260 * @param string q
261 * @param string field
262 * @return string
263 */
264function get_qsearch_like_clause($q, $field)
265{
266  $tokens = preg_split('/[\s,.;!\?]+/', $q);
267  for ($i=0; $i<count($tokens); $i++)
268  {
269    $tokens[$i]=str_replace('*','%', $tokens[$i]);
270    if (preg_match('/^[+<>]/',$tokens[$i]) )
271      $tokens[$i]=substr($tokens[$i], 1);
272    else if (substr($tokens[$i], 0, 1)=='-')
273    {
274      unset($tokens[$i]);
275      $i--;
276    }
277  }
278
279  if (!empty($tokens))
280  {
281    $query = '(';
282    for ($i=0; $i<count($tokens); $i++)
283    {
284      if ($i>0) $query .= 'OR ';
285      $query .= ' '.$field.' LIKE "%'.$tokens[$i].'%" ';
286    }
287    $query .= ')';
288    return $query;
289  }
290  return null;
291}
292
293
294/**
295 * returns the search results (array of image ids) corresponding to a
296 * quick/query search. A quick/query search returns many items (search is
297 * not strict), but results are sorted by relevance.
298 *
299 * @param string q
300 * @return array
301 */
302function get_quick_search_results($q)
303{
304  global $user, $page, $filter;
305  $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 = '
315SELECT 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 = '
322SELECT 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
334  // prepare the big join on images, comments and categories
335  $query = '
336SELECT
337  i.id, i.file, CAST( CONCAT_WS(" ",
338    IFNULL(i.name,""),
339    IFNULL(i.comment,""),
340    IFNULL(GROUP_CONCAT(DISTINCT co.content),""),
341    IFNULL(GROUP_CONCAT(DISTINCT c.dir),""),
342    IFNULL(GROUP_CONCAT(DISTINCT c.name),""),
343    IFNULL(GROUP_CONCAT(DISTINCT c.comment),"") ) AS CHAR) AS ft
344FROM (
345  (
346    '.IMAGES_TABLE.' i LEFT JOIN '.COMMENTS_TABLE.' co on i.id=co.image_id
347  )
348    INNER JOIN
349  '.IMAGE_CATEGORY_TABLE.' ic on ic.image_id=i.id
350  )
351    INNER JOIN
352  '.CATEGORIES_TABLE.' c on c.id=ic.category_id
353'.get_sql_condition_FandF
354  (
355    array
356      (
357        'forbidden_categories' => 'category_id',
358        'visible_categories' => 'category_id',
359        'visible_images' => 'ic.image_id'
360      ),
361    'WHERE'
362  ).'
363GROUP BY i.id';
364
365  $query = 'SELECT id, MATCH(ft) AGAINST( "'.$q.'" IN BOOLEAN MODE) AS q FROM ('.$query.') AS Y
366WHERE MATCH(ft) AGAINST( "'.$q.'" IN BOOLEAN MODE)';
367
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
376  $by_weights=array();
377  $result = pwg_query($query);
378  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);
387  }
388
389  //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
397  {
398    // before returning the result "as is", make sure the user has the
399    // permissions for every item
400    $query = '
401SELECT DISTINCT(id)
402  FROM '.IMAGES_TABLE.'
403    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
404  WHERE id IN ('.implode(',', array_keys($by_tag_weights) ).')
405'.get_sql_condition_FandF
406  (
407    array
408      (
409        'forbidden_categories' => 'category_id',
410        'visible_categories' => 'category_id',
411        'visible_images' => 'ic.image_id'
412      ),
413    'AND'
414  );
415    $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  }
423  return $search_results;
424}
425
426/**
427 * returns an array of 'items' corresponding to the search id
428 *
429 * @param int search id
430 * @return array
431 */
432function get_search_results($search_id)
433{
434  $search = get_search_array($search_id);
435  if ( !isset($search['q']) )
436  {
437    $result['items'] = get_regular_search_results($search);
438    return $result;
439  }
440  else
441  {
442    return get_quick_search_results($search['q']);
443  }
444}
445?>
Note: See TracBrowser for help on using the repository browser.