|
1
|
|
|
<?php |
|
2
|
|
|
/*************************************************************************** |
|
3
|
|
|
* functions_search.php |
|
4
|
|
|
* ------------------- |
|
5
|
|
|
* begin : Wed Sep 05 2001 |
|
6
|
|
|
* copyright : (C) 2002 The phpBB Group |
|
7
|
|
|
* email : [email protected] |
|
8
|
|
|
* |
|
9
|
|
|
* $Id: functions_search.php,v 1.8.2.16 2003/06/30 17:18:37 acydburn Exp $ |
|
10
|
|
|
* |
|
11
|
|
|
****************************************************************************/ |
|
12
|
|
|
|
|
13
|
|
|
/*************************************************************************** |
|
14
|
|
|
* |
|
15
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
16
|
|
|
* it under the terms of the GNU General Public License as published by |
|
17
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
|
18
|
|
|
* (at your option) any later version. |
|
19
|
|
|
* |
|
20
|
|
|
***************************************************************************/ |
|
21
|
|
|
|
|
22
|
|
|
function clean_words($mode, &$entry) { |
|
23
|
|
|
static $drop_char_match = array('^', '$', '&', '(', ')', '<', '>', '`', '\'', '"', '|', ',', '@', '_', '?', |
|
24
|
|
|
'%', '-', '~', '+', '.', '[', ']', '{', '}', ':', '\\', '/', '=', '#', '\'', ';', '!'); |
|
25
|
|
|
static $drop_char_replace = array(' ', ' ', ' ', ' ', ' ', ' ', ' ', '', '', ' ', ' ', ' ', ' ', '', ' ', |
|
26
|
|
|
' ', '', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' , ' ', ' ', ' ', ' ', ' ', ' '); |
|
27
|
|
|
|
|
28
|
|
|
$entry = ' ' . strip_tags(strtolower($entry)) . ' '; |
|
29
|
|
|
|
|
30
|
|
|
if ($mode == 'post') { |
|
31
|
|
|
// Replace line endings by a space |
|
32
|
|
|
$entry = preg_replace('/[\n\r]/is', ' ', $entry); |
|
33
|
|
|
// HTML entities like |
|
34
|
|
|
$entry = preg_replace('/\b&[a-z]+;\b/', ' ', $entry); |
|
35
|
|
|
// Remove URL's |
|
36
|
|
|
$entry = preg_replace('/\b[a-z0-9]+:\/\/[a-z0-9\.\-]+(\/[a-z0-9\?\.%_\-\+=&\/]+)?/', ' ', $entry); |
|
37
|
|
|
// Quickly remove BBcode. |
|
38
|
|
|
$entry = preg_replace('/\[img:[a-z0-9]{10,}\].*?\[\/img:[a-z0-9]{10,}\]/', ' ', $entry); |
|
39
|
|
|
$entry = preg_replace('/\[\/?url(=.*?)?\]/', ' ', $entry); |
|
40
|
|
|
$entry = preg_replace('/\[\/?[a-z\*=\+\-]+(\:?[0-9a-z]+)?:[a-z0-9]{10,}(\:[a-z0-9]+)?=?.*?\]/', ' ', $entry); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// |
|
44
|
|
|
// Filter out strange characters like ^, $, &, change "it's" to "its" |
|
45
|
|
|
// |
|
46
|
|
|
for ($i = 0; $i < count($drop_char_match); $i++) { |
|
|
|
|
|
|
47
|
|
|
$entry = str_replace($drop_char_match[$i], $drop_char_replace[$i], $entry); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if ($mode == 'post') { |
|
51
|
|
|
$entry = str_replace('*', ' ', $entry); |
|
52
|
|
|
|
|
53
|
|
|
// 'words' that consist of <3 or >20 characters are removed. |
|
54
|
|
|
$entry = preg_replace('/[ ]([\S]{1,2}|[\S]{21,})[ ]/', ' ', $entry); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $entry; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
function split_words(&$entry, $mode = 'post') { |
|
|
|
|
|
|
61
|
|
|
return explode(' ', trim(preg_replace('#\s+#', ' ', $entry))); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
function add_search_words($mode, $post_id, $post_text, $post_title = '') { |
|
65
|
|
|
include("../../config/connect.php"); |
|
66
|
|
|
|
|
67
|
|
|
$search_raw_words = array(); |
|
68
|
|
|
$post_text = clean_words('post', $post_text); |
|
69
|
|
|
$post_title = clean_words('post', $post_title); |
|
70
|
|
|
$search_raw_words['text'] = split_words($post_text); |
|
71
|
|
|
$search_raw_words['title'] = split_words($post_title); |
|
72
|
|
|
|
|
73
|
|
|
@set_time_limit(0); |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
$word = array(); |
|
76
|
|
|
$word_insert_sql = array(); |
|
77
|
|
|
while (list($word_in, $search_matches) = @each($search_raw_words)) { |
|
78
|
|
|
$word_insert_sql[$word_in] = ''; |
|
79
|
|
|
if (!empty($search_matches)) { |
|
80
|
|
|
for ($i = 0; $i < count($search_matches); $i++) { |
|
|
|
|
|
|
81
|
|
|
$search_matches[$i] = trim($search_matches[$i]); |
|
82
|
|
|
|
|
83
|
|
|
if ($search_matches[$i] != '') { |
|
84
|
|
|
$word[] = $search_matches[$i]; |
|
85
|
|
|
if (!strstr($word_insert_sql[$word_in], "'" . $search_matches[$i] . "'")) { |
|
86
|
|
|
$word_insert_sql[$word_in] .= ($word_insert_sql[$word_in] != "") ? ", '" . |
|
87
|
|
|
$search_matches[$i] . "'" : "'" . $search_matches[$i] . "'"; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if (count($word)) { |
|
95
|
|
|
sort($word); |
|
96
|
|
|
|
|
97
|
|
|
$prev_word = ''; |
|
98
|
|
|
$word_text_sql = ''; |
|
99
|
|
|
$temp_word = array(); |
|
100
|
|
|
for ($i = 0; $i < count($word); $i++) { |
|
|
|
|
|
|
101
|
|
|
if ($word[$i] != $prev_word) { |
|
102
|
|
|
$temp_word[] = $word[$i]; |
|
103
|
|
|
$word_text_sql .= (($word_text_sql != '') ? ', ' : '') . "'" . $word[$i] . "'"; |
|
104
|
|
|
} |
|
105
|
|
|
$prev_word = $word[$i]; |
|
106
|
|
|
} |
|
107
|
|
|
$word = $temp_word; |
|
108
|
|
|
|
|
109
|
|
|
$check_words = array(); |
|
110
|
|
|
|
|
111
|
|
|
$value_sql = ''; |
|
112
|
|
|
$match_word = array(); |
|
|
|
|
|
|
113
|
|
|
for ($i = 0; $i < count($word); $i++) { |
|
|
|
|
|
|
114
|
|
|
$new_match = true; |
|
115
|
|
|
if (isset($check_words[$word[$i]])) { |
|
116
|
|
|
$new_match = false; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
if ($new_match) { |
|
120
|
|
|
$value_sql .= (($value_sql != '') ? ', ' : '') . '(\'' . $word[$i] . '\')'; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if ($value_sql != '') { |
|
125
|
|
|
$sql = "INSERT IGNORE INTO news_search_wordlist (news_word_text) |
|
126
|
|
|
VALUES $value_sql"; |
|
127
|
|
|
|
|
128
|
|
|
$query = $mysqli->query($sql) or die("Inserting in news_search_wordlist failed"); |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
while (list($word_in, $match_sql) = @each($word_insert_sql)) { |
|
133
|
|
|
$title_match = ($word_in == 'title') ? 1 : 0; |
|
134
|
|
|
|
|
135
|
|
|
if ($match_sql != '') { |
|
136
|
|
|
$sql = "INSERT IGNORE INTO news_search_wordmatch (news_id, news_word_id, news_title_match) |
|
137
|
|
|
SELECT $post_id, news_word_id, $title_match |
|
138
|
|
|
FROM news_search_wordlist |
|
139
|
|
|
WHERE news_word_text IN ($match_sql)"; |
|
140
|
|
|
|
|
141
|
|
|
$query = $mysqli->query($sql) or die("Inserting in news_search_wordmatch failed"); |
|
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
return; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
function remove_search_post($post_id_sql) { |
|
148
|
|
|
$words_removed = false; |
|
149
|
|
|
|
|
150
|
|
|
$sql = "SELECT news_word_id |
|
151
|
|
|
FROM news_search_wordmatch |
|
152
|
|
|
WHERE news_id IN ($post_id_sql) |
|
153
|
|
|
GROUP BY news_word_id"; |
|
154
|
|
|
if ($result = mysql_query($sql)) { |
|
155
|
|
|
$word_id_sql = ''; |
|
156
|
|
|
while ($row = mysql_fetch_array($result)) { |
|
|
|
|
|
|
157
|
|
|
$word_id_sql .= ($word_id_sql != '') ? ', ' . $row['news_word_id'] : $row['news_word_id']; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
$sql = "SELECT news_word_id |
|
161
|
|
|
FROM news_search_wordmatch |
|
162
|
|
|
WHERE news_word_id IN ($word_id_sql) |
|
163
|
|
|
GROUP BY news_word_id |
|
164
|
|
|
HAVING COUNT(news_word_id) = 1"; |
|
165
|
|
|
if ($result = mysql_query($sql)) { |
|
166
|
|
|
$word_id_sql = ''; |
|
167
|
|
|
while ($row = mysql_fetch_array($result)) { |
|
168
|
|
|
$word_id_sql .= ($word_id_sql != '') ? ', ' . $row['news_word_id'] : $row['news_word_id']; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
if ($word_id_sql != '') { |
|
172
|
|
|
$sql = "DELETE FROM news_search_wordlist |
|
173
|
|
|
WHERE news_word_id IN ($word_id_sql)"; |
|
174
|
|
|
if (!mysql_query($sql)) { |
|
175
|
|
|
die("Could not delete word list entry"); |
|
|
|
|
|
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
$words_removed = mysql_affected_rows(); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
$sql = "DELETE FROM news_search_wordmatch |
|
184
|
|
|
WHERE news_id IN ($post_id_sql)"; |
|
185
|
|
|
if (!mysql_query($sql)) { |
|
186
|
|
|
die("Error in deleting post"); |
|
|
|
|
|
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
return $words_removed; |
|
190
|
|
|
} |
|
191
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: