|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This class handles database search. (PostgreSQL) |
|
5
|
|
|
* |
|
6
|
|
|
* @name ElkArte Forum |
|
7
|
|
|
* @copyright ElkArte Forum contributors |
|
8
|
|
|
* @license BSD http://opensource.org/licenses/BSD-3-Clause |
|
9
|
|
|
* |
|
10
|
|
|
* @version 2.0 dev |
|
11
|
|
|
* |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* PostgreSQL implementation of DbSearch |
|
16
|
|
|
*/ |
|
17
|
|
|
class DbSearch_PostgreSQL implements DbSearch |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* This instance of the search |
|
21
|
|
|
* @var DbSearch_PostgreSQL |
|
22
|
|
|
*/ |
|
23
|
|
|
private static $_search = null; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The way to skip a database error |
|
27
|
|
|
* @var boolean |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $_skip_error = false; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* This function will tell you whether this database type supports this search type. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $search_type |
|
35
|
|
|
*/ |
|
36
|
|
|
public function search_support($search_type) |
|
37
|
|
|
{ |
|
38
|
|
|
$supported_types = array('custom'); |
|
39
|
|
|
|
|
40
|
|
|
return in_array($search_type, $supported_types); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Compute and execute the correct query for search. |
|
45
|
|
|
* Returns the result of executing the query. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $identifier |
|
48
|
|
|
* @param string $db_string |
|
49
|
|
|
* @param mixed[] $db_values default array() |
|
50
|
|
|
* @param resource|null $connection |
|
51
|
|
|
*/ |
|
52
|
|
|
public function search_query($identifier, $db_string, $db_values = array(), $connection = null) |
|
53
|
|
|
{ |
|
54
|
|
|
$db = database(); |
|
55
|
|
|
|
|
56
|
|
|
$replacements = array( |
|
57
|
|
|
'create_tmp_log_search_topics' => array( |
|
58
|
|
|
'~mediumint\(\d\)~i' => 'int', |
|
59
|
|
|
'~unsigned~i' => '', |
|
60
|
|
|
'~ENGINE=MEMORY~i' => '', |
|
61
|
|
|
), |
|
62
|
|
|
'create_tmp_log_search_messages' => array( |
|
63
|
|
|
'~mediumint\(\d\)~i' => 'int', |
|
64
|
|
|
'~unsigned~i' => '', |
|
65
|
|
|
'~ENGINE=MEMORY~i' => '', |
|
66
|
|
|
), |
|
67
|
|
|
'drop_tmp_log_search_topics' => array( |
|
68
|
|
|
'~IF\sEXISTS~i' => '', |
|
69
|
|
|
), |
|
70
|
|
|
'drop_tmp_log_search_messages' => array( |
|
71
|
|
|
'~IF\sEXISTS~i' => '', |
|
72
|
|
|
), |
|
73
|
|
|
'insert_into_log_messages_fulltext' => array( |
|
74
|
|
|
'~LIKE~i' => 'iLIKE', |
|
75
|
|
|
'~NOT\sLIKE~i' => '~NOT iLIKE', |
|
76
|
|
|
'~NOT\sRLIKE~i' => '!~*', |
|
77
|
|
|
'~RLIKE~i' => '~*', |
|
78
|
|
|
), |
|
79
|
|
|
'insert_log_search_results_subject' => array( |
|
80
|
|
|
'~LIKE~i' => 'iLIKE', |
|
81
|
|
|
'~NOT\sLIKE~i' => 'NOT iLIKE', |
|
82
|
|
|
'~NOT\sRLIKE~i' => '!~*', |
|
83
|
|
|
'~RLIKE~i' => '~*', |
|
84
|
|
|
), |
|
85
|
|
|
); |
|
86
|
|
|
|
|
87
|
|
|
if (isset($replacements[$identifier])) |
|
88
|
|
|
$db_string = preg_replace(array_keys($replacements[$identifier]), array_values($replacements[$identifier]), $db_string); |
|
89
|
|
|
elseif (preg_match('~^\s*INSERT\sIGNORE~i', $db_string) != 0) |
|
90
|
|
|
{ |
|
91
|
|
|
$db_string = preg_replace('~^\s*INSERT\sIGNORE~i', 'INSERT', $db_string); |
|
92
|
|
|
// Don't error on multi-insert. |
|
93
|
|
|
$this->_skip_error = true; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if ($this->_skip_error === true) |
|
97
|
|
|
{ |
|
98
|
|
|
$db->skip_next_error(); |
|
99
|
|
|
$this->_skip_error = false; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$return = $db->query('', $db_string, |
|
103
|
|
|
$db_values, $connection |
|
104
|
|
|
); |
|
105
|
|
|
|
|
106
|
|
|
return $return; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* {@inheritDoc} |
|
111
|
|
|
*/ |
|
112
|
|
|
public function skip_next_error() |
|
113
|
|
|
{ |
|
114
|
|
|
$this->_skip_error = true; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Returns some basic info about the {db_prefix}messages table |
|
119
|
|
|
* Used in ManageSearch.controller.php in the page to select the index method |
|
120
|
|
|
*/ |
|
121
|
|
|
public function membersTableInfo() |
|
122
|
|
|
{ |
|
123
|
|
|
global $db_prefix, $txt; |
|
124
|
|
|
|
|
125
|
|
|
$db = database(); |
|
126
|
|
|
$db_table = db_table(); |
|
127
|
|
|
|
|
128
|
|
|
$table_info = array(); |
|
129
|
|
|
|
|
130
|
|
|
// In order to report the sizes correctly we need to perform vacuum (optimize) on the tables we will be using. |
|
131
|
|
|
$db_table->optimize('{db_prefix}messages'); |
|
132
|
|
|
if ($db_table->table_exists('{db_prefix}log_search_words')) |
|
133
|
|
|
$db_table->optimize('{db_prefix}log_search_words'); |
|
134
|
|
|
|
|
135
|
|
|
// PostGreSql has some hidden sizes. |
|
136
|
|
|
$request = $db->query('', ' |
|
137
|
|
|
SELECT relname, relpages * 8 *1024 AS "KB" FROM pg_class |
|
138
|
|
|
WHERE relname = {string:messages} OR relname = {string:log_search_words} |
|
139
|
|
|
ORDER BY relpages DESC', |
|
140
|
|
|
array( |
|
141
|
|
|
'messages' => $db_prefix . 'messages', |
|
142
|
|
|
'log_search_words' => $db_prefix . 'log_search_words', |
|
143
|
|
|
) |
|
144
|
|
|
); |
|
145
|
|
|
|
|
146
|
|
|
if ($request !== false && $db->num_rows($request) > 0) |
|
147
|
|
|
{ |
|
148
|
|
|
while ($row = $db->fetch_assoc($request)) |
|
149
|
|
|
{ |
|
150
|
|
|
if ($row['relname'] == $db_prefix . 'messages') |
|
151
|
|
|
{ |
|
152
|
|
|
$table_info['data_length'] = (int) $row['KB']; |
|
153
|
|
|
$table_info['index_length'] = (int) $row['KB']; |
|
154
|
|
|
|
|
155
|
|
|
// Doesn't support fulltext |
|
156
|
|
|
$table_info['fulltext_length'] = $txt['not_applicable']; |
|
157
|
|
|
} |
|
158
|
|
|
elseif ($row['relname'] == $db_prefix . 'log_search_words') |
|
159
|
|
|
{ |
|
160
|
|
|
$table_info['index_length'] = (int) $row['KB']; |
|
161
|
|
|
$table_info['custom_index_length'] = (int) $row['KB']; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
$db->free_result($request); |
|
165
|
|
|
} |
|
166
|
|
View Code Duplication |
else |
|
167
|
|
|
// Didn't work for some reason... |
|
168
|
|
|
$table_info = array( |
|
169
|
|
|
'data_length' => $txt['not_applicable'], |
|
170
|
|
|
'index_length' => $txt['not_applicable'], |
|
171
|
|
|
'fulltext_length' => $txt['not_applicable'], |
|
172
|
|
|
'custom_index_length' => $txt['not_applicable'], |
|
173
|
|
|
); |
|
174
|
|
|
|
|
175
|
|
|
return $table_info; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Make a custom word index. |
|
180
|
|
|
* |
|
181
|
|
|
* @param string $size |
|
182
|
|
|
*/ |
|
183
|
|
|
public function create_word_search($size) |
|
184
|
|
|
{ |
|
185
|
|
|
$db = database(); |
|
186
|
|
|
|
|
187
|
|
|
$size = 'int'; |
|
188
|
|
|
|
|
189
|
|
|
$db->query('', ' |
|
190
|
|
|
CREATE TABLE {db_prefix}log_search_words ( |
|
191
|
|
|
id_word {raw:size} NOT NULL default {string:string_zero}, |
|
192
|
|
|
id_msg int NOT NULL default {string:string_zero}, |
|
193
|
|
|
PRIMARY KEY (id_word, id_msg) |
|
194
|
|
|
)', |
|
195
|
|
|
array( |
|
196
|
|
|
'size' => $size, |
|
197
|
|
|
'string_zero' => '0', |
|
198
|
|
|
) |
|
199
|
|
|
); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Static method that allows to retrieve or create an instance of this class. |
|
204
|
|
|
*/ |
|
205
|
|
|
public static function db_search() |
|
206
|
|
|
{ |
|
207
|
|
|
if (is_null(self::$_search)) |
|
208
|
|
|
{ |
|
209
|
|
|
self::$_search = new self(); |
|
210
|
|
|
} |
|
211
|
|
|
return self::$_search; |
|
212
|
|
|
} |
|
213
|
|
|
} |