PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.8.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.8.2
4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / Database / WpDbInfo.php
wp-staging / Framework / Database Last commit date
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