aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/NetscapeBookmarkUtils.php
diff options
context:
space:
mode:
authorArthur <arthur@hoa.ro>2016-08-10 12:10:41 +0200
committerGitHub <noreply@github.com>2016-08-10 12:10:41 +0200
commite0dd77c37e433d71462f87c8e8c75196aa958940 (patch)
tree4a13ddf32779cb8f02600d7cc20d23f692c9068c /application/NetscapeBookmarkUtils.php
parent085157c5cb6bd0df928c7875fb7997683418f1fb (diff)
parenta973afeac7b7399d35b881920f0afc1947765ccd (diff)
downloadShaarli-e0dd77c37e433d71462f87c8e8c75196aa958940.tar.gz
Shaarli-e0dd77c37e433d71462f87c8e8c75196aa958940.tar.zst
Shaarli-e0dd77c37e433d71462f87c8e8c75196aa958940.zip
Merge pull request #612 from virtualtam/refactor/bookmark-parser
Refactor bookmark import using a generic Netscape parser
Diffstat (limited to 'application/NetscapeBookmarkUtils.php')
-rw-r--r--application/NetscapeBookmarkUtils.php142
1 files changed, 142 insertions, 0 deletions
diff --git a/application/NetscapeBookmarkUtils.php b/application/NetscapeBookmarkUtils.php
index fdbb0ad7..b99a432e 100644
--- a/application/NetscapeBookmarkUtils.php
+++ b/application/NetscapeBookmarkUtils.php
@@ -51,4 +51,146 @@ class NetscapeBookmarkUtils
51 51
52 return $bookmarkLinks; 52 return $bookmarkLinks;
53 } 53 }
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
89 * @param array $file Server $_FILES parameters
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
101 // Sniff file type
102 if (! startsWith($data, '<!DOCTYPE NETSCAPE-Bookmark-file-1>')) {
103 return self::importStatus($filename, $filesize);
104 }
105
106 // Overwrite existing links?
107 $overwrite = ! empty($post['overwrite']);
108
109 // Add tags to all imported links?
110 if (empty($post['default_tags'])) {
111 $defaultTags = array();
112 } else {
113 $defaultTags = preg_split(
114 '/[\s,]+/',
115 escape($post['default_tags'])
116 );
117 }
118
119 // links are imported as public by default
120 $defaultPrivacy = 0;
121
122 $parser = new NetscapeBookmarkParser(
123 true, // nested tag support
124 $defaultTags, // additional user-specified tags
125 strval(1 - $defaultPrivacy) // defaultPub = 1 - defaultPrivacy
126 );
127 $bookmarks = $parser->parseString($data);
128
129 $importCount = 0;
130 $overwriteCount = 0;
131 $skipCount = 0;
132
133 foreach ($bookmarks as $bkm) {
134 $private = $defaultPrivacy;
135 if (empty($post['privacy']) || $post['privacy'] == 'default') {
136 // use value from the imported file
137 $private = $bkm['pub'] == '1' ? 0 : 1;
138 } else if ($post['privacy'] == 'private') {
139 // all imported links are private
140 $private = 1;
141 } else if ($post['privacy'] == 'public') {
142 // all imported links are public
143 $private = 0;
144 }
145
146 $newLink = array(
147 'title' => $bkm['title'],
148 'url' => $bkm['uri'],
149 'description' => $bkm['note'],
150 'private' => $private,
151 'linkdate'=> '',
152 'tags' => $bkm['tags']
153 );
154
155 $existingLink = $linkDb->getLinkFromUrl($bkm['uri']);
156
157 if ($existingLink !== false) {
158 if ($overwrite === false) {
159 // Do not overwrite an existing link
160 $skipCount++;
161 continue;
162 }
163
164 // Overwrite an existing link, keep its date
165 $newLink['linkdate'] = $existingLink['linkdate'];
166 $linkDb[$existingLink['linkdate']] = $newLink;
167 $importCount++;
168 $overwriteCount++;
169 continue;
170 }
171
172 // Add a new link
173 $newLinkDate = new DateTime('@'.strval($bkm['time']));
174 while (!empty($linkDb[$newLinkDate->format(LinkDB::LINK_DATE_FORMAT)])) {
175 // Ensure the date/time is not already used
176 // - this hack is necessary as the date/time acts as a primary key
177 // - apply 1 second increments until an unused index is found
178 // See https://github.com/shaarli/Shaarli/issues/351
179 $newLinkDate->add(new DateInterval('PT1S'));
180 }
181 $linkDbDate = $newLinkDate->format(LinkDB::LINK_DATE_FORMAT);
182 $newLink['linkdate'] = $linkDbDate;
183 $linkDb[$linkDbDate] = $newLink;
184 $importCount++;
185 }
186
187 $linkDb->savedb($pagecache);
188 return self::importStatus(
189 $filename,
190 $filesize,
191 $importCount,
192 $overwriteCount,
193 $skipCount
194 );
195 }
54} 196}