Completed
Pull Request — master (#35)
by Maxence
01:38
created

FilesProvider::isDocumentUpToDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
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\Provider;
32
33
34
use OCA\Files_FullTextSearch\Exceptions\FileIsNotIndexableException;
35
use OCA\Files_FullTextSearch\Model\FilesDocument;
36
use OCA\Files_FullTextSearch\Service\ConfigService;
37
use OCA\Files_FullTextSearch\Service\FilesService;
38
use OCA\Files_FullTextSearch\Service\MiscService;
39
use OCA\Files_FullTextSearch\Service\SearchService;
40
use OCP\Files\InvalidPathException;
41
use OCP\Files\NotFoundException;
42
use OCP\FullTextSearch\IFullTextSearchPlatform;
43
use OCP\FullTextSearch\IFullTextSearchProvider;
44
use OCP\FullTextSearch\Model\IIndex;
45
use OCP\FullTextSearch\Model\IIndexOptions;
46
use OCP\FullTextSearch\Model\IndexDocument;
47
use OCP\FullTextSearch\Model\IRunner;
48
use OCP\FullTextSearch\Model\ISearchRequest;
49
use OCP\FullTextSearch\Model\ISearchResult;
50
use OCP\FullTextSearch\Model\SearchOption;
51
use OCP\FullTextSearch\Model\SearchTemplate;
52
use OCP\IL10N;
53
54
55
/**
56
 * Class FilesProvider
57
 *
58
 * @package OCA\Files_FullTextSearch\Provider
59
 */
60
class FilesProvider implements IFullTextSearchProvider {
61
62
63
	const FILES_PROVIDER_ID = 'files';
64
65
66
	/** @var IL10N */
67
	private $l10n;
68
69
	/** @var ConfigService */
70
	private $configService;
71
72
	/** @var FilesService */
73
	private $filesService;
74
75
	/** @var SearchService */
76
	private $searchService;
77
78
	/** @var MiscService */
79
	private $miscService;
80
81
	/** @var IRunner */
82
	private $runner;
83
84
	/** @var IIndexOptions */
85
	private $indexOptions = [];
86
87
88 View Code Duplication
	public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
89
		IL10N $l10n, ConfigService $configService, FilesService $filesService,
90
		SearchService $searchService, MiscService $miscService
91
	) {
92
		$this->l10n = $l10n;
93
		$this->configService = $configService;
94
		$this->filesService = $filesService;
95
		$this->searchService = $searchService;
96
		$this->miscService = $miscService;
97
	}
98
99
	/**
100
	 * return unique id of the provider
101
	 */
102
	public function getId(): string {
103
		return self::FILES_PROVIDER_ID;
104
	}
105
106
107
	/**
108
	 * return name of the provider
109
	 */
110
	public function getName(): string {
111
		return 'Files';
112
	}
113
114
115
	/**
116
	 * @return array
117
	 */
118
	public function getConfiguration(): array {
119
		return $this->configService->getConfig();
120
	}
121
122
123
	/**
124
	 * @param IRunner $runner
125
	 */
126
	public function setRunner(IRunner $runner) {
127
		$this->runner = $runner;
128
		$this->filesService->setRunner($runner);
129
	}
130
131
132
	/**
133
	 * @param IIndexOptions $options
134
	 */
135
	public function setIndexOptions(IIndexOptions $options) {
136
		$this->indexOptions = $options;
137
	}
138
139
140
	/**
141
	 * @return SearchTemplate
142
	 */
143
	public function getSearchTemplate(): SearchTemplate {
144
		$template = new SearchTemplate('icon-fts-files', 'fulltextsearch');
145
146
		$template->addPanelOption(
147
			new SearchOption(
148
				'files_within_dir', $this->l10n->t('Within current directory'),
149
				SearchOption::CHECKBOX
150
			)
151
		);
152
153
		$template->addPanelOption(
154
			new SearchOption(
155
				'files_local', $this->l10n->t('Within local files'),
156
				SearchOption::CHECKBOX
157
			)
158
		);
159
		$template->addNavigationOption(
160
			new SearchOption(
161
				'files_local', $this->l10n->t('Local files'),
162
				SearchOption::CHECKBOX
163
			)
164
		);
165
166 View Code Duplication
		if ($this->configService->getAppValue(ConfigService::FILES_EXTERNAL) === '1') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
167
			$template->addPanelOption(
168
				new SearchOption(
169
					'files_external', $this->l10n->t('Within external files'),
170
					SearchOption::CHECKBOX
171
				)
172
			);
173
			$template->addNavigationOption(
174
				new SearchOption(
175
					'files_external', $this->l10n->t('External files'), SearchOption::CHECKBOX
176
				)
177
			);
178
		}
179
180 View Code Duplication
		if ($this->configService->getAppValue(ConfigService::FILES_GROUP_FOLDERS) === '1') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
181
			$template->addPanelOption(
182
				new SearchOption(
183
					'files_group_folders', $this->l10n->t('Within group folders'),
184
					SearchOption::CHECKBOX
185
				)
186
			);
187
			$template->addNavigationOption(
188
				new SearchOption(
189
					'files_group_folders', $this->l10n->t('Group folders'),
190
					SearchOption::CHECKBOX
191
				)
192
			);
193
		}
194
195
		$template->addPanelOption(
196
			new SearchOption(
197
				'files_extension', $this->l10n->t('Filter by extension'), SearchOption::INPUT,
198
				SearchOption::INPUT_SMALL, 'txt'
199
			)
200
		);
201
		$template->addNavigationOption(
202
			new SearchOption(
203
				'files_extension', $this->l10n->t('Extension'), SearchOption::INPUT,
204
				SearchOption::INPUT_SMALL, 'txt'
205
			)
206
		);
207
208
		return $template;
209
	}
210
211
212
	/**
213
	 *
214
	 */
215
	public function loadProvider() {
216
	}
217
218
219
	/**
220
	 * @param string $userId
221
	 *
222
	 * @return array
223
	 * @throws InvalidPathException
224
	 * @throws NotFoundException
225
	 */
226
	public function generateChunks(string $userId): array {
227
		$chunks = $this->filesService->getChunksFromUser($userId, $this->indexOptions);
228
		return $chunks;
229
	}
230
231
232
	/**
233
	 * @param string $userId
234
	 *
235
	 * @param string $chunk
236
	 *
237
	 * @return IndexDocument[]
238
	 * @throws InvalidPathException
239
	 * @throws NotFoundException
240
	 */
241
	public function generateIndexableDocuments(string $userId, string $chunk): array {
242
		$files = $this->filesService->getFilesFromUser($userId, $chunk);
243
244
		return $files;
245
	}
246
247
248
	/**
249
	 * @param IndexDocument $document
250
	 */
251
	public function fillIndexDocument(IndexDocument $document) {
252
		/** @var FilesDocument $document */
253
		$this->filesService->generateDocument($document);
254
		$this->updateRunnerInfo('info', $document->getMimetype());
255
	}
256
257
258
	/**
259
	 * @param IndexDocument $document
260
	 *
261
	 * @return bool
262
	 */
263
	public function isDocumentUpToDate(IndexDocument $document): bool {
264
		return $this->filesService->isDocumentUpToDate($document);
265
	}
266
267
268
	/**
269
	 * @param IIndex $index
270
	 *
271
	 * @return IndexDocument
272
	 * @throws InvalidPathException
273
	 * @throws NotFoundException
274
	 * @throws FileIsNotIndexableException
275
	 */
276
	public function updateDocument(IIndex $index): IndexDocument {
277
		$document = $this->filesService->updateDocument($index);
278
		$this->updateRunnerInfo('info', $document->getMimetype());
279
280
		return $document;
281
	}
282
283
284
	/**
285
	 * @param IFullTextSearchPlatform $platform
286
	 */
287
	public function onInitializingIndex(IFullTextSearchPlatform $platform) {
288
	}
289
290
291
	/**
292
	 * @param IFullTextSearchPlatform $platform
293
	 */
294
	public function onResettingIndex(IFullTextSearchPlatform $platform) {
295
	}
296
297
298
	/**
299
	 * not used yet
300
	 */
301
	public function unloadProvider() {
302
	}
303
304
305
	/**
306
	 * before a search, improve the request
307
	 *
308
	 * @param ISearchRequest $request
309
	 */
310
	public function improveSearchRequest(ISearchRequest $request) {
311
		$this->searchService->improveSearchRequest($request);
312
	}
313
314
315
	/**
316
	 * after a search, improve results
317
	 *
318
	 * @param ISearchResult $searchResult
319
	 */
320
	public function improveSearchResult(ISearchResult $searchResult) {
321
		$this->searchService->improveSearchResult($searchResult);
322
	}
323
324
325
	/**
326
	 * @param string $info
327
	 * @param string $value
328
	 */
329
	private function updateRunnerInfo(string $info, string $value) {
330
		if ($this->runner === null) {
331
			return;
332
		}
333
334
		$this->runner->setInfo($info, $value);
335
	}
336
337
}
338
339