‪TYPO3CMS  ‪main
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 
20 use Doctrine\DBAL\Platforms\PostgreSQLPlatform as DoctrinePostgreSQLPlatform;
27 
40 {
46  protected ‪$minimumPostgreSQLVerion = '10.0';
47 
52  protected ‪$minimumLibPQVersion = '10.0';
53 
58  protected ‪$databaseCharsetToCheck = [
59  'utf8',
60  ];
61 
67  'utf8',
68  ];
69 
76  public function ‪getStatus(): ‪FlashMessageQueue
77  {
78  $defaultConnection = GeneralUtility::makeInstance(ConnectionPool::class)
79  ->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME);
80  $platform = $defaultConnection->getDatabasePlatform();
81  if (!($platform instanceof DoctrinePostgreSQLPlatform)) {
83  }
84 
85  $this->‪checkPostgreSqlVersion($defaultConnection);
86  $this->‪checkLibpqVersion();
87  $this->‪checkDefaultDatabaseCharset($defaultConnection);
88  $this->‪checkDefaultDatabaseServerCharset($defaultConnection);
89  $this->‪checkDatabaseName($defaultConnection);
91  }
92 
98  protected function ‪checkPostgreSqlVersion(‪Connection $connection)
99  {
100  preg_match('/PostgreSQL ((\d+\.)*(\d+\.)*\d+)/', $connection->‪getPlatformServerVersion(), $match);
101  $currentPostgreSqlVersion = $match[1];
102  if (version_compare($currentPostgreSqlVersion, $this->minimumPostgreSQLVerion, '<')) {
103  $this->messageQueue->enqueue(new ‪FlashMessage(
104  'Your PostgreSQL version ' . $currentPostgreSqlVersion . ' is not supported. TYPO3 CMS does not run'
105  . ' with this version. The minimum supported PostgreSQL version is ' . $this->minimumPostgreSQLVerion,
106  'PostgreSQL Server version is unsupported',
107  ContextualFeedbackSeverity::ERROR
108  ));
109  } else {
110  $this->messageQueue->enqueue(new ‪FlashMessage(
111  '',
112  'PostgreSQL Server version is supported'
113  ));
114  }
115  }
116 
120  protected function ‪checkLibpqVersion()
121  {
122  if (!defined('PGSQL_LIBPQ_VERSION')) {
123  $this->messageQueue->enqueue(new ‪FlashMessage(
124  'It is not possible to retrieve your PostgreSQL libpq version. Please check the version'
125  . ' in the "phpinfo" area of the "System environment" module in the install tool manually.'
126  . ' This should be found in section "pdo_pgsql".'
127  . ' You should have at least the following version of PostgreSQL libpq installed: '
128  . $this->minimumLibPQVersion,
129  'PostgreSQL libpq version cannot be determined',
130  ContextualFeedbackSeverity::WARNING
131  ));
132  } else {
133  preg_match('/((\d+\.)*(\d+\.)*\d+)/', \PGSQL_LIBPQ_VERSION, $match);
134  $currentPostgreSqlLibpqVersion = $match[1];
135 
136  if (version_compare($currentPostgreSqlLibpqVersion, $this->minimumLibPQVersion, '<')) {
137  $this->messageQueue->enqueue(new ‪FlashMessage(
138  'Your PostgreSQL libpq version "' . $currentPostgreSqlLibpqVersion . '" is unsupported.'
139  . ' TYPO3 CMS does not run with this version. The minimum supported libpq version is '
140  . $this->minimumLibPQVersion,
141  'PostgreSQL libpq version is unsupported',
142  ContextualFeedbackSeverity::ERROR
143  ));
144  } else {
145  $this->messageQueue->enqueue(new ‪FlashMessage(
146  '',
147  'PostgreSQL libpq version is supported'
148  ));
149  }
150  }
151  }
152 
158  public function ‪checkDefaultDatabaseCharset(‪Connection $connection): void
159  {
160  $defaultDatabaseCharset = $connection->executeQuery(
161  'SELECT pg_catalog.pg_encoding_to_char(pg_database.encoding) from pg_database where datname = ?',
162  [$connection->getDatabase()],
163  [Connection::PARAM_STR]
164  )->fetchAssociative();
165 
166  if (!in_array(strtolower($defaultDatabaseCharset['pg_encoding_to_char']), $this->databaseCharsetToCheck, true)) {
167  $this->messageQueue->enqueue(new ‪FlashMessage(
168  sprintf(
169  'Checking database character set failed, got key "%s" instead of "%s"',
170  $defaultDatabaseCharset['pg_encoding_to_char'],
171  implode(' or ', $this->databaseCharsetToCheck)
172  ),
173  'PostgreSQL database character set check failed',
174  ContextualFeedbackSeverity::ERROR
175  ));
176  } else {
177  $this->messageQueue->enqueue(new ‪FlashMessage(
178  '',
179  sprintf('PostgreSQL database uses %s. All good.', implode(' or ', $this->databaseCharsetToCheck))
180  ));
181  }
182  }
183 
189  public function ‪checkDefaultDatabaseServerCharset(Connection $connection): void
190  {
191  $defaultServerCharset = $connection->executeQuery('SHOW SERVER_ENCODING')->fetchAssociative();
192 
193  if (!in_array(strtolower($defaultServerCharset['server_encoding']), $this->databaseCharsetToCheck, true)) {
194  $this->messageQueue->enqueue(new FlashMessage(
195  sprintf(
196  'Checking server character set failed, got key "%s" instead of "%s"',
197  $defaultServerCharset['server_encoding'],
198  implode(' or ', $this->databaseServerCharsetToCheck)
199  ),
200  'PostgreSQL database character set check failed',
201  ContextualFeedbackSeverity::INFO
202  ));
203  } else {
204  $this->messageQueue->enqueue(new FlashMessage(
205  '',
206  sprintf('PostgreSQL server default uses %s. All good.', implode(' or ', $this->databaseCharsetToCheck))
207  ));
208  }
209  }
210 
214  public static function ‪isValidDatabaseName(string $databaseName): bool
215  {
216  return strlen($databaseName) <= static::SCHEMA_NAME_MAX_LENGTH && preg_match('/^(?!pg_)[a-zA-Z0-9\$_]*$/', $databaseName);
217  }
218 
219  protected function ‪checkDatabaseName(‪Connection $connection): void
220  {
221  if (static::isValidDatabaseName((string)$connection->getDatabase())) {
222  return;
223  }
224 
225  $this->messageQueue->enqueue(
226  new ‪FlashMessage(
227  'The given database name must not be longer than ' . static::SCHEMA_NAME_MAX_LENGTH . ' characters'
228  . ' and consist solely of basic latin letters (a-z), digits (0-9), dollar signs ($)'
229  . ' and underscores (_) and does not start with "pg_".',
230  'Database name not valid',
231  ContextualFeedbackSeverity::ERROR
232  )
233  );
234  }
235 }
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\PostgreSql\checkLibpqVersion
‪checkLibpqVersion()
Definition: PostgreSql.php:116
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\PostgreSql\$databaseCharsetToCheck
‪array $databaseCharsetToCheck
Definition: PostgreSql.php:55
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\PostgreSql\checkDatabaseName
‪checkDatabaseName(Connection $connection)
Definition: PostgreSql.php:215
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\PostgreSql\$databaseServerCharsetToCheck
‪array $databaseServerCharsetToCheck
Definition: PostgreSql.php:62
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\AbstractPlatform
Definition: AbstractPlatform.php:29
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\PostgreSql\$minimumPostgreSQLVerion
‪string $minimumPostgreSQLVerion
Definition: PostgreSql.php:45
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\PostgreSql\$minimumLibPQVersion
‪string $minimumLibPQVersion
Definition: PostgreSql.php:50
‪TYPO3\CMS\Core\Type\ContextualFeedbackSeverity
‪ContextualFeedbackSeverity
Definition: ContextualFeedbackSeverity.php:25
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform
Definition: AbstractPlatform.php:18
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\PostgreSql\isValidDatabaseName
‪static isValidDatabaseName(string $databaseName)
Definition: PostgreSql.php:210
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\PostgreSql\getStatus
‪getStatus()
Definition: PostgreSql.php:72
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\PostgreSql
Definition: PostgreSql.php:40
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\PostgreSql\checkDefaultDatabaseServerCharset
‪checkDefaultDatabaseServerCharset(Connection $connection)
Definition: PostgreSql.php:185
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\AbstractPlatform\$messageQueue
‪FlashMessageQueue $messageQueue
Definition: AbstractPlatform.php:32
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:27
‪TYPO3\CMS\Core\Database\Connection\getPlatformServerVersion
‪getPlatformServerVersion()
Definition: Connection.php:361
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\PostgreSql\checkPostgreSqlVersion
‪checkPostgreSqlVersion(Connection $connection)
Definition: PostgreSql.php:94
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Messaging\FlashMessageQueue
Definition: FlashMessageQueue.php:29
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\PostgreSql\checkDefaultDatabaseCharset
‪checkDefaultDatabaseCharset(Connection $connection)
Definition: PostgreSql.php:154