[ Index ]
 

Code source de e107 0.7.8

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/e107_handlers/ -> search_class.php (source)

   1  <?php
   2  /*
   3  + ----------------------------------------------------------------------------+
   4  |     e107 website system
   5  |
   6  |     ©Steve Dunstan 2001-2002
   7  |     http://e107.org
   8  |     jalist@e107.org
   9  |
  10  |     Released under the terms and conditions of the
  11  |     GNU General Public License (http://gnu.org).
  12  |
  13  |     $Source: /cvsroot/e107/e107_0.7/e107_handlers/search_class.php,v $
  14  |     $Revision: 1.36 $
  15  |     $Date: 2006/04/14 00:28:13 $
  16  |     $Author: sweetas $
  17  +----------------------------------------------------------------------------+
  18  */
  19  
  20  if (!defined('e107_INIT')) { exit; }
  21  
  22  class e_search {
  23      
  24      var $query;
  25      var $text;
  26      var $pos;
  27      var $bullet;
  28      var $keywords;
  29      var $stopwords_php = "|a|about|an|and|are|as|at|be|by|com|edu|for|from|how|i|in|is|it|of|on|or|that|the|this|to|was|what|when|where|who|will|with|the|www|";
  30      var $stopwords_mysql = "|a|a's|able|about|above|according|accordingly|across|actually|after|afterwards|again|against|ain't|all|allow|allows|almost|alone|along|already|also|although|always|am|among|amongst|an|and|another|any|anybody|anyhow|anyone|anything|anyway|anyways|anywhere|apart|appear|appreciate|appropriate|are|aren't|around|as|aside|ask|asking|associated|at|available|away|awfully|be|became|because|become|becomes|becoming|been|before|beforehand|behind|being|believe|below|beside|besides|best|better|between|beyond|both|brief|but|by|c'mon|c's|came|can|can't|cannot|cant|cause|causes|certain|certainly|changes|clearly|co|com|come|comes|concerning|consequently|consider|considering|contain|containing|contains|corresponding|could|couldn't|course|currently|definitely|described|despite|did|didn't|different|do|does|doesn't|doing|don't|done|down|downwards|during|each|edu|eg|eight|either|else|elsewhere|enough|entirely|especially|et|etc|even|ever|every|everybody|everyone|everything|everywhere|ex|exactly|example|except|far|few|fifth|first|five|followed|following|follows|for|former|formerly|forth|four|from|further|furthermore|get|gets|getting|given|gives|go|goes|going|gone|got|gotten|greetings|had|hadn't|happens|hardly|has|hasn't|have|haven't|having|he|he's|hello|help|hence|her|here|here's|hereafter|hereby|herein|hereupon|hers|herself|hi|him|himself|his|hither|hopefully|how|howbeit|however|i|i'd|i'll|i'm|i've|ie|if|ignored|immediate|in|inasmuch|inc|indeed|indicate|indicated|indicates|inner|insofar|instead|into|inward|is|isn't|it|it'd|it'll|it's|its|itself|just|keep|keeps|kept|know|knows|known|last|lately|later|latter|latterly|least|less|lest|let|let's|like|liked|likely|little|look|looking|looks|ltd|mainly|many|may|maybe|me|mean|meanwhile|merely|might|more|moreover|most|mostly|much|must|my|myself|name|namely|nd|near|nearly|necessary|need|needs|neither|never|nevertheless|new|next|nine|no|nobody|non|none|noone|nor|normally|not|nothing|novel|now|nowhere|obviously|of|off|often|oh|ok|okay|old|on|once|one|ones|only|onto|or|other|others|otherwise|ought|our|ours|ourselves|out|outside|over|overall|own|particular|particularly|per|perhaps|php|placed|please|plus|possible|presumably|probably|provides|que|quite|qv|rather|rd|re|really|reasonably|regarding|regardless|regards|relatively|respectively|right|said|same|saw|say|saying|says|second|secondly|see|seeing|seem|seemed|seeming|seems|seen|self|selves|sensible|sent|serious|seriously|seven|several|shall|she|should|shouldn't|since|six|so|some|somebody|somehow|someone|something|sometime|sometimes|somewhat|somewhere|soon|sorry|specified|specify|specifying|still|sub|such|sup|sure|t's|take|taken|tell|tends|th|than|thank|thanks|thanx|that|that's|thats|the|their|theirs|them|themselves|then|thence|there|there's|thereafter|thereby|therefore|therein|theres|thereupon|these|they|they'd|they'll|they're|they've|think|third|this|thorough|thoroughly|those|though|three|through|throughout|thru|thus|to|together|too|took|toward|towards|tried|tries|truly|try|trying|twice|two|un|under|unfortunately|unless|unlikely|until|unto|up|upon|us|use|used|useful|uses|using|usually|value|various|very|via|viz|vs|want|wants|was|wasn't|way|we|we'd|we'll|we're|we've|welcome|well|went|were|weren't|what|what's|whatever|when|whence|whenever|where|where's|whereafter|whereas|whereby|wherein|whereupon|wherever|whether|which|while|whither|who|who's|whoever|whole|whom|whose|why|will|willing|wish|with|within|without|won't|wonder|would|would|wouldn't|yes|yet|you|you'd|you'll|you're|you've|your|yours|yourself|yourselves|zero|";
  31      
  32  	function e_search() {
  33          global $query, $tp;
  34          $this -> query = $query;
  35          $this -> bullet = (defined("BULLET") ? "<img src='".THEME."images/".BULLET."' alt='' style='vertical-align: middle' />" : "<img src='".THEME."images/bullet2.gif' alt='' style='vertical-align: middle' />");
  36          preg_match_all('/(\W?".*?")|(.*?)(\s|$)/', $this -> query, $boolean_keys);
  37          sort($this -> keywords['split'] = array_unique(array_filter(str_replace('"', '', array_merge($boolean_keys[1], $boolean_keys[2])))));
  38          foreach ($this -> keywords['split'] as $k_key => $key) {
  39              if (!$this -> stopword($key)) {
  40                  if ($key{(strlen($key) - 1)} == '*') {
  41                      $this -> keywords['wildcard'][$k_key] = TRUE;
  42                      $key = substr($key, 0, -1);
  43                  } else {
  44                      $this -> keywords['wildcard'][$k_key] = FALSE;
  45                  }
  46                  if ($key{0} == '+') {
  47                      $this -> keywords['boolean'][$k_key] = '+';
  48                      $this -> keywords['match'][$k_key] = substr($key, 1);
  49                  } else if ($key{0} == '-') {
  50                      $this -> keywords['boolean'][$k_key] = '-';
  51                      $this -> keywords['match'][$k_key] = substr($key, 1);
  52                  } else {
  53                      $this -> keywords['boolean'][$k_key] = FALSE;
  54                      $this -> keywords['match'][$k_key] = $key;
  55                  }
  56                  $this -> keywords['exact'][$k_key] = (strpos($key, ' ') !== FALSE) ? TRUE : FALSE;
  57                  $this -> keywords['match'][$k_key] = $tp -> toDB($this -> keywords['match'][$k_key]);
  58              } else {
  59                  unset ($this -> keywords['split'][$k_key]);
  60              }
  61          }
  62      }
  63  
  64  	function parsesearch($table, $return_fields, $search_fields, $weights, $handler, $no_results, $where, $order) {
  65          global $sql, $query, $tp, $search_prefs, $pre_title, $search_chars, $search_res, $result_flag;
  66          $this -> query = $tp -> toDB($query);
  67          if (!$search_prefs['mysql_sort']) {
  68              $field_operator = 'AND ';
  69              foreach ($this -> keywords['match'] as $k_key => $key) {
  70                  $boolean_regex = '';
  71                  if ($this -> keywords['boolean'][$k_key] == '+') {
  72                      $key_operator = 'OR ';
  73                      $break = TRUE;
  74                      $no_exact = TRUE;
  75                  } else if ($this -> keywords['boolean'][$k_key] == '-') {
  76                      foreach ($this -> keywords as $unset_key => $unset_value) {
  77                          unset($this -> keywords[$unset_key][$k_key]);
  78                      }
  79                      $key_operator = 'AND ';
  80                      $boolean_regex = 'NOT';
  81                      $no_exact = TRUE;
  82                  } else if (!isset($break)) {
  83                      $key_operator = 'OR ';
  84                      if (isset($switch)) {
  85                          $field_operator = 'OR ';
  86                      }
  87                      $switch = TRUE;
  88                  } else {
  89                      break;
  90                  }
  91                  $match_query .= isset($uninitial_field) ? " ".$field_operator." (" : "(";
  92                  $uninitial_field = TRUE;
  93                  if ($this -> keywords['wildcard'][$k_key] || !$search_prefs['boundary']) {
  94                      $wildcard = '';
  95                  } else {
  96                      $wildcard = '[[:>:]]';
  97                  }
  98                  $key_count = 1;
  99                  foreach ($search_fields as $field) {
 100                      $regexp = $search_prefs['boundary'] ? "[[:<:]]".$key.$wildcard : $key;
 101                      $match_query .= " ".$field." ".$boolean_regex." REGEXP '".$regexp."' ";
 102                      if ($key_count != count($search_fields)) {
 103                          $match_query .= $key_operator;
 104                      }
 105                      $key_count++;
 106                  }
 107                  $match_query .= ")";
 108              }
 109              if ($order) {
 110                  $sql_order = 'ORDER BY ';
 111                  $order_count = count($order);
 112                  $i = 1;
 113                  foreach ($order as $sort_key => $sort_value) {
 114                      $sql_order .= $sort_key.' '.$sort_value;
 115                      if ($i != $order_count) {
 116                          $sql_order .= ', ';
 117                      }
 118                      $i++;
 119                  }
 120              } else {
 121                  $sql_order = '';
 122              }
 123              $limit = $search_prefs['php_limit'] ? ' LIMIT 0,'.$search_prefs['php_limit'] : '';
 124              $sql_query = "SELECT ".$return_fields." FROM #".$table." WHERE ".$where." (".$match_query.") ".$sql_order.$limit.";";
 125              if ((($keycount = count($this -> keywords['split'])) > 1) && (strpos($query, '"') === FALSE) && (!isset($no_exact))) {
 126                  $exact_query[] = $query;
 127                  $this -> keywords['split'] = array_merge($exact_query, $this -> keywords['split']);
 128              }
 129          } else {
 130              $this -> query = str_replace('&quot;', '"', $this -> query);
 131              $field_query = implode(',', $search_fields);
 132              foreach ($search_fields as $field_key => $field) {
 133                  $search_query[] = "(".$weights[$field_key]." * (MATCH(".$field.") AGAINST ('".$this -> query."' IN BOOLEAN MODE)))";
 134              }
 135              $match_query = implode(' + ', $search_query);
 136              $sql_order = '';
 137              foreach ($order as $sort_key => $sort_value) {
 138                  $sql_order .= ', '.$sort_key.' '.$sort_value;
 139              }
 140              $limit = " LIMIT ".$result_flag.",".$search_res;
 141              $sql_query = "SELECT SQL_CALC_FOUND_ROWS ".$return_fields.", (".$match_query.") AS relevance FROM #".$table." WHERE ".$where." ( MATCH(".$field_query.") AGAINST ('".$this -> query."' IN BOOLEAN MODE) ) HAVING relevance > 0 ORDER BY relevance DESC ".$sql_order.$limit.";";
 142          }
 143  
 144          if ($ps['results'] = $sql -> db_Select_gen($sql_query)) {
 145              if (!$search_prefs['mysql_sort']) {
 146                  $x = 0;
 147                  foreach ($search_fields as $field_key => $field) {
 148                      $crop_fields[] = preg_replace('/(.*?)\./', '', $field);
 149                  }
 150                  while ($row = $sql -> db_Fetch()) {
 151                      $weight = 0;
 152                      foreach ($crop_fields as $field_key => $field) {
 153                          $this -> text = $row[$field];
 154                          foreach ($this -> keywords['match'] as $k_key => $this -> query) {
 155                              if (stristr($this -> text, $this -> query) !== FALSE) {
 156                                  if ($this -> keywords['exact'][$k_key] || $this -> keywords['boolean'][$k_key]) {
 157                                      $weight += (($weights[$field_key] * 2) * ($keycount));
 158                                      $endweight = TRUE;
 159                                  } else if (!$endweight) {
 160                                      $weight += $weights[$field_key];
 161                                  }
 162                              }
 163                          }
 164                          $endweight = FALSE;
 165                          
 166                      }
 167                      foreach ($row as $r_key => $r_value) {
 168                          $qrow[$x][$r_key] = $r_value;
 169                          $qrow[$x]['relevance'] = $weight;
 170                          $qrow[$x]['search_id'] = $x;
 171                      }
 172                      $x++;
 173                  }
 174      
 175                  foreach($qrow as $info) {
 176                      $sortarr[] = $info['relevance'];
 177                  }
 178                  array_multisort($sortarr, SORT_DESC, $qrow, SORT_DESC);
 179                  
 180                  $result_number = ($x < ($result_flag + $search_res)) ? $x : $result_flag + $search_res;
 181                  for ($i = $result_flag; $i < $result_number; $i++) {
 182                      $display_row[] = $qrow[$i];
 183                  }
 184  
 185              } else {
 186                  $x = 0;
 187                  while ($row = $sql -> db_Fetch()) {
 188                      $display_row[] = $row;
 189                      $x++;
 190                  }
 191              }
 192  
 193              foreach ($display_row as $row) {
 194                  $res = call_user_func($handler, $row);
 195                  if (!$res['omit_result']) {
 196                      $matches = array($res['title'], $res['summary']);
 197                      $endcrop = FALSE;
 198                      $output = '';
 199                      $title = TRUE;
 200                      foreach ($matches as $this -> text) {
 201                          $this -> text = nl2br($this -> text);
 202                          $t_search = $tp -> search;
 203                          $t_replace = $tp -> replace;
 204                          $s_search = array('<br />', '[', ']');
 205                          $s_replace = array(' ', '<', '>');
 206                          $search = array_merge($t_search, $s_search);
 207                          $replace = array_merge($t_replace, $s_replace);
 208                          $this -> text = strip_tags(str_replace($search, $replace, $this -> text));
 209                          foreach ($this -> keywords['match'] as $match_id => $this -> query) {
 210                              $boundary = $search_prefs['boundary'] ? '\b' : '';
 211                              if ($this -> keywords['wildcard'][$match_id]) {
 212                                  $regex_append = ".*?".$boundary.")";
 213                              } else {
 214                                  $regex_append = $boundary.")";    
 215                              }
 216                              if (($match_start = stristr($this -> text, $this -> query)) !== FALSE) {
 217                                  $this -> pos = strlen($this -> text) - strlen($match_start);
 218                                  if (!$endcrop && !$title) {
 219                                      $this -> parsesearch_crop();
 220                                      $endcrop = TRUE;
 221                                  }
 222                                  $key = substr($this -> text, $this -> pos, strlen($this -> query));
 223                                  $this -> text = preg_replace("#(".$boundary.$this -> query.$regex_append."#i", "<span class='searchhighlight'>\\1</span>", $this -> text);
 224                              }
 225                          }
 226                          if ($title) {
 227                              if ($pre_title == 0) {
 228                                  $pre_title_output = "";
 229                              } else if ($pre_title == 1) {
 230                                  $pre_title_output = $res['pre_title'];
 231                              } else if ($pre_title == 2) {
 232                                  $pre_title_output = $pre_title;
 233                              }
 234                              $this -> text = $this -> bullet." <b><a class='visit' href='".$res['link']."'>".$pre_title_output.$this -> text."</a></b><br />".$res['pre_summary'];
 235                          } else if (!$endcrop) {
 236                              $this -> parsesearch_crop();
 237                          }
 238                          $output .= $this -> text;
 239                          $title = FALSE;
 240                      }
 241                      $display_rel = $search_prefs['relevance'] ? " | ".LAN_SEARCH_69.": ".round($row['relevance'], 1) : "";
 242                      $output_array['text'][] = $output.$res['post_summary']."<br /><span class='smalltext'>".$res['detail'].$display_rel."</span><br /><br />";
 243                  } else {
 244                      $ps['results']--;
 245                      $res['omit_result'] = FALSE;
 246                  }
 247              }
 248              $ps_limit = $output_array['text'];
 249              $result_number = ($x < $search_res) ? $x : $search_res;
 250              for ($i = 0; $i < $result_number; $i++) {
 251                  $ps['text'] .= $ps_limit[$i];
 252              }
 253          } else {
 254              $ps['text'] = $no_results;
 255          }
 256          if ($search_prefs['mysql_sort']) {
 257              $sql -> db_Query("SELECT FOUND_ROWS()");
 258              $frows = $sql -> db_Fetch();
 259              $ps['results'] = $frows[0];
 260          }
 261          return $ps;
 262      }
 263      
 264  	function parsesearch_crop() {
 265          global $search_chars;
 266          if (strlen($this -> text) > $search_chars) {
 267              if ($this -> pos < ($search_chars - strlen($this -> query))) {
 268                  $this -> text = substr($this -> text, 0, $search_chars)."...";
 269              } else if ($this -> pos > (strlen($this -> text) - ($search_chars - strlen($this -> query)))) {
 270                  $this -> text = "...".substr($this -> text, (strlen($this -> text) - ($search_chars - strlen($this -> query))));
 271              } else {
 272                  $this -> text = "...".substr($this -> text, ($this -> pos - round(($search_chars / 3))), $search_chars)."...";
 273              }
 274              $match_start = stristr($this -> text, $this -> query);
 275              $this -> pos = strlen($this -> text) - strlen($match_start);
 276          }
 277      }
 278      
 279  	function stopword($key) {
 280          global $search_prefs;
 281          if ($search_prefs['mysql_sort'] && ($key{0} == '+')) {
 282              $key = substr($key, 1);
 283          }
 284          if (($key{(strlen($key) - 1)} != '*') && ($key{0} != '+')) {
 285              if (strlen($key) > 2) {
 286                  if ($search_prefs['mysql_sort']) {
 287                      $stopword_list = $this -> stopwords_mysql;
 288                  } else {
 289                      $stopword_list = $this -> stopwords_php;
 290                  }
 291                  if (strpos($stopword_list, '|'.$key.'|') !== FALSE) {
 292                      $this -> stop_keys[] = $key;
 293                      return TRUE;
 294                  } else {
 295                      return FALSE;
 296                  }
 297              } else {
 298                  $this -> stop_keys[] = $key;
 299                  return TRUE;
 300              }
 301          } else {
 302              return FALSE;
 303          }
 304      }
 305  }
 306  
 307  ?>


Généré le : Sun Apr 1 01:23:32 2007 par Balluche grâce à PHPXref 0.7