4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5 * Copyright (C) 2003-2009 The Nucleus Group
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)
13 * SEARCH(querystring) offers different functionality to create an
14 * SQL query to find certain items. (and comments)
16 * based on code by David Altherr:
17 * http://www.evolt.org/article/Boolean_Fulltext_Searching_with_PHP_and_MySQL/18/15665/
18 * http://davidaltherr.net/web/php_functions/boolean/funcs.mysql.boolean.txt
28 function SEARCH($text) {
30 // $text = preg_replace ("/[<,>,=,?,!,#,^,(,),[,\],:,;,\\\,%]/","",$text);
31 /* * * for jp * * * * * * * * * * */
32 $this->encoding = strtolower(preg_replace('|[^a-z0-9-_]|i', '', _CHARSET));
33 if ($this->encoding != 'utf-8') {
34 $text = mb_convert_encoding($text, "UTF-8", $this->encoding);
36 $text = str_replace ("\xE3\x80\x80",' ',$text);
37 $text = preg_replace ("/[<>=?!#^()[\]:;\\%]/","",$text);
39 $this->ascii = '[\x00-\x7F]';
40 $this->two = '[\xC0-\xDF][\x80-\xBF]';
41 $this->three = '[\xE0-\xEF][\x80-\xBF][\x80-\xBF]';
43 $this->jpmarked = $this->boolean_mark_atoms_jp($text);
44 /* * * * * * * * * * * * * * * * */
46 $this->querystring = $text;
47 // $this->marked = $this->boolean_mark_atoms($text);
48 $this->inclusive = $this->boolean_inclusive_atoms($text);
49 $this->blogs = array();
51 // get all public searchable blogs, no matter what, include the current blog allways.
52 $res = sql_query('SELECT bnumber FROM ' . sql_table('blog') . ' WHERE bincludesearch=1 ');
53 while ($obj = sql_fetch_object($res)) {
54 $this->blogs[] = intval($obj->bnumber);
58 function boolean_sql_select($match) {
59 if (!isset($stringsum)) {
62 if (strlen($this->inclusive) > 0) {
63 /* build sql for determining score for each record */
64 $result=explode(" ",$this->inclusive);
65 if (!isset($stringsum_long)) {
68 for ($cth = 0; $cth < count($result); $cth++) {
69 if (strlen($result[$cth])>=4) {
70 $stringsum_long .= " $result[$cth] ";
72 $stringsum_a[] = ' ' . $this->boolean_sql_select_short($result[$cth], $match) . ' ';
76 if (strlen($stringsum_long) > 0) {
77 $stringsum_long = sql_real_escape_string($stringsum_long);
78 $stringsum_a[] = " match ($match) against ('$stringsum_long') ";
81 $stringsum .= implode("+", $stringsum_a);
89 function boolean_inclusive_atoms($string) {
90 $result = trim($string);
91 $result = preg_replace("#([[:space:]]{2,})#", ' ', $result);
93 # replaced eregi_replace() below with preg_replace(). ereg* functions are deprecated in PHP 5.3.0
94 # just added delimiters to regex and the 'i' for case-insensitive matching
96 /* convert normal boolean operators to shortened syntax */
97 $result = preg_replace('# not #i', ' -', $result);
98 $result = preg_replace('# and #i', ' ', $result);
99 $result = preg_replace('# or #i', ',', $result);
101 /* drop unnecessary spaces */
102 $result = str_replace(' ,', ',', $result);
103 $result = str_replace(', ', ',', $result);
104 $result = str_replace('- ', '-', $result);
105 $result = str_replace('+', '', $result);
107 /* strip exlusive atoms */
108 $result = preg_replace(
109 "#\-\(([A-Za-z0-9]|$this->two|$this->three){1,}([A-Za-z0-9\-\.\_\,]|$this->two|$this->three){0,}\)#",
113 $result = str_replace('(', ' ', $result);
114 $result = str_replace(')', ' ', $result);
115 $result = str_replace(',', ' ', $result);
116 if ($this->encoding != 'utf-8') {
117 $result = mb_convert_encoding($result, $this->encoding, "UTF-8");
122 function boolean_sql_where($match) {
124 $result = $this->marked;
125 $result = preg_replace(
126 "/foo\[\(\'([^\)]{4,})\'\)\]bar/e",
127 " 'match ('.\$match.') against (\''.\$this->copyvalue(\"$1\").'\') > 0 ' ",
129 $result = preg_replace(
130 "/foo\[\(\'([^\)]{1,3})\'\)\]bar/e",
131 " '('.\$this->boolean_sql_where_short(\"$1\",\"$match\").')' ",
134 $result = $this->jpmarked; /* for jp */
135 $result = $this->boolean_sql_where_jp_short($result, $match);/* for jp */
136 if ($this->encoding != 'utf-8') {
137 $result = mb_convert_encoding($result, $this->encoding, "UTF-8");
142 // there must be a simple way to simply copy a value with backslashes in it through
143 // the preg_replace, but I cannot currently find it (karma 2003-12-30)
144 function copyvalue($foo) {
148 function boolean_mark_atoms($string){
149 $result=trim($string);
150 $result=preg_replace("/([[:space:]]{2,})/",' ',$result);
152 # replaced eregi_replace() below with preg_replace(). ereg* functions are deprecated in PHP 5.3.0
153 # just added delimiters to regex and the 'i' for case-insensitive matching
155 $result = preg_replace('# not #i', ' -', $result);
156 $result = preg_replace('# and #i', ' ', $result);
157 $result = preg_replace('# or #i', ',', $result);
159 // strip excessive whitespace
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 $result=str_replace('+','',$result);
167 // remove double spaces (we might have introduced some new ones above)
168 $result=trim($result);
169 $result=preg_replace("#([[:space:]]{2,})#",' ',$result);
171 // apply arbitrary function to all 'word' atoms
173 $result_a = explode(' ',$result);
174 for($word=0;$word<count($result_a);$word++)
176 $result_a[$word] = "foo[('".$result_a[$word]."')]bar";
178 $result = implode(" ",$result_a);
180 // dispatch ' ' to ' AND '
181 $result=str_replace(' ',' AND ',$result);
183 // dispatch ',' to ' OR '
184 $result=str_replace(',',' OR ',$result);
186 // dispatch '-' to ' NOT '
187 $result=str_replace(' -',' NOT ',$result);
191 function boolean_sql_where_short($string,$match){
192 $match_a = explode(',',$match);
193 for($ith=0;$ith<count($match_a);$ith++){
194 $like_a[$ith] = " $match_a[$ith] LIKE '% $string %' ";
196 $like = implode(" OR ",$like_a);
202 function boolean_sql_select_short($string, $match) {
203 $match_a = explode(',', $match);
204 $score_unit_weight = .2;
205 for ($ith = 0; $ith< count($match_a); $ith++){
207 " $score_unit_weight*(
208 LENGTH(" . sql_real_escape_string($match_a[$ith]) . ") -
209 LENGTH(REPLACE(LOWER(" . sql_real_escape_string($match_a[$ith]) . "),LOWER('" . sql_real_escape_string($string) . "'),'')))
210 /LENGTH('" . sql_real_escape_string($string) . "') ";
212 $score = implode(" + ", $score_a);
217 /***********************************************
219 ***********************************************/
221 function boolean_mark_atoms_jp($string) {
222 $result = trim($string);
223 $result = preg_replace("/([[:space:]]{2,})/", ' ', $result);
225 /* convert normal boolean operators to shortened syntax */
226 $result = preg_replace('# not #i', ' -', $result);
227 $result = preg_replace('# and #i', ' ', $result);
228 $result = preg_replace('# or #i', ',', $result);
230 /* strip excessive whitespace */
231 $result = str_replace(', ', ',', $result);
232 $result = str_replace(' ,', ',', $result);
233 $result = str_replace('- ', '-', $result);
234 $result = str_replace('+', '', $result);
235 $result = str_replace(',', ' ,', $result);
240 function boolean_sql_where_jp_short($string, $match) {
241 $match_a = explode(',', $match);
242 $key_a = explode(' ', $string);
244 for ($ith=0; $ith<count($match_a); $ith++) {
245 // $temp_a[$ith] = "(i.$match_a[$ith] LIKE '%" . sql_real_escape_string($key_a[0]) . "%') ";
246 $binKey = preg_match('/[a-zA-Z]/', $key_a[0]) ? '' : 'BINARY';
247 $temp_a[$ith] = "(i.$match_a[$ith] LIKE " . $binKey . " '%" . sql_real_escape_string($key_a[0]) . "%') ";
249 $like = '('.implode(' or ',$temp_a).')';
251 for ($kn = 1; $kn < count($key_a); $kn++) {
252 $binKey = preg_match('/[a-zA-Z]/', $key_a[$kn]) ? '' : 'BINARY';
253 if (substr($key_a[$kn], 0, 1) == ",") {
254 for($ith = 0; $ith < count($match_a); $ith++) {
255 // $temp_a[$ith] = " (i.$match_a[$ith] LIKE '%" . sql_real_escape_string(substr($key_a[$kn],1)) . "%') ";
256 $temp_a[$ith] = " (i.$match_a[$ith] LIKE " . $binKey . " '%" . sql_real_escape_string(substr($key_a[$kn], 1)) . "%') ";
258 $like .=' OR ('. implode(' or ', $temp_a).')';
259 }elseif(substr($key_a[$kn],0,1) != '-'){
260 for($ith=0;$ith<count($match_a);$ith++){
261 // $temp_a[$ith] = " (i.$match_a[$ith] LIKE '%" . sql_real_escape_string($key_a[$kn]) . "%') ";
262 $temp_a[$ith] = " (i.$match_a[$ith] LIKE " . $binKey . " '%" . sql_real_escape_string($key_a[$kn]) . "%') ";
264 $like .=' AND ('. implode(' or ', $temp_a).')';
266 for($ith=0;$ith<count($match_a);$ith++){
267 // $temp_a[$ith] = " NOT(i.$match_a[$ith] LIKE '%" . sql_real_escape_string(substr($key_a[$kn],1)) . "%') ";
268 $temp_a[$ith] = " NOT(i.$match_a[$ith] LIKE " . $binKey . " '%" . sql_real_escape_string(substr($key_a[$kn], 1)) . "%') ";
270 $like .=' AND ('. implode(' and ', $temp_a).')';
274 $like = '('.$like.')';