Ignore:
Timestamp:
Jan 20, 2006, 3:34:37 PM (18 years ago)
Author:
plg
Message:

Search engine redesign, first part :

  • new table #search to store search rules associated to a search id.
  • search rules are not passed through GET anymore, the search array build in search.php is serialized in #search table, so no need to rebuild it in function include/functions_category.inc.php::category_initialize
  • search array build code is improved (efficiency and layout) in search.php
  • SQL related to search is build in a dedicated function include/functions::get_sql_search_clause
  • direct search author:<...>, date_avalaible:<...>, date_creation:<...>, keywords:<...> from picture.php are not available anymore. They will come back later, with improvement (new design). Same for date_*:<> in calendar calendar category.
File:
1 edited

Legend:

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

    r960 r1008  
    742742
    743743/**
    744  * returns the corresponding value from $themeconf if existing. Else, the key is
    745  * returned
     744 * returns the corresponding value from $themeconf if existing. Else, the
     745 * key is returned
    746746 *
    747747 * @param string key
     
    754754  return $themeconf[$key];
    755755}
     756
     757/**
     758 * Prepends and appends a string at each value of the given array.
     759 *
     760 * @param array
     761 * @param string prefix to each array values
     762 * @param string suffix to each array values
     763 */
     764function prepend_append_array_items($array, $prepend_str, $append_str)
     765{
     766  array_walk(
     767    $array,
     768    create_function('&$s', '$s = "'.$prepend_str.'".$s."'.$append_str.'";')
     769    );
     770
     771  return $array;
     772}
     773
     774/**
     775 * returns the SQL clause from a search identifier
     776 *
     777 * Search rules are stored in search table as a serialized array. This array
     778 * need to be transformed into an SQL clause to be used in queries.
     779 *
     780 * @param int search_id
     781 * @return string
     782 */
     783function get_sql_search_clause($search_id)
     784{
     785  if (!is_numeric($search_id))
     786  {
     787    die('Search id must be an integer');
     788  }
     789 
     790  $query = '
     791SELECT rules
     792  FROM '.SEARCH_TABLE.'
     793  WHERE id = '.$_GET['search'].'
     794;';
     795  list($serialized_rules) = mysql_fetch_row(pwg_query($query));
     796 
     797  $search = unserialize($serialized_rules);
     798
     799//   echo '<pre>';
     800//   print_r($search);
     801//   echo '</pre>';
     802
     803  // SQL where clauses are stored in $clauses array during query
     804  // construction
     805  $clauses = array();
     806 
     807  foreach (array('file','name','comment','keywords','author') as $textfield)
     808  {
     809    if (isset($search['fields'][$textfield]))
     810    {
     811      $local_clauses = array();
     812      foreach ($search['fields'][$textfield]['words'] as $word)
     813      {
     814        array_push($local_clauses, $textfield." LIKE '%".$word."%'");
     815      }
     816
     817      // adds brackets around where clauses
     818      $local_clauses = prepend_append_array_items($local_clauses, '(', ')');
     819
     820      array_push(
     821        $clauses,
     822        implode(
     823          ' '.$search['fields'][$textfield]['mode'].' ',
     824          $local_clauses
     825          )
     826        );
     827    }
     828  }
     829 
     830  if (isset($search['fields']['allwords']))
     831  {
     832    $fields = array('file', 'name', 'comment', 'keywords', 'author');
     833    // in the OR mode, request bust be :
     834    // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%')
     835    // OR (field1 LIKE '%word2%' OR field2 LIKE '%word2%'))
     836    //
     837    // in the AND mode :
     838    // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%')
     839    // AND (field1 LIKE '%word2%' OR field2 LIKE '%word2%'))
     840    $word_clauses = array();
     841    foreach ($search['fields']['allwords']['words'] as $word)
     842    {
     843      $field_clauses = array();
     844      foreach ($fields as $field)
     845      {
     846        array_push($field_clauses, $field." LIKE '%".$word."%'");
     847      }
     848      // adds brackets around where clauses
     849      array_push(
     850        $word_clauses,
     851        implode(
     852          "\n          OR ",
     853          $field_clauses
     854          )
     855        );
     856    }
     857   
     858    array_walk(
     859      $word_clauses,
     860      create_function('&$s','$s="(".$s.")";')
     861      );
     862   
     863    array_push(
     864      $clauses,
     865      "\n         ".
     866      implode(
     867        "\n         ".
     868              $search['fields']['allwords']['mode'].
     869        "\n         ",
     870        $word_clauses
     871        )
     872      );
     873  }
     874 
     875  foreach (array('date_available', 'date_creation') as $datefield)
     876  {
     877    if (isset($search['fields'][$datefield]))
     878    {
     879      array_push(
     880        $clauses,
     881        $datefield." = '".$search['fields'][$datefield]['date']."'"
     882        );
     883    }
     884   
     885    foreach (array('after','before') as $suffix)
     886    {
     887      $key = $datefield.'-'.$suffix;
     888     
     889      if (isset($search['fields'][$key]))
     890      {
     891        array_push(
     892          $clauses,
     893         
     894          $datefield.
     895          ($suffix == 'after'             ? ' >' : ' <').
     896          ($search['fields'][$key]['inc'] ? '='  : '').
     897          " '".$search['fields'][$key]['date']."'"
     898         
     899          );
     900      }
     901    }
     902  }
     903 
     904  if (isset($search['fields']['cat']))
     905  {
     906    if ($search['fields']['cat']['sub_inc'])
     907    {
     908      // searching all the categories id of sub-categories
     909      $cat_ids = get_subcat_ids($search['fields']['cat']['words']);
     910    }
     911    else
     912    {
     913      $cat_ids = $search['fields']['cat']['words'];
     914    }
     915   
     916    $local_clause = 'category_id IN ('.implode(',', $cat_ids).')';
     917    array_push($clauses, $local_clause);
     918  }
     919 
     920  // adds brackets around where clauses
     921  $clauses = prepend_append_array_items($clauses, '(', ')');
     922 
     923  $where_separator =
     924    implode(
     925      "\n    ".$search['mode'].' ',
     926      $clauses
     927      );
     928 
     929  $search_clause = $where_separator;
     930 
     931  if (isset($forbidden))
     932  {
     933    $search_clause.= "\n    AND ".$forbidden;
     934  }
     935
     936  return $search_clause;
     937}
    756938?>
Note: See TracChangeset for help on using the changeset viewer.