Skip to content

Commit

Permalink
Improve handling of database port during installation process (#22252)
Browse files Browse the repository at this point in the history
* Allow providing database port during installation

* don't reveal already provided password during installation

* Revert using an additional field for host

* apply review feedback
  • Loading branch information
sgiehl authored May 27, 2024
1 parent 5eb299e commit dd3af29
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 15 deletions.
3 changes: 2 additions & 1 deletion core/Db/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ private static function getAdapterClassName($adapterName)
/**
* Get default port for named adapter
*
* @deprecated use Schema::getDefaultPortForSchema instead
* @param string $adapterName
* @return int
*/
Expand Down Expand Up @@ -118,7 +119,7 @@ public static function getAdapters()
foreach ($adapterNames as $adapterName) {
$className = '\Piwik\Db\Adapter\\' . $adapterName;
if (call_user_func(array($className, 'isEnabled'))) {
$adapters[strtoupper($adapterName)] = call_user_func(array($className, 'getDefaultPort'));
$adapters[] = strtoupper($adapterName);
}
}

Expand Down
1 change: 1 addition & 0 deletions core/Db/Adapter/Mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function resetConfig()
/**
* Return default port.
*
* @deprecated Use Schema::getDefaultPortForSchema instead
* @return int
*/
public static function getDefaultPort()
Expand Down
1 change: 1 addition & 0 deletions core/Db/Adapter/Pdo/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public function resetConfig()
/**
* Return default port.
*
* @deprecated Use Schema::getDefaultPortForSchema instead
* @return int
*/
public static function getDefaultPort()
Expand Down
1 change: 1 addition & 0 deletions core/Db/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function resetConfig();

/**
* Return default port.
* @deprecated Use Schema::getDefaultPortForSchema instead
*
* @return int
*/
Expand Down
13 changes: 13 additions & 0 deletions core/Db/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ private static function getSchemaClassName($schemaName): string
return '\Piwik\Db\Schema\\' . $class;
}

/**
* Return the default port for the provided database schema
*
* @param string $schemaName
* @return int
*/
public static function getDefaultPortForSchema(string $schemaName): int
{
$schemaClassName = self::getSchemaClassName($schemaName);
/** @var SchemaInterface $schemaClass */
$schemaClass = new $schemaClassName();
return $schemaClass->getDefaultPort();
}

/**
* Load schema
Expand Down
5 changes: 5 additions & 0 deletions core/Db/Schema/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,11 @@ public function supportsComplexColumnUpdates(): bool
return true;
}

public function getDefaultPort(): int
{
return 3306;
}

private function getTablePrefix()
{
return $this->getDbSettings()->getTablePrefix();
Expand Down
5 changes: 5 additions & 0 deletions core/Db/Schema/Tidb.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ public function supportsComplexColumnUpdates(): bool
{
return false;
}

public function getDefaultPort(): int
{
return 4000;
}
}
7 changes: 7 additions & 0 deletions core/Db/SchemaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,11 @@ public function addMaxExecutionTimeHintToQuery(string $sql, float $limit): strin
* @return bool
*/
public function supportsComplexColumnUpdates(): bool;

/**
* Return the default port used by this database engine
*
* @return int
*/
public function getDefaultPort(): int;
}
2 changes: 1 addition & 1 deletion plugins/Diagnostics/Diagnostic/DbAdapterCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private function checkDbAdapters()
$results = array();
$adapters = Adapter::getAdapters();

foreach ($adapters as $adapter => $port) {
foreach ($adapters as $adapter) {
$label = $adapter . ' ' . $this->translator->translate('Installation_Extension');

$results[] = DiagnosticResult::singleResult($label, DiagnosticResult::STATUS_OK);
Expand Down
51 changes: 38 additions & 13 deletions plugins/Installation/FormDatabaseSetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
*/
class FormDatabaseSetup extends QuickForm2
{
const MASKED_PASSWORD_VALUE = '**********';

function __construct($id = 'databasesetupform', $method = 'post', $attributes = null, $trackSubmit = false)
{
parent::__construct($id, $method, $attributes = array('autocomplete' => 'off'), $trackSubmit);
Expand All @@ -46,7 +48,7 @@ function init()

$availableAdapters = Adapter::getAdapters();
$adapters = array();
foreach ($availableAdapters as $adapter => $port) {
foreach ($availableAdapters as $adapter) {
$adapters[$adapter] = $adapter;
if (Adapter::isRecommendedAdapter($adapter)) {
$adapters[$adapter] .= ' (' . Piwik::translate('General_Recommended') . ')';
Expand Down Expand Up @@ -98,23 +100,39 @@ function init()
'type' => $defaultDatabaseType,
'tables_prefix' => 'matomo_',
'schema' => 'Mysql',
'port' => '3306',
);

$defaultsEnvironment = array('host', 'adapter', 'tables_prefix', 'username', 'schema', 'password', 'dbname');
foreach ($defaultsEnvironment as $name) {
$envName = 'DATABASE_' . strtoupper($name); // fyi getenv is case insensitive
$envNameMatomo = 'MATOMO_' . $envName;
if (getenv($envNameMatomo)) {
$defaults[$name] = getenv($envNameMatomo);
} elseif (getenv($envName)) {
$defaults[$name] = getenv($envName);
$envValue = $this->getEnvironmentSetting($name);

if (null !== $envValue) {
$defaults[$name] = $envValue;
}
}

if (array_key_exists('password', $defaults)) {
$defaults['password'] = self::MASKED_PASSWORD_VALUE; // ensure not to show password in UI
}

// default values
$this->addDataSource(new HTML_QuickForm2_DataSource_Array($defaults));
}

private function getEnvironmentSetting(string $name): ?string
{
$envName = 'DATABASE_' . strtoupper($name); // fyi getenv is case insensitive
$envNameMatomo = 'MATOMO_' . $envName;
if (is_string(getenv($envNameMatomo))) {
return getenv($envNameMatomo);
} elseif (is_string(getenv($envName))) {
return getenv($envName);
}

return null;
}

/**
* Creates database object based on form data.
*
Expand All @@ -130,20 +148,27 @@ public function createDatabaseObject()
}

$adapter = $this->getSubmitValue('adapter');
$port = Adapter::getDefaultPortForAdapter($adapter);

$host = $this->getSubmitValue('host');
$tables_prefix = $this->getSubmitValue('tables_prefix');


$password = $this->getSubmitValue('password');
$passwordFromEnv = $this->getEnvironmentSetting('password');

if ($password === self::MASKED_PASSWORD_VALUE && null !== $passwordFromEnv) {
$password = $passwordFromEnv;
}

$schema = $this->getSubmitValue('schema');

$dbInfos = array(
'host' => (is_null($host)) ? $host : trim($host),
'username' => $this->getSubmitValue('username'),
'password' => $this->getSubmitValue('password'),
'password' => $password,
'dbname' => $dbname,
'tables_prefix' => (is_null($tables_prefix)) ? $tables_prefix : trim($tables_prefix),
'adapter' => $adapter,
'port' => $port,
'schema' => $this->getSubmitValue('schema'),
'port' => Db\Schema::getDefaultPortForSchema($schema),
'schema' => $schema,
'type' => $this->getSubmitValue('type'),
'enable_ssl' => false
);
Expand Down

0 comments on commit dd3af29

Please sign in to comment.