Completed
Push — master ( 679959...72d37e )
by Maxence
02:10
created

FilesProvider::fillIndexDocument()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Files_FullTextSearch - Index the content of your files
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\Files_FullTextSearch\Provider;
28
29
use OCA\Files_FullTextSearch\AppInfo\Application;
30
use OCA\Files_FullTextSearch\Exceptions\FileIsNotIndexableException;
31
use OCA\Files_FullTextSearch\Model\FilesDocument;
32
use OCA\Files_FullTextSearch\Service\ConfigService;
33
use OCA\Files_FullTextSearch\Service\ElasticSearchService;
34
use OCA\Files_FullTextSearch\Service\FilesService;
35
use OCA\Files_FullTextSearch\Service\MiscService;
36
use OCA\Files_FullTextSearch\Service\SearchService;
37
use OCA\FullTextSearch\Exceptions\InterruptException;
38
use OCA\FullTextSearch\Exceptions\TickDoesNotExistException;
39
use OCA\FullTextSearch\IFullTextSearchPlatform;
40
use OCA\FullTextSearch\IFullTextSearchProvider;
41
use OCA\FullTextSearch\Model\Index;
42
use OCA\FullTextSearch\Model\IndexDocument;
43
use OCA\FullTextSearch\Model\IndexOptions;
44
use OCA\FullTextSearch\Model\Runner;
45
use OCA\FullTextSearch\Model\SearchRequest;
46
use OCA\FullTextSearch\Model\SearchResult;
47
use OCP\AppFramework\QueryException;
48
use OCP\Files\InvalidPathException;
49
use OCP\Files\NotFoundException;
50
use OCP\Files\NotPermittedException;
51
52
class FilesProvider implements IFullTextSearchProvider {
53
54
55
	const FILES_PROVIDER_ID = 'files';
56
57
	/** @var ConfigService */
58
	private $configService;
59
60
	/** @var FilesService */
61
	private $filesService;
62
63
	/** @var SearchService */
64
	private $searchService;
65
66
	/** @var ElasticSearchService */
67
	private $elasticSearchService;
68
69
	/** @var MiscService */
70
	private $miscService;
71
72
73
	/** @var Runner */
74
	private $runner;
75
76
	/** @var IndexOptions */
77
	private $indexOptions = [];
78
79
80
	/**
81
	 * return unique id of the provider
82
	 */
83
	public function getId() {
84
		return self::FILES_PROVIDER_ID;
85
	}
86
87
88
	/**
89
	 * return name of the provider
90
	 */
91
	public function getName() {
92
		return 'Files';
93
	}
94
95
96
	/**
97
	 * @return string
98
	 */
99
	public function getVersion() {
100
		return $this->configService->getAppValue('installed_version');
101
	}
102
103
104
	/**
105
	 * @return array
106
	 */
107
	public function getConfiguration() {
108
		return $this->configService->getConfig();
109
	}
110
111
112
	/**
113
	 * @return string
114
	 */
115
	public function getAppId() {
116
		return Application::APP_NAME;
117
	}
118
119
120
	public function setRunner(Runner $runner) {
121
		$this->runner = $runner;
122
	}
123
124
125
	/**
126
	 * @param IndexOptions $options
127
	 */
128
	public function setIndexOptions($options) {
129
		$this->indexOptions = $options;
130
	}
131
132
133
	/**
134
	 * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
135
	 */
136
	public function getOptionsTemplate() {
137
//				'template' => 'options.panel',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
138
		return [
139
			'panel'      => [
140
				'options' => [
141
					[
142
						'name'  => 'files_within_dir',
143
						'title' => 'Within current directory',
144
						'type'  => 'checkbox'
145
					],
146
					[
147
						'name'  => 'files_local',
148
						'title' => 'Within local files',
149
						'type'  => 'checkbox'
150
					],
151
					[
152
						'name'  => 'files_external',
153
						'title' => 'Within external files',
154
						'type'  => 'checkbox'
155
					],
156
					[
157
						'name'  => 'files_group_folders',
158
						'title' => 'Within group folders',
159
						'type'  => 'checkbox'
160
					],
161
					[
162
						'name'        => 'files_extension',
163
						'title'       => 'Filter by extension',
164
						'type'        => 'input',
165
						'size'        => 'small',
166
						'placeholder' => 'txt'
167
					]
168
				]
169
			],
170
			'navigation' => [
171
				'icon'    => 'icon-fts-files',
172
				'options' => [
173
					[
174
						'name'  => 'files_local',
175
						'title' => 'Local Files',
176
						'type'  => 'checkbox'
177
					],
178
					[
179
						'name'  => 'files_external',
180
						'title' => 'External Files',
181
						'type'  => 'checkbox'
182
					],
183
					[
184
						'name'  => 'files_group_folders',
185
						'title' => 'Group Folders',
186
						'type'  => 'checkbox'
187
					],
188
					[
189
						'name'        => 'files_extension',
190
						'title'       => 'Extension',
191
						'type'        => 'input',
192
						'size'        => 'small',
193
						'placeholder' => 'txt'
194
					]
195
				]
196
			]
197
		];
198
	}
199
200
201
	/**
202
	 * @deprecated
203
	 */
204
	public function getOptions() {
205
		return $this->getOptionsTemplate();
206
	}
207
208
209
	/**
210
	 * called when loading all providers.
211
	 *
212
	 * Loading some containers.
213
	 *
214
	 * @throws QueryException
215
	 */
216
	public function loadProvider() {
217
		$app = new Application();
218
219
		$container = $app->getContainer();
220
		$this->configService = $container->query(ConfigService::class);
221
		$this->filesService = $container->query(FilesService::class);
222
		$this->searchService = $container->query(SearchService::class);
223
		$this->elasticSearchService = $container->query(ElasticSearchService::class);
224
		$this->miscService = $container->query(MiscService::class);
225
	}
226
227
228
	/**
229
	 * returns all indexable document for a user.
230
	 * There is no need to fill the document with content at this point.
231
	 *
232
	 * $platform is provided if the mapping needs to be changed.
233
	 *
234
	 * @param string $userId
235
	 *
236
	 * @return IndexDocument[]
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use FilesDocument[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
237
	 * @throws InterruptException
238
	 * @throws TickDoesNotExistException
239
	 * @throws InvalidPathException
240
	 * @throws NotFoundException
241
	 */
242
	public function generateIndexableDocuments($userId) {
243
		$files = $this->filesService->getFilesFromUser($this->runner, $userId, $this->indexOptions);
244
245
		return $files;
246
	}
247
248
249
	/**
250
	 * generate documents prior to the indexing.
251
	 * throw NoResultException if no more result
252
	 *
253
	 * @param IndexDocument[] $chunk
254
	 *
255
	 * @return IndexDocument[]
256
	 * @deprecated
257
	 */
258
	public function fillIndexDocuments($chunk) {
259
		return [];
260
	}
261
262
263
	/**
264
	 * @param IndexDocument $document
265
	 */
266
	public function fillIndexDocument(IndexDocument $document) {
267
		/** @var FilesDocument $document */
268
		$this->filesService->generateDocument($document);
269
		$this->updateRunnerInfo('info', $document->getMimetype());
270
	}
271
272
273
	/**
274
	 * @param IndexDocument $document
275
	 *
276
	 * @return bool
277
	 */
278
	public function isDocumentUpToDate($document) {
279
		return $this->filesService->isDocumentUpToDate($document);
280
	}
281
282
283
	/**
284
	 * @param Index $index
285
	 *
286
	 * @return IndexDocument
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use FilesDocument.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
287
	 * @throws InvalidPathException
288
	 * @throws NotFoundException
289
	 * @throws NotPermittedException
290
	 * @throws FileIsNotIndexableException
291
	 */
292
	public function updateDocument(Index $index) {
293
		$document = $this->filesService->updateDocument($index);
294
		$this->updateRunnerInfo('info', $document->getMimetype());
295
296
		return $document;
297
	}
298
299
300
	/**
301
	 * @param IFullTextSearchPlatform $platform
302
	 */
303
	public function onInitializingIndex(IFullTextSearchPlatform $platform) {
304
		$this->elasticSearchService->onInitializingIndex($platform);
305
	}
306
307
308
	/**
309
	 * @param IFullTextSearchPlatform $platform
310
	 */
311
	public function onResettingIndex(IFullTextSearchPlatform $platform) {
312
		$this->elasticSearchService->onResettingIndex($platform);
313
	}
314
315
316
	/**
317
	 * not used yet
318
	 */
319
	public function unloadProvider() {
320
	}
321
322
323
	/**
324
	 * before a search, improve the request
325
	 *
326
	 * @param SearchRequest $request
327
	 */
328
	public function improveSearchRequest(SearchRequest $request) {
329
		$this->searchService->improveSearchRequest($request);
330
	}
331
332
333
	/**
334
	 * after a search, improve results
335
	 *
336
	 * @param SearchResult $searchResult
337
	 */
338
	public function improveSearchResult(SearchResult $searchResult) {
339
		$this->searchService->improveSearchResult($searchResult);
340
	}
341
342
343
	/**
344
	 * @param string $info
345
	 * @param string $value
346
	 */
347
	private function updateRunnerInfo($info, $value) {
348
		if ($this->runner === null) {
349
			return;
350
		}
351
352
		$this->runner->setInfo($info, $value);
353
	}
354
355
356
}