PluginProbe
Migrate to WordPress.com / trunk
Migrate to WordPress.com vtrunk
6.72 6.65 trunk 5.72 5.81 5.88
wpcom-migration / wp_db.php

wp_db.php in Migrate to WordPress.com trunk, at wp_db.php

257 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared
3 if (!defined('ABSPATH')) exit;
4 if (!class_exists('WPCOMWPDb')) :
5
6 class WPCOMWPDb {
7 public function dbprefix() {
8 global $wpdb;
9 $prefix = $wpdb->base_prefix ? $wpdb->base_prefix : $wpdb->prefix;
10 return $prefix;
11 }
12
13 public function prepare($query, $args) {
14 global $wpdb;
15 return $wpdb->prepare($query, $args);
16 }
17
18 public function getSiteId() {
19 global $wpdb;
20 return $wpdb->siteid;
21 }
22
23 public function getResult($query, $obj = ARRAY_A) {
24 global $wpdb;
25 return $wpdb->get_results($query, $obj);
26 }
27
28 public function query($query) {
29 global $wpdb;
30 return $wpdb->query($query);
31 }
32
33 public function getVar($query, $col = 0, $row = 0) {
34 global $wpdb;
35 return $wpdb->get_var($query, $col, $row);
36 }
37
38 public function getCol($query, $col = 0) {
39 global $wpdb;
40 return $wpdb->get_col($query, $col);
41 }
42
43 public function getLastRowId($col_name, $table_name) {
44 $query = "SELECT MAX($col_name) AS last_id FROM $table_name";
45 $results = $this->getResult($query);
46 if (empty($results) || !is_array($results) || !is_array($results[0]) ||
47 !array_key_exists("last_id", $results[0])) {
48 return null;
49 }
50 $last_id = $results[0]['last_id'];
51 if ($last_id === null) {
52 return 0;
53 } else if (is_numeric($last_id) === true) {
54 return (int) $last_id;
55 }
56 }
57
58 public function tableName($table) {
59 return $table[0];
60 }
61
62 public function showTables() {
63 $tables = $this->getResult("SHOW TABLES", ARRAY_N);
64 return array_map(array($this, 'tableName'), $tables);
65 }
66
67
68 public function showTableStatus() {
69 return $this->getResult("SHOW TABLE STATUS");
70 }
71
72 public function tableKeys($table) {
73 return $this->getResult("SHOW KEYS FROM $table;");
74 }
75
76 public function showDbVariables($variable) {
77 $variables = $this->getResult("Show variables like '%$variable%' ;");
78 $result = array();
79 foreach ($variables as $variable) {
80 $result[$variable["Variable_name"]] = $variable["Value"];
81 }
82 return $result;
83 }
84
85 public function describeTable($table) {
86 return $this->getResult("DESCRIBE $table;");
87 }
88
89 public function showTableIndex($table) {
90 return $this->getResult("SHOW INDEX FROM $table");
91 }
92
93 public function checkTable($table, $type) {
94 return $this->getResult("CHECK TABLE $table $type;");
95 }
96
97 public function repairTable($table) {
98 return $this->getResult("REPAIR TABLE $table;");
99 }
100
101 public function showTableCreate($table) {
102 return $this->getVar("SHOW CREATE TABLE $table;", 1);
103 }
104
105 public function rowsCount($table) {
106 $count = $this->getVar("SELECT COUNT(*) FROM $table;");
107 return intval($count);
108 }
109
110 public function createTable($query, $name, $usedbdelta = false) {
111 $table = $this->getBVTable($name);
112 if (!$this->isTablePresent($table)) {
113 if ($usedbdelta) {
114 if (!function_exists('dbDelta'))
115 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
116 dbDelta($query);
117 } else {
118 $this->query($query);
119 }
120 }
121 return $this->isTablePresent($table);
122 }
123
124 public function createTables($tables, $usedbdelta = false) {
125 $result = array();
126 foreach ($tables as $table => $query) {
127 $result[$table] = $this->createTable($query, $table, $usedbdelta);
128 }
129 return $result;
130 }
131
132 public function alterBVTable($query, $name) {
133 $resp = false;
134 $table = $this->getBVTable($name);
135 if ($this->isTablePresent($table)) {
136 $resp = $this->query($query);
137 }
138 return $resp;
139 }
140
141 public function alterTables($tables) {
142 $result = array();
143 foreach ($tables as $table => $query) {
144 $result[$table] = $this->alterBVTable($query, $table);
145 }
146 return $result;
147 }
148
149 public function getTableContent($table, $fields = '*', $filter = '', $limit = 0, $offset = 0) {
150 $query = "SELECT $fields from $table $filter";
151 if ($limit > 0)
152 $query .= " LIMIT $limit";
153 if ($offset > 0)
154 $query .= " OFFSET $offset";
155 $rows = $this->getResult($query);
156 return $rows;
157 }
158
159 public function isTablePresent($table) {
160 return ($this->getVar("SHOW TABLES LIKE '$table'") === $table);
161 }
162
163 public function getCharsetCollate() {
164 global $wpdb;
165 return $wpdb->get_charset_collate();
166 }
167
168 public function getWPTable($name) {
169 return ($this->dbprefix() . $name);
170 }
171
172 public function getBVTable($name) {
173 return ($this->getWPTable("bv_" . $name));
174 }
175
176 public function truncateBVTable($name) {
177 $table = $this->getBVTable($name);
178 if ($this->isTablePresent($table)) {
179 return $this->query("TRUNCATE TABLE $table;");
180 } else {
181 return false;
182 }
183 }
184
185 public function deleteBVTableContent($name, $filter = "") {
186 $table = $this->getBVTable($name);
187 if ($this->isTablePresent($table)) {
188 return $this->query("DELETE FROM $table $filter;");
189 } else {
190 return false;
191 }
192 }
193
194 public function dropBVTable($name) {
195 $table = $this->getBVTable($name);
196 if ($this->isTablePresent($table)) {
197 $this->query("DROP TABLE IF EXISTS $table;");
198 }
199 return !$this->isTablePresent($table);
200 }
201
202 public function dropTables($tables) {
203 $result = array();
204 foreach ($tables as $table) {
205 $result[$table] = $this->dropBVTable($table);
206 }
207 return $result;
208 }
209
210 public function truncateTables($tables) {
211 $result = array();
212 foreach ($tables as $table) {
213 $result[$table] = $this->truncateBVTable($table);
214 }
215 return $result;
216 }
217
218 public function deleteRowsFromtable($name, $count = 1) {
219 $table = $this->getBVTable($name);
220 if ($this->isTablePresent($table)) {
221 return $this->getResult("DELETE FROM $table LIMIT $count;");
222 } else {
223 return false;
224 }
225 }
226
227 public function replaceIntoBVTable($name, $value) {
228 global $wpdb;
229 $table = $this->getBVTable($name);
230 return $wpdb->replace($table, $value);
231 }
232
233 public function insertIntoBVTable($name, $value) {
234 global $wpdb;
235 $table = $this->getBVTable($name);
236 return $wpdb->insert($table, $value);
237 }
238
239 public function tinfo($name) {
240 $result = array();
241 $table = $this->getBVTable($name);
242
243 $result['name'] = $table;
244
245 if ($this->isTablePresent($table)) {
246 $result['exists'] = true;
247 $result['createquery'] = $this->showTableCreate($table);
248 }
249
250 return $result;
251 }
252
253 public function getMysqlVersion() {
254 return $this->showDbVariables('version')['version'];
255 }
256 }
257 endif;