1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Files_FullTextSearch - Index the content of your files |
7
|
|
|
* |
8
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
9
|
|
|
* later. See the COPYING file. |
10
|
|
|
* |
11
|
|
|
* @author Maxence Lange <[email protected]> |
12
|
|
|
* @copyright 2018 |
13
|
|
|
* @license GNU AGPL version 3 or any later version |
14
|
|
|
* |
15
|
|
|
* This program is free software: you can redistribute it and/or modify |
16
|
|
|
* it under the terms of the GNU Affero General Public License as |
17
|
|
|
* published by the Free Software Foundation, either version 3 of the |
18
|
|
|
* License, or (at your option) any later version. |
19
|
|
|
* |
20
|
|
|
* This program is distributed in the hope that it will be useful, |
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23
|
|
|
* GNU Affero General Public License for more details. |
24
|
|
|
* |
25
|
|
|
* You should have received a copy of the GNU Affero General Public License |
26
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
27
|
|
|
* |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
namespace OCA\Files_FullTextSearch\Events; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
use daita\MySmallPhpTools\Traits\TArrayTools; |
35
|
|
|
use OCA\Files_FullTextSearch\Service\ConfigService; |
36
|
|
|
use OCA\Files_FullTextSearch\Service\FilesService; |
37
|
|
|
use OCA\Files_FullTextSearch\Service\MiscService; |
38
|
|
|
use OCP\App\IAppManager; |
39
|
|
|
use OCP\AppFramework\QueryException; |
40
|
|
|
use OCP\Comments\CommentsEvent; |
41
|
|
|
use OCP\Files\InvalidPathException; |
42
|
|
|
use OCP\Files\NotFoundException; |
43
|
|
|
use OCP\FullTextSearch\IFullTextSearchManager; |
44
|
|
|
use OCP\FullTextSearch\Model\IIndex; |
45
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent; |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Class FilesEvents |
50
|
|
|
* |
51
|
|
|
* @package OCA\Files_FullTextSearch\Events |
52
|
|
|
*/ |
53
|
|
|
class FilesEvents { |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
use TArrayTools; |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** @var string */ |
60
|
|
|
private $userId; |
61
|
|
|
|
62
|
|
|
/** @var IAppManager */ |
63
|
|
|
private $appManager; |
64
|
|
|
|
65
|
|
|
/** @var IFullTextSearchManager */ |
66
|
|
|
private $fullTextSearchManager; |
67
|
|
|
|
68
|
|
|
/** @var FilesService */ |
69
|
|
|
private $filesService; |
70
|
|
|
|
71
|
|
|
/** @var ConfigService */ |
72
|
|
|
private $configService; |
73
|
|
|
|
74
|
|
|
/** @var MiscService */ |
75
|
|
|
private $miscService; |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* FilesEvents constructor. |
80
|
|
|
* |
81
|
|
|
* @param string $userId |
82
|
|
|
* @param IAppManager $appManager |
83
|
|
|
* @param IFullTextSearchManager $fullTextSearchManager |
84
|
|
|
* @param FilesService $filesService |
85
|
|
|
* @param ConfigService $configService |
86
|
|
|
* @param MiscService $miscService |
87
|
|
|
*/ |
88
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
89
|
|
|
$userId, IAppManager $appManager, IFullTextSearchManager $fullTextSearchManager, |
90
|
|
|
FilesService $filesService, ConfigService $configService, MiscService $miscService |
91
|
|
|
) { |
92
|
|
|
$this->userId = $userId; |
93
|
|
|
$this->appManager = $appManager; |
94
|
|
|
$this->fullTextSearchManager = $fullTextSearchManager; |
95
|
|
|
$this->filesService = $filesService; |
96
|
|
|
$this->configService = $configService; |
97
|
|
|
$this->miscService = $miscService; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @throws QueryException |
103
|
|
|
*/ |
104
|
|
|
private function registerFullTextSearchServices() { |
105
|
|
|
|
106
|
|
|
if (!$this->appManager->isInstalled('fulltextsearch') |
107
|
|
|
|| !class_exists('\OCA\FullTextSearch\AppInfo\Application')) { |
108
|
|
|
$this->miscService->log('fulltextsearch not installed', 1); |
109
|
|
|
|
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$fulltextsearch = new \OCA\FullTextSearch\AppInfo\Application(); |
114
|
|
|
$fulltextsearch->registerServices(); |
115
|
|
|
|
116
|
|
|
return true; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param array $params |
122
|
|
|
* |
123
|
|
|
* @throws InvalidPathException |
124
|
|
|
* @throws NotFoundException |
125
|
|
|
* @throws QueryException |
126
|
|
|
*/ |
127
|
|
|
public function onNewFile(array $params) { |
128
|
|
|
if (!$this->registerFullTextSearchServices()) { |
129
|
|
|
return; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$path = $this->get('path', $params, ''); |
133
|
|
|
if ($path === '') { |
134
|
|
|
return; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$file = $this->filesService->getFileFromPath($this->userId, $path); |
138
|
|
|
if ($this->configService->isCloudVersionAtLeast(15, 0, 1)) { |
139
|
|
|
$this->fullTextSearchManager->createIndex( |
140
|
|
|
'files', (string)$file->getId(), $this->userId, IIndex::INDEX_FULL |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param array $params |
148
|
|
|
* |
149
|
|
|
* @throws InvalidPathException |
150
|
|
|
* @throws NotFoundException |
151
|
|
|
* @throws QueryException |
152
|
|
|
*/ |
153
|
|
View Code Duplication |
public function onFileUpdate(array $params) { |
|
|
|
|
154
|
|
|
if (!$this->registerFullTextSearchServices()) { |
155
|
|
|
return; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$path = $this->get('path', $params, ''); |
159
|
|
|
if ($path === '') { |
160
|
|
|
return; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$file = $this->filesService->getFileFromPath($this->userId, $path); |
164
|
|
|
$this->fullTextSearchManager->updateIndexStatus( |
165
|
|
|
'files', (string)$file->getId(), IIndex::INDEX_CONTENT |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param array $params |
172
|
|
|
* |
173
|
|
|
* @throws NotFoundException |
174
|
|
|
* @throws InvalidPathException |
175
|
|
|
* @throws QueryException |
176
|
|
|
*/ |
177
|
|
View Code Duplication |
public function onFileRename(array $params) { |
|
|
|
|
178
|
|
|
if (!$this->registerFullTextSearchServices()) { |
179
|
|
|
return; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$target = $this->get('newpath', $params, ''); |
183
|
|
|
if ($target === '') { |
184
|
|
|
return; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$file = $this->filesService->getFileFromPath($this->userId, $target); |
188
|
|
|
$this->fullTextSearchManager->updateIndexStatus( |
189
|
|
|
'files', (string)$file->getId(), IIndex::INDEX_META |
190
|
|
|
); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param array $params |
196
|
|
|
* |
197
|
|
|
* @throws InvalidPathException |
198
|
|
|
* @throws NotFoundException |
199
|
|
|
* @throws QueryException |
200
|
|
|
*/ |
201
|
|
View Code Duplication |
public function onFileTrash(array $params) { |
|
|
|
|
202
|
|
|
if (!$this->registerFullTextSearchServices()) { |
203
|
|
|
return; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
// check if trashbin does not exist. -> onFileDelete |
207
|
|
|
// we do not index trashbin |
208
|
|
|
$path = $this->get('path', $params, ''); |
209
|
|
|
if ($path === '') { |
210
|
|
|
return; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
$file = $this->filesService->getFileFromPath($this->userId, $path); |
214
|
|
|
$this->fullTextSearchManager->updateIndexStatus( |
215
|
|
|
'files', (string)$file->getId(), IIndex::INDEX_REMOVE, true |
216
|
|
|
); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param array $params |
222
|
|
|
* |
223
|
|
|
* @throws InvalidPathException |
224
|
|
|
* @throws NotFoundException |
225
|
|
|
* @throws QueryException |
226
|
|
|
*/ |
227
|
|
View Code Duplication |
public function onFileRestore(array $params) { |
|
|
|
|
228
|
|
|
if (!$this->registerFullTextSearchServices()) { |
229
|
|
|
return; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
$path = $this->get('filePath', $params, ''); |
233
|
|
|
if ($path === '') { |
234
|
|
|
return; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
$file = $this->filesService->getFileFromPath($this->userId, $path); |
238
|
|
|
$this->fullTextSearchManager->updateIndexStatus( |
239
|
|
|
'files', (string)$file->getId(), IIndex::INDEX_FULL |
240
|
|
|
); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @param array $params |
246
|
|
|
*/ |
247
|
|
|
public function onFileDelete(array $params) { |
|
|
|
|
248
|
|
|
// if (!$this->registerFullTextSearchServices()) { |
249
|
|
|
// return; |
250
|
|
|
// } |
251
|
|
|
|
252
|
|
|
// $path = $this->get('path', $params, ''); |
253
|
|
|
// if ($path === '') { |
254
|
|
|
// return; |
255
|
|
|
// } |
256
|
|
|
|
257
|
|
|
// $file = $this->filesService->getFileFromPath($this->userId, $path); |
258
|
|
|
// $this->fullTextSearchManager->updateIndexStatus('files', (string) $file->getId(), Index::INDEX_REMOVE); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @param array $params |
264
|
|
|
* |
265
|
|
|
* @throws QueryException |
266
|
|
|
*/ |
267
|
|
View Code Duplication |
public function onFileShare(array $params) { |
|
|
|
|
268
|
|
|
if (!$this->registerFullTextSearchServices()) { |
269
|
|
|
return; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
$fileId = $this->get('itemSource', $params, ''); |
273
|
|
|
if ($fileId === '') { |
274
|
|
|
return; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
$this->fullTextSearchManager->updateIndexStatus('files', $fileId, IIndex::INDEX_META); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @param array $params |
283
|
|
|
* |
284
|
|
|
* @throws QueryException |
285
|
|
|
*/ |
286
|
|
View Code Duplication |
public function onFileUnshare(array $params) { |
|
|
|
|
287
|
|
|
if (!$this->registerFullTextSearchServices()) { |
288
|
|
|
return; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
$fileId = $this->get('itemSource', $params, ''); |
292
|
|
|
if ($fileId === '') { |
293
|
|
|
return; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
$this->fullTextSearchManager->updateIndexStatus('files', $fileId, IIndex::INDEX_META); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @param CommentsEvent $event |
302
|
|
|
* |
303
|
|
|
* @throws QueryException |
304
|
|
|
*/ |
305
|
|
|
public function onCommentNew(CommentsEvent $event) { |
306
|
|
|
if (!$this->registerFullTextSearchServices()) { |
307
|
|
|
return; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
$comment = $event->getComment(); |
311
|
|
|
$fileId = $comment->getObjectId(); |
312
|
|
|
|
313
|
|
|
$this->fullTextSearchManager->updateIndexStatus('files', $fileId, IIndex::INDEX_CONTENT); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
|
317
|
|
|
public function onNewScannedFile2(array $params) { |
|
|
|
|
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
|
321
|
|
|
public function onNewScannedFile(array $params) { |
|
|
|
|
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.