| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('UPDRAFTPLUS_DIR')) die('No direct access allowed'); |
| 4 |
|
| 5 |
class UpdraftPlus_Database_Utility { |
| 6 |
|
| 7 |
private $whichdb; |
| 8 |
|
| 9 |
private $table_prefix_raw; |
| 10 |
|
| 11 |
private $dbhandle; |
| 12 |
|
| 13 |
/** |
| 14 |
* Constructor |
| 15 |
*/ |
| 16 |
/** |
| 17 |
* Constructor |
| 18 |
* |
| 19 |
* @param String $whichdb - which database is being backed up |
| 20 |
* @param String $table_prefix_raw - the base table prefix |
| 21 |
* @param Object $dbhandle - WPDB object |
| 22 |
*/ |
| 23 |
public function __construct($whichdb, $table_prefix_raw, $dbhandle) { |
| 24 |
$this->whichdb = $whichdb; |
| 25 |
$this->table_prefix_raw = $table_prefix_raw; |
| 26 |
$this->dbhandle = $dbhandle; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* The purpose of this function is to make sure that the options table is put in the database first, then the users table, then the site + blogs tables (if present - multisite), then the usermeta table; and after that the core WP tables - so that when restoring we restore the core tables first |
| 31 |
* |
| 32 |
* @param Array $a_arr the first array |
| 33 |
* @param Array $b_arr the second array |
| 34 |
* |
| 35 |
* @return Integer - the sort result, according to the rules of PHP custom sorting functions |
| 36 |
*/ |
| 37 |
public function backup_db_sorttables($a_arr, $b_arr) { |
| 38 |
|
| 39 |
$a = $a_arr['name']; |
| 40 |
$a_table_type = $a_arr['type']; |
| 41 |
$b = $b_arr['name']; |
| 42 |
$b_table_type = $b_arr['type']; |
| 43 |
|
| 44 |
// Views must always go after tables (since they can depend upon them) |
| 45 |
if ('VIEW' == $a_table_type && 'VIEW' != $b_table_type) return 1; |
| 46 |
if ('VIEW' == $b_table_type && 'VIEW' != $a_table_type) return -1; |
| 47 |
|
| 48 |
if ('wp' != $this->whichdb) return strcmp($a, $b); |
| 49 |
|
| 50 |
global $updraftplus; |
| 51 |
if ($a == $b) return 0; |
| 52 |
$our_table_prefix = $this->table_prefix_raw; |
| 53 |
if ($a == $our_table_prefix.'options') return -1; |
| 54 |
if ($b == $our_table_prefix.'options') return 1; |
| 55 |
if ($a == $our_table_prefix.'site') return -1; |
| 56 |
if ($b == $our_table_prefix.'site') return 1; |
| 57 |
if ($a == $our_table_prefix.'blogs') return -1; |
| 58 |
if ($b == $our_table_prefix.'blogs') return 1; |
| 59 |
if ($a == $our_table_prefix.'users') return -1; |
| 60 |
if ($b == $our_table_prefix.'users') return 1; |
| 61 |
if ($a == $our_table_prefix.'usermeta') return -1; |
| 62 |
if ($b == $our_table_prefix.'usermeta') return 1; |
| 63 |
|
| 64 |
if (empty($our_table_prefix)) return strcmp($a, $b); |
| 65 |
|
| 66 |
try { |
| 67 |
$core_tables = array_merge($this->dbhandle->tables, $this->dbhandle->global_tables, $this->dbhandle->ms_global_tables); |
| 68 |
} catch (Exception $e) { |
| 69 |
$updraftplus->log($e->getMessage()); |
| 70 |
} |
| 71 |
|
| 72 |
if (empty($core_tables)) $core_tables = array('terms', 'term_taxonomy', 'termmeta', 'term_relationships', 'commentmeta', 'comments', 'links', 'postmeta', 'posts', 'site', 'sitemeta', 'blogs', 'blogversions', 'blogmeta'); |
| 73 |
|
| 74 |
$na = UpdraftPlus_Manipulation_Functions::str_replace_once($our_table_prefix, '', $a); |
| 75 |
$nb = UpdraftPlus_Manipulation_Functions::str_replace_once($our_table_prefix, '', $b); |
| 76 |
if (in_array($na, $core_tables) && !in_array($nb, $core_tables)) return -1; |
| 77 |
if (!in_array($na, $core_tables) && in_array($nb, $core_tables)) return 1; |
| 78 |
return strcmp($a, $b); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
class UpdraftPlus_WPDB_OtherDB_Utility extends wpdb { |
| 83 |
/** |
| 84 |
* This adjusted bail() does two things: 1) Never dies and 2) logs in the UD log |
| 85 |
* |
| 86 |
* @param String $message a string containing a message |
| 87 |
* @param String $error_code a string containing an error code |
| 88 |
* @return Boolean returns false |
| 89 |
*/ |
| 90 |
public function bail($message, $error_code = '500') { |
| 91 |
global $updraftplus; |
| 92 |
if ('db_connect_fail' == $error_code) $message = 'Connection failed: check your access details, that the database server is up, and that the network connection is not firewalled.'; |
| 93 |
$updraftplus->log("WPDB_OtherDB error: $message ($error_code)"); |
| 94 |
// Now do the things that would have been done anyway |
| 95 |
$this->error = class_exists('WP_Error') ? new WP_Error($error_code, $message) : $message; |
| 96 |
return false; |
| 97 |
} |
| 98 |
} |
| 99 |
|