Completed
Push — master ( c1f910...a24cd2 )
by Maxence
01:53
created

BookmarksProvider::updateRunnerInfo()   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 2
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
		return $chunk;
208
	}
209
210
//
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% 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...
211
//		$index = [];
212
//		/** @var BookmarksDocument[] $chunk */
213
//		foreach ($chunk as $document) {
214
//			if (!($document instanceof BookmarksDocument)) {
215
//				continue;
216
//			}
217
//
218
//			try {
219
//				$this->bookmarksService->updateDocumentFromBookmarksDocument($document);
220
//			} catch (Exception $e) {
221
//				$this->manageErrorException($document, $e);
222
//			}
223
//
224
//			$index[] = $document;
225
//		}
226
//
227
//		return $index;
228
//	}
229
230
	/**
231
	 * @param IndexDocument $document
232
	 */
233
	public function fillIndexDocument(IndexDocument $document) {
234
		try {
235
			$this->updateRunnerInfo('info', $document->getSource());
236
237
			/** @var BookmarksDocument $document */
238
			$this->bookmarksService->updateDocumentFromBookmarksDocument($document);
239
240
		} catch (Exception $e) {
241
			$this->manageErrorException($document, $e);
242
		}
243
	}
244
245
246
	/**
247
	 * @param IndexDocument $document
248
	 *
249
	 * @return bool
250
	 */
251
	public function isDocumentUpToDate($document) {
252
		return $this->bookmarksService->isDocumentUpToDate($document);
253
	}
254
255
256
	/**
257
	 * @param Index $index
258
	 *
259
	 * @return BookmarksDocument|null
260
	 */
261
	public function updateDocument(Index $index) {
262
		return $this->bookmarksService->updateDocument($index);
263
	}
264
265
266
	/**
267
	 * @param IFullTextSearchPlatform $platform
268
	 */
269
	public function onInitializingIndex(IFullTextSearchPlatform $platform) {
270
		$this->elasticSearchService->onInitializingIndex($platform);
271
	}
272
273
274
	/**
275
	 * @param IFullTextSearchPlatform $platform
276
	 */
277
	public function onResettingIndex(IFullTextSearchPlatform $platform) {
278
		$this->elasticSearchService->onResettingIndex($platform);
279
	}
280
281
282
	/**
283
	 * not used yet
284
	 */
285
	public function unloadProvider() {
286
	}
287
288
289
	/**
290
	 * before a search, improve the request
291
	 *
292
	 * @param SearchRequest $request
293
	 */
294
	public function improveSearchRequest(SearchRequest $request) {
295
		$this->searchService->improveSearchRequest($request);
296
	}
297
298
299
	/**
300
	 * after a search, improve results
301
	 *
302
	 * @param SearchResult $searchResult
303
	 */
304
	public function improveSearchResult(SearchResult $searchResult) {
305
		foreach ($searchResult->getDocuments() as $document) {
306
			$document->setLink($document->getSource());
307
			$document->setInfo('source', $document->getSource());
308
		}
309
	}
310
311
312
	/**
313
	 * @param IndexDocument $document
314
	 * @param Exception $e
315
	 */
316
	private function manageErrorException(IndexDocument $document, Exception $e) {
317
		$document->getIndex()
318
				 ->addError($e->getMessage(), get_class($e), Index::ERROR_SEV_3);
319
		$this->updateNewIndexError(
320
			$document->getIndex(), $e->getMessage(), get_class($e), Index::ERROR_SEV_3
321
		);
322
	}
323
324
325
	/**
326
	 * @param Index $index
327
	 * @param string $message
328
	 * @param string $exception
329
	 * @param int $sev
330
	 */
331
	private function updateNewIndexError($index, $message, $exception, $sev) {
332
		if ($this->runner === null) {
333
			return;
334
		}
335
336
		$this->runner->newIndexError($index, $message, $exception, $sev);
337
	}
338
339
340
	/**
341
	 * @param string $info
342
	 * @param string $value
343
	 */
344
	private function updateRunnerInfo($info, $value) {
345
		if ($this->runner === null) {
346
			return;
347
		}
348
349
		$this->runner->setInfo($info, $value);
350
	}
351
352
353
}