app = $application; $this->connection = $connection; $this->connectionSettings = $connectionSettings; } /** * @return string */ abstract public function get_base_type(); /** * @return SimpleXMLElement * @throws Exception */ public function get_schema() { if ($this->schema) { return $this->schema; } $this->load_schema(); return $this->schema; } /** * @return string */ public function get_dbname() { return $this->connectionSettings->getDatabaseName(); } /** * @return string */ public function get_passwd() { return $this->connectionSettings->getPassword(); } /** * @return string */ public function get_user() { return $this->connectionSettings->getUser(); } /** * @return int */ public function get_port() { return $this->connectionSettings->getPort(); } /** * @return string */ public function get_host() { return $this->connectionSettings->getHost(); } /** * @return Connection */ public function get_connection() { return $this->connection; } /** * @return \Alchemy\Phrasea\Cache\Cache */ public function get_cache() { return $this->app['cache']; } public function get_data_from_cache($option = null) { if ($this->get_base_type() == self::DATA_BOX) { \cache_databox::refresh($this->app, $this->id); } $data = $this->get_cache()->get($this->get_cache_key($option)); if (is_object($data) && method_exists($data, 'hydrate')) { $data->hydrate($this->app); } return $data; } public function set_data_to_cache($value, $option = null, $duration = 0) { return $this->get_cache()->save($this->get_cache_key($option), $value, $duration); } public function delete_data_from_cache($option = null) { $appbox = $this->get_base_type() == self::APPLICATION_BOX ? $this : $this->get_appbox(); if ($option === appbox::CACHE_LIST_BASES) { $keys = [$this->get_cache_key(appbox::CACHE_LIST_BASES)]; phrasea::reset_sbasDatas($appbox); phrasea::reset_baseDatas($appbox); phrasea::clear_sbas_params($this->app); return $this->get_cache()->deleteMulti($keys); } if (is_array($option)) { foreach ($option as $key => $value) { $option[$key] = $this->get_cache_key($value); } return $this->get_cache()->deleteMulti($option); } else { return $this->get_cache()->delete($this->get_cache_key($option)); } } /** * @param mixed $option * @throws Exception * @return string */ public function get_cache_key($option = null) { throw new Exception(__METHOD__ . ' must be defined in extended class'); } 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; } return $this->version; } protected function upgradeDb($applyPatches) { $service = new DatabaseMaintenanceService($this->app, $this->connection); 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 */ protected function load_schema() { if ($this->schema) { return $this; } if (false === $structure = simplexml_load_file(__DIR__ . "/../../lib/conf.d/bases_structure.xml")) { throw new Exception('Unable to load schema'); } if ($this->get_base_type() === self::APPLICATION_BOX) { $this->schema = $structure->appbox; } elseif ($this->get_base_type() === self::DATA_BOX) { $this->schema = $structure->databox; } else { throw new Exception('Unknown schema type'); } return $this; } /** * @return base */ public function insert_datas() { $this->load_schema(); $service = new DatabaseMaintenanceService($this->app, $this->connection); foreach ($this->get_schema()->tables->table as $table) { $service->createTable($table); } $this->setVersion($this->app['phraseanet.version']); return $this; } public function apply_patches($from, $to, $post_process, Application $app) { $service = new DatabaseMaintenanceService($this->app, $this->connection); return $service->applyPatches($this, $from, $to, $post_process, $app); } }