PluginProbe
Redirection / 4.3
Redirection v4.3
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / database / database-upgrader.php

database-upgrader.php in Redirection 4.3, at database/database-upgrader.php

125 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 abstract class Red_Database_Upgrader {
4 private $queries = [];
5 private $live = true;
6
7 /**
8 * Return an array of all the stages for an upgrade
9 *
10 * @return array stage name => reason
11 */
12 abstract public function get_stages();
13
14 public function get_reason( $stage ) {
15 $stages = $this->get_stages();
16
17 if ( isset( $stages[ $stage ] ) ) {
18 return $stages[ $stage ];
19 }
20
21 return 'Unknown';
22 }
23
24 /**
25 * Run a particular stage on the current upgrader
26 *
27 * @return Red_Database_Status
28 */
29 public function perform_stage( Red_Database_Status $status ) {
30 global $wpdb;
31
32 $stage = $status->get_current_stage();
33 if ( $this->has_stage( $stage ) && method_exists( $this, $stage ) ) {
34 try {
35 $this->$stage( $wpdb );
36 $status->set_ok( $this->get_reason( $stage ) );
37 } catch ( Exception $e ) {
38 $status->set_error( $e->getMessage() );
39 }
40 } else {
41 $status->set_error( 'No stage found for upgrade ' . $stage );
42 }
43 }
44
45 public function get_queries_for_stage( $stage ) {
46 global $wpdb;
47
48 $this->queries = [];
49 $this->live = false;
50 $this->$stage( $wpdb );
51 $this->live = true;
52
53 return $this->queries;
54 }
55
56 /**
57 * Returns the current database charset
58 *
59 * @return string Database charset
60 */
61 public function get_charset() {
62 global $wpdb;
63
64 $charset_collate = '';
65 if ( ! empty( $wpdb->charset ) ) {
66 // Fix some common invalid charset values
67 $fixes = [
68 'utf-8',
69 'utf',
70 ];
71
72 $charset = $wpdb->charset;
73 if ( in_array( strtolower( $charset ), $fixes, true ) ) {
74 $charset = 'utf8';
75 }
76
77 $charset_collate = "DEFAULT CHARACTER SET $charset";
78 }
79
80 if ( ! empty( $wpdb->collate ) ) {
81 $charset_collate .= " COLLATE=$wpdb->collate";
82 }
83
84 return $charset_collate;
85 }
86
87 /**
88 * Performs a $wpdb->query, and throws an exception if an error occurs
89 *
90 * @return bool true if query is performed ok, otherwise an exception is thrown
91 */
92 protected function do_query( $wpdb, $sql ) {
93 if ( ! $this->live ) {
94 $this->queries[] = $sql;
95 return true;
96 }
97
98 // These are known queries without user input
99 // phpcs:ignore
100 $result = $wpdb->query( $sql );
101
102 if ( $result === false ) {
103 /* translators: 1: SQL string */
104 throw new Exception( sprintf( __( 'Failed to perform query "%s"' ), $sql ) );
105 }
106
107 return true;
108 }
109
110 /**
111 * Load a database upgrader class
112 *
113 * @return object Database upgrader
114 */
115 public static function get( $version ) {
116 include_once dirname( __FILE__ ) . '/schema/' . str_replace( [ '..', '/' ], '', $version['file'] );
117
118 return new $version['class'];
119 }
120
121 private function has_stage( $stage ) {
122 return in_array( $stage, array_keys( $this->get_stages() ), true );
123 }
124 }
125