Completed
Push — master ( 985e16...434333 )
by Maxence
01:52
created

FilesProvider::getOptionsTemplate()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
rs 8.8571
cc 1
eloc 16
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<strin...rray<string,string>[]>>.

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
		return [
125
			'panel'      => [
126
				'template' => 'options.panel'
127
			],
128
			'navigation' => [
129
				'icon'    => 'icon-folder',
130
				'options' => [
131
					[
132
						'name'  => 'files_local',
133
						'title' => 'Local Files',
134
						'type'  => 'checkbox'
135
					],
136
					[
137
						'name'  => 'files_external',
138
						'title' => 'External Files',
139
						'type'  => 'checkbox'
140
					],
141
					[
142
						'name'  => 'files_extensions',
143
						'type'  => 'input',
144
						'title' => 'Extensions'
145
					]
146
				]
147
			]
148
		];
149
	}
150
151
152
	/**
153
	 * called when loading all providers.
154
	 *
155
	 * Loading some containers.
156
	 *
157
	 * @throws QueryException
158
	 */
159
	public function loadProvider() {
160
		$app = new Application();
161
162
		$container = $app->getContainer();
163
		$this->configService = $container->query(ConfigService::class);
164
		$this->filesService = $container->query(FilesService::class);
165
		$this->searchService = $container->query(SearchService::class);
166
		$this->elasticSearchService = $container->query(ElasticSearchService::class);
167
		$this->miscService = $container->query(MiscService::class);
168
	}
169
170
171
	/**
172
	 * returns all indexable document for a user.
173
	 * There is no need to fill the document with content at this point.
174
	 *
175
	 * $platform is provided if the mapping needs to be changed.
176
	 *
177
	 * @param string $userId
178
	 *
179
	 * @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...
180
	 * @throws InterruptException
181
	 * @throws TickDoesNotExistException
182
	 * @throws InvalidPathException
183
	 * @throws NotFoundException
184
	 */
185
	public function generateIndexableDocuments($userId) {
186
		$files = $this->filesService->getFilesFromUser($this->runner, $userId);
187
188
		return $files;
189
	}
190
191
192
	/**
193
	 * generate documents prior to the indexing.
194
	 * throw NoResultException if no more result
195
	 *
196
	 * @param IndexDocument[] $chunk
197
	 *
198
	 * @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...
199
	 */
200
	public function fillIndexDocuments($chunk) {
201
202
		/** @var FilesDocument[] $chunk */
203
		$result = $this->filesService->generateDocuments($chunk);
204
205
		return $result;
206
	}
207
208
209
	/**
210
	 * @param IndexDocument $document
211
	 *
212
	 * @return bool
213
	 */
214
	public function isDocumentUpToDate($document) {
215
		return $this->filesService->isDocumentUpToDate($document);
216
	}
217
218
219
	/**
220
	 * @param Index $index
221
	 *
222
	 * @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...
223
	 * @throws InvalidPathException
224
	 * @throws NotFoundException
225
	 * @throws NotPermittedException
226
	 */
227
	public function updateDocument(Index $index) {
228
		return $this->filesService->updateDocument($index);
229
	}
230
231
232
	/**
233
	 * @param IFullTextSearchPlatform $platform
234
	 */
235
	public function onInitializingIndex(IFullTextSearchPlatform $platform) {
236
		$this->elasticSearchService->onInitializingIndex($platform);
237
	}
238
239
	/**
240
	 * @param IFullTextSearchPlatform $platform
241
	 * @param array $arr
242
	 */
243
	public function onIndexingDocument(IFullTextSearchPlatform $platform, &$arr) {
244
		$this->elasticSearchService->onIndexingDocument($platform, $arr);
245
	}
246
247
	/**
248
	 * @param IFullTextSearchPlatform $platform
249
	 */
250
	public function onResettingIndex(IFullTextSearchPlatform $platform) {
251
		$this->elasticSearchService->onResettingIndex($platform);
252
	}
253
254
	/**
255
	 * @param IFullTextSearchPlatform $platform
256
	 * @param SearchRequest $request
257
	 * @param array $arr
258
	 */
259
	public function onSearchingQuery(
260
		IFullTextSearchPlatform $platform, SearchRequest $request, &$arr
261
	) {
262
		$this->elasticSearchService->onSearchingQuery($platform, $request, $arr);
263
	}
264
265
266
	/**
267
	 * not used yet
268
	 */
269
	public function unloadProvider() {
270
	}
271
272
273
	/**
274
	 * before a search, improve the request
275
	 *
276
	 * @param SearchRequest $request
277
	 */
278
	public function improveSearchRequest(SearchRequest $request) {
279
		$this->searchService->improveSearchRequest($request);
280
	}
281
282
283
	/**
284
	 * after a search, improve results
285
	 *
286
	 * @param SearchResult $searchResult
287
	 *
288
	 * @throws InvalidPathException
289
	 * @throws NotFoundException
290
	 */
291
	public function improveSearchResult(SearchResult $searchResult) {
292
293
		// TODO - avoid ->setInfo()
294
		foreach ($searchResult->getDocuments() as $document) {
295
			$this->filesService->setDocumentInfo($document);
296
			$this->filesService->setDocumentTitle($document);
297
			$this->filesService->setDocumentLink($document);
298
			$this->filesService->setDocumentMore($document);
299
		}
300
	}
301
302
303
}