root/trunk/wp-admin/install-helper.php
| Revision 1519, 6.1 kB (checked in by donncha, 6 days ago) | |
|---|---|
| |
| Line | |
|---|---|
| 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 Plugin |
| 39 | */ |
| 40 | |
| 41 | /** |
| 42 | * @global bool $wp_only_load_config |
| 43 | * @name $wp_only_load_config |
| 44 | * @var bool |
| 45 | * @since unknown |
| 46 | */ |
| 47 | $wp_only_load_config = true; |
| 48 | |
| 49 | /** Load WordPress Bootstrap */ |
| 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 | */ |
| 59 | $debug = 0; |
| 60 | |
| 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 Plugin |
| 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 | */ |
| 74 | function maybe_create_table($table_name, $create_ddl) { |
| 75 | global $wpdb; |
| 76 | foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) { |
| 77 | if ($table == $table_name) { |
| 78 | return true; |
| 79 | } |
| 80 | } |
| 81 | //didn't find it try to create it. |
| 82 | $wpdb->query($create_ddl); |
| 83 | // we cannot directly tell that whether this succeeded! |
| 84 | foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) { |
| 85 | if ($table == $table_name) { |
| 86 | return true; |
| 87 | } |
| 88 | } |
| 89 | return false; |
| 90 | } |
| 91 | endif; |
| 92 | |
| 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 Plugin |
| 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 | */ |
| 108 | function maybe_add_column($table_name, $column_name, $create_ddl) { |
| 109 | global $wpdb, $debug; |
| 110 | foreach ($wpdb->get_col("DESC $table_name",0) as $column ) { |
| 111 | if ($debug) echo("checking $column == $column_name<br />"); |
| 112 | |
| 113 | if ($column == $column_name) { |
| 114 | return true; |
| 115 | } |
| 116 | } |
| 117 | //didn't find it try to create it. |
| 118 | $wpdb->query($create_ddl); |
| 119 | // we cannot directly tell that whether this succeeded! |
| 120 | foreach ($wpdb->get_col("DESC $table_name",0) as $column ) { |
| 121 | if ($column == $column_name) { |
| 122 | return true; |
| 123 | } |
| 124 | } |
| 125 | return false; |
| 126 | } |
| 127 | endif; |
| 128 | |
| 129 | /** |
| 130 | * Drop column from database table, if it exists. |
| 131 | * |
| 132 | * @since unknown |
| 133 | * @package WordPress |
| 134 | * @subpackage Plugin |
| 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. |
| 141 | */ |
| 142 | function maybe_drop_column($table_name, $column_name, $drop_ddl) { |
| 143 | global $wpdb; |
| 144 | foreach ($wpdb->get_col("DESC $table_name",0) as $column ) { |
| 145 | if ($column == $column_name) { |
| 146 | //found it try to drop it. |
| 147 | $wpdb->query($drop_ddl); |
| 148 | // we cannot directly tell that whether this succeeded! |
| 149 | foreach ($wpdb->get_col("DESC $table_name",0) as $column ) { |
| 150 | if ($column == $column_name) { |
| 151 | return false; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | // else didn't find it |
| 157 | return true; |
| 158 | } |
| 159 | |
| 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 | * @since unknown |
| 177 | * @package WordPress |
| 178 | * @subpackage Plugin |
| 179 | * |
| 180 | * @param string $table_name Table name |
| 181 | * @param string $col_name Column name |
| 182 | * @param string $col_type Column type |
| 183 | * @param bool $is_null Optional. Check is null. |
| 184 | * @param mixed $key Optional. Key info. |
| 185 | * @param mixed $default Optional. Default value. |
| 186 | * @param mixed $extra Optional. Extra value. |
| 187 | * @return bool True, if matches. False, if not matching. |
| 188 | */ |
| 189 | function check_column($table_name, $col_name, $col_type, $is_null = null, $key = null, $default = null, $extra = null) { |
| 190 | global $wpdb, $debug; |
| 191 | $diffs = 0; |
| 192 | $results = $wpdb->get_results("DESC $table_name"); |
| 193 | |
| 194 | foreach ($results as $row ) { |
| 195 | if ($debug > 1) print_r($row); |
| 196 | |
| 197 | if ($row->Field == $col_name) { |
| 198 | // got our column, check the params |
| 199 | if ($debug) echo ("checking $row->Type against $col_type\n"); |
| 200 | if (($col_type != null) && ($row->Type != $col_type)) { |
| 201 | ++$diffs; |
| 202 | } |
| 203 | if (($is_null != null) && ($row->Null != $is_null)) { |
| 204 | ++$diffs; |
| 205 | } |
| 206 | if (($key != null) && ($row->Key != $key)) { |
| 207 | ++$diffs; |
| 208 | } |
| 209 | if (($default != null) && ($row->Default != $default)) { |
| 210 | ++$diffs; |
| 211 | } |
| 212 | if (($extra != null) && ($row->Extra != $extra)) { |
| 213 | ++$diffs; |
| 214 | } |
| 215 | if ($diffs > 0) { |
| 216 | if ($debug) echo ("diffs = $diffs returning false\n"); |
| 217 | return false; |
| 218 | } |
| 219 | return true; |
| 220 | } // end if found our column |
| 221 | } |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | ?> |
| 226 |
Note: See TracBrowser for help on using the browser.
