wp/wp-admin/install-helper.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    33  * @package WordPress
    33  * @package WordPress
    34  * @subpackage Plugin
    34  * @subpackage Plugin
    35  */
    35  */
    36 
    36 
    37 /** Load WordPress Bootstrap */
    37 /** Load WordPress Bootstrap */
    38 require_once(dirname(dirname(__FILE__)).'/wp-load.php');
    38 require_once( dirname( dirname( __FILE__ ) ) . '/wp-load.php' );
    39 
    39 
    40 if ( ! function_exists('maybe_create_table') ) :
    40 if ( ! function_exists( 'maybe_create_table' ) ) :
    41 /**
    41 	/**
    42  * Create database table, if it doesn't already exist.
    42 	 * Create database table, if it doesn't already exist.
    43  *
    43 	 *
    44  * @since 1.0.0
    44 	 * @since 1.0.0
    45  *
    45 	 *
    46  * @global wpdb $wpdb WordPress database abstraction object.
    46 	 * @global wpdb $wpdb WordPress database abstraction object.
    47  *
    47 	 *
    48  * @param string $table_name Database table name.
    48 	 * @param string $table_name Database table name.
    49  * @param string $create_ddl Create database table SQL.
    49 	 * @param string $create_ddl Create database table SQL.
    50  * @return bool False on error, true if already exists or success.
    50 	 * @return bool False on error, true if already exists or success.
    51  */
    51 	 */
    52 function maybe_create_table($table_name, $create_ddl) {
    52 	function maybe_create_table( $table_name, $create_ddl ) {
    53 	global $wpdb;
    53 		global $wpdb;
    54 	foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
    54 		foreach ( $wpdb->get_col( 'SHOW TABLES', 0 ) as $table ) {
    55 		if ($table == $table_name) {
    55 			if ( $table == $table_name ) {
    56 			return true;
    56 				return true;
    57 		}
    57 			}
    58 	}
    58 		}
    59 	// Didn't find it, so try to create it.
    59 		// Didn't find it, so try to create it.
    60 	$wpdb->query($create_ddl);
    60 		$wpdb->query( $create_ddl );
    61 
    61 
    62 	// We cannot directly tell that whether this succeeded!
    62 		// We cannot directly tell that whether this succeeded!
    63 	foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
    63 		foreach ( $wpdb->get_col( 'SHOW TABLES', 0 ) as $table ) {
    64 		if ($table == $table_name) {
    64 			if ( $table == $table_name ) {
    65 			return true;
    65 				return true;
    66 		}
    66 			}
    67 	}
    67 		}
    68 	return false;
    68 		return false;
    69 }
    69 	}
    70 endif;
    70 endif;
    71 
    71 
    72 if ( ! function_exists('maybe_add_column') ) :
    72 if ( ! function_exists( 'maybe_add_column' ) ) :
    73 /**
    73 	/**
    74  * Add column to database table, if column doesn't already exist in table.
    74 	 * Add column to database table, if column doesn't already exist in table.
    75  *
    75 	 *
    76  * @since 1.0.0
    76 	 * @since 1.0.0
    77  *
    77 	 *
    78  * @global wpdb $wpdb WordPress database abstraction object.
    78 	 * @global wpdb $wpdb WordPress database abstraction object.
    79  *
    79 	 *
    80  * @param string $table_name Database table name
    80 	 * @param string $table_name Database table name
    81  * @param string $column_name Table column name
    81 	 * @param string $column_name Table column name
    82  * @param string $create_ddl SQL to add column to table.
    82 	 * @param string $create_ddl SQL to add column to table.
    83  * @return bool False on failure. True, if already exists or was successful.
    83 	 * @return bool False on failure. True, if already exists or was successful.
    84  */
    84 	 */
    85 function maybe_add_column($table_name, $column_name, $create_ddl) {
    85 	function maybe_add_column( $table_name, $column_name, $create_ddl ) {
    86 	global $wpdb;
    86 		global $wpdb;
    87 	foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
    87 		foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) {
    88 
    88 
    89 		if ($column == $column_name) {
    89 			if ( $column == $column_name ) {
    90 			return true;
    90 				return true;
    91 		}
    91 			}
    92 	}
    92 		}
    93 
    93 
    94 	// Didn't find it, so try to create it.
    94 		// Didn't find it, so try to create it.
    95 	$wpdb->query($create_ddl);
    95 		$wpdb->query( $create_ddl );
    96 
    96 
    97 	// We cannot directly tell that whether this succeeded!
    97 		// We cannot directly tell that whether this succeeded!
    98 	foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
    98 		foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) {
    99 		if ($column == $column_name) {
    99 			if ( $column == $column_name ) {
   100 			return true;
   100 				return true;
   101 		}
   101 			}
   102 	}
   102 		}
   103 	return false;
   103 		return false;
   104 }
   104 	}
   105 endif;
   105 endif;
   106 
   106 
   107 /**
   107 /**
   108  * Drop column from database table, if it exists.
   108  * Drop column from database table, if it exists.
   109  *
   109  *
   114  * @param string $table_name Table name
   114  * @param string $table_name Table name
   115  * @param string $column_name Column name
   115  * @param string $column_name Column name
   116  * @param string $drop_ddl SQL statement to drop column.
   116  * @param string $drop_ddl SQL statement to drop column.
   117  * @return bool False on failure, true on success or doesn't exist.
   117  * @return bool False on failure, true on success or doesn't exist.
   118  */
   118  */
   119 function maybe_drop_column($table_name, $column_name, $drop_ddl) {
   119 function maybe_drop_column( $table_name, $column_name, $drop_ddl ) {
   120 	global $wpdb;
   120 	global $wpdb;
   121 	foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
   121 	foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) {
   122 		if ($column == $column_name) {
   122 		if ( $column == $column_name ) {
   123 
   123 
   124 			// Found it, so try to drop it.
   124 			// Found it, so try to drop it.
   125 			$wpdb->query($drop_ddl);
   125 			$wpdb->query( $drop_ddl );
   126 
   126 
   127 			// We cannot directly tell that whether this succeeded!
   127 			// We cannot directly tell that whether this succeeded!
   128 			foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
   128 			foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) {
   129 				if ($column == $column_name) {
   129 				if ( $column == $column_name ) {
   130 					return false;
   130 					return false;
   131 				}
   131 				}
   132 			}
   132 			}
   133 		}
   133 		}
   134 	}
   134 	}
   163  * @param mixed  $key        Optional. Key info.
   163  * @param mixed  $key        Optional. Key info.
   164  * @param mixed  $default    Optional. Default value.
   164  * @param mixed  $default    Optional. Default value.
   165  * @param mixed  $extra      Optional. Extra value.
   165  * @param mixed  $extra      Optional. Extra value.
   166  * @return bool True, if matches. False, if not matching.
   166  * @return bool True, if matches. False, if not matching.
   167  */
   167  */
   168 function check_column($table_name, $col_name, $col_type, $is_null = null, $key = null, $default = null, $extra = null) {
   168 function check_column( $table_name, $col_name, $col_type, $is_null = null, $key = null, $default = null, $extra = null ) {
   169 	global $wpdb;
   169 	global $wpdb;
   170 	$diffs = 0;
   170 	$diffs   = 0;
   171 	$results = $wpdb->get_results("DESC $table_name");
   171 	$results = $wpdb->get_results( "DESC $table_name" );
   172 
   172 
   173 	foreach ($results as $row ) {
   173 	foreach ( $results as $row ) {
   174 
   174 
   175 		if ($row->Field == $col_name) {
   175 		if ( $row->Field == $col_name ) {
   176 
   176 
   177 			// Got our column, check the params.
   177 			// Got our column, check the params.
   178 			if (($col_type != null) && ($row->Type != $col_type)) {
   178 			if ( ( $col_type != null ) && ( $row->Type != $col_type ) ) {
   179 				++$diffs;
   179 				++$diffs;
   180 			}
   180 			}
   181 			if (($is_null != null) && ($row->Null != $is_null)) {
   181 			if ( ( $is_null != null ) && ( $row->Null != $is_null ) ) {
   182 				++$diffs;
   182 				++$diffs;
   183 			}
   183 			}
   184 			if (($key != null) && ($row->Key  != $key)) {
   184 			if ( ( $key != null ) && ( $row->Key != $key ) ) {
   185 				++$diffs;
   185 				++$diffs;
   186 			}
   186 			}
   187 			if (($default != null) && ($row->Default != $default)) {
   187 			if ( ( $default != null ) && ( $row->Default != $default ) ) {
   188 				++$diffs;
   188 				++$diffs;
   189 			}
   189 			}
   190 			if (($extra != null) && ($row->Extra != $extra)) {
   190 			if ( ( $extra != null ) && ( $row->Extra != $extra ) ) {
   191 				++$diffs;
   191 				++$diffs;
   192 			}
   192 			}
   193 			if ($diffs > 0) {
   193 			if ( $diffs > 0 ) {
   194 				return false;
   194 				return false;
   195 			}
   195 			}
   196 			return true;
   196 			return true;
   197 		} // end if found our column
   197 		} // end if found our column
   198 	}
   198 	}