1
|
|
|
<?php |
2
|
|
|
namespace EventEspresso\core\services\database; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* |
6
|
|
|
* Class TableAnalysis |
7
|
|
|
* |
8
|
|
|
* For analyzing database tables; should not perform any manipulation, or have |
9
|
|
|
* any EE business logic |
10
|
|
|
* |
11
|
|
|
* @package Event Espresso |
12
|
|
|
* @subpackage |
13
|
|
|
* @author Mike Nelson |
14
|
|
|
* @since $VID:$ |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
class TableAnalysis extends \EE_Base { |
18
|
|
|
/** |
19
|
|
|
* Returns the table name which will definitely have the wpdb prefix on the front, |
20
|
|
|
* except if it currently has the wpdb->base_prefix on the front, in which case |
21
|
|
|
* it will have the wpdb->base_prefix on it |
22
|
|
|
* |
23
|
|
|
* @global \wpdb $wpdb |
24
|
|
|
* @param string $table_name |
25
|
|
|
* @return string $tableName, having ensured it has the wpdb prefix on the front |
26
|
|
|
*/ |
27
|
|
|
public function ensureTableNameHasPrefix( $table_name ) |
28
|
|
|
{ |
29
|
|
|
global $wpdb; |
30
|
|
|
return strpos( $table_name, $wpdb->base_prefix ) === 0 ? $table_name : $wpdb->prefix . $table_name; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Indicates whether or not the table has any entries. $table_name can |
37
|
|
|
* optionally start with $wpdb->prefix or not |
38
|
|
|
* @global \wpdb $wpdb |
39
|
|
|
* @param string $table_name |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
|
public function tableIsEmpty( $table_name ) |
43
|
|
|
{ |
44
|
|
|
global $wpdb; |
45
|
|
|
$table_name = $this->ensureTableNameHasPrefix( $table_name ); |
46
|
|
|
if ( $this->tableExists( $table_name ) ) { |
47
|
|
|
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $table_name" ); |
48
|
|
|
return absint( $count ) === 0 ? true : false; |
49
|
|
|
} |
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Indicates whether or not the table exists. $table_name can optionally |
57
|
|
|
* have the $wpdb->prefix on the beginning, or not. |
58
|
|
|
* @global \wpdb $wpdb |
59
|
|
|
* @global array EZSQL_Error |
60
|
|
|
* @param $table_name |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
public function tableExists( $table_name ) |
64
|
|
|
{ |
65
|
|
|
global $wpdb, $EZSQL_ERROR; |
66
|
|
|
$table_name = $this->ensureTableNameHasPrefix( $table_name ); |
67
|
|
|
//ignore if this causes an sql error |
68
|
|
|
$old_error = $wpdb->last_error; |
69
|
|
|
$old_suppress_errors = $wpdb->suppress_errors(); |
70
|
|
|
$old_show_errors_value = $wpdb->show_errors( FALSE ); |
71
|
|
|
$ezsql_error_cache = $EZSQL_ERROR; |
72
|
|
|
$wpdb->get_results( "SELECT * from $table_name LIMIT 1"); |
73
|
|
|
$wpdb->show_errors( $old_show_errors_value ); |
74
|
|
|
$wpdb->suppress_errors( $old_suppress_errors ); |
75
|
|
|
$new_error = $wpdb->last_error; |
76
|
|
|
$wpdb->last_error = $old_error; |
77
|
|
|
$EZSQL_ERROR = $ezsql_error_cache; |
78
|
|
|
//if there was a table doesn't exist error |
79
|
|
|
if( ! empty( $new_error ) ) { |
80
|
|
|
if( |
81
|
|
|
in_array( |
82
|
|
|
\EEH_Activation::last_wpdb_error_code(), |
83
|
|
|
array( |
84
|
|
|
1051, //bad table |
85
|
|
|
1109, //unknown table |
86
|
|
|
117, //no such table |
87
|
|
|
) |
88
|
|
|
) |
89
|
|
|
|| |
90
|
|
|
preg_match( '~^Table .* doesn\'t exist~', $new_error ) //in case not using mysql and error codes aren't reliable, just check for this error string |
91
|
|
|
) { |
92
|
|
|
return false; |
93
|
|
|
} else { |
94
|
|
|
//log this because that's weird. Just use the normal PHP error log |
95
|
|
|
error_log( |
96
|
|
|
sprintf( |
97
|
|
|
__( 'Event Espresso error detected when checking if table existed: %1$s (it wasn\'t just that the table didn\'t exist either)', 'event_espresso' ), |
98
|
|
|
$new_error |
99
|
|
|
) |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param $table_name |
110
|
|
|
* @param $index_name |
111
|
|
|
* @return array of columns used on that index, Each entry is an object with the following properties { |
112
|
|
|
* @type string Table |
113
|
|
|
* @type string Non_unique "0" or "1" |
114
|
|
|
* @type string Key_name |
115
|
|
|
* @type string Seq_in_index |
116
|
|
|
* @type string Column_name |
117
|
|
|
* @type string Collation |
118
|
|
|
* @type string Cardinality |
119
|
|
|
* @type string Sub_part on a column, usually this is just the number of characters from this column to use in indexing |
120
|
|
|
* @type string|null Packed |
121
|
|
|
* @type string Null |
122
|
|
|
* @type string Index_type |
123
|
|
|
* @type string Comment |
124
|
|
|
* @type string Index_comment |
125
|
|
|
* } |
126
|
|
|
*/ |
127
|
|
|
public function showIndexes($table_name, $index_name){ |
128
|
|
|
global $wpdb; |
129
|
|
|
$table_name = $this->ensureTableNameHasPrefix($table_name); |
130
|
|
|
$index_exists_query = "SHOW INDEX FROM {$table_name} WHERE Key_name = '{$index_name}'"; |
131
|
|
|
return $wpdb->get_results($index_exists_query); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|