]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Wallabag plugin improvement 417/head
authorArthurHoaro <arthur@hoa.ro>
Tue, 22 Dec 2015 09:20:27 +0000 (10:20 +0100)
committerArthurHoaro <arthur@hoa.ro>
Sun, 27 Dec 2015 14:30:34 +0000 (15:30 +0100)
  * Fixes a bug where URL weren't properly encoded.
  * Adds Wallabag V2 support.
  * Adds a URL function to handle trailing slash.
  * UT.
  * README updated.

application/Url.php
plugins/wallabag/README.md
plugins/wallabag/WallabagInstance.php [new file with mode: 0644]
plugins/wallabag/config.php.dist
plugins/wallabag/wallabag.html
plugins/wallabag/wallabag.php
tests/Url/UrlTest.php
tests/plugins/PluginWallabagTest.php
tests/plugins/WallabagInstanceTest.php [new file with mode: 0644]

index af43b457961729f3a5c2abc82a407b4a402531ec..d80c9c582f32e5e8b8053b291d354a112e21b6c0 100644 (file)
@@ -51,6 +51,18 @@ function get_url_scheme($url)
   return $obj_url->getScheme();
 }
 
+/**
+ * Adds a trailing slash at the end of URL if necessary.
+ *
+ * @param string $url URL to check/edit.
+ *
+ * @return string $url URL with a end trailing slash.
+ */
+function add_trailing_slash($url)
+{
+    return $url . (!endsWith($url, '/') ? '/' : '');
+}
+
 /**
  * URL representation and cleanup utilities
  *
index 08e0d44a0921e7af96b1dd3e3ee883812cd4f8bd..5bc35be11a847fa9cf93620f3446a517bc5d2aac 100644 (file)
@@ -2,7 +2,8 @@
 
 For each link in your Shaarli, adds a button to save the target page in your [wallabag](https://www.wallabag.org/).
 
-### Installation/configuration
+### Installation
+
 Clone this repository inside your `tpl/plugins/` directory, or download the archive and unpack it there.  
 The directory structure should look like:
 
@@ -11,19 +12,31 @@ The directory structure should look like:
     └── plugins
         └── wallabag
             ├── README.md
+            ├── config.php.dist
             ├── wallabag.html
+            ├── wallabag.php
             └── wallabag.png
 ```
 
-To enable the plugin, add `'wallabag'` to your list of enabled plugins in `data/options.php` (`PLUGINS` array)
-This should look like:
+To enable the plugin, add `'wallabag'` to your list of enabled plugins in `data/options.php` (`PLUGINS` array).
+This should look like:
 
 ```
 $GLOBALS['config']['PLUGINS'] = array('qrcode', 'any_other_plugin', 'wallabag')
 ```
 
-Then, set the `WALLABAG_URL` variable in `data/options.php` pointing to your wallabag URL. Example:
+### Configuration
+
+Copy `config.php.dist` into `config.php` and setup your instance.
 
+*Wallabag instance URL*
 ```
-$GLOBALS['config']['WALLABAG_URL'] = 'http://demo.wallabag.org' ; //Base URL of your wallabag installation
-```
\ No newline at end of file
+$GLOBALS['config']['WALLABAG_URL'] = 'http://v2.wallabag.org' ;
+```
+
+*Wallabag version*: either `1` (for 1.x) or `2` (for 2.x)
+```
+$GLOBALS['config']['WALLABAG_VERSION'] = 2;
+```
+
+> 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 (file)
index 0000000..87352e6
--- /dev/null
@@ -0,0 +1,71 @@
+<?php\r
+\r
+/**\r
+ * Class WallabagInstance.\r
+ */\r
+class WallabagInstance\r
+{\r
+    /**\r
+     * @var array Static reference to differrent WB API versions.\r
+     *          - key: version ID, must match plugin settings.\r
+     *          - value: version name.\r
+     */\r
+    private static $wallabagVersions = array(\r
+        1 => '1.x',\r
+        2 => '2.x',\r
+    );\r
+\r
+    /**\r
+     * @var array Static reference to WB endpoint according to the API version.\r
+     *          - key: version name.\r
+     *          - value: endpoint.\r
+     */\r
+    private static $wallabagEndpoints = array(\r
+        '1.x' => '?plainurl=',\r
+        '2.x' => 'bookmarklet?url=',\r
+    );\r
+\r
+    /**\r
+     * @var string Wallabag user instance URL.\r
+     */\r
+    private $instanceUrl;\r
+\r
+    /**\r
+     * @var string Wallabag user instance API version.\r
+     */\r
+    private $apiVersion;\r
+\r
+    function __construct($instance, $version)\r
+    {\r
+        if ($this->isVersionAllowed($version)) {\r
+            $this->apiVersion = self::$wallabagVersions[$version];\r
+        } else {\r
+            // Default API version: 1.x.\r
+            $this->apiVersion = self::$wallabagVersions[1];\r
+        }\r
+\r
+        $this->instanceUrl = add_trailing_slash($instance);\r
+    }\r
+\r
+    /**\r
+     * Build the Wallabag URL to reach from instance URL and API version endpoint.\r
+     *\r
+     * @return string wallabag url.\r
+     */\r
+    public function getWallabagUrl()\r
+    {\r
+        return $this->instanceUrl . self::$wallabagEndpoints[$this->apiVersion];\r
+    }\r
+\r
+    /**\r
+     * Checks version configuration.\r
+     *\r
+     * @param mixed $version given version ID.\r
+     *\r
+     * @return bool true if it's valid, false otherwise.\r
+     */\r
+    private function isVersionAllowed($version)\r
+    {\r
+        return isset(self::$wallabagVersions[$version]);\r
+    }\r
+}\r
index 7cf0d303516c075d4dfcaaeb54b433bc0ee890e8..a602708f0babeb8e76e3971a8347013f00fad64f 100644 (file)
@@ -1,3 +1,4 @@
 <?php
 
-$GLOBALS['plugins']['WALLABAG_URL'] = 'https://demo.wallabag.org/';
\ No newline at end of file
+$GLOBALS['plugins']['WALLABAG_URL'] = 'https://demo.wallabag.org';
+$GLOBALS['plugins']['WALLABAG_VERSION'] = 1;
\ No newline at end of file
index ddcf8126c56d4bf66d9dd1372eec9f78eefd01f5..d0382adc95b1fab36916b6fb516078a04eda3875 100644 (file)
@@ -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>
index 37969c976d780e71cb5b78c508d060d98476a356..e3c399a9ab5d85a124db5f99a1f0a135f8fbbbc8 100644 (file)
@@ -4,6 +4,8 @@
  * Plugin Wallabag.
  */
 
+require_once 'WallabagInstance.php';
+
 // don't raise unnecessary warnings
 if (is_file(PluginManager::$PLUGINS_PATH . '/wallabag/config.php')) {
     include PluginManager::$PLUGINS_PATH . '/wallabag/config.php';
@@ -28,12 +30,23 @@ function hook_wallabag_render_linklist($data)
         return $data;
     }
 
-    $wallabag_html = file_get_contents(PluginManager::$PLUGINS_PATH . '/wallabag/wallabag.html');
+    $version = isset($GLOBALS['plugins']['WALLABAG_VERSION'])
+        ? $GLOBALS['plugins']['WALLABAG_VERSION']
+        : '';
+    $wallabagInstance = new WallabagInstance($GLOBALS['plugins']['WALLABAG_URL'], $version);
+
+    $wallabagHtml = file_get_contents(PluginManager::$PLUGINS_PATH . '/wallabag/wallabag.html');
 
     foreach ($data['links'] as &$value) {
-        $wallabag = sprintf($wallabag_html, $GLOBALS['plugins']['WALLABAG_URL'], $value['url'], PluginManager::$PLUGINS_PATH);
+        $wallabag = sprintf(
+            $wallabagHtml,
+            $wallabagInstance->getWallabagUrl(),
+            urlencode($value['url']),
+            PluginManager::$PLUGINS_PATH
+        );
         $value['link_plugin'][] = $wallabag;
     }
 
     return $data;
 }
+
index e498d79e8ed8dbf9057ce88c04fd1e98143a5408..af6daaa4df844a46b2476f8eaa143b30ed3189c7 100644 (file)
@@ -145,4 +145,15 @@ class UrlTest extends PHPUnit_Framework_TestCase
         $url = new Url('git://domain.tld/push?pull=clone#checkout');
         $this->assertEquals('git', $url->getScheme());
     }
+
+    /**
+     * Test add trailing slash.
+     */
+    function testAddTrailingSlash()
+    {
+        $strOn = 'http://randomstr.com/test/';
+        $strOff = 'http://randomstr.com/test';
+        $this->assertEquals($strOn, add_trailing_slash($strOn));
+        $this->assertEquals($strOn, add_trailing_slash($strOff));
+    }
 }
index 7cc83f4f7fc2515e41e1a40bc253a2c91d04eb1e..5d3a60e02d109fec041f433070a38a8b2f6fede3 100644 (file)
@@ -44,6 +44,8 @@ class PluginWallabagTest extends PHPUnit_Framework_TestCase
 
         // plugin data
         $this->assertEquals(1, count($link['link_plugin']));
-        $this->assertNotFalse(strpos($link['link_plugin'][0], $str));
+        $this->assertNotFalse(strpos($link['link_plugin'][0], urlencode($str)));
+        $this->assertNotFalse(strpos($link['link_plugin'][0], $GLOBALS['plugins']['WALLABAG_URL']));
     }
 }
+
diff --git a/tests/plugins/WallabagInstanceTest.php b/tests/plugins/WallabagInstanceTest.php
new file mode 100644 (file)
index 0000000..7c14c1d
--- /dev/null
@@ -0,0 +1,60 @@
+<?php
+
+require_once 'plugins/wallabag/WallabagInstance.php';
+
+/**
+ * Class WallabagInstanceTest
+ */
+class WallabagInstanceTest extends PHPUnit_Framework_TestCase
+{
+    /**
+     * @var string wallabag url.
+     */
+    private $instance;
+
+    /**
+     * Reset plugin path
+     */
+    function setUp()
+    {
+        $this->instance = 'http://some.url';
+    }
+
+    /**
+     * Test WallabagInstance with API V1.
+     */
+    function testWallabagInstanceV1()
+    {
+        $instance = new WallabagInstance($this->instance, 1);
+        $expected = $this->instance . '/?plainurl=';
+        $result = $instance->getWallabagUrl();
+        $this->assertEquals($expected, $result);
+    }
+
+    /**
+     * Test WallabagInstance with API V2.
+     */
+    function testWallabagInstanceV2()
+    {
+        $instance = new WallabagInstance($this->instance, 2);
+        $expected = $this->instance . '/bookmarklet?url=';
+        $result = $instance->getWallabagUrl();
+        $this->assertEquals($expected, $result);
+    }
+
+    /**
+     * Test WallabagInstance with an invalid API version.
+     */
+    function testWallabagInstanceInvalidVersion()
+    {
+        $instance = new WallabagInstance($this->instance, false);
+        $expected = $this->instance . '/?plainurl=';
+        $result = $instance->getWallabagUrl();
+        $this->assertEquals($expected, $result);
+
+        $instance = new WallabagInstance($this->instance, 3);
+        $expected = $this->instance . '/?plainurl=';
+        $result = $instance->getWallabagUrl();
+        $this->assertEquals($expected, $result);
+    }
+}