1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* FullTextSearch - Full text search framework for extcloud |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the COPYING file. |
7
|
|
|
* |
8
|
|
|
* @author Maxence Lange <[email protected]> |
9
|
|
|
* @copyright 2018 |
10
|
|
|
* @license GNU AGPL version 3 or any later version |
11
|
|
|
* |
12
|
|
|
* This program is free software: you can redistribute it and/or modify |
13
|
|
|
* it under the terms of the GNU Affero General Public License as |
14
|
|
|
* published by the Free Software Foundation, either version 3 of the |
15
|
|
|
* License, or (at your option) any later version. |
16
|
|
|
* |
17
|
|
|
* This program is distributed in the hope that it will be useful, |
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20
|
|
|
* GNU Affero General Public License for more details. |
21
|
|
|
* |
22
|
|
|
* You should have received a copy of the GNU Affero General Public License |
23
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace OCA\FullTextSearch\Model; |
28
|
|
|
|
29
|
|
|
class Index implements \JsonSerializable { |
30
|
|
|
|
31
|
|
|
const INDEX_OK = 1; |
32
|
|
|
const INDEX_IGNORE = 2; |
33
|
|
|
|
34
|
|
|
const INDEX_META = 4; |
35
|
|
|
const INDEX_CONTENT = 8; |
36
|
|
|
const INDEX_FULL = 12; |
37
|
|
|
const INDEX_REMOVE = 16; |
38
|
|
|
|
39
|
|
|
const INDEX_DONE = 32; |
40
|
|
|
const INDEX_FAILED = 64; |
41
|
|
|
|
42
|
|
|
const ERROR_FAILED = 1; |
43
|
|
|
const ERROR_FAILED2 = 2; |
44
|
|
|
const ERROR_FAILED3 = 4; |
45
|
|
|
|
46
|
|
|
const ERROR_SEV_1 = 1; |
47
|
|
|
const ERROR_SEV_2 = 2; |
48
|
|
|
const ERROR_SEV_3 = 3; |
49
|
|
|
const ERROR_SEV_4 = 4; |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** @var string */ |
53
|
|
|
private $providerId; |
54
|
|
|
|
55
|
|
|
/** @var string */ |
56
|
|
|
private $documentId; |
57
|
|
|
|
58
|
|
|
/** @var string */ |
59
|
|
|
private $source = ''; |
60
|
|
|
|
61
|
|
|
/** @var string */ |
62
|
|
|
private $ownerId = ''; |
63
|
|
|
|
64
|
|
|
/** @var int */ |
65
|
|
|
private $status = 0; |
66
|
|
|
|
67
|
|
|
/** @var array */ |
68
|
|
|
private $options = []; |
69
|
|
|
|
70
|
|
|
/** @var int */ |
71
|
|
|
private $err = 0; |
72
|
|
|
|
73
|
|
|
/** @var array */ |
74
|
|
|
private $errors = []; |
75
|
|
|
|
76
|
|
|
/** @var int */ |
77
|
|
|
private $lastIndex = 0; |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
public function __construct($providerId, $documentId) { |
81
|
|
|
$this->providerId = $providerId; |
82
|
|
|
$this->documentId = $documentId; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function getProviderId() { |
90
|
|
|
return $this->providerId; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public function getDocumentId() { |
97
|
|
|
return $this->documentId; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $source |
103
|
|
|
* |
104
|
|
|
* @return $this |
105
|
|
|
*/ |
106
|
|
|
public function setSource($source) { |
107
|
|
|
$this->source = $source; |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function getSource() { |
116
|
|
|
return $this->source; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $ownerId |
122
|
|
|
* |
123
|
|
|
* @return $this |
124
|
|
|
*/ |
125
|
|
|
public function setOwnerId($ownerId) { |
126
|
|
|
$this->ownerId = $ownerId; |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public function getOwnerId() { |
135
|
|
|
return $this->ownerId; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param int $status |
141
|
|
|
* @param bool $reset |
142
|
|
|
* |
143
|
|
|
* @return $this |
144
|
|
|
*/ |
145
|
|
|
public function setStatus($status, $reset = false) { |
146
|
|
|
if ($reset === true) { |
147
|
|
|
$this->status = $status; |
148
|
|
|
} else if (!$this->isStatus($status)) { |
149
|
|
|
$this->status += $status; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return int |
157
|
|
|
*/ |
158
|
|
|
public function getStatus() { |
159
|
|
|
return $this->status; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param int $status |
164
|
|
|
* |
165
|
|
|
* @return bool |
|
|
|
|
166
|
|
|
*/ |
167
|
|
|
public function isStatus($status) { |
168
|
|
|
return ((int)$status & $this->getStatus()); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param int $status |
173
|
|
|
*/ |
174
|
|
|
public function unsetStatus($status) { |
175
|
|
|
if (!$this->isStatus($status)) { |
176
|
|
|
return; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$this->status -= $status; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param string $option |
185
|
|
|
* @param string|int $value |
186
|
|
|
* |
187
|
|
|
* @return $this |
188
|
|
|
*/ |
189
|
|
|
public function addOption($option, $value) { |
190
|
|
|
$this->options[$option] = $value; |
191
|
|
|
|
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param array $options |
197
|
|
|
* |
198
|
|
|
* @return $this |
199
|
|
|
*/ |
200
|
|
|
public function setOptions($options) { |
201
|
|
|
$this->options = $options; |
202
|
|
|
|
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return array |
208
|
|
|
*/ |
209
|
|
|
public function getOptions() { |
210
|
|
|
return $this->options; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param string $option |
216
|
|
|
* @param string $default |
217
|
|
|
* |
218
|
|
|
* @return mixed|string |
219
|
|
|
*/ |
220
|
|
|
public function getOption($option, $default = '') { |
221
|
|
|
if (!array_key_exists($option, $this->options)) { |
222
|
|
|
return $default; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return $this->options[$option]; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param int $err |
231
|
|
|
* |
232
|
|
|
* @return $this |
233
|
|
|
*/ |
234
|
|
|
public function setErrorCount($err) { |
235
|
|
|
$this->err = $err; |
236
|
|
|
|
237
|
|
|
return $this; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @return int |
242
|
|
|
*/ |
243
|
|
|
public function getErrorCount() { |
244
|
|
|
return $this->err; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @return array |
249
|
|
|
*/ |
250
|
|
|
public function getLastError() { |
251
|
|
|
return array_values(array_slice($this->errors, -1))[0]; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* |
257
|
|
|
*/ |
258
|
|
|
public function resetErrors() { |
259
|
|
|
$this->setErrors([]); |
260
|
|
|
$this->setErrorCount(0); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @return array |
265
|
|
|
*/ |
266
|
|
|
public function getErrors() { |
267
|
|
|
return $this->errors; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @param array $messages |
272
|
|
|
* |
273
|
|
|
* @return Index |
274
|
|
|
*/ |
275
|
|
|
public function setErrors($messages) { |
276
|
|
|
$this->errors = $messages; |
277
|
|
|
|
278
|
|
|
return $this; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @param string $message |
284
|
|
|
* @param string $exception |
285
|
|
|
* @param int $sev |
286
|
|
|
*/ |
287
|
|
|
public function addError($message, $exception = '', $sev = self::ERROR_SEV_3) { |
288
|
|
|
$this->errors[] = [ |
289
|
|
|
'message' => substr($message, 0, 1800), |
290
|
|
|
'exception' => $exception, |
291
|
|
|
'severity' => $sev |
292
|
|
|
]; |
293
|
|
|
|
294
|
|
|
$this->err++; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @param int $lastIndex |
300
|
|
|
* |
301
|
|
|
* @return $this |
302
|
|
|
*/ |
303
|
|
|
public function setLastIndex($lastIndex = -1) { |
304
|
|
|
if ($lastIndex === -1) { |
305
|
|
|
$lastIndex = time(); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
$this->lastIndex = $lastIndex; |
309
|
|
|
|
310
|
|
|
return $this; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @return int |
315
|
|
|
*/ |
316
|
|
|
public function getLastIndex() { |
317
|
|
|
return $this->lastIndex; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* @return array<string,string|integer> |
|
|
|
|
323
|
|
|
*/ |
324
|
|
|
public function jsonSerialize() { |
325
|
|
|
return [ |
326
|
|
|
'ownerId' => $this->getOwnerId(), |
327
|
|
|
'providerId' => $this->getProviderId(), |
328
|
|
|
'source' => $this->getSource(), |
329
|
|
|
'documentId' => $this->getDocumentId(), |
330
|
|
|
'lastIndex' => $this->getLastIndex(), |
331
|
|
|
'errors' => $this->getErrors(), |
332
|
|
|
'errorCount' => $this->getErrorCount(), |
333
|
|
|
'status' => (int)$this->getStatus(), |
334
|
|
|
'options' => $this->getOptions() |
335
|
|
|
]; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
|
339
|
|
|
public function __destruct() { |
340
|
|
|
unset($this->providerId); |
341
|
|
|
unset($this->documentId); |
342
|
|
|
unset($this->ownerId); |
343
|
|
|
unset($this->status); |
344
|
|
|
unset($this->options); |
345
|
|
|
unset($this->err); |
346
|
|
|
unset($this->message); |
347
|
|
|
unset($this->lastIndex); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
} |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.