Extract VO class for database connection settings

This commit is contained in:
Aztech
2015-07-07 23:24:49 +02:00
committed by Thibaud Fabre
parent be320ea7a2
commit 2605729137
5 changed files with 319 additions and 136 deletions

View File

@@ -0,0 +1,100 @@
<?php
namespace Alchemy\Phrasea\Core\Connection;
class ConnectionSettings
{
/**
* @var string
*/
protected $host;
/**
* @var int
*/
protected $port;
/**
* @var string
*/
protected $databaseName;
/**
* @var string
*/
protected $user;
/**
* @var string
*/
protected $password;
/**
* @param string $host
* @param int|null $port
* @param string $databaseName
* @param string $user
* @param string $password
*/
public function __construct($host, $port, $databaseName, $user, $password)
{
$this->host = (string) $host;
$this->port = ((int) $port) > 0 ? (int) $port : null;
$this->databaseName = (string) $databaseName;
$this->user = $user;
$this->password = $password;
}
/**
* @return string
*/
public function getHost()
{
return $this->host;
}
/**
* @return int
*/
public function getPort()
{
return $this->port;
}
/**
* @return string
*/
public function getDatabaseName()
{
return $this->databaseName;
}
/**
* @return string
*/
public function getUser()
{
return $this->user;
}
/**
* @return string
*/
public function getPassword()
{
return $this->password;
}
public function toArray()
{
return [
'dbname' => $this->databaseName,
'host' => $this->host,
'port' => $this->port,
'user' => $this->user,
'password' => $this->password
];
}
}

View File

@@ -11,6 +11,7 @@
use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Core\Configuration\AccessRestriction; use Alchemy\Phrasea\Core\Configuration\AccessRestriction;
use Alchemy\Phrasea\Core\Connection\ConnectionSettings;
use Alchemy\Phrasea\Databox\DataboxRepositoryInterface; use Alchemy\Phrasea\Databox\DataboxRepositoryInterface;
use Doctrine\ORM\Tools\SchemaTool; use Doctrine\ORM\Tools\SchemaTool;
use MediaAlchemyst\Alchemyst; use MediaAlchemyst\Alchemyst;
@@ -23,33 +24,39 @@ use vierbergenlars\SemVer\version;
class appbox extends base class appbox extends base
{ {
/** @var int */
protected $id;
/** /**
* constant defining the app type * constant defining the app type
*/ */
const BASE_TYPE = self::APPLICATION_BOX; const BASE_TYPE = self::APPLICATION_BOX;
protected $cache;
protected $connection;
protected $app;
protected $databoxes;
const CACHE_LIST_BASES = 'list_bases'; const CACHE_LIST_BASES = 'list_bases';
const CACHE_SBAS_IDS = 'sbas_ids'; const CACHE_SBAS_IDS = 'sbas_ids';
/**
* @var int
*/
protected $id;
/**
* @var \databox[]
*/
protected $databoxes;
public function __construct(Application $app) public function __construct(Application $app)
{ {
$connexion = $app['conf']->get(['main', 'database']); $connectionConfig = $app['conf']->get(['main', 'database']);
parent::__construct($app, $app['db.provider']($connexion));
$this->host = $connexion['host']; $connectionSettings = new ConnectionSettings(
$this->port = $connexion['port']; $connectionConfig['host'],
$this->user = $connexion['user']; $connectionConfig['port'],
$this->passwd = $connexion['password']; $connectionConfig['dbname'],
$this->dbname = $connexion['dbname']; $connectionConfig['user'],
$connectionConfig['password']
);
$connection = $app['db.provider']($connectionConfig);
parent::__construct($app, $app['db.provider']($connection), $connectionSettings);
} }
public function write_collection_pic(Alchemyst $alchemyst, Filesystem $filesystem, collection $collection, SymfoFile $pathfile = null, $pic_type) public function write_collection_pic(Alchemyst $alchemyst, Filesystem $filesystem, collection $collection, SymfoFile $pathfile = null, $pic_type)
@@ -216,9 +223,8 @@ class appbox extends base
} }
/** /**
*
* @param databox $databox * @param databox $databox
* @param <type> $boolean * @param bool $boolean
* @return appbox * @return appbox
*/ */
public function set_databox_indexable(databox $databox, $boolean) public function set_databox_indexable(databox $databox, $boolean)
@@ -237,9 +243,8 @@ class appbox extends base
} }
/** /**
*
* @param databox $databox * @param databox $databox
* @return <type> * @return bool
*/ */
public function is_databox_indexable(databox $databox) public function is_databox_indexable(databox $databox)
{ {
@@ -256,8 +261,7 @@ class appbox extends base
} }
/** /**
* * @return string
* @return const
*/ */
public function get_base_type() public function get_base_type()
{ {

View File

@@ -10,39 +10,69 @@
*/ */
use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Core\Connection\ConnectionSettings;
use Alchemy\Phrasea\Core\Version as PhraseaVersion; use Alchemy\Phrasea\Core\Version as PhraseaVersion;
use vierbergenlars\SemVer\version; use vierbergenlars\SemVer\version;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
abstract class base implements cache_cacheableInterface abstract class base implements cache_cacheableInterface
{ {
protected $version;
/** @var int */
protected $id;
protected $schema;
protected $dbname;
protected $passwd;
protected $user;
protected $port;
protected $host;
/** @var Connection */
protected $connection;
/** @var Application */
protected $app;
const APPLICATION_BOX = 'APPLICATION_BOX'; const APPLICATION_BOX = 'APPLICATION_BOX';
const DATA_BOX = 'DATA_BOX'; const DATA_BOX = 'DATA_BOX';
public function __construct(Application $application, Connection $connection) /**
* @var string
*/
protected $version;
/**
* @var int
*/
protected $id;
/**
* @var SimpleXMLElement
*/
protected $schema;
/**
* @var ConnectionSettings
*/
protected $connectionSettings;
/**
* @var Connection
*/
protected $connection;
/**
* @var Application
*/
protected $app;
/**
* @param Application $application
* @param Connection $connection
* @param ConnectionSettings $connectionSettings
*/
public function __construct(Application $application, Connection $connection, ConnectionSettings $connectionSettings)
{ {
$this->app = $application; $this->app = $application;
$this->connection = $connection; $this->connection = $connection;
$this->connectionSettings = $connectionSettings;
} }
/**
* @return string
*/
abstract public function get_base_type(); abstract public function get_base_type();
/**
* @return SimpleXMLElement
* @throws Exception
*/
public function get_schema() public function get_schema()
{ {
if ($this->schema) { if ($this->schema) {
@@ -54,32 +84,49 @@ abstract class base implements cache_cacheableInterface
return $this->schema; return $this->schema;
} }
/**
* @return string
*/
public function get_dbname() public function get_dbname()
{ {
return $this->dbname; return $this->connectionSettings->getDatabaseName();
} }
/**
* @return string
*/
public function get_passwd() public function get_passwd()
{ {
return $this->passwd; return $this->connectionSettings->getPassword();
} }
/**
* @return string
*/
public function get_user() public function get_user()
{ {
return $this->user; return $this->connectionSettings->getUser();
} }
/**
* @return int
*/
public function get_port() public function get_port()
{ {
return $this->port; return $this->connectionSettings->getPort();
} }
/**
* @return string
*/
public function get_host() public function get_host()
{ {
return $this->host; return $this->connectionSettings->getHost();
} }
/** @return Connection */ /**
* @return Connection
*/
public function get_connection() public function get_connection()
{ {
return $this->connection; return $this->connection;
@@ -126,8 +173,9 @@ abstract class base implements cache_cacheableInterface
} }
if (is_array($option)) { if (is_array($option)) {
foreach ($option as $key => $value) foreach ($option as $key => $value) {
$option[$key] = $this->get_cache_key($value); $option[$key] = $this->get_cache_key($value);
}
return $this->get_cache()->deleteMulti($option); return $this->get_cache()->deleteMulti($option);
} else { } else {
@@ -154,18 +202,21 @@ abstract class base implements cache_cacheableInterface
$version = '0.0.0'; $version = '0.0.0';
$sql = ''; $sql = '';
if ($this->get_base_type() == self::APPLICATION_BOX) if ($this->get_base_type() == self::APPLICATION_BOX) {
$sql = 'SELECT version FROM sitepreff'; $sql = 'SELECT version FROM sitepreff';
if ($this->get_base_type() == self::DATA_BOX) }
if ($this->get_base_type() == self::DATA_BOX) {
$sql = 'SELECT value AS version FROM pref WHERE prop="version" LIMIT 1;'; $sql = 'SELECT value AS version FROM pref WHERE prop="version" LIMIT 1;';
}
if ($sql !== '') { if ($sql !== '') {
$stmt = $this->get_connection()->prepare($sql); $stmt = $this->get_connection()->prepare($sql);
$stmt->execute(); $stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC); $row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor(); $stmt->closeCursor();
if ($row) if ($row) {
$version = $row['version']; $version = $row['version'];
}
} }
$this->version = $version; $this->version = $version;
@@ -182,7 +233,7 @@ abstract class base implements cache_cacheableInterface
$schema = $this->get_schema(); $schema = $this->get_schema();
foreach ($schema->tables->table as $table) { foreach ($schema->tables->table as $table) {
$allTables[(string) $table['name']] = $table; $allTables[(string)$table['name']] = $table;
} }
$sql = "SHOW TABLE STATUS"; $sql = "SHOW TABLE STATUS";
@@ -249,8 +300,9 @@ abstract class base implements cache_cacheableInterface
$stmt->closeCursor(); $stmt->closeCursor();
} catch (\Exception $e) { } catch (\Exception $e) {
$recommends[] = [ $recommends[] = [
'message' => $app->trans('Erreur lors de la tentative ; errreur : %message%', ['%message%' => $e->getMessage()]), 'message' => $app->trans('Erreur lors de la tentative ; errreur : %message%',
'sql' => $sql ['%message%' => $e->getMessage()]),
'sql' => $sql
]; ];
} }
} }
@@ -258,10 +310,10 @@ abstract class base implements cache_cacheableInterface
$ret = self::upgradeTable($allTables[$tname]); $ret = self::upgradeTable($allTables[$tname]);
$recommends = array_merge($recommends, $ret); $recommends = array_merge($recommends, $ret);
unset($allTables[$tname]); unset($allTables[$tname]);
} elseif ( ! in_array($tname, $ORMTables)) { } elseif (!in_array($tname, $ORMTables)) {
$recommends[] = [ $recommends[] = [
'message' => 'Une table pourrait etre supprime', 'message' => 'Une table pourrait etre supprime',
'sql' => 'DROP TABLE ' . $this->dbname . '.`' . $tname . '`;' 'sql' => 'DROP TABLE ' . $this->dbname . '.`' . $tname . '`;'
]; ];
} }
} }
@@ -282,8 +334,9 @@ abstract class base implements cache_cacheableInterface
{ {
try { try {
$sql = ''; $sql = '';
if ($this->get_base_type() === self::APPLICATION_BOX) if ($this->get_base_type() === self::APPLICATION_BOX) {
$sql = 'UPDATE sitepreff SET version = :version WHERE id = 1'; $sql = 'UPDATE sitepreff SET version = :version WHERE id = 1';
}
if ($this->get_base_type() === self::DATA_BOX) { if ($this->get_base_type() === self::DATA_BOX) {
$sql = 'DELETE FROM pref WHERE prop="version"'; $sql = 'DELETE FROM pref WHERE prop="version"';
$this->get_connection()->query($sql); $this->get_connection()->query($sql);
@@ -299,7 +352,7 @@ abstract class base implements cache_cacheableInterface
return true; return true;
} }
} catch (\Exception $e) { } catch (\Exception $e) {
throw new Exception('Unable to set the database version : '.$e->getMessage()); throw new Exception('Unable to set the database version : ' . $e->getMessage());
} }
return; return;
@@ -307,6 +360,7 @@ abstract class base implements cache_cacheableInterface
/** /**
* @return base * @return base
* @throws Exception
*/ */
protected function load_schema() protected function load_schema()
{ {
@@ -318,12 +372,13 @@ abstract class base implements cache_cacheableInterface
throw new Exception('Unable to load schema'); throw new Exception('Unable to load schema');
} }
if ($this->get_base_type() === self::APPLICATION_BOX) if ($this->get_base_type() === self::APPLICATION_BOX) {
$this->schema = $structure->appbox; $this->schema = $structure->appbox;
elseif ($this->get_base_type() === self::DATA_BOX) } elseif ($this->get_base_type() === self::DATA_BOX) {
$this->schema = $structure->databox; $this->schema = $structure->databox;
else } else {
throw new Exception('Unknown schema type'); throw new Exception('Unknown schema type');
}
return $this; return $this;
} }
@@ -357,19 +412,21 @@ abstract class base implements cache_cacheableInterface
foreach ($table->fields->field as $field) { foreach ($table->fields->field as $field) {
$isnull = trim($field->null) == "" ? "NOT NULL" : "NULL"; $isnull = trim($field->null) == "" ? "NOT NULL" : "NULL";
if (trim($field->default) != "" && trim($field->default) != "CURRENT_TIMESTAMP") if (trim($field->default) != "" && trim($field->default) != "CURRENT_TIMESTAMP") {
$is_default = " default '" . $field->default . "'"; $is_default = " default '" . $field->default . "'";
elseif (trim($field->default) == "CURRENT_TIMESTAMP") } elseif (trim($field->default) == "CURRENT_TIMESTAMP") {
$is_default = " default " . $field->default; $is_default = " default " . $field->default;
else } else {
$is_default = ''; $is_default = '';
}
$character_set = ''; $character_set = '';
if (in_array(strtolower((string) $field->type), ['text', 'longtext', 'mediumtext', 'tinytext']) if (in_array(strtolower((string)$field->type), ['text', 'longtext', 'mediumtext', 'tinytext'])
|| substr(strtolower((string) $field->type), 0, 7) == 'varchar' || substr(strtolower((string)$field->type), 0, 7) == 'varchar'
|| in_array(substr(strtolower((string) $field->type), 0, 4), ['char', 'enum'])) { || in_array(substr(strtolower((string)$field->type), 0, 4), ['char', 'enum'])
) {
$collation = trim((string) $field->collation) != '' ? trim((string) $field->collation) : 'utf8_unicode_ci'; $collation = trim((string)$field->collation) != '' ? trim((string)$field->collation) : 'utf8_unicode_ci';
$collations = array_reverse(explode('_', $collation)); $collations = array_reverse(explode('_', $collation));
$code = array_pop($collations); $code = array_pop($collations);
@@ -421,27 +478,31 @@ abstract class base implements cache_cacheableInterface
$nonce = $this->app['random.medium']->generateString(16); $nonce = $this->app['random.medium']->generateString(16);
foreach ($default->data as $data) { foreach ($default->data as $data) {
$k = trim($data['key']); $k = trim($data['key']);
if ($k === 'usr_password') if ($k === 'usr_password') {
$data = $this->app['auth.password-encoder']->encodePassword($data, $nonce); $data = $this->app['auth.password-encoder']->encodePassword($data, $nonce);
if ($k === 'nonce') }
if ($k === 'nonce') {
$data = $nonce; $data = $nonce;
}
$v = trim(str_replace(["\r\n", "\r", "\n", "\t"], '', $data)); $v = trim(str_replace(["\r\n", "\r", "\n", "\t"], '', $data));
if (trim(mb_strtolower($v)) == 'now()') if (trim(mb_strtolower($v)) == 'now()') {
$dates_values [$k] = 'NOW()'; $dates_values [$k] = 'NOW()';
else } else {
$params[$k] = (trim(mb_strtolower($v)) == 'null' ? null : $v); $params[$k] = (trim(mb_strtolower($v)) == 'null' ? null : $v);
}
} }
$separator = ((count($params) > 0 && count($dates_values) > 0) ? ', ' : ''); $separator = ((count($params) > 0 && count($dates_values) > 0) ? ', ' : '');
$defaults_stmt[] = [ $defaults_stmt[] = [
'sql' => 'sql' =>
'INSERT INTO `' . $table['name'] . '` (' . implode(', ', array_keys($params)) 'INSERT INTO `' . $table['name'] . '` (' . implode(', ', array_keys($params))
. $separator . implode(', ', array_keys($dates_values)) . ') . $separator . implode(', ', array_keys($dates_values)) . ')
VALUES (:' . implode(', :', array_keys($params)) VALUES (:' . implode(', :', array_keys($params))
. $separator . implode(', ', array_values($dates_values)) . ') ' . $separator . implode(', ', array_values($dates_values)) . ') '
, 'params' => $params ,
'params' => $params
]; ];
} }
} }
@@ -472,49 +533,54 @@ abstract class base implements cache_cacheableInterface
$alter = $alter_pre = $return = []; $alter = $alter_pre = $return = [];
foreach ($table->fields->field as $field) { foreach ($table->fields->field as $field) {
$expr = trim((string) $field->type); $expr = trim((string)$field->type);
$_extra = trim((string) $field->extra); $_extra = trim((string)$field->extra);
if ($_extra) if ($_extra) {
$expr .= ' ' . $_extra; $expr .= ' ' . $_extra;
}
$collation = trim((string) $field->collation) != '' ? trim((string) $field->collation) : 'utf8_unicode_ci'; $collation = trim((string)$field->collation) != '' ? trim((string)$field->collation) : 'utf8_unicode_ci';
if (in_array(strtolower((string) $field->type), ['text', 'longtext', 'mediumtext', 'tinytext']) if (in_array(strtolower((string)$field->type), ['text', 'longtext', 'mediumtext', 'tinytext'])
|| substr(strtolower((string) $field->type), 0, 7) == 'varchar' || substr(strtolower((string)$field->type), 0, 7) == 'varchar'
|| in_array(substr(strtolower((string) $field->type), 0, 4), ['char', 'enum'])) { || in_array(substr(strtolower((string)$field->type), 0, 4), ['char', 'enum'])
) {
$collations = array_reverse(explode('_', $collation)); $collations = array_reverse(explode('_', $collation));
$code = array_pop($collations); $code = array_pop($collations);
$collation = ' CHARACTER SET ' . $code . ' COLLATE ' . $collation; $collation = ' CHARACTER SET ' . $code . ' COLLATE ' . $collation;
$correct_table['collation'][trim((string) $field->name)] = $collation; $correct_table['collation'][trim((string)$field->name)] = $collation;
$expr .= $collation; $expr .= $collation;
} }
$_null = mb_strtolower(trim((string) $field->null)); $_null = mb_strtolower(trim((string)$field->null));
if ( ! $_null || $_null == 'no') if (!$_null || $_null == 'no') {
$expr .= ' NOT NULL'; $expr .= ' NOT NULL';
}
$_default = (string) $field->default; $_default = (string)$field->default;
if ($_default && $_default != 'CURRENT_TIMESTAMP') if ($_default && $_default != 'CURRENT_TIMESTAMP') {
$expr .= ' DEFAULT \'' . $_default . '\''; $expr .= ' DEFAULT \'' . $_default . '\'';
elseif ($_default == 'CURRENT_TIMESTAMP') } elseif ($_default == 'CURRENT_TIMESTAMP') {
$expr .= ' DEFAULT ' . $_default . ''; $expr .= ' DEFAULT ' . $_default . '';
}
$correct_table['fields'][trim((string) $field->name)] = $expr; $correct_table['fields'][trim((string)$field->name)] = $expr;
} }
if ($table->indexes) { if ($table->indexes) {
foreach ($table->indexes->index as $index) { foreach ($table->indexes->index as $index) {
$i_name = (string) $index->name; $i_name = (string)$index->name;
$expr = []; $expr = [];
foreach ($index->fields->field as $field) foreach ($index->fields->field as $field) {
$expr[] = '`' . trim((string) $field) . '`'; $expr[] = '`' . trim((string)$field) . '`';
}
$expr = implode(', ', $expr); $expr = implode(', ', $expr);
switch ((string) $index->type) { switch ((string)$index->type) {
case "PRIMARY": case "PRIMARY":
$correct_table['indexes']['PRIMARY'] = 'PRIMARY KEY (' . $expr . ')'; $correct_table['indexes']['PRIMARY'] = 'PRIMARY KEY (' . $expr . ')';
break; break;
@@ -540,8 +606,9 @@ abstract class base implements cache_cacheableInterface
$_extra = $row2['Extra']; $_extra = $row2['Extra'];
if ($_extra) if ($_extra) {
$expr_found .= ' ' . $_extra; $expr_found .= ' ' . $_extra;
}
$_collation = $row2['Collation']; $_collation = $row2['Collation'];
@@ -550,21 +617,24 @@ abstract class base implements cache_cacheableInterface
if ($_collation) { if ($_collation) {
$_collation = explode('_', $row2['Collation']); $_collation = explode('_', $row2['Collation']);
$expr_found .= $current_collation = ' CHARACTER SET ' . $_collation[0] . ' COLLATE ' . implode('_', $_collation); $expr_found .= $current_collation = ' CHARACTER SET ' . $_collation[0] . ' COLLATE ' . implode('_',
$_collation);
} }
$_null = mb_strtolower(trim($row2['Null'])); $_null = mb_strtolower(trim($row2['Null']));
if ( ! $_null || $_null == 'no') if (!$_null || $_null == 'no') {
$expr_found .= ' NOT NULL'; $expr_found .= ' NOT NULL';
}
$_default = $row2['Default']; $_default = $row2['Default'];
if ($_default) { if ($_default) {
if (trim($row2['Type']) == 'timestamp' && $_default == 'CURRENT_TIMESTAMP') if (trim($row2['Type']) == 'timestamp' && $_default == 'CURRENT_TIMESTAMP') {
$expr_found .= ' DEFAULT CURRENT_TIMESTAMP'; $expr_found .= ' DEFAULT CURRENT_TIMESTAMP';
else } else {
$expr_found .= ' DEFAULT \'' . $_default . '\''; $expr_found .= ' DEFAULT \'' . $_default . '\'';
}
} }
if (isset($correct_table['fields'][$f_name])) { if (isset($correct_table['fields'][$f_name])) {
@@ -586,10 +656,12 @@ abstract class base implements cache_cacheableInterface
$new_type = 'tinyblob'; $new_type = 'tinyblob';
break; break;
default: default:
if (substr($old_type, 0, 4) == 'char') if (substr($old_type, 0, 4) == 'char') {
$new_type = 'varbinary(255)'; $new_type = 'varbinary(255)';
if (substr($old_type, 0, 7) == 'varchar') }
if (substr($old_type, 0, 7) == 'varchar') {
$new_type = 'varbinary(767)'; $new_type = 'varbinary(767)';
}
break; break;
} }
@@ -605,7 +677,7 @@ abstract class base implements cache_cacheableInterface
} else { } else {
$return[] = [ $return[] = [
'message' => 'Un champ pourrait etre supprime', 'message' => 'Un champ pourrait etre supprime',
'sql' => "ALTER TABLE " . $this->dbname . ".`" . $table['name'] . "` DROP `$f_name`;" 'sql' => "ALTER TABLE " . $this->dbname . ".`" . $table['name'] . "` DROP `$f_name`;"
]; ];
} }
} }
@@ -622,26 +694,29 @@ abstract class base implements cache_cacheableInterface
$stmt->closeCursor(); $stmt->closeCursor();
foreach ($rs2 as $row2) { foreach ($rs2 as $row2) {
if ( ! isset($tIndex[$row2['Key_name']])) if (!isset($tIndex[$row2['Key_name']])) {
$tIndex[$row2['Key_name']] = ['unique' => ((int) ($row2['Non_unique']) == 0), 'columns' => []]; $tIndex[$row2['Key_name']] = ['unique' => ((int)($row2['Non_unique']) == 0), 'columns' => []];
$tIndex[$row2['Key_name']]['columns'][(int) ($row2['Seq_in_index'])] = $row2['Column_name']; }
$tIndex[$row2['Key_name']]['columns'][(int)($row2['Seq_in_index'])] = $row2['Column_name'];
} }
foreach ($tIndex as $kIndex => $vIndex) { foreach ($tIndex as $kIndex => $vIndex) {
$strColumns = []; $strColumns = [];
foreach ($vIndex['columns'] as $column) foreach ($vIndex['columns'] as $column) {
$strColumns[] = '`' . $column . '`'; $strColumns[] = '`' . $column . '`';
}
$strColumns = '(' . implode(', ', $strColumns) . ')'; $strColumns = '(' . implode(', ', $strColumns) . ')';
if ($kIndex == 'PRIMARY') if ($kIndex == 'PRIMARY') {
$expr_found = 'PRIMARY KEY ' . $strColumns; $expr_found = 'PRIMARY KEY ' . $strColumns;
else { } else {
if ($vIndex['unique']) if ($vIndex['unique']) {
$expr_found = 'UNIQUE KEY `' . $kIndex . '` ' . $strColumns; $expr_found = 'UNIQUE KEY `' . $kIndex . '` ' . $strColumns;
else } else {
$expr_found = 'KEY `' . $kIndex . '` ' . $strColumns; $expr_found = 'KEY `' . $kIndex . '` ' . $strColumns;
}
} }
$full_name_index = ($kIndex == 'PRIMARY') ? 'PRIMARY KEY' : ('INDEX `' . $kIndex . '`'); $full_name_index = ($kIndex == 'PRIMARY') ? 'PRIMARY KEY' : ('INDEX `' . $kIndex . '`');
@@ -656,13 +731,14 @@ abstract class base implements cache_cacheableInterface
} else { } else {
$return[] = [ $return[] = [
'message' => 'Un index pourrait etre supprime', 'message' => 'Un index pourrait etre supprime',
'sql' => 'ALTER TABLE ' . $this->dbname . '.`' . $table['name'] . '` DROP ' . $full_name_index . ';' 'sql' => 'ALTER TABLE ' . $this->dbname . '.`' . $table['name'] . '` DROP ' . $full_name_index . ';'
]; ];
} }
} }
foreach ($correct_table['indexes'] as $kIndex => $expr) foreach ($correct_table['indexes'] as $kIndex => $expr) {
$alter[] = 'ALTER TABLE `' . $table['name'] . '` ADD ' . $expr; $alter[] = 'ALTER TABLE `' . $table['name'] . '` ADD ' . $expr;
}
foreach ($alter_pre as $a) { foreach ($alter_pre as $a) {
try { try {
@@ -671,8 +747,9 @@ abstract class base implements cache_cacheableInterface
$stmt->closeCursor(); $stmt->closeCursor();
} catch (\Exception $e) { } catch (\Exception $e) {
$return[] = [ $return[] = [
'message' => $this->app->trans('Erreur lors de la tentative ; errreur : %message%', ['%message%' => $e->getMessage()]), 'message' => $this->app->trans('Erreur lors de la tentative ; errreur : %message%',
'sql' => $a ['%message%' => $e->getMessage()]),
'sql' => $a
]; ];
} }
} }
@@ -684,8 +761,9 @@ abstract class base implements cache_cacheableInterface
$stmt->closeCursor(); $stmt->closeCursor();
} catch (\Exception $e) { } catch (\Exception $e) {
$return[] = [ $return[] = [
'message' => $this->app->trans('Erreur lors de la tentative ; errreur : %message%', ['%message%' => $e->getMessage()]), 'message' => $this->app->trans('Erreur lors de la tentative ; errreur : %message%',
'sql' => $a ['%message%' => $e->getMessage()]),
'sql' => $a
]; ];
} }
} }
@@ -704,20 +782,23 @@ abstract class base implements cache_cacheableInterface
$iterator = new DirectoryIterator($this->app['root.path'] . '/lib/classes/patch/'); $iterator = new DirectoryIterator($this->app['root.path'] . '/lib/classes/patch/');
foreach ($iterator as $fileinfo) { foreach ($iterator as $fileinfo) {
if ( ! $fileinfo->isDot()) { if (!$fileinfo->isDot()) {
if (substr($fileinfo->getFilename(), 0, 1) == '.') if (substr($fileinfo->getFilename(), 0, 1) == '.') {
continue; continue;
}
$versions = array_reverse(explode('.', $fileinfo->getFilename())); $versions = array_reverse(explode('.', $fileinfo->getFilename()));
$classname = 'patch_' . array_pop($versions); $classname = 'patch_' . array_pop($versions);
$patch = new $classname(); $patch = new $classname();
if ( ! in_array($this->get_base_type(), $patch->concern())) if (!in_array($this->get_base_type(), $patch->concern())) {
continue; continue;
}
if ( ! ! $post_process !== ! ! $patch->require_all_upgrades()) if (!!$post_process !== !!$patch->require_all_upgrades()) {
continue; continue;
}
// if patch is older than current install // if patch is older than current install
if (version::lte($patch->get_release(), $from)) { if (version::lte($patch->get_release(), $from)) {
@@ -731,7 +812,7 @@ abstract class base implements cache_cacheableInterface
$n = 0; $n = 0;
do { do {
$key = $patch->get_release() . '.' . $n; $key = $patch->get_release() . '.' . $n;
$n ++; $n++;
} while (isset($list_patches[$key])); } while (isset($list_patches[$key]));
$list_patches[$key] = $patch; $list_patches[$key] = $patch;

View File

@@ -10,6 +10,7 @@
*/ */
use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Core\Connection\ConnectionSettings;
use Alchemy\Phrasea\Databox\DataboxFieldRepositoryInterface; use Alchemy\Phrasea\Databox\DataboxFieldRepositoryInterface;
use Alchemy\Phrasea\Model\Entities\User; use Alchemy\Phrasea\Model\Entities\User;
use Alchemy\Phrasea\Exception\InvalidArgumentException; use Alchemy\Phrasea\Exception\InvalidArgumentException;
@@ -56,7 +57,6 @@ class databox extends base
const CACHE_STRUCTURE = 'structure'; const CACHE_STRUCTURE = 'structure';
const PIC_PDF = 'logopdf'; const PIC_PDF = 'logopdf';
protected $cache;
private $labels = []; private $labels = [];
private $ord; private $ord;
private $viewname; private $viewname;
@@ -73,26 +73,24 @@ class databox extends base
$this->id = $sbas_id; $this->id = $sbas_id;
$connection_params = phrasea::sbas_params($app); $connectionConfigs = phrasea::sbas_params($app);
if (! isset($connection_params[$sbas_id])) { if (! isset($connectionConfigs[$sbas_id])) {
throw new NotFoundHttpException(sprintf('databox %d not found', $sbas_id)); throw new NotFoundHttpException(sprintf('databox %d not found', $sbas_id));
} }
$params = [ $connectionConfig = $connectionConfigs[$sbas_id];
'host' => $connection_params[$sbas_id]['host'], $connectionSettings = new ConnectionSettings(
'port' => $connection_params[$sbas_id]['port'], $connectionConfig['host'],
'user' => $connection_params[$sbas_id]['user'], $connectionConfig['port'],
'password' => $connection_params[$sbas_id]['pwd'], $connectionConfig['dbname'],
'dbname' => $connection_params[$sbas_id]['dbname'], $connectionConfig['user'],
]; $connectionConfig['password']
parent::__construct($app, $app['db.provider']($params)); );
$this->host = $params['host']; $connection = $app['db.provider']($connectionConfig);
$this->port = $params['port'];
$this->user = $params['user']; parent::__construct($app, $connection, $connectionSettings);
$this->passwd = $params['password'];
$this->dbname = $params['dbname'];
$this->loadFromRow($row); $this->loadFromRow($row);
} }

View File

@@ -58,7 +58,7 @@ class phrasea
self::$_sbas_params = []; self::$_sbas_params = [];
$sql = 'SELECT sbas_id, host, port, user, pwd, dbname FROM sbas'; $sql = 'SELECT sbas_id, host, port, user, pwd as password, dbname FROM sbas';
$stmt = $app->getApplicationBox()->get_connection()->prepare($sql); $stmt = $app->getApplicationBox()->get_connection()->prepare($sql);
$stmt->execute(); $stmt->execute();
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC); $rs = $stmt->fetchAll(PDO::FETCH_ASSOC);