]>
Commit | Line | Data |
---|---|---|
cd5327be V |
1 | <?php |
2 | ||
48417aed | 3 | use Psr\Log\LogLevel; |
87e9631e | 4 | use Shaarli\Config\ConfigManager; |
48417aed A |
5 | use Shaarli\NetscapeBookmarkParser\NetscapeBookmarkParser; |
6 | use Katzgrau\KLogger\Logger; | |
7 | ||
cd5327be V |
8 | /** |
9 | * Utilities to import and export bookmarks using the Netscape format | |
48417aed | 10 | * TODO: Not static, use a container. |
cd5327be V |
11 | */ |
12 | class NetscapeBookmarkUtils | |
13 | { | |
14 | ||
15 | /** | |
16 | * Filters links and adds Netscape-formatted fields | |
17 | * | |
18 | * Added fields: | |
19 | * - timestamp link addition date, using the Unix epoch format | |
20 | * - taglist comma-separated tag list | |
21 | * | |
bb4a23aa V |
22 | * @param LinkDB $linkDb Link datastore |
23 | * @param string $selection Which links to export: (all|private|public) | |
24 | * @param bool $prependNoteUrl Prepend note permalinks with the server's URL | |
25 | * @param string $indexUrl Absolute URL of the Shaarli index page | |
cd5327be V |
26 | * |
27 | * @throws Exception Invalid export selection | |
28 | * | |
29 | * @return array The links to be exported, with additional fields | |
30 | */ | |
bb4a23aa | 31 | public static function filterAndFormat($linkDb, $selection, $prependNoteUrl, $indexUrl) |
cd5327be V |
32 | { |
33 | // see tpl/export.html for possible values | |
bb4a23aa | 34 | if (! in_array($selection, array('all', 'public', 'private'))) { |
12266213 | 35 | throw new Exception(t('Invalid export selection:') .' "'.$selection.'"'); |
cd5327be V |
36 | } |
37 | ||
38 | $bookmarkLinks = array(); | |
cd5327be V |
39 | foreach ($linkDb as $link) { |
40 | if ($link['private'] != 0 && $selection == 'public') { | |
41 | continue; | |
42 | } | |
43 | if ($link['private'] == 0 && $selection == 'private') { | |
44 | continue; | |
45 | } | |
01878a75 | 46 | $date = $link['created']; |
cd5327be V |
47 | $link['timestamp'] = $date->getTimestamp(); |
48 | $link['taglist'] = str_replace(' ', ',', $link['tags']); | |
bb4a23aa V |
49 | |
50 | if (startsWith($link['url'], '?') && $prependNoteUrl) { | |
51 | $link['url'] = $indexUrl . $link['url']; | |
52 | } | |
53 | ||
cd5327be V |
54 | $bookmarkLinks[] = $link; |
55 | } | |
56 | ||
57 | return $bookmarkLinks; | |
58 | } | |
a973afea V |
59 | |
60 | /** | |
61 | * Generates an import status summary | |
62 | * | |
63 | * @param string $filename name of the file to import | |
64 | * @param int $filesize size of the file to import | |
65 | * @param int $importCount how many links were imported | |
66 | * @param int $overwriteCount how many links were overwritten | |
67 | * @param int $skipCount how many links were skipped | |
66e74d50 | 68 | * @param int $duration how many seconds did the import take |
a973afea V |
69 | * |
70 | * @return string Summary of the bookmark import status | |
71 | */ | |
72 | private static function importStatus( | |
73 | $filename, | |
74 | $filesize, | |
f211e417 V |
75 | $importCount = 0, |
76 | $overwriteCount = 0, | |
77 | $skipCount = 0, | |
78 | $duration = 0 | |
79 | ) { | |
12266213 | 80 | $status = sprintf(t('File %s (%d bytes) '), $filename, $filesize); |
a973afea | 81 | if ($importCount == 0 && $overwriteCount == 0 && $skipCount == 0) { |
12266213 | 82 | $status .= t('has an unknown file format. Nothing was imported.'); |
a973afea | 83 | } else { |
12266213 A |
84 | $status .= vsprintf( |
85 | t('was successfully processed in %d seconds: %d links imported, %d links overwritten, %d links skipped.'), | |
86 | [$duration, $importCount, $overwriteCount, $skipCount] | |
87 | ); | |
a973afea V |
88 | } |
89 | return $status; | |
90 | } | |
91 | ||
92 | /** | |
93 | * Imports Web bookmarks from an uploaded Netscape bookmark dump | |
94 | * | |
48417aed A |
95 | * @param array $post Server $_POST parameters |
96 | * @param array $files Server $_FILES parameters | |
97 | * @param LinkDB $linkDb Loaded LinkDB instance | |
98 | * @param ConfigManager $conf instance | |
4306b184 | 99 | * @param History $history History instance |
a973afea V |
100 | * |
101 | * @return string Summary of the bookmark import status | |
102 | */ | |
4306b184 | 103 | public static function import($post, $files, $linkDb, $conf, $history) |
a973afea | 104 | { |
66e74d50 | 105 | $start = time(); |
a973afea V |
106 | $filename = $files['filetoupload']['name']; |
107 | $filesize = $files['filetoupload']['size']; | |
108 | $data = file_get_contents($files['filetoupload']['tmp_name']); | |
109 | ||
3ff1ce47 | 110 | if (preg_match('/<!DOCTYPE NETSCAPE-Bookmark-file-1>/i', $data) === 0) { |
a973afea V |
111 | return self::importStatus($filename, $filesize); |
112 | } | |
113 | ||
114 | // Overwrite existing links? | |
115 | $overwrite = ! empty($post['overwrite']); | |
116 | ||
117 | // Add tags to all imported links? | |
118 | if (empty($post['default_tags'])) { | |
119 | $defaultTags = array(); | |
120 | } else { | |
121 | $defaultTags = preg_split( | |
122 | '/[\s,]+/', | |
123 | escape($post['default_tags']) | |
124 | ); | |
125 | } | |
126 | ||
127 | // links are imported as public by default | |
128 | $defaultPrivacy = 0; | |
129 | ||
130 | $parser = new NetscapeBookmarkParser( | |
48417aed A |
131 | true, // nested tag support |
132 | $defaultTags, // additional user-specified tags | |
133 | strval(1 - $defaultPrivacy), // defaultPub = 1 - defaultPrivacy | |
134 | $conf->get('resource.data_dir') // log path, will be overridden | |
135 | ); | |
136 | $logger = new Logger( | |
137 | $conf->get('resource.data_dir'), | |
138 | ! $conf->get('dev.debug') ? LogLevel::INFO : LogLevel::DEBUG, | |
139 | [ | |
140 | 'prefix' => 'import.', | |
141 | 'extension' => 'log', | |
142 | ] | |
a973afea | 143 | ); |
48417aed | 144 | $parser->setLogger($logger); |
a973afea V |
145 | $bookmarks = $parser->parseString($data); |
146 | ||
147 | $importCount = 0; | |
148 | $overwriteCount = 0; | |
149 | $skipCount = 0; | |
150 | ||
151 | foreach ($bookmarks as $bkm) { | |
152 | $private = $defaultPrivacy; | |
153 | if (empty($post['privacy']) || $post['privacy'] == 'default') { | |
154 | // use value from the imported file | |
155 | $private = $bkm['pub'] == '1' ? 0 : 1; | |
d2d4f993 | 156 | } elseif ($post['privacy'] == 'private') { |
a973afea V |
157 | // all imported links are private |
158 | $private = 1; | |
d2d4f993 | 159 | } elseif ($post['privacy'] == 'public') { |
a973afea V |
160 | // all imported links are public |
161 | $private = 0; | |
3ff1ce47 | 162 | } |
a973afea V |
163 | |
164 | $newLink = array( | |
165 | 'title' => $bkm['title'], | |
166 | 'url' => $bkm['uri'], | |
167 | 'description' => $bkm['note'], | |
168 | 'private' => $private, | |
a973afea V |
169 | 'tags' => $bkm['tags'] |
170 | ); | |
171 | ||
172 | $existingLink = $linkDb->getLinkFromUrl($bkm['uri']); | |
173 | ||
174 | if ($existingLink !== false) { | |
175 | if ($overwrite === false) { | |
176 | // Do not overwrite an existing link | |
177 | $skipCount++; | |
178 | continue; | |
179 | } | |
180 | ||
181 | // Overwrite an existing link, keep its date | |
01878a75 A |
182 | $newLink['id'] = $existingLink['id']; |
183 | $newLink['created'] = $existingLink['created']; | |
184 | $newLink['updated'] = new DateTime(); | |
28794b69 | 185 | $newLink['shorturl'] = $existingLink['shorturl']; |
01878a75 | 186 | $linkDb[$existingLink['id']] = $newLink; |
a973afea V |
187 | $importCount++; |
188 | $overwriteCount++; | |
189 | continue; | |
190 | } | |
191 | ||
01878a75 | 192 | // Add a new link - @ used for UNIX timestamps |
a973afea | 193 | $newLinkDate = new DateTime('@'.strval($bkm['time'])); |
01878a75 A |
194 | $newLinkDate->setTimezone(new DateTimeZone(date_default_timezone_get())); |
195 | $newLink['created'] = $newLinkDate; | |
196 | $newLink['id'] = $linkDb->getNextId(); | |
d592daea | 197 | $newLink['shorturl'] = link_small_hash($newLink['created'], $newLink['id']); |
01878a75 | 198 | $linkDb[$newLink['id']] = $newLink; |
a973afea V |
199 | $importCount++; |
200 | } | |
201 | ||
48417aed | 202 | $linkDb->save($conf->get('resource.page_cache')); |
66e74d50 A |
203 | $history->importLinks(); |
204 | ||
205 | $duration = time() - $start; | |
a973afea V |
206 | return self::importStatus( |
207 | $filename, | |
208 | $filesize, | |
209 | $importCount, | |
210 | $overwriteCount, | |
66e74d50 A |
211 | $skipCount, |
212 | $duration | |
a973afea V |
213 | ); |
214 | } | |
cd5327be | 215 | } |