OSDN Git Service

Subversion由来のタグを削除
[nucleus-jp/nucleus-jp-ancient.git] / nucleus / libs / SEARCH.php
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  * SEARCH(querystring) offers different functionality to create an
14  * SQL query to find certain items. (and comments)
15  *
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
19  */
20
21 class SEARCH {
22
23         var $querystring;
24         var $marked;
25         var $inclusive;
26         var $blogs;
27
28         function SEARCH($text) {
29                 global $blogid;
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);
35                 }
36                 $text = str_replace ("\xE3\x80\x80",' ',$text);
37                 $text = preg_replace ("/[<>=?!#^()[\]:;\\%]/","",$text);
38
39                 $this->ascii       = '[\x00-\x7F]';
40                 $this->two               = '[\xC0-\xDF][\x80-\xBF]';
41                 $this->three       = '[\xE0-\xEF][\x80-\xBF][\x80-\xBF]';
42
43                 $this->jpmarked = $this->boolean_mark_atoms_jp($text);
44                 /* * * * * * * * * * * * * * * * */
45
46                 $this->querystring = $text;
47 //              $this->marked     = $this->boolean_mark_atoms($text);
48                 $this->inclusive   = $this->boolean_inclusive_atoms($text);
49                 $this->blogs       = array();
50
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);
55                 }
56         }
57
58         function  boolean_sql_select($match) {
59                 if (!isset($stringsum)) {
60                         $stringsum = '';
61                 }
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)) {
66                                 $stringsum_long = '';
67                         }
68                         for ($cth = 0; $cth < count($result); $cth++) {
69                                 if (strlen($result[$cth])>=4) {
70                                         $stringsum_long .=  " $result[$cth] ";
71                                 } else {
72                                         $stringsum_a[] = ' ' . $this->boolean_sql_select_short($result[$cth], $match) . ' ';
73                                 }
74                         }
75
76                         if (strlen($stringsum_long) > 0) {
77                                 $stringsum_long = sql_real_escape_string($stringsum_long);
78                                 $stringsum_a[]  = " match ($match) against ('$stringsum_long') ";
79                         }
80
81                         $stringsum .= implode("+", $stringsum_a);
82
83                         return $stringsum;
84                 }
85         }
86
87         
88
89         function boolean_inclusive_atoms($string) {
90                 $result = trim($string);
91                 $result = preg_replace("#([[:space:]]{2,})#", ' ', $result);
92                 
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
95                 
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);
100                 
101                 /* drop unnecessary spaces */
102                 $result = str_replace(' ,', ',', $result);
103                 $result = str_replace(', ', ',', $result);
104                 $result = str_replace('- ', '-', $result);
105                 $result = str_replace('+', '', $result);
106                 
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,}\)#",
110                         '',
111                         $result);
112                 
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");
118                 }
119                 return $result;
120         }
121
122         function boolean_sql_where($match) {
123 /*
124                 $result = $this->marked;
125                 $result = preg_replace(
126                         "/foo\[\(\'([^\)]{4,})\'\)\]bar/e",
127                         " 'match ('.\$match.') against (\''.\$this->copyvalue(\"$1\").'\') > 0 ' ",
128                         $result);
129                 $result = preg_replace(
130                         "/foo\[\(\'([^\)]{1,3})\'\)\]bar/e",
131                         " '('.\$this->boolean_sql_where_short(\"$1\",\"$match\").')' ",
132                         $result);
133 */
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");
138                 }
139                 return $result;
140         }
141
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) {
145                 return $foo;
146         }
147 /*
148         function boolean_mark_atoms($string){
149                 $result=trim($string);
150                 $result=preg_replace("/([[:space:]]{2,})/",' ',$result);
151
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
154
155                 $result = preg_replace('# not #i', ' -', $result);
156                 $result = preg_replace('# and #i', ' ', $result);
157                 $result = preg_replace('# or #i', ',', $result);
158
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);
166
167                 // remove double spaces (we might have introduced some new ones above)
168                 $result=trim($result);
169                 $result=preg_replace("#([[:space:]]{2,})#",' ',$result);
170
171                 // apply arbitrary function to all 'word' atoms
172
173                 $result_a = explode(' ',$result);
174                 for($word=0;$word<count($result_a);$word++)
175                 {
176                         $result_a[$word] = "foo[('".$result_a[$word]."')]bar";
177                 }
178                 $result = implode(" ",$result_a);
179
180                 // dispatch ' ' to ' AND '
181                 $result=str_replace(' ',' AND ',$result);
182
183                 // dispatch ',' to ' OR '
184                 $result=str_replace(',',' OR ',$result);
185
186                 // dispatch '-' to ' NOT '
187                 $result=str_replace(' -',' NOT ',$result);
188                 return $result;
189         }
190
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 %' ";
195                 }
196                 $like = implode(" OR ",$like_a);
197
198                 return $like;
199         }
200 */
201
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++){
206                         $score_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) . "') ";
211                 }
212                 $score = implode(" + ", $score_a);
213
214                 return $score;
215         }
216
217 /***********************************************
218         Make "WHERE" (jp)
219 ***********************************************/
220
221         function boolean_mark_atoms_jp($string) {
222                 $result = trim($string);
223                 $result = preg_replace("/([[:space:]]{2,})/", ' ', $result);
224                 
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);
229                 
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);
236
237                 return $result;
238         }
239
240         function boolean_sql_where_jp_short($string, $match) {
241                 $match_a = explode(',', $match);
242                 $key_a   = explode(' ', $string);
243
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]) . "%') ";
248                 }
249                 $like = '('.implode(' or ',$temp_a).')';
250
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)) . "%') ";
257                                 }
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]) . "%') ";
263                                 }
264                                 $like .=' AND ('. implode(' or ', $temp_a).')';
265                         }else{
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)) . "%') ";
269                                 }
270                                 $like .=' AND ('. implode(' and ', $temp_a).')';
271                         }
272                 }
273                 
274                 $like = '('.$like.')';
275                 return $like;
276         }
277 }
278 ?>