]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/NetscapeBookmarkUtils.php
Add a persistent 'shorturl' key to all links
[github/shaarli/Shaarli.git] / application / NetscapeBookmarkUtils.php
CommitLineData
cd5327be
V
1<?php
2
3/**
4 * Utilities to import and export bookmarks using the Netscape format
5 */
6class NetscapeBookmarkUtils
7{
8
9 /**
10 * Filters links and adds Netscape-formatted fields
11 *
12 * Added fields:
13 * - timestamp link addition date, using the Unix epoch format
14 * - taglist comma-separated tag list
15 *
bb4a23aa
V
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
cd5327be
V
20 *
21 * @throws Exception Invalid export selection
22 *
23 * @return array The links to be exported, with additional fields
24 */
bb4a23aa 25 public static function filterAndFormat($linkDb, $selection, $prependNoteUrl, $indexUrl)
cd5327be
V
26 {
27 // see tpl/export.html for possible values
bb4a23aa 28 if (! in_array($selection, array('all', 'public', 'private'))) {
cd5327be
V
29 throw new Exception('Invalid export selection: "'.$selection.'"');
30 }
31
32 $bookmarkLinks = array();
33
34 foreach ($linkDb as $link) {
35 if ($link['private'] != 0 && $selection == 'public') {
36 continue;
37 }
38 if ($link['private'] == 0 && $selection == 'private') {
39 continue;
40 }
01878a75 41 $date = $link['created'];
cd5327be
V
42 $link['timestamp'] = $date->getTimestamp();
43 $link['taglist'] = str_replace(' ', ',', $link['tags']);
bb4a23aa
V
44
45 if (startsWith($link['url'], '?') && $prependNoteUrl) {
46 $link['url'] = $indexUrl . $link['url'];
47 }
48
cd5327be
V
49 $bookmarkLinks[] = $link;
50 }
51
52 return $bookmarkLinks;
53 }
a973afea
V
54
55 /**
56 * Generates an import status summary
57 *
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
63 *
64 * @return string Summary of the bookmark import status
65 */
66 private static function importStatus(
67 $filename,
68 $filesize,
69 $importCount=0,
70 $overwriteCount=0,
71 $skipCount=0
72 )
73 {
74 $status = 'File '.$filename.' ('.$filesize.' bytes) ';
75 if ($importCount == 0 && $overwriteCount == 0 && $skipCount == 0) {
76 $status .= 'has an unknown file format. Nothing was imported.';
77 } else {
78 $status .= 'was successfully processed: '.$importCount.' links imported, ';
79 $status .= $overwriteCount.' links overwritten, ';
80 $status .= $skipCount.' links skipped.';
81 }
82 return $status;
83 }
84
85 /**
86 * Imports Web bookmarks from an uploaded Netscape bookmark dump
87 *
88 * @param array $post Server $_POST parameters
7af9a418 89 * @param array $files Server $_FILES parameters
a973afea
V
90 * @param LinkDB $linkDb Loaded LinkDB instance
91 * @param string $pagecache Page cache
92 *
93 * @return string Summary of the bookmark import status
94 */
95 public static function import($post, $files, $linkDb, $pagecache)
96 {
97 $filename = $files['filetoupload']['name'];
98 $filesize = $files['filetoupload']['size'];
99 $data = file_get_contents($files['filetoupload']['tmp_name']);
100
f4ad7bde 101 if (strpos($data, '<!DOCTYPE NETSCAPE-Bookmark-file-1>') === false) {
a973afea
V
102 return self::importStatus($filename, $filesize);
103 }
104
105 // Overwrite existing links?
106 $overwrite = ! empty($post['overwrite']);
107
108 // Add tags to all imported links?
109 if (empty($post['default_tags'])) {
110 $defaultTags = array();
111 } else {
112 $defaultTags = preg_split(
113 '/[\s,]+/',
114 escape($post['default_tags'])
115 );
116 }
117
118 // links are imported as public by default
119 $defaultPrivacy = 0;
120
121 $parser = new NetscapeBookmarkParser(
122 true, // nested tag support
123 $defaultTags, // additional user-specified tags
124 strval(1 - $defaultPrivacy) // defaultPub = 1 - defaultPrivacy
125 );
126 $bookmarks = $parser->parseString($data);
127
128 $importCount = 0;
129 $overwriteCount = 0;
130 $skipCount = 0;
131
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
139 $private = 1;
140 } else if ($post['privacy'] == 'public') {
141 // all imported links are public
142 $private = 0;
143 }
144
145 $newLink = array(
146 'title' => $bkm['title'],
147 'url' => $bkm['uri'],
148 'description' => $bkm['note'],
149 'private' => $private,
a973afea
V
150 'tags' => $bkm['tags']
151 );
152
153 $existingLink = $linkDb->getLinkFromUrl($bkm['uri']);
154
155 if ($existingLink !== false) {
156 if ($overwrite === false) {
157 // Do not overwrite an existing link
158 $skipCount++;
159 continue;
160 }
161
162 // Overwrite an existing link, keep its date
01878a75
A
163 $newLink['id'] = $existingLink['id'];
164 $newLink['created'] = $existingLink['created'];
165 $newLink['updated'] = new DateTime();
166 $linkDb[$existingLink['id']] = $newLink;
a973afea
V
167 $importCount++;
168 $overwriteCount++;
169 continue;
170 }
171
01878a75 172 // Add a new link - @ used for UNIX timestamps
a973afea 173 $newLinkDate = new DateTime('@'.strval($bkm['time']));
01878a75
A
174 $newLinkDate->setTimezone(new DateTimeZone(date_default_timezone_get()));
175 $newLink['created'] = $newLinkDate;
176 $newLink['id'] = $linkDb->getNextId();
d592daea 177 $newLink['shorturl'] = link_small_hash($newLink['created'], $newLink['id']);
01878a75 178 $linkDb[$newLink['id']] = $newLink;
a973afea
V
179 $importCount++;
180 }
181
f21abf32 182 $linkDb->save($pagecache);
a973afea
V
183 return self::importStatus(
184 $filename,
185 $filesize,
186 $importCount,
187 $overwriteCount,
188 $skipCount
189 );
190 }
cd5327be 191}