PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / trunk
JetBackup – Backup, Restore & Migrate vtrunk
3.1.22.3 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.8.1 1.4.9 1.5.0 1.5.1 1.5.1.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.10 1.6.11 1.6.12 1.6.13 1.6.15 1.6.5.1 1.6.8.8 1.6.9 1.6.9.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7.5 2.0.8.7 2.0.9.11 2.0.9.14 2.0.9.15 2.0.9.6 2.0.9.7 2.0.9.9 3.1.10.7 3.1.11.1 3.1.12.3 3.1.13.4 3.1.14.17 3.1.15.4 3.1.16.1 3.1.17.5 3.1.18.10 3.1.18.8 3.1.18.9 3.1.19.8 3.1.20.3 3.1.21.3 3.1.7.9 3.1.9.2 trunk 1.1.90 1.1.91 1.2.0 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2
backup / src / JetBackup / Wordpress / MySQL.php
backup / src / JetBackup / Wordpress Last commit date
.htaccess 1 year ago Abilities.php 1 day ago Blog.php 1 year ago Helper.php 5 months ago Init.php 5 months ago Installer.php 7 months ago MySQL.php 1 year ago UI.php 4 months ago Update.php 1 year ago Wordpress.php 1 day ago index.html 1 year ago web.config 1 year ago
MySQL.php
199 lines
1 <?php
2
3 namespace JetBackup\Wordpress;
4
5 use Exception;
6 use JetBackup\Exception\DBException;
7 use wpdb;
8
9 if (!defined( '__JETBACKUP__')) die('Direct access is not allowed');
10
11 /**
12 * JetDB Class
13 * Wrapper for WordPress database operations using wpdb.
14 */
15 class MySQL {
16
17 private wpdb $wpdb;
18
19 public function __construct() {
20 global $wpdb;
21 $this->wpdb = $wpdb;
22 }
23
24 /**
25 * Get all tables with the WordPress table prefix.
26 *
27 * @return array List of table names.
28 * @throws DBException
29 */
30 public function listTables(): array {
31 try {
32 $like_pattern = $this->wpdb->esc_like($this->getPrefix()) . '%';
33 $tables = $this->query("SHOW TABLES LIKE %s", [$like_pattern], ARRAY_N);
34
35 $tableList = [];
36 foreach ($tables as $table) {
37 $tableList[] = $table[0];
38 }
39
40 return $tableList;
41 } catch (Exception $e) {
42 throw new DBException($e->getMessage());
43 }
44 }
45
46
47
48 /**
49 * @return string|null
50 * Check if the current database user has the required privileges to restore the database.
51 */
52 public function checkPrivileges(): ?string {
53 try {
54 $this->execRaw("CREATE TEMPORARY TABLE jetbackup_priv_test (id INT);");
55 $this->execRaw("DROP TEMPORARY TABLE jetbackup_priv_test;");
56 return null;
57 } catch (Exception $e) {
58 return 'Database permissions error: ' . $e->getMessage();
59 }
60 }
61
62
63 /**
64 * Get the WordPress table prefix.
65 *
66 * @return string Table prefix.
67 */
68 public function getPrefix(): string {
69 return $this->wpdb->prefix;
70 }
71
72 /**
73 * Execute a query and return the results.
74 *
75 * @param string $query SQL query string.
76 * @param array $params Parameters for SQL query.
77 * @param string $resultType Type of result (OBJECT, ARRAY_A, ARRAY_N).
78 *
79 * @throws DBException
80 */
81 public function query(string $query, array $params = [], string $resultType = OBJECT) {
82 try {
83 if (!empty($params)) {
84 $query = $this->wpdb->prepare($query, $params);
85 }
86 return $this->wpdb->get_results($query, $resultType);
87 } catch (Exception $e) {
88 throw new DBException($e->getMessage());
89 }
90 }
91
92 /**
93 * Execute an SQL query and return the number of rows affected.
94 *
95 * @param string $query SQL query string.
96 *
97 * @return int Number of rows affected.
98 * @throws DBException
99 */
100 public function exec( string $query): int {
101 try {
102 return $this->wpdb->query($this->escapeSql($query));
103 } catch (Exception $e) {
104 throw new DBException ($e->getMessage());
105 }
106 }
107
108 /**
109 * Execute a raw SQL query without preparation.
110 *
111 * @param string $query Raw SQL query string.
112 *
113 * @throws DBException
114 */
115 public function execRaw( string $query)
116 {
117 try {
118 return $this->wpdb->get_results($this->escapeSql($query));
119 } catch (Exception $e) {
120 throw new DBException ($e->getMessage());
121 }
122 }
123
124 /**
125 * Fetch a single row from the result set.
126 *
127 * @param string $query SQL query string.
128 * @param array $params Parameters for SQL query.
129 * @param string $resultType Type of result (OBJECT, ARRAY_A, ARRAY_N).
130 *
131 * @throws DBException
132 */
133 public function fetch( string $query, array $params = [], string $resultType = OBJECT)
134 {
135 try {
136 $preparedQuery = $this->wpdb->prepare($query, $params);
137 return $this->wpdb->get_row($preparedQuery, $resultType);
138 } catch (Exception $e) {
139 throw new DBException ($e->getMessage());
140 }
141 }
142
143 /**
144 * Get the ID of the last inserted row.
145 *
146 * @return int Last insert ID.
147 */
148 public function lastInsertId(): int {
149 return $this->wpdb->insert_id;
150 }
151
152 /**
153 * Get the last error occurred in database operations.
154 *
155 * @return string Last error message.
156 */
157 public function getLastError(): string {
158 return $this->wpdb->last_error;
159 }
160
161 /**
162 * Flush the cached results.
163 */
164 public function flush()
165 {
166 $this->wpdb->flush();
167 }
168
169 /**
170 * Escape a SQL string for safe use in queries.
171 *
172 * @param mixed $value The value to be escaped.
173 * @return array|string The escaped value.
174 */
175 public function escapeSql($value)
176 {
177 if (function_exists('esc_sql')) {
178 return esc_sql($value);
179 } else {
180 return $this->wpdb->_escape($value);
181 }
182 }
183
184 /**
185 * Clears all post revisions from the WordPress database.
186 *
187 * @return int The number of rows affected.
188 * @throws DBException
189 */
190 public function clearPostRevisions(): int {
191 try {
192 $query = "DELETE FROM {$this->wpdb->posts} WHERE post_type = %s";
193 return $this->exec($this->wpdb->prepare($query, ['revision']));
194 } catch (Exception $e) {
195 throw new DBException($e->getMessage());
196 }
197 }
198
199 }