| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * WordPress DB Class |
|---|
| 4 | * |
|---|
| 5 | * Original code from {@link http://php.justinvincent.com Justin Vincent (justin@visunet.ie)} |
|---|
| 6 | * |
|---|
| 7 | * @package WordPress |
|---|
| 8 | * @subpackage Database |
|---|
| 9 | * @since 0.71 |
|---|
| 10 | */ |
|---|
| 11 | |
|---|
| 12 | /** |
|---|
| 13 | * @since 0.71 |
|---|
| 14 | */ |
|---|
| 15 | define( 'EZSQL_VERSION', 'WP1.25' ); |
|---|
| 16 | |
|---|
| 17 | /** |
|---|
| 18 | * @since 0.71 |
|---|
| 19 | */ |
|---|
| 20 | define( 'OBJECT', 'OBJECT', true ); |
|---|
| 21 | |
|---|
| 22 | /** |
|---|
| 23 | * @since 2.5.0 |
|---|
| 24 | */ |
|---|
| 25 | define( 'OBJECT_K', 'OBJECT_K' ); |
|---|
| 26 | |
|---|
| 27 | /** |
|---|
| 28 | * @since 0.71 |
|---|
| 29 | */ |
|---|
| 30 | define( 'ARRAY_A', 'ARRAY_A' ); |
|---|
| 31 | |
|---|
| 32 | /** |
|---|
| 33 | * @since 0.71 |
|---|
| 34 | */ |
|---|
| 35 | define( 'ARRAY_N', 'ARRAY_N' ); |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | * WordPress Database Access Abstraction Object |
|---|
| 39 | * |
|---|
| 40 | * It is possible to replace this class with your own |
|---|
| 41 | * by setting the $wpdb global variable in wp-content/db.php |
|---|
| 42 | * file to your class. The wpdb class will still be included, |
|---|
| 43 | * so you can extend it or simply use your own. |
|---|
| 44 | * |
|---|
| 45 | * @link http://codex.wordpress.org/Function_Reference/wpdb_Class |
|---|
| 46 | * |
|---|
| 47 | * @package WordPress |
|---|
| 48 | * @subpackage Database |
|---|
| 49 | * @since 0.71 |
|---|
| 50 | */ |
|---|
| 51 | class wpdb { |
|---|
| 52 | |
|---|
| 53 | /** |
|---|
| 54 | * Whether to show SQL/DB errors |
|---|
| 55 | * |
|---|
| 56 | * @since 0.71 |
|---|
| 57 | * @access private |
|---|
| 58 | * @var bool |
|---|
| 59 | */ |
|---|
| 60 | var $show_errors = false; |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * Whether to suppress errors during the DB bootstrapping. |
|---|
| 64 | * |
|---|
| 65 | * @access private |
|---|
| 66 | * @since 2.5.0 |
|---|
| 67 | * @var bool |
|---|
| 68 | */ |
|---|
| 69 | var $suppress_errors = false; |
|---|
| 70 | |
|---|
| 71 | /** |
|---|
| 72 | * The last error during query. |
|---|
| 73 | * |
|---|
| 74 | * @since 2.5.0 |
|---|
| 75 | * @var string |
|---|
| 76 | */ |
|---|
| 77 | var $last_error = ''; |
|---|
| 78 | |
|---|
| 79 | /** |
|---|
| 80 | * Amount of queries made |
|---|
| 81 | * |
|---|
| 82 | * @since 1.2.0 |
|---|
| 83 | * @access private |
|---|
| 84 | * @var int |
|---|
| 85 | */ |
|---|
| 86 | var $num_queries = 0; |
|---|
| 87 | |
|---|
| 88 | /** |
|---|
| 89 | * Count of rows returned by previous query |
|---|
| 90 | * |
|---|
| 91 | * @since 0.71 |
|---|
| 92 | * @access private |
|---|
| 93 | * @var int |
|---|
| 94 | */ |
|---|
| 95 | var $num_rows = 0; |
|---|
| 96 | |
|---|
| 97 | /** |
|---|
| 98 | * Count of affected rows by previous query |
|---|
| 99 | * |
|---|
| 100 | * @since 0.71 |
|---|
| 101 | * @access private |
|---|
| 102 | * @var int |
|---|
| 103 | */ |
|---|
| 104 | var $rows_affected = 0; |
|---|
| 105 | |
|---|
| 106 | /** |
|---|
| 107 | * The ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT). |
|---|
| 108 | * |
|---|
| 109 | * @since 0.71 |
|---|
| 110 | * @access public |
|---|
| 111 | * @var int |
|---|
| 112 | */ |
|---|
| 113 | var $insert_id = 0; |
|---|
| 114 | |
|---|
| 115 | /** |
|---|
| 116 | * Last query made |
|---|
| 117 | * |
|---|
| 118 | * @since 0.71 |
|---|
| 119 | * @access private |
|---|
| 120 | * @var array |
|---|
| 121 | */ |
|---|
| 122 | var $last_query; |
|---|
| 123 | |
|---|
| 124 | /** |
|---|
| 125 | * Results of the last query made |
|---|
| 126 | * |
|---|
| 127 | * @since 0.71 |
|---|
| 128 | * @access private |
|---|
| 129 | * @var array|null |
|---|
| 130 | */ |
|---|
| 131 | var $last_result; |
|---|
| 132 | |
|---|
| 133 | /** |
|---|
| 134 | * MySQL result, which is either a resource or boolean. |
|---|
| 135 | * |
|---|
| 136 | * @since 0.71 |
|---|
| 137 | * @access protected |
|---|
| 138 | * @var mixed |
|---|
| 139 | */ |
|---|
| 140 | protected $result; |
|---|
| 141 | |
|---|
| 142 | /** |
|---|
| 143 | * Saved info on the table column |
|---|
| 144 | * |
|---|
| 145 | * @since 0.71 |
|---|
| 146 | * @access protected |
|---|
| 147 | * @var array |
|---|
| 148 | */ |
|---|
| 149 | protected $col_info; |
|---|
| 150 | |
|---|
| 151 | /** |
|---|
| 152 | * Saved queries that were executed |
|---|
| 153 | * |
|---|
| 154 | * @since 1.5.0 |
|---|
| 155 | * @access private |
|---|
| 156 | * @var array |
|---|
| 157 | */ |
|---|
| 158 | var $queries; |
|---|
| 159 | |
|---|
| 160 | /** |
|---|
| 161 | * WordPress table prefix |
|---|
| 162 | * |
|---|
| 163 | * You can set this to have multiple WordPress installations |
|---|
| 164 | * in a single database. The second reason is for possible |
|---|
| 165 | * security precautions. |
|---|
| 166 | * |
|---|
| 167 | * @since 2.5.0 |
|---|
| 168 | * @access private |
|---|
| 169 | * @var string |
|---|
| 170 | */ |
|---|
| 171 | var $prefix = ''; |
|---|
| 172 | |
|---|
| 173 | /** |
|---|
| 174 | * Whether the database queries are ready to start executing. |
|---|
| 175 | * |
|---|
| 176 | * @since 2.3.2 |
|---|
| 177 | * @access private |
|---|
| 178 | * @var bool |
|---|
| 179 | */ |
|---|
| 180 | var $ready = false; |
|---|
| 181 | |
|---|
| 182 | /** |
|---|
| 183 | * {@internal Missing Description}} |
|---|
| 184 | * |
|---|
| 185 | * @since 3.0.0 |
|---|
| 186 | * @access public |
|---|
| 187 | * @var int |
|---|
| 188 | */ |
|---|
| 189 | var $blogid = 0; |
|---|
| 190 | |
|---|
| 191 | /** |
|---|
| 192 | * {@internal Missing Description}} |
|---|
| 193 | * |
|---|
| 194 | * @since 3.0.0 |
|---|
| 195 | * @access public |
|---|
| 196 | * @var int |
|---|
| 197 | */ |
|---|
| 198 | var $siteid = 0; |
|---|
| 199 | |
|---|
| 200 | /** |
|---|
| 201 | * List of WordPress per-blog tables |
|---|
| 202 | * |
|---|
| 203 | * @since 2.5.0 |
|---|
| 204 | * @access private |
|---|
| 205 | * @see wpdb::tables() |
|---|
| 206 | * @var array |
|---|
| 207 | */ |
|---|
| 208 | var $tables = array( 'posts', 'comments', 'links', 'options', 'postmeta', |
|---|
| 209 | 'terms', 'term_taxonomy', 'term_relationships', 'commentmeta' ); |
|---|
| 210 | |
|---|
| 211 | /** |
|---|
| 212 | * List of deprecated WordPress tables |
|---|
| 213 | * |
|---|
| 214 | * categories, post2cat, and link2cat were deprecated in 2.3.0, db version 5539 |
|---|
| 215 | * |
|---|
| 216 | * @since 2.9.0 |
|---|
| 217 | * @access private |
|---|
| 218 | * @see wpdb::tables() |
|---|
| 219 | * @var array |
|---|
| 220 | */ |
|---|
| 221 | var $old_tables = array( 'categories', 'post2cat', 'link2cat' ); |
|---|
| 222 | |
|---|
| 223 | /** |
|---|
| 224 | * List of WordPress global tables |
|---|
| 225 | * |
|---|
| 226 | * @since 3.0.0 |
|---|
| 227 | * @access private |
|---|
| 228 | * @see wpdb::tables() |
|---|
| 229 | * @var array |
|---|
| 230 | */ |
|---|
| 231 | var $global_tables = array( 'users', 'usermeta' ); |
|---|
| 232 | |
|---|
| 233 | /** |
|---|
| 234 | * List of Multisite global tables |
|---|
| 235 | * |
|---|
| 236 | * @since 3.0.0 |
|---|
| 237 | * @access private |
|---|
| 238 | * @see wpdb::tables() |
|---|
| 239 | * @var array |
|---|
| 240 | */ |
|---|
| 241 | var $ms_global_tables = array( 'blogs', 'signups', 'site', 'sitemeta', |
|---|
| 242 | 'sitecategories', 'registration_log', 'blog_versions' ); |
|---|
| 243 | |
|---|
| 244 | /** |
|---|
| 245 | * WordPress Comments table |
|---|
| 246 | * |
|---|
| 247 | * @since 1.5.0 |
|---|
| 248 | * @access public |
|---|
| 249 | * @var string |
|---|
| 250 | */ |
|---|
| 251 | var $comments; |
|---|
| 252 | |
|---|
| 253 | /** |
|---|
| 254 | * WordPress Comment Metadata table |
|---|
| 255 | * |
|---|
| 256 | * @since 2.9.0 |
|---|
| 257 | * @access public |
|---|
| 258 | * @var string |
|---|
| 259 | */ |
|---|
| 260 | var $commentmeta; |
|---|
| 261 | |
|---|
| 262 | /** |
|---|
| 263 | * WordPress Links table |
|---|
| 264 | * |
|---|
| 265 | * @since 1.5.0 |
|---|
| 266 | * @access public |
|---|
| 267 | * @var string |
|---|
| 268 | */ |
|---|
| 269 | var $links; |
|---|
| 270 | |
|---|
| 271 | /** |
|---|
| 272 | * WordPress Options table |
|---|
| 273 | * |
|---|
| 274 | * @since 1.5.0 |
|---|
| 275 | * @access public |
|---|
| 276 | * @var string |
|---|
| 277 | */ |
|---|
| 278 | var $options; |
|---|
| 279 | |
|---|
| 280 | /** |
|---|
| 281 | * WordPress Post Metadata table |
|---|
| 282 | * |
|---|
| 283 | * @since 1.5.0 |
|---|
| 284 | * @access public |
|---|
| 285 | * @var string |
|---|
| 286 | */ |
|---|
| 287 | var $postmeta; |
|---|
| 288 | |
|---|
| 289 | /** |
|---|
| 290 | * WordPress Posts table |
|---|
| 291 | * |
|---|
| 292 | * @since 1.5.0 |
|---|
| 293 | * @access public |
|---|
| 294 | * @var string |
|---|
| 295 | */ |
|---|
| 296 | var $posts; |
|---|
| 297 | |
|---|
| 298 | /** |
|---|
| 299 | * WordPress Terms table |
|---|
| 300 | * |
|---|
| 301 | * @since 2.3.0 |
|---|
| 302 | * @access public |
|---|
| 303 | * @var string |
|---|
| 304 | */ |
|---|
| 305 | var $terms; |
|---|
| 306 | |
|---|
| 307 | /** |
|---|
| 308 | * WordPress Term Relationships table |
|---|
| 309 | * |
|---|
| 310 | * @since 2.3.0 |
|---|
| 311 | * @access public |
|---|
| 312 | * @var string |
|---|
| 313 | */ |
|---|
| 314 | var $term_relationships; |
|---|
| 315 | |
|---|
| 316 | /** |
|---|
| 317 | * WordPress Term Taxonomy table |
|---|
| 318 | * |
|---|
| 319 | * @since 2.3.0 |
|---|
| 320 | * @access public |
|---|
| 321 | * @var string |
|---|
| 322 | */ |
|---|
| 323 | var $term_taxonomy; |
|---|
| 324 | |
|---|
| 325 | /* |
|---|
| 326 | * Global and Multisite tables |
|---|
| 327 | */ |
|---|
| 328 | |
|---|
| 329 | /** |
|---|
| 330 | * WordPress User Metadata table |
|---|
| 331 | * |
|---|
| 332 | * @since 2.3.0 |
|---|
| 333 | * @access public |
|---|
| 334 | * @var string |
|---|
| 335 | */ |
|---|
| 336 | var $usermeta; |
|---|
| 337 | |
|---|
| 338 | /** |
|---|
| 339 | * WordPress Users table |
|---|
| 340 | * |
|---|
| 341 | * @since 1.5.0 |
|---|
| 342 | * @access public |
|---|
| 343 | * @var string |
|---|
| 344 | */ |
|---|
| 345 | var $users; |
|---|
| 346 | |
|---|
| 347 | /** |
|---|
| 348 | * Multisite Blogs table |
|---|
| 349 | * |
|---|
| 350 | * @since 3.0.0 |
|---|
| 351 | * @access public |
|---|
| 352 | * @var string |
|---|
| 353 | */ |
|---|
| 354 | var $blogs; |
|---|
| 355 | |
|---|
| 356 | /** |
|---|
| 357 | * Multisite Blog Versions table |
|---|
| 358 | * |
|---|
| 359 | * @since 3.0.0 |
|---|
| 360 | * @access public |
|---|
| 361 | * @var string |
|---|
| 362 | */ |
|---|
| 363 | var $blog_versions; |
|---|
| 364 | |
|---|
| 365 | /** |
|---|
| 366 | * Multisite Registration Log table |
|---|
| 367 | * |
|---|
| 368 | * @since 3.0.0 |
|---|
| 369 | * @access public |
|---|
| 370 | * @var string |
|---|
| 371 | */ |
|---|
| 372 | var $registration_log; |
|---|
| 373 | |
|---|
| 374 | /** |
|---|
| 375 | * Multisite Signups table |
|---|
| 376 | * |
|---|
| 377 | * @since 3.0.0 |
|---|
| 378 | * @access public |
|---|
| 379 | * @var string |
|---|
| 380 | */ |
|---|
| 381 | var $signups; |
|---|
| 382 | |
|---|
| 383 | /** |
|---|
| 384 | * Multisite Sites table |
|---|
| 385 | * |
|---|
| 386 | * @since 3.0.0 |
|---|
| 387 | * @access public |
|---|
| 388 | * @var string |
|---|
| 389 | */ |
|---|
| 390 | var $site; |
|---|
| 391 | |
|---|
| 392 | /** |
|---|
| 393 | * Multisite Sitewide Terms table |
|---|
| 394 | * |
|---|
| 395 | * @since 3.0.0 |
|---|
| 396 | * @access public |
|---|
| 397 | * @var string |
|---|
| 398 | */ |
|---|
| 399 | var $sitecategories; |
|---|
| 400 | |
|---|
| 401 | /** |
|---|
| 402 | * Multisite Site Metadata table |
|---|
| 403 | * |
|---|
| 404 | * @since 3.0.0 |
|---|
| 405 | * @access public |
|---|
| 406 | * @var string |
|---|
| 407 | */ |
|---|
| 408 | var $sitemeta; |
|---|
| 409 | |
|---|
| 410 | /** |
|---|
| 411 | * Format specifiers for DB columns. Columns not listed here default to %s. Initialized during WP load. |
|---|
| 412 | * |
|---|
| 413 | * Keys are column names, values are format types: 'ID' => '%d' |
|---|
| 414 | * |
|---|
| 415 | * @since 2.8.0 |
|---|
| 416 | * @see wpdb::prepare() |
|---|
| 417 | * @see wpdb::insert() |
|---|
| 418 | * @see wpdb::update() |
|---|
| 419 | * @see wpdb::delete() |
|---|
| 420 | * @see wp_set_wpdb_vars() |
|---|
| 421 | * @access public |
|---|
| 422 | * @var array |
|---|
| 423 | */ |
|---|
| 424 | var $field_types = array(); |
|---|
| 425 | |
|---|
| 426 | /** |
|---|
| 427 | * Database table columns charset |
|---|
| 428 | * |
|---|
| 429 | * @since 2.2.0 |
|---|
| 430 | * @access public |
|---|
| 431 | * @var string |
|---|
| 432 | */ |
|---|
| 433 | var $charset; |
|---|
| 434 | |
|---|
| 435 | /** |
|---|
| 436 | * Database table columns collate |
|---|
| 437 | * |
|---|
| 438 | * @since 2.2.0 |
|---|
| 439 | * @access public |
|---|
| 440 | * @var string |
|---|
| 441 | */ |
|---|
| 442 | var $collate; |
|---|
| 443 | |
|---|
| 444 | /** |
|---|
| 445 | * Database Username |
|---|
| 446 | * |
|---|
| 447 | * @since 2.9.0 |
|---|
| 448 | * @access protected |
|---|
| 449 | * @var string |
|---|
| 450 | */ |
|---|
| 451 | protected $dbuser; |
|---|
| 452 | |
|---|
| 453 | /** |
|---|
| 454 | * Database Password |
|---|
| 455 | * |
|---|
| 456 | * @since 3.1.0 |
|---|
| 457 | * @access protected |
|---|
| 458 | * @var string |
|---|
| 459 | */ |
|---|
| 460 | protected $dbpassword; |
|---|
| 461 | |
|---|
| 462 | /** |
|---|
| 463 | * Database Name |
|---|
| 464 | * |
|---|
| 465 | * @since 3.1.0 |
|---|
| 466 | * @access protected |
|---|
| 467 | * @var string |
|---|
| 468 | */ |
|---|
| 469 | protected $dbname; |
|---|
| 470 | |
|---|
| 471 | /** |
|---|
| 472 | * Database Host |
|---|
| 473 | * |
|---|
| 474 | * @since 3.1.0 |
|---|
| 475 | * @access protected |
|---|
| 476 | * @var string |
|---|
| 477 | */ |
|---|
| 478 | protected $dbhost; |
|---|
| 479 | |
|---|
| 480 | /** |
|---|
| 481 | * Database Handle |
|---|
| 482 | * |
|---|
| 483 | * @since 0.71 |
|---|
| 484 | * @access protected |
|---|
| 485 | * @var string |
|---|
| 486 | */ |
|---|
| 487 | protected $dbh; |
|---|
| 488 | |
|---|
| 489 | /** |
|---|
| 490 | * A textual description of the last query/get_row/get_var call |
|---|
| 491 | * |
|---|
| 492 | * @since 3.0.0 |
|---|
| 493 | * @access public |
|---|
| 494 | * @var string |
|---|
| 495 | */ |
|---|
| 496 | var $func_call; |
|---|
| 497 | |
|---|
| 498 | /** |
|---|
| 499 | * Whether MySQL is used as the database engine. |
|---|
| 500 | * |
|---|
| 501 | * Set in WPDB::db_connect() to true, by default. This is used when checking |
|---|
| 502 | * against the required MySQL version for WordPress. Normally, a replacement |
|---|
| 503 | * database drop-in (db.php) will skip these checks, but setting this to true |
|---|
| 504 | * will force the checks to occur. |
|---|
| 505 | * |
|---|
| 506 | * @since 3.3.0 |
|---|
| 507 | * @access public |
|---|
| 508 | * @var bool |
|---|
| 509 | */ |
|---|
| 510 | public $is_mysql = null; |
|---|
| 511 | |
|---|
| 512 | /** |
|---|
| 513 | * Connects to the database server and selects a database |
|---|
| 514 | * |
|---|
| 515 | * PHP5 style constructor for compatibility with PHP5. Does |
|---|
| 516 | * the actual setting up of the class properties and connection |
|---|
| 517 | * to the database. |
|---|
| 518 | * |
|---|
| 519 | * @link http://core.trac.wordpress.org/ticket/3354 |
|---|
| 520 | * @since 2.0.8 |
|---|
| 521 | * |
|---|
| 522 | * @param string $dbuser MySQL database user |
|---|
| 523 | * @param string $dbpassword MySQL database password |
|---|
| 524 | * @param string $dbname MySQL database name |
|---|
| 525 | * @param string $dbhost MySQL database host |
|---|
| 526 | */ |
|---|
| 527 | function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) { |
|---|
| 528 | register_shutdown_function( array( $this, '__destruct' ) ); |
|---|
| 529 | |
|---|
| 530 | if ( WP_DEBUG && WP_DEBUG_DISPLAY ) |
|---|
| 531 | $this->show_errors(); |
|---|
| 532 | |
|---|
| 533 | $this->init_charset(); |
|---|
| 534 | |
|---|
| 535 | $this->dbuser = $dbuser; |
|---|
| 536 | $this->dbpassword = $dbpassword; |
|---|
| 537 | $this->dbname = $dbname; |
|---|
| 538 | $this->dbhost = $dbhost; |
|---|
| 539 | |
|---|
| 540 | $this->db_connect(); |
|---|
| 541 | } |
|---|
| 542 | |
|---|
| 543 | /** |
|---|
| 544 | * PHP5 style destructor and will run when database object is destroyed. |
|---|
| 545 | * |
|---|
| 546 | * @see wpdb::__construct() |
|---|
| 547 | * @since 2.0.8 |
|---|
| 548 | * @return bool true |
|---|
| 549 | */ |
|---|
| 550 | function __destruct() { |
|---|
| 551 | return true; |
|---|
| 552 | } |
|---|
| 553 | |
|---|
| 554 | /** |
|---|
| 555 | * PHP5 style magic getter, used to lazy-load expensive data. |
|---|
| 556 | * |
|---|
| 557 | * @since 3.5.0 |
|---|
| 558 | * |
|---|
| 559 | * @param string $name The private member to get, and optionally process |
|---|
| 560 | * @return mixed The private member |
|---|
| 561 | */ |
|---|
| 562 | function __get( $name ) { |
|---|
| 563 | if ( 'col_info' == $name ) |
|---|
| 564 | $this->load_col_info(); |
|---|
| 565 | |
|---|
| 566 | return $this->$name; |
|---|
| 567 | } |
|---|
| 568 | |
|---|
| 569 | /** |
|---|
| 570 | * Magic function, for backwards compatibility |
|---|
| 571 | * |
|---|
| 572 | * @since 3.5.0 |
|---|
| 573 | * |
|---|
| 574 | * @param string $name The private member to set |
|---|
| 575 | * @param mixed $value The value to set |
|---|
| 576 | */ |
|---|
| 577 | function __set( $name, $value ) { |
|---|
| 578 | $this->$name = $value; |
|---|
| 579 | } |
|---|
| 580 | |
|---|
| 581 | /** |
|---|
| 582 | * Magic function, for backwards compatibility |
|---|
| 583 | * |
|---|
| 584 | * @since 3.5.0 |
|---|
| 585 | * |
|---|
| 586 | * @param string $name The private member to check |
|---|
| 587 | * |
|---|
| 588 | * @return bool If the member is set or not |
|---|
| 589 | */ |
|---|
| 590 | function __isset( $name ) { |
|---|
| 591 | return isset( $this->$name ); |
|---|
| 592 | } |
|---|
| 593 | |
|---|
| 594 | /** |
|---|
| 595 | * Magic function, for backwards compatibility |
|---|
| 596 | * |
|---|
| 597 | * @since 3.5.0 |
|---|
| 598 | * |
|---|
| 599 | * @param string $name The private member to unset |
|---|
| 600 | */ |
|---|
| 601 | function __unset( $name ) { |
|---|
| 602 | unset( $this->$name ); |
|---|
| 603 | } |
|---|
| 604 | |
|---|
| 605 | /** |
|---|
| 606 | * Set $this->charset and $this->collate |
|---|
| 607 | * |
|---|
| 608 | * @since 3.1.0 |
|---|
| 609 | */ |
|---|
| 610 | function init_charset() { |
|---|
| 611 | if ( function_exists('is_multisite') && is_multisite() ) { |
|---|
| 612 | $this->charset = 'utf8'; |
|---|
| 613 | if ( defined( 'DB_COLLATE' ) && DB_COLLATE ) |
|---|
| 614 | $this->collate = DB_COLLATE; |
|---|
| 615 | else |
|---|
| 616 | $this->collate = 'utf8_general_ci'; |
|---|
| 617 | } elseif ( defined( 'DB_COLLATE' ) ) { |
|---|
| 618 | $this->collate = DB_COLLATE; |
|---|
| 619 | } |
|---|
| 620 | |
|---|
| 621 | if ( defined( 'DB_CHARSET' ) ) |
|---|
| 622 | $this->charset = DB_CHARSET; |
|---|
| 623 | } |
|---|
| 624 | |
|---|
| 625 | /** |
|---|
| 626 | * Sets the connection's character set. |
|---|
| 627 | * |
|---|
| 628 | * @since 3.1.0 |
|---|
| 629 | * |
|---|
| 630 | * @param resource $dbh The resource given by mysql_connect |
|---|
| 631 | * @param string $charset The character set (optional) |
|---|
| 632 | * @param string $collate The collation (optional) |
|---|
| 633 | */ |
|---|
| 634 | function set_charset( $dbh, $charset = null, $collate = null ) { |
|---|
| 635 | if ( ! isset( $charset ) ) |
|---|
| 636 | $charset = $this->charset; |
|---|
| 637 | if ( ! isset( $collate ) ) |
|---|
| 638 | $collate = $this->collate; |
|---|
| 639 | if ( $this->has_cap( 'collation' ) && ! empty( $charset ) ) { |
|---|
| 640 | if ( function_exists( 'mysql_set_charset' ) && $this->has_cap( 'set_charset' ) ) { |
|---|
| 641 | mysql_set_charset( $charset, $dbh ); |
|---|
| 642 | } else { |
|---|
| 643 | $query = $this->prepare( 'SET NAMES %s', $charset ); |
|---|
| 644 | if ( ! empty( $collate ) ) |
|---|
| 645 | $query .= $this->prepare( ' COLLATE %s', $collate ); |
|---|
| 646 | mysql_query( $query, $dbh ); |
|---|
| 647 | } |
|---|
| 648 | } |
|---|
| 649 | } |
|---|
| 650 | |
|---|
| 651 | /** |
|---|
| 652 | * Sets the table prefix for the WordPress tables. |
|---|
| 653 | * |
|---|
| 654 | * @since 2.5.0 |
|---|
| 655 | * |
|---|
| 656 | * @param string $prefix Alphanumeric name for the new prefix. |
|---|
| 657 | * @param bool $set_table_names Optional. Whether the table names, e.g. wpdb::$posts, should be updated or not. |
|---|
| 658 | * @return string|WP_Error Old prefix or WP_Error on error |
|---|
| 659 | */ |
|---|
| 660 | function set_prefix( $prefix, $set_table_names = true ) { |
|---|
| 661 | |
|---|
| 662 | if ( preg_match( '|[^a-z0-9_]|i', $prefix ) ) |
|---|
| 663 | return new WP_Error('invalid_db_prefix', 'Invalid database prefix' ); |
|---|
| 664 | |
|---|
| 665 | $old_prefix = is_multisite() ? '' : $prefix; |
|---|
| 666 | |
|---|
| 667 | if ( isset( $this->base_prefix ) ) |
|---|
| 668 | $old_prefix = $this->base_prefix; |
|---|
| 669 | |
|---|
| 670 | $this->base_prefix = $prefix; |
|---|
| 671 | |
|---|
| 672 | if ( $set_table_names ) { |
|---|
| 673 | foreach ( $this->tables( 'global' ) as $table => $prefixed_table ) |
|---|
| 674 | $this->$table = $prefixed_table; |
|---|
| 675 | |
|---|
| 676 | if ( is_multisite() && empty( $this->blogid ) ) |
|---|
| 677 | return $old_prefix; |
|---|
| 678 | |
|---|
| 679 | $this->prefix = $this->get_blog_prefix(); |
|---|
| 680 | |
|---|
| 681 | foreach ( $this->tables( 'blog' ) as $table => $prefixed_table ) |
|---|
| 682 | $this->$table = $prefixed_table; |
|---|
| 683 | |
|---|
| 684 | foreach ( $this->tables( 'old' ) as $table => $prefixed_table ) |
|---|
| 685 | $this->$table = $prefixed_table; |
|---|
| 686 | } |
|---|
| 687 | return $old_prefix; |
|---|
| 688 | } |
|---|
| 689 | |
|---|
| 690 | /** |
|---|
| 691 | * Sets blog id. |
|---|
| 692 | * |
|---|
| 693 | * @since 3.0.0 |
|---|
| 694 | * @access public |
|---|
| 695 | * @param int $blog_id |
|---|
| 696 | * @param int $site_id Optional. |
|---|
| 697 | * @return string previous blog id |
|---|
| 698 | */ |
|---|
| 699 | function set_blog_id( $blog_id, $site_id = 0 ) { |
|---|
| 700 | if ( ! empty( $site_id ) ) |
|---|
| 701 | $this->siteid = $site_id; |
|---|
| 702 | |
|---|
| 703 | $old_blog_id = $this->blogid; |
|---|
| 704 | $this->blogid = $blog_id; |
|---|
| 705 | |
|---|
| 706 | $this->prefix = $this->get_blog_prefix(); |
|---|
| 707 | |
|---|
| 708 | foreach ( $this->tables( 'blog' ) as $table => $prefixed_table ) |
|---|
| 709 | $this->$table = $prefixed_table; |
|---|
| 710 | |
|---|
| 711 | foreach ( $this->tables( 'old' ) as $table => $prefixed_table ) |
|---|
| 712 | $this->$table = $prefixed_table; |
|---|
| 713 | |
|---|
| 714 | return $old_blog_id; |
|---|
| 715 | } |
|---|
| 716 | |
|---|
| 717 | /** |
|---|
| 718 | * Gets blog prefix. |
|---|
| 719 | * |
|---|
| 720 | * @uses is_multisite() |
|---|
| 721 | * @since 3.0.0 |
|---|
| 722 | * @param int $blog_id Optional. |
|---|
| 723 | * @return string Blog prefix. |
|---|
| 724 | */ |
|---|
| 725 | function get_blog_prefix( $blog_id = null ) { |
|---|
| 726 | if ( is_multisite() ) { |
|---|
| 727 | if ( null === $blog_id ) |
|---|
| 728 | $blog_id = $this->blogid; |
|---|
| 729 | $blog_id = (int) $blog_id; |
|---|
| 730 | if ( defined( 'MULTISITE' ) && ( 0 == $blog_id || 1 == $blog_id ) ) |
|---|
| 731 | return $this->base_prefix; |
|---|
| 732 | else |
|---|
| 733 | return $this->base_prefix . $blog_id . '_'; |
|---|
| 734 | } else { |
|---|
| 735 | return $this->base_prefix; |
|---|
| 736 | } |
|---|
| 737 | } |
|---|
| 738 | |
|---|
| 739 | /** |
|---|
| 740 | * Returns an array of WordPress tables. |
|---|
| 741 | * |
|---|
| 742 | * Also allows for the CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE to |
|---|
| 743 | * override the WordPress users and usermeta tables that would otherwise |
|---|
| 744 | * be determined by the prefix. |
|---|
| 745 | * |
|---|
| 746 | * The scope argument can take one of the following: |
|---|
| 747 | * |
|---|
| 748 | * 'all' - returns 'all' and 'global' tables. No old tables are returned. |
|---|
| 749 | * 'blog' - returns the blog-level tables for the queried blog. |
|---|
| 750 | * 'global' - returns the global tables for the installation, returning multisite tables only if running multisite. |
|---|
| 751 | * 'ms_global' - returns the multisite global tables, regardless if current installation is multisite. |
|---|
| 752 | * 'old' - returns tables which are deprecated. |
|---|
| 753 | * |
|---|
| 754 | * @since 3.0.0 |
|---|
| 755 | * @uses wpdb::$tables |
|---|
| 756 | * @uses wpdb::$old_tables |
|---|
| 757 | * @uses wpdb::$global_tables |
|---|
| 758 | * @uses wpdb::$ms_global_tables |
|---|
| 759 | * @uses is_multisite() |
|---|
| 760 | * |
|---|
| 761 | * @param string $scope Optional. Can be all, global, ms_global, blog, or old tables. Defaults to all. |
|---|
| 762 | * @param bool $prefix Optional. Whether to include table prefixes. Default true. If blog |
|---|
| 763 | * prefix is requested, then the custom users and usermeta tables will be mapped. |
|---|
| 764 | * @param int $blog_id Optional. The blog_id to prefix. Defaults to wpdb::$blogid. Used only when prefix is requested. |
|---|
| 765 | * @return array Table names. When a prefix is requested, the key is the unprefixed table name. |
|---|
| 766 | */ |
|---|
| 767 | function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) { |
|---|
| 768 | switch ( $scope ) { |
|---|
| 769 | case 'all' : |
|---|
| 770 | $tables = array_merge( $this->global_tables, $this->tables ); |
|---|
| 771 | if ( is_multisite() ) |
|---|
| 772 | $tables = array_merge( $tables, $this->ms_global_tables ); |
|---|
| 773 | break; |
|---|
| 774 | case 'blog' : |
|---|
| 775 | $tables = $this->tables; |
|---|
| 776 | break; |
|---|
| 777 | case 'global' : |
|---|
| 778 | $tables = $this->global_tables; |
|---|
| 779 | if ( is_multisite() ) |
|---|
| 780 | $tables = array_merge( $tables, $this->ms_global_tables ); |
|---|
| 781 | break; |
|---|
| 782 | case 'ms_global' : |
|---|
| 783 | $tables = $this->ms_global_tables; |
|---|
| 784 | break; |
|---|
| 785 | case 'old' : |
|---|
| 786 | $tables = $this->old_tables; |
|---|
| 787 | break; |
|---|
| 788 | default : |
|---|
| 789 | return array(); |
|---|
| 790 | break; |
|---|
| 791 | } |
|---|
| 792 | |
|---|
| 793 | if ( $prefix ) { |
|---|
| 794 | if ( ! $blog_id ) |
|---|
| 795 | $blog_id = $this->blogid; |
|---|
| 796 | $blog_prefix = $this->get_blog_prefix( $blog_id ); |
|---|
| 797 | $base_prefix = $this->base_prefix; |
|---|
| 798 | $global_tables = array_merge( $this->global_tables, $this->ms_global_tables ); |
|---|
| 799 | foreach ( $tables as $k => $table ) { |
|---|
| 800 | if ( in_array( $table, $global_tables ) ) |
|---|
| 801 | $tables[ $table ] = $base_prefix . $table; |
|---|
| 802 | else |
|---|
| 803 | $tables[ $table ] = $blog_prefix . $table; |
|---|
| 804 | unset( $tables[ $k ] ); |
|---|
| 805 | } |
|---|
| 806 | |
|---|
| 807 | if ( isset( $tables['users'] ) && defined( 'CUSTOM_USER_TABLE' ) ) |
|---|
| 808 | $tables['users'] = CUSTOM_USER_TABLE; |
|---|
| 809 | |
|---|
| 810 | if ( isset( $tables['usermeta'] ) && defined( 'CUSTOM_USER_META_TABLE' ) ) |
|---|
| 811 | $tables['usermeta'] = CUSTOM_USER_META_TABLE; |
|---|
| 812 | } |
|---|
| 813 | |
|---|
| 814 | return $tables; |
|---|
| 815 | } |
|---|
| 816 | |
|---|
| 817 | /** |
|---|
| 818 | * Selects a database using the current database connection. |
|---|
| 819 | * |
|---|
| 820 | * The database name will be changed based on the current database |
|---|
| 821 | * connection. On failure, the execution will bail and display an DB error. |
|---|
| 822 | * |
|---|
| 823 | * @since 0.71 |
|---|
| 824 | * |
|---|
| 825 | * @param string $db MySQL database name |
|---|
| 826 | * @param resource $dbh Optional link identifier. |
|---|
| 827 | * @return null Always null. |
|---|
| 828 | */ |
|---|
| 829 | function select( $db, $dbh = null ) { |
|---|
| 830 | if ( is_null($dbh) ) |
|---|
| 831 | $dbh = $this->dbh; |
|---|
| 832 | |
|---|
| 833 | if ( !@mysql_select_db( $db, $dbh ) ) { |
|---|
| 834 | $this->ready = false; |
|---|
| 835 | wp_load_translations_early(); |
|---|
| 836 | $this->bail( sprintf( __( '<h1>Can’t select database</h1> |
|---|
| 837 | <p>We were able to connect to the database server (which means your username and password is okay) but not able to select the <code>%1$s</code> database.</p> |
|---|
| 838 | <ul> |
|---|
| 839 | <li>Are you sure it exists?</li> |
|---|
| 840 | <li>Does the user <code>%2$s</code> have permission to use the <code>%1$s</code> database?</li> |
|---|
| 841 | <li>On some systems the name of your database is prefixed with your username, so it would be like <code>username_%1$s</code>. Could that be the problem?</li> |
|---|
| 842 | </ul> |
|---|
| 843 | <p>If you don\'t know how to set up a database you should <strong>contact your host</strong>. If all else fails you may find help at the <a href="http://wordpress.org/support/">WordPress Support Forums</a>.</p>' ), htmlspecialchars( $db, ENT_QUOTES ), htmlspecialchars( $this->dbuser, ENT_QUOTES ) ), 'db_select_fail' ); |
|---|
| 844 | return; |
|---|
| 845 | } |
|---|
| 846 | } |
|---|
| 847 | |
|---|
| 848 | /** |
|---|
| 849 | * Do not use, deprecated. |
|---|
| 850 | * |
|---|
| 851 | * Use esc_sql() or wpdb::prepare() instead. |
|---|
| 852 | * |
|---|
| 853 | * @since 2.8.0 |
|---|
| 854 | * @deprecated 3.6.0 |
|---|
| 855 | * @see wpdb::prepare |
|---|
| 856 | * @see esc_sql() |
|---|
| 857 | * @access private |
|---|
| 858 | * |
|---|
| 859 | * @param string $string |
|---|
| 860 | * @return string |
|---|
| 861 | */ |
|---|
| 862 | function _weak_escape( $string ) { |
|---|
| 863 | if ( func_num_args() === 1 && function_exists( '_deprecated_function' ) ) |
|---|
| 864 | _deprecated_function( __METHOD__, '3.6', 'wpdb::prepare() or esc_sql()' ); |
|---|
| 865 | return addslashes( $string ); |
|---|
| 866 | } |
|---|
| 867 | |
|---|
| 868 | /** |
|---|
| 869 | * Real escape, using mysql_real_escape_string() |
|---|
| 870 | * |
|---|
| 871 | * @see mysql_real_escape_string() |
|---|
| 872 | * @since 2.8.0 |
|---|
| 873 | * @access private |
|---|
| 874 | * |
|---|
| 875 | * @param string $string to escape |
|---|
| 876 | * @return string escaped |
|---|
| 877 | */ |
|---|
| 878 | function _real_escape( $string ) { |
|---|
| 879 | if ( $this->dbh ) |
|---|
| 880 | return mysql_real_escape_string( $string, $this->dbh ); |
|---|
| 881 | |
|---|
| 882 | $class = get_class( $this ); |
|---|
| 883 | _doing_it_wrong( $class, "$class must set a database connection for use with escaping.", E_USER_NOTICE ); |
|---|
| 884 | return addslashes( $string ); |
|---|
| 885 | } |
|---|
| 886 | |
|---|
| 887 | /** |
|---|
| 888 | * Escape data. Works on arrays. |
|---|
| 889 | * |
|---|
| 890 | * @uses wpdb::_real_escape() |
|---|
| 891 | * @since 2.8.0 |
|---|
| 892 | * @access private |
|---|
| 893 | * |
|---|
| 894 | * @param string|array $data |
|---|
| 895 | * @return string|array escaped |
|---|
| 896 | */ |
|---|
| 897 | function _escape( $data ) { |
|---|
| 898 | if ( is_array( $data ) ) { |
|---|
| 899 | foreach ( $data as $k => $v ) { |
|---|
| 900 | if ( is_array($v) ) |
|---|
| 901 | $data[$k] = $this->_escape( $v ); |
|---|
| 902 | else |
|---|
| 903 | $data[$k] = $this->_real_escape( $v ); |
|---|
| 904 | } |
|---|
| 905 | } else { |
|---|
| 906 | $data = $this->_real_escape( $data ); |
|---|
| 907 | } |
|---|
| 908 | |
|---|
| 909 | return $data; |
|---|
| 910 | } |
|---|
| 911 | |
|---|
| 912 | /** |
|---|
| 913 | * Do not use, deprecated. |
|---|
| 914 | * |
|---|
| 915 | * Use esc_sql() or wpdb::prepare() instead. |
|---|
| 916 | * |
|---|
| 917 | * @since 0.71 |
|---|
| 918 | * @deprecated 3.6.0 |
|---|
| 919 | * @see wpdb::prepare() |
|---|
| 920 | * @see esc_sql() |
|---|
| 921 | * |
|---|
| 922 | * @param mixed $data |
|---|
| 923 | * @return mixed |
|---|
| 924 | */ |
|---|
| 925 | function escape( $data ) { |
|---|
| 926 | if ( func_num_args() === 1 && function_exists( '_deprecated_function' ) ) |
|---|
| 927 | _deprecated_function( __METHOD__, '3.6', 'wpdb::prepare() or esc_sql()' ); |
|---|
| 928 | if ( is_array( $data ) ) { |
|---|
| 929 | foreach ( $data as $k => $v ) { |
|---|
| 930 | if ( is_array( $v ) ) |
|---|
| 931 | $data[$k] = $this->escape( $v, 'recursive' ); |
|---|
| 932 | else |
|---|
| 933 | $data[$k] = $this->_weak_escape( $v, 'internal' ); |
|---|
| 934 | } |
|---|
| 935 | } else { |
|---|
| 936 | $data = $this->_weak_escape( $data, 'internal' ); |
|---|
| 937 | } |
|---|
| 938 | |
|---|
| 939 | return $data; |
|---|
| 940 | } |
|---|
| 941 | |
|---|
| 942 | /** |
|---|
| 943 | * Escapes content by reference for insertion into the database, for security |
|---|
| 944 | * |
|---|
| 945 | * @uses wpdb::_real_escape() |
|---|
| 946 | * @since 2.3.0 |
|---|
| 947 | * @param string $string to escape |
|---|
| 948 | * @return void |
|---|
| 949 | */ |
|---|
| 950 | function escape_by_ref( &$string ) { |
|---|
| 951 | if ( ! is_float( $string ) ) |
|---|
| 952 | $string = $this->_real_escape( $string ); |
|---|
| 953 | } |
|---|
| 954 | |
|---|
| 955 | /** |
|---|
| 956 | * Prepares a SQL query for safe execution. Uses sprintf()-like syntax. |
|---|
| 957 | * |
|---|
| 958 | * The following directives can be used in the query format string: |
|---|
| 959 | * %d (integer) |
|---|
| 960 | * %f (float) |
|---|
| 961 | * %s (string) |
|---|
| 962 | * %% (literal percentage sign - no argument needed) |
|---|
| 963 | * |
|---|
| 964 | * All of %d, %f, and %s are to be left unquoted in the query string and they need an argument passed for them. |
|---|
| 965 | * Literals (%) as parts of the query must be properly written as %%. |
|---|
| 966 | * |
|---|
| 967 | * This function only supports a small subset of the sprintf syntax; it only supports %d (integer), %f (float), and %s (string). |
|---|
| 968 | * Does not support sign, padding, alignment, width or precision specifiers. |
|---|
| 969 | * Does not support argument numbering/swapping. |
|---|
| 970 | * |
|---|
| 971 | * May be called like {@link http://php.net/sprintf sprintf()} or like {@link http://php.net/vsprintf vsprintf()}. |
|---|
| 972 | * |
|---|
| 973 | * Both %d and %s should be left unquoted in the query string. |
|---|
| 974 | * |
|---|
| 975 | * <code> |
|---|
| 976 | * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", 'foo', 1337 ) |
|---|
| 977 | * wpdb::prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' ); |
|---|
| 978 | * </code> |
|---|
| 979 | * |
|---|
| 980 | * @link http://php.net/sprintf Description of syntax. |
|---|
| 981 | * @since 2.3.0 |
|---|
| 982 | * |
|---|
| 983 | * @param string $query Query statement with sprintf()-like placeholders |
|---|
| 984 | * @param array|mixed $args The array of variables to substitute into the query's placeholders if being called like |
|---|
| 985 | * {@link http://php.net/vsprintf vsprintf()}, or the first variable to substitute into the query's placeholders if |
|---|
| 986 | * being called like {@link http://php.net/sprintf sprintf()}. |
|---|
| 987 | * @param mixed $args,... further variables to substitute into the query's placeholders if being called like |
|---|
| 988 | * {@link http://php.net/sprintf sprintf()}. |
|---|
| 989 | * @return null|false|string Sanitized query string, null if there is no query, false if there is an error and string |
|---|
| 990 | * if there was something to prepare |
|---|
| 991 | */ |
|---|
| 992 | function prepare( $query, $args ) { |
|---|
| 993 | if ( is_null( $query ) ) |
|---|
| 994 | return; |
|---|
| 995 | |
|---|
| 996 | $args = func_get_args(); |
|---|
| 997 | array_shift( $args ); |
|---|
| 998 | // If args were passed as an array (as in vsprintf), move them up |
|---|
| 999 | if ( isset( $args[0] ) && is_array($args[0]) ) |
|---|
| 1000 | $args = $args[0]; |
|---|
| 1001 | $query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it |
|---|
| 1002 | $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting |
|---|
| 1003 | $query = preg_replace( '|(?<!%)%f|' , '%F', $query ); // Force floats to be locale unaware |
|---|
| 1004 | $query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s |
|---|
| 1005 | array_walk( $args, array( $this, 'escape_by_ref' ) ); |
|---|
| 1006 | return @vsprintf( $query, $args ); |
|---|
| 1007 | } |
|---|
| 1008 | |
|---|
| 1009 | /** |
|---|
| 1010 | * Print SQL/DB error. |
|---|
| 1011 | * |
|---|
| 1012 | * @since 0.71 |
|---|
| 1013 | * @global array $EZSQL_ERROR Stores error information of query and error string |
|---|
| 1014 | * |
|---|
| 1015 | * @param string $str The error to display |
|---|
| 1016 | * @return bool False if the showing of errors is disabled. |
|---|
| 1017 | */ |
|---|
| 1018 | function print_error( $str = '' ) { |
|---|
| 1019 | global $EZSQL_ERROR; |
|---|
| 1020 | |
|---|
| 1021 | if ( !$str ) |
|---|
| 1022 | $str = mysql_error( $this->dbh ); |
|---|
| 1023 | $EZSQL_ERROR[] = array( 'query' => $this->last_query, 'error_str' => $str ); |
|---|
| 1024 | |
|---|
| 1025 | if ( $this->suppress_errors ) |
|---|
| 1026 | return false; |
|---|
| 1027 | |
|---|
| 1028 | wp_load_translations_early(); |
|---|
| 1029 | |
|---|
| 1030 | if ( $caller = $this->get_caller() ) |
|---|
| 1031 | $error_str = sprintf( __( 'WordPress database error %1$s for query %2$s made by %3$s' ), $str, $this->last_query, $caller ); |
|---|
| 1032 | else |
|---|
| 1033 | $error_str = sprintf( __( 'WordPress database error %1$s for query %2$s' ), $str, $this->last_query ); |
|---|
| 1034 | |
|---|
| 1035 | error_log( $error_str ); |
|---|
| 1036 | |
|---|
| 1037 | // Are we showing errors? |
|---|
| 1038 | if ( ! $this->show_errors ) |
|---|
| 1039 | return false; |
|---|
| 1040 | |
|---|
| 1041 | // If there is an error then take note of it |
|---|
| 1042 | if ( is_multisite() ) { |
|---|
| 1043 | $msg = "WordPress database error: [$str]\n{$this->last_query}\n"; |
|---|
| 1044 | if ( defined( 'ERRORLOGFILE' ) ) |
|---|
| 1045 | error_log( $msg, 3, ERRORLOGFILE ); |
|---|
| 1046 | if ( defined( 'DIEONDBERROR' ) ) |
|---|
| 1047 | wp_die( $msg ); |
|---|
| 1048 | } else { |
|---|
| 1049 | $str = htmlspecialchars( $str, ENT_QUOTES ); |
|---|
| 1050 | $query = htmlspecialchars( $this->last_query, ENT_QUOTES ); |
|---|
| 1051 | |
|---|
| 1052 | print "<div id='error'> |
|---|
| 1053 | <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br /> |
|---|
| 1054 | <code>$query</code></p> |
|---|
| 1055 | </div>"; |
|---|
| 1056 | } |
|---|
| 1057 | } |
|---|
| 1058 | |
|---|
| 1059 | /** |
|---|
| 1060 | * Enables showing of database errors. |
|---|
| 1061 | * |
|---|
| 1062 | * This function should be used only to enable showing of errors. |
|---|
| 1063 | * wpdb::hide_errors() should be used instead for hiding of errors. However, |
|---|
| 1064 | * this function can be used to enable and disable showing of database |
|---|
| 1065 | * errors. |
|---|
| 1066 | * |
|---|
| 1067 | * @since 0.71 |
|---|
| 1068 | * @see wpdb::hide_errors() |
|---|
| 1069 | * |
|---|
| 1070 | * @param bool $show Whether to show or hide errors |
|---|
| 1071 | * @return bool Old value for showing errors. |
|---|
| 1072 | */ |
|---|
| 1073 | function show_errors( $show = true ) { |
|---|
| 1074 | $errors = $this->show_errors; |
|---|
| 1075 | $this->show_errors = $show; |
|---|
| 1076 | return $errors; |
|---|
| 1077 | } |
|---|
| 1078 | |
|---|
| 1079 | /** |
|---|
| 1080 | * Disables showing of database errors. |
|---|
| 1081 | * |
|---|
| 1082 | * By default database errors are not shown. |
|---|
| 1083 | * |
|---|
| 1084 | * @since 0.71 |
|---|
| 1085 | * @see wpdb::show_errors() |
|---|
| 1086 | * |
|---|
| 1087 | * @return bool Whether showing of errors was active |
|---|
| 1088 | */ |
|---|
| 1089 | function hide_errors() { |
|---|
| 1090 | $show = $this->show_errors; |
|---|
| 1091 | $this->show_errors = false; |
|---|
| 1092 | return $show; |
|---|
| 1093 | } |
|---|
| 1094 | |
|---|
| 1095 | /** |
|---|
| 1096 | * Whether to suppress database errors. |
|---|
| 1097 | * |
|---|
| 1098 | * By default database errors are suppressed, with a simple |
|---|
| 1099 | * call to this function they can be enabled. |
|---|
| 1100 | * |
|---|
| 1101 | * @since 2.5.0 |
|---|
| 1102 | * @see wpdb::hide_errors() |
|---|
| 1103 | * @param bool $suppress Optional. New value. Defaults to true. |
|---|
| 1104 | * @return bool Old value |
|---|
| 1105 | */ |
|---|
| 1106 | function suppress_errors( $suppress = true ) { |
|---|
| 1107 | $errors = $this->suppress_errors; |
|---|
| 1108 | $this->suppress_errors = (bool) $suppress; |
|---|
| 1109 | return $errors; |
|---|
| 1110 | } |
|---|
| 1111 | |
|---|
| 1112 | /** |
|---|
| 1113 | * Kill cached query results. |
|---|
| 1114 | * |
|---|
| 1115 | * @since 0.71 |
|---|
| 1116 | * @return void |
|---|
| 1117 | */ |
|---|
| 1118 | function flush() { |
|---|
| 1119 | $this->last_result = array(); |
|---|
| 1120 | $this->col_info = null; |
|---|
| 1121 | $this->last_query = null; |
|---|
| 1122 | $this->rows_affected = $this->num_rows = 0; |
|---|
| 1123 | $this->last_error = ''; |
|---|
| 1124 | |
|---|
| 1125 | if ( is_resource( $this->result ) ) |
|---|
| 1126 | mysql_free_result( $this->result ); |
|---|
| 1127 | } |
|---|
| 1128 | |
|---|
| 1129 | /** |
|---|
| 1130 | * Connect to and select database |
|---|
| 1131 | * |
|---|
| 1132 | * @since 3.0.0 |
|---|
| 1133 | */ |
|---|
| 1134 | function db_connect() { |
|---|
| 1135 | |
|---|
| 1136 | $this->is_mysql = true; |
|---|
| 1137 | |
|---|
| 1138 | $new_link = defined( 'MYSQL_NEW_LINK' ) ? MYSQL_NEW_LINK : true; |
|---|
| 1139 | $client_flags = defined( 'MYSQL_CLIENT_FLAGS' ) ? MYSQL_CLIENT_FLAGS : 0; |
|---|
| 1140 | |
|---|
| 1141 | if ( WP_DEBUG ) { |
|---|
| 1142 | $this->dbh = mysqli_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); |
|---|
| 1143 | } else { |
|---|
| 1144 | $this->dbh = @mysqli_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); |
|---|
| 1145 | } |
|---|
| 1146 | |
|---|
| 1147 | if ( !$this->dbh ) { |
|---|
| 1148 | wp_load_translations_early(); |
|---|
| 1149 | $this->bail( sprintf( __( " |
|---|
| 1150 | <h1>Error establishing a database connection</h1> |
|---|
| 1151 | <p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can't contact the database server at <code>%s</code>. This could mean your host's database server is down.</p> |
|---|
| 1152 | <ul> |
|---|
| 1153 | <li>Are you sure you have the correct username and password?</li> |
|---|
| 1154 | <li>Are you sure that you have typed the correct hostname?</li> |
|---|
| 1155 | <li>Are you sure that the database server is running?</li> |
|---|
| 1156 | </ul> |
|---|
| 1157 | <p>If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href='http://wordpress.org/support/'>WordPress Support Forums</a>.</p> |
|---|
| 1158 | " ), htmlspecialchars( $this->dbhost, ENT_QUOTES ) ), 'db_connect_fail' ); |
|---|
| 1159 | |
|---|
| 1160 | return; |
|---|
| 1161 | } |
|---|
| 1162 | |
|---|
| 1163 | $this->set_charset( $this->dbh ); |
|---|
| 1164 | |
|---|
| 1165 | $this->ready = true; |
|---|
| 1166 | |
|---|
| 1167 | $this->select( $this->dbname, $this->dbh ); |
|---|
| 1168 | } |
|---|
| 1169 | |
|---|
| 1170 | /** |
|---|
| 1171 | * Perform a MySQL database query, using current database connection. |
|---|
| 1172 | * |
|---|
| 1173 | * More information can be found on the codex page. |
|---|
| 1174 | * |
|---|
| 1175 | * @since 0.71 |
|---|
| 1176 | * |
|---|
| 1177 | * @param string $query Database query |
|---|
| 1178 | * @return int|false Number of rows affected/selected or false on error |
|---|
| 1179 | */ |
|---|
| 1180 | function query( $query ) { |
|---|
| 1181 | if ( ! $this->ready ) |
|---|
| 1182 | return false; |
|---|
| 1183 | |
|---|
| 1184 | // some queries are made before the plugins have been loaded, and thus cannot be filtered with this method |
|---|
| 1185 | $query = apply_filters( 'query', $query ); |
|---|
| 1186 | |
|---|
| 1187 | $return_val = 0; |
|---|
| 1188 | $this->flush(); |
|---|
| 1189 | |
|---|
| 1190 | // Log how the function was called |
|---|
| 1191 | $this->func_call = "\$db->query(\"$query\")"; |
|---|
| 1192 | |
|---|
| 1193 | // Keep track of the last query for debug.. |
|---|
| 1194 | $this->last_query = $query; |
|---|
| 1195 | |
|---|
| 1196 | if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) |
|---|
| 1197 | $this->timer_start(); |
|---|
| 1198 | |
|---|
| 1199 | $this->result = @mysql_query( $query, $this->dbh ); |
|---|
| 1200 | $this->num_queries++; |
|---|
| 1201 | |
|---|
| 1202 | if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) |
|---|
| 1203 | $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); |
|---|
| 1204 | |
|---|
| 1205 | // If there is an error then take note of it.. |
|---|
| 1206 | if ( $this->last_error = mysql_error( $this->dbh ) ) { |
|---|
| 1207 | // Clear insert_id on a subsequent failed insert. |
|---|
| 1208 | if ( $this->insert_id && preg_match( '/^\s*(insert|replace)\s/i', $query ) ) |
|---|
| 1209 | $this->insert_id = 0; |
|---|
| 1210 | |
|---|
| 1211 | $this->print_error(); |
|---|
| 1212 | return false; |
|---|
| 1213 | } |
|---|
| 1214 | |
|---|
| 1215 | if ( preg_match( '/^\s*(create|alter|truncate|drop)\s/i', $query ) ) { |
|---|
| 1216 | $return_val = $this->result; |
|---|
| 1217 | } elseif ( preg_match( '/^\s*(insert|delete|update|replace)\s/i', $query ) ) { |
|---|
| 1218 | $this->rows_affected = mysql_affected_rows( $this->dbh ); |
|---|
| 1219 | // Take note of the insert_id |
|---|
| 1220 | if ( preg_match( '/^\s*(insert|replace)\s/i', $query ) ) { |
|---|
| 1221 | $this->insert_id = mysql_insert_id($this->dbh); |
|---|
| 1222 | } |
|---|
| 1223 | // Return number of rows affected |
|---|
| 1224 | $return_val = $this->rows_affected; |
|---|
| 1225 | } else { |
|---|
| 1226 | $num_rows = 0; |
|---|
| 1227 | while ( $row = @mysql_fetch_object( $this->result ) ) { |
|---|
| 1228 | $this->last_result[$num_rows] = $row; |
|---|
| 1229 | $num_rows++; |
|---|
| 1230 | } |
|---|
| 1231 | |
|---|
| 1232 | // Log number of rows the query returned |
|---|
| 1233 | // and return number of rows selected |
|---|
| 1234 | $this->num_rows = $num_rows; |
|---|
| 1235 | $return_val = $num_rows; |
|---|
| 1236 | } |
|---|
| 1237 | |
|---|
| 1238 | return $return_val; |
|---|
| 1239 | } |
|---|
| 1240 | |
|---|
| 1241 | /** |
|---|
| 1242 | * Insert a row into a table. |
|---|
| 1243 | * |
|---|
| 1244 | * <code> |
|---|
| 1245 | * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 'bar' ) ) |
|---|
| 1246 | * wpdb::insert( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) ) |
|---|
| 1247 | * </code> |
|---|
| 1248 | * |
|---|
| 1249 | * @since 2.5.0 |
|---|
| 1250 | * @see wpdb::prepare() |
|---|
| 1251 | * @see wpdb::$field_types |
|---|
| 1252 | * @see wp_set_wpdb_vars() |
|---|
| 1253 | * |
|---|
| 1254 | * @param string $table table name |
|---|
| 1255 | * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). |
|---|
| 1256 | * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data. |
|---|
| 1257 | * A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types. |
|---|
| 1258 | * @return int|false The number of rows inserted, or false on error. |
|---|
| 1259 | */ |
|---|
| 1260 | function insert( $table, $data, $format = null ) { |
|---|
| 1261 | return $this->_insert_replace_helper( $table, $data, $format, 'INSERT' ); |
|---|
| 1262 | } |
|---|
| 1263 | |
|---|
| 1264 | /** |
|---|
| 1265 | * Replace a row into a table. |
|---|
| 1266 | * |
|---|
| 1267 | * <code> |
|---|
| 1268 | * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 'bar' ) ) |
|---|
| 1269 | * wpdb::replace( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( '%s', '%d' ) ) |
|---|
| 1270 | * </code> |
|---|
| 1271 | * |
|---|
| 1272 | * @since 3.0.0 |
|---|
| 1273 | * @see wpdb::prepare() |
|---|
| 1274 | * @see wpdb::$field_types |
|---|
| 1275 | * @see wp_set_wpdb_vars() |
|---|
| 1276 | * |
|---|
| 1277 | * @param string $table table name |
|---|
| 1278 | * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). |
|---|
| 1279 | * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data. |
|---|
| 1280 | * A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types. |
|---|
| 1281 | * @return int|false The number of rows affected, or false on error. |
|---|
| 1282 | */ |
|---|
| 1283 | function replace( $table, $data, $format = null ) { |
|---|
| 1284 | return $this->_insert_replace_helper( $table, $data, $format, 'REPLACE' ); |
|---|
| 1285 | } |
|---|
| 1286 | |
|---|
| 1287 | /** |
|---|
| 1288 | * Helper function for insert and replace. |
|---|
| 1289 | * |
|---|
| 1290 | * Runs an insert or replace query based on $type argument. |
|---|
| 1291 | * |
|---|
| 1292 | * @access private |
|---|
| 1293 | * @since 3.0.0 |
|---|
| 1294 | * @see wpdb::prepare() |
|---|
| 1295 | * @see wpdb::$field_types |
|---|
| 1296 | * @see wp_set_wpdb_vars() |
|---|
| 1297 | * |
|---|
| 1298 | * @param string $table table name |
|---|
| 1299 | * @param array $data Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). |
|---|
| 1300 | * @param array|string $format Optional. An array of formats to be mapped to each of the value in $data. If string, that format will be used for all of the values in $data. |
|---|
| 1301 | * A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types. |
|---|
| 1302 | * @param string $type Optional. What type of operation is this? INSERT or REPLACE. Defaults to INSERT. |
|---|
| 1303 | * @return int|false The number of rows affected, or false on error. |
|---|
| 1304 | */ |
|---|
| 1305 | function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) { |
|---|
| 1306 | if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) ) |
|---|
| 1307 | return false; |
|---|
| 1308 | $this->insert_id = 0; |
|---|
| 1309 | $formats = $format = (array) $format; |
|---|
| 1310 | $fields = array_keys( $data ); |
|---|
| 1311 | $formatted_fields = array(); |
|---|
| 1312 | foreach ( $fields as $field ) { |
|---|
| 1313 | if ( !empty( $format ) ) |
|---|
| 1314 | $form = ( $form = array_shift( $formats ) ) ? $form : $format[0]; |
|---|
| 1315 | elseif ( isset( $this->field_types[$field] ) ) |
|---|
| 1316 | $form = $this->field_types[$field]; |
|---|
| 1317 | else |
|---|
| 1318 | $form = '%s'; |
|---|
| 1319 | $formatted_fields[] = $form; |
|---|
| 1320 | } |
|---|
| 1321 | $sql = "{$type} INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES (" . implode( ",", $formatted_fields ) . ")"; |
|---|
| 1322 | return $this->query( $this->prepare( $sql, $data ) ); |
|---|
| 1323 | } |
|---|
| 1324 | |
|---|
| 1325 | /** |
|---|
| 1326 | * Update a row in the table |
|---|
| 1327 | * |
|---|
| 1328 | * <code> |
|---|
| 1329 | * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 'bar' ), array( 'ID' => 1 ) ) |
|---|
| 1330 | * wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) ) |
|---|
| 1331 | * </code> |
|---|
| 1332 | * |
|---|
| 1333 | * @since 2.5.0 |
|---|
| 1334 | * @see wpdb::prepare() |
|---|
| 1335 | * @see wpdb::$field_types |
|---|
| 1336 | * @see wp_set_wpdb_vars() |
|---|
| 1337 | * |
|---|
| 1338 | * @param string $table table name |
|---|
| 1339 | * @param array $data Data to update (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). |
|---|
| 1340 | * @param array $where A named array of WHERE clauses (in column => value pairs). Multiple clauses will be joined with ANDs. Both $where columns and $where values should be "raw". |
|---|
| 1341 | * @param array|string $format Optional. An array of formats to be mapped to each of the values in $data. If string, that format will be used for all of the values in $data. |
|---|
| 1342 | * A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types. |
|---|
| 1343 | * @param array|string $where_format Optional. An array of formats to be mapped to each of the values in $where. If string, that format will be used for all of the items in $where. A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $where will be treated as strings. |
|---|
| 1344 | * @return int|false The number of rows updated, or false on error. |
|---|
| 1345 | */ |
|---|
| 1346 | function update( $table, $data, $where, $format = null, $where_format = null ) { |
|---|
| 1347 | if ( ! is_array( $data ) || ! is_array( $where ) ) |
|---|
| 1348 | return false; |
|---|
| 1349 | |
|---|
| 1350 | $formats = $format = (array) $format; |
|---|
| 1351 | $bits = $wheres = array(); |
|---|
| 1352 | foreach ( (array) array_keys( $data ) as $field ) { |
|---|
| 1353 | if ( !empty( $format ) ) |
|---|
| 1354 | $form = ( $form = array_shift( $formats ) ) ? $form : $format[0]; |
|---|
| 1355 | elseif ( isset($this->field_types[$field]) ) |
|---|
| 1356 | $form = $this->field_types[$field]; |
|---|
| 1357 | else |
|---|
| 1358 | $form = '%s'; |
|---|
| 1359 | $bits[] = "`$field` = {$form}"; |
|---|
| 1360 | } |
|---|
| 1361 | |
|---|
| 1362 | $where_formats = $where_format = (array) $where_format; |
|---|
| 1363 | foreach ( (array) array_keys( $where ) as $field ) { |
|---|
| 1364 | if ( !empty( $where_format ) ) |
|---|
| 1365 | $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0]; |
|---|
| 1366 | elseif ( isset( $this->field_types[$field] ) ) |
|---|
| 1367 | $form = $this->field_types[$field]; |
|---|
| 1368 | else |
|---|
| 1369 | $form = '%s'; |
|---|
| 1370 | $wheres[] = "`$field` = {$form}"; |
|---|
| 1371 | } |
|---|
| 1372 | |
|---|
| 1373 | $sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres ); |
|---|
| 1374 | return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) ); |
|---|
| 1375 | } |
|---|
| 1376 | |
|---|
| 1377 | /** |
|---|
| 1378 | * Delete a row in the table |
|---|
| 1379 | * |
|---|
| 1380 | * <code> |
|---|
| 1381 | * wpdb::delete( 'table', array( 'ID' => 1 ) ) |
|---|
| 1382 | * wpdb::delete( 'table', array( 'ID' => 1 ), array( '%d' ) ) |
|---|
| 1383 | * </code> |
|---|
| 1384 | * |
|---|
| 1385 | * @since 3.4.0 |
|---|
| 1386 | * @see wpdb::prepare() |
|---|
| 1387 | * @see wpdb::$field_types |
|---|
| 1388 | * @see wp_set_wpdb_vars() |
|---|
| 1389 | * |
|---|
| 1390 | * @param string $table table name |
|---|
| 1391 | * @param array $where A named array of WHERE clauses (in column => value pairs). Multiple clauses will be joined with ANDs. Both $where columns and $where values should be "raw". |
|---|
| 1392 | * @param array|string $where_format Optional. An array of formats to be mapped to each of the values in $where. If string, that format will be used for all of the items in $where. A format is one of '%d', '%f', '%s' (integer, float, string). If omitted, all values in $where will be treated as strings unless otherwise specified in wpdb::$field_types. |
|---|
| 1393 | * @return int|false The number of rows updated, or false on error. |
|---|
| 1394 | */ |
|---|
| 1395 | function delete( $table, $where, $where_format = null ) { |
|---|
| 1396 | if ( ! is_array( $where ) ) |
|---|
| 1397 | return false; |
|---|
| 1398 | |
|---|
| 1399 | $bits = $wheres = array(); |
|---|
| 1400 | |
|---|
| 1401 | $where_formats = $where_format = (array) $where_format; |
|---|
| 1402 | |
|---|
| 1403 | foreach ( array_keys( $where ) as $field ) { |
|---|
| 1404 | if ( !empty( $where_format ) ) { |
|---|
| 1405 | $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0]; |
|---|
| 1406 | } elseif ( isset( $this->field_types[ $field ] ) ) { |
|---|
| 1407 | $form = $this->field_types[ $field ]; |
|---|
| 1408 | } else { |
|---|
| 1409 | $form = '%s'; |
|---|
| 1410 | } |
|---|
| 1411 | |
|---|
| 1412 | $wheres[] = "$field = $form"; |
|---|
| 1413 | } |
|---|
| 1414 | |
|---|
| 1415 | $sql = "DELETE FROM $table WHERE " . implode( ' AND ', $wheres ); |
|---|
| 1416 | return $this->query( $this->prepare( $sql, $where ) ); |
|---|
| 1417 | } |
|---|
| 1418 | |
|---|
| 1419 | |
|---|
| 1420 | /** |
|---|
| 1421 | * Retrieve one variable from the database. |
|---|
| 1422 | * |
|---|
| 1423 | * Executes a SQL query and returns the value from the SQL result. |
|---|
| 1424 | * If the SQL result contains more than one column and/or more than one row, this function returns the value in the column and row specified. |
|---|
| 1425 | * If $query is null, this function returns the value in the specified column and row from the previous SQL result. |
|---|
| 1426 | * |
|---|
| 1427 | * @since 0.71 |
|---|
| 1428 | * |
|---|
| 1429 | * @param string|null $query Optional. SQL query. Defaults to null, use the result from the previous query. |
|---|
| 1430 | * @param int $x Optional. Column of value to return. Indexed from 0. |
|---|
| 1431 | * @param int $y Optional. Row of value to return. Indexed from 0. |
|---|
| 1432 | * @return string|null Database query result (as string), or null on failure |
|---|
| 1433 | */ |
|---|
| 1434 | function get_var( $query = null, $x = 0, $y = 0 ) { |
|---|
| 1435 | $this->func_call = "\$db->get_var(\"$query\", $x, $y)"; |
|---|
| 1436 | if ( $query ) |
|---|
| 1437 | $this->query( $query ); |
|---|
| 1438 | |
|---|
| 1439 | // Extract var out of cached results based x,y vals |
|---|
| 1440 | if ( !empty( $this->last_result[$y] ) ) { |
|---|
| 1441 | $values = array_values( get_object_vars( $this->last_result[$y] ) ); |
|---|
| 1442 | } |
|---|
| 1443 | |
|---|
| 1444 | // If there is a value return it else return null |
|---|
| 1445 | return ( isset( $values[$x] ) && $values[$x] !== '' ) ? $values[$x] : null; |
|---|
| 1446 | } |
|---|
| 1447 | |
|---|
| 1448 | /** |
|---|
| 1449 | * Retrieve one row from the database. |
|---|
| 1450 | * |
|---|
| 1451 | * Executes a SQL query and returns the row from the SQL result. |
|---|
| 1452 | * |
|---|
| 1453 | * @since 0.71 |
|---|
| 1454 | * |
|---|
| 1455 | * @param string|null $query SQL query. |
|---|
| 1456 | * @param string $output Optional. one of ARRAY_A | ARRAY_N | OBJECT constants. Return an associative array (column => value, ...), |
|---|
| 1457 | * a numerically indexed array (0 => value, ...) or an object ( ->column = value ), respectively. |
|---|
| 1458 | * @param int $y Optional. Row to return. Indexed from 0. |
|---|
| 1459 | * @return mixed Database query result in format specified by $output or null on failure |
|---|
| 1460 | */ |
|---|
| 1461 | function get_row( $query = null, $output = OBJECT, $y = 0 ) { |
|---|
| 1462 | $this->func_call = "\$db->get_row(\"$query\",$output,$y)"; |
|---|
| 1463 | if ( $query ) |
|---|
| 1464 | $this->query( $query ); |
|---|
| 1465 | else |
|---|
| 1466 | return null; |
|---|
| 1467 | |
|---|
| 1468 | if ( !isset( $this->last_result[$y] ) ) |
|---|
| 1469 | return null; |
|---|
| 1470 | |
|---|
| 1471 | if ( $output == OBJECT ) { |
|---|
| 1472 | return $this->last_result[$y] ? $this->last_result[$y] : null; |
|---|
| 1473 | } elseif ( $output == ARRAY_A ) { |
|---|
| 1474 | return $this->last_result[$y] ? get_object_vars( $this->last_result[$y] ) : null; |
|---|
| 1475 | } elseif ( $output == ARRAY_N ) { |
|---|
| 1476 | return $this->last_result[$y] ? array_values( get_object_vars( $this->last_result[$y] ) ) : null; |
|---|
| 1477 | } else { |
|---|
| 1478 | $this->print_error( " \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N" ); |
|---|
| 1479 | } |
|---|
| 1480 | } |
|---|
| 1481 | |
|---|
| 1482 | /** |
|---|
| 1483 | * Retrieve one column from the database. |
|---|
| 1484 | * |
|---|
| 1485 | * Executes a SQL query and returns the column from the SQL result. |
|---|
| 1486 | * If the SQL result contains more than one column, this function returns the column specified. |
|---|
| 1487 | * If $query is null, this function returns the specified column from the previous SQL result. |
|---|
| 1488 | * |
|---|
| 1489 | * @since 0.71 |
|---|
| 1490 | * |
|---|
| 1491 | * @param string|null $query Optional. SQL query. Defaults to previous query. |
|---|
| 1492 | * @param int $x Optional. Column to return. Indexed from 0. |
|---|
| 1493 | * @return array Database query result. Array indexed from 0 by SQL result row number. |
|---|
| 1494 | */ |
|---|
| 1495 | function get_col( $query = null , $x = 0 ) { |
|---|
| 1496 | if ( $query ) |
|---|
| 1497 | $this->query( $query ); |
|---|
| 1498 | |
|---|
| 1499 | $new_array = array(); |
|---|
| 1500 | // Extract the column values |
|---|
| 1501 | for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) { |
|---|
| 1502 | $new_array[$i] = $this->get_var( null, $x, $i ); |
|---|
| 1503 | } |
|---|
| 1504 | return $new_array; |
|---|
| 1505 | } |
|---|
| 1506 | |
|---|
| 1507 | /** |
|---|
| 1508 | * Retrieve an entire SQL result set from the database (i.e., many rows) |
|---|
| 1509 | * |
|---|
| 1510 | * Executes a SQL query and returns the entire SQL result. |
|---|
| 1511 | * |
|---|
| 1512 | * @since 0.71 |
|---|
| 1513 | * |
|---|
| 1514 | * @param string $query SQL query. |
|---|
| 1515 | * @param string $output Optional. Any of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants. With one of the first three, return an array of rows indexed from 0 by SQL result row number. |
|---|
| 1516 | * Each row is an associative array (column => value, ...), a numerically indexed array (0 => value, ...), or an object. ( ->column = value ), respectively. |
|---|
| 1517 | * With OBJECT_K, return an associative array of row objects keyed by the value of each row's first column's value. Duplicate keys are discarded. |
|---|
| 1518 | * @return mixed Database query results |
|---|
| 1519 | */ |
|---|
| 1520 | function get_results( $query = null, $output = OBJECT ) { |
|---|
| 1521 | $this->func_call = "\$db->get_results(\"$query\", $output)"; |
|---|
| 1522 | |
|---|
| 1523 | if ( $query ) |
|---|
| 1524 | $this->query( $query ); |
|---|
| 1525 | else |
|---|
| 1526 | return null; |
|---|
| 1527 | |
|---|
| 1528 | $new_array = array(); |
|---|
| 1529 | if ( $output == OBJECT ) { |
|---|
| 1530 | // Return an integer-keyed array of row objects |
|---|
| 1531 | return $this->last_result; |
|---|
| 1532 | } elseif ( $output == OBJECT_K ) { |
|---|
| 1533 | // Return an array of row objects with keys from column 1 |
|---|
| 1534 | // (Duplicates are discarded) |
|---|
| 1535 | foreach ( $this->last_result as $row ) { |
|---|
| 1536 | $var_by_ref = get_object_vars( $row ); |
|---|
| 1537 | $key = array_shift( $var_by_ref ); |
|---|
| 1538 | if ( ! isset( $new_array[ $key ] ) ) |
|---|
| 1539 | $new_array[ $key ] = $row; |
|---|
| 1540 | } |
|---|
| 1541 | return $new_array; |
|---|
| 1542 | } elseif ( $output == ARRAY_A || $output == ARRAY_N ) { |
|---|
| 1543 | // Return an integer-keyed array of... |
|---|
| 1544 | if ( $this->last_result ) { |
|---|
| 1545 | foreach( (array) $this->last_result as $row ) { |
|---|
| 1546 | if ( $output == ARRAY_N ) { |
|---|
| 1547 | // ...integer-keyed row arrays |
|---|
| 1548 | $new_array[] = array_values( get_object_vars( $row ) ); |
|---|
| 1549 | } else { |
|---|
| 1550 | // ...column name-keyed row arrays |
|---|
| 1551 | $new_array[] = get_object_vars( $row ); |
|---|
| 1552 | } |
|---|
| 1553 | } |
|---|
| 1554 | } |
|---|
| 1555 | return $new_array; |
|---|
| 1556 | } |
|---|
| 1557 | return null; |
|---|
| 1558 | } |
|---|
| 1559 | |
|---|
| 1560 | /** |
|---|
| 1561 | * Load the column metadata from the last query. |
|---|
| 1562 | * |
|---|
| 1563 | * @since 3.5.0 |
|---|
| 1564 | * |
|---|
| 1565 | * @access protected |
|---|
| 1566 | */ |
|---|
| 1567 | protected function load_col_info() { |
|---|
| 1568 | if ( $this->col_info ) |
|---|
| 1569 | return; |
|---|
| 1570 | |
|---|
| 1571 | for ( $i = 0; $i < @mysql_num_fields( $this->result ); $i++ ) { |
|---|
| 1572 | $this->col_info[ $i ] = @mysql_fetch_field( $this->result, $i ); |
|---|
| 1573 | } |
|---|
| 1574 | } |
|---|
| 1575 | |
|---|
| 1576 | /** |
|---|
| 1577 | * Retrieve column metadata from the last query. |
|---|
| 1578 | * |
|---|
| 1579 | * @since 0.71 |
|---|
| 1580 | * |
|---|
| 1581 | * @param string $info_type Optional. Type one of name, table, def, max_length, not_null, primary_key, multiple_key, unique_key, numeric, blob, type, unsigned, zerofill |
|---|
| 1582 | * @param int $col_offset Optional. 0: col name. 1: which table the col's in. 2: col's max length. 3: if the col is numeric. 4: col's type |
|---|
| 1583 | * @return mixed Column Results |
|---|
| 1584 | */ |
|---|
| 1585 | function get_col_info( $info_type = 'name', $col_offset = -1 ) { |
|---|
| 1586 | $this->load_col_info(); |
|---|
| 1587 | |
|---|
| 1588 | if ( $this->col_info ) { |
|---|
| 1589 | if ( $col_offset == -1 ) { |
|---|
| 1590 | $i = 0; |
|---|
| 1591 | $new_array = array(); |
|---|
| 1592 | foreach( (array) $this->col_info as $col ) { |
|---|
| 1593 | $new_array[$i] = $col->{$info_type}; |
|---|
| 1594 | $i++; |
|---|
| 1595 | } |
|---|
| 1596 | return $new_array; |
|---|
| 1597 | } else { |
|---|
| 1598 | return $this->col_info[$col_offset]->{$info_type}; |
|---|
| 1599 | } |
|---|
| 1600 | } |
|---|
| 1601 | } |
|---|
| 1602 | |
|---|
| 1603 | /** |
|---|
| 1604 | * Starts the timer, for debugging purposes. |
|---|
| 1605 | * |
|---|
| 1606 | * @since 1.5.0 |
|---|
| 1607 | * |
|---|
| 1608 | * @return true |
|---|
| 1609 | */ |
|---|
| 1610 | function timer_start() { |
|---|
| 1611 | $this->time_start = microtime( true ); |
|---|
| 1612 | return true; |
|---|
| 1613 | } |
|---|
| 1614 | |
|---|
| 1615 | /** |
|---|
| 1616 | * Stops the debugging timer. |
|---|
| 1617 | * |
|---|
| 1618 | * @since 1.5.0 |
|---|
| 1619 | * |
|---|
| 1620 | * @return float Total time spent on the query, in seconds |
|---|
| 1621 | */ |
|---|
| 1622 | function timer_stop() { |
|---|
| 1623 | return ( microtime( true ) - $this->time_start ); |
|---|
| 1624 | } |
|---|
| 1625 | |
|---|
| 1626 | /** |
|---|
| 1627 | * Wraps errors in a nice header and footer and dies. |
|---|
| 1628 | * |
|---|
| 1629 | * Will not die if wpdb::$show_errors is false. |
|---|
| 1630 | * |
|---|
| 1631 | * @since 1.5.0 |
|---|
| 1632 | * |
|---|
| 1633 | * @param string $message The Error message |
|---|
| 1634 | * @param string $error_code Optional. A Computer readable string to identify the error. |
|---|
| 1635 | * @return false|void |
|---|
| 1636 | */ |
|---|
| 1637 | function bail( $message, $error_code = '500' ) { |
|---|
| 1638 | if ( !$this->show_errors ) { |
|---|
| 1639 | if ( class_exists( 'WP_Error' ) ) |
|---|
| 1640 | $this->error = new WP_Error($error_code, $message); |
|---|
| 1641 | else |
|---|
| 1642 | $this->error = $message; |
|---|
| 1643 | return false; |
|---|
| 1644 | } |
|---|
| 1645 | wp_die($message); |
|---|
| 1646 | } |
|---|
| 1647 | |
|---|
| 1648 | /** |
|---|
| 1649 | * Whether MySQL database is at least the required minimum version. |
|---|
| 1650 | * |
|---|
| 1651 | * @since 2.5.0 |
|---|
| 1652 | * @uses $wp_version |
|---|
| 1653 | * @uses $required_mysql_version |
|---|
| 1654 | * |
|---|
| 1655 | * @return WP_Error |
|---|
| 1656 | */ |
|---|
| 1657 | function check_database_version() { |
|---|
| 1658 | global $wp_version, $required_mysql_version; |
|---|
| 1659 | // Make sure the server has the required MySQL version |
|---|
| 1660 | if ( version_compare($this->db_version(), $required_mysql_version, '<') ) |
|---|
| 1661 | return new WP_Error('database_version', sprintf( __( '<strong>ERROR</strong>: WordPress %1$s requires MySQL %2$s or higher' ), $wp_version, $required_mysql_version )); |
|---|
| 1662 | } |
|---|
| 1663 | |
|---|
| 1664 | /** |
|---|
| 1665 | * Whether the database supports collation. |
|---|
| 1666 | * |
|---|
| 1667 | * Called when WordPress is generating the table scheme. |
|---|
| 1668 | * |
|---|
| 1669 | * @since 2.5.0 |
|---|
| 1670 | * @deprecated 3.5.0 |
|---|
| 1671 | * @deprecated Use wpdb::has_cap( 'collation' ) |
|---|
| 1672 | * |
|---|
| 1673 | * @return bool True if collation is supported, false if version does not |
|---|
| 1674 | */ |
|---|
| 1675 | function supports_collation() { |
|---|
| 1676 | _deprecated_function( __FUNCTION__, '3.5', 'wpdb::has_cap( \'collation\' )' ); |
|---|
| 1677 | return $this->has_cap( 'collation' ); |
|---|
| 1678 | } |
|---|
| 1679 | |
|---|
| 1680 | /** |
|---|
| 1681 | * The database character collate. |
|---|
| 1682 | * |
|---|
| 1683 | * @since 3.5.0 |
|---|
| 1684 | * |
|---|
| 1685 | * @return string The database character collate. |
|---|
| 1686 | */ |
|---|
| 1687 | public function get_charset_collate() { |
|---|
| 1688 | $charset_collate = ''; |
|---|
| 1689 | |
|---|
| 1690 | if ( ! empty( $this->charset ) ) |
|---|
| 1691 | $charset_collate = "DEFAULT CHARACTER SET $this->charset"; |
|---|
| 1692 | if ( ! empty( $this->collate ) ) |
|---|
| 1693 | $charset_collate .= " COLLATE $this->collate"; |
|---|
| 1694 | |
|---|
| 1695 | return $charset_collate; |
|---|
| 1696 | } |
|---|
| 1697 | |
|---|
| 1698 | /** |
|---|
| 1699 | * Determine if a database supports a particular feature. |
|---|
| 1700 | * |
|---|
| 1701 | * @since 2.7.0 |
|---|
| 1702 | * @see wpdb::db_version() |
|---|
| 1703 | * |
|---|
| 1704 | * @param string $db_cap The feature to check for. |
|---|
| 1705 | * @return bool |
|---|
| 1706 | */ |
|---|
| 1707 | function has_cap( $db_cap ) { |
|---|
| 1708 | $version = $this->db_version(); |
|---|
| 1709 | |
|---|
| 1710 | switch ( strtolower( $db_cap ) ) { |
|---|
| 1711 | case 'collation' : // @since 2.5.0 |
|---|
| 1712 | case 'group_concat' : // @since 2.7.0 |
|---|
| 1713 | case 'subqueries' : // @since 2.7.0 |
|---|
| 1714 | return version_compare( $version, '4.1', '>=' ); |
|---|
| 1715 | case 'set_charset' : |
|---|
| 1716 | return version_compare( $version, '5.0.7', '>=' ); |
|---|
| 1717 | }; |
|---|
| 1718 | |
|---|
| 1719 | return false; |
|---|
| 1720 | } |
|---|
| 1721 | |
|---|
| 1722 | /** |
|---|
| 1723 | * Retrieve the name of the function that called wpdb. |
|---|
| 1724 | * |
|---|
| 1725 | * Searches up the list of functions until it reaches |
|---|
| 1726 | * the one that would most logically had called this method. |
|---|
| 1727 | * |
|---|
| 1728 | * @since 2.5.0 |
|---|
| 1729 | * |
|---|
| 1730 | * @return string The name of the calling function |
|---|
| 1731 | */ |
|---|
| 1732 | function get_caller() { |
|---|
| 1733 | return wp_debug_backtrace_summary( __CLASS__ ); |
|---|
| 1734 | } |
|---|
| 1735 | |
|---|
| 1736 | /** |
|---|
| 1737 | * The database version number. |
|---|
| 1738 | * |
|---|
| 1739 | * @since 2.7.0 |
|---|
| 1740 | * |
|---|
| 1741 | * @return false|string false on failure, version number on success |
|---|
| 1742 | */ |
|---|
| 1743 | function db_version() { |
|---|
| 1744 | return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) ); |
|---|
| 1745 | } |
|---|
| 1746 | } |
|---|