Introduce a "DatabaseException" class for fatal exceptions (used in testmode to throw...
authorPhilipp <admin@philipp.info>
Tue, 6 Oct 2020 18:03:38 +0000 (20:03 +0200)
committerPhilipp <admin@philipp.info>
Tue, 6 Oct 2020 18:03:38 +0000 (20:03 +0200)
src/Database/Database.php
src/Database/DatabaseException.php [new file with mode: 0644]

index 80fd02d..e25323b 100644 (file)
 
 namespace Friendica\Database;
 
-use Exception;
 use Friendica\Core\Config\Cache;
 use Friendica\Core\System;
-use Friendica\DI;
 use Friendica\Network\HTTPException\InternalServerErrorException;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Profiler;
@@ -75,7 +73,6 @@ class Database
                $this->profiler      = $profiler;
                $this->logger        = $logger;
 
-               $this->readServerVariables($server);
                $this->connect();
 
                if ($this->isConnected()) {
@@ -84,30 +81,6 @@ class Database
                }
        }
 
-       private function readServerVariables(array $server)
-       {
-               // Use environment variables for mysql if they are set beforehand
-               if (!empty($server['MYSQL_HOST'])
-                   && (!empty($server['MYSQL_USERNAME']) || !empty($server['MYSQL_USER']))
-                   && $server['MYSQL_PASSWORD'] !== false
-                   && !empty($server['MYSQL_DATABASE']))
-               {
-                       $db_host = $server['MYSQL_HOST'];
-                       if (!empty($server['MYSQL_PORT'])) {
-                               $db_host .= ':' . $server['MYSQL_PORT'];
-                       }
-                       $this->configCache->set('database', 'hostname', $db_host);
-                       unset($db_host);
-                       if (!empty($server['MYSQL_USERNAME'])) {
-                               $this->configCache->set('database', 'username', $server['MYSQL_USERNAME']);
-                       } else {
-                               $this->configCache->set('database', 'username', $server['MYSQL_USER']);
-                       }
-                       $this->configCache->set('database', 'password', (string) $server['MYSQL_PASSWORD']);
-                       $this->configCache->set('database', 'database', $server['MYSQL_DATABASE']);
-               }
-       }
-
        public function connect()
        {
                if (!is_null($this->connection) && $this->connected()) {
@@ -124,6 +97,11 @@ class Database
                if (count($serverdata) > 1) {
                        $port = trim($serverdata[1]);
                }
+
+               if (!empty(trim($this->configCache->get('database', 'port')))) {
+                       $port = trim(trim($this->configCache->get('database', 'port')));
+               }
+
                $server  = trim($server);
                $user    = trim($this->configCache->get('database', 'username'));
                $pass    = trim($this->configCache->get('database', 'password'));
@@ -658,7 +636,7 @@ class Database
                        $errorno = $this->errorno;
 
                        if ($this->testmode) {
-                               throw new Exception(DI::l10n()->t('Database error %d "%s" at "%s"', $errorno, $error, $this->replaceParameters($sql, $args)));
+                               throw new DatabaseException($error, $errorno, $this->replaceParameters($sql, $args));
                        }
 
                        $this->logger->error('DB Error', [
@@ -761,7 +739,7 @@ class Database
                        $errorno = $this->errorno;
 
                        if ($this->testmode) {
-                               throw new Exception(DI::l10n()->t('Database error %d "%s" at "%s"', $errorno, $error, $this->replaceParameters($sql, $params)));
+                               throw new DatabaseException($error, $errorno, $this->replaceParameters($sql, $params));
                        }
 
                        $this->logger->error('DB Error', [
diff --git a/src/Database/DatabaseException.php b/src/Database/DatabaseException.php
new file mode 100644 (file)
index 0000000..8bf5d8a
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+declare(strict_types=1);
+
+namespace Friendica\Database;
+
+use Exception;
+use Throwable;
+
+/**
+ * A database fatal exception, which shouldn't occur
+ */
+class DatabaseException extends Exception
+{
+       protected $query;
+
+       /**
+        * Construct the exception. Note: The message is NOT binary safe.
+        *
+        * @link https://php.net/manual/en/exception.construct.php
+        *
+        * @param string    $message  The Database error message.
+        * @param int       $code     The Database error code.
+        * @param string    $query    The Database error query.
+        * @param Throwable $previous [optional] The previous throwable used for the exception chaining.
+        */
+       public function __construct(string $message, int $code, string $query, Throwable $previous = null)
+       {
+               parent::__construct($message, $code, $previous);
+               $this->query = $query;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public function __toString()
+       {
+               return sprintf('Database error %d "%s" at "%s"', $this->message, $this->code, $this->query);
+       }
+}