[ Index ]

PHP Cross Reference of Nucleus CMS v3.51 code documentation

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-2009 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-2009 The Nucleus Group

  23   * @version $Id: SEARCH.php 1388 2009-07-18 06:31:28Z shizuki $

  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 = sql_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  
 102          $result = $this->marked;
 103  
 104          $this->boolean_sql_where_cb1($match); // set the static $match

 105  
 106          $result = preg_replace_callback(
 107  
 108              "/foo\[\(\'([^\)]{4,})\'\)\]bar/",
 109  
 110              array($this,'boolean_sql_where_cb1'),
 111  
 112              $result);
 113  
 114          $this->boolean_sql_where_cb2($match); // set the static $match

 115  
 116          $result = preg_replace_callback(
 117  
 118              "/foo\[\(\'([^\)]{1,3})\'\)\]bar/",
 119  
 120              array($this,'boolean_sql_where_cb2'),
 121  
 122              $result);
 123  
 124          return $result;
 125  
 126      }
 127  
 128      function boolean_sql_where_cb1($matches){
 129  
 130          static $match;
 131  
 132          if (!is_array($matches)) $match=$matches;
 133  
 134          else return ' match ('.$match.') against (\''.addslashes($matches[1]).'\') > 0 ';
 135  
 136      }
 137  
 138      function boolean_sql_where_cb2($matches){
 139  
 140          static $match;
 141  
 142          if (!is_array($matches)) $match=$matches;
 143  
 144          else return ' ('.$this->boolean_sql_where_short(addslashes($mathes[1]),$match).') ';
 145  
 146      }    
 147  
 148  	function boolean_mark_atoms($string){
 149          $result=trim($string);
 150          $result=preg_replace("/([[:space:]]{2,})/",' ',$result);
 151  
 152          /* convert normal boolean operators to shortened syntax */

 153          $result=eregi_replace(' not ',' -',$result);
 154          $result=eregi_replace(' and ',' ',$result);
 155          $result=eregi_replace(' or ',',',$result);
 156  
 157  
 158          /* strip excessive whitespace */

 159          $result=str_replace('( ','(',$result);
 160          $result=str_replace(' )',')',$result);
 161          $result=str_replace(', ',',',$result);
 162          $result=str_replace(' ,',',',$result);
 163          $result=str_replace('- ','-',$result);
 164          $result=str_replace('+','',$result);
 165  
 166          // remove double spaces (we might have introduced some new ones above)

 167          $result=trim($result);
 168          $result=preg_replace("/([[:space:]]{2,})/",' ',$result);
 169  
 170          /* apply arbitrary function to all 'word' atoms */

 171  
 172          $result_a = explode(" ",$result);
 173          for($word=0;$word<count($result_a);$word++){
 174              $result_a[$word] = "foo[('".$result_a[$word]."')]bar";
 175          }
 176          $result = implode(" ",$result_a);
 177  
 178          /* dispatch ' ' to ' AND ' */

 179          $result=str_replace(' ',' AND ',$result);
 180  
 181          /* dispatch ',' to ' OR ' */

 182          $result=str_replace(',',' OR ',$result);
 183  
 184          /* dispatch '-' to ' NOT ' */

 185          $result=str_replace(' -',' NOT ',$result);
 186          return $result;
 187      }
 188  
 189  	function boolean_sql_where_short($string,$match){
 190          $match_a = explode(',',$match);
 191          for($ith=0;$ith<count($match_a);$ith++){
 192              $like_a[$ith] = " $match_a[$ith] LIKE '% $string %' ";
 193          }
 194          $like = implode(" OR ",$like_a);
 195  
 196          return $like;
 197      }
 198  	function boolean_sql_select_short($string,$match){
 199          $match_a = explode(',',$match);
 200          $score_unit_weight = .2;
 201          for($ith=0;$ith<count($match_a);$ith++){
 202              $score_a[$ith] =
 203                             " $score_unit_weight*(
 204                             LENGTH(" . addslashes($match_a[$ith]) . ") -
 205                             LENGTH(REPLACE(LOWER(" . addslashes($match_a[$ith]) . "),LOWER('" . addslashes($string) . "'),'')))
 206                             /LENGTH('" . addslashes($string) . "') ";
 207          }
 208          $score = implode(" + ",$score_a);
 209  
 210          return $score;
 211      }
 212  }
 213  ?>


Generated: Sun Aug 1 03:56:06 2010
Open Source related documentation for developers.