-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.php
167 lines (146 loc) · 4.14 KB
/
search.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
session_start();
$maxSearchCacheCount = 10;
$compareweight = Array("equal"=>1000, "locate"=>10);
$colshash = Array("name"=>0,"description"=>5, "tag" =>6 , "species"=> 4);
function cleanSearchCache(){
global $maxSearchCacheCount;
$fi = new FilesystemIterator("./temp", FilesystemIterator::SKIP_DOTS);
if($fi > $maxSearchCacheCount){
//delete the oldest one
$files = glob( './temp/*' );
// Sort files by modified time, latest to earliest
// Use SORT_ASC in place of SORT_DESC for earliest to latest
array_multisort(
array_map( 'filemtime', $files ),
SORT_NUMERIC,
SORT_ASC,
$files
);
unlink($files[0]);
}
}
if(!is_dir("temp")){
mkdir("temp", 0777, true);
}
$count = 0;
//if searchmd5 setted and equal to the existed value
if(isset($_SESSION['keywords']) && $_SESSION['keywords'] == $keywords){
//read
$ret = readresult($_SESSION['tmpsearchfile']);
if($ret != NULL) {
buildret($ret,$page);
return;
}
}
if(isset($_SESSION['tmpsearchfile'])){
file_exists($_SESSION['tmpsearchfile']) && unlink($_SESSION['tmpsearchfile']);
unset($_SESSION['tmpsearchfile']);
}
//if the md5 has not been setted or not equal to the existed
$_SESSION['keywords']= $keywords;
function buildret($ret, $page){
global $pagesize,$count;
$offset = $page*$pagesize;
$ret = array_slice($ret, $offset, $pagesize);
$retjson = array('msg' => "Success", 'count'=>$count,'children' => $ret, 'query'=>"COMPLEX:NOT READABLE");
echo json_encode($retjson);
}
function writeresult($filename,$arr){
cleanSearchCache();
$handle = fopen($filename, "w");
fwrite($handle,serialize($arr));
fclose($handle);
}
function readresult($filename){
if(!file_exists($filename))
return NULL;
global $count;
$arr = unserialize(file_get_contents($filename));
$count = count($arr);
return $arr;
}
function selectequal($col,$val,$skey){
$connector = " ";
global $count, $compareweight;
$sql = "SELECT * FROM pets_table WHERE LOWER($col)=LOWER('$skey')";
$result = mysql_query($sql);
$ret = Array();
while($item = mysql_fetch_array($result, MYSQL_ASSOC)){
$count ++;
$item["weight"] = $compareweight["equal"]*$val; $ret[] = $item;
# echo "!!!!!!!!!".$item["weight"];
}
return $ret;
}
function selectlocate($col,$val,$skey){
echo "!!! $col,$val, $skey <br>";
global $count, $compareweight;
$sql = "SELECT *,(LENGTH(description) - LENGTH(REPLACE(description, '$skey', '')))/LENGTH('$skey')*$val*".$compareweight["locate"]." as weight FROM pets_table WHERE LOCATE(LOWER('$skey'),LOWER($col))>0 AND LOWER($col)!=LOWER('$skey')";
$result = mysql_query($sql);
$ret = Array();
while($item = mysql_fetch_array($result, MYSQL_ASSOC)){
$count ++;
$ret[] = $item;
echo "!!!!!!!!!".$item["weight"];
}
return $ret;
}
function cleanarr($arr){
global $count;
$tmp = Array();
$tmpall = Array();
foreach($arr as $item){
if(array_key_exists($item['uid'], $tmp)){
$tmp[$item['uid']]+=$item["weight"];
}
else{
$tmp[$item['uid']] = $item["weight"];
$tmpall[$item['uid']] = $item;
}
}
//sort by weight in descending order
arsort($tmp);
$ret = Array();
//rebuild the return arary
$count = 0;
foreach($tmp as $k=>$v){
$count++;
$tmpall[$k]['weight']=$v;
$ret[]=$tmpall[$k];
}
return $ret;
}
require_once("consvr.php");
$sql = "";
$ret = Array();
if($keywords == "猫" || $keywords == "狗"){
$ret = array_merge($ret,selectequal("species",$colshash["species"], $keywords));
}
else{
$keywords=explode(" ", $keywords);
$cols = array_keys($colshash);
foreach($keywords as $keyword){
//special locgic for species
if($keyword == "猫" || $keyword == "狗"){
//reduce the equal weight when query spieces
$compareweight['equal'] = 5;
$ret = array_merge($ret,selectequal("species",$colshash["species"], $keyword));
$compareweight['equal'] = 1000;
continue;
}
foreach($colshash as $col=>$val){
if($col == "species")
continue;
$ret = array_merge($ret,selectequal($col,$val, $keyword));
$ret = array_merge($ret,selectlocate($col,$val, $keyword));
}
}
$ret = cleanarr($ret);
}
$count = count($ret);
$_SESSION['tmpsearchfile'] = tempnam("./temp",date("YmdHis")) ;
writeresult($_SESSION['tmpsearchfile'], $ret);
buildret($ret,$page);
mysql_close();
?>