-
-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathBookmarksParser.php
More file actions
292 lines (274 loc) Β· 7.47 KB
/
Copy pathBookmarksParser.php
File metadata and controls
292 lines (274 loc) Β· 7.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
/*
* Copyright (c) 2020-2024. The Nextcloud Bookmarks contributors.
*
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
*/
/*
* (c) Pedro Rodrigues <relvas.rodrigues@gmail.com>
*/
namespace OCA\Bookmarks\Service;
use DateTime;
use DOMDocument;
use DOMNode;
use DOMXPath;
use OCA\Bookmarks\Exception\HtmlParseError;
use const LIBXML_PARSEHUGE;
/**
* Bookmark parser
*
* @author Pedro Rodrigues <relvas.rodrigues@gmail.com>
*/
class BookmarksParser {
public const THOUSAND_YEARS = 60 * 60 * 24 * 365 * 1000;
/**
* Netscape Bookmark File Format DOCTYPE
*/
public const DOCTYPE = 'NETSCAPE-Bookmark-file-1';
/**
* DOMXPath
*
* @var DOMXPath
*/
private $xpath;
/**
* An array of bookmarks
*
* @var array
*/
public $bookmarks = [];
/**
* The parent bookmark folder
*
* @var array
*/
private $parentFolder = [];
/**
* The current folder
*
* @var array
*/
public $currentFolder = [];
/**
* Folder depth
*
* @var array
*/
private $folderDepth = [];
/**
* If we should use \DateTime objects
*
* @var bool
*/
private $useDateTimeObjects = true;
/**
* If the Personal Toolbar Folder should be ignored
*
* @var bool
*/
private $ignorePersonalToolbarFolder = true;
/**
* Constructor
*
* @param bool $useInternalErrors Use internal errors
*/
public function __construct($useInternalErrors = true) {
libxml_use_internal_errors($useInternalErrors);
}
/**
* Check if doctype file is valid for parsing
*
* @param string $doctype Document Doctype
*
* @return boolean
*/
public static function isValid($doctype): bool {
return $doctype === self::DOCTYPE;
}
/**
* Parses a Netscape Bookmark File Format HTML string to a PHP value.
*
* @param string $input A Netscape Bookmark File Format HTML string
* @param bool $ignorePersonalToolbarFolder If we should ignore the personal toolbar bookmark folder
* @param bool $useDateTimeObjects If we should return \DateTime objects
*
* @return mixed A PHP value
*
* @throws HtmlParseError
*/
public function parse($input, $ignorePersonalToolbarFolder = true, $useDateTimeObjects = true) {
$document = new DOMDocument();
$document->preserveWhiteSpace = false;
if (empty($input)) {
throw new HtmlParseError("The input shouldn't be empty");
}
if ($document->loadHTML($input, LIBXML_PARSEHUGE) === false) {
throw new HtmlParseError('The HTML value does not appear to be valid Netscape Bookmark File Format HTML.');
}
$this->xpath = new DOMXPath($document);
$this->ignorePersonalToolbarFolder = $ignorePersonalToolbarFolder;
$this->useDateTimeObjects = $useDateTimeObjects;
// set root folder
$this->currentFolder = ['bookmarks' => [], 'children' => []];
$this->folderDepth[] = & $this->currentFolder;
$this->traverse();
return empty($this->bookmarks) ? null : $this->bookmarks;
}
private function traverse(?DOMNode $node = null): void {
if ($node !== null) {
$entries = $node->childNodes;
} else {
$query = './*';
$entries = $this->xpath->query($query, null);
}
if (!$entries) {
return;
}
foreach ($entries as $entry) {
if ($entry === null) {
continue;
}
switch ($entry->nodeName) {
case 'dl':
$this->traverse($entry);
if (count($this->folderDepth) > 1) {
$this->closeFolder();
}
break;
case 'a':
$this->addBookmark($entry);
break;
case 'dd':
$this->addDescription($entry);
if ($entry->hasChildNodes()) {
$this->traverse($entry);
}
break;
case 'h3':
$this->addFolder($entry);
break;
default:
if ($entry->hasChildNodes()) {
$this->traverse($entry);
}
}
}
}
/**
* Add a folder from a \DOMNode
*
* @param DOMNode $node
*/
private function addFolder(DOMNode $node): void {
$folder = [
'title' => $node->textContent,
'children' => [],
'bookmarks' => [],
];
$folder = array_merge($folder, $this->getAttributes($node));
if (isset($folder['personal_toolbar_folder']) && $this->ignorePersonalToolbarFolder) {
return;
}
$this->currentFolder['children'][] = & $folder;
$this->folderDepth[] = & $folder;
$this->currentFolder = & $folder;
}
/**
* Close current folder
*/
private function closeFolder(): void {
array_pop($this->folderDepth);
$this->currentFolder = & $this->folderDepth[count($this->folderDepth) - 1];
}
/**
* Add a bookmark from a \DOMNode
*
* @param DOMNode $node
*/
private function addBookmark(DOMNode $node): void {
$bookmark = [
'title' => $node->textContent,
'description' => '',
'tags' => [],
];
$bookmark = array_merge($bookmark, $this->getAttributes($node));
$this->currentFolder['bookmarks'][] = & $bookmark;
$this->bookmarks[] = & $bookmark;
}
/**
* Add a bookmark from a \DOMNode
*
* @param DOMNode $node
*/
private function addDescription(DOMNode $node): void {
$count = count($this->bookmarks);
if ($count === 0) {
return;
}
$bookmark = & $this->bookmarks[$count - 1];
$bookmark['description'] = $node->textContent;
}
/**
* Get attributes of a \DOMNode
*
* @param DOMNode $node
*
* @return array
*
* @psalm-return array<string, mixed>
*/
private function getAttributes(DOMNode $node): array {
$attributes = [];
if ($node->attributes) {
foreach ($node->attributes as $attribute) {
$attributes[strtolower($attribute->nodeName)] = $attribute->nodeValue;
}
}
$lastModified = null;
if (isset($attributes['time_added'])) {
$attributes['add_date'] = $attributes['time_added'];
}
if ($this->useDateTimeObjects) {
if (isset($attributes['add_date'])) {
$added = new DateTime();
if ((int)$attributes['add_date'] > self::THOUSAND_YEARS) {
// Google exports dates in milliseconds. This way we only lose the first year of UNIX Epoch.
// This is invalid once we hit 2970. So, quite a long time.
$added->setTimestamp(((int)($attributes['add_date']) / 1000));
} else {
$added->setTimestamp((int)$attributes['add_date']);
}
$attributes['add_date'] = $added;
}
if (isset($attributes['last_modified'])) {
$modified = new DateTime();
$modified->setTimestamp($attributes['last_modified'] instanceof DateTime ? $attributes['last_modified']->getTimestamp() : (int)$attributes['last_modified']);
$attributes['last_modified'] = $modified;
}
} else {
if (isset($attributes['add_date'])) {
if ((int)$attributes['add_date'] > self::THOUSAND_YEARS) {
// Google exports dates in milliseconds. This way we only lose the first year of UNIX Epoch.
// This is invalid once we hit 2970. So, quite a long time.
$attributes['add_date'] = ((int)($attributes['add_date']) / 1000);
} else {
$attributes['add_date'] = (int)$attributes['add_date'];
}
}
if (isset($attributes['last_modified'])) {
$attributes['last_modified'] = $attributes['last_modified'] instanceof DateTime ? $attributes['last_modified']->getTimestamp() : (int)$attributes['last_modified'];
if ((int)$attributes['last_modified'] > self::THOUSAND_YEARS) {
// Google exports dates in milliseconds. This way we only lose the first year of UNIX Epoch.
// This is invalid once we hit 2970. So, quite a long time.
$attributes['last_modified'] = ((int)($attributes['last_modified']) / 1000);
} else {
$attributes['last_modified'] = (int)$attributes['last_modified'];
}
}
}
if (isset($attributes['tags'])) {
$attributes['tags'] = explode(',', $attributes['tags']);
}
return $attributes;
}
}