From bcf056c9d92e5240e645c76a4cdc8ae159693f9a Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Mon, 3 Dec 2018 23:49:20 +0100 Subject: [PATCH] namespacing: \Shaarli\Updater Signed-off-by: VirtualTam --- application/config/ConfigPhp.php | 2 +- application/{ => updater}/Updater.php | 134 ++++-------------- application/updater/UpdaterUtils.php | 39 +++++ .../updater/exception/UpdaterException.php | 60 ++++++++ composer.json | 4 +- index.php | 3 +- tests/{Updater => updater}/DummyUpdater.php | 9 +- tests/{Updater => updater}/UpdaterTest.php | 29 ++-- tests/utils/config/configPhp.php | 2 +- 9 files changed, 154 insertions(+), 128 deletions(-) rename application/{ => updater}/Updater.php (82%) create mode 100644 application/updater/UpdaterUtils.php create mode 100644 application/updater/exception/UpdaterException.php rename tests/{Updater => updater}/DummyUpdater.php (88%) rename tests/{Updater => updater}/UpdaterTest.php (97%) diff --git a/application/config/ConfigPhp.php b/application/config/ConfigPhp.php index 9ed5d31f..cad34594 100644 --- a/application/config/ConfigPhp.php +++ b/application/config/ConfigPhp.php @@ -27,7 +27,7 @@ class ConfigPhp implements ConfigIO /** * Map legacy config keys with the new ones. * If ConfigPhp is used, getting will actually look for . - * 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. */ diff --git a/application/Updater.php b/application/updater/Updater.php similarity index 82% rename from application/Updater.php rename to application/updater/Updater.php index ca05ecc2..55251a30 100644 --- a/application/Updater.php +++ b/application/updater/Updater.php @@ -1,15 +1,24 @@ 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) { // Not an update method or already done, pass. - if (! startsWith($method->getName(), 'updateMethod') + if (!startsWith($method->getName(), 'updateMethod') || in_array($method->getName(), $this->doneUpdates) ) { continue; @@ -143,7 +152,7 @@ class Updater } } $this->conf->write($this->isLoggedIn); - unlink($this->conf->get('resource.data_dir').'/options.php'); + unlink($this->conf->get('resource.data_dir') . '/options.php'); } return true; @@ -178,10 +187,10 @@ class Updater $subConfig = array('config', 'plugins'); foreach ($subConfig as $sub) { foreach ($oldConfig[$sub] as $key => $value) { - if (isset($legacyMap[$sub .'.'. $key])) { - $configKey = $legacyMap[$sub .'.'. $key]; + if (isset($legacyMap[$sub . '.' . $key])) { + $configKey = $legacyMap[$sub . '.' . $key]; } else { - $configKey = $sub .'.'. $key; + $configKey = $sub . '.' . $key; } $this->conf->set($configKey, $value); } @@ -237,7 +246,7 @@ class Updater return true; } - $save = $this->conf->get('resource.data_dir') .'/datastore.'. date('YmdHis') .'.php'; + $save = $this->conf->get('resource.data_dir') . '/datastore.' . date('YmdHis') . '.php'; copy($this->conf->get('resource.datastore'), $save); $links = array(); @@ -311,7 +320,7 @@ class Updater // We run the update only if this folder still contains the template files. $tplDir = $this->conf->get('resource.raintpl_tpl'); $tplFile = $tplDir . '/linklist.html'; - if (! file_exists($tplFile)) { + if (!file_exists($tplFile)) { return true; } @@ -335,7 +344,7 @@ class Updater */ public function updateMethodMoveUserCss() { - if (! is_file('inc/user.css')) { + if (!is_file('inc/user.css')) { return true; } @@ -371,11 +380,11 @@ class Updater */ public function updateMethodPiwikUrl() { - if (! $this->conf->exists('plugins.PIWIK_URL') || startsWith($this->conf->get('plugins.PIWIK_URL'), 'http')) { + if (!$this->conf->exists('plugins.PIWIK_URL') || startsWith($this->conf->get('plugins.PIWIK_URL'), 'http')) { return true; } - $this->conf->set('plugins.PIWIK_URL', 'http://'. $this->conf->get('plugins.PIWIK_URL')); + $this->conf->set('plugins.PIWIK_URL', 'http://' . $this->conf->get('plugins.PIWIK_URL')); $this->conf->write($this->isLoggedIn); return true; @@ -485,11 +494,11 @@ class Updater return true; } - if (! $this->conf->exists('general.download_max_size')) { - $this->conf->set('general.download_max_size', 1024*1024*4); + if (!$this->conf->exists('general.download_max_size')) { + $this->conf->set('general.download_max_size', 1024 * 1024 * 4); } - if (! $this->conf->exists('general.download_timeout')) { + if (!$this->conf->exists('general.download_timeout')) { $this->conf->set('general.download_timeout', 30); } @@ -542,96 +551,3 @@ class Updater 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 . '.')); - } -} diff --git a/application/updater/UpdaterUtils.php b/application/updater/UpdaterUtils.php new file mode 100644 index 00000000..34d4f422 --- /dev/null +++ b/application/updater/UpdaterUtils.php @@ -0,0 +1,39 @@ +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; + } +} diff --git a/composer.json b/composer.json index 4c14e794..af763472 100644 --- a/composer.json +++ b/composer.json @@ -46,7 +46,9 @@ "Shaarli\\Feed\\": "application/feed", "Shaarli\\Http\\": "application/http", "Shaarli\\Render\\": "application/render", - "Shaarli\\Security\\": "application/security" + "Shaarli\\Security\\": "application/security", + "Shaarli\\Updater\\": "application/updater", + "Shaarli\\Updater\\Exception\\": "application/updater/exception" } } } diff --git a/index.php b/index.php index 146b4457..ce0373e1 100644 --- a/index.php +++ b/index.php @@ -62,6 +62,7 @@ require_once 'application/config/ConfigPlugin.php'; require_once 'application/feed/Cache.php'; require_once 'application/http/HttpUtils.php'; require_once 'application/http/UrlUtils.php'; +require_once 'application/updater/UpdaterUtils.php'; require_once 'application/FileUtils.php'; require_once 'application/History.php'; require_once 'application/NetscapeBookmarkUtils.php'; @@ -69,7 +70,6 @@ require_once 'application/TimeZone.php'; require_once 'application/Utils.php'; require_once 'application/PluginManager.php'; require_once 'application/Router.php'; -require_once 'application/Updater.php'; use \Shaarli\Bookmark\Exception\LinkNotFoundException; use \Shaarli\Bookmark\LinkDB; @@ -83,6 +83,7 @@ use \Shaarli\Render\ThemeUtils; use \Shaarli\Security\LoginManager; use \Shaarli\Security\SessionManager; use \Shaarli\Thumbnailer; +use Shaarli\Updater\Updater; // Ensure the PHP version is supported try { diff --git a/tests/Updater/DummyUpdater.php b/tests/updater/DummyUpdater.php similarity index 88% rename from tests/Updater/DummyUpdater.php rename to tests/updater/DummyUpdater.php index 3c74b4ff..9e866f1f 100644 --- a/tests/Updater/DummyUpdater.php +++ b/tests/updater/DummyUpdater.php @@ -1,12 +1,15 @@ conf->setConfigFile('tests/utils/config/configPhp'); $this->conf->reset(); - $optionsFile = 'tests/Updater/options.php'; + $optionsFile = 'tests/updater/options.php'; $options = 'conf->setConfigFile('tests/Updater/config'); + $this->conf->setConfigFile('tests/updater/config'); // merge configs $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(); // make sure updated field is changed @@ -218,7 +223,7 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; */ public function testRenameDashTags() { - $refDB = new ReferenceLinkDB(); + $refDB = new \ReferenceLinkDB(); $refDB->write(self::$testDatastore); $linkDB = new LinkDB(self::$testDatastore, true, false); @@ -364,7 +369,7 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; 'private' => true, ), ); - $refDB = new ReferenceLinkDB(); + $refDB = new \ReferenceLinkDB(); $refDB->setLinks($links); $refDB->write(self::$testDatastore); $linkDB = new LinkDB(self::$testDatastore, true, false); @@ -428,7 +433,7 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; */ public function testDatastoreIdsNothingToDo() { - $refDB = new ReferenceLinkDB(); + $refDB = new \ReferenceLinkDB(); $refDB->write(self::$testDatastore); $linkDB = new LinkDB(self::$testDatastore, true, false); @@ -765,7 +770,7 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; 1 => ['id' => 1] + $blank, 2 => ['id' => 2] + $blank, ]; - $refDB = new ReferenceLinkDB(); + $refDB = new \ReferenceLinkDB(); $refDB->setLinks($links); $refDB->write(self::$testDatastore); $linkDB = new LinkDB(self::$testDatastore, true, false); @@ -796,7 +801,7 @@ $GLOBALS[\'privateLinkByDefault\'] = true;'; 1 => ['id' => 1, 'sticky' => true] + $blank, 2 => ['id' => 2] + $blank, ]; - $refDB = new ReferenceLinkDB(); + $refDB = new \ReferenceLinkDB(); $refDB->setLinks($links); $refDB->write(self::$testDatastore); $linkDB = new LinkDB(self::$testDatastore, true, false); diff --git a/tests/utils/config/configPhp.php b/tests/utils/config/configPhp.php index 34b11fcd..7dc81e22 100644 --- a/tests/utils/config/configPhp.php +++ b/tests/utils/config/configPhp.php @@ -8,7 +8,7 @@ $GLOBALS['titleLink'] = 'titleLink'; $GLOBALS['redirector'] = 'lala'; $GLOBALS['disablesessionprotection'] = false; $GLOBALS['privateLinkByDefault'] = false; -$GLOBALS['config']['DATADIR'] = 'tests/Updater'; +$GLOBALS['config']['DATADIR'] = 'tests/updater'; $GLOBALS['config']['PAGECACHE'] = 'sandbox/pagecache'; $GLOBALS['config']['DATASTORE'] = 'data/datastore.php'; $GLOBALS['plugins']['WALLABAG_VERSION'] = '1'; -- 2.41.0