| 1 |
<?php |
| 2 |
|
| 3 |
require_once __DIR__ . '/database-status.php'; |
| 4 |
require_once __DIR__ . '/database-upgrade.php'; |
| 5 |
require_once __DIR__ . '/database-upgrader.php'; |
| 6 |
|
| 7 |
class Red_Database { |
| 8 |
/** |
| 9 |
* Get all upgrades for a database version |
| 10 |
* |
| 11 |
* @param string $current_version |
| 12 |
* @param string|false $current_stage |
| 13 |
* @return list<Red_Database_Upgrade> Array of versions from self::get_upgrades() |
| 14 |
*/ |
| 15 |
public function get_upgrades_for_version( $current_version, $current_stage ) { |
| 16 |
if ( empty( $current_version ) ) { |
| 17 |
return [ |
| 18 |
new Red_Database_Upgrade( REDIRECTION_DB_VERSION, 'latest.php', 'Red_Latest_Database' ), |
| 19 |
]; |
| 20 |
} |
| 21 |
|
| 22 |
$upgraders = []; |
| 23 |
$found = false; |
| 24 |
|
| 25 |
foreach ( $this->get_upgrades() as $upgrade ) { |
| 26 |
if ( ! $found ) { |
| 27 |
$upgrader = Red_Database_Upgrader::get( $upgrade ); |
| 28 |
|
| 29 |
$stage_present = is_string( $current_stage ) && in_array( $current_stage, array_keys( $upgrader->get_stages() ), true ); |
| 30 |
$same_version = $current_stage === false && version_compare( $upgrade->get_version(), $current_version, 'gt' ); |
| 31 |
|
| 32 |
if ( $stage_present || $same_version ) { |
| 33 |
$found = true; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
if ( $found ) { |
| 38 |
$upgraders[] = $upgrade; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
return $upgraders; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Apply a particular upgrade stage |
| 47 |
* |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
public function apply_upgrade( Red_Database_Status $status ) { |
| 51 |
$upgraders = $this->get_upgrades_for_version( $status->get_current_version(), $status->get_current_stage() ); |
| 52 |
|
| 53 |
if ( count( $upgraders ) === 0 ) { |
| 54 |
$status->set_error( 'No upgrades found for version ' . $status->get_current_version() ); |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
if ( $status->get_current_stage() === false ) { |
| 59 |
if ( $status->needs_installing() ) { |
| 60 |
$status->start_install( $upgraders ); |
| 61 |
} else { |
| 62 |
$status->start_upgrade( $upgraders ); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
// Look at first upgrade |
| 67 |
$upgrader = Red_Database_Upgrader::get( $upgraders[0] ); |
| 68 |
|
| 69 |
// Perform the upgrade |
| 70 |
$upgrader->perform_stage( $status ); |
| 71 |
|
| 72 |
if ( ! $status->is_error() ) { |
| 73 |
$status->set_next_stage(); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Apply a callback to all sites in a multisite network, or to the current site if not multisite. |
| 79 |
* |
| 80 |
* @param callable $callback Callback function to apply to each site. |
| 81 |
* @return void |
| 82 |
*/ |
| 83 |
public static function apply_to_sites( $callback ) { |
| 84 |
if ( is_multisite() && ( is_network_admin() || ( defined( 'WP_CLI' ) && WP_CLI ) ) ) { |
| 85 |
$total = get_sites( [ 'count' => true ] ); |
| 86 |
$per_page = 100; |
| 87 |
|
| 88 |
// Paginate through all sites and apply the callback |
| 89 |
for ( $offset = 0; $offset < $total; $offset += $per_page ) { |
| 90 |
array_map( |
| 91 |
function ( $site ) use ( $callback ) { |
| 92 |
switch_to_blog( (int) $site->blog_id ); |
| 93 |
|
| 94 |
$callback(); |
| 95 |
|
| 96 |
restore_current_blog(); |
| 97 |
}, |
| 98 |
get_sites( [ 'number' => $per_page, 'offset' => $offset ] ) |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
$callback(); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Get latest database installer |
| 110 |
* |
| 111 |
* @return Red_Latest_Database Red_Latest_Database |
| 112 |
*/ |
| 113 |
public static function get_latest_database() { |
| 114 |
include_once __DIR__ . '/schema/latest.php'; |
| 115 |
|
| 116 |
return new Red_Latest_Database(); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* List of all upgrades and their associated file |
| 121 |
* |
| 122 |
* @return list<Red_Database_Upgrade> Database upgrade array |
| 123 |
*/ |
| 124 |
public function get_upgrades() { |
| 125 |
return [ |
| 126 |
new Red_Database_Upgrade( '2.0.1', '201.php', 'Red_Database_201' ), |
| 127 |
new Red_Database_Upgrade( '2.1.16', '216.php', 'Red_Database_216' ), |
| 128 |
new Red_Database_Upgrade( '2.2', '220.php', 'Red_Database_220' ), |
| 129 |
new Red_Database_Upgrade( '2.3.1', '231.php', 'Red_Database_231' ), |
| 130 |
new Red_Database_Upgrade( '2.3.2', '232.php', 'Red_Database_232' ), |
| 131 |
new Red_Database_Upgrade( '2.3.3', '233.php', 'Red_Database_233' ), |
| 132 |
new Red_Database_Upgrade( '2.4', '240.php', 'Red_Database_240' ), |
| 133 |
new Red_Database_Upgrade( '4.0', '400.php', 'Red_Database_400' ), |
| 134 |
new Red_Database_Upgrade( '4.1', '410.php', 'Red_Database_410' ), |
| 135 |
new Red_Database_Upgrade( '4.2', '420.php', 'Red_Database_420' ), |
| 136 |
]; |
| 137 |
} |
| 138 |
} |
| 139 |
|