‪TYPO3CMS  11.5
PostgreSql.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
25 
38 {
44  protected ‪$minimumPostgreSQLVerion = '9.2';
45 
50  protected ‪$minimumLibPQVersion = '9.0';
51 
56  protected ‪$databaseCharsetToCheck = [
57  'utf8',
58  ];
59 
65  'utf8',
66  ];
67 
75  public function ‪getStatus(): ‪FlashMessageQueue
76  {
77  $defaultConnection = GeneralUtility::makeInstance(ConnectionPool::class)
78  ->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME);
79  if (strpos($defaultConnection->getServerVersion(), 'PostgreSQL') !== 0) {
81  }
82 
83  $this->‪checkPostgreSqlVersion($defaultConnection);
84  $this->‪checkLibpqVersion();
85  $this->‪checkDefaultDatabaseCharset($defaultConnection);
86  $this->‪checkDefaultDatabaseServerCharset($defaultConnection);
87  $this->‪checkDatabaseName($defaultConnection);
89  }
90 
96  protected function ‪checkPostgreSqlVersion(‪Connection $connection)
97  {
98  preg_match('/PostgreSQL ((\d+\.)*(\d+\.)*\d+)/', $connection->‪getServerVersion(), $match);
99  $currentPostgreSqlVersion = $match[1];
100  if (version_compare($currentPostgreSqlVersion, $this->minimumPostgreSQLVerion, '<')) {
101  $this->messageQueue->enqueue(new ‪FlashMessage(
102  'Your PostgreSQL version ' . $currentPostgreSqlVersion . ' is not supported. TYPO3 CMS does not run'
103  . ' with this version. The minimum supported PostgreSQL version is ' . $this->minimumPostgreSQLVerion,
104  'PostgreSQL Server version is unsupported',
106  ));
107  } else {
108  $this->messageQueue->enqueue(new ‪FlashMessage(
109  '',
110  'PostgreSQL Server version is supported'
111  ));
112  }
113  }
114 
118  protected function ‪checkLibpqVersion()
119  {
120  if (!defined('PGSQL_LIBPQ_VERSION')) {
121  $this->messageQueue->enqueue(new ‪FlashMessage(
122  'It is not possible to retrieve your PostgreSQL libpq version. Please check the version'
123  . ' in the "phpinfo" area of the "System environment" module in the install tool manually.'
124  . ' This should be found in section "pdo_pgsql".'
125  . ' You should have at least the following version of PostgreSQL libpq installed: '
126  . $this->minimumLibPQVersion,
127  'PostgreSQL libpq version cannot be determined',
129  ));
130  } else {
131  preg_match('/((\d+\.)*(\d+\.)*\d+)/', \PGSQL_LIBPQ_VERSION, $match);
132  $currentPostgreSqlLibpqVersion = $match[1];
133 
134  if (version_compare($currentPostgreSqlLibpqVersion, $this->minimumLibPQVersion, '<')) {
135  $this->messageQueue->enqueue(new ‪FlashMessage(
136  'Your PostgreSQL libpq version "' . $currentPostgreSqlLibpqVersion . '" is unsupported.'
137  . ' TYPO3 CMS does not run with this version. The minimum supported libpq version is '
138  . $this->minimumLibPQVersion,
139  'PostgreSQL libpq version is unsupported',
141  ));
142  } else {
143  $this->messageQueue->enqueue(new ‪FlashMessage(
144  '',
145  'PostgreSQL libpq version is supported'
146  ));
147  }
148  }
149  }
150 
156  public function ‪checkDefaultDatabaseCharset(‪Connection $connection): void
157  {
158  $defaultDatabaseCharset = $connection->executeQuery(
159  'SELECT pg_catalog.pg_encoding_to_char(pg_database.encoding) from pg_database where datname = ?',
160  [$connection->getDatabase()],
161  [Connection::PARAM_STR]
162  )->fetchAssociative();
163 
164  if (!in_array(strtolower($defaultDatabaseCharset['pg_encoding_to_char']), $this->databaseCharsetToCheck, true)) {
165  $this->messageQueue->enqueue(new ‪FlashMessage(
166  sprintf(
167  'Checking database character set failed, got key "%s" instead of "%s"',
168  $defaultDatabaseCharset['pg_encoding_to_char'],
169  implode(' or ', $this->databaseCharsetToCheck)
170  ),
171  'PostgreSQL database character set check failed',
173  ));
174  } else {
175  $this->messageQueue->enqueue(new ‪FlashMessage(
176  '',
177  sprintf('PostgreSQL database uses %s. All good.', implode(' or ', $this->databaseCharsetToCheck))
178  ));
179  }
180  }
181 
187  public function ‪checkDefaultDatabaseServerCharset(Connection $connection): void
188  {
189  $defaultServerCharset = $connection->executeQuery('SHOW SERVER_ENCODING')->fetchAssociative();
190 
191  if (!in_array(strtolower($defaultServerCharset['server_encoding']), $this->databaseCharsetToCheck, true)) {
192  $this->messageQueue->enqueue(new FlashMessage(
193  sprintf(
194  'Checking server character set failed, got key "%s" instead of "%s"',
195  $defaultServerCharset['server_encoding'],
196  implode(' or ', $this->databaseServerCharsetToCheck)
197  ),
198  'PostgreSQL database character set check failed',
200  ));
201  } else {
202  $this->messageQueue->enqueue(new FlashMessage(
203  '',
204  sprintf('PostgreSQL server default uses %s. All good.', implode(' or ', $this->databaseCharsetToCheck))
205  ));
206  }
207  }
208 
215  public static function ‪isValidDatabaseName(string $databaseName): bool
216  {
217  return strlen($databaseName) <= static::SCHEMA_NAME_MAX_LENGTH && preg_match('/^(?!pg_)[a-zA-Z0-9\$_]*$/', $databaseName);
218  }
219 
220  protected function ‪checkDatabaseName(‪Connection $connection): void
221  {
222  if (static::isValidDatabaseName((string)$connection->getDatabase())) {
223  return;
224  }
225 
226  $this->messageQueue->enqueue(
227  new ‪FlashMessage(
228  'The given database name must not be longer than ' . static::SCHEMA_NAME_MAX_LENGTH . ' characters'
229  . ' and consist solely of basic latin letters (a-z), digits (0-9), dollar signs ($)'
230  . ' and underscores (_) and does not start with "pg_".',
231  'Database name not valid',
233  )
234  );
235  }
236 }
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\PostgreSql\checkLibpqVersion
‪checkLibpqVersion()
Definition: PostgreSql.php:114
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\PostgreSql\$databaseCharsetToCheck
‪array $databaseCharsetToCheck
Definition: PostgreSql.php:53
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\PostgreSql\checkDatabaseName
‪checkDatabaseName(Connection $connection)
Definition: PostgreSql.php:216
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\PostgreSql\$databaseServerCharsetToCheck
‪array $databaseServerCharsetToCheck
Definition: PostgreSql.php:60
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\AbstractPlatform
Definition: AbstractPlatform.php:28
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\PostgreSql\$minimumPostgreSQLVerion
‪string $minimumPostgreSQLVerion
Definition: PostgreSql.php:43
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\PostgreSql\$minimumLibPQVersion
‪string $minimumLibPQVersion
Definition: PostgreSql.php:48
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform
Definition: AbstractPlatform.php:18
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\PostgreSql\isValidDatabaseName
‪static bool isValidDatabaseName(string $databaseName)
Definition: PostgreSql.php:211
‪TYPO3\CMS\Core\Messaging\AbstractMessage\WARNING
‪const WARNING
Definition: AbstractMessage.php:30
‪TYPO3\CMS\Core\Database\Connection\getServerVersion
‪string getServerVersion()
Definition: Connection.php:383
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\PostgreSql
Definition: PostgreSql.php:38
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\PostgreSql\checkDefaultDatabaseServerCharset
‪checkDefaultDatabaseServerCharset(Connection $connection)
Definition: PostgreSql.php:183
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\AbstractPlatform\$messageQueue
‪FlashMessageQueue $messageQueue
Definition: AbstractPlatform.php:31
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:38
‪TYPO3\CMS\Core\Messaging\AbstractMessage\INFO
‪const INFO
Definition: AbstractMessage.php:28
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:26
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\PostgreSql\checkPostgreSqlVersion
‪checkPostgreSqlVersion(Connection $connection)
Definition: PostgreSql.php:92
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue
Definition: FlashMessageQueue.php:29
‪TYPO3\CMS\Core\Messaging\AbstractMessage\ERROR
‪const ERROR
Definition: AbstractMessage.php:31
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\PostgreSql\getStatus
‪FlashMessageQueue getStatus()
Definition: PostgreSql.php:71
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\PostgreSql\checkDefaultDatabaseCharset
‪checkDefaultDatabaseCharset(Connection $connection)
Definition: PostgreSql.php:152