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