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

BookmarksService::generateDocuments()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 4
nc 4
nop 1
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\Service;
28
29
30
use OCA\Bookmarks\Controller\Lib\Bookmarks;
31
use OCA\Bookmarks_FullTextSearch\Exceptions\WebpageIsNotIndexableException;
32
use OCA\Bookmarks_FullTextSearch\Model\BookmarksDocument;
33
use OCA\FullTextSearch\Model\DocumentAccess;
34
use OCA\FullTextSearch\Model\Index;
35
use OCA\FullTextSearch\Model\IndexDocument;
36
use OCA\FullTextSearch\Model\Runner;
37
use OCP\AppFramework\IAppContainer;
38
use OCP\AppFramework\QueryException;
39
40
class BookmarksService {
41
42
	const DOCUMENT_TYPE = 'bookmarks';
43
44
45
	/** @var ConfigService */
46
	private $configService;
47
48
	/** @var MiscService */
49
	private $miscService;
50
51
	/** @var Bookmarks */
52
	private $bookmarksClass;
53
54
55
	/**
56
	 * BookmarksService constructor.
57
	 *
58
	 * @param IAppContainer $container
59
	 * @param ConfigService $configService
60
	 * @param MiscService $miscService
61
	 */
62 View Code Duplication
	public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
		IAppContainer $container, ConfigService $configService, MiscService $miscService
64
	) {
65
		$this->configService = $configService;
66
		$this->miscService = $miscService;
67
68
		try {
69
			$this->bookmarksClass = $container->query(Bookmarks::class);
70
		} catch (QueryException $e) {
71
			/** we do nothing */
72
		}
73
	}
74
75
76
	/**
77
	 * @param Runner $runner
78
	 * @param string $userId
79
	 *
80
	 * @return BookmarksDocument[]
81
	 */
82
	public function getBookmarksFromUser(Runner $runner, $userId) {
83
84
		$bookmarks = $this->bookmarksClass->findBookmarks($userId, 0, 'id', [], false, -1);
85
86
		$documents = [];
87
		foreach ($bookmarks as $bookmark) {
88
			$document = $this->generateBookmarksDocumentFromBookmark($bookmark, $userId);
89
90
			$documents[] = $document;
91
		}
92
93
		return $documents;
94
	}
95
96
97
	/**
98
	 * @param BookmarksDocument $document
99
	 *
100
	 * @throws WebpageIsNotIndexableException
101
	 */
102
	public function updateDocumentFromBookmarksDocument(BookmarksDocument $document) {
103
		$userId = $document->getAccess()
104
						   ->getOwnerId();
105
106
		$bookmark = $this->bookmarksClass->findUniqueBookmark($document->getId(), $userId);
107
		$document->addPart('description', $bookmark['description']);
108
109
		$html = $this->getWebpageFromUrl($document->getSource());
110
		$document->setContent(base64_encode($html), IndexDocument::ENCODED_BASE64);
111
	}
112
113
114
	/**
115
	 * @param $url
116
	 *
117
	 * @return mixed
118
	 * @throws WebpageIsNotIndexableException
119
	 */
120
	private function getWebpageFromUrl($url) {
121
		$curl = curl_init($url);
122
		curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
123
		curl_setopt($curl, CURLOPT_TIMEOUT, 10);
124
125
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
126
127
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
128
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
129
130
		$html = curl_exec($curl);
131
		if (curl_error($curl)) {
132
			throw new WebpageIsNotIndexableException('Webpage is not reachable - ' . $url);
133
		}
134
135
		curl_close($curl);
136
137
		return $html;
138
	}
139
140
141
	/**
142
	 * @param IndexDocument $document
143
	 *
144
	 * @return bool
145
	 */
146
	public function isDocumentUpToDate($document) {
147
		$index = $document->getIndex();
148
149
		if ($index->getStatus() !== Index::INDEX_OK) {
150
			return false;
151
		}
152
153
		$s = $this->configService->getAppValue(ConfigService::BOOKMARKS_TTL) * 3600 * 24;
154
155
		return ($index->getLastIndex() > time() - $s);
156
	}
157
158
159
	/**
160
	 * @param $bookmark
161
	 * @param $userId
162
	 *
163
	 * @return BookmarksDocument
164
	 */
165
	private function generateBookmarksDocumentFromBookmark($bookmark, $userId) {
166
		$document = new BookmarksDocument($bookmark['id']);
167
168
		$document->setAccess(new DocumentAccess($userId))
169
				 ->setModifiedTime($bookmark['lastmodified'])
170
				 ->setSource($bookmark['url'])
171
				 ->setTitle($bookmark['title'])
172
				 ->setTags($bookmark['tags']);
173
174
		return $document;
175
	}
176
177
178
	/**
179
	 * @param Index $index
180
	 *
181
	 * @return null|BookmarksDocument
182
	 */
183
	public function updateDocument(Index $index) {
184
		try {
185
			$document = $this->generateDocumentFromIndex($index);
186
187
			return $document;
188
		} catch (WebpageIsNotIndexableException $e) {
189
			return null;
190
		}
191
	}
192
193
194
	/**
195
	 * @param Index $index
196
	 *
197
	 * @return BookmarksDocument
198
	 * @throws WebpageIsNotIndexableException
199
	 */
200
	private function generateDocumentFromIndex(Index $index) {
201
202
		$bookmark =
203
			$this->bookmarksClass->findUniqueBookmark(
204
				$index->getDocumentId(), $index->getOwnerId()
205
			);
206
207
		if (sizeof($bookmark) === 0) {
208
			$index->setStatus(Index::INDEX_REMOVE);
209
			$document = new BookmarksDocument($index->getDocumentId());
210
			$document->setIndex($index);
211
212
			return $document;
213
		}
214
215
		$document = $this->generateBookmarksDocumentFromBookmark($bookmark, $index->getOwnerId());
216
		$document->setIndex($index);
217
218
		$this->updateDocumentFromBookmarksDocument($document);
219
220
		return $document;
221
	}
222
223
224
}