[ Index ]

PHP Cross Reference of Nucleus CMS 3.32

title

Body

[close]

/nucleus/libs/ -> SEARCH.php (source)

   1  <?php
   2  
   3  /*
   4   * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
   5   * Copyright (C) 2003-2007 The Nucleus Group
   6   *
   7   * This program is free software; you can redistribute it and/or
   8   * modify it under the terms of the GNU General Public License
   9   * as published by the Free Software Foundation; either version 2
  10   * of the License, or (at your option) any later version.
  11   * (see nucleus/documentation/index.html#license for more info)
  12   */
  13  /**
  14   * SEARCH(querystring) offers different functionality to create an
  15   * SQL query to find certain items. (and comments)
  16   *
  17   * based on code by David Altherr:
  18   * http://www.evolt.org/article/Boolean_Fulltext_Searching_with_PHP_and_MySQL/18/15665/
  19   * http://davidaltherr.net/web/php_functions/boolean/funcs.mysql.boolean.txt
  20   *
  21   * @license http://nucleuscms.org/license.txt GNU General Public License
  22   * @copyright Copyright (C) 2002-2007 The Nucleus Group
  23   * @version $Id: SEARCH.php 1116 2007-02-03 08:24:29Z kimitake $
  24   */
  25  
  26  
  27  
  28  class SEARCH {
  29  
  30      var $querystring;
  31      var $marked;
  32      var $inclusive;
  33      var $blogs;
  34  
  35  
  36  	function SEARCH($text) {
  37          global $blogid;
  38          $text = preg_replace ("/[<,>,=,?,!,#,^,(,),[,\],:,;,\\\,%]/","",$text);
  39          $this->querystring    = $text;
  40          $this->marked        = $this->boolean_mark_atoms($text);
  41          $this->inclusive    = $this->boolean_inclusive_atoms($text);
  42          $this->blogs        = array();
  43  
  44          // get all public searchable blogs, no matter what, include the current blog allways.
  45          $res = sql_query('SELECT bnumber FROM '.sql_table('blog').' WHERE bincludesearch=1 ');
  46          while ($obj = mysql_fetch_object($res))
  47              $this->blogs[] = intval($obj->bnumber);
  48      }
  49  
  50  	function  boolean_sql_select($match){
  51          if (strlen($this->inclusive) > 0) {
  52             /* build sql for determining score for each record */
  53             $result=explode(" ",$this->inclusive);
  54             for($cth=0;$cth<count($result);$cth++){
  55                 if(strlen($result[$cth])>=4){
  56                     $stringsum_long .=  " $result[$cth] ";
  57                 }else{
  58                     $stringsum_a[] = ' '.$this->boolean_sql_select_short($result[$cth],$match).' ';
  59                 }
  60             }
  61  
  62             if(strlen($stringsum_long)>0){
  63                  $stringsum_long = addslashes($stringsum_long);
  64                  $stringsum_a[] = " match ($match) against ('$stringsum_long') ";
  65             }
  66  
  67             $stringsum .= implode("+",$stringsum_a);
  68             return $stringsum;
  69          }
  70      }
  71  
  72  	function boolean_inclusive_atoms($string){
  73          $result=trim($string);
  74          $result=preg_replace("/([[:space:]]{2,})/",' ',$result);
  75  
  76          /* convert normal boolean operators to shortened syntax */
  77          $result=eregi_replace(' not ',' -',$result);
  78          $result=eregi_replace(' and ',' ',$result);
  79          $result=eregi_replace(' or ',',',$result);
  80  
  81          /* drop unnecessary spaces */
  82          $result=str_replace(' ,',',',$result);
  83          $result=str_replace(', ',',',$result);
  84          $result=str_replace('- ','-',$result);
  85          $result=str_replace('+','',$result);
  86  
  87          /* strip exlusive atoms */
  88          $result=preg_replace(
  89              "(\-\([A-Za-z0-9]{1,}[A-Za-z0-9\-\.\_\,]{0,}\))",
  90              '',
  91              $result);
  92  
  93          $result=str_replace('(',' ',$result);
  94          $result=str_replace(')',' ',$result);
  95          $result=str_replace(',',' ',$result);
  96  
  97          return $result;
  98      }
  99  
 100  	function boolean_sql_where($match){
 101          $result = $this->marked;
 102          $result = preg_replace(
 103              "/foo\[\(\'([^\)]{4,})\'\)\]bar/e",
 104              " 'match ('.\$match.') against (\''.\$this->copyvalue(\"$1\").'\') > 0 ' ",
 105              $result);
 106  
 107          $result = preg_replace(
 108              "/foo\[\(\'([^\)]{1,3})\'\)\]bar/e",
 109              " '('.\$this->boolean_sql_where_short(\"$1\",\"$match\").')' ",
 110              $result);
 111          return $result;
 112      }
 113  
 114      // there must be a simple way to simply copy a value with backslashes in it through
 115      // the preg_replace, but I cannot currently find it (karma 2003-12-30)
 116  	function copyvalue($foo) {
 117          return $foo;
 118      }
 119  
 120  
 121  	function boolean_mark_atoms($string){
 122          $result=trim($string);
 123          $result=preg_replace("/([[:space:]]{2,})/",' ',$result);
 124  
 125          /* convert normal boolean operators to shortened syntax */
 126          $result=eregi_replace(' not ',' -',$result);
 127          $result=eregi_replace(' and ',' ',$result);
 128          $result=eregi_replace(' or ',',',$result);
 129  
 130  
 131          /* strip excessive whitespace */
 132          $result=str_replace('( ','(',$result);
 133          $result=str_replace(' )',')',$result);
 134          $result=str_replace(', ',',',$result);
 135          $result=str_replace(' ,',',',$result);
 136          $result=str_replace('- ','-',$result);
 137          $result=str_replace('+','',$result);
 138  
 139          // remove double spaces (we might have introduced some new ones above)
 140          $result=trim($result);
 141          $result=preg_replace("/([[:space:]]{2,})/",' ',$result);
 142  
 143          /* apply arbitrary function to all 'word' atoms */
 144  
 145          $result_a = explode(" ",$result);
 146          for($word=0;$word<count($result_a);$word++){
 147              $result_a[$word] = "foo[('".$result_a[$word]."')]bar";
 148          }
 149          $result = implode(" ",$result_a);
 150  
 151          /* dispatch ' ' to ' AND ' */
 152          $result=str_replace(' ',' AND ',$result);
 153  
 154          /* dispatch ',' to ' OR ' */
 155          $result=str_replace(',',' OR ',$result);
 156  
 157          /* dispatch '-' to ' NOT ' */
 158          $result=str_replace(' -',' NOT ',$result);
 159          return $result;
 160      }
 161  
 162  	function boolean_sql_where_short($string,$match){
 163          $match_a = explode(',',$match);
 164          for($ith=0;$ith<count($match_a);$ith++){
 165              $like_a[$ith] = " $match_a[$ith] LIKE '% $string %' ";
 166          }
 167          $like = implode(" OR ",$like_a);
 168  
 169          return $like;
 170      }
 171  	function boolean_sql_select_short($string,$match){
 172          $match_a = explode(',',$match);
 173          $score_unit_weight = .2;
 174          for($ith=0;$ith<count($match_a);$ith++){
 175              $score_a[$ith] =
 176                             " $score_unit_weight*(
 177                             LENGTH(" . addslashes($match_a[$ith]) . ") -
 178                             LENGTH(REPLACE(LOWER(" . addslashes($match_a[$ith]) . "),LOWER('" . addslashes($string) . "'),'')))
 179                             /LENGTH('" . addslashes($string) . "') ";
 180          }
 181          $score = implode(" + ",$score_a);
 182  
 183          return $score;
 184      }
 185  }
 186  ?>


Generated: Tue Feb 12 15:34:36 2008 Cross-referenced by PHPXref 0.7