diff --git a/application/Url.php b/application/Url.php index af43b45..d80c9c5 100644 --- a/application/Url.php +++ b/application/Url.php @@ -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 * diff --git a/plugins/wallabag/README.md b/plugins/wallabag/README.md index 08e0d44..5bc35be 100644 --- a/plugins/wallabag/README.md +++ b/plugins/wallabag/README.md @@ -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 index 0000000..87352e6 --- /dev/null +++ b/plugins/wallabag/WallabagInstance.php @@ -0,0 +1,71 @@ + '1.x', + 2 => '2.x', + ); + + /** + * @var array Static reference to WB endpoint according to the API version. + * - key: version name. + * - value: endpoint. + */ + private static $wallabagEndpoints = array( + '1.x' => '?plainurl=', + '2.x' => 'bookmarklet?url=', + ); + + /** + * @var string Wallabag user instance URL. + */ + private $instanceUrl; + + /** + * @var string Wallabag user instance API version. + */ + private $apiVersion; + + function __construct($instance, $version) + { + if ($this->isVersionAllowed($version)) { + $this->apiVersion = self::$wallabagVersions[$version]; + } else { + // Default API version: 1.x. + $this->apiVersion = self::$wallabagVersions[1]; + } + + $this->instanceUrl = add_trailing_slash($instance); + } + + /** + * Build the Wallabag URL to reach from instance URL and API version endpoint. + * + * @return string wallabag url. + */ + public function getWallabagUrl() + { + return $this->instanceUrl . self::$wallabagEndpoints[$this->apiVersion]; + } + + /** + * Checks version configuration. + * + * @param mixed $version given version ID. + * + * @return bool true if it's valid, false otherwise. + */ + private function isVersionAllowed($version) + { + return isset(self::$wallabagVersions[$version]); + } +} diff --git a/plugins/wallabag/config.php.dist b/plugins/wallabag/config.php.dist index 7cf0d30..a602708 100644 --- a/plugins/wallabag/config.php.dist +++ b/plugins/wallabag/config.php.dist @@ -1,3 +1,4 @@ + diff --git a/plugins/wallabag/wallabag.php b/plugins/wallabag/wallabag.php index 37969c9..e3c399a 100644 --- a/plugins/wallabag/wallabag.php +++ b/plugins/wallabag/wallabag.php @@ -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; } + diff --git a/tests/Url/UrlTest.php b/tests/Url/UrlTest.php index e498d79..af6daaa 100644 --- a/tests/Url/UrlTest.php +++ b/tests/Url/UrlTest.php @@ -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)); + } } diff --git a/tests/plugins/PluginWallabagTest.php b/tests/plugins/PluginWallabagTest.php index 7cc83f4..5d3a60e 100644 --- a/tests/plugins/PluginWallabagTest.php +++ b/tests/plugins/PluginWallabagTest.php @@ -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 index 0000000..7c14c1d --- /dev/null +++ b/tests/plugins/WallabagInstanceTest.php @@ -0,0 +1,60 @@ +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); + } +}