diff --git a/lib/Alchemy/Phrasea/Core/Version.php b/lib/Alchemy/Phrasea/Core/Version.php index b21227ff07..d0e92e5ac2 100644 --- a/lib/Alchemy/Phrasea/Core/Version.php +++ b/lib/Alchemy/Phrasea/Core/Version.php @@ -13,14 +13,27 @@ namespace Alchemy\Phrasea\Core; class Version { + /** + * @var string + */ private $number = '4.0.0-alpha.2'; + + /** + * @var string + */ private $name = 'Herrerasaurus'; + /** + * @return string + */ public function getNumber() { return $this->number; } + /** + * @return string + */ public function getName() { return $this->name; diff --git a/lib/Alchemy/Phrasea/Core/Version/AppboxVersionRepository.php b/lib/Alchemy/Phrasea/Core/Version/AppboxVersionRepository.php new file mode 100644 index 0000000000..db7415de97 --- /dev/null +++ b/lib/Alchemy/Phrasea/Core/Version/AppboxVersionRepository.php @@ -0,0 +1,50 @@ +connection = $connection; + } + + /** + * @return string + */ + public function getVersion() + { + $result = $this->connection->fetchAssoc('SELECT version FROM sitepreff'); + + if ($result !== false) { + return $result['version']; + } + + return VersionRepository::DEFAULT_VERSION; + } + + /** + * @param Version $version + * @return bool + */ + public function saveVersion(Version $version) + { + $statement = $this->connection->executeQuery( + 'UPDATE sitepreff SET version = :version WHERE id = 1', + [ ':version' => $version->getNumber() ] + ); + + return $statement->rowCount() == 1; + } +} diff --git a/lib/Alchemy/Phrasea/Core/Version/DataboxVersionRepository.php b/lib/Alchemy/Phrasea/Core/Version/DataboxVersionRepository.php new file mode 100644 index 0000000000..dd9f5d1ad0 --- /dev/null +++ b/lib/Alchemy/Phrasea/Core/Version/DataboxVersionRepository.php @@ -0,0 +1,53 @@ +connection = $connection; + } + + /** + * @return string + */ + public function getVersion() + { + $result = $this->connection->fetchAssoc('SELECT value AS version FROM pref WHERE prop="version"'); + + if ($result !== false) { + return $result['version']; + } + + return VersionRepository::DEFAULT_VERSION; + } + + /** + * @param Version $version + * @return bool + * @throws \Doctrine\DBAL\DBALException + */ + public function saveVersion(Version $version) + { + $this->connection->exec("DELETE FROM pref WHERE prop='version'"); + + $statement = $this->connection->executeQuery( + 'INSERT INTO pref (prop, value, locale, updated_on) VALUES ("version", :version, "", NOW())', + [ ':version' => $version->getNumber() ] + ); + + return $statement->rowCount() == 1; + } +} diff --git a/lib/Alchemy/Phrasea/Core/Version/VersionRepository.php b/lib/Alchemy/Phrasea/Core/Version/VersionRepository.php new file mode 100644 index 0000000000..f6734f4363 --- /dev/null +++ b/lib/Alchemy/Phrasea/Core/Version/VersionRepository.php @@ -0,0 +1,22 @@ +get(['main', 'database']); + $connection = $app['db.provider']($connectionConfig); $connectionSettings = new ConnectionSettings( $connectionConfig['host'], @@ -54,9 +56,9 @@ class appbox extends base $connectionConfig['password'] ); - $connection = $app['db.provider']($connectionConfig); + $versionRepository = new AppboxVersionRepository($connection); - parent::__construct($app, $connection, $connectionSettings); + parent::__construct($app, $connection, $connectionSettings, $versionRepository); } public function write_collection_pic(Alchemyst $alchemyst, Filesystem $filesystem, collection $collection, SymfoFile $pathfile = null, $pic_type) diff --git a/lib/classes/base.php b/lib/classes/base.php index 66ddea5f59..c09369c276 100644 --- a/lib/classes/base.php +++ b/lib/classes/base.php @@ -13,7 +13,6 @@ use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Core\Connection\ConnectionSettings; use Alchemy\Phrasea\Core\Database\DatabaseMaintenanceService; use Alchemy\Phrasea\Core\Version as PhraseaVersion; -use vierbergenlars\SemVer\version; use Doctrine\DBAL\Connection; abstract class base implements cache_cacheableInterface @@ -53,16 +52,26 @@ abstract class base implements cache_cacheableInterface */ protected $app; + /** + * @var PhraseaVersion\VersionRepository + */ + protected $versionRepository; + /** * @param Application $application * @param Connection $connection * @param ConnectionSettings $connectionSettings + * @param PhraseaVersion\VersionRepository $versionRepository */ - public function __construct(Application $application, Connection $connection, ConnectionSettings $connectionSettings) + public function __construct(Application $application, + Connection $connection, + ConnectionSettings $connectionSettings, + PhraseaVersion\VersionRepository $versionRepository) { $this->app = $application; $this->connection = $connection; $this->connectionSettings = $connectionSettings; + $this->versionRepository = $versionRepository; } /** @@ -199,31 +208,21 @@ abstract class base implements cache_cacheableInterface public function get_version() { if (! $this->version) { - $version = '0.0.0'; - $sql = ''; - - if ($this->get_base_type() == self::APPLICATION_BOX) { - $sql = 'SELECT version FROM sitepreff'; - } - - if ($this->get_base_type() == self::DATA_BOX) { - $sql = 'SELECT value AS version FROM pref WHERE prop="version" LIMIT 1;'; - } - - if ($sql !== '') { - $row = $this->connection->fetchAssoc($sql); - - if ($row) { - $version = $row['version']; - } - } - - $this->version = $version; + $this->version = $this->versionRepository->getVersion($this); } return $this->version; } + protected function setVersion(PhraseaVersion $version) + { + try { + return $this->versionRepository->saveVersion($this, $version); + } catch (\Exception $e) { + throw new Exception('Unable to set the database version : ' . $e->getMessage()); + } + } + protected function upgradeDb($applyPatches) { $service = new DatabaseMaintenanceService($this->app, $this->connection); @@ -231,32 +230,6 @@ abstract class base implements cache_cacheableInterface return $service->upgradeDatabase($this, $applyPatches); } - protected function setVersion(PhraseaVersion $version) - { - try { - $sql = ''; - if ($this->get_base_type() === self::APPLICATION_BOX) { - $sql = 'UPDATE sitepreff SET version = :version WHERE id = 1'; - } - if ($this->get_base_type() === self::DATA_BOX) { - $sql = 'DELETE FROM pref WHERE prop="version"'; - $this->get_connection()->query($sql); - $sql = 'INSERT INTO pref (prop, value, locale, updated_on) VALUES ("version", :version, "", NOW())'; - } - if ($sql !== '') { - $stmt = $this->get_connection()->prepare($sql); - $stmt->execute([':version' => $version->getNumber()]); - $stmt->closeCursor(); - - $this->version = $version->getNumber(); - - return true; - } - } catch (\Exception $e) { - throw new Exception('Unable to set the database version : ' . $e->getMessage()); - } - } - /** * @return base * @throws Exception diff --git a/lib/classes/databox.php b/lib/classes/databox.php index ac1197272b..e554b865af 100644 --- a/lib/classes/databox.php +++ b/lib/classes/databox.php @@ -11,6 +11,7 @@ use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Core\Connection\ConnectionSettings; +use Alchemy\Phrasea\Core\Version\DataboxVersionRepository; use Alchemy\Phrasea\Databox\DataboxFieldRepositoryInterface; use Alchemy\Phrasea\Model\Entities\User; use Alchemy\Phrasea\Exception\InvalidArgumentException; @@ -80,6 +81,8 @@ class databox extends base } $connectionConfig = $connectionConfigs[$sbas_id]; + $connection = $app['db.provider']($connectionConfig); + $connectionSettings = new ConnectionSettings( $connectionConfig['host'], $connectionConfig['port'], @@ -88,9 +91,9 @@ class databox extends base $connectionConfig['password'] ); - $connection = $app['db.provider']($connectionConfig); + $versionRepository = new DataboxVersionRepository($connection); - parent::__construct($app, $connection, $connectionSettings); + parent::__construct($app, $connection, $connectionSettings, $versionRepository); $this->loadFromRow($row); }