]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/NetscapeBookmarkUtils.php
4 * Utilities to import and export bookmarks using the Netscape format
6 class NetscapeBookmarkUtils
10 * Filters links and adds Netscape-formatted fields
13 * - timestamp link addition date, using the Unix epoch format
14 * - taglist comma-separated tag list
16 * @param LinkDB $linkDb Link datastore
17 * @param string $selection Which links to export: (all|private|public)
18 * @param bool $prependNoteUrl Prepend note permalinks with the server's URL
19 * @param string $indexUrl Absolute URL of the Shaarli index page
21 * @throws Exception Invalid export selection
23 * @return array The links to be exported, with additional fields
25 public static function filterAndFormat($linkDb, $selection, $prependNoteUrl, $indexUrl)
27 // see tpl/export.html for possible values
28 if (! in_array($selection, array('all', 'public', 'private'))) {
29 throw new Exception('Invalid export selection: "'.$selection.'"');
32 $bookmarkLinks = array();
34 foreach ($linkDb as $link) {
35 if ($link['private'] != 0 && $selection == 'public') {
38 if ($link['private'] == 0 && $selection == 'private') {
41 $date = $link['created'];
42 $link['timestamp'] = $date->getTimestamp();
43 $link['taglist'] = str_replace(' ', ',', $link['tags']);
45 if (startsWith($link['url'], '?') && $prependNoteUrl) {
46 $link['url'] = $indexUrl . $link['url'];
49 $bookmarkLinks[] = $link;
52 return $bookmarkLinks;
56 * Generates an import status summary
58 * @param string $filename name of the file to import
59 * @param int $filesize size of the file to import
60 * @param int $importCount how many links were imported
61 * @param int $overwriteCount how many links were overwritten
62 * @param int $skipCount how many links were skipped
64 * @return string Summary of the bookmark import status
66 private static function importStatus(
74 $status = 'File '.$filename.' ('.$filesize.' bytes) ';
75 if ($importCount == 0 && $overwriteCount == 0 && $skipCount == 0) {
76 $status .= 'has an unknown file format. Nothing was imported.';
78 $status .= 'was successfully processed: '.$importCount.' links imported, ';
79 $status .= $overwriteCount.' links overwritten, ';
80 $status .= $skipCount.' links skipped.';
86 * Imports Web bookmarks from an uploaded Netscape bookmark dump
88 * @param array $post Server $_POST parameters
89 * @param array $files Server $_FILES parameters
90 * @param LinkDB $linkDb Loaded LinkDB instance
91 * @param string $pagecache Page cache
93 * @return string Summary of the bookmark import status
95 public static function import($post, $files, $linkDb, $pagecache)
97 $filename = $files['filetoupload']['name'];
98 $filesize = $files['filetoupload']['size'];
99 $data = file_get_contents($files['filetoupload']['tmp_name']);
101 if (strpos($data, '<!DOCTYPE NETSCAPE-Bookmark-file-1>') === false) {
102 return self
::importStatus($filename, $filesize);
105 // Overwrite existing links?
106 $overwrite = ! empty($post['overwrite']);
108 // Add tags to all imported links?
109 if (empty($post['default_tags'])) {
110 $defaultTags = array();
112 $defaultTags = preg_split(
114 escape($post['default_tags'])
118 // links are imported as public by default
121 $parser = new NetscapeBookmarkParser(
122 true, // nested tag support
123 $defaultTags, // additional user-specified tags
124 strval(1 - $defaultPrivacy) // defaultPub = 1 - defaultPrivacy
126 $bookmarks = $parser->parseString($data);
132 foreach ($bookmarks as $bkm) {
133 $private = $defaultPrivacy;
134 if (empty($post['privacy']) || $post['privacy'] == 'default') {
135 // use value from the imported file
136 $private = $bkm['pub'] == '1' ? 0 : 1;
137 } else if ($post['privacy'] == 'private') {
138 // all imported links are private
140 } else if ($post['privacy'] == 'public') {
141 // all imported links are public
146 'title' => $bkm['title'],
147 'url' => $bkm['uri'],
148 'description' => $bkm['note'],
149 'private' => $private,
150 'tags' => $bkm['tags']
153 $existingLink = $linkDb->getLinkFromUrl($bkm['uri']);
155 if ($existingLink !== false) {
156 if ($overwrite === false) {
157 // Do not overwrite an existing link
162 // Overwrite an existing link, keep its date
163 $newLink['id'] = $existingLink['id'];
164 $newLink['created'] = $existingLink['created'];
165 $newLink['updated'] = new DateTime();
166 $linkDb[$existingLink['id']] = $newLink;
172 // Add a new link - @ used for UNIX timestamps
173 $newLinkDate = new DateTime('@'.strval($bkm['time']));
174 $newLinkDate->setTimezone(new DateTimeZone(date_default_timezone_get()));
175 $newLink['created'] = $newLinkDate;
176 $newLink['id'] = $linkDb->getNextId();
177 $newLink['shorturl'] = link_small_hash($newLink['created'], $newLink['id']);
178 $linkDb[$newLink['id']] = $newLink;
182 $linkDb->save($pagecache);
183 return self
::importStatus(
193 * Generates an import status summary
195 * @param string $filename name of the file to import
196 * @param int $filesize size of the file to import
197 * @param int $importCount how many links were imported
198 * @param int $overwriteCount how many links were overwritten
199 * @param int $skipCount how many links were skipped
201 * @return string Summary of the bookmark import status
203 private static function importStatus(
211 $status = 'File '.$filename.' ('.$filesize.' bytes) ';
212 if ($importCount == 0 && $overwriteCount == 0 && $skipCount == 0) {
213 $status .= 'has an unknown file format. Nothing was imported.';
215 $status .= 'was successfully processed: '.$importCount.' links imported, ';
216 $status .= $overwriteCount.' links overwritten, ';
217 $status .= $skipCount.' links skipped.';
223 * Imports Web bookmarks from an uploaded Netscape bookmark dump
225 * @param array $post Server $_POST parameters
226 * @param array $files Server $_FILES parameters
227 * @param LinkDB $linkDb Loaded LinkDB instance
228 * @param string $pagecache Page cache
230 * @return string Summary of the bookmark import status
232 public static function import($post, $files, $linkDb, $pagecache)
234 $filename = $files['filetoupload']['name'];
235 $filesize = $files['filetoupload']['size'];
236 $data = file_get_contents($files['filetoupload']['tmp_name']);
238 if (strpos($data, '<!DOCTYPE NETSCAPE-Bookmark-file-1>') === false) {
239 return self
::importStatus($filename, $filesize);
242 // Overwrite existing links?
243 $overwrite = ! empty($post['overwrite']);
245 // Add tags to all imported links?
246 if (empty($post['default_tags'])) {
247 $defaultTags = array();
249 $defaultTags = preg_split(
251 escape($post['default_tags'])
255 // links are imported as public by default
258 $parser = new NetscapeBookmarkParser(
259 true, // nested tag support
260 $defaultTags, // additional user-specified tags
261 strval(1 - $defaultPrivacy) // defaultPub = 1 - defaultPrivacy
263 $bookmarks = $parser->parseString($data);
269 foreach ($bookmarks as $bkm) {
270 $private = $defaultPrivacy;
271 if (empty($post['privacy']) || $post['privacy'] == 'default') {
272 // use value from the imported file
273 $private = $bkm['pub'] == '1' ? 0 : 1;
274 } else if ($post['privacy'] == 'private') {
275 // all imported links are private
277 } else if ($post['privacy'] == 'public') {
278 // all imported links are public
283 'title' => $bkm['title'],
284 'url' => $bkm['uri'],
285 'description' => $bkm['note'],
286 'private' => $private,
288 'tags' => $bkm['tags']
291 $existingLink = $linkDb->getLinkFromUrl($bkm['uri']);
293 if ($existingLink !== false) {
294 if ($overwrite === false) {
295 // Do not overwrite an existing link
300 // Overwrite an existing link, keep its date
301 $newLink['linkdate'] = $existingLink['linkdate'];
302 $linkDb[$existingLink['linkdate']] = $newLink;
309 $newLinkDate = new DateTime('@'.strval($bkm['time']));
310 while (!empty($linkDb[$newLinkDate->format(LinkDB
::LINK_DATE_FORMAT
)])) {
311 // Ensure the date/time is not already used
312 // - this hack is necessary as the date/time acts as a primary key
313 // - apply 1 second increments until an unused index is found
314 // See https://github.com/shaarli/Shaarli/issues/351
315 $newLinkDate->add(new DateInterval('PT1S'));
317 $linkDbDate = $newLinkDate->format(LinkDB
::LINK_DATE_FORMAT
);
318 $newLink['linkdate'] = $linkDbDate;
319 $linkDb[$linkDbDate] = $newLink;
323 $linkDb->save($pagecache);
324 return self
::importStatus(