database-upgrader.php
2.62 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
<?php
abstract class Red_Database_Upgrader {
private $queries = [];
private $live = true;
/**
* Return an array of all the stages for an upgrade
*
* @return array stage name => reason
*/
abstract public function get_stages();
public function get_reason( $stage ) {
$stages = $this->get_stages();
if ( isset( $stages[ $stage ] ) ) {
return $stages[ $stage ];
}
return 'Unknown';
}
/**
* Run a particular stage on the current upgrader
*
* @return Red_Database_Status
*/
public function perform_stage( Red_Database_Status $status ) {
global $wpdb;
$stage = $status->get_current_stage();
if ( $this->has_stage( $stage ) && method_exists( $this, $stage ) ) {
try {
$this->$stage( $wpdb );
$status->set_ok( $this->get_reason( $stage ) );
} catch ( Exception $e ) {
$status->set_error( $e->getMessage() );
}
} else {
$status->set_error( 'No stage found for upgrade ' . $stage );
}
}
public function get_queries_for_stage( $stage ) {
global $wpdb;
$this->queries = [];
$this->live = false;
$this->$stage( $wpdb, false );
$this->live = true;
return $this->queries;
}
/**
* Returns the current database charset
*
* @return string Database charset
*/
public function get_charset() {
global $wpdb;
$charset_collate = '';
if ( ! empty( $wpdb->charset ) ) {
// Fix some common invalid charset values
$fixes = [
'utf-8',
'utf',
];
$charset = $wpdb->charset;
if ( in_array( strtolower( $charset ), $fixes, true ) ) {
$charset = 'utf8';
}
$charset_collate = "DEFAULT CHARACTER SET $charset";
}
if ( ! empty( $wpdb->collate ) ) {
$charset_collate .= " COLLATE=$wpdb->collate";
}
return $charset_collate;
}
/**
* Performs a $wpdb->query, and throws an exception if an error occurs
*
* @return bool true if query is performed ok, otherwise an exception is thrown
*/
protected function do_query( $wpdb, $sql ) {
if ( ! $this->live ) {
$this->queries[] = $sql;
return true;
}
// These are known queries without user input
// phpcs:ignore
$result = $wpdb->query( $sql );
if ( $result === false ) {
/* translators: 1: SQL string */
throw new Exception( sprintf( 'Failed to perform query "%s"', $sql ) );
}
return true;
}
/**
* Load a database upgrader class
*
* @return object Database upgrader
*/
public static function get( $version ) {
include_once dirname( __FILE__ ) . '/schema/' . str_replace( [ '..', '/' ], '', $version['file'] );
return new $version['class'];
}
private function has_stage( $stage ) {
return in_array( $stage, array_keys( $this->get_stages() ), true );
}
}