2016-04-10 17:34:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utilities to import and export bookmarks using the Netscape format
|
|
|
|
*/
|
|
|
|
class NetscapeBookmarkUtils
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters links and adds Netscape-formatted fields
|
|
|
|
*
|
|
|
|
* Added fields:
|
|
|
|
* - timestamp link addition date, using the Unix epoch format
|
|
|
|
* - taglist comma-separated tag list
|
|
|
|
*
|
2016-05-05 19:22:06 +02:00
|
|
|
* @param LinkDB $linkDb Link datastore
|
|
|
|
* @param string $selection Which links to export: (all|private|public)
|
|
|
|
* @param bool $prependNoteUrl Prepend note permalinks with the server's URL
|
|
|
|
* @param string $indexUrl Absolute URL of the Shaarli index page
|
2016-04-10 17:34:07 +02:00
|
|
|
*
|
|
|
|
* @throws Exception Invalid export selection
|
|
|
|
*
|
|
|
|
* @return array The links to be exported, with additional fields
|
|
|
|
*/
|
2016-05-05 19:22:06 +02:00
|
|
|
public static function filterAndFormat($linkDb, $selection, $prependNoteUrl, $indexUrl)
|
2016-04-10 17:34:07 +02:00
|
|
|
{
|
|
|
|
// see tpl/export.html for possible values
|
2016-05-05 19:22:06 +02:00
|
|
|
if (! in_array($selection, array('all', 'public', 'private'))) {
|
2016-04-10 17:34:07 +02:00
|
|
|
throw new Exception('Invalid export selection: "'.$selection.'"');
|
|
|
|
}
|
|
|
|
|
|
|
|
$bookmarkLinks = array();
|
|
|
|
|
|
|
|
foreach ($linkDb as $link) {
|
|
|
|
if ($link['private'] != 0 && $selection == 'public') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($link['private'] == 0 && $selection == 'private') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
|
|
|
|
$link['timestamp'] = $date->getTimestamp();
|
|
|
|
$link['taglist'] = str_replace(' ', ',', $link['tags']);
|
2016-05-05 19:22:06 +02:00
|
|
|
|
|
|
|
if (startsWith($link['url'], '?') && $prependNoteUrl) {
|
|
|
|
$link['url'] = $indexUrl . $link['url'];
|
|
|
|
}
|
|
|
|
|
2016-04-10 17:34:07 +02:00
|
|
|
$bookmarkLinks[] = $link;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $bookmarkLinks;
|
|
|
|
}
|
|
|
|
}
|