Merge pull request #554 from virtualtam/fix/bookmark-export

Export: allow prepending notes with the Shaarli instance's URL
This commit is contained in:
VirtualTam 2016-05-06 21:50:55 +02:00
commit 00be05aaf5
4 changed files with 74 additions and 19 deletions

View file

@ -13,17 +13,19 @@ class NetscapeBookmarkUtils
* - timestamp link addition date, using the Unix epoch format * - timestamp link addition date, using the Unix epoch format
* - taglist comma-separated tag list * - taglist comma-separated tag list
* *
* @param LinkDB $linkDb The link datastore * @param LinkDB $linkDb Link datastore
* @param string $selection Which links to export: (all|private|public) * @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
* *
* @throws Exception Invalid export selection * @throws Exception Invalid export selection
* *
* @return array The links to be exported, with additional fields * @return array The links to be exported, with additional fields
*/ */
public static function filterAndFormat($linkDb, $selection) public static function filterAndFormat($linkDb, $selection, $prependNoteUrl, $indexUrl)
{ {
// see tpl/export.html for possible values // see tpl/export.html for possible values
if (! in_array($selection, array('all','public','private'))) { if (! in_array($selection, array('all', 'public', 'private'))) {
throw new Exception('Invalid export selection: "'.$selection.'"'); throw new Exception('Invalid export selection: "'.$selection.'"');
} }
@ -39,6 +41,11 @@ public static function filterAndFormat($linkDb, $selection)
$date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
$link['timestamp'] = $date->getTimestamp(); $link['timestamp'] = $date->getTimestamp();
$link['taglist'] = str_replace(' ', ',', $link['tags']); $link['taglist'] = str_replace(' ', ',', $link['tags']);
if (startsWith($link['url'], '?') && $prependNoteUrl) {
$link['url'] = $indexUrl . $link['url'];
}
$bookmarkLinks[] = $link; $bookmarkLinks[] = $link;
} }

View file

@ -1590,8 +1590,9 @@ function renderPage()
exit; exit;
} }
// -------- Export as Netscape Bookmarks HTML file.
if ($targetPage == Router::$PAGE_EXPORT) { if ($targetPage == Router::$PAGE_EXPORT) {
// Export links as a Netscape Bookmarks file
if (empty($_GET['selection'])) { if (empty($_GET['selection'])) {
$PAGE->assign('linkcount',count($LINKSDB)); $PAGE->assign('linkcount',count($LINKSDB));
$PAGE->renderPage('export'); $PAGE->renderPage('export');
@ -1600,10 +1601,21 @@ function renderPage()
// export as bookmarks_(all|private|public)_YYYYmmdd_HHMMSS.html // export as bookmarks_(all|private|public)_YYYYmmdd_HHMMSS.html
$selection = $_GET['selection']; $selection = $_GET['selection'];
if (isset($_GET['prepend_note_url'])) {
$prependNoteUrl = $_GET['prepend_note_url'];
} else {
$prependNoteUrl = false;
}
try { try {
$PAGE->assign( $PAGE->assign(
'links', 'links',
NetscapeBookmarkUtils::filterAndFormat($LINKSDB, $selection) NetscapeBookmarkUtils::filterAndFormat(
$LINKSDB,
$selection,
$prependNoteUrl,
index_url($_SERVER)
)
); );
} catch (Exception $exc) { } catch (Exception $exc) {
header('Content-Type: text/plain; charset=utf-8'); header('Content-Type: text/plain; charset=utf-8');

View file

@ -39,7 +39,7 @@ public static function setUpBeforeClass()
*/ */
public function testFilterAndFormatInvalid() public function testFilterAndFormatInvalid()
{ {
NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'derp'); NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'derp', false, '');
} }
/** /**
@ -47,7 +47,7 @@ public function testFilterAndFormatInvalid()
*/ */
public function testFilterAndFormatAll() public function testFilterAndFormatAll()
{ {
$links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'all'); $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'all', false, '');
$this->assertEquals(self::$refDb->countLinks(), sizeof($links)); $this->assertEquals(self::$refDb->countLinks(), sizeof($links));
foreach ($links as $link) { foreach ($links as $link) {
$date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
@ -67,7 +67,7 @@ public function testFilterAndFormatAll()
*/ */
public function testFilterAndFormatPrivate() public function testFilterAndFormatPrivate()
{ {
$links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'private'); $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'private', false, '');
$this->assertEquals(self::$refDb->countPrivateLinks(), sizeof($links)); $this->assertEquals(self::$refDb->countPrivateLinks(), sizeof($links));
foreach ($links as $link) { foreach ($links as $link) {
$date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
@ -87,7 +87,7 @@ public function testFilterAndFormatPrivate()
*/ */
public function testFilterAndFormatPublic() public function testFilterAndFormatPublic()
{ {
$links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'public'); $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'public', false, '');
$this->assertEquals(self::$refDb->countPublicLinks(), sizeof($links)); $this->assertEquals(self::$refDb->countPublicLinks(), sizeof($links));
foreach ($links as $link) { foreach ($links as $link) {
$date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
@ -101,4 +101,34 @@ public function testFilterAndFormatPublic()
); );
} }
} }
/**
* Do not prepend notes with the Shaarli index's URL
*/
public function testFilterAndFormatDoNotPrependNoteUrl()
{
$links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'public', false, '');
$this->assertEquals(
'?WDWyig',
$links[0]['url']
);
}
/**
* Prepend notes with the Shaarli index's URL
*/
public function testFilterAndFormatPrependNoteUrl()
{
$indexUrl = 'http://localhost:7469/shaarli/';
$links = NetscapeBookmarkUtils::filterAndFormat(
self::$linkDb,
'public',
true,
$indexUrl
);
$this->assertEquals(
$indexUrl . '?WDWyig',
$links[0]['url']
);
}
} }

View file

@ -5,15 +5,21 @@
<div id="pageheader"> <div id="pageheader">
{include="page.header"} {include="page.header"}
<div id="toolsdiv"> <div id="toolsdiv">
<a href="?do=export&amp;selection=all"> <form method="GET">
<b>Export all</b><span>: Export all links</span> <input type="hidden" name="do" value="export">
</a><br> Selection:<br>
<a href="?do=export&amp;selection=public"> <input type="radio" name="selection" value="all" checked="true"> All<br>
<b>Export public</b><span>: Only export public links</span> <input type="radio" name="selection" value="private"> Private<br>
</a><br> <input type="radio" name="selection" value="public"> Public<br>
<a href="?do=export&amp;selection=private"> <br>
<b>Export private</b><span>: Only export private links</span> <input type="checkbox" name="prepend_note_url" id="prepend_note_url">
</a> <label for="prepend_note_url">
Prepend note permalinks with this Shaarli instance's URL
<em>(useful to import bookmarks in a web browser)</em>
</label>
<br><br>
<input class="bigbutton" type="submit" value="Export">
</form>
<div class="clear"></div> <div class="clear"></div>
</div> </div>
</div> </div>