1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW\SQLStore\QueryEngine\Fulltext; |
4
|
|
|
|
5
|
|
|
use SMW\MediaWiki\Database; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @license GNU GPL v2+ |
9
|
|
|
* @since 2.5 |
10
|
|
|
* |
11
|
|
|
* @author mwjames |
12
|
|
|
*/ |
13
|
|
|
class SearchTableUpdater { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var Database |
17
|
|
|
*/ |
18
|
|
|
private $connection; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var SearchTable |
22
|
|
|
*/ |
23
|
|
|
private $searchTable; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var TextSanitizer |
27
|
|
|
*/ |
28
|
|
|
private $textSanitizer; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @since 2.5 |
32
|
|
|
* |
33
|
|
|
* @param Database $connection |
34
|
|
|
* @param SearchTable $searchTable |
35
|
|
|
* @param TextSanitizer $textSanitizer |
36
|
|
|
*/ |
37
|
211 |
|
public function __construct( Database $connection, SearchTable $searchTable, TextSanitizer $textSanitizer ) { |
38
|
211 |
|
$this->connection = $connection; |
39
|
211 |
|
$this->searchTable = $searchTable; |
40
|
211 |
|
$this->textSanitizer = $textSanitizer; |
41
|
211 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @since 2.5 |
45
|
|
|
* |
46
|
|
|
* @return SearchTable |
47
|
|
|
*/ |
48
|
4 |
|
public function getSearchTable() { |
49
|
4 |
|
return $this->searchTable; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @since 2.5 |
54
|
|
|
* |
55
|
|
|
* @return boolean |
56
|
|
|
*/ |
57
|
202 |
|
public function isEnabled() { |
58
|
202 |
|
return $this->searchTable->isEnabled(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @since 2.5 |
63
|
|
|
* |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
public function getPropertyTables() { |
67
|
|
|
return $this->searchTable->getPropertyTables(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @see http://dev.mysql.com/doc/refman/5.7/en/fulltext-fine-tuning.html |
72
|
|
|
* @see http://dev.mysql.com/doc/refman/5.7/en/optimize-table.html |
73
|
|
|
* |
74
|
|
|
* "Running OPTIMIZE TABLE on a table with a full-text index rebuilds the |
75
|
|
|
* full-text index, removing deleted Document IDs and consolidating multiple |
76
|
|
|
* entries for the same word, where possible." |
77
|
|
|
* |
78
|
|
|
* @since 2.5 |
79
|
|
|
* |
80
|
|
|
* @return boolean |
81
|
|
|
*/ |
82
|
2 |
|
public function optimize() { |
83
|
|
|
|
84
|
2 |
|
if ( !$this->connection->isType( 'mysql' ) ) { |
85
|
1 |
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
$this->connection->query( |
89
|
1 |
|
"OPTIMIZE TABLE " . $this->searchTable->getTableName(), |
90
|
1 |
|
__METHOD__ |
91
|
|
|
); |
92
|
|
|
|
93
|
1 |
|
return true; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @since 2.5 |
98
|
|
|
* |
99
|
|
|
* @param integer $sid |
100
|
|
|
* @param integer $pid |
101
|
|
|
* |
102
|
|
|
* @return boolean |
103
|
|
|
*/ |
104
|
4 |
|
public function exists( $sid, $pid ) { |
105
|
4 |
|
|
106
|
4 |
|
$row = $this->connection->selectRow( |
107
|
4 |
|
$this->searchTable->getTableName(), |
108
|
|
|
array( 's_id' ), |
109
|
4 |
|
array( |
110
|
4 |
|
's_id' => (int)$sid, |
111
|
|
|
'p_id' => (int)$pid |
112
|
4 |
|
), |
113
|
|
|
__METHOD__ |
114
|
|
|
); |
115
|
4 |
|
|
116
|
3 |
|
return $row !== false; |
117
|
|
|
} |
118
|
|
|
|
119
|
3 |
|
/** |
120
|
|
|
* @since 2.5 |
121
|
|
|
* |
122
|
|
|
* @param integer $sid |
123
|
|
|
* @param integer $pid |
124
|
|
|
* |
125
|
|
|
* @return false|string |
126
|
|
|
*/ |
127
|
|
|
public function read( $sid, $pid ) { |
128
|
|
|
$row = $this->connection->selectRow( |
129
|
5 |
|
$this->searchTable->getTableName(), |
130
|
|
|
array( 'o_text' ), |
131
|
5 |
|
array( |
132
|
3 |
|
's_id' => (int)$sid, |
133
|
|
|
'p_id' => (int)$pid |
134
|
|
|
), |
135
|
4 |
|
__METHOD__ |
136
|
4 |
|
); |
137
|
|
|
|
138
|
4 |
|
if ( $row === false ) { |
139
|
4 |
|
return false; |
140
|
|
|
} |
141
|
|
|
|
142
|
4 |
|
return $this->textSanitizer->sanitize( $row->o_text ); |
143
|
4 |
|
} |
144
|
|
|
|
145
|
4 |
|
/** |
146
|
|
|
* @since 2.5 |
147
|
4 |
|
* |
148
|
|
|
* @param integer $sid |
149
|
|
|
* @param integer $pid |
150
|
|
|
* @param string $text |
151
|
|
|
*/ |
152
|
|
|
public function update( $sid, $pid, $text ) { |
153
|
|
|
|
154
|
|
|
if ( trim( $text ) === '' || ( $indexableText = $this->textSanitizer->sanitize( $text ) ) === '' ) { |
155
|
4 |
|
return $this->delete( $sid, $pid ); |
156
|
4 |
|
} |
157
|
4 |
|
|
158
|
|
|
$this->connection->update( |
159
|
4 |
|
$this->searchTable->getTableName(), |
160
|
4 |
|
array( |
161
|
4 |
|
'o_text' => $indexableText, |
162
|
|
|
'o_sort' => mb_substr( $text, 0, 32 ) |
163
|
4 |
|
), |
164
|
|
|
array( |
165
|
4 |
|
's_id' => (int)$sid, |
166
|
|
|
'p_id' => (int)$pid |
167
|
|
|
), |
168
|
|
|
__METHOD__ |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
4 |
|
* @since 2.5 |
174
|
4 |
|
* |
175
|
4 |
|
* @param integer $sid |
176
|
|
|
* @param integer $pid |
177
|
4 |
|
*/ |
178
|
4 |
|
public function insert( $sid, $pid ) { |
179
|
|
|
$this->connection->insert( |
180
|
4 |
|
$this->searchTable->getTableName(), |
181
|
|
|
array( |
182
|
4 |
|
's_id' => (int)$sid, |
183
|
|
|
'p_id' => (int)$pid, |
184
|
|
|
'o_text' => '' |
185
|
|
|
), |
186
|
|
|
__METHOD__ |
187
|
1 |
|
); |
188
|
1 |
|
} |
189
|
1 |
|
|
190
|
1 |
|
/** |
191
|
1 |
|
* @since 2.5 |
192
|
|
|
* |
193
|
1 |
|
* @param integer $sid |
194
|
|
|
* @param integer $pid |
195
|
|
|
*/ |
196
|
|
|
public function delete( $sid, $pid ) { |
197
|
|
|
$this->connection->delete( |
198
|
|
|
$this->searchTable->getTableName(), |
199
|
|
|
array( |
200
|
|
|
's_id' => (int)$sid, |
201
|
|
|
'p_id' => (int)$pid |
202
|
|
|
), |
203
|
|
|
__METHOD__ |
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @since 2.5 |
209
|
|
|
*/ |
210
|
|
|
public function flushTable() { |
211
|
|
|
$this->connection->delete( |
212
|
|
|
$this->searchTable->getTableName(), |
213
|
|
|
'*', |
214
|
|
|
__METHOD__ |
215
|
|
|
); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
} |
219
|
|
|
|