-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdb-module.php
386 lines (346 loc) · 11.6 KB
/
db-module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<?php
/**
* WordPress DB Class
*
* Original code from {@link http://www.poradnik-webmastera.com/ Daniel Fruzynski (daniel@poradnik-webmastera.com)}
* Original code from {@link http://php.justinvincent.com Justin Vincent (justin@visunet.ie)}
*
* @package WordPress
* @subpackage Database
* @since 0.71
*/
/**
* WordPress Database Access Abstraction Object
*
* It is possible to replace this class with your own
* by setting the $wpdb global variable in wp-content/db.php
* file with your class. You can name it wpdb also, since
* this file will not be included, if the other file is
* available.
*
* @link http://codex.wordpress.org/Function_Reference/wpdb_Class
*
* @package WordPress
* @subpackage Database
* @since 0.71
* @final
*/
if ( ! class_exists( 'dbrc_wpdb' ) ) {
class dbrc_wpdb extends wpdb {
// --- DB Cache Start ---
/**
* Amount of all queries cached by DB Cache Reloaded made
*
* @var int
*/
var $num_cachequeries = 0;
/**
* Amount of DML queries
*
* @var int
*/
var $dbcr_num_dml_queries = 0;
/**
* True if caching is active, otherwise false
*
* @var bool
*/
var $dbcr_cacheable = true;
/**
* Array with DB Cache Reloaded config
*
* @var array
*/
var $dbcr_config = null;
/**
* DB Cache Reloaded helper
*
* @var object of pcache
*/
var $dbcr_cache = null;
/**
* True if DB Cache Reloaded should show error in admin section
*
* @var bool
*/
var $dbcr_show_error = false;
/**
* DB Cache Reloaded DB module version
*
* @var int
*/
var $dbcr_version = DBCR_DB_MODULE_VER;
// --- DB Cache End ---
/**
* Connects to the database server and selects a database
*
* PHP4 compatibility layer for calling the PHP5 constructor.
*
* @uses wpdb::__construct() Passes parameters and returns result
* @since 0.71
*
* @param string $dbuser MySQL database user
* @param string $dbpassword MySQL database password
* @param string $dbname MySQL database name
* @param string $dbhost MySQL database host
*/
function wpdb( $dbuser, $dbpassword, $dbname, $dbhost ) {
if ( defined( 'WP_USE_MULTIPLE_DB' ) && WP_USE_MULTIPLE_DB ) {
$this->db_connect();
}
return $this->__construct( $dbuser, $dbpassword, $dbname, $dbhost );
}
/**
* Connects to the database server and selects a database
*
* PHP5 style constructor for compatibility with PHP5. Does
* the actual setting up of the class properties and connection
* to the database.
*
* @link http://core.trac.wordpress.org/ticket/3354
* @since 2.0.8
*
* @param string $dbuser MySQL database user
* @param string $dbpassword MySQL database password
* @param string $dbname MySQL database name
* @param string $dbhost MySQL database host
*/
function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) {
register_shutdown_function( array( $this, '__destruct' ) );
if ( WP_DEBUG ) {
$this->show_errors();
}
$this->init_charset();
$this->dbuser = $dbuser;
$this->dbpassword = $dbpassword;
$this->dbname = $dbname;
$this->dbhost = $dbhost;
$this->db_connect();
// --- DB Cache Start ---
// Caching
// require_once would be better, but some people deletes plugin without deactivating it first
if ( @include_once( DBCR_PATH . '/db-functions.php' ) ) {
$this->dbcr_config = unserialize( @file_get_contents( WP_CONTENT_DIR . '/db-config.ini' ) );
$this->dbcr_config['debug'] = false; //TODO: put this into option page
$this->dbcr_cache = new pcache();
$this->dbcr_cache->lifetime = isset( $this->dbcr_config['timeout'] ) ? $this->dbcr_config['timeout'] : 1800;
$this->dbcr_cache->lifetime *= 60; //convert to seconds
// cache only frontside
if (
( defined( 'WP_ADMIN' ) && WP_ADMIN ) ||
( defined( 'DOING_CRON' ) && DOING_CRON ) ||
( defined( 'DOING_AJAX' ) && DOING_AJAX ) ||
strpos( $_SERVER['REQUEST_URI'], 'wp-admin' ) ||
strpos( $_SERVER['REQUEST_URI'], 'wp-login' ) ||
strpos( $_SERVER['REQUEST_URI'], 'wp-register' ) ||
strpos( $_SERVER['REQUEST_URI'], 'wp-signup' )
) {
$this->dbcr_cacheable = false;
}
} else { // Cannot include db-functions.php
$this->dbcr_cacheable = false;
$this->dbcr_show_error = true;
}
// --- DB Cache End ---
}
/**
* PHP5 style destructor and will run when database object is destroyed.
*
* @see wpdb::__construct()
* @since 2.0.8
* @return bool true
*/
function __destruct() {
return true;
}
/**
* Perform a MySQL database query, using current database connection.
*
* More information can be found on the codex page.
*
* @since 0.71
*
* @param string $query Database query
*
* @return int|false Number of rows affected/selected or false on error
*/
function query( $query ) {
return $this->dbcr_query( $query, true );
}
function dbcr_query( $query, $maybe_cache = true ) {
if ( ! $this->ready ) {
return false;
}
// some queries are made before the plugins have been loaded, and thus cannot be filtered with this method
if ( function_exists( 'apply_filters' ) ) {
$query = apply_filters( 'query', $query );
}
$return_val = 0;
$this->flush();
// Log how the function was called
$this->func_call = "\$db->query(\"$query\")";
// Keep track of the last query for debug..
$this->last_query = $query;
// Perform the query via std mysql_query function..
if ( ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) || ( defined( 'DBCR_SAVEQUERIES' ) && DBCR_SAVEQUERIES ) ) {
$this->timer_start();
}
// --- DB Cache Start ---
$dbcr_db = 'local';
// --- DB Cache End ---
// use $this->dbh for read ops, and $this->dbhwrite for write ops
// use $this->dbhglobal for gloal table ops
unset( $dbh );
if ( defined( 'WP_USE_MULTIPLE_DB' ) && WP_USE_MULTIPLE_DB ) {
if ( $this->blogs != '' && preg_match( "/(" . $this->blogs . "|" . $this->users . "|" . $this->usermeta . "|" . $this->site . "|" . $this->sitemeta . "|" . $this->sitecategories . ")/i", $query ) ) {
if ( false == isset( $this->dbhglobal ) ) {
$this->db_connect( $query );
}
$dbh = $this->dbhglobal;
$this->last_db_used = "global";
// --- DB Cache Start ---
$dbcr_db = 'global';
// --- DB Cache End ---
} elseif ( preg_match( "/^\\s*(alter table|create|insert|delete|update|replace) /i", $query ) ) {
if ( false == isset( $this->dbhwrite ) ) {
$this->db_connect( $query );
}
$dbh = $this->dbhwrite;
$this->last_db_used = "write";
} else {
$dbh = $this->dbh;
$this->last_db_used = "read";
}
} else {
$dbh = $this->dbh;
$this->last_db_used = "other/read";
// DB Cache Start
if ( $this->blogs != '' && preg_match( "/(" . $this->blogs . "|" . $this->users . "|" . $this->usermeta . "|" . $this->site . "|" . $this->sitemeta . "|" . $this->sitecategories . ")/i", $query ) ) {
$dbcr_db = 'global';
}
// DB Cache End
}
// Caching
$dbcr_cacheable = false;
// check if pcache object is in place
if ( ! is_null( $this->dbcr_cache ) ) {
$dbcr_cacheable = $this->dbcr_cacheable && $maybe_cache;
if ( $dbcr_cacheable ) {
// do not cache non-select queries
if ( preg_match( "/\\s*(insert|delete|update|replace|alter|SET NAMES|FOUND_ROWS|RAND)\\b/si", $query ) ) {
$dbcr_cacheable = false;
} elseif ( // For hard queries - skip them
//preg_match( "/\\s*(JOIN)/si", $query ) ||
// User-defined cache filters
( isset( $this->dbcr_config['filter'] ) && ( $this->dbcr_config['filter'] != '' ) &&
preg_match( "/\\s*(" . $this->dbcr_config['filter'] . ")/si", $query ) ) ) {
$dbcr_cacheable = false;
}
}
if ( $dbcr_cacheable ) {
$dbcr_queryid = md5( $query );
if ( strpos( $query, '_options' ) ) {
$this->dbcr_cache->set_storage( $dbcr_db, 'options' );
} elseif ( strpos( $query, '_links' ) ) {
$this->dbcr_cache->set_storage( $dbcr_db, 'links' );
} elseif ( strpos( $query, '_terms' ) ) {
$this->dbcr_cache->set_storage( $dbcr_db, 'terms' );
} elseif ( strpos( $query, '_user' ) ) {
$this->dbcr_cache->set_storage( $dbcr_db, 'users' );
} elseif ( strpos( $query, '_post' ) ) {
$this->dbcr_cache->set_storage( $dbcr_db, 'posts' );
} elseif ( strpos( $query, 'JOIN' ) ) {
$this->dbcr_cache->set_storage( $dbcr_db, 'joins' );
} else {
$this->dbcr_cache->set_storage( $dbcr_db, '' );
}
}
/* Debug part */
if ( isset( $this->dbcr_config['debug'] ) && $this->dbcr_config['debug'] ) {
if ( $dbcr_cacheable ) {
echo "\n<!-- cache: $query -->\n\n";
} else {
echo "\n<!-- mysql: $query -->\n\n";
}
}
} elseif ( $this->dbcr_show_error ) {
$this->dbcr_show_error = false;
add_action( 'admin_notices', array( $this, '_dbcr_admin_notice' ) );
}
$dbcr_cached = false;
if ( $dbcr_cacheable ) {
// Try to load cached query
$dbcr_cached = $this->dbcr_cache->load( $dbcr_queryid );
}
if ( $dbcr_cached !== false ) {
// Extract cached query
++ $this->num_cachequeries;
$dbcr_cached = unserialize( $dbcr_cached );
$this->last_error = '';
$this->last_query = $dbcr_cached['last_query'];
$this->last_result = $dbcr_cached['last_result'];
$this->col_info = $dbcr_cached['col_info'];
$this->num_rows = $dbcr_cached['num_rows'];
$return_val = $this->num_rows;
if ( defined( 'DBCR_SAVEQUERIES' ) && DBCR_SAVEQUERIES ) {
$this->queries[] = array( $query, $this->timer_stop(), $this->get_caller(), true );
}
} else {
// Cache not found or query is not cacheable, perform query as usual
// --- DB Cache End ---
$this->result = @mysql_query( $query, $dbh );
$this->num_queries ++;
if ( defined( 'DBCR_SAVEQUERIES' ) && DBCR_SAVEQUERIES ) {
$this->queries[] = array( $query, $this->timer_stop(), $this->get_caller(), false );
} elseif ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
$this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
}
}
// If there is an error then take note of it..
if ( $this->last_error = mysql_error( $dbh ) ) {
$this->print_error();
return false;
}
if ( preg_match( "/^\\s*(insert|delete|update|replace|alter) /i", $query ) ) {
$this->rows_affected = mysql_affected_rows( $dbh );
// Take note of the insert_id
if ( preg_match( "/^\\s*(insert|replace) /i", $query ) ) {
$this->insert_id = mysql_insert_id( $dbh );
}
// Return number of rows affected
$return_val = $this->rows_affected;
// --- DB Cache Start ---
++ $this->dbcr_num_dml_queries;
// --- DB Cache End ---
} else {
$i = 0;
while ( $i < @mysql_num_fields( $this->result ) ) {
$this->col_info[ $i ] = @mysql_fetch_field( $this->result );
$i ++;
}
$num_rows = 0;
while ( $row = @mysql_fetch_object( $this->result ) ) {
$this->last_result[ $num_rows ] = $row;
$num_rows ++;
}
@mysql_free_result( $this->result );
// Log number of rows the query returned
// and return number of rows selected
$this->num_rows = $num_rows;
$return_val = $num_rows;
// --- DB Cache Start ---
if ( $dbcr_cacheable && ( $dbcr_cached === false ) ) {
$dbcr_cached = serialize( array(
'last_query' => $this->last_query,
'last_result' => $this->last_result,
'col_info' => $this->col_info,
'num_rows' => $this->num_rows,
) );
$this->dbcr_cache->save( $dbcr_cached, $dbcr_queryid );
}
// --- DB Cache End ---
}
return $return_val;
}
}
}