Completed
Push — master ( d323cb...c1f910 )
by Maxence
01:34
created

BookmarksProvider::updateNewIndexError()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 4
1
<?php
2
/**
3
 * Bookmarks_FullTextSearch - Indexing bookmarks
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\Bookmarks_FullTextSearch\Provider;
28
29
use Exception;
30
use OCA\Bookmarks_FullTextSearch\AppInfo\Application;
31
use OCA\Bookmarks_FullTextSearch\Model\BookmarksDocument;
32
use OCA\Bookmarks_FullTextSearch\Service\BookmarksService;
33
use OCA\Bookmarks_FullTextSearch\Service\ConfigService;
34
use OCA\Bookmarks_FullTextSearch\Service\ElasticSearchService;
35
use OCA\Bookmarks_FullTextSearch\Service\MiscService;
36
use OCA\Bookmarks_FullTextSearch\Service\SearchService;
37
use OCA\Bookmarks_FullTextSearch\Service\TagsService;
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
48
49
class BookmarksProvider implements IFullTextSearchProvider {
50
51
52
	const BOOKMARKS_PROVIDER_ID = 'bookmarks';
53
54
	/** @var ConfigService */
55
	private $configService;
56
57
	/** @var BookmarksService */
58
	private $bookmarksService;
59
60
	/** @var TagsService */
61
	private $tagsService;
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::BOOKMARKS_PROVIDER_ID;
85
	}
86
87
88
	/**
89
	 * return name of the provider
90
	 */
91
	public function getName() {
92
		return 'Bookmarks';
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 string
106
	 */
107
	public function getAppId() {
108
		return Application::APP_NAME;
109
	}
110
111
112
	/**
113
	 * @return array
114
	 */
115
	public function getConfiguration() {
116
		return $this->configService->getConfig();
117
	}
118
119
120
	/**
121
	 * @param Runner $runner
122
	 */
123
	public function setRunner(Runner $runner) {
124
		$this->runner = $runner;
125
	}
126
127
128
	/**
129
	 * @param IndexOptions $options
130
	 */
131
	public function setIndexOptions($options) {
132
		$this->indexOptions = $options;
133
	}
134
135
136
	/**
137
	 * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array<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...
138
	 */
139
	public function getOptionsTemplate() {
140
		return [
141
			'navigation' => [
142
				'icon' => 'icon-fts-bookmarks',
143
				//				'options' => [
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
144
				//					[
145
				//						'name'  => 'bookmarks_tags',
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...
146
				//						'title' => 'Filter tags',
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...
147
				//						'type'  => 'tags',
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...
148
				//						'list'  => $this->tagsService->getAllForUser()
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
149
				//					]
150
				//				]
151
			]
152
		];
153
	}
154
155
156
	/**
157
	 * called when loading all providers.
158
	 *
159
	 * Loading some containers.
160
	 *
161
	 * @throws QueryException
162
	 */
163
	public function loadProvider() {
164
		$appManager = \OC::$server->getAppManager();
165
		if (!$appManager->isInstalled('bookmarks')) {
166
			throw new QueryException();
167
		}
168
169
		$app = new Application();
170
171
		$container = $app->getContainer();
172
		$this->configService = $container->query(ConfigService::class);
173
		$this->bookmarksService = $container->query(BookmarksService::class);
174
		$this->tagsService = $container->query(TagsService::class);
175
		$this->searchService = $container->query(SearchService::class);
176
		$this->elasticSearchService = $container->query(ElasticSearchService::class);
177
		$this->miscService = $container->query(MiscService::class);
178
	}
179
180
181
	/**
182
	 * returns all indexable document for a user.
183
	 * There is no need to fill the document with content at this point.
184
	 *
185
	 * $platform is provided if the mapping needs to be changed.
186
	 *
187
	 * @param string $userId
188
	 *
189
	 * @return BookmarksDocument[]
190
	 */
191
	public function generateIndexableDocuments($userId) {
192
		$bookmarks = $this->bookmarksService->getBookmarksFromUser($this->runner, $userId);
193
194
		return $bookmarks;
195
	}
196
197
198
	/**
199
	 * generate documents prior to the indexing.
200
	 * throw NoResultException if no more result
201
	 *
202
	 * @param IndexDocument[] $chunk
203
	 *
204
	 * @return IndexDocument[]
205
	 */
206
	public function fillIndexDocuments($chunk) {
207
208
		$index = [];
209
		/** @var BookmarksDocument[] $chunk */
210
		foreach ($chunk as $document) {
211
			if (!($document instanceof BookmarksDocument)) {
212
				continue;
213
			}
214
			$this->miscService->log('#BM: ' . $document->getId());
215
			try {
216
				$this->bookmarksService->updateDocumentFromBookmarksDocument($document);
217
			} catch (Exception $e) {
218
				$this->manageErrorException($document, $e);
219
			}
220
221
			$index[] = $document;
222
		}
223
224
		return $index;
225
	}
226
227
228
	/**
229
	 * @param IndexDocument $document
230
	 *
231
	 * @return bool
232
	 */
233
	public function isDocumentUpToDate($document) {
234
		return $this->bookmarksService->isDocumentUpToDate($document);
235
	}
236
237
238
	/**
239
	 * @param Index $index
240
	 *
241
	 * @return BookmarksDocument|null
242
	 */
243
	public function updateDocument(Index $index) {
244
		return $this->bookmarksService->updateDocument($index);
245
	}
246
247
248
	/**
249
	 * @param IFullTextSearchPlatform $platform
250
	 */
251
	public function onInitializingIndex(IFullTextSearchPlatform $platform) {
252
		$this->elasticSearchService->onInitializingIndex($platform);
253
	}
254
255
256
	/**
257
	 * @param IFullTextSearchPlatform $platform
258
	 */
259
	public function onResettingIndex(IFullTextSearchPlatform $platform) {
260
		$this->elasticSearchService->onResettingIndex($platform);
261
	}
262
263
264
	/**
265
	 * not used yet
266
	 */
267
	public function unloadProvider() {
268
	}
269
270
271
	/**
272
	 * before a search, improve the request
273
	 *
274
	 * @param SearchRequest $request
275
	 */
276
	public function improveSearchRequest(SearchRequest $request) {
277
		$this->searchService->improveSearchRequest($request);
278
	}
279
280
281
	/**
282
	 * after a search, improve results
283
	 *
284
	 * @param SearchResult $searchResult
285
	 */
286
	public function improveSearchResult(SearchResult $searchResult) {
287
		foreach ($searchResult->getDocuments() as $document) {
288
			$document->setLink($document->getSource());
289
			$document->setInfo('source', $document->getSource());
290
		}
291
	}
292
293
294
	/**
295
	 * @param IndexDocument $document
296
	 * @param Exception $e
297
	 */
298
	private function manageErrorException(IndexDocument $document, Exception $e) {
299
		$document->getIndex()
300
				 ->addError($e->getMessage(), get_class($e), Index::ERROR_SEV_3);
301
		$this->updateNewIndexError(
302
			$document->getIndex(), $e->getMessage(), get_class($e), Index::ERROR_SEV_3
303
		);
304
	}
305
306
307
	/**
308
	 * @param Index $index
309
	 * @param string $message
310
	 * @param string $exception
311
	 * @param int $sev
312
	 */
313
	private function updateNewIndexError($index, $message, $exception, $sev) {
314
		if ($this->runner === null) {
315
			return;
316
		}
317
318
		$this->runner->newIndexError($index, $message, $exception, $sev);
319
	}
320
321
322
}