| 1 |
<?php |
| 2 |
|
| 3 |
class Red_Database_Status { |
| 4 |
// Used in < 3.7 versions of Redirection, but since migrated to general settings |
| 5 |
const OLD_DB_VERSION = 'redirection_version'; |
| 6 |
const DB_UPGRADE_STAGE = 'redirection_database_stage'; |
| 7 |
|
| 8 |
const RESULT_OK = 'ok'; |
| 9 |
const RESULT_ERROR = 'error'; |
| 10 |
|
| 11 |
const STATUS_OK = 'ok'; |
| 12 |
const STATUS_NEED_INSTALL = 'need-install'; |
| 13 |
const STATUS_NEED_UPDATING = 'need-update'; |
| 14 |
const STATUS_FINISHED_INSTALL = 'finish-install'; |
| 15 |
const STATUS_FINISHED_UPDATING = 'finish-update'; |
| 16 |
|
| 17 |
private $stage = false; |
| 18 |
private $stages = []; |
| 19 |
|
| 20 |
private $status = false; |
| 21 |
private $result = false; |
| 22 |
private $reason = false; |
| 23 |
private $debug = []; |
| 24 |
|
| 25 |
public function __construct() { |
| 26 |
$this->status = self::STATUS_OK; |
| 27 |
|
| 28 |
if ( $this->needs_installing() ) { |
| 29 |
$this->status = self::STATUS_NEED_INSTALL; |
| 30 |
} elseif ( $this->needs_updating() ) { |
| 31 |
$this->status = self::STATUS_NEED_UPDATING; |
| 32 |
} |
| 33 |
|
| 34 |
$info = get_option( self::DB_UPGRADE_STAGE ); |
| 35 |
if ( $info ) { |
| 36 |
$this->stage = isset( $info['stage'] ) ? $info['stage'] : false; |
| 37 |
$this->stages = isset( $info['stages'] ) ? $info['stages'] : []; |
| 38 |
$this->status = isset( $info['status'] ) ? $info['status'] : false; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Does the database need install |
| 44 |
* |
| 45 |
* @return bool true if needs installing, false otherwise |
| 46 |
*/ |
| 47 |
public function needs_installing() { |
| 48 |
$settings = red_get_options(); |
| 49 |
|
| 50 |
if ( $settings['database'] === '' && $this->get_old_version() === false ) { |
| 51 |
return true; |
| 52 |
} |
| 53 |
|
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Does the current database need updating to the target |
| 59 |
* |
| 60 |
* @return bool true if needs updating, false otherwise |
| 61 |
*/ |
| 62 |
public function needs_updating() { |
| 63 |
// We need updating if we don't need to install, and the current version is less than target version |
| 64 |
if ( $this->needs_installing() === false && version_compare( $this->get_current_version(), REDIRECTION_DB_VERSION, '<' ) ) { |
| 65 |
return true; |
| 66 |
} |
| 67 |
|
| 68 |
// Also if we're still in the process of upgrading |
| 69 |
if ( $this->get_current_stage() ) { |
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Get current database version |
| 78 |
* |
| 79 |
* @return string Current database version |
| 80 |
*/ |
| 81 |
public function get_current_version() { |
| 82 |
$settings = red_get_options(); |
| 83 |
|
| 84 |
if ( $settings['database'] !== '' ) { |
| 85 |
return $settings['database']; |
| 86 |
} elseif ( $this->get_old_version() !== false ) { |
| 87 |
$version = $this->get_old_version(); |
| 88 |
|
| 89 |
// Upgrade the old value |
| 90 |
red_set_options( array( 'database' => $version ) ); |
| 91 |
delete_option( self::OLD_DB_VERSION ); |
| 92 |
$this->clear_cache(); |
| 93 |
return $version; |
| 94 |
} |
| 95 |
|
| 96 |
return ''; |
| 97 |
} |
| 98 |
|
| 99 |
private function get_old_version() { |
| 100 |
return get_option( self::OLD_DB_VERSION ); |
| 101 |
} |
| 102 |
|
| 103 |
public function check_tables_exist() { |
| 104 |
$latest = Red_Database::get_latest_database(); |
| 105 |
$missing = $latest->get_missing_tables(); |
| 106 |
|
| 107 |
// No tables installed - do a fresh install |
| 108 |
if ( count( $missing ) === count( $latest->get_all_tables() ) ) { |
| 109 |
delete_option( Red_Database_Status::OLD_DB_VERSION ); |
| 110 |
red_set_options( [ 'database' => '' ] ); |
| 111 |
$this->clear_cache(); |
| 112 |
|
| 113 |
$this->status = self::STATUS_NEED_INSTALL; |
| 114 |
$this->stop_update(); |
| 115 |
} elseif ( count( $missing ) > 0 && version_compare( $this->get_current_version(), '2.3.3', 'ge' ) ) { |
| 116 |
// Some tables are missing - try and fill them in |
| 117 |
$latest->install(); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Does the current database support a particular version |
| 123 |
* |
| 124 |
* @param string $version Target version |
| 125 |
* @return bool true if supported, false otherwise |
| 126 |
*/ |
| 127 |
public function does_support( $version ) { |
| 128 |
return version_compare( $this->get_current_version(), $version, 'ge' ); |
| 129 |
} |
| 130 |
|
| 131 |
public function is_error() { |
| 132 |
return $this->result === self::RESULT_ERROR; |
| 133 |
} |
| 134 |
|
| 135 |
public function set_error( $error ) { |
| 136 |
global $wpdb; |
| 137 |
|
| 138 |
$this->result = self::RESULT_ERROR; |
| 139 |
$this->reason = str_replace( "\t", ' ', $error ); |
| 140 |
|
| 141 |
if ( $wpdb->last_error ) { |
| 142 |
$this->debug[] = $wpdb->last_error; |
| 143 |
|
| 144 |
if ( strpos( $wpdb->last_error, 'command denied to user' ) !== false ) { |
| 145 |
$this->reason .= ' - ' . __( 'Insufficient database permissions detected. Please give your database user appropriate permissions.', 'redirection' ); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
$latest = Red_Database::get_latest_database(); |
| 150 |
$this->debug = array_merge( $this->debug, $latest->get_table_schema() ); |
| 151 |
$this->debug[] = 'Stage: ' . $this->get_current_stage(); |
| 152 |
} |
| 153 |
|
| 154 |
public function set_ok( $reason ) { |
| 155 |
$this->reason = $reason; |
| 156 |
$this->result = self::RESULT_OK; |
| 157 |
$this->debug = []; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Stop current upgrade |
| 162 |
*/ |
| 163 |
public function stop_update() { |
| 164 |
$this->stage = false; |
| 165 |
$this->stages = []; |
| 166 |
$this->debug = []; |
| 167 |
|
| 168 |
delete_option( self::DB_UPGRADE_STAGE ); |
| 169 |
$this->clear_cache(); |
| 170 |
} |
| 171 |
|
| 172 |
public function finish() { |
| 173 |
$this->stop_update(); |
| 174 |
|
| 175 |
if ( $this->status === self::STATUS_NEED_INSTALL ) { |
| 176 |
$this->status = self::STATUS_FINISHED_INSTALL; |
| 177 |
} elseif ( $this->status === self::STATUS_NEED_UPDATING ) { |
| 178 |
$this->status = self::STATUS_FINISHED_UPDATING; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Get current upgrade stage |
| 184 |
* @return string|bool Current stage name, or false if not upgrading |
| 185 |
*/ |
| 186 |
public function get_current_stage() { |
| 187 |
return $this->stage; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Move current stage on to the next |
| 192 |
*/ |
| 193 |
public function set_next_stage() { |
| 194 |
$stage = $this->get_current_stage(); |
| 195 |
|
| 196 |
if ( $stage ) { |
| 197 |
$stage = $this->get_next_stage( $stage ); |
| 198 |
|
| 199 |
// Save next position |
| 200 |
if ( $stage ) { |
| 201 |
$this->set_stage( $stage ); |
| 202 |
} else { |
| 203 |
$this->finish(); |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Get current upgrade status |
| 210 |
* |
| 211 |
* @return array Database status array |
| 212 |
*/ |
| 213 |
public function get_json() { |
| 214 |
// Base information |
| 215 |
$result = [ |
| 216 |
'status' => $this->status, |
| 217 |
'inProgress' => $this->stage !== false, |
| 218 |
]; |
| 219 |
|
| 220 |
// Add on version status |
| 221 |
if ( $this->status === self::STATUS_NEED_INSTALL || $this->status === self::STATUS_NEED_UPDATING ) { |
| 222 |
$result = array_merge( |
| 223 |
$result, |
| 224 |
$this->get_version_upgrade(), |
| 225 |
[ 'manual' => $this->get_manual_upgrade() ] |
| 226 |
); |
| 227 |
} |
| 228 |
|
| 229 |
// Add on upgrade status |
| 230 |
if ( $this->is_error() ) { |
| 231 |
$result = array_merge( $result, $this->get_version_upgrade(), $this->get_progress_status(), $this->get_error_status() ); |
| 232 |
} elseif ( $result['inProgress'] ) { |
| 233 |
$result = array_merge( $result, $this->get_progress_status() ); |
| 234 |
} elseif ( $this->status === self::STATUS_FINISHED_INSTALL || $this->status === self::STATUS_FINISHED_UPDATING ) { |
| 235 |
$result['complete'] = 100; |
| 236 |
$result['reason'] = $this->reason; |
| 237 |
} |
| 238 |
|
| 239 |
return $result; |
| 240 |
} |
| 241 |
|
| 242 |
private function get_error_status() { |
| 243 |
return [ |
| 244 |
'reason' => $this->reason, |
| 245 |
'result' => self::RESULT_ERROR, |
| 246 |
'debug' => $this->debug, |
| 247 |
]; |
| 248 |
} |
| 249 |
|
| 250 |
private function get_progress_status() { |
| 251 |
$complete = 0; |
| 252 |
|
| 253 |
if ( $this->stage ) { |
| 254 |
$complete = round( ( array_search( $this->stage, $this->stages, true ) / count( $this->stages ) ) * 100, 1 ); |
| 255 |
} |
| 256 |
|
| 257 |
return [ |
| 258 |
'complete' => $complete, |
| 259 |
'result' => self::RESULT_OK, |
| 260 |
'reason' => $this->reason, |
| 261 |
]; |
| 262 |
} |
| 263 |
|
| 264 |
private function get_version_upgrade() { |
| 265 |
return [ |
| 266 |
'current' => $this->get_current_version() ? $this->get_current_version() : '-', |
| 267 |
'next' => REDIRECTION_DB_VERSION, |
| 268 |
'time' => microtime( true ), |
| 269 |
]; |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Set the status information for a database upgrade |
| 274 |
*/ |
| 275 |
public function start_install( array $upgrades ) { |
| 276 |
$this->set_stages( $upgrades ); |
| 277 |
$this->status = self::STATUS_NEED_INSTALL; |
| 278 |
} |
| 279 |
|
| 280 |
public function start_upgrade( array $upgrades ) { |
| 281 |
$this->set_stages( $upgrades ); |
| 282 |
$this->status = self::STATUS_NEED_UPDATING; |
| 283 |
} |
| 284 |
|
| 285 |
private function set_stages( array $upgrades ) { |
| 286 |
$this->stages = []; |
| 287 |
|
| 288 |
foreach ( $upgrades as $upgrade ) { |
| 289 |
$upgrader = Red_Database_Upgrader::get( $upgrade ); |
| 290 |
$this->stages = array_merge( $this->stages, array_keys( $upgrader->get_stages() ) ); |
| 291 |
} |
| 292 |
|
| 293 |
if ( count( $this->stages ) > 0 ) { |
| 294 |
$this->set_stage( $this->stages[0] ); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
public function set_stage( $stage ) { |
| 299 |
$this->stage = $stage; |
| 300 |
$this->save_details(); |
| 301 |
} |
| 302 |
|
| 303 |
private function save_details() { |
| 304 |
update_option( self::DB_UPGRADE_STAGE, [ |
| 305 |
'stage' => $this->stage, |
| 306 |
'stages' => $this->stages, |
| 307 |
'status' => $this->status, |
| 308 |
] ); |
| 309 |
|
| 310 |
$this->clear_cache(); |
| 311 |
} |
| 312 |
|
| 313 |
private function get_manual_upgrade() { |
| 314 |
$queries = []; |
| 315 |
$database = new Red_Database(); |
| 316 |
$upgraders = $database->get_upgrades_for_version( $this->get_current_version(), false ); |
| 317 |
|
| 318 |
foreach ( $upgraders as $upgrade ) { |
| 319 |
$upgrade = Red_Database_Upgrader::get( $upgrade ); |
| 320 |
|
| 321 |
$stages = $upgrade->get_stages(); |
| 322 |
foreach ( array_keys( $stages ) as $stage ) { |
| 323 |
$queries = array_merge( $queries, $upgrade->get_queries_for_stage( $stage ) ); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
return $queries; |
| 328 |
} |
| 329 |
|
| 330 |
private function get_next_stage( $stage ) { |
| 331 |
$database = new Red_Database(); |
| 332 |
$upgraders = $database->get_upgrades_for_version( $this->get_current_version(), $this->get_current_stage() ); |
| 333 |
|
| 334 |
if ( count( $upgraders ) === 0 ) { |
| 335 |
$upgraders = $database->get_upgrades_for_version( $this->get_current_version(), false ); |
| 336 |
} |
| 337 |
|
| 338 |
$upgrader = Red_Database_Upgrader::get( $upgraders[0] ); |
| 339 |
|
| 340 |
// Where are we in this? |
| 341 |
$pos = array_search( $this->stage, $this->stages, true ); |
| 342 |
|
| 343 |
if ( $pos === count( $this->stages ) - 1 ) { |
| 344 |
$this->save_db_version( REDIRECTION_DB_VERSION ); |
| 345 |
return false; |
| 346 |
} |
| 347 |
|
| 348 |
// Set current DB version |
| 349 |
$current_stages = array_keys( $upgrader->get_stages() ); |
| 350 |
|
| 351 |
if ( array_search( $this->stage, $current_stages, true ) === count( $current_stages ) - 1 ) { |
| 352 |
$this->save_db_version( $upgraders[1]['version'] ); |
| 353 |
} |
| 354 |
|
| 355 |
// Move on to next in current version |
| 356 |
return $this->stages[ $pos + 1 ]; |
| 357 |
} |
| 358 |
|
| 359 |
public function save_db_version( $version ) { |
| 360 |
red_set_options( array( 'database' => $version ) ); |
| 361 |
delete_option( self::OLD_DB_VERSION ); |
| 362 |
|
| 363 |
$this->clear_cache(); |
| 364 |
} |
| 365 |
|
| 366 |
private function clear_cache() { |
| 367 |
if ( file_exists( WP_CONTENT_DIR . '/object-cache.php' ) && function_exists( 'wp_cache_flush' ) ) { |
| 368 |
wp_cache_flush(); |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
|