Changeset 8645 for trunk/wp-admin/install-helper.php
- Timestamp:
- 08/14/2008 06:30:38 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-admin/install-helper.php
r8610 r8645 1 1 <?php 2 /** 3 * Plugins may load this file to gain access to special helper functions for 4 * plugin installation. This file is not included by WordPress and it is 5 * recommended, to prevent fatal errors, that this file is included using 6 * require_once(). 7 * 8 * These functions are not optimized for speed, but they should only be used 9 * once in a while, so speed shouldn't be a concern. If it is and you are 10 * needing to use these functions a lot, you might experience time outs. If you 11 * do, then it is advised to just write the SQL code yourself. 12 * 13 * You can turn debugging on, by setting $debug to 1 after you include this 14 * file. 15 * 16 * <code> 17 * check_column('wp_links', 'link_description', 'mediumtext'); 18 * if (check_column($wpdb->comments, 'comment_author', 'tinytext')) 19 * echo "ok\n"; 20 * 21 * $error_count = 0; 22 * $tablename = $wpdb->links; 23 * // check the column 24 * if (!check_column($wpdb->links, 'link_description', 'varchar(255)')) { 25 * $ddl = "ALTER TABLE $wpdb->links MODIFY COLUMN link_description varchar(255) NOT NULL DEFAULT '' "; 26 * $q = $wpdb->query($ddl); 27 * } 28 * 29 * if (check_column($wpdb->links, 'link_description', 'varchar(255)')) { 30 * $res .= $tablename . ' - ok <br />'; 31 * } else { 32 * $res .= 'There was a problem with ' . $tablename . '<br />'; 33 * ++$error_count; 34 * } 35 * </code> 36 * 37 * @package WordPress 38 * @subpackage Plugins 39 */ 40 41 /** 42 * @global bool $wp_only_load_config 43 * @name $wp_only_load_config 44 * @var bool 45 * @since unknown 46 */ 2 47 $wp_only_load_config = true; 48 49 /** Load WordPress Bootstrap */ 3 50 require_once(dirname(dirname(__FILE__)).'/wp-load.php'); 51 52 /** 53 * Turn debugging on or off. 54 * @global bool|int $debug 55 * @name $debug 56 * @var bool|int 57 * @since unknown 58 */ 4 59 $debug = 0; 5 60 6 /**7 ** maybe_create_table()8 ** Create db table if it doesn't exist.9 ** Returns: true if already exists or on successful completion10 ** false on error11 */12 61 if ( ! function_exists('maybe_create_table') ) : 62 /** 63 * Create database table, if it doesn't already exist. 64 * 65 * @since unknown 66 * @package WordPress 67 * @subpackage Plugins 68 * @uses $wpdb 69 * 70 * @param string $table_name Database table name. 71 * @param string $create_ddl Create database table SQL. 72 * @return bool False on error, true if already exists or success. 73 */ 13 74 function maybe_create_table($table_name, $create_ddl) { 14 75 global $wpdb; … … 30 91 endif; 31 92 32 /**33 ** maybe_add_column()34 ** Add column to db table if it doesn't exist.35 ** Returns: true if already exists or on successful completion36 ** false on error37 */38 93 if ( ! function_exists('maybe_add_column') ) : 94 /** 95 * Add column to database table, if column doesn't already exist in table. 96 * 97 * @since unknown 98 * @package WordPress 99 * @subpackage Plugins 100 * @uses $wpdb 101 * @uses $debug 102 * 103 * @param string $table_name Database table name 104 * @param string $column_name Table column name 105 * @param string $create_ddl SQL to add column to table. 106 * @return bool False on failure. True, if already exists or was successful. 107 */ 39 108 function maybe_add_column($table_name, $column_name, $create_ddl) { 40 109 global $wpdb, $debug; 41 110 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) { 42 111 if ($debug) echo("checking $column == $column_name<br />"); 43 if ($column == $column_name) { 44 return true; 45 } 112 113 if ($column == $column_name) { 114 return true; 115 } 46 116 } 47 117 //didn't find it try to create it. … … 58 128 59 129 /** 60 ** maybe_drop_column() 61 ** Drop column from db table if it exists. 62 ** Returns: true if it doesn't already exist or on successful drop 63 ** false on error 130 * Drop column from database table, if it exists. 131 * 132 * @since unknown 133 * @package WordPress 134 * @subpackage Plugins 135 * @uses $wpdb 136 * 137 * @param string $table_name Table name 138 * @param string $column_name Column name 139 * @param string $drop_ddl SQL statement to drop column. 140 * @return bool False on failure, true on success or doesn't exist. 64 141 */ 65 142 function maybe_drop_column($table_name, $column_name, $drop_ddl) { … … 81 158 } 82 159 83 84 /** 85 ** check_column() 86 ** Check column matches passed in criteria. 87 ** Pass in null to skip checking that criteria 88 ** Returns: true if it matches 89 ** false otherwise 90 ** (case sensitive) Column names returned from DESC table are: 91 ** Field 92 ** Type 93 ** Null 94 ** Key 95 ** Default 96 ** Extra 160 /** 161 * Check column matches criteria. 162 * 163 * Uses the SQL DESC for retrieving the table info for the column. It will help 164 * understand the parameters, if you do more research on what column information 165 * is returned by the SQL statement. Pass in null to skip checking that 166 * criteria. 167 * 168 * Column names returned from DESC table are case sensitive and are listed: 169 * Field 170 * Type 171 * Null 172 * Key 173 * Default 174 * Extra 175 * 176 * @param string $table_name Table name 177 * @param string $col_name Column name 178 * @param string $col_type Column type 179 * @param bool $is_null Optional. Check is null. 180 * @param mixed $key Optional. Key info. 181 * @param mixed $default Optional. Default value. 182 * @param mixed $extra Optional. Extra value. 183 * @return bool True, if matches. False, if not matching. 97 184 */ 98 185 function check_column($table_name, $col_name, $col_type, $is_null = null, $key = null, $default = null, $extra = null) { … … 103 190 foreach ($results as $row ) { 104 191 if ($debug > 1) print_r($row); 105 if ($row->Field == $col_name) { 106 // got our column, check the params 107 if ($debug) echo ("checking $row->Type against $col_type\n"); 108 if (($col_type != null) && ($row->Type != $col_type)) { 109 ++$diffs; 110 } 111 if (($is_null != null) && ($row->Null != $is_null)) { 112 ++$diffs; 113 } 114 if (($key != null) && ($row->Key != $key)) { 115 ++$diffs; 116 } 117 if (($default != null) && ($row->Default != $default)) { 118 ++$diffs; 119 } 120 if (($extra != null) && ($row->Extra != $extra)) { 121 ++$diffs; 122 } 123 if ($diffs > 0) { 124 if ($debug) echo ("diffs = $diffs returning false\n"); 125 return false; 126 } 127 return true; 128 } // end if found our column 192 193 if ($row->Field == $col_name) { 194 // got our column, check the params 195 if ($debug) echo ("checking $row->Type against $col_type\n"); 196 if (($col_type != null) && ($row->Type != $col_type)) { 197 ++$diffs; 198 } 199 if (($is_null != null) && ($row->Null != $is_null)) { 200 ++$diffs; 201 } 202 if (($key != null) && ($row->Key != $key)) { 203 ++$diffs; 204 } 205 if (($default != null) && ($row->Default != $default)) { 206 ++$diffs; 207 } 208 if (($extra != null) && ($row->Extra != $extra)) { 209 ++$diffs; 210 } 211 if ($diffs > 0) { 212 if ($debug) echo ("diffs = $diffs returning false\n"); 213 return false; 214 } 215 return true; 216 } // end if found our column 129 217 } 130 218 return false; 131 219 } 132 220 133 /*134 echo "<p>testing</p>";135 echo "<pre>";136 137 //check_column('wp_links', 'link_description', 'mediumtext');138 //if (check_column($wpdb->comments, 'comment_author', 'tinytext'))139 // echo "ok\n";140 $error_count = 0;141 $tablename = $wpdb->links;142 // check the column143 if (!check_column($wpdb->links, 'link_description', 'varchar(255)'))144 {145 $ddl = "ALTER TABLE $wpdb->links MODIFY COLUMN link_description varchar(255) NOT NULL DEFAULT '' ";146 $q = $wpdb->query($ddl);147 }148 if (check_column($wpdb->links, 'link_description', 'varchar(255)')) {149 $res .= $tablename . ' - ok <br />';150 } else {151 $res .= 'There was a problem with ' . $tablename . '<br />';152 ++$error_count;153 }154 echo "</pre>";155 */156 221 ?>
Note: See TracChangeset
for help on using the changeset viewer.