/**
* Map legacy config keys with the new ones.
* 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.
*/
<?php
+namespace Shaarli\Updater;
+
+use ApplicationUtils;
+use Exception;
+use RainTPL;
+use ReflectionClass;
+use ReflectionException;
+use ReflectionMethod;
use Shaarli\Bookmark\LinkDB;
use Shaarli\Bookmark\LinkFilter;
use Shaarli\Config\ConfigJson;
-use Shaarli\Config\ConfigPhp;
use Shaarli\Config\ConfigManager;
+use Shaarli\Config\ConfigPhp;
use Shaarli\Exceptions\IOException;
use Shaarli\Thumbnailer;
+use Shaarli\Updater\Exception\UpdaterException;
/**
- * Class Updater.
+ * Class updater.
* 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.
*/
}
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) {
// 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;
}
}
$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;
$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);
}
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();
// 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;
}
*/
public function updateMethodMoveUserCss()
{
- if (! is_file('inc/user.css')) {
+ if (!is_file('inc/user.css')) {
return true;
}
*/
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;
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);
}
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 . '.'));
- }
-}
--- /dev/null
+<?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 . '.'));
+ }
+}
--- /dev/null
+<?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;
+ }
+}
"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"
}
}
}
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';
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;
use \Shaarli\Security\LoginManager;
use \Shaarli\Security\SessionManager;
use \Shaarli\Thumbnailer;
+use Shaarli\Updater\Updater;
// Ensure the PHP version is supported
try {
<?php
+namespace Shaarli\Updater;
+use Exception;
+use ReflectionClass;
+use ReflectionMethod;
use Shaarli\Bookmark\LinkDB;
-
-require_once 'application/Updater.php';
+use Shaarli\Config\ConfigManager;
/**
* 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
{
<?php
+namespace Shaarli\Updater;
+use DateTime;
+use Exception;
use Shaarli\Bookmark\LinkDB;
use Shaarli\Config\ConfigJson;
use Shaarli\Config\ConfigManager;
use Shaarli\Config\ConfigPhp;
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';
/**
* 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.
/**
* Test Update failed.
*
- * @expectedException UpdaterException
+ * @expectedException \Exception
*/
public function testUpdateFailed()
{
$this->conf->setConfigFile('tests/utils/config/configPhp');
$this->conf->reset();
- $optionsFile = 'tests/Updater/options.php';
+ $optionsFile = 'tests/updater/options.php';
$options = '<?php
$GLOBALS[\'privateLinkByDefault\'] = true;';
file_put_contents($optionsFile, $options);
// tmp config file.
- $this->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
*/
public function testRenameDashTags()
{
- $refDB = new ReferenceLinkDB();
+ $refDB = new \ReferenceLinkDB();
$refDB->write(self::$testDatastore);
$linkDB = new LinkDB(self::$testDatastore, true, false);
'private' => true,
),
);
- $refDB = new ReferenceLinkDB();
+ $refDB = new \ReferenceLinkDB();
$refDB->setLinks($links);
$refDB->write(self::$testDatastore);
$linkDB = new LinkDB(self::$testDatastore, true, false);
*/
public function testDatastoreIdsNothingToDo()
{
- $refDB = new ReferenceLinkDB();
+ $refDB = new \ReferenceLinkDB();
$refDB->write(self::$testDatastore);
$linkDB = new LinkDB(self::$testDatastore, true, false);
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);
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);
$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';