Completed
Push — master ( d5da72...6387aa )
by Maxence
01:37
created

FilesProvider::isDocumentUpToDate()   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 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\ElasticSearchService;
32
use OCA\Files_FullTextSearch\Service\FilesService;
33
use OCA\Files_FullTextSearch\Service\MiscService;
34
use OCA\Files_FullTextSearch\Service\SearchService;
35
use OCA\FullTextSearch\Exceptions\InterruptException;
36
use OCA\FullTextSearch\Exceptions\TickDoesNotExistException;
37
use OCA\FullTextSearch\IFullTextSearchPlatform;
38
use OCA\FullTextSearch\IFullTextSearchProvider;
39
use OCA\FullTextSearch\Model\Index;
40
use OCA\FullTextSearch\Model\IndexDocument;
41
use OCA\FullTextSearch\Model\Runner;
42
use OCA\FullTextSearch\Model\SearchRequest;
43
use OCA\FullTextSearch\Model\SearchResult;
44
use OCP\AppFramework\Http\TemplateResponse;
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 FilesService */
56
	private $filesService;
57
58
	/** @var SearchService */
59
	private $searchService;
60
61
	/** @var ElasticSearchService */
62
	private $elasticSearchService;
63
64
	/** @var MiscService */
65
	private $miscService;
66
67
68
	/** @var Runner */
69
	private $runner;
70
71
72
	/**
73
	 * return unique id of the provider
74
	 */
75
	public function getId() {
76
		return self::FILES_PROVIDER_ID;
77
	}
78
79
80
	/**
81
	 * return name of the provider
82
	 */
83
	public function getName() {
84
		return 'Files';
85
	}
86
87
88
	public function getAppId() {
89
		return Application::APP_NAME;
90
	}
91
92
93
	public function setRunner(Runner $runner) {
94
		$this->runner = $runner;
95
	}
96
97
98
	/**
99
	 * @return string
100
	 */
101
	public function getOptionsTemplate() {
102
		return 'options.panel';
103
	}
104
105
106
	/**
107
	 * called when loading all providers.
108
	 *
109
	 * Loading some containers.
110
	 *
111
	 * @throws QueryException
112
	 */
113
	public function loadProvider() {
114
		$app = new Application();
115
116
		$container = $app->getContainer();
117
		$this->filesService = $container->query(FilesService::class);
118
		$this->searchService = $container->query(SearchService::class);
119
		$this->elasticSearchService = $container->query(ElasticSearchService::class);
120
		$this->miscService = $container->query(MiscService::class);
121
	}
122
123
124
	/**
125
	 * returns all indexable document for a user.
126
	 * There is no need to fill the document with content at this point.
127
	 *
128
	 * $platform is provided if the mapping needs to be changed.
129
	 *
130
	 * @param string $userId
131
	 *
132
	 * @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...
133
	 * @throws InterruptException
134
	 * @throws TickDoesNotExistException
135
	 * @throws InvalidPathException
136
	 * @throws NotFoundException
137
	 */
138
	public function generateIndexableDocuments($userId) {
139
		$files = $this->filesService->getFilesFromUser($this->runner, $userId);
140
141
		return $files;
142
	}
143
144
145
	/**
146
	 * generate documents prior to the indexing.
147
	 * throw NoResultException if no more result
148
	 *
149
	 * @param IndexDocument[] $chunk
150
	 *
151
	 * @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...
152
	 */
153
	public function fillIndexDocuments($chunk) {
154
155
		/** @var FilesDocument[] $chunk */
156
		$result = $this->filesService->generateDocuments($chunk);
157
158
		return $result;
159
	}
160
161
162
	/**
163
	 * @param IndexDocument $document
164
	 *
165
	 * @return bool
166
	 */
167
	public function isDocumentUpToDate($document) {
168
		return $this->filesService->isDocumentUpToDate($document);
169
	}
170
171
172
	/**
173
	 * @param Index $index
174
	 *
175
	 * @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...
176
	 * @throws InvalidPathException
177
	 * @throws NotFoundException
178
	 * @throws NotPermittedException
179
	 */
180
	public function updateDocument(Index $index) {
181
		return $this->filesService->updateDocument($index);
182
	}
183
184
185
	/**
186
	 * @param IFullTextSearchPlatform $platform
187
	 */
188
	public function onInitializingIndex(IFullTextSearchPlatform $platform) {
189
		$this->elasticSearchService->onInitializingIndex($platform);
190
	}
191
192
	/**
193
	 * @param IFullTextSearchPlatform $platform
194
	 * @param array $arr
195
	 */
196
	public function onIndexingDocument(IFullTextSearchPlatform $platform, &$arr) {
197
		$this->elasticSearchService->onIndexingDocument($platform, $arr);
198
	}
199
200
	/**
201
	 * @param IFullTextSearchPlatform $platform
202
	 */
203
	public function onRemovingIndex(IFullTextSearchPlatform $platform) {
204
		$this->elasticSearchService->onRemovingIndex($platform);
205
	}
206
207
	/**
208
	 * @param IFullTextSearchPlatform $platform
209
	 * @param SearchRequest $request
210
	 * @param array $arr
211
	 */
212
	public function onSearchingQuery(IFullTextSearchPlatform $platform, SearchRequest $request, &$arr) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 101 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
213
		$this->elasticSearchService->onSearchingQuery($platform, $request, $arr);
214
	}
215
216
217
	/**
218
	 * not used yet
219
	 */
220
	public function unloadProvider() {
221
	}
222
223
224
	/**
225
	 * before a search, improve the request
226
	 *
227
	 * @param SearchRequest $request
228
	 */
229
	public function improveSearchRequest(SearchRequest $request) {
230
		$this->searchService->improveSearchRequest($request);
231
	}
232
233
234
	/**
235
	 * after a search, improve results
236
	 *
237
	 * @param SearchResult $searchResult
238
	 *
239
	 * @throws InvalidPathException
240
	 * @throws NotFoundException
241
	 */
242
	public function improveSearchResult(SearchResult $searchResult) {
243
244
		foreach ($searchResult->getDocuments() as $document) {
245
			$this->filesService->setDocumentInfo($document);
246
			$this->filesService->setDocumentTitle($document);
247
			$this->filesService->setDocumentLink($document);
248
			$this->filesService->setDocumentMore($document);
249
		}
250
	}
251
252
253
}