Database
1 day ago
Database.php
1 day ago
DatabaseInterface.php
1 year ago
DateTimeAdapter.php
1 day ago
Directory.php
1 day ago
DirectoryInterface.php
1 year ago
Maintenance.php
1 day ago
PhpAdapter.php
1 day ago
SourceDatabase.php
1 day ago
WpAdapter.php
1 day ago
Database.php
267 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | namespace WPStaging\Framework\Adapter; |
| 10 | |
| 11 | use RuntimeException; |
| 12 | use wpdb; |
| 13 | use WPStaging\Core\WPStaging; |
| 14 | use WPStaging\Framework\Adapter\Database\MysqliAdapter; |
| 15 | use WPStaging\Framework\Adapter\Database\SqliteAdapter; |
| 16 | use WPStaging\Framework\Adapter\Database\WpDbAdapter; |
| 17 | use WPStaging\Framework\Adapter\Database\InterfaceDatabaseClient; |
| 18 | use SplObjectStorage; |
| 19 | use WP_SQLite_Translator; |
| 20 | |
| 21 | class Database implements DatabaseInterface |
| 22 | { |
| 23 | |
| 24 | private $client; |
| 25 | |
| 26 | |
| 27 | private $wpdba; |
| 28 | |
| 29 | |
| 30 | private $wpdb; |
| 31 | |
| 32 | |
| 33 | private $productionPrefix; |
| 34 | |
| 35 | |
| 36 | private $mysqlVersion; |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | public function __construct($wpDatabase = null) |
| 42 | { |
| 43 | $this->setWpDatabase($wpDatabase); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | public function setWpDatabase($wpDatabase = null) |
| 51 | { |
| 52 | global $wpdb; |
| 53 | $this->wpdb = $wpdb; |
| 54 | if ($wpDatabase !== null && $wpDatabase !== $wpdb) { |
| 55 | $this->wpdb = $wpDatabase; |
| 56 | } |
| 57 | |
| 58 | $this->mysqlVersion = null; |
| 59 | $this->productionPrefix = $wpdb->prefix; |
| 60 | $this->wpdba = new WpDbAdapter($this->wpdb); |
| 61 | $this->client = $this->findClient(); |
| 62 | } |
| 63 | |
| 64 | public function getClient(): InterfaceDatabaseClient |
| 65 | { |
| 66 | return $this->client; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | public function getWpdba() |
| 74 | { |
| 75 | return $this->wpdba; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | public function getPrefix(): string |
| 85 | { |
| 86 | if (WPStaging::isWindowsOs() || $this->getLowerTablesNameSettings() === '1') { |
| 87 | return strtolower($this->wpdb->prefix); |
| 88 | } |
| 89 | |
| 90 | return $this->wpdb->prefix; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | public function getBasePrefix(): string |
| 97 | { |
| 98 | return $this->wpdb->base_prefix; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | public function getProductionPrefix() |
| 105 | { |
| 106 | return $this->productionPrefix; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | public function isExternal() |
| 113 | { |
| 114 | return !($this->wpdb->__get('dbhost') === DB_HOST && $this->wpdb->__get('dbname') === DB_NAME); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | public function escapeSqlPrefixForLIKE($prefix = null) |
| 127 | { |
| 128 | if (!$prefix) { |
| 129 | $prefix = $this->getPrefix(); |
| 130 | } |
| 131 | |
| 132 | return str_replace('_', '\_', $prefix); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | public function getCharset() |
| 140 | { |
| 141 | return $this->wpdb->get_charset_collate(); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | public function exec($statement) |
| 149 | { |
| 150 | return $this->wpdba->exec($statement); |
| 151 | } |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | public function find($sql, array $conditions = []) |
| 160 | { |
| 161 | return $this->wpdba->find($sql, $conditions); |
| 162 | } |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | public function getSqlVersion(bool $compact = false, bool $refresh = false): string |
| 170 | { |
| 171 | if ($refresh || empty($this->mysqlVersion)) { |
| 172 | $this->mysqlVersion = $this->wpdb->get_var('SELECT VERSION()'); |
| 173 | } |
| 174 | |
| 175 | if (empty($this->mysqlVersion)) { |
| 176 | $this->mysqlVersion = $this->wpdb->db_version(); |
| 177 | } |
| 178 | |
| 179 | if (!$compact) { |
| 180 | return $this->mysqlVersion; |
| 181 | } |
| 182 | |
| 183 | return explode('-', explode(' ', explode('_', $this->mysqlVersion)[0])[0])[0]; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | public function getServerType() |
| 190 | { |
| 191 | $dbInfo = $this->getSqlVersion(); |
| 192 | if (strpos($dbInfo, 'maria')) { |
| 193 | return 'MariaDB'; |
| 194 | } |
| 195 | |
| 196 | if (strpos($dbInfo, 'percona')) { |
| 197 | return 'Percona'; |
| 198 | } |
| 199 | |
| 200 | return 'MySQL'; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | private function findClient() |
| 207 | { |
| 208 | $link = $this->wpdb->dbh; |
| 209 | if (!$link) { |
| 210 | throw new RuntimeException('Database handler / link is not set'); |
| 211 | } |
| 212 | |
| 213 | // @phpstan-ignore-next-line |
| 214 | if ($link instanceof WP_SQLite_Translator) { |
| 215 | return new SqliteAdapter($link); |
| 216 | } |
| 217 | |
| 218 | if (isset($this->wpdb->use_mysqli) && (bool)$this->wpdb->use_mysqli !== true) { |
| 219 | throw new RuntimeException('Use of mysql_* functions is not allowed'); |
| 220 | } |
| 221 | |
| 222 | return new MysqliAdapter($link); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | public function getWpdb() |
| 229 | { |
| 230 | return $this->wpdb; |
| 231 | } |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | public function getPrefixBySubsiteId(int $subsiteId): string |
| 238 | { |
| 239 | if ($subsiteId === 0 || $subsiteId === 1) { |
| 240 | return $this->getBasePrefix(); |
| 241 | } |
| 242 | |
| 243 | return $this->getBasePrefix() . $subsiteId . '_'; |
| 244 | } |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | public function getLowerTablesNameSettings(): string |
| 250 | { |
| 251 | $result = $this->getClient()->query("SHOW VARIABLES LIKE 'lower_case_table_names'"); |
| 252 | if (!$result) { |
| 253 | return 'N/A'; |
| 254 | } |
| 255 | |
| 256 | $resultRow = $this->getClient()->fetchAssoc($result); |
| 257 | |
| 258 | |
| 259 | if (!empty($resultRow) && isset($resultRow['Value'])) { |
| 260 | |
| 261 | return $resultRow['Value']; |
| 262 | } |
| 263 | |
| 264 | return 'N/A'; |
| 265 | } |
| 266 | } |
| 267 |