Completed
Push — master ( 324e78...e457da )
by Maxence
01:39
created

FilesProvider::fillIndexDocuments()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 9.392
c 0
b 0
f 0
cc 3
nc 3
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\Model\FilesDocument;
31
use OCA\Files_FullTextSearch\Service\ConfigService;
32
use OCA\Files_FullTextSearch\Service\ElasticSearchService;
33
use OCA\Files_FullTextSearch\Service\FilesService;
34
use OCA\Files_FullTextSearch\Service\MiscService;
35
use OCA\Files_FullTextSearch\Service\SearchService;
36
use OCA\FullTextSearch\Exceptions\InterruptException;
37
use OCA\FullTextSearch\Exceptions\TickDoesNotExistException;
38
use OCA\FullTextSearch\IFullTextSearchPlatform;
39
use OCA\FullTextSearch\IFullTextSearchProvider;
40
use OCA\FullTextSearch\Model\Index;
41
use OCA\FullTextSearch\Model\IndexDocument;
42
use OCA\FullTextSearch\Model\IndexOptions;
43
use OCA\FullTextSearch\Model\Runner;
44
use OCA\FullTextSearch\Model\SearchRequest;
45
use OCA\FullTextSearch\Model\SearchResult;
46
use OCP\AppFramework\QueryException;
47
use OCP\Files\InvalidPathException;
48
use OCP\Files\NotFoundException;
49
use OCP\Files\NotPermittedException;
50
51
class FilesProvider implements IFullTextSearchProvider {
52
53
54
	const FILES_PROVIDER_ID = 'files';
55
56
	/** @var ConfigService */
57
	private $configService;
58
59
	/** @var FilesService */
60
	private $filesService;
61
62
	/** @var SearchService */
63
	private $searchService;
64
65
	/** @var ElasticSearchService */
66
	private $elasticSearchService;
67
68
	/** @var MiscService */
69
	private $miscService;
70
71
72
	/** @var Runner */
73
	private $runner;
74
75
	/** @var IndexOptions */
76
	private $indexOptions = [];
77
78
79
	/**
80
	 * return unique id of the provider
81
	 */
82
	public function getId() {
83
		return self::FILES_PROVIDER_ID;
84
	}
85
86
87
	/**
88
	 * return name of the provider
89
	 */
90
	public function getName() {
91
		return 'Files';
92
	}
93
94
95
	/**
96
	 * @return string
97
	 */
98
	public function getVersion() {
99
		return $this->configService->getAppValue('installed_version');
100
	}
101
102
103
	/**
104
	 * @return array
105
	 */
106
	public function getConfiguration() {
107
		return $this->configService->getConfig();
108
	}
109
110
111
	/**
112
	 * @return string
113
	 */
114
	public function getAppId() {
115
		return Application::APP_NAME;
116
	}
117
118
119
	public function setRunner(Runner $runner) {
120
		$this->runner = $runner;
121
	}
122
123
124
	/**
125
	 * @param IndexOptions $options
126
	 */
127
	public function setIndexOptions($options) {
128
		$this->indexOptions = $options;
129
	}
130
131
132
	/**
133
	 * @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...
134
	 */
135
	public function getOptionsTemplate() {
136
//				'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...
137
		return [
138
			'panel'      => [
139
				'options' => [
140
					[
141
						'name'  => 'files_within_dir',
142
						'title' => 'Within current directory',
143
						'type'  => 'checkbox'
144
					],
145
					[
146
						'name'  => 'files_local',
147
						'title' => 'Within local files',
148
						'type'  => 'checkbox'
149
					],
150
					[
151
						'name'  => 'files_external',
152
						'title' => 'Within external files',
153
						'type'  => 'checkbox'
154
					],
155
					[
156
						'name'  => 'files_group_folders',
157
						'title' => 'Within group folders',
158
						'type'  => 'checkbox'
159
					],
160
					[
161
						'name'        => 'files_extension',
162
						'title'       => 'Filter by extension',
163
						'type'        => 'input',
164
						'size'        => 'small',
165
						'placeholder' => 'txt'
166
					]
167
				]
168
			],
169
			'navigation' => [
170
				'icon'    => 'icon-fts-files',
171
				'options' => [
172
					[
173
						'name'  => 'files_local',
174
						'title' => 'Local Files',
175
						'type'  => 'checkbox'
176
					],
177
					[
178
						'name'  => 'files_external',
179
						'title' => 'External Files',
180
						'type'  => 'checkbox'
181
					],
182
					[
183
						'name'  => 'files_group_folders',
184
						'title' => 'Group Folders',
185
						'type'  => 'checkbox'
186
					],
187
					[
188
						'name'        => 'files_extension',
189
						'title'       => 'Extension',
190
						'type'        => 'input',
191
						'size'        => 'small',
192
						'placeholder' => 'txt'
193
					]
194
				]
195
			]
196
		];
197
	}
198
199
200
	/**
201
	 * @deprecated
202
	 */
203
	public function getOptions() {
204
		return $this->getOptionsTemplate();
205
	}
206
207
208
	/**
209
	 * called when loading all providers.
210
	 *
211
	 * Loading some containers.
212
	 *
213
	 * @throws QueryException
214
	 */
215
	public function loadProvider() {
216
		$app = new Application();
217
218
		$container = $app->getContainer();
219
		$this->configService = $container->query(ConfigService::class);
220
		$this->filesService = $container->query(FilesService::class);
221
		$this->searchService = $container->query(SearchService::class);
222
		$this->elasticSearchService = $container->query(ElasticSearchService::class);
223
		$this->miscService = $container->query(MiscService::class);
224
	}
225
226
227
	/**
228
	 * returns all indexable document for a user.
229
	 * There is no need to fill the document with content at this point.
230
	 *
231
	 * $platform is provided if the mapping needs to be changed.
232
	 *
233
	 * @param string $userId
234
	 *
235
	 * @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...
236
	 * @throws InterruptException
237
	 * @throws TickDoesNotExistException
238
	 * @throws InvalidPathException
239
	 * @throws NotFoundException
240
	 */
241
	public function generateIndexableDocuments($userId) {
242
		$files = $this->filesService->getFilesFromUser($this->runner, $userId, $this->indexOptions);
243
244
		return $files;
245
	}
246
247
248
	/**
249
	 * generate documents prior to the indexing.
250
	 * throw NoResultException if no more result
251
	 *
252
	 * @param IndexDocument[] $chunk
253
	 *
254
	 * @return IndexDocument[]
255
	 * @throws InterruptException
256
	 * @throws TickDoesNotExistException
257
	 */
258
	public function fillIndexDocuments($chunk) {
259
260
		/** @var FilesDocument[] $chunk */
261
262
		$index = [];
263
		foreach ($chunk as $document) {
264
			$this->updateRunnerAction('fillDocument', true);
265
			$this->updateRunnerInfoArray(
266
				[
267
					'documentId' => $document->getId(),
268
					'title'      => '',
269
					'content'    => ''
270
				]
271
			);
272
273
			if (!($document instanceof FilesDocument)) {
274
				continue;
275
			}
276
277
			$result = $this->filesService->generateDocument($document);
278
			$this->updateRunnerInfoArray(
279
				[
280
					'info' => $document->getMimetype(),
281
					'title'   => $document->getTitle(),
282
					'content' => $document->getContentSize()
283
				]
284
			);
285
286
			$index[] = $result;
287
		}
288
289
		return $index;
290
	}
291
292
293
	/**
294
	 * @param IndexDocument $document
295
	 *
296
	 * @return bool
297
	 */
298
	public function isDocumentUpToDate($document) {
299
		return $this->filesService->isDocumentUpToDate($document);
300
	}
301
302
303
	/**
304
	 * @param Index $index
305
	 *
306
	 * @return IndexDocument|null
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use FilesDocument|null.

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...
307
	 * @throws InvalidPathException
308
	 * @throws NotFoundException
309
	 * @throws NotPermittedException
310
	 */
311
	public function updateDocument(Index $index) {
312
		return $this->filesService->updateDocument($index);
313
	}
314
315
316
	/**
317
	 * @param IFullTextSearchPlatform $platform
318
	 */
319
	public function onInitializingIndex(IFullTextSearchPlatform $platform) {
320
		$this->elasticSearchService->onInitializingIndex($platform);
321
	}
322
323
324
	/**
325
	 * @param IFullTextSearchPlatform $platform
326
	 */
327
	public function onResettingIndex(IFullTextSearchPlatform $platform) {
328
		$this->elasticSearchService->onResettingIndex($platform);
329
	}
330
331
332
	/**
333
	 * not used yet
334
	 */
335
	public function unloadProvider() {
336
	}
337
338
339
	/**
340
	 * before a search, improve the request
341
	 *
342
	 * @param SearchRequest $request
343
	 */
344
	public function improveSearchRequest(SearchRequest $request) {
345
		$this->searchService->improveSearchRequest($request);
346
	}
347
348
349
	/**
350
	 * after a search, improve results
351
	 *
352
	 * @param SearchResult $searchResult
353
	 */
354
	public function improveSearchResult(SearchResult $searchResult) {
355
		$this->searchService->improveSearchResult($searchResult);
356
	}
357
358
359
	/**
360
	 * @param $action
361
	 * @param bool $force
362
	 *
363
	 * @throws InterruptException
364
	 * @throws TickDoesNotExistException
365
	 */
366
	private function updateRunnerAction($action, $force = false) {
367
		if ($this->runner === null) {
368
			return;
369
		}
370
371
		$this->runner->updateAction($action, $force);
372
	}
373
374
	/**
375
	 * @param array $data
376
	 */
377
	private function updateRunnerInfoArray($data) {
378
		if ($this->runner === null) {
379
			return;
380
		}
381
382
		$this->runner->setInfoArray($data);
383
	}
384
385
386
}