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