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