]>
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'))) { |
cd5327be V |
35 | throw new Exception('Invalid export selection: "'.$selection.'"'); |
36 | } | |
37 | ||
38 | $bookmarkLinks = array(); | |
39 | ||
40 | foreach ($linkDb as $link) { | |
41 | if ($link['private'] != 0 && $selection == 'public') { | |
42 | continue; | |
43 | } | |
44 | if ($link['private'] == 0 && $selection == 'private') { | |
45 | continue; | |
46 | } | |
01878a75 | 47 | $date = $link['created']; |
cd5327be V |
48 | $link['timestamp'] = $date->getTimestamp(); |
49 | $link['taglist'] = str_replace(' ', ',', $link['tags']); | |
bb4a23aa V |
50 | |
51 | if (startsWith($link['url'], '?') && $prependNoteUrl) { | |
52 | $link['url'] = $indexUrl . $link['url']; | |
53 | } | |
54 | ||
cd5327be V |
55 | $bookmarkLinks[] = $link; |
56 | } | |
57 | ||
58 | return $bookmarkLinks; | |
59 | } | |
a973afea V |
60 | |
61 | /** | |
62 | * Generates an import status summary | |
63 | * | |
64 | * @param string $filename name of the file to import | |
65 | * @param int $filesize size of the file to import | |
66 | * @param int $importCount how many links were imported | |
67 | * @param int $overwriteCount how many links were overwritten | |
68 | * @param int $skipCount how many links were skipped | |
66e74d50 | 69 | * @param int $duration how many seconds did the import take |
a973afea V |
70 | * |
71 | * @return string Summary of the bookmark import status | |
72 | */ | |
73 | private static function importStatus( | |
74 | $filename, | |
75 | $filesize, | |
76 | $importCount=0, | |
77 | $overwriteCount=0, | |
66e74d50 A |
78 | $skipCount=0, |
79 | $duration=0 | |
a973afea V |
80 | ) |
81 | { | |
82 | $status = 'File '.$filename.' ('.$filesize.' bytes) '; | |
83 | if ($importCount == 0 && $overwriteCount == 0 && $skipCount == 0) { | |
84 | $status .= 'has an unknown file format. Nothing was imported.'; | |
85 | } else { | |
66e74d50 A |
86 | $status .= 'was successfully processed in '. $duration .' seconds: '; |
87 | $status .= $importCount.' links imported, '; | |
a973afea V |
88 | $status .= $overwriteCount.' links overwritten, '; |
89 | $status .= $skipCount.' links skipped.'; | |
90 | } | |
91 | return $status; | |
92 | } | |
93 | ||
94 | /** | |
95 | * Imports Web bookmarks from an uploaded Netscape bookmark dump | |
96 | * | |
48417aed A |
97 | * @param array $post Server $_POST parameters |
98 | * @param array $files Server $_FILES parameters | |
99 | * @param LinkDB $linkDb Loaded LinkDB instance | |
100 | * @param ConfigManager $conf instance | |
4306b184 | 101 | * @param History $history History instance |
a973afea V |
102 | * |
103 | * @return string Summary of the bookmark import status | |
104 | */ | |
4306b184 | 105 | public static function import($post, $files, $linkDb, $conf, $history) |
a973afea | 106 | { |
66e74d50 | 107 | $start = time(); |
a973afea V |
108 | $filename = $files['filetoupload']['name']; |
109 | $filesize = $files['filetoupload']['size']; | |
110 | $data = file_get_contents($files['filetoupload']['tmp_name']); | |
111 | ||
f4ad7bde | 112 | if (strpos($data, '<!DOCTYPE NETSCAPE-Bookmark-file-1>') === false) { |
a973afea V |
113 | return self::importStatus($filename, $filesize); |
114 | } | |
115 | ||
116 | // Overwrite existing links? | |
117 | $overwrite = ! empty($post['overwrite']); | |
118 | ||
119 | // Add tags to all imported links? | |
120 | if (empty($post['default_tags'])) { | |
121 | $defaultTags = array(); | |
122 | } else { | |
123 | $defaultTags = preg_split( | |
124 | '/[\s,]+/', | |
125 | escape($post['default_tags']) | |
126 | ); | |
127 | } | |
128 | ||
129 | // links are imported as public by default | |
130 | $defaultPrivacy = 0; | |
131 | ||
132 | $parser = new NetscapeBookmarkParser( | |
48417aed A |
133 | true, // nested tag support |
134 | $defaultTags, // additional user-specified tags | |
135 | strval(1 - $defaultPrivacy), // defaultPub = 1 - defaultPrivacy | |
136 | $conf->get('resource.data_dir') // log path, will be overridden | |
137 | ); | |
138 | $logger = new Logger( | |
139 | $conf->get('resource.data_dir'), | |
140 | ! $conf->get('dev.debug') ? LogLevel::INFO : LogLevel::DEBUG, | |
141 | [ | |
142 | 'prefix' => 'import.', | |
143 | 'extension' => 'log', | |
144 | ] | |
a973afea | 145 | ); |
48417aed | 146 | $parser->setLogger($logger); |
a973afea V |
147 | $bookmarks = $parser->parseString($data); |
148 | ||
149 | $importCount = 0; | |
150 | $overwriteCount = 0; | |
151 | $skipCount = 0; | |
152 | ||
153 | foreach ($bookmarks as $bkm) { | |
154 | $private = $defaultPrivacy; | |
155 | if (empty($post['privacy']) || $post['privacy'] == 'default') { | |
156 | // use value from the imported file | |
157 | $private = $bkm['pub'] == '1' ? 0 : 1; | |
158 | } else if ($post['privacy'] == 'private') { | |
159 | // all imported links are private | |
160 | $private = 1; | |
161 | } else if ($post['privacy'] == 'public') { | |
162 | // all imported links are public | |
163 | $private = 0; | |
164 | } | |
165 | ||
166 | $newLink = array( | |
167 | 'title' => $bkm['title'], | |
168 | 'url' => $bkm['uri'], | |
169 | 'description' => $bkm['note'], | |
170 | 'private' => $private, | |
a973afea V |
171 | 'tags' => $bkm['tags'] |
172 | ); | |
173 | ||
174 | $existingLink = $linkDb->getLinkFromUrl($bkm['uri']); | |
175 | ||
176 | if ($existingLink !== false) { | |
177 | if ($overwrite === false) { | |
178 | // Do not overwrite an existing link | |
179 | $skipCount++; | |
180 | continue; | |
181 | } | |
182 | ||
183 | // Overwrite an existing link, keep its date | |
01878a75 A |
184 | $newLink['id'] = $existingLink['id']; |
185 | $newLink['created'] = $existingLink['created']; | |
186 | $newLink['updated'] = new DateTime(); | |
28794b69 | 187 | $newLink['shorturl'] = $existingLink['shorturl']; |
01878a75 | 188 | $linkDb[$existingLink['id']] = $newLink; |
a973afea V |
189 | $importCount++; |
190 | $overwriteCount++; | |
191 | continue; | |
192 | } | |
193 | ||
01878a75 | 194 | // Add a new link - @ used for UNIX timestamps |
a973afea | 195 | $newLinkDate = new DateTime('@'.strval($bkm['time'])); |
01878a75 A |
196 | $newLinkDate->setTimezone(new DateTimeZone(date_default_timezone_get())); |
197 | $newLink['created'] = $newLinkDate; | |
198 | $newLink['id'] = $linkDb->getNextId(); | |
d592daea | 199 | $newLink['shorturl'] = link_small_hash($newLink['created'], $newLink['id']); |
01878a75 | 200 | $linkDb[$newLink['id']] = $newLink; |
a973afea V |
201 | $importCount++; |
202 | } | |
203 | ||
48417aed | 204 | $linkDb->save($conf->get('resource.page_cache')); |
66e74d50 A |
205 | $history->importLinks(); |
206 | ||
207 | $duration = time() - $start; | |
a973afea V |
208 | return self::importStatus( |
209 | $filename, | |
210 | $filesize, | |
211 | $importCount, | |
212 | $overwriteCount, | |
66e74d50 A |
213 | $skipCount, |
214 | $duration | |
a973afea V |
215 | ); |
216 | } | |
cd5327be | 217 | } |