Completed
Branch BUG-8957-add-countries (d46858)
by
unknown
31:05 queued 15:34
created

TableAnalysis::tableExists()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 42
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 30
nc 3
nop 1
dl 0
loc 42
rs 8.5806
c 0
b 0
f 0
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