Completed
Push — master ( bba49c...c01dbf )
by Maxence
02:15
created

FilesProvider::getAppId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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\Runner;
43
use OCA\FullTextSearch\Model\SearchRequest;
44
use OCA\FullTextSearch\Model\SearchResult;
45
use OCP\AppFramework\QueryException;
46
use OCP\Files\InvalidPathException;
47
use OCP\Files\NotFoundException;
48
use OCP\Files\NotPermittedException;
49
50
class FilesProvider implements IFullTextSearchProvider {
51
52
53
	const FILES_PROVIDER_ID = 'files';
54
55
	/** @var ConfigService */
56
	private $configService;
57
58
	/** @var FilesService */
59
	private $filesService;
60
61
	/** @var SearchService */
62
	private $searchService;
63
64
	/** @var ElasticSearchService */
65
	private $elasticSearchService;
66
67
	/** @var MiscService */
68
	private $miscService;
69
70
71
	/** @var Runner */
72
	private $runner;
73
74
75
	/**
76
	 * return unique id of the provider
77
	 */
78
	public function getId() {
79
		return self::FILES_PROVIDER_ID;
80
	}
81
82
83
	/**
84
	 * return name of the provider
85
	 */
86
	public function getName() {
87
		return 'Files';
88
	}
89
90
91
	/**
92
	 * @return string
93
	 */
94
	public function getVersion() {
95
		return $this->configService->getAppValue('installed_version');
96
	}
97
98
99
	/**
100
	 * @return array
101
	 */
102
	public function getConfiguration() {
103
		return $this->configService->getConfig();
104
	}
105
106
107
	/**
108
	 * @return string
109
	 */
110
	public function getAppId() {
111
		return Application::APP_NAME;
112
	}
113
114
115
	public function setRunner(Runner $runner) {
116
		$this->runner = $runner;
117
	}
118
119
120
	/**
121
	 * @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...
122
	 */
123
	public function getOptionsTemplate() {
124
//				'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...
125
		return [
126
			'panel'      => [
127
				'options' => [
128
					[
129
						'name'  => 'files_within_dir',
130
						'title' => 'Within current directory',
131
						'type'  => 'checkbox'
132
					],
133
					[
134
						'name'  => 'files_local',
135
						'title' => 'Within local files',
136
						'type'  => 'checkbox'
137
					],
138
					[
139
						'name'  => 'files_external',
140
						'title' => 'Within external files',
141
						'type'  => 'checkbox'
142
					],
143
					[
144
						'name'  => 'files_group_folders',
145
						'title' => 'Within group folders',
146
						'type'  => 'checkbox'
147
					],
148
					[
149
						'name'        => 'files_extension',
150
						'title'       => 'Filter by extension',
151
						'type'        => 'input',
152
						'size'        => 'small',
153
						'placeholder' => 'txt'
154
					]
155
				]
156
			],
157
			'navigation' => [
158
				'css'     => 'fulltextsearch',
159
				'icon'    => 'icon-files',
160
				'options' => [
161
					[
162
						'name'  => 'files_local',
163
						'title' => 'Local Files',
164
						'type'  => 'checkbox'
165
					],
166
					[
167
						'name'  => 'files_external',
168
						'title' => 'External Files',
169
						'type'  => 'checkbox'
170
					],
171
					[
172
						'name'  => 'files_group_folders',
173
						'title' => 'Group Folders',
174
						'type'  => 'checkbox'
175
					],
176
					[
177
						'name'        => 'files_extension',
178
						'title'       => 'Extension',
179
						'type'        => 'input',
180
						'size'        => 'small',
181
						'placeholder' => 'txt'
182
					]
183
				]
184
			]
185
		];
186
	}
187
188
189
	/**
190
	 * called when loading all providers.
191
	 *
192
	 * Loading some containers.
193
	 *
194
	 * @throws QueryException
195
	 */
196
	public function loadProvider() {
197
		$app = new Application();
198
199
		$container = $app->getContainer();
200
		$this->configService = $container->query(ConfigService::class);
201
		$this->filesService = $container->query(FilesService::class);
202
		$this->searchService = $container->query(SearchService::class);
203
		$this->elasticSearchService = $container->query(ElasticSearchService::class);
204
		$this->miscService = $container->query(MiscService::class);
205
	}
206
207
208
	/**
209
	 * returns all indexable document for a user.
210
	 * There is no need to fill the document with content at this point.
211
	 *
212
	 * $platform is provided if the mapping needs to be changed.
213
	 *
214
	 * @param string $userId
215
	 *
216
	 * @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...
217
	 * @throws InterruptException
218
	 * @throws TickDoesNotExistException
219
	 * @throws InvalidPathException
220
	 * @throws NotFoundException
221
	 */
222
	public function generateIndexableDocuments($userId) {
223
		$files = $this->filesService->getFilesFromUser($this->runner, $userId);
224
225
		return $files;
226
	}
227
228
229
	/**
230
	 * generate documents prior to the indexing.
231
	 * throw NoResultException if no more result
232
	 *
233
	 * @param IndexDocument[] $chunk
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
	 */
237
	public function fillIndexDocuments($chunk) {
238
239
		/** @var FilesDocument[] $chunk */
240
		$result = $this->filesService->generateDocuments($chunk);
241
242
		return $result;
243
	}
244
245
246
	/**
247
	 * @param IndexDocument $document
248
	 *
249
	 * @return bool
250
	 */
251
	public function isDocumentUpToDate($document) {
252
		return $this->filesService->isDocumentUpToDate($document);
253
	}
254
255
256
	/**
257
	 * @param Index $index
258
	 *
259
	 * @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...
260
	 * @throws InvalidPathException
261
	 * @throws NotFoundException
262
	 * @throws NotPermittedException
263
	 */
264
	public function updateDocument(Index $index) {
265
		return $this->filesService->updateDocument($index);
266
	}
267
268
269
	/**
270
	 * @param IFullTextSearchPlatform $platform
271
	 */
272
	public function onInitializingIndex(IFullTextSearchPlatform $platform) {
273
		$this->elasticSearchService->onInitializingIndex($platform);
274
	}
275
276
277
	/**
278
	 * @param IFullTextSearchPlatform $platform
279
	 */
280
	public function onResettingIndex(IFullTextSearchPlatform $platform) {
281
		$this->elasticSearchService->onResettingIndex($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 SearchRequest $request
296
	 */
297
	public function improveSearchRequest(SearchRequest $request) {
298
		$this->searchService->improveSearchRequest($request);
299
	}
300
301
302
	/**
303
	 * after a search, improve results
304
	 *
305
	 * @param SearchResult $searchResult
306
	 *
307
	 * @throws InvalidPathException
308
	 * @throws NotFoundException
309
	 */
310
	public function improveSearchResult(SearchResult $searchResult) {
311
312
		$indexDocuments = $searchResult->getDocuments();
313
		$filesDocuments = [];
314
		foreach ($indexDocuments as $indexDocument) {
315
316
			$filesDocument = FilesDocument::fromIndexDocument($indexDocument);
317
			$this->filesService->setDocumentInfo($filesDocument);
318
			$this->filesService->setDocumentTitle($filesDocument);
319
			$this->filesService->setDocumentLink($filesDocument);
320
			$this->filesService->setDocumentMore($filesDocument);
321
322
			$filesDocuments[] = $filesDocument;
323
		}
324
325
		$searchResult->setDocuments($filesDocuments);
326
	}
327
328
329
}