Merge pull request #417 from ArthurHoaro/wallabag-improve
Wallabag plugin improvement
This commit is contained in:
commit
defc8a3f03
9 changed files with 194 additions and 11 deletions
|
@ -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
|
||||
*
|
||||
|
|
|
@ -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
|
||||
```
|
||||
$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`.
|
71
plugins/wallabag/WallabagInstance.php
Normal file
71
plugins/wallabag/WallabagInstance.php
Normal file
|
@ -0,0 +1,71 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Class WallabagInstance.
|
||||
*/
|
||||
class WallabagInstance
|
||||
{
|
||||
/**
|
||||
* @var array Static reference to differrent WB API versions.
|
||||
* - key: version ID, must match plugin settings.
|
||||
* - value: version name.
|
||||
*/
|
||||
private static $wallabagVersions = array(
|
||||
1 => '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]);
|
||||
}
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
<?php
|
||||
|
||||
$GLOBALS['plugins']['WALLABAG_URL'] = 'https://demo.wallabag.org/';
|
||||
$GLOBALS['plugins']['WALLABAG_URL'] = 'https://demo.wallabag.org';
|
||||
$GLOBALS['plugins']['WALLABAG_VERSION'] = 1;
|
|
@ -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>
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -145,4 +145,15 @@ public function testDefaultScheme() {
|
|||
$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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,6 +44,8 @@ function testWallabagLinklist()
|
|||
|
||||
// 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']));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
60
tests/plugins/WallabagInstanceTest.php
Normal file
60
tests/plugins/WallabagInstanceTest.php
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue