* Fixes a bug where URL weren't properly encoded.
* Adds Wallabag V2 support.
* Adds a URL function to handle trailing slash.
* UT.
* README updated.
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
*
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:
└── 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
--- /dev/null
+<?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
<?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
-<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>
* 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';
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;
}
+
$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));
+ }
}
// 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']));
}
}
+
--- /dev/null
+<?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);
+ }
+}