aboutsummaryrefslogtreecommitdiffhomepage
path: root/application
diff options
context:
space:
mode:
Diffstat (limited to 'application')
-rw-r--r--application/FeedBuilder.php2
-rw-r--r--application/NetscapeBookmarkUtils.php141
-rw-r--r--application/Router.php6
3 files changed, 148 insertions, 1 deletions
diff --git a/application/FeedBuilder.php b/application/FeedBuilder.php
index fedd90e6..b0aa5764 100644
--- a/application/FeedBuilder.php
+++ b/application/FeedBuilder.php
@@ -143,7 +143,7 @@ class FeedBuilder
143 */ 143 */
144 protected function buildItem($link, $pageaddr) 144 protected function buildItem($link, $pageaddr)
145 { 145 {
146 $link['guid'] = $pageaddr .'?'. $link['shorturl']; 146 $link['guid'] = $pageaddr .'?'. smallHash($link['linkdate']);
147 // Check for both signs of a note: starting with ? and 7 chars long. 147 // Check for both signs of a note: starting with ? and 7 chars long.
148 if ($link['url'][0] === '?' && strlen($link['url']) === 7) { 148 if ($link['url'][0] === '?' && strlen($link['url']) === 7) {
149 $link['url'] = $pageaddr . $link['url']; 149 $link['url'] = $pageaddr . $link['url'];
diff --git a/application/NetscapeBookmarkUtils.php b/application/NetscapeBookmarkUtils.php
index e7148d00..f21ee359 100644
--- a/application/NetscapeBookmarkUtils.php
+++ b/application/NetscapeBookmarkUtils.php
@@ -188,4 +188,145 @@ class NetscapeBookmarkUtils
188 $skipCount 188 $skipCount
189 ); 189 );
190 } 190 }
191
192 /**
193 * Generates an import status summary
194 *
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
200 *
201 * @return string Summary of the bookmark import status
202 */
203 private static function importStatus(
204 $filename,
205 $filesize,
206 $importCount=0,
207 $overwriteCount=0,
208 $skipCount=0
209 )
210 {
211 $status = 'File '.$filename.' ('.$filesize.' bytes) ';
212 if ($importCount == 0 && $overwriteCount == 0 && $skipCount == 0) {
213 $status .= 'has an unknown file format. Nothing was imported.';
214 } else {
215 $status .= 'was successfully processed: '.$importCount.' links imported, ';
216 $status .= $overwriteCount.' links overwritten, ';
217 $status .= $skipCount.' links skipped.';
218 }
219 return $status;
220 }
221
222 /**
223 * Imports Web bookmarks from an uploaded Netscape bookmark dump
224 *
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
229 *
230 * @return string Summary of the bookmark import status
231 */
232 public static function import($post, $files, $linkDb, $pagecache)
233 {
234 $filename = $files['filetoupload']['name'];
235 $filesize = $files['filetoupload']['size'];
236 $data = file_get_contents($files['filetoupload']['tmp_name']);
237
238 if (strpos($data, '<!DOCTYPE NETSCAPE-Bookmark-file-1>') === false) {
239 return self::importStatus($filename, $filesize);
240 }
241
242 // Overwrite existing links?
243 $overwrite = ! empty($post['overwrite']);
244
245 // Add tags to all imported links?
246 if (empty($post['default_tags'])) {
247 $defaultTags = array();
248 } else {
249 $defaultTags = preg_split(
250 '/[\s,]+/',
251 escape($post['default_tags'])
252 );
253 }
254
255 // links are imported as public by default
256 $defaultPrivacy = 0;
257
258 $parser = new NetscapeBookmarkParser(
259 true, // nested tag support
260 $defaultTags, // additional user-specified tags
261 strval(1 - $defaultPrivacy) // defaultPub = 1 - defaultPrivacy
262 );
263 $bookmarks = $parser->parseString($data);
264
265 $importCount = 0;
266 $overwriteCount = 0;
267 $skipCount = 0;
268
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
276 $private = 1;
277 } else if ($post['privacy'] == 'public') {
278 // all imported links are public
279 $private = 0;
280 }
281
282 $newLink = array(
283 'title' => $bkm['title'],
284 'url' => $bkm['uri'],
285 'description' => $bkm['note'],
286 'private' => $private,
287 'linkdate'=> '',
288 'tags' => $bkm['tags']
289 );
290
291 $existingLink = $linkDb->getLinkFromUrl($bkm['uri']);
292
293 if ($existingLink !== false) {
294 if ($overwrite === false) {
295 // Do not overwrite an existing link
296 $skipCount++;
297 continue;
298 }
299
300 // Overwrite an existing link, keep its date
301 $newLink['linkdate'] = $existingLink['linkdate'];
302 $linkDb[$existingLink['linkdate']] = $newLink;
303 $importCount++;
304 $overwriteCount++;
305 continue;
306 }
307
308 // Add a new link
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'));
316 }
317 $linkDbDate = $newLinkDate->format(LinkDB::LINK_DATE_FORMAT);
318 $newLink['linkdate'] = $linkDbDate;
319 $linkDb[$linkDbDate] = $newLink;
320 $importCount++;
321 }
322
323 $linkDb->save($pagecache);
324 return self::importStatus(
325 $filename,
326 $filesize,
327 $importCount,
328 $overwriteCount,
329 $skipCount
330 );
331 }
191} 332}
diff --git a/application/Router.php b/application/Router.php
index caed4a28..c9a51912 100644
--- a/application/Router.php
+++ b/application/Router.php
@@ -31,6 +31,8 @@ class Router
31 31
32 public static $PAGE_EDITLINK = 'edit_link'; 32 public static $PAGE_EDITLINK = 'edit_link';
33 33
34 public static $PAGE_DELETELINK = 'delete_link';
35
34 public static $PAGE_EXPORT = 'export'; 36 public static $PAGE_EXPORT = 'export';
35 37
36 public static $PAGE_IMPORT = 'import'; 38 public static $PAGE_IMPORT = 'import';
@@ -120,6 +122,10 @@ class Router
120 return self::$PAGE_EDITLINK; 122 return self::$PAGE_EDITLINK;
121 } 123 }
122 124
125 if (isset($get['delete_link'])) {
126 return self::$PAGE_DELETELINK;
127 }
128
123 if (startsWith($query, 'do='. self::$PAGE_EXPORT)) { 129 if (startsWith($query, 'do='. self::$PAGE_EXPORT)) {
124 return self::$PAGE_EXPORT; 130 return self::$PAGE_EXPORT;
125 } 131 }