diff options
author | VirtualTam <virtualtam@flibidi.net> | 2016-05-05 19:22:06 +0200 |
---|---|---|
committer | VirtualTam <virtualtam@flibidi.net> | 2016-05-06 16:12:46 +0200 |
commit | bb4a23aa863b63e6148a085c15dedd7c960b4206 (patch) | |
tree | d4a484ca0eef729f5c02d95e0b20cc40e120853a | |
parent | 6275a65969a84a360588e0a62b025963e9ec98e0 (diff) | |
download | Shaarli-bb4a23aa863b63e6148a085c15dedd7c960b4206.tar.gz Shaarli-bb4a23aa863b63e6148a085c15dedd7c960b4206.tar.zst Shaarli-bb4a23aa863b63e6148a085c15dedd7c960b4206.zip |
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 <virtualtam@flibidi.net>
-rw-r--r-- | application/NetscapeBookmarkUtils.php | 15 | ||||
-rw-r--r-- | index.php | 16 | ||||
-rw-r--r-- | tests/NetscapeBookmarkUtilsTest.php | 38 | ||||
-rw-r--r-- | tpl/export.html | 24 |
4 files changed, 74 insertions, 19 deletions
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 | |||
13 | * - timestamp link addition date, using the Unix epoch format | 13 | * - timestamp link addition date, using the Unix epoch format |
14 | * - taglist comma-separated tag list | 14 | * - taglist comma-separated tag list |
15 | * | 15 | * |
16 | * @param LinkDB $linkDb The link datastore | 16 | * @param LinkDB $linkDb Link datastore |
17 | * @param string $selection Which links to export: (all|private|public) | 17 | * @param string $selection Which links to export: (all|private|public) |
18 | * @param bool $prependNoteUrl Prepend note permalinks with the server's URL | ||
19 | * @param string $indexUrl Absolute URL of the Shaarli index page | ||
18 | * | 20 | * |
19 | * @throws Exception Invalid export selection | 21 | * @throws Exception Invalid export selection |
20 | * | 22 | * |
21 | * @return array The links to be exported, with additional fields | 23 | * @return array The links to be exported, with additional fields |
22 | */ | 24 | */ |
23 | public static function filterAndFormat($linkDb, $selection) | 25 | public static function filterAndFormat($linkDb, $selection, $prependNoteUrl, $indexUrl) |
24 | { | 26 | { |
25 | // see tpl/export.html for possible values | 27 | // see tpl/export.html for possible values |
26 | if (! in_array($selection, array('all','public','private'))) { | 28 | if (! in_array($selection, array('all', 'public', 'private'))) { |
27 | throw new Exception('Invalid export selection: "'.$selection.'"'); | 29 | throw new Exception('Invalid export selection: "'.$selection.'"'); |
28 | } | 30 | } |
29 | 31 | ||
@@ -39,6 +41,11 @@ class NetscapeBookmarkUtils | |||
39 | $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); | 41 | $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); |
40 | $link['timestamp'] = $date->getTimestamp(); | 42 | $link['timestamp'] = $date->getTimestamp(); |
41 | $link['taglist'] = str_replace(' ', ',', $link['tags']); | 43 | $link['taglist'] = str_replace(' ', ',', $link['tags']); |
44 | |||
45 | if (startsWith($link['url'], '?') && $prependNoteUrl) { | ||
46 | $link['url'] = $indexUrl . $link['url']; | ||
47 | } | ||
48 | |||
42 | $bookmarkLinks[] = $link; | 49 | $bookmarkLinks[] = $link; |
43 | } | 50 | } |
44 | 51 | ||
@@ -1590,8 +1590,9 @@ function renderPage() | |||
1590 | exit; | 1590 | exit; |
1591 | } | 1591 | } |
1592 | 1592 | ||
1593 | // -------- Export as Netscape Bookmarks HTML file. | ||
1594 | if ($targetPage == Router::$PAGE_EXPORT) { | 1593 | if ($targetPage == Router::$PAGE_EXPORT) { |
1594 | // Export links as a Netscape Bookmarks file | ||
1595 | |||
1595 | if (empty($_GET['selection'])) { | 1596 | if (empty($_GET['selection'])) { |
1596 | $PAGE->assign('linkcount',count($LINKSDB)); | 1597 | $PAGE->assign('linkcount',count($LINKSDB)); |
1597 | $PAGE->renderPage('export'); | 1598 | $PAGE->renderPage('export'); |
@@ -1600,10 +1601,21 @@ function renderPage() | |||
1600 | 1601 | ||
1601 | // export as bookmarks_(all|private|public)_YYYYmmdd_HHMMSS.html | 1602 | // export as bookmarks_(all|private|public)_YYYYmmdd_HHMMSS.html |
1602 | $selection = $_GET['selection']; | 1603 | $selection = $_GET['selection']; |
1604 | if (isset($_GET['prepend_note_url'])) { | ||
1605 | $prependNoteUrl = $_GET['prepend_note_url']; | ||
1606 | } else { | ||
1607 | $prependNoteUrl = false; | ||
1608 | } | ||
1609 | |||
1603 | try { | 1610 | try { |
1604 | $PAGE->assign( | 1611 | $PAGE->assign( |
1605 | 'links', | 1612 | 'links', |
1606 | NetscapeBookmarkUtils::filterAndFormat($LINKSDB, $selection) | 1613 | NetscapeBookmarkUtils::filterAndFormat( |
1614 | $LINKSDB, | ||
1615 | $selection, | ||
1616 | $prependNoteUrl, | ||
1617 | index_url($_SERVER) | ||
1618 | ) | ||
1607 | ); | 1619 | ); |
1608 | } catch (Exception $exc) { | 1620 | } catch (Exception $exc) { |
1609 | header('Content-Type: text/plain; charset=utf-8'); | 1621 | 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 | |||
39 | */ | 39 | */ |
40 | public function testFilterAndFormatInvalid() | 40 | public function testFilterAndFormatInvalid() |
41 | { | 41 | { |
42 | NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'derp'); | 42 | NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'derp', false, ''); |
43 | } | 43 | } |
44 | 44 | ||
45 | /** | 45 | /** |
@@ -47,7 +47,7 @@ class NetscapeBookmarkUtilsTest extends PHPUnit_Framework_TestCase | |||
47 | */ | 47 | */ |
48 | public function testFilterAndFormatAll() | 48 | public function testFilterAndFormatAll() |
49 | { | 49 | { |
50 | $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'all'); | 50 | $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'all', false, ''); |
51 | $this->assertEquals(self::$refDb->countLinks(), sizeof($links)); | 51 | $this->assertEquals(self::$refDb->countLinks(), sizeof($links)); |
52 | foreach ($links as $link) { | 52 | foreach ($links as $link) { |
53 | $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); | 53 | $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); |
@@ -67,7 +67,7 @@ class NetscapeBookmarkUtilsTest extends PHPUnit_Framework_TestCase | |||
67 | */ | 67 | */ |
68 | public function testFilterAndFormatPrivate() | 68 | public function testFilterAndFormatPrivate() |
69 | { | 69 | { |
70 | $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'private'); | 70 | $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'private', false, ''); |
71 | $this->assertEquals(self::$refDb->countPrivateLinks(), sizeof($links)); | 71 | $this->assertEquals(self::$refDb->countPrivateLinks(), sizeof($links)); |
72 | foreach ($links as $link) { | 72 | foreach ($links as $link) { |
73 | $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); | 73 | $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); |
@@ -87,7 +87,7 @@ class NetscapeBookmarkUtilsTest extends PHPUnit_Framework_TestCase | |||
87 | */ | 87 | */ |
88 | public function testFilterAndFormatPublic() | 88 | public function testFilterAndFormatPublic() |
89 | { | 89 | { |
90 | $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'public'); | 90 | $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'public', false, ''); |
91 | $this->assertEquals(self::$refDb->countPublicLinks(), sizeof($links)); | 91 | $this->assertEquals(self::$refDb->countPublicLinks(), sizeof($links)); |
92 | foreach ($links as $link) { | 92 | foreach ($links as $link) { |
93 | $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); | 93 | $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); |
@@ -101,4 +101,34 @@ class NetscapeBookmarkUtilsTest extends PHPUnit_Framework_TestCase | |||
101 | ); | 101 | ); |
102 | } | 102 | } |
103 | } | 103 | } |
104 | |||
105 | /** | ||
106 | * Do not prepend notes with the Shaarli index's URL | ||
107 | */ | ||
108 | public function testFilterAndFormatDoNotPrependNoteUrl() | ||
109 | { | ||
110 | $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'public', false, ''); | ||
111 | $this->assertEquals( | ||
112 | '?WDWyig', | ||
113 | $links[0]['url'] | ||
114 | ); | ||
115 | } | ||
116 | |||
117 | /** | ||
118 | * Prepend notes with the Shaarli index's URL | ||
119 | */ | ||
120 | public function testFilterAndFormatPrependNoteUrl() | ||
121 | { | ||
122 | $indexUrl = 'http://localhost:7469/shaarli/'; | ||
123 | $links = NetscapeBookmarkUtils::filterAndFormat( | ||
124 | self::$linkDb, | ||
125 | 'public', | ||
126 | true, | ||
127 | $indexUrl | ||
128 | ); | ||
129 | $this->assertEquals( | ||
130 | $indexUrl . '?WDWyig', | ||
131 | $links[0]['url'] | ||
132 | ); | ||
133 | } | ||
104 | } | 134 | } |
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 @@ | |||
5 | <div id="pageheader"> | 5 | <div id="pageheader"> |
6 | {include="page.header"} | 6 | {include="page.header"} |
7 | <div id="toolsdiv"> | 7 | <div id="toolsdiv"> |
8 | <a href="?do=export&selection=all"> | 8 | <form method="GET"> |
9 | <b>Export all</b><span>: Export all links</span> | 9 | <input type="hidden" name="do" value="export"> |
10 | </a><br> | 10 | Selection:<br> |
11 | <a href="?do=export&selection=public"> | 11 | <input type="radio" name="selection" value="all" checked="true"> All<br> |
12 | <b>Export public</b><span>: Only export public links</span> | 12 | <input type="radio" name="selection" value="private"> Private<br> |
13 | </a><br> | 13 | <input type="radio" name="selection" value="public"> Public<br> |
14 | <a href="?do=export&selection=private"> | 14 | <br> |
15 | <b>Export private</b><span>: Only export private links</span> | 15 | <input type="checkbox" name="prepend_note_url" id="prepend_note_url"> |
16 | </a> | 16 | <label for="prepend_note_url"> |
17 | Prepend note permalinks with this Shaarli instance's URL | ||
18 | <em>(useful to import bookmarks in a web browser)</em> | ||
19 | </label> | ||
20 | <br><br> | ||
21 | <input class="bigbutton" type="submit" value="Export"> | ||
22 | </form> | ||
17 | <div class="clear"></div> | 23 | <div class="clear"></div> |
18 | </div> | 24 | </div> |
19 | </div> | 25 | </div> |