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