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\Service; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
use OCA\Bookmarks\Bookmarks; |
35
|
|
|
use OCA\Bookmarks_FullTextSearch\Exceptions\WebpageIsNotIndexableException; |
36
|
|
|
use OCA\Bookmarks_FullTextSearch\Model\BookmarksDocument; |
37
|
|
|
use OCP\FullTextSearch\Model\DocumentAccess; |
38
|
|
|
use OCP\FullTextSearch\Model\IIndex; |
39
|
|
|
use OCP\FullTextSearch\Model\IndexDocument; |
40
|
|
|
use OCP\AppFramework\IAppContainer; |
41
|
|
|
use OCP\AppFramework\QueryException; |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
class BookmarksService { |
45
|
|
|
|
46
|
|
|
const DOCUMENT_TYPE = 'bookmarks'; |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** @var ConfigService */ |
50
|
|
|
private $configService; |
51
|
|
|
|
52
|
|
|
/** @var MiscService */ |
53
|
|
|
private $miscService; |
54
|
|
|
|
55
|
|
|
/** @var Bookmarks */ |
56
|
|
|
private $bookmarksClass; |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* BookmarksService constructor. |
61
|
|
|
* |
62
|
|
|
* @param IAppContainer $container |
63
|
|
|
* @param ConfigService $configService |
64
|
|
|
* @param MiscService $miscService |
65
|
|
|
*/ |
66
|
|
|
public function __construct( |
67
|
|
|
IAppContainer $container, ConfigService $configService, MiscService $miscService |
68
|
|
|
) { |
69
|
|
|
$this->configService = $configService; |
70
|
|
|
$this->miscService = $miscService; |
71
|
|
|
|
72
|
|
|
try { |
73
|
|
|
$this->bookmarksClass = $container->query(Bookmarks::class); |
74
|
|
|
} catch (QueryException $e) { |
75
|
|
|
$miscService->log('### ' . $e->getMessage()); |
76
|
|
|
/** we do nothing */ |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $userId |
83
|
|
|
* |
84
|
|
|
* @return BookmarksDocument[] |
85
|
|
|
*/ |
86
|
|
|
public function getBookmarksFromUser(string $userId): array { |
87
|
|
|
|
88
|
|
|
$bookmarks = $this->bookmarksClass->findBookmarks($userId, 0, 'id', [], false, -1); |
89
|
|
|
|
90
|
|
|
$documents = []; |
91
|
|
|
foreach ($bookmarks as $bookmark) { |
92
|
|
|
$document = $this->generateBookmarksDocumentFromBookmark($bookmark, $userId); |
93
|
|
|
|
94
|
|
|
$documents[] = $document; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $documents; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param BookmarksDocument $document |
103
|
|
|
* |
104
|
|
|
* @throws WebpageIsNotIndexableException |
105
|
|
|
*/ |
106
|
|
|
public function updateDocumentFromBookmarksDocument(BookmarksDocument $document) { |
107
|
|
|
if ($this->bookmarksClass === null) { |
108
|
|
|
return; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$userId = $document->getAccess() |
112
|
|
|
->getOwnerId(); |
113
|
|
|
|
114
|
|
|
$bookmark = $this->bookmarksClass->findUniqueBookmark($document->getId(), $userId); |
115
|
|
|
$document->addPart('description', $bookmark['description']); |
116
|
|
|
|
117
|
|
|
$html = $this->getWebpageFromUrl($document->getSource()); |
118
|
|
|
$document->setContent(base64_encode($html), IndexDocument::ENCODED_BASE64); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $url |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
* @throws WebpageIsNotIndexableException |
127
|
|
|
*/ |
128
|
|
|
private function getWebpageFromUrl(string $url): string { |
129
|
|
|
$curl = curl_init($url); |
130
|
|
|
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); |
131
|
|
|
curl_setopt($curl, CURLOPT_TIMEOUT, 10); |
132
|
|
|
|
133
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
134
|
|
|
|
135
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); |
136
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); |
137
|
|
|
|
138
|
|
|
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); |
139
|
|
|
|
140
|
|
|
$html = (string)curl_exec($curl); |
141
|
|
|
if (curl_error($curl)) { |
142
|
|
|
throw new WebpageIsNotIndexableException('Webpage is not reachable - ' . $url); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
curl_close($curl); |
146
|
|
|
|
147
|
|
|
return $html; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param IndexDocument $document |
153
|
|
|
* |
154
|
|
|
* @return bool |
155
|
|
|
*/ |
156
|
|
|
public function isDocumentUpToDate(IndexDocument $document): bool { |
157
|
|
|
$index = $document->getIndex(); |
158
|
|
|
|
159
|
|
|
if ($index->getStatus() !== IIndex::INDEX_OK) { |
160
|
|
|
return false; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$s = $this->configService->getAppValue(ConfigService::BOOKMARKS_TTL) * 3600 * 24; |
164
|
|
|
|
165
|
|
|
return ($index->getLastIndex() > time() - $s); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param array $bookmark |
171
|
|
|
* @param string $userId |
172
|
|
|
* |
173
|
|
|
* @return BookmarksDocument |
174
|
|
|
*/ |
175
|
|
|
private function generateBookmarksDocumentFromBookmark(array $bookmark, string $userId) { |
176
|
|
|
$document = new BookmarksDocument($bookmark['id']); |
177
|
|
|
|
178
|
|
|
$document->setAccess(new DocumentAccess($userId)) |
179
|
|
|
->setModifiedTime((int)$bookmark['lastmodified']) |
180
|
|
|
->setSource($bookmark['url']) |
181
|
|
|
->setTitle($bookmark['title']) |
182
|
|
|
->setTags($bookmark['tags']); |
183
|
|
|
|
184
|
|
|
return $document; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param IIndex $index |
190
|
|
|
* |
191
|
|
|
* @return BookmarksDocument |
192
|
|
|
* @throws WebpageIsNotIndexableException |
193
|
|
|
*/ |
194
|
|
|
public function updateDocument(IIndex $index): BookmarksDocument { |
195
|
|
|
$document = $this->generateDocumentFromIndex($index); |
196
|
|
|
|
197
|
|
|
return $document; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param IIndex $index |
203
|
|
|
* |
204
|
|
|
* @return BookmarksDocument |
205
|
|
|
* @throws WebpageIsNotIndexableException |
206
|
|
|
*/ |
207
|
|
|
private function generateDocumentFromIndex(IIndex $index) { |
208
|
|
|
|
209
|
|
|
$bookmark = |
210
|
|
|
$this->bookmarksClass->findUniqueBookmark( |
211
|
|
|
$index->getDocumentId(), $index->getOwnerId() |
212
|
|
|
); |
213
|
|
|
|
214
|
|
|
if (sizeof($bookmark) === 0) { |
215
|
|
|
$index->setStatus(IIndex::INDEX_REMOVE); |
216
|
|
|
$document = new BookmarksDocument($index->getDocumentId()); |
217
|
|
|
$document->setIndex($index); |
218
|
|
|
|
219
|
|
|
return $document; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
$document = $this->generateBookmarksDocumentFromBookmark($bookmark, $index->getOwnerId()); |
223
|
|
|
$document->setIndex($index); |
224
|
|
|
|
225
|
|
|
$this->updateDocumentFromBookmarksDocument($document); |
226
|
|
|
|
227
|
|
|
return $document; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
|