From 938d9cce77ed5098dd69643795cb4014f3688b35 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Tue, 22 Dec 2015 10:20:27 +0100 Subject: 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. --- plugins/wallabag/README.md | 25 +++++++++--- plugins/wallabag/WallabagInstance.php | 71 +++++++++++++++++++++++++++++++++++ plugins/wallabag/config.php.dist | 3 +- plugins/wallabag/wallabag.html | 2 +- plugins/wallabag/wallabag.php | 17 ++++++++- 5 files changed, 108 insertions(+), 10 deletions(-) create mode 100644 plugins/wallabag/WallabagInstance.php (limited to 'plugins/wallabag') 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 @@ 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 00000000..87352e66 --- /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 7cf0d303..a602708f 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 37969c97..e3c399a9 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; } + -- cgit v1.2.3