QueryBuilder
2 years ago
DbInfo.php
2 years ago
ExcludedTables.php
4 years ago
OptionPreservationHandler.php
2 years ago
SearchReplace.php
2 years ago
SelectedTables.php
2 years ago
TableDto.php
5 years ago
TableService.php
2 years ago
TablesRenamer.php
2 years ago
WpDbInfo.php
2 years ago
WpOptionsInfo.php
2 years ago
iDbInfo.php
2 years ago
WpDbInfo.php
135 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Database; |
| 4 | |
| 5 | use wpdb; |
| 6 | |
| 7 | class WpDbInfo implements iDbInfo |
| 8 | { |
| 9 | /** |
| 10 | * Default version to use when the version cannot be determined. |
| 11 | * @var int |
| 12 | */ |
| 13 | const DEFAULT_VERSION = -1; |
| 14 | |
| 15 | /** |
| 16 | * @var wpdb |
| 17 | */ |
| 18 | protected $wpdb; |
| 19 | |
| 20 | /** |
| 21 | * @param wpdb $wpdb |
| 22 | */ |
| 23 | public function __construct(wpdb $wpdb) |
| 24 | { |
| 25 | $this->wpdb = $wpdb; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Get the database default collation: collation_database |
| 30 | * @return string |
| 31 | */ |
| 32 | public function getDbCollation(): string |
| 33 | { |
| 34 | return $this->getVariableByName('collation_database'); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @return string |
| 39 | */ |
| 40 | public function getDbEngine(): string |
| 41 | { |
| 42 | return $this->getVariableByName('default_storage_engine'); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @return int |
| 47 | */ |
| 48 | public function getMySqlServerVersion(): int |
| 49 | { |
| 50 | if (!is_null($this->wpdb->dbh)) { |
| 51 | return $this->wpdb->dbh->server_version; |
| 52 | } |
| 53 | |
| 54 | $value = $this->wpdb->get_var("SELECT @@version"); |
| 55 | |
| 56 | return is_null($value) ? self::DEFAULT_VERSION : $this->versionToInt($value); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @return int |
| 61 | */ |
| 62 | public function getMySqlClientVersion(): int |
| 63 | { |
| 64 | if (!is_null($this->wpdb->dbh)) { |
| 65 | return $this->wpdb->dbh->client_version; |
| 66 | } |
| 67 | |
| 68 | return self::DEFAULT_VERSION; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @return string |
| 73 | */ |
| 74 | public function getServerIp(): string |
| 75 | { |
| 76 | return $this->getVariableByName('hostname'); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @return int |
| 81 | */ |
| 82 | public function getServerPort(): int |
| 83 | { |
| 84 | return (int)$this->getVariableByName('port'); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Return the server name and port as server:port |
| 89 | * @return string |
| 90 | */ |
| 91 | public function getServer(): string |
| 92 | { |
| 93 | return $this->getServerIp() . ':' . $this->getServerPort(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @return array |
| 98 | */ |
| 99 | public function toArray(): array |
| 100 | { |
| 101 | return [ |
| 102 | 'db_engine' => $this->getDbEngine(), |
| 103 | 'db_collation' => $this->getDbCollation(), |
| 104 | 'db_server_ver' => $this->getMySqlServerVersion(), |
| 105 | 'db_client_ver' => $this->getMySqlClientVersion() |
| 106 | ]; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Fetch the database variable value by name. |
| 111 | * @param string $varName |
| 112 | * @return string |
| 113 | */ |
| 114 | protected function getVariableByName(string $varName): string |
| 115 | { |
| 116 | $query = "SHOW VARIABLES WHERE Variable_name = '" . $varName . "';"; |
| 117 | $value = $this->wpdb->get_var($query, 1); |
| 118 | |
| 119 | return is_null($value) ? '' : $value; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Convert version string to integer. |
| 124 | * |
| 125 | * @param string $version MySQL server version |
| 126 | * @return int |
| 127 | */ |
| 128 | protected static function versionToInt(string $version): int |
| 129 | { |
| 130 | $match = explode('.', $version); |
| 131 | |
| 132 | return (int)sprintf('%d%02d%02d', $match[0], $match[1], intval($match[2])); |
| 133 | } |
| 134 | } |
| 135 |