namespacing: \Shaarli\Updater

Signed-off-by: VirtualTam <virtualtam@flibidi.net>
This commit is contained in:
VirtualTam 2018-12-03 23:49:20 +01:00
parent 92c6439dbc
commit bcf056c9d9
9 changed files with 154 additions and 128 deletions

View file

@ -27,7 +27,7 @@ class ConfigPhp implements ConfigIO
/** /**
* Map legacy config keys with the new ones. * Map legacy config keys with the new ones.
* If ConfigPhp is used, getting <newkey> will actually look for <legacykey>. * If ConfigPhp is used, getting <newkey> will actually look for <legacykey>.
* The Updater will use this array to transform keys when switching to JSON. * The updater will use this array to transform keys when switching to JSON.
* *
* @var array current key => legacy key. * @var array current key => legacy key.
*/ */

View file

@ -1,15 +1,24 @@
<?php <?php
namespace Shaarli\Updater;
use ApplicationUtils;
use Exception;
use RainTPL;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
use Shaarli\Bookmark\LinkDB; use Shaarli\Bookmark\LinkDB;
use Shaarli\Bookmark\LinkFilter; use Shaarli\Bookmark\LinkFilter;
use Shaarli\Config\ConfigJson; use Shaarli\Config\ConfigJson;
use Shaarli\Config\ConfigPhp;
use Shaarli\Config\ConfigManager; use Shaarli\Config\ConfigManager;
use Shaarli\Config\ConfigPhp;
use Shaarli\Exceptions\IOException; use Shaarli\Exceptions\IOException;
use Shaarli\Thumbnailer; use Shaarli\Thumbnailer;
use Shaarli\Updater\Exception\UpdaterException;
/** /**
* Class Updater. * Class updater.
* Used to update stuff when a new Shaarli's version is reached. * Used to update stuff when a new Shaarli's version is reached.
* Update methods are ran only once, and the stored in a JSON file. * Update methods are ran only once, and the stored in a JSON file.
*/ */
@ -87,7 +96,7 @@ public function update()
} }
if ($this->methods === null) { if ($this->methods === null) {
throw new UpdaterException(t('Couldn\'t retrieve Updater class methods.')); throw new UpdaterException(t('Couldn\'t retrieve updater class methods.'));
} }
foreach ($this->methods as $method) { foreach ($this->methods as $method) {
@ -542,96 +551,3 @@ public function updateMethodSetSticky()
return true; return true;
} }
} }
/**
* Class UpdaterException.
*/
class UpdaterException extends Exception
{
/**
* @var string Method where the error occurred.
*/
protected $method;
/**
* @var Exception The parent exception.
*/
protected $previous;
/**
* Constructor.
*
* @param string $message Force the error message if set.
* @param string $method Method where the error occurred.
* @param Exception|bool $previous Parent exception.
*/
public function __construct($message = '', $method = '', $previous = false)
{
$this->method = $method;
$this->previous = $previous;
$this->message = $this->buildMessage($message);
}
/**
* Build the exception error message.
*
* @param string $message Optional given error message.
*
* @return string The built error message.
*/
private function buildMessage($message)
{
$out = '';
if (! empty($message)) {
$out .= $message . PHP_EOL;
}
if (! empty($this->method)) {
$out .= t('An error occurred while running the update ') . $this->method . PHP_EOL;
}
if (! empty($this->previous)) {
$out .= ' '. $this->previous->getMessage();
}
return $out;
}
}
/**
* Read the updates file, and return already done updates.
*
* @param string $updatesFilepath Updates file path.
*
* @return array Already done update methods.
*/
function read_updates_file($updatesFilepath)
{
if (! empty($updatesFilepath) && is_file($updatesFilepath)) {
$content = file_get_contents($updatesFilepath);
if (! empty($content)) {
return explode(';', $content);
}
}
return array();
}
/**
* Write updates file.
*
* @param string $updatesFilepath Updates file path.
* @param array $updates Updates array to write.
*
* @throws Exception Couldn't write version number.
*/
function write_updates_file($updatesFilepath, $updates)
{
if (empty($updatesFilepath)) {
throw new Exception(t('Updates file path is not set, can\'t write updates.'));
}
$res = file_put_contents($updatesFilepath, implode(';', $updates));
if ($res === false) {
throw new Exception(t('Unable to write updates in '. $updatesFilepath . '.'));
}
}

View file

@ -0,0 +1,39 @@
<?php
/**
* Read the updates file, and return already done updates.
*
* @param string $updatesFilepath Updates file path.
*
* @return array Already done update methods.
*/
function read_updates_file($updatesFilepath)
{
if (! empty($updatesFilepath) && is_file($updatesFilepath)) {
$content = file_get_contents($updatesFilepath);
if (! empty($content)) {
return explode(';', $content);
}
}
return array();
}
/**
* Write updates file.
*
* @param string $updatesFilepath Updates file path.
* @param array $updates Updates array to write.
*
* @throws Exception Couldn't write version number.
*/
function write_updates_file($updatesFilepath, $updates)
{
if (empty($updatesFilepath)) {
throw new Exception(t('Updates file path is not set, can\'t write updates.'));
}
$res = file_put_contents($updatesFilepath, implode(';', $updates));
if ($res === false) {
throw new Exception(t('Unable to write updates in '. $updatesFilepath . '.'));
}
}

View file

@ -0,0 +1,60 @@
<?php
namespace Shaarli\Updater\Exception;
use Exception;
/**
* Class UpdaterException.
*/
class UpdaterException extends Exception
{
/**
* @var string Method where the error occurred.
*/
protected $method;
/**
* @var Exception The parent exception.
*/
protected $previous;
/**
* Constructor.
*
* @param string $message Force the error message if set.
* @param string $method Method where the error occurred.
* @param Exception|bool $previous Parent exception.
*/
public function __construct($message = '', $method = '', $previous = false)
{
$this->method = $method;
$this->previous = $previous;
$this->message = $this->buildMessage($message);
}
/**
* Build the exception error message.
*
* @param string $message Optional given error message.
*
* @return string The built error message.
*/
private function buildMessage($message)
{
$out = '';
if (!empty($message)) {
$out .= $message . PHP_EOL;
}
if (!empty($this->method)) {
$out .= t('An error occurred while running the update ') . $this->method . PHP_EOL;
}
if (!empty($this->previous)) {
$out .= ' ' . $this->previous->getMessage();
}
return $out;
}
}

View file

@ -46,7 +46,9 @@
"Shaarli\\Feed\\": "application/feed", "Shaarli\\Feed\\": "application/feed",
"Shaarli\\Http\\": "application/http", "Shaarli\\Http\\": "application/http",
"Shaarli\\Render\\": "application/render", "Shaarli\\Render\\": "application/render",
"Shaarli\\Security\\": "application/security" "Shaarli\\Security\\": "application/security",
"Shaarli\\Updater\\": "application/updater",
"Shaarli\\Updater\\Exception\\": "application/updater/exception"
} }
} }
} }

View file

@ -62,6 +62,7 @@
require_once 'application/feed/Cache.php'; require_once 'application/feed/Cache.php';
require_once 'application/http/HttpUtils.php'; require_once 'application/http/HttpUtils.php';
require_once 'application/http/UrlUtils.php'; require_once 'application/http/UrlUtils.php';
require_once 'application/updater/UpdaterUtils.php';
require_once 'application/FileUtils.php'; require_once 'application/FileUtils.php';
require_once 'application/History.php'; require_once 'application/History.php';
require_once 'application/NetscapeBookmarkUtils.php'; require_once 'application/NetscapeBookmarkUtils.php';
@ -69,7 +70,6 @@
require_once 'application/Utils.php'; require_once 'application/Utils.php';
require_once 'application/PluginManager.php'; require_once 'application/PluginManager.php';
require_once 'application/Router.php'; require_once 'application/Router.php';
require_once 'application/Updater.php';
use \Shaarli\Bookmark\Exception\LinkNotFoundException; use \Shaarli\Bookmark\Exception\LinkNotFoundException;
use \Shaarli\Bookmark\LinkDB; use \Shaarli\Bookmark\LinkDB;
@ -83,6 +83,7 @@
use \Shaarli\Security\LoginManager; use \Shaarli\Security\LoginManager;
use \Shaarli\Security\SessionManager; use \Shaarli\Security\SessionManager;
use \Shaarli\Thumbnailer; use \Shaarli\Thumbnailer;
use Shaarli\Updater\Updater;
// Ensure the PHP version is supported // Ensure the PHP version is supported
try { try {

View file

@ -1,12 +1,15 @@
<?php <?php
namespace Shaarli\Updater;
use Exception;
use ReflectionClass;
use ReflectionMethod;
use Shaarli\Bookmark\LinkDB; use Shaarli\Bookmark\LinkDB;
use Shaarli\Config\ConfigManager;
require_once 'application/Updater.php';
/** /**
* Class DummyUpdater. * Class DummyUpdater.
* Extends Updater to add update method designed for unit tests. * Extends updater to add update method designed for unit tests.
*/ */
class DummyUpdater extends Updater class DummyUpdater extends Updater
{ {

View file

@ -1,19 +1,24 @@
<?php <?php
namespace Shaarli\Updater;
use DateTime;
use Exception;
use Shaarli\Bookmark\LinkDB; use Shaarli\Bookmark\LinkDB;
use Shaarli\Config\ConfigJson; use Shaarli\Config\ConfigJson;
use Shaarli\Config\ConfigManager; use Shaarli\Config\ConfigManager;
use Shaarli\Config\ConfigPhp; use Shaarli\Config\ConfigPhp;
use Shaarli\Thumbnailer; use Shaarli\Thumbnailer;
require_once 'tests/Updater/DummyUpdater.php'; require_once 'application/updater/UpdaterUtils.php';
require_once 'tests/updater/DummyUpdater.php';
require_once 'tests/utils/ReferenceLinkDB.php';
require_once 'inc/rain.tpl.class.php'; require_once 'inc/rain.tpl.class.php';
/** /**
* Class UpdaterTest. * Class UpdaterTest.
* Runs unit tests against the Updater class. * Runs unit tests against the updater class.
*/ */
class UpdaterTest extends PHPUnit_Framework_TestCase class UpdaterTest extends \PHPUnit\Framework\TestCase
{ {
/** /**
* @var string Path to test datastore. * @var string Path to test datastore.
@ -155,7 +160,7 @@ public function testOneUpdate()
/** /**
* Test Update failed. * Test Update failed.
* *
* @expectedException UpdaterException * @expectedException \Exception
*/ */
public function testUpdateFailed() public function testUpdateFailed()
{ {
@ -181,17 +186,17 @@ public function testUpdateMergeDeprecatedConfig()
$this->conf->setConfigFile('tests/utils/config/configPhp'); $this->conf->setConfigFile('tests/utils/config/configPhp');
$this->conf->reset(); $this->conf->reset();
$optionsFile = 'tests/Updater/options.php'; $optionsFile = 'tests/updater/options.php';
$options = '<?php $options = '<?php
$GLOBALS[\'privateLinkByDefault\'] = true;'; $GLOBALS[\'privateLinkByDefault\'] = true;';
file_put_contents($optionsFile, $options); file_put_contents($optionsFile, $options);
// tmp config file. // tmp config file.
$this->conf->setConfigFile('tests/Updater/config'); $this->conf->setConfigFile('tests/updater/config');
// merge configs // merge configs
$updater = new Updater(array(), array(), $this->conf, true); $updater = new Updater(array(), array(), $this->conf, true);
// This writes a new config file in tests/Updater/config.php // This writes a new config file in tests/updater/config.php
$updater->updateMethodMergeDeprecatedConfigFile(); $updater->updateMethodMergeDeprecatedConfigFile();
// make sure updated field is changed // make sure updated field is changed
@ -218,7 +223,7 @@ public function testMergeDeprecatedConfigNoFile()
*/ */
public function testRenameDashTags() public function testRenameDashTags()
{ {
$refDB = new ReferenceLinkDB(); $refDB = new \ReferenceLinkDB();
$refDB->write(self::$testDatastore); $refDB->write(self::$testDatastore);
$linkDB = new LinkDB(self::$testDatastore, true, false); $linkDB = new LinkDB(self::$testDatastore, true, false);
@ -364,7 +369,7 @@ public function testDatastoreIds()
'private' => true, 'private' => true,
), ),
); );
$refDB = new ReferenceLinkDB(); $refDB = new \ReferenceLinkDB();
$refDB->setLinks($links); $refDB->setLinks($links);
$refDB->write(self::$testDatastore); $refDB->write(self::$testDatastore);
$linkDB = new LinkDB(self::$testDatastore, true, false); $linkDB = new LinkDB(self::$testDatastore, true, false);
@ -428,7 +433,7 @@ public function testDatastoreIds()
*/ */
public function testDatastoreIdsNothingToDo() public function testDatastoreIdsNothingToDo()
{ {
$refDB = new ReferenceLinkDB(); $refDB = new \ReferenceLinkDB();
$refDB->write(self::$testDatastore); $refDB->write(self::$testDatastore);
$linkDB = new LinkDB(self::$testDatastore, true, false); $linkDB = new LinkDB(self::$testDatastore, true, false);
@ -765,7 +770,7 @@ public function testUpdateStickyValid()
1 => ['id' => 1] + $blank, 1 => ['id' => 1] + $blank,
2 => ['id' => 2] + $blank, 2 => ['id' => 2] + $blank,
]; ];
$refDB = new ReferenceLinkDB(); $refDB = new \ReferenceLinkDB();
$refDB->setLinks($links); $refDB->setLinks($links);
$refDB->write(self::$testDatastore); $refDB->write(self::$testDatastore);
$linkDB = new LinkDB(self::$testDatastore, true, false); $linkDB = new LinkDB(self::$testDatastore, true, false);
@ -796,7 +801,7 @@ public function testUpdateStickyNothingToDo()
1 => ['id' => 1, 'sticky' => true] + $blank, 1 => ['id' => 1, 'sticky' => true] + $blank,
2 => ['id' => 2] + $blank, 2 => ['id' => 2] + $blank,
]; ];
$refDB = new ReferenceLinkDB(); $refDB = new \ReferenceLinkDB();
$refDB->setLinks($links); $refDB->setLinks($links);
$refDB->write(self::$testDatastore); $refDB->write(self::$testDatastore);
$linkDB = new LinkDB(self::$testDatastore, true, false); $linkDB = new LinkDB(self::$testDatastore, true, false);

View file

@ -8,7 +8,7 @@
$GLOBALS['redirector'] = 'lala'; $GLOBALS['redirector'] = 'lala';
$GLOBALS['disablesessionprotection'] = false; $GLOBALS['disablesessionprotection'] = false;
$GLOBALS['privateLinkByDefault'] = false; $GLOBALS['privateLinkByDefault'] = false;
$GLOBALS['config']['DATADIR'] = 'tests/Updater'; $GLOBALS['config']['DATADIR'] = 'tests/updater';
$GLOBALS['config']['PAGECACHE'] = 'sandbox/pagecache'; $GLOBALS['config']['PAGECACHE'] = 'sandbox/pagecache';
$GLOBALS['config']['DATASTORE'] = 'data/datastore.php'; $GLOBALS['config']['DATASTORE'] = 'data/datastore.php';
$GLOBALS['plugins']['WALLABAG_VERSION'] = '1'; $GLOBALS['plugins']['WALLABAG_VERSION'] = '1';