PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.8.6
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.8.6
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 / Adapter / Database.php
wp-staging / Framework / Adapter Last commit date
Database 1 year ago Database.php 1 year ago DatabaseInterface.php 1 year ago DateTimeAdapter.php 1 year ago Directory.php 1 year ago DirectoryInterface.php 1 year ago Maintenance.php 4 years ago PhpAdapter.php 2 years ago SourceDatabase.php 2 years ago WpAdapter.php 3 years ago
Database.php
230 lines
1 <?php
2
3 /**
4 * IMPORTANT use only named queries or WP queries
5 */
6
7 /** @noinspection PhpUndefinedClassInspection */
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\WpDbAdapter;
16 use WPStaging\Framework\Adapter\Database\InterfaceDatabaseClient;
17 use SplObjectStorage;
18
19 class Database implements DatabaseInterface
20 {
21 /** @var MysqliAdapter */
22 private $client;
23
24 /** @var WpDbAdapter */
25 private $wpdba;
26
27 /** @var wpdb */
28 private $wpdb;
29
30 /** @var string */
31 private $productionPrefix;
32
33 /** @var string|null */
34 private $mysqlVersion;
35
36 /**
37 * @param wpdb $wpDatabase
38 */
39 public function __construct($wpDatabase = null)
40 {
41 global $wpdb;
42 $this->wpdb = $wpdb;
43 if ($wpDatabase !== null && $wpDatabase !== $wpdb) {
44 $this->wpdb = $wpDatabase;
45 }
46
47 $this->mysqlVersion = null;
48 $this->productionPrefix = $wpdb->prefix;
49 $this->wpdba = new WpDbAdapter($this->wpdb);
50 $this->client = $this->findClient();
51 }
52
53 public function getClient(): InterfaceDatabaseClient
54 {
55 return $this->client;
56 }
57
58 /**
59 * @return WpDbAdapter
60 * @noinspection PhpUnused
61 */
62 public function getWpdba()
63 {
64 return $this->wpdba;
65 }
66
67 /**
68 * Will always return prefix in the lowercase for Windows.
69 * In *nix it will return prefix in whatever the case it was written in wp-config.
70 *
71 * @return string
72 */
73 public function getPrefix(): string
74 {
75 if (WPStaging::isWindowsOs()) {
76 return strtolower($this->wpdb->prefix);
77 }
78
79 return $this->wpdb->prefix;
80 }
81
82 /**
83 * @return string
84 */
85 public function getBasePrefix(): string
86 {
87 return $this->wpdb->base_prefix;
88 }
89
90 /**
91 * @return string
92 */
93 public function getProductionPrefix()
94 {
95 return $this->productionPrefix;
96 }
97
98 /**
99 * @return bool
100 */
101 public function isExternal()
102 {
103 return !($this->wpdb->__get('dbhost') === DB_HOST && $this->wpdb->__get('dbname') === DB_NAME);
104 }
105
106 /**
107 * Return escaped prefix to use it in sql queries like 'wp\_'
108 *
109 * _ is interpreted as a single-character wildcard when
110 * executed in a LIKE SQL statement.
111 *
112 * @param string|null $prefix
113 * @return string|string[]
114 */
115 public function escapeSqlPrefixForLIKE($prefix = null)
116 {
117 if (!$prefix) {
118 $prefix = $this->getPrefix();
119 }
120
121 return str_replace('_', '\_', $prefix);
122 }
123
124 /**
125 * @return string
126 * @noinspection PhpUnused
127 */
128 public function getCharset()
129 {
130 return $this->wpdb->get_charset_collate();
131 }
132
133 /**
134 * @param string $statement
135 * @return bool
136 */
137 public function exec($statement)
138 {
139 return $this->wpdba->exec($statement);
140 }
141
142 /**
143 * TODO: use client directly
144 * @param string $sql
145 * @param array $conditions
146 * @return SplObjectStorage|null
147 */
148 public function find($sql, array $conditions = [])
149 {
150 return $this->wpdba->find($sql, $conditions);
151 }
152
153 /**
154 * @param bool $compact if true return just the version with other info
155 * @param bool $refresh if true fetch info again from server
156 * @return string
157 */
158 public function getSqlVersion(bool $compact = false, bool $refresh = false): string
159 {
160 if ($refresh || empty($this->mysqlVersion)) {
161 $this->mysqlVersion = $this->wpdb->get_var('SELECT VERSION()');
162 }
163
164 if (empty($this->mysqlVersion)) {
165 $this->mysqlVersion = $this->wpdb->db_version();
166 }
167
168 if (!$compact) {
169 return $this->mysqlVersion;
170 }
171
172 return explode('-', explode(' ', explode('_', $this->mysqlVersion)[0])[0])[0];
173 }
174
175 /**
176 * @return string
177 */
178 public function getServerType()
179 {
180 $dbInfo = $this->getSqlVersion();
181 if (strpos($dbInfo, 'maria')) {
182 return 'MariaDB';
183 }
184
185 if (strpos($dbInfo, 'percona')) {
186 return 'Percona';
187 }
188
189 return 'MySQL';
190 }
191
192 /**
193 * @return MysqliAdapter
194 */
195 private function findClient()
196 {
197 $link = $this->wpdb->dbh;
198 if (!$link) {
199 throw new RuntimeException('Database handler / link is not set');
200 }
201
202 if (isset($this->wpdb->use_mysqli) && (bool)$this->wpdb->use_mysqli !== true) {
203 throw new RuntimeException('Use of mysql_* functions is not allowed');
204 }
205
206 return new MysqliAdapter($link);
207 }
208
209 /**
210 * @return wpdb
211 */
212 public function getWpdb()
213 {
214 return $this->wpdb;
215 }
216
217 /**
218 * @param int $subsiteId
219 * @return string
220 */
221 public function getPrefixBySubsiteId(int $subsiteId): string
222 {
223 if ($subsiteId === 0 || $subsiteId === 1) {
224 return $this->getBasePrefix();
225 }
226
227 return $this->getBasePrefix() . $subsiteId . '_';
228 }
229 }
230