-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.php
345 lines (294 loc) · 9.1 KB
/
server.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>基于语义的搜索引擎</title>
<link rel="stylesheet" type="text/css" href="./css/server.css">
</head>
<body>
<script src="//cdn.bootcss.com/jquery/1.12.4/jquery.js"></script>
<script type="text/javascript">
function update_weight( url )
{
$.post("http://localhost/update_weight.php", { name : url } );
}
</script>
<?php
$search_text = "";
if( isset($_POST['search_text']) && $_POST['search_text'] != "" )
{
$search_text = $_POST['search_text'];
}
if( $_GET ) {
$page_num = $_GET['page_num']? $_GET['page_num']:1 ;
$search_text = $_GET['search_text'];
}
?>
<div id="header">
<form action="./server.php" method="post">
<a id="index" href="./index.php">精灵</a>
<input class="search_text" name="search_text" type="text" value="<?php echo $search_text;?>">
<input class="search_button" name="search_button" type="submit" value="搜 索" >
</form>
</div>
<div id="content">
<?php
$log = './log/search.log';
$error_log = './log/error.log';
$cache = './data/cache.txt';
$handle = fopen($log, 'a+');
$handle_error = fopen($error_log, 'a+');
$handle_cache = fopen($cache, 'w+');
if( ! $handle )
{
echo "不能打开文件$log\n";
exit;
}
if( ! $handle_error )
{
echo "不能打开文件$error_log\n";
exit;
}
/*
if( ! $handle_cache )
{
echo "不能打开文件$cache\n";
exit;
}
*/
$db = mysql_connect("139.129.129.74:3306", "disher", "disher");
if( !$db )
{
echo "mysql_connect error!\n";
exit();
}
mysql_select_db( "fairy", $db );
mysql_query("set names 'utf8'");
require_once("./lib/nlp/nlp.php");
$time_now = microtime();
/* 返回生成的SQL语句数组,以及对应的权重数组 */
$result = nlp_hander( $search_text );
$sql_array = $result[0];
$weight_array = $result[1];
$classify = $result[2];
$search_result = array();
$index = 0;
if( array_key_exists('title_and', $weight_array) )
{
$weight = $weight_array['title_and'];
$sql = $sql_array['title_and'];
/* 用and连起来的sql语句,是一个数组 */
$sql_num = count( $sql );
for( $i = 0; $i < $sql_num; $i++ )
{
$title_and_result = mysql_query( $sql[$i] );
while( $row = mysql_fetch_array($title_and_result) )
{
$row['weight'] = $row['weight'] * $weight;
$search_result[$index++] = $row;
}
}
}
if( array_key_exists('title_or', $weight_array) )
{
$weight = $weight_array['title_or'];
$sql = $sql_array['title_or']; // 用or连起来的sql语句,在数组中只占一个位置
$title_or_result = mysql_query( $sql );
while( $row = mysql_fetch_array($title_or_result) )
{
$row['weight'] = $row['weight'] * $weight;
$search_result[$index++] = $row;
}
}
if( array_key_exists('material_and', $weight_array) )
{
$weight = $weight_array['material_and'];
$sql = $sql_array['material_and'];
$sql_num = count( $sql );
for( $i = 0; $i < $sql_num; $i++ )
{
$material_and_result = mysql_query( $sql[$i] );
while( $row = mysql_fetch_array($material_and_result) )
{
$row['weight'] = $row['weight'] * $weight;
$search_result[$index++] = $row;
}
}
}
if( array_key_exists('material_or', $weight_array) )
{
$weight = $weight_array['material_or'];
$sql = $sql_array['material_or'];
$material_or_result = mysql_query( $sql );
while( $row = mysql_fetch_array($material_or_result) )
{
$row['weight'] = $row['weight'] * $weight;
$search_result[$index++] = $row;
}
}
if( array_key_exists('type_and', $weight_array) )
{
$weight = $weight_array['type_and'];
$sql = $sql_array['type_and'];
$sql_num = count( $sql );
for( $i = 0; $i < $sql_num; $i++ )
{
$type_and_result = mysql_query( $sql[$i] );
while( $row = mysql_fetch_array($type_and_result) )
{
$row['weight'] = $row['weight'] * $weight;
$search_result[$index++] = $row;
}
}
}
if( array_key_exists('type_or', $weight_array) )
{
$weight = $weight_array['type_or'];
$sql = $sql_array['type_or'];
$type_or_result = mysql_query( $sql );
while( $row = mysql_fetch_array($type_or_result) )
{
$row['weight'] = $row['weight'] * $weight;
$search_result[$index++] = $row;
}
}
$row_count = count( $search_result );
quickSort( $search_result, 0, $row_count - 1 );
$time_end = microtime();
$row_per_page = 8;
$page_total = ceil( $row_count / $row_per_page );
if( $page_num < 1 )
{
/* 如当前页小于1,则把当前页置1 */
$page_num = 1;
}
if( $page_num > $page_total )
{
/* 当前页大于总页数,则当前页为总页数 */
$page_num = $page_total;
}
?>
<p>共找到 <?php echo $row_count;?> 条记录,用时:<?php echo $time_end - $time_now; ?> 秒</p>
<?php
$start_now = ($page_num - 1) * $row_per_page;
?>
<table id="big_table" cellspacing="0" cellpadding="0">
<?php
for( $i = $start_now; $i < $start_now + 8; $i++ )
{
?>
<tr>
<td>
<table class="small_table" cellspacing="0" cellpadding="0">
<tr>
<td>
<a href="<?php echo $search_result[$i]['url'];?>" target="_blank" onclick=update_weight('<?php echo $search_result[$i]['url'];?>')><img class="pic" src="<?php echo $search_result[$i]['picture'];?>" height="100" width="100" alt="<?php echo $search_result[$i]['title'];?>"></a>
</td>
<td>
<table class="show_table">
<tr>
<td>
<a href="<?php echo $search_result[$i]['url'];?>" target="_blank" onclick=update_weight('<?php echo $search_result[$i]['url'];?>')><b><?php echo $search_result[$i]['title']?></b></a>
</td>
<tr>
<td>
<b>原料:</b><?php echo $search_result[$i]['material']; ?>
</td>
<tr>
<td>
<b>类型:<?php echo $search_result[$i]['type']; ?></b>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<?php
}
?>
</table>
<br/>
<br/>
<table>
<tr>
<?php
$start_page = $page_num - floor($row_per_page / 2);
$start_page = $start_page < 1 ? 1 : $start_page;
$end_page = $page_num + floor($row_per_page / 2);
$end_page = $end_page > $page_total ? $page_total : $end_page;
/* 当前的显示的页数不够最大页码时,进行左右调整 */
$current_page = $end_page - $start_page + 1;
/* 进行向左调整 */
if( $current_page < $row_per_page && $start_page > 1 )
{
$start_page = $start_page - ($row_per_page - $current_page);
$start_page = $start_page < 1 ? 1 : $start_page;
$current_page = $end_page - $start_page + 1;
}
/* 向右调整 */
if( $current_page < $row_per_page && $end_page < $page_total )
{
$end_page = $end_page + ($row_per_page - $current_page);
$end_page = $end_page > $page_total ? $page_total : $end_page;
}
/* 最多显示8页 */
for( $j = $start_page; $j <= $end_page; $j++ )
{
?>
<td>
<a href="./server.php?page_num=<?php echo $j;?>&search_text=<?php echo $search_text;?>"><p id="pagenum"><?php echo $j; ?></p></a>
</td>
<?php
}
?>
<td>
<p>共 <?php echo $page_total;?> 页</p>
</td>
</tr>
</table>
</div>
<!--
$write_log = $now.">>".$search_text.";\r\n";
if( ! mysql_error() )
{
fwrite($handle, $write_log);
}
else
{
fwrite( $handle_error, $write_log );
echo mysql_error()."\n";
}
-->
<div id="footer">
<!--
<script language=JavaScript>
today=new Date();
function initArray(){
this.length=initArray.arguments.length
for(var i=0;i<this.length;i++)
this[i+1]=initArray.arguments[i] }
var d=new initArray(
"星期日",
"星期一",
"星期二",
"星期三",
"星期四",
"星期五",
"星期六");
document.write(
"<font color=#0000FF style='font-size:9pt;font-family:fantasy'> ",
today.getFullYear(),"年 ",
today.getMonth()+1,"月 ",
today.getDate(),"日 ",
d[today.getDay()+1],
"</font>" );
</script>
-->
<font color=grey style='font-size:8pt;'>
Copyright by USTC 2015 - 2016
</font>
</div>
</body>
</html>