Completed
Push — master ( bf3f6e...f59a0d )
by Maxence
02:56 queued 01:15
created

BookmarksProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 6
1
<?php
2
declare(strict_types=1);
3
4
5
/**
6
 * Bookmarks_FullTextSearch - Indexing bookmarks
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later. See the COPYING file.
10
 *
11
 * @author Maxence Lange <[email protected]>
12
 * @copyright 2018
13
 * @license GNU AGPL version 3 or any later version
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
 *
28
 */
29
30
31
namespace OCA\Bookmarks_FullTextSearch\Provider;
32
33
34
use Exception;
35
use OCA\Bookmarks_FullTextSearch\Exceptions\WebpageIsNotIndexableException;
36
use OCA\Bookmarks_FullTextSearch\Model\BookmarksDocument;
37
use OCA\Bookmarks_FullTextSearch\Service\BookmarksService;
38
use OCA\Bookmarks_FullTextSearch\Service\ConfigService;
39
use OCA\Bookmarks_FullTextSearch\Service\MiscService;
40
use OCA\Bookmarks_FullTextSearch\Service\SearchService;
41
use OCA\Bookmarks_FullTextSearch\Service\TagsService;
42
use OCP\AppFramework\QueryException;
43
use OCP\FullTextSearch\IFullTextSearchPlatform;
44
use OCP\FullTextSearch\IFullTextSearchProvider;
45
use OCP\FullTextSearch\Model\IIndex;
46
use OCP\FullTextSearch\Model\IIndexOptions;
47
use OCP\FullTextSearch\Model\IndexDocument;
48
use OCP\FullTextSearch\Model\IRunner;
49
use OCP\FullTextSearch\Model\ISearchRequest;
50
use OCP\FullTextSearch\Model\ISearchResult;
51
use OCP\FullTextSearch\Model\SearchTemplate;
52
use OCP\IL10N;
53
54
55
class BookmarksProvider implements IFullTextSearchProvider {
56
57
58
	const BOOKMARKS_PROVIDER_ID = 'bookmarks';
59
60
61
	/** @var IL10N */
62
	private $l10n;
63
64
	/** @var ConfigService */
65
	private $configService;
66
67
	/** @var BookmarksService */
68
	private $bookmarksService;
69
70
	/** @var TagsService */
71
	private $tagsService;
72
73
	/** @var SearchService */
74
	private $searchService;
75
76
	/** @var MiscService */
77
	private $miscService;
78
79
80
	/** @var IRunner */
81
	private $runner;
82
83
	/** @var IIndexOptions */
84
	private $indexOptions;
85
86
87
	public function __construct(
88
		IL10N $l10n, ConfigService $configService, BookmarksService $bookmarksService,
89
		TagsService $tagsService, SearchService $searchService, MiscService $miscService
90
	) {
91
		$this->l10n = $l10n;
92
		$this->configService = $configService;
93
		$this->bookmarksService = $bookmarksService;
94
		$this->tagsService = $tagsService;
95
		$this->searchService = $searchService;
96
		$this->miscService = $miscService;
97
	}
98
99
100
	/**
101
	 * return unique id of the provider
102
	 */
103
	public function getId(): string {
104
		return self::BOOKMARKS_PROVIDER_ID;
105
	}
106
107
108
	/**
109
	 * return name of the provider
110
	 */
111
	public function getName(): string {
112
		return 'Bookmarks';
113
	}
114
115
116
//	/**
117
//	 * @return string
118
//	 */
119
//	public function getVersion() {
120
//		return $this->configService->getAppValue('installed_version');
121
//	}
122
//
123
//
124
//	/**
125
//	 * @return string
126
//	 */
127
//	public function getAppId() {
128
//		return Application::APP_NAME;
129
//	}
130
//
131
132
	/**
133
	 * @return array
134
	 */
135
	public function getConfiguration(): array {
136
		return $this->configService->getConfig();
137
	}
138
139
140
	/**
141
	 * @param IRunner $runner
142
	 */
143
	public function setRunner(IRunner $runner) {
144
		$this->runner = $runner;
145
	}
146
147
148
	/**
149
	 * @param IIndexOptions $options
150
	 */
151
	public function setIndexOptions(IIndexOptions $options) {
152
		$this->indexOptions = $options;
153
	}
154
155
156
	public function getSearchTemplate(): SearchTemplate {
157
158
		$template = new SearchTemplate('icon-fts-bookmarks', 'fulltextsearch');
159
160
//		$template->addPanelOption(
161
//			new SearchOption(
162
//				'files_within_dir', $this->l10n->t('Within current directory'),
163
//				SearchOption::CHECKBOX
164
//			)
165
//		);
166
//
167
//		$template->addPanelOption(
168
//			new SearchOption(
169
//				'files_local', $this->l10n->t('Within local files'),
170
//				SearchOption::CHECKBOX
171
//			)
172
//		);
173
//		$template->addNavigationOption(
174
//			new SearchOption(
175
//				'files_local', $this->l10n->t('Local files'),
176
//				SearchOption::CHECKBOX
177
//			)
178
//		);
179
//
180
//		if ($this->configService->getAppValue(ConfigService::FILES_EXTERNAL) === '1') {
181
//			$template->addPanelOption(
182
//				new SearchOption(
183
//					'files_external', $this->l10n->t('Within external files'),
184
//					SearchOption::CHECKBOX
185
//				)
186
//			);
187
//			$template->addNavigationOption(
188
//				new SearchOption(
189
//					'files_external', $this->l10n->t('External files'), SearchOption::CHECKBOX
190
//				)
191
//			);
192
//		}
193
//
194
//		if ($this->configService->getAppValue(ConfigService::FILES_GROUP_FOLDERS) === '1') {
195
//			$template->addPanelOption(
196
//				new SearchOption(
197
//					'files_group_folders', $this->l10n->t('Within group folders'),
198
//					SearchOption::CHECKBOX
199
//				)
200
//			);
201
//			$template->addNavigationOption(
202
//				new SearchOption(
203
//					'files_group_folders', $this->l10n->t('Group folders'),
204
//					SearchOption::CHECKBOX
205
//				)
206
//			);
207
//		}
208
//
209
//		$template->addPanelOption(
210
//			new SearchOption(
211
//				'files_extension', $this->l10n->t('Filter by extension'), SearchOption::INPUT,
212
//				SearchOption::INPUT_SMALL, 'txt'
213
//			)
214
//		);
215
//		$template->addNavigationOption(
216
//			new SearchOption(
217
//				'files_extension', $this->l10n->t('Extension'), SearchOption::INPUT,
218
//				SearchOption::INPUT_SMALL, 'txt'
219
//			)
220
//		);
221
222
		return $template;
223
	}
224
225
226
227
//		/**
228
//	 * @return array
229
//	 */
230
//	public function getOptionsTemplate() {
231
//		return [
232
//			'navigation' => [
233
//				'icon' => 'icon-fts-bookmarks',
234
//				//				'options' => [
235
//				//					[
236
//				//						'name'  => 'bookmarks_tags',
237
//				//						'title' => 'Filter tags',
238
//				//						'type'  => 'tags',
239
//				//						'list'  => $this->tagsService->getAllForUser()
240
//				//					]
241
//				//				]
242
//			]
243
//		];
244
//	}
245
246
247
	/**
248
	 * called when loading all providers.
249
	 *
250
	 * Loading some containers.
251
	 *
252
	 * @throws QueryException
253
	 */
254
	public function loadProvider() {
255
		$appManager = \OC::$server->getAppManager();
256
		if (!$appManager->isInstalled('bookmarks')) {
257
			throw new QueryException('bookmarks app not available');
258
		}
259
	}
260
261
262
	/**
263
	 * returns all indexable document for a user.
264
	 * There is no need to fill the document with content at this point.
265
	 *
266
	 * $platform is provided if the mapping needs to be changed.
267
	 *
268
	 * @param string $userId
269
	 *
270
	 * @return IndexDocument[]
271
	 */
272
	public function generateIndexableDocuments(string $userId): array {
273
		$bookmarks = $this->bookmarksService->getBookmarksFromUser($userId);
274
275
		return $bookmarks;
276
	}
277
278
279
	/**
280
	 * @param IndexDocument $document
281
	 */
282
	public function fillIndexDocument(IndexDocument $document) {
283
		/** @var BookmarksDocument $document */
284
		try {
285
			$this->updateRunnerInfoArray(
286
				[
287
					'info'    => $document->getSource(),
288
					'title'   => '',
289
					'content' => ''
290
				]
291
			);
292
293
			/** @var BookmarksDocument $document */
294
			$this->bookmarksService->updateDocumentFromBookmarksDocument($document);
295
		} catch (Exception $e) {
296
			$this->manageErrorException($document, $e);
297
		}
298
299
	}
300
301
302
//	/**
303
//	 * @param IndexDocument $document
304
//	 */
305
//	public function fillIndexDocument(IndexDocument $document) {
306
//		try {
307
//			$this->updateRunnerInfo('info', $document->getSource());
308
//
309
//			/** @var BookmarksDocument $document */
310
//			$this->bookmarksService->updateDocumentFromBookmarksDocument($document);
311
//
312
//		} catch (Exception $e) {
313
//			$this->manageErrorException($document, $e);
314
//		}
315
//	}
316
317
318
	/**
319
	 * @param IndexDocument $document
320
	 *
321
	 * @return bool
322
	 */
323
	public function isDocumentUpToDate(IndexDocument $document): bool {
324
		return $this->bookmarksService->isDocumentUpToDate($document);
325
	}
326
327
328
	/**
329
	 * @param IIndex $index
330
	 *
331
	 * @return IndexDocument
332
	 * @throws WebpageIsNotIndexableException
333
	 */
334
	public function updateDocument(IIndex $index): IndexDocument {
335
		/** @var BookmarksDocument $document */
336
		$document = $this->bookmarksService->updateDocument($index);
337
		$this->updateRunnerInfo('info', $document->getSource());
338
339
		return $document;
340
	}
341
342
343
	/**
344
	 * @param IFullTextSearchPlatform $platform
345
	 */
346
	public function onInitializingIndex(IFullTextSearchPlatform $platform) {
347
	}
348
349
350
	/**
351
	 * @param IFullTextSearchPlatform $platform
352
	 */
353
	public function onResettingIndex(IFullTextSearchPlatform $platform) {
354
	}
355
356
357
	/**
358
	 * not used yet
359
	 */
360
	public function unloadProvider() {
361
	}
362
363
364
	/**
365
	 * before a search, improve the request
366
	 *
367
	 * @param ISearchRequest $request
368
	 */
369
	public function improveSearchRequest(ISearchRequest $request) {
370
		$this->searchService->improveSearchRequest($request);
371
	}
372
373
374
	/**
375
	 * after a search, improve results
376
	 *
377
	 * @param ISearchResult $searchResult
378
	 */
379
	public function improveSearchResult(ISearchResult $searchResult) {
380
		foreach ($searchResult->getDocuments() as $document) {
381
			/** @var BookmarksDocument $document */
382
			$document->setLink($document->getSource());
383
			$document->setInfo('source', $document->getSource());
384
		}
385
	}
386
387
388
	/**
389
	 * @param IndexDocument $document
390
	 * @param Exception $e
391
	 */
392
	private function manageErrorException(IndexDocument $document, Exception $e) {
393
		$document->getIndex()
394
				 ->addError($e->getMessage(), get_class($e), IIndex::ERROR_SEV_3);
395
		$this->updateNewIndexError(
396
			$document->getIndex(), $e->getMessage(), get_class($e), IIndex::ERROR_SEV_3
397
		);
398
	}
399
400
401
	/**
402
	 * @param IIndex $index
403
	 * @param string $message
404
	 * @param string $exception
405
	 * @param int $sev
406
	 */
407
	private function updateNewIndexError(IIndex $index, string $message, string $exception, int $sev
408
	) {
409
		if ($this->runner === null) {
410
			return;
411
		}
412
413
		$this->runner->newIndexError($index, $message, $exception, $sev);
414
	}
415
416
417
	/**
418
	 * @param string $info
419
	 * @param string $value
420
	 */
421
	private function updateRunnerInfo(string $info, string $value) {
422
		if ($this->runner === null) {
423
			return;
424
		}
425
426
		$this->runner->setInfo($info, $value);
427
	}
428
429
430
	/**
431
	 * @param array $data
432
	 */
433
	private function updateRunnerInfoArray(array $data) {
434
		if ($this->runner === null) {
435
			return;
436
		}
437
438
		$this->runner->setInfoArray($data);
439
	}
440
441
}
442
443