From: VirtualTam Date: Thu, 5 May 2016 17:22:06 +0000 (+0200) Subject: Export: allow prepending notes with the Shaarli instance's URL X-Git-Tag: v0.7.0~7^2 X-Git-Url: https://git.immae.eu/?p=github%2Fshaarli%2FShaarli.git;a=commitdiff_plain;h=bb4a23aa863b63e6148a085c15dedd7c960b4206 Export: allow prepending notes with the Shaarli instance's URL Relates to #102 Additions: - application: - export: allow prepending note permalinks with the instance's URL - test coverage Modifications: - export template: switch to an HTML form - link selection (all/private/public) - prepend note permalinks with the instance's URL Signed-off-by: VirtualTam --- diff --git a/application/NetscapeBookmarkUtils.php b/application/NetscapeBookmarkUtils.php index 8a296705..fdbb0ad7 100644 --- a/application/NetscapeBookmarkUtils.php +++ b/application/NetscapeBookmarkUtils.php @@ -13,17 +13,19 @@ class NetscapeBookmarkUtils * - timestamp link addition date, using the Unix epoch format * - taglist comma-separated tag list * - * @param LinkDB $linkDb The link datastore - * @param string $selection Which links to export: (all|private|public) + * @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 * * @throws Exception Invalid export selection * * @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 - if (! in_array($selection, array('all','public','private'))) { + if (! in_array($selection, array('all', 'public', 'private'))) { throw new Exception('Invalid export selection: "'.$selection.'"'); } @@ -39,6 +41,11 @@ class NetscapeBookmarkUtils $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); $link['timestamp'] = $date->getTimestamp(); $link['taglist'] = str_replace(' ', ',', $link['tags']); + + if (startsWith($link['url'], '?') && $prependNoteUrl) { + $link['url'] = $indexUrl . $link['url']; + } + $bookmarkLinks[] = $link; } diff --git a/index.php b/index.php index 47bae6e8..408aeae9 100644 --- a/index.php +++ b/index.php @@ -1590,8 +1590,9 @@ function renderPage() exit; } - // -------- Export as Netscape Bookmarks HTML file. if ($targetPage == Router::$PAGE_EXPORT) { + // Export links as a Netscape Bookmarks file + if (empty($_GET['selection'])) { $PAGE->assign('linkcount',count($LINKSDB)); $PAGE->renderPage('export'); @@ -1600,10 +1601,21 @@ function renderPage() // export as bookmarks_(all|private|public)_YYYYmmdd_HHMMSS.html $selection = $_GET['selection']; + if (isset($_GET['prepend_note_url'])) { + $prependNoteUrl = $_GET['prepend_note_url']; + } else { + $prependNoteUrl = false; + } + try { $PAGE->assign( 'links', - NetscapeBookmarkUtils::filterAndFormat($LINKSDB, $selection) + NetscapeBookmarkUtils::filterAndFormat( + $LINKSDB, + $selection, + $prependNoteUrl, + index_url($_SERVER) + ) ); } catch (Exception $exc) { header('Content-Type: text/plain; charset=utf-8'); diff --git a/tests/NetscapeBookmarkUtilsTest.php b/tests/NetscapeBookmarkUtilsTest.php index b7472d92..41e6d84c 100644 --- a/tests/NetscapeBookmarkUtilsTest.php +++ b/tests/NetscapeBookmarkUtilsTest.php @@ -39,7 +39,7 @@ class NetscapeBookmarkUtilsTest extends PHPUnit_Framework_TestCase */ public function testFilterAndFormatInvalid() { - NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'derp'); + NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'derp', false, ''); } /** @@ -47,7 +47,7 @@ class NetscapeBookmarkUtilsTest extends PHPUnit_Framework_TestCase */ public function testFilterAndFormatAll() { - $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'all'); + $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'all', false, ''); $this->assertEquals(self::$refDb->countLinks(), sizeof($links)); foreach ($links as $link) { $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); @@ -67,7 +67,7 @@ class NetscapeBookmarkUtilsTest extends PHPUnit_Framework_TestCase */ public function testFilterAndFormatPrivate() { - $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'private'); + $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'private', false, ''); $this->assertEquals(self::$refDb->countPrivateLinks(), sizeof($links)); foreach ($links as $link) { $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); @@ -87,7 +87,7 @@ class NetscapeBookmarkUtilsTest extends PHPUnit_Framework_TestCase */ public function testFilterAndFormatPublic() { - $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'public'); + $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'public', false, ''); $this->assertEquals(self::$refDb->countPublicLinks(), sizeof($links)); foreach ($links as $link) { $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); @@ -101,4 +101,34 @@ class NetscapeBookmarkUtilsTest extends PHPUnit_Framework_TestCase ); } } + + /** + * 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'] + ); + } } diff --git a/tpl/export.html b/tpl/export.html index 9582627a..67c3d05f 100644 --- a/tpl/export.html +++ b/tpl/export.html @@ -5,15 +5,21 @@