Completed
Pull Request — master (#50)
by Maxence
01:37
created

FilesProvider::updateRunnerInfoArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
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
	public function __construct(
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
	/**
101
	 * return unique id of the provider
102
	 */
103
	public function getId(): string {
104
		return self::FILES_PROVIDER_ID;
105
	}
106
107
108
	/**
109
	 * return name of the provider
110
	 */
111
	public function getName(): string {
112
		return 'Files';
113
	}
114
115
116
	/**
117
	 * @return array
118
	 */
119
	public function getConfiguration(): array {
120
		return $this->configService->getConfig();
121
	}
122
123
124
	/**
125
	 * @param IRunner $runner
126
	 */
127
	public function setRunner(IRunner $runner) {
128
		$this->runner = $runner;
129
		$this->filesService->setRunner($runner);
130
	}
131
132
133
	/**
134
	 * @param IIndexOptions $options
135
	 */
136
	public function setIndexOptions(IIndexOptions $options) {
137
		$this->indexOptions = $options;
138
	}
139
140
141
	/**
142
	 * @return SearchTemplate
143
	 */
144
	public function getSearchTemplate(): SearchTemplate {
145
		$template = new SearchTemplate('icon-fts-files', 'fulltextsearch');
146
147
		$template->addPanelOption(
148
			new SearchOption(
149
				'files_within_dir', $this->l10n->t('Within current directory'),
150
				SearchOption::CHECKBOX
151
			)
152
		);
153
154
		$template->addPanelOption(
155
			new SearchOption(
156
				'files_local', $this->l10n->t('Within local files'),
157
				SearchOption::CHECKBOX
158
			)
159
		);
160
		$template->addNavigationOption(
161
			new SearchOption(
162
				'files_local', $this->l10n->t('Local files'),
163
				SearchOption::CHECKBOX
164
			)
165
		);
166
167 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...
168
			$template->addPanelOption(
169
				new SearchOption(
170
					'files_external', $this->l10n->t('Within external files'),
171
					SearchOption::CHECKBOX
172
				)
173
			);
174
			$template->addNavigationOption(
175
				new SearchOption(
176
					'files_external', $this->l10n->t('External files'), SearchOption::CHECKBOX
177
				)
178
			);
179
		}
180
181 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...
182
			$template->addPanelOption(
183
				new SearchOption(
184
					'files_group_folders', $this->l10n->t('Within group folders'),
185
					SearchOption::CHECKBOX
186
				)
187
			);
188
			$template->addNavigationOption(
189
				new SearchOption(
190
					'files_group_folders', $this->l10n->t('Group folders'),
191
					SearchOption::CHECKBOX
192
				)
193
			);
194
		}
195
196
		$template->addPanelOption(
197
			new SearchOption(
198
				'files_extension', $this->l10n->t('Filter by extension'), SearchOption::INPUT,
199
				SearchOption::INPUT_SMALL, 'txt'
200
			)
201
		);
202
		$template->addNavigationOption(
203
			new SearchOption(
204
				'files_extension', $this->l10n->t('Extension'), SearchOption::INPUT,
205
				SearchOption::INPUT_SMALL, 'txt'
206
			)
207
		);
208
209
		return $template;
210
	}
211
212
213
	/**
214
	 *
215
	 */
216
	public function loadProvider() {
217
	}
218
219
220
	/**
221
	 * @param string $userId
222
	 *
223
	 * @return array
224
	 * @throws InvalidPathException
225
	 * @throws NotFoundException
226
	 */
227
	public function generateChunks(string $userId): array {
228
		$chunks = $this->filesService->getChunksFromUser($userId, $this->indexOptions);
229
		return $chunks;
230
	}
231
232
233
	/**
234
	 * @param string $userId
235
	 *
236
	 * @param string $chunk
237
	 *
238
	 * @return IndexDocument[]
239
	 * @throws InvalidPathException
240
	 * @throws NotFoundException
241
	 */
242
	public function generateIndexableDocuments(string $userId, string $chunk): array {
243
		$files = $this->filesService->getFilesFromUser($userId, $chunk);
244
245
		return $files;
246
	}
247
248
249
	/**
250
	 * @param IndexDocument $document
251
	 */
252
	public function fillIndexDocument(IndexDocument $document) {
253
		/** @var FilesDocument $document */
254
		$this->updateRunnerInfoArray(
255
			[
256
				'info'  => $document->getMimetype(),
257
				'title' => $document->getPath()
258
			]
259
		);
260
261
		$this->filesService->generateDocument($document);
262
	}
263
264
265
	/**
266
	 * @param IndexDocument $document
267
	 *
268
	 * @return bool
269
	 */
270
	public function isDocumentUpToDate(IndexDocument $document): bool {
271
		return $this->filesService->isDocumentUpToDate($document);
272
	}
273
274
275
	/**
276
	 * @param IIndex $index
277
	 *
278
	 * @return IndexDocument
279
	 * @throws InvalidPathException
280
	 * @throws NotFoundException
281
	 * @throws FileIsNotIndexableException
282
	 */
283
	public function updateDocument(IIndex $index): IndexDocument {
284
		$document = $this->filesService->updateDocument($index);
285
		$this->updateRunnerInfo('info', $document->getMimetype());
286
287
		return $document;
288
	}
289
290
291
	/**
292
	 * @param IFullTextSearchPlatform $platform
293
	 */
294
	public function onInitializingIndex(IFullTextSearchPlatform $platform) {
295
	}
296
297
298
	/**
299
	 * @param IFullTextSearchPlatform $platform
300
	 */
301
	public function onResettingIndex(IFullTextSearchPlatform $platform) {
302
	}
303
304
305
	/**
306
	 * not used yet
307
	 */
308
	public function unloadProvider() {
309
	}
310
311
312
	/**
313
	 * before a search, improve the request
314
	 *
315
	 * @param ISearchRequest $request
316
	 */
317
	public function improveSearchRequest(ISearchRequest $request) {
318
		$this->searchService->improveSearchRequest($request);
319
	}
320
321
322
	/**
323
	 * after a search, improve results
324
	 *
325
	 * @param ISearchResult $searchResult
326
	 */
327
	public function improveSearchResult(ISearchResult $searchResult) {
328
		$this->searchService->improveSearchResult($searchResult);
329
	}
330
331
332
	/**
333
	 * @param string $info
334
	 * @param string $value
335
	 */
336
	private function updateRunnerInfo(string $info, string $value) {
337
		if ($this->runner === null) {
338
			return;
339
		}
340
341
		$this->runner->setInfo($info, $value);
342
	}
343
344
	/**
345
	 * @param array $info
346
	 */
347
	private function updateRunnerInfoArray(array $info) {
348
		if ($this->runner === null) {
349
			return;
350
		}
351
352
		$this->runner->setInfoArray($info);
353
	}
354
355
}
356
357