diff --git a/application/NetscapeBookmarkUtils.php b/application/NetscapeBookmarkUtils.php new file mode 100644 index 0000000..8a29670 --- /dev/null +++ b/application/NetscapeBookmarkUtils.php @@ -0,0 +1,47 @@ +getTimestamp(); + $link['taglist'] = str_replace(' ', ',', $link['tags']); + $bookmarkLinks[] = $link; + } + + return $bookmarkLinks; + } +} diff --git a/index.php b/index.php index d3369a2..456d93c 100644 --- a/index.php +++ b/index.php @@ -161,6 +161,7 @@ require_once 'application/HttpUtils.php'; require_once 'application/LinkDB.php'; require_once 'application/LinkFilter.php'; require_once 'application/LinkUtils.php'; +require_once 'application/NetscapeBookmarkUtils.php'; require_once 'application/TimeZone.php'; require_once 'application/Url.php'; require_once 'application/Utils.php'; @@ -1584,44 +1585,36 @@ function renderPage() } // -------- Export as Netscape Bookmarks HTML file. - if ($targetPage == Router::$PAGE_EXPORT) - { - if (empty($_GET['what'])) - { + if ($targetPage == Router::$PAGE_EXPORT) { + if (empty($_GET['selection'])) { $PAGE->assign('linkcount',count($LINKSDB)); $PAGE->renderPage('export'); exit; } - $exportWhat=$_GET['what']; - if (!array_intersect(array('all','public','private'),array($exportWhat))) die('What are you trying to export???'); - header('Content-Type: text/html; charset=utf-8'); - header('Content-disposition: attachment; filename=bookmarks_'.$exportWhat.'_'.strval(date('Ymd_His')).'.html'); - $currentdate=date('Y/m/d H:i:s'); - echo << - - - -Bookmarks -

Bookmarks

-HTML; - foreach($LINKSDB as $link) - { - if ($exportWhat=='all' || - ($exportWhat=='private' && $link['private']!=0) || - ($exportWhat=='public' && $link['private']==0)) - { - $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); - echo '
'.$link['title']."\n"; - if ($link['description']!='') echo '
'.$link['description']."\n"; - } + // export as bookmarks_(all|private|public)_YYYYmmdd_HHMMSS.html + $selection = $_GET['selection']; + try { + $PAGE->assign( + 'links', + NetscapeBookmarkUtils::filterAndFormat($LINKSDB, $selection) + ); + } catch (Exception $exc) { + header('Content-Type: text/plain; charset=utf-8'); + echo $exc->getMessage(); + exit; } - exit; + $now = new DateTime(); + header('Content-Type: text/html; charset=utf-8'); + header( + 'Content-disposition: attachment; filename=bookmarks_' + .$selection.'_'.$now->format(LinkDB::LINK_DATE_FORMAT).'.html' + ); + $PAGE->assign('date', $now->format(DateTime::RFC822)); + $PAGE->assign('eol', PHP_EOL); + $PAGE->assign('selection', $selection); + $PAGE->renderPage('export.bookmarks'); + exit; } // -------- User is uploading a file for import diff --git a/tests/NetscapeBookmarkUtilsTest.php b/tests/NetscapeBookmarkUtilsTest.php new file mode 100644 index 0000000..b7472d9 --- /dev/null +++ b/tests/NetscapeBookmarkUtilsTest.php @@ -0,0 +1,104 @@ +write(self::$testDatastore); + self::$linkDb = new LinkDB(self::$testDatastore, true, false); + } + + /** + * Attempt to export an invalid link selection + * @expectedException Exception + * @expectedExceptionMessageRegExp /Invalid export selection/ + */ + public function testFilterAndFormatInvalid() + { + NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'derp'); + } + + /** + * Prepare all links for export + */ + public function testFilterAndFormatAll() + { + $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'all'); + $this->assertEquals(self::$refDb->countLinks(), sizeof($links)); + foreach ($links as $link) { + $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); + $this->assertEquals( + $date->getTimestamp(), + $link['timestamp'] + ); + $this->assertEquals( + str_replace(' ', ',', $link['tags']), + $link['taglist'] + ); + } + } + + /** + * Prepare private links for export + */ + public function testFilterAndFormatPrivate() + { + $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'private'); + $this->assertEquals(self::$refDb->countPrivateLinks(), sizeof($links)); + foreach ($links as $link) { + $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); + $this->assertEquals( + $date->getTimestamp(), + $link['timestamp'] + ); + $this->assertEquals( + str_replace(' ', ',', $link['tags']), + $link['taglist'] + ); + } + } + + /** + * Prepare public links for export + */ + public function testFilterAndFormatPublic() + { + $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'public'); + $this->assertEquals(self::$refDb->countPublicLinks(), sizeof($links)); + foreach ($links as $link) { + $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); + $this->assertEquals( + $date->getTimestamp(), + $link['timestamp'] + ); + $this->assertEquals( + str_replace(' ', ',', $link['tags']), + $link['taglist'] + ); + } + } +} diff --git a/tpl/export.bookmarks.html b/tpl/export.bookmarks.html new file mode 100644 index 0000000..da73325 --- /dev/null +++ b/tpl/export.bookmarks.html @@ -0,0 +1,10 @@ + + +{ignore}The RainTPL loop is formatted to avoid generating extra newlines{/ignore} +{$pagetitle} +

Shaarli export of {$selection} bookmarks on {$date}

+

{loop="links"} +

{$value.title}{if="$value.description"}{$eol}
{$value.description}{/if}{/loop} +

diff --git a/tpl/export.html b/tpl/export.html index 9d101db..9582627 100644 --- a/tpl/export.html +++ b/tpl/export.html @@ -2,15 +2,21 @@ {include="includes"} -

-{include="page.footer"} + + {include="page.footer"}