‪TYPO3CMS  10.4
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_STR')) {
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('/PostgreSQL ((\d+\.)*(\d+\.)*\d+)/', \PGSQL_LIBPQ_VERSION_STR, $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  [\PDO::PARAM_STR]
162  )
163  ->fetch();
164 
165  if (!in_array(strtolower($defaultDatabaseCharset['pg_encoding_to_char']), $this->databaseCharsetToCheck, true)) {
166  $this->messageQueue->enqueue(new ‪FlashMessage(
167  sprintf(
168  'Checking database character set failed, got key "%s" instead of "%s"',
169  $defaultDatabaseCharset,
170  implode(' or ', $this->databaseCharsetToCheck)
171  ),
172  'PostgreSQL database character set check failed',
174  ));
175  } else {
176  $this->messageQueue->enqueue(new ‪FlashMessage(
177  '',
178  sprintf('PostgreSQL database uses %s. All good.', implode(' or ', $this->databaseCharsetToCheck))
179  ));
180  }
181  }
182 
188  public function ‪checkDefaultDatabaseServerCharset(Connection $connection): void
189  {
190  $defaultServerCharset = $connection->executeQuery('SHOW SERVER_ENCODING')->fetch();
191 
192  if (!in_array(strtolower($defaultServerCharset['server_encoding']), $this->databaseCharsetToCheck, true)) {
193  $this->messageQueue->enqueue(new FlashMessage(
194  sprintf(
195  'Checking server character set failed, got key "%s" instead of "%s"',
196  $defaultServerCharset,
197  implode(' or ', $this->databaseServerCharsetToCheck)
198  ),
199  'PostgreSQL database character set check failed',
201  ));
202  } else {
203  $this->messageQueue->enqueue(new FlashMessage(
204  '',
205  sprintf('PostgreSQL server default uses %s. All good.', implode(' or ', $this->databaseCharsetToCheck))
206  ));
207  }
208  }
209 
216  public static function ‪isValidDatabaseName(string $databaseName): bool
217  {
218  return strlen($databaseName) <= static::SCHEMA_NAME_MAX_LENGTH && preg_match('/^(?!pg_)[a-zA-Z0-9\$_]*$/', $databaseName);
219  }
220 
221  protected function ‪checkDatabaseName(‪Connection $connection): void
222  {
223  if (static::isValidDatabaseName($connection->getDatabase())) {
224  return;
225  }
226 
227  $this->messageQueue->enqueue(
228  new ‪FlashMessage(
229  'The given database name must not be longer than ' . static::SCHEMA_NAME_MAX_LENGTH . ' characters'
230  . ' and consist solely of basic latin letters (a-z), digits (0-9), dollar signs ($)'
231  . ' and underscores (_) and does not start with "pg_".',
232  'Database name not valid',
234  )
235  );
236  }
237 }
‪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:217
‪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:212
‪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:184
‪TYPO3\CMS\Install\SystemEnvironment\DatabaseCheck\Platform\AbstractPlatform\$messageQueue
‪FlashMessageQueue $messageQueue
Definition: AbstractPlatform.php:31
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:36
‪TYPO3\CMS\Core\Messaging\AbstractMessage\INFO
‪const INFO
Definition: AbstractMessage.php:28
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:24
‪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:46
‪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