Completed
Push — master ( 8de354...4e0f9f )
by Maxence
01:38
created

FilesProvider::generateIndexableDocuments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
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\Files\NotPermittedException;
43
use OCP\FullTextSearch\IFullTextSearchPlatform;
44
use OCP\FullTextSearch\IFullTextSearchProvider;
45
use OCP\FullTextSearch\Model\IIndex;
46
use OCP\FullTextSearch\Model\IIndexOptions;
47
use OCP\FullTextSearch\Model\IndexDocument;
48
use OCP\FullTextSearch\Model\IRunner;
49
use OCP\FullTextSearch\Model\ISearchRequest;
50
use OCP\FullTextSearch\Model\ISearchResult;
51
use OCP\FullTextSearch\Model\SearchOption;
52
use OCP\FullTextSearch\Model\SearchTemplate;
53
use OCP\IL10N;
54
55
56
/**
57
 * Class FilesProvider
58
 *
59
 * @package OCA\Files_FullTextSearch\Provider
60
 */
61
class FilesProvider implements IFullTextSearchProvider {
62
63
64
	const FILES_PROVIDER_ID = 'files';
65
66
67
	/** @var IL10N */
68
	private $l10n;
69
70
	/** @var ConfigService */
71
	private $configService;
72
73
	/** @var FilesService */
74
	private $filesService;
75
76
	/** @var SearchService */
77
	private $searchService;
78
79
	/** @var MiscService */
80
	private $miscService;
81
82
	/** @var IRunner */
83
	private $runner;
84
85
	/** @var IIndexOptions */
86
	private $indexOptions = [];
87
88
89 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...
90
		IL10N $l10n, ConfigService $configService, FilesService $filesService,
91
		SearchService $searchService, MiscService $miscService
92
	) {
93
		$this->l10n = $l10n;
94
		$this->configService = $configService;
95
		$this->filesService = $filesService;
96
		$this->searchService = $searchService;
97
		$this->miscService = $miscService;
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
	}
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 IndexDocument[]
223
	 * @throws InvalidPathException
224
	 * @throws NotFoundException
225
	 */
226
	public function generateIndexableDocuments(string $userId): array {
227
		$this->filesService->setRunner($this->runner);
228
		$files = $this->filesService->getFilesFromUser($userId, $this->indexOptions);
229
230
		return $files;
231
	}
232
233
234
	/**
235
	 * @param IndexDocument $document
236
	 */
237
	public function fillIndexDocument(IndexDocument $document) {
238
		/** @var FilesDocument $document */
239
		$this->filesService->generateDocument($document);
240
		$this->updateRunnerInfo('info', $document->getMimetype());
241
	}
242
243
244
	/**
245
	 * @param IndexDocument $document
246
	 *
247
	 * @return bool
248
	 */
249
	public function isDocumentUpToDate(IndexDocument $document): bool {
250
		return $this->filesService->isDocumentUpToDate($document);
251
	}
252
253
254
	/**
255
	 * @param IIndex $index
256
	 *
257
	 * @return IndexDocument
258
	 * @throws InvalidPathException
259
	 * @throws NotFoundException
260
	 * @throws NotPermittedException
261
	 * @throws FileIsNotIndexableException
262
	 */
263
	public function updateDocument(IIndex $index): IndexDocument {
264
		$document = $this->filesService->updateDocument($index);
265
		$this->updateRunnerInfo('info', $document->getMimetype());
266
267
		return $document;
268
	}
269
270
271
	/**
272
	 * @param IFullTextSearchPlatform $platform
273
	 */
274
	public function onInitializingIndex(IFullTextSearchPlatform $platform) {
275
	}
276
277
278
	/**
279
	 * @param IFullTextSearchPlatform $platform
280
	 */
281
	public function onResettingIndex(IFullTextSearchPlatform $platform) {
282
	}
283
284
285
	/**
286
	 * not used yet
287
	 */
288
	public function unloadProvider() {
289
	}
290
291
292
	/**
293
	 * before a search, improve the request
294
	 *
295
	 * @param ISearchRequest $request
296
	 */
297
	public function improveSearchRequest(ISearchRequest $request) {
298
		$this->searchService->improveSearchRequest($request);
299
	}
300
301
302
	/**
303
	 * after a search, improve results
304
	 *
305
	 * @param ISearchResult $searchResult
306
	 */
307
	public function improveSearchResult(ISearchResult $searchResult) {
308
		$this->searchService->improveSearchResult($searchResult);
309
	}
310
311
312
	/**
313
	 * @param string $info
314
	 * @param string $value
315
	 */
316
	private function updateRunnerInfo(string $info, string $value) {
317
		if ($this->runner === null) {
318
			return;
319
		}
320
321
		$this->runner->setInfo($info, $value);
322
	}
323
324
325
}
326