diff options
Diffstat (limited to 'application/NetscapeBookmarkUtils.php')
-rw-r--r-- | application/NetscapeBookmarkUtils.php | 61 |
1 files changed, 43 insertions, 18 deletions
diff --git a/application/NetscapeBookmarkUtils.php b/application/NetscapeBookmarkUtils.php index e7148d00..dd7057f8 100644 --- a/application/NetscapeBookmarkUtils.php +++ b/application/NetscapeBookmarkUtils.php | |||
@@ -1,7 +1,13 @@ | |||
1 | <?php | 1 | <?php |
2 | 2 | ||
3 | use Psr\Log\LogLevel; | ||
4 | use Shaarli\Config\ConfigManager; | ||
5 | use Shaarli\NetscapeBookmarkParser\NetscapeBookmarkParser; | ||
6 | use Katzgrau\KLogger\Logger; | ||
7 | |||
3 | /** | 8 | /** |
4 | * Utilities to import and export bookmarks using the Netscape format | 9 | * Utilities to import and export bookmarks using the Netscape format |
10 | * TODO: Not static, use a container. | ||
5 | */ | 11 | */ |
6 | class NetscapeBookmarkUtils | 12 | class NetscapeBookmarkUtils |
7 | { | 13 | { |
@@ -26,11 +32,10 @@ class NetscapeBookmarkUtils | |||
26 | { | 32 | { |
27 | // see tpl/export.html for possible values | 33 | // see tpl/export.html for possible values |
28 | if (! in_array($selection, array('all', 'public', 'private'))) { | 34 | if (! in_array($selection, array('all', 'public', 'private'))) { |
29 | throw new Exception('Invalid export selection: "'.$selection.'"'); | 35 | throw new Exception(t('Invalid export selection:') .' "'.$selection.'"'); |
30 | } | 36 | } |
31 | 37 | ||
32 | $bookmarkLinks = array(); | 38 | $bookmarkLinks = array(); |
33 | |||
34 | foreach ($linkDb as $link) { | 39 | foreach ($linkDb as $link) { |
35 | if ($link['private'] != 0 && $selection == 'public') { | 40 | if ($link['private'] != 0 && $selection == 'public') { |
36 | continue; | 41 | continue; |
@@ -60,6 +65,7 @@ class NetscapeBookmarkUtils | |||
60 | * @param int $importCount how many links were imported | 65 | * @param int $importCount how many links were imported |
61 | * @param int $overwriteCount how many links were overwritten | 66 | * @param int $overwriteCount how many links were overwritten |
62 | * @param int $skipCount how many links were skipped | 67 | * @param int $skipCount how many links were skipped |
68 | * @param int $duration how many seconds did the import take | ||
63 | * | 69 | * |
64 | * @return string Summary of the bookmark import status | 70 | * @return string Summary of the bookmark import status |
65 | */ | 71 | */ |
@@ -68,16 +74,18 @@ class NetscapeBookmarkUtils | |||
68 | $filesize, | 74 | $filesize, |
69 | $importCount=0, | 75 | $importCount=0, |
70 | $overwriteCount=0, | 76 | $overwriteCount=0, |
71 | $skipCount=0 | 77 | $skipCount=0, |
78 | $duration=0 | ||
72 | ) | 79 | ) |
73 | { | 80 | { |
74 | $status = 'File '.$filename.' ('.$filesize.' bytes) '; | 81 | $status = sprintf(t('File %s (%d bytes) '), $filename, $filesize); |
75 | if ($importCount == 0 && $overwriteCount == 0 && $skipCount == 0) { | 82 | if ($importCount == 0 && $overwriteCount == 0 && $skipCount == 0) { |
76 | $status .= 'has an unknown file format. Nothing was imported.'; | 83 | $status .= t('has an unknown file format. Nothing was imported.'); |
77 | } else { | 84 | } else { |
78 | $status .= 'was successfully processed: '.$importCount.' links imported, '; | 85 | $status .= vsprintf( |
79 | $status .= $overwriteCount.' links overwritten, '; | 86 | t('was successfully processed in %d seconds: %d links imported, %d links overwritten, %d links skipped.'), |
80 | $status .= $skipCount.' links skipped.'; | 87 | [$duration, $importCount, $overwriteCount, $skipCount] |
88 | ); | ||
81 | } | 89 | } |
82 | return $status; | 90 | return $status; |
83 | } | 91 | } |
@@ -85,15 +93,17 @@ class NetscapeBookmarkUtils | |||
85 | /** | 93 | /** |
86 | * Imports Web bookmarks from an uploaded Netscape bookmark dump | 94 | * Imports Web bookmarks from an uploaded Netscape bookmark dump |
87 | * | 95 | * |
88 | * @param array $post Server $_POST parameters | 96 | * @param array $post Server $_POST parameters |
89 | * @param array $files Server $_FILES parameters | 97 | * @param array $files Server $_FILES parameters |
90 | * @param LinkDB $linkDb Loaded LinkDB instance | 98 | * @param LinkDB $linkDb Loaded LinkDB instance |
91 | * @param string $pagecache Page cache | 99 | * @param ConfigManager $conf instance |
100 | * @param History $history History instance | ||
92 | * | 101 | * |
93 | * @return string Summary of the bookmark import status | 102 | * @return string Summary of the bookmark import status |
94 | */ | 103 | */ |
95 | public static function import($post, $files, $linkDb, $pagecache) | 104 | public static function import($post, $files, $linkDb, $conf, $history) |
96 | { | 105 | { |
106 | $start = time(); | ||
97 | $filename = $files['filetoupload']['name']; | 107 | $filename = $files['filetoupload']['name']; |
98 | $filesize = $files['filetoupload']['size']; | 108 | $filesize = $files['filetoupload']['size']; |
99 | $data = file_get_contents($files['filetoupload']['tmp_name']); | 109 | $data = file_get_contents($files['filetoupload']['tmp_name']); |
@@ -119,10 +129,20 @@ class NetscapeBookmarkUtils | |||
119 | $defaultPrivacy = 0; | 129 | $defaultPrivacy = 0; |
120 | 130 | ||
121 | $parser = new NetscapeBookmarkParser( | 131 | $parser = new NetscapeBookmarkParser( |
122 | true, // nested tag support | 132 | true, // nested tag support |
123 | $defaultTags, // additional user-specified tags | 133 | $defaultTags, // additional user-specified tags |
124 | strval(1 - $defaultPrivacy) // defaultPub = 1 - defaultPrivacy | 134 | strval(1 - $defaultPrivacy), // defaultPub = 1 - defaultPrivacy |
135 | $conf->get('resource.data_dir') // log path, will be overridden | ||
125 | ); | 136 | ); |
137 | $logger = new Logger( | ||
138 | $conf->get('resource.data_dir'), | ||
139 | ! $conf->get('dev.debug') ? LogLevel::INFO : LogLevel::DEBUG, | ||
140 | [ | ||
141 | 'prefix' => 'import.', | ||
142 | 'extension' => 'log', | ||
143 | ] | ||
144 | ); | ||
145 | $parser->setLogger($logger); | ||
126 | $bookmarks = $parser->parseString($data); | 146 | $bookmarks = $parser->parseString($data); |
127 | 147 | ||
128 | $importCount = 0; | 148 | $importCount = 0; |
@@ -163,6 +183,7 @@ class NetscapeBookmarkUtils | |||
163 | $newLink['id'] = $existingLink['id']; | 183 | $newLink['id'] = $existingLink['id']; |
164 | $newLink['created'] = $existingLink['created']; | 184 | $newLink['created'] = $existingLink['created']; |
165 | $newLink['updated'] = new DateTime(); | 185 | $newLink['updated'] = new DateTime(); |
186 | $newLink['shorturl'] = $existingLink['shorturl']; | ||
166 | $linkDb[$existingLink['id']] = $newLink; | 187 | $linkDb[$existingLink['id']] = $newLink; |
167 | $importCount++; | 188 | $importCount++; |
168 | $overwriteCount++; | 189 | $overwriteCount++; |
@@ -179,13 +200,17 @@ class NetscapeBookmarkUtils | |||
179 | $importCount++; | 200 | $importCount++; |
180 | } | 201 | } |
181 | 202 | ||
182 | $linkDb->save($pagecache); | 203 | $linkDb->save($conf->get('resource.page_cache')); |
204 | $history->importLinks(); | ||
205 | |||
206 | $duration = time() - $start; | ||
183 | return self::importStatus( | 207 | return self::importStatus( |
184 | $filename, | 208 | $filename, |
185 | $filesize, | 209 | $filesize, |
186 | $importCount, | 210 | $importCount, |
187 | $overwriteCount, | 211 | $overwriteCount, |
188 | $skipCount | 212 | $skipCount, |
213 | $duration | ||
189 | ); | 214 | ); |
190 | } | 215 | } |
191 | } | 216 | } |