diff options
author | ArthurHoaro <arthur@hoa.ro> | 2015-12-22 10:20:27 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2015-12-27 15:30:34 +0100 |
commit | 938d9cce77ed5098dd69643795cb4014f3688b35 (patch) | |
tree | b0bbdd80b81e6f54009f0c67631e69020e2f4399 | |
parent | 79851b489087f8a3027ecd805255cd13ee6fcf63 (diff) | |
download | Shaarli-938d9cce77ed5098dd69643795cb4014f3688b35.tar.gz Shaarli-938d9cce77ed5098dd69643795cb4014f3688b35.tar.zst Shaarli-938d9cce77ed5098dd69643795cb4014f3688b35.zip |
Wallabag plugin improvement
* Fixes a bug where URL weren't properly encoded.
* Adds Wallabag V2 support.
* Adds a URL function to handle trailing slash.
* UT.
* README updated.
-rw-r--r-- | application/Url.php | 12 | ||||
-rw-r--r-- | plugins/wallabag/README.md | 25 | ||||
-rw-r--r-- | plugins/wallabag/WallabagInstance.php | 71 | ||||
-rw-r--r-- | plugins/wallabag/config.php.dist | 3 | ||||
-rw-r--r-- | plugins/wallabag/wallabag.html | 2 | ||||
-rw-r--r-- | plugins/wallabag/wallabag.php | 17 | ||||
-rw-r--r-- | tests/Url/UrlTest.php | 11 | ||||
-rw-r--r-- | tests/plugins/PluginWallabagTest.php | 4 | ||||
-rw-r--r-- | tests/plugins/WallabagInstanceTest.php | 60 |
9 files changed, 194 insertions, 11 deletions
diff --git a/application/Url.php b/application/Url.php index af43b457..d80c9c58 100644 --- a/application/Url.php +++ b/application/Url.php | |||
@@ -52,6 +52,18 @@ function get_url_scheme($url) | |||
52 | } | 52 | } |
53 | 53 | ||
54 | /** | 54 | /** |
55 | * Adds a trailing slash at the end of URL if necessary. | ||
56 | * | ||
57 | * @param string $url URL to check/edit. | ||
58 | * | ||
59 | * @return string $url URL with a end trailing slash. | ||
60 | */ | ||
61 | function add_trailing_slash($url) | ||
62 | { | ||
63 | return $url . (!endsWith($url, '/') ? '/' : ''); | ||
64 | } | ||
65 | |||
66 | /** | ||
55 | * URL representation and cleanup utilities | 67 | * URL representation and cleanup utilities |
56 | * | 68 | * |
57 | * Form | 69 | * Form |
diff --git a/plugins/wallabag/README.md b/plugins/wallabag/README.md index 08e0d44a..5bc35be1 100644 --- a/plugins/wallabag/README.md +++ b/plugins/wallabag/README.md | |||
@@ -2,7 +2,8 @@ | |||
2 | 2 | ||
3 | For each link in your Shaarli, adds a button to save the target page in your [wallabag](https://www.wallabag.org/). | 3 | For each link in your Shaarli, adds a button to save the target page in your [wallabag](https://www.wallabag.org/). |
4 | 4 | ||
5 | ### Installation/configuration | 5 | ### Installation |
6 | |||
6 | Clone this repository inside your `tpl/plugins/` directory, or download the archive and unpack it there. | 7 | Clone this repository inside your `tpl/plugins/` directory, or download the archive and unpack it there. |
7 | The directory structure should look like: | 8 | The directory structure should look like: |
8 | 9 | ||
@@ -11,19 +12,31 @@ The directory structure should look like: | |||
11 | └── plugins | 12 | └── plugins |
12 | └── wallabag | 13 | └── wallabag |
13 | ├── README.md | 14 | ├── README.md |
15 | ├── config.php.dist | ||
14 | ├── wallabag.html | 16 | ├── wallabag.html |
17 | ├── wallabag.php | ||
15 | └── wallabag.png | 18 | └── wallabag.png |
16 | ``` | 19 | ``` |
17 | 20 | ||
18 | To enable the plugin, add `'wallabag'` to your list of enabled plugins in `data/options.php` (`PLUGINS` array) | 21 | To enable the plugin, add `'wallabag'` to your list of enabled plugins in `data/options.php` (`PLUGINS` array). |
19 | . This should look like: | 22 | This should look like: |
20 | 23 | ||
21 | ``` | 24 | ``` |
22 | $GLOBALS['config']['PLUGINS'] = array('qrcode', 'any_other_plugin', 'wallabag') | 25 | $GLOBALS['config']['PLUGINS'] = array('qrcode', 'any_other_plugin', 'wallabag') |
23 | ``` | 26 | ``` |
24 | 27 | ||
25 | Then, set the `WALLABAG_URL` variable in `data/options.php` pointing to your wallabag URL. Example: | 28 | ### Configuration |
29 | |||
30 | Copy `config.php.dist` into `config.php` and setup your instance. | ||
26 | 31 | ||
32 | *Wallabag instance URL* | ||
27 | ``` | 33 | ``` |
28 | $GLOBALS['config']['WALLABAG_URL'] = 'http://demo.wallabag.org' ; //Base URL of your wallabag installation | 34 | $GLOBALS['config']['WALLABAG_URL'] = 'http://v2.wallabag.org' ; |
29 | ``` \ No newline at end of file | 35 | ``` |
36 | |||
37 | *Wallabag version*: either `1` (for 1.x) or `2` (for 2.x) | ||
38 | ``` | ||
39 | $GLOBALS['config']['WALLABAG_VERSION'] = 2; | ||
40 | ``` | ||
41 | |||
42 | > Note: these settings can also be set in `data/config.php`. \ No newline at end of file | ||
diff --git a/plugins/wallabag/WallabagInstance.php b/plugins/wallabag/WallabagInstance.php new file mode 100644 index 00000000..87352e66 --- /dev/null +++ b/plugins/wallabag/WallabagInstance.php | |||
@@ -0,0 +1,71 @@ | |||
1 | <?php | ||
2 | |||
3 | /** | ||
4 | * Class WallabagInstance. | ||
5 | */ | ||
6 | class WallabagInstance | ||
7 | { | ||
8 | /** | ||
9 | * @var array Static reference to differrent WB API versions. | ||
10 | * - key: version ID, must match plugin settings. | ||
11 | * - value: version name. | ||
12 | */ | ||
13 | private static $wallabagVersions = array( | ||
14 | 1 => '1.x', | ||
15 | 2 => '2.x', | ||
16 | ); | ||
17 | |||
18 | /** | ||
19 | * @var array Static reference to WB endpoint according to the API version. | ||
20 | * - key: version name. | ||
21 | * - value: endpoint. | ||
22 | */ | ||
23 | private static $wallabagEndpoints = array( | ||
24 | '1.x' => '?plainurl=', | ||
25 | '2.x' => 'bookmarklet?url=', | ||
26 | ); | ||
27 | |||
28 | /** | ||
29 | * @var string Wallabag user instance URL. | ||
30 | */ | ||
31 | private $instanceUrl; | ||
32 | |||
33 | /** | ||
34 | * @var string Wallabag user instance API version. | ||
35 | */ | ||
36 | private $apiVersion; | ||
37 | |||
38 | function __construct($instance, $version) | ||
39 | { | ||
40 | if ($this->isVersionAllowed($version)) { | ||
41 | $this->apiVersion = self::$wallabagVersions[$version]; | ||
42 | } else { | ||
43 | // Default API version: 1.x. | ||
44 | $this->apiVersion = self::$wallabagVersions[1]; | ||
45 | } | ||
46 | |||
47 | $this->instanceUrl = add_trailing_slash($instance); | ||
48 | } | ||
49 | |||
50 | /** | ||
51 | * Build the Wallabag URL to reach from instance URL and API version endpoint. | ||
52 | * | ||
53 | * @return string wallabag url. | ||
54 | */ | ||
55 | public function getWallabagUrl() | ||
56 | { | ||
57 | return $this->instanceUrl . self::$wallabagEndpoints[$this->apiVersion]; | ||
58 | } | ||
59 | |||
60 | /** | ||
61 | * Checks version configuration. | ||
62 | * | ||
63 | * @param mixed $version given version ID. | ||
64 | * | ||
65 | * @return bool true if it's valid, false otherwise. | ||
66 | */ | ||
67 | private function isVersionAllowed($version) | ||
68 | { | ||
69 | return isset(self::$wallabagVersions[$version]); | ||
70 | } | ||
71 | } | ||
diff --git a/plugins/wallabag/config.php.dist b/plugins/wallabag/config.php.dist index 7cf0d303..a602708f 100644 --- a/plugins/wallabag/config.php.dist +++ b/plugins/wallabag/config.php.dist | |||
@@ -1,3 +1,4 @@ | |||
1 | <?php | 1 | <?php |
2 | 2 | ||
3 | $GLOBALS['plugins']['WALLABAG_URL'] = 'https://demo.wallabag.org/'; \ No newline at end of file | 3 | $GLOBALS['plugins']['WALLABAG_URL'] = 'https://demo.wallabag.org'; |
4 | $GLOBALS['plugins']['WALLABAG_VERSION'] = 1; \ No newline at end of file | ||
diff --git a/plugins/wallabag/wallabag.html b/plugins/wallabag/wallabag.html index ddcf8126..d0382adc 100644 --- a/plugins/wallabag/wallabag.html +++ b/plugins/wallabag/wallabag.html | |||
@@ -1 +1 @@ | |||
<span><a href="%s/?plainurl=%s" target="_blank"><img width="13" height="13" src="%s/wallabag/wallabag.png" title="Save to wallabag" /></a></span> | <span><a href="%s%s" target="_blank"><img width="13" height="13" src="%s/wallabag/wallabag.png" title="Save to wallabag" /></a></span> | ||
diff --git a/plugins/wallabag/wallabag.php b/plugins/wallabag/wallabag.php index 37969c97..e3c399a9 100644 --- a/plugins/wallabag/wallabag.php +++ b/plugins/wallabag/wallabag.php | |||
@@ -4,6 +4,8 @@ | |||
4 | * Plugin Wallabag. | 4 | * Plugin Wallabag. |
5 | */ | 5 | */ |
6 | 6 | ||
7 | require_once 'WallabagInstance.php'; | ||
8 | |||
7 | // don't raise unnecessary warnings | 9 | // don't raise unnecessary warnings |
8 | if (is_file(PluginManager::$PLUGINS_PATH . '/wallabag/config.php')) { | 10 | if (is_file(PluginManager::$PLUGINS_PATH . '/wallabag/config.php')) { |
9 | include PluginManager::$PLUGINS_PATH . '/wallabag/config.php'; | 11 | include PluginManager::$PLUGINS_PATH . '/wallabag/config.php'; |
@@ -28,12 +30,23 @@ function hook_wallabag_render_linklist($data) | |||
28 | return $data; | 30 | return $data; |
29 | } | 31 | } |
30 | 32 | ||
31 | $wallabag_html = file_get_contents(PluginManager::$PLUGINS_PATH . '/wallabag/wallabag.html'); | 33 | $version = isset($GLOBALS['plugins']['WALLABAG_VERSION']) |
34 | ? $GLOBALS['plugins']['WALLABAG_VERSION'] | ||
35 | : ''; | ||
36 | $wallabagInstance = new WallabagInstance($GLOBALS['plugins']['WALLABAG_URL'], $version); | ||
37 | |||
38 | $wallabagHtml = file_get_contents(PluginManager::$PLUGINS_PATH . '/wallabag/wallabag.html'); | ||
32 | 39 | ||
33 | foreach ($data['links'] as &$value) { | 40 | foreach ($data['links'] as &$value) { |
34 | $wallabag = sprintf($wallabag_html, $GLOBALS['plugins']['WALLABAG_URL'], $value['url'], PluginManager::$PLUGINS_PATH); | 41 | $wallabag = sprintf( |
42 | $wallabagHtml, | ||
43 | $wallabagInstance->getWallabagUrl(), | ||
44 | urlencode($value['url']), | ||
45 | PluginManager::$PLUGINS_PATH | ||
46 | ); | ||
35 | $value['link_plugin'][] = $wallabag; | 47 | $value['link_plugin'][] = $wallabag; |
36 | } | 48 | } |
37 | 49 | ||
38 | return $data; | 50 | return $data; |
39 | } | 51 | } |
52 | |||
diff --git a/tests/Url/UrlTest.php b/tests/Url/UrlTest.php index e498d79e..af6daaa4 100644 --- a/tests/Url/UrlTest.php +++ b/tests/Url/UrlTest.php | |||
@@ -145,4 +145,15 @@ class UrlTest extends PHPUnit_Framework_TestCase | |||
145 | $url = new Url('git://domain.tld/push?pull=clone#checkout'); | 145 | $url = new Url('git://domain.tld/push?pull=clone#checkout'); |
146 | $this->assertEquals('git', $url->getScheme()); | 146 | $this->assertEquals('git', $url->getScheme()); |
147 | } | 147 | } |
148 | |||
149 | /** | ||
150 | * Test add trailing slash. | ||
151 | */ | ||
152 | function testAddTrailingSlash() | ||
153 | { | ||
154 | $strOn = 'http://randomstr.com/test/'; | ||
155 | $strOff = 'http://randomstr.com/test'; | ||
156 | $this->assertEquals($strOn, add_trailing_slash($strOn)); | ||
157 | $this->assertEquals($strOn, add_trailing_slash($strOff)); | ||
158 | } | ||
148 | } | 159 | } |
diff --git a/tests/plugins/PluginWallabagTest.php b/tests/plugins/PluginWallabagTest.php index 7cc83f4f..5d3a60e0 100644 --- a/tests/plugins/PluginWallabagTest.php +++ b/tests/plugins/PluginWallabagTest.php | |||
@@ -44,6 +44,8 @@ class PluginWallabagTest extends PHPUnit_Framework_TestCase | |||
44 | 44 | ||
45 | // plugin data | 45 | // plugin data |
46 | $this->assertEquals(1, count($link['link_plugin'])); | 46 | $this->assertEquals(1, count($link['link_plugin'])); |
47 | $this->assertNotFalse(strpos($link['link_plugin'][0], $str)); | 47 | $this->assertNotFalse(strpos($link['link_plugin'][0], urlencode($str))); |
48 | $this->assertNotFalse(strpos($link['link_plugin'][0], $GLOBALS['plugins']['WALLABAG_URL'])); | ||
48 | } | 49 | } |
49 | } | 50 | } |
51 | |||
diff --git a/tests/plugins/WallabagInstanceTest.php b/tests/plugins/WallabagInstanceTest.php new file mode 100644 index 00000000..7c14c1df --- /dev/null +++ b/tests/plugins/WallabagInstanceTest.php | |||
@@ -0,0 +1,60 @@ | |||
1 | <?php | ||
2 | |||
3 | require_once 'plugins/wallabag/WallabagInstance.php'; | ||
4 | |||
5 | /** | ||
6 | * Class WallabagInstanceTest | ||
7 | */ | ||
8 | class WallabagInstanceTest extends PHPUnit_Framework_TestCase | ||
9 | { | ||
10 | /** | ||
11 | * @var string wallabag url. | ||
12 | */ | ||
13 | private $instance; | ||
14 | |||
15 | /** | ||
16 | * Reset plugin path | ||
17 | */ | ||
18 | function setUp() | ||
19 | { | ||
20 | $this->instance = 'http://some.url'; | ||
21 | } | ||
22 | |||
23 | /** | ||
24 | * Test WallabagInstance with API V1. | ||
25 | */ | ||
26 | function testWallabagInstanceV1() | ||
27 | { | ||
28 | $instance = new WallabagInstance($this->instance, 1); | ||
29 | $expected = $this->instance . '/?plainurl='; | ||
30 | $result = $instance->getWallabagUrl(); | ||
31 | $this->assertEquals($expected, $result); | ||
32 | } | ||
33 | |||
34 | /** | ||
35 | * Test WallabagInstance with API V2. | ||
36 | */ | ||
37 | function testWallabagInstanceV2() | ||
38 | { | ||
39 | $instance = new WallabagInstance($this->instance, 2); | ||
40 | $expected = $this->instance . '/bookmarklet?url='; | ||
41 | $result = $instance->getWallabagUrl(); | ||
42 | $this->assertEquals($expected, $result); | ||
43 | } | ||
44 | |||
45 | /** | ||
46 | * Test WallabagInstance with an invalid API version. | ||
47 | */ | ||
48 | function testWallabagInstanceInvalidVersion() | ||
49 | { | ||
50 | $instance = new WallabagInstance($this->instance, false); | ||
51 | $expected = $this->instance . '/?plainurl='; | ||
52 | $result = $instance->getWallabagUrl(); | ||
53 | $this->assertEquals($expected, $result); | ||
54 | |||
55 | $instance = new WallabagInstance($this->instance, 3); | ||
56 | $expected = $this->instance . '/?plainurl='; | ||
57 | $result = $instance->getWallabagUrl(); | ||
58 | $this->assertEquals($expected, $result); | ||
59 | } | ||
60 | } | ||