| 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 | /** Load WordPress Bootstrap */
|
|---|
| 42 | require_once(dirname(dirname(__FILE__)).'/wp-load.php');
|
|---|
| 43 |
|
|---|
| 44 | /**
|
|---|
| 45 | * Turn debugging on or off.
|
|---|
| 46 | * @global bool|int $debug
|
|---|
| 47 | * @name $debug
|
|---|
| 48 | * @var bool|int
|
|---|
| 49 | * @since unknown
|
|---|
| 50 | */
|
|---|
| 51 | $debug = 0;
|
|---|
| 52 |
|
|---|
| 53 | if ( ! function_exists('maybe_create_table') ) :
|
|---|
| 54 | /**
|
|---|
| 55 | * Create database table, if it doesn't already exist.
|
|---|
| 56 | *
|
|---|
| 57 | * @since unknown
|
|---|
| 58 | * @package WordPress
|
|---|
| 59 | * @subpackage Plugin
|
|---|
| 60 | * @uses $wpdb
|
|---|
| 61 | *
|
|---|
| 62 | * @param string $table_name Database table name.
|
|---|
| 63 | * @param string $create_ddl Create database table SQL.
|
|---|
| 64 | * @return bool False on error, true if already exists or success.
|
|---|
| 65 | */
|
|---|
| 66 | function maybe_create_table($table_name, $create_ddl) {
|
|---|
| 67 | global $wpdb;
|
|---|
| 68 | if(strtolower($wpdb->get_var("SHOW TABLES LIKE '$table_name'")) == strtolower($table_name)) {
|
|---|
| 69 | return true;
|
|---|
| 70 | }
|
|---|
| 71 | // didn't find it try to create it.
|
|---|
| 72 | $wpdb->query($create_ddl);
|
|---|
| 73 | // we cannot directly tell that whether this succeeded!
|
|---|
| 74 | return strtolower($wpdb->get_var("SHOW TABLES LIKE '$table_name'")) == strtolower($table_name);
|
|---|
| 75 | }
|
|---|
| 76 | endif;
|
|---|
| 77 |
|
|---|
| 78 | if ( ! function_exists('maybe_add_column') ) :
|
|---|
| 79 | /**
|
|---|
| 80 | * Add column to database table, if column doesn't already exist in table.
|
|---|
| 81 | *
|
|---|
| 82 | * @since unknown
|
|---|
| 83 | * @package WordPress
|
|---|
| 84 | * @subpackage Plugin
|
|---|
| 85 | * @uses $wpdb
|
|---|
| 86 | * @uses $debug
|
|---|
| 87 | *
|
|---|
| 88 | * @param string $table_name Database table name
|
|---|
| 89 | * @param string $column_name Table column name
|
|---|
| 90 | * @param string $create_ddl SQL to add column to table.
|
|---|
| 91 | * @return bool False on failure. True, if already exists or was successful.
|
|---|
| 92 | */
|
|---|
| 93 | function maybe_add_column($table_name, $column_name, $create_ddl) {
|
|---|
| 94 | global $wpdb, $debug;
|
|---|
| 95 | foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
|
|---|
| 96 | if ($debug) echo("checking $column == $column_name<br />");
|
|---|
| 97 |
|
|---|
| 98 | if ($column == $column_name) {
|
|---|
| 99 | return true;
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 | //didn't find it try to create it.
|
|---|
| 103 | $wpdb->query($create_ddl);
|
|---|
| 104 | // we cannot directly tell that whether this succeeded!
|
|---|
| 105 | foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
|
|---|
| 106 | if ($column == $column_name) {
|
|---|
| 107 | return true;
|
|---|
| 108 | }
|
|---|
| 109 | }
|
|---|
| 110 | return false;
|
|---|
| 111 | }
|
|---|
| 112 | endif;
|
|---|
| 113 |
|
|---|
| 114 | /**
|
|---|
| 115 | * Drop column from database table, if it exists.
|
|---|
| 116 | *
|
|---|
| 117 | * @since unknown
|
|---|
| 118 | * @package WordPress
|
|---|
| 119 | * @subpackage Plugin
|
|---|
| 120 | * @uses $wpdb
|
|---|
| 121 | *
|
|---|
| 122 | * @param string $table_name Table name
|
|---|
| 123 | * @param string $column_name Column name
|
|---|
| 124 | * @param string $drop_ddl SQL statement to drop column.
|
|---|
| 125 | * @return bool False on failure, true on success or doesn't exist.
|
|---|
| 126 | */
|
|---|
| 127 | function maybe_drop_column($table_name, $column_name, $drop_ddl) {
|
|---|
| 128 | global $wpdb;
|
|---|
| 129 | foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
|
|---|
| 130 | if ($column == $column_name) {
|
|---|
| 131 | //found it try to drop it.
|
|---|
| 132 | $wpdb->query($drop_ddl);
|
|---|
| 133 | // we cannot directly tell that whether this succeeded!
|
|---|
| 134 | foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
|
|---|
| 135 | if ($column == $column_name) {
|
|---|
| 136 | return false;
|
|---|
| 137 | }
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|
| 141 | // else didn't find it
|
|---|
| 142 | return true;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | /**
|
|---|
| 146 | * Check column matches criteria.
|
|---|
| 147 | *
|
|---|
| 148 | * Uses the SQL DESC for retrieving the table info for the column. It will help
|
|---|
| 149 | * understand the parameters, if you do more research on what column information
|
|---|
| 150 | * is returned by the SQL statement. Pass in null to skip checking that
|
|---|
| 151 | * criteria.
|
|---|
| 152 | *
|
|---|
| 153 | * Column names returned from DESC table are case sensitive and are listed:
|
|---|
| 154 | * Field
|
|---|
| 155 | * Type
|
|---|
| 156 | * Null
|
|---|
| 157 | * Key
|
|---|
| 158 | * Default
|
|---|
| 159 | * Extra
|
|---|
| 160 | *
|
|---|
| 161 | * @since unknown
|
|---|
| 162 | * @package WordPress
|
|---|
| 163 | * @subpackage Plugin
|
|---|
| 164 | *
|
|---|
| 165 | * @param string $table_name Table name
|
|---|
| 166 | * @param string $col_name Column name
|
|---|
| 167 | * @param string $col_type Column type
|
|---|
| 168 | * @param bool $is_null Optional. Check is null.
|
|---|
| 169 | * @param mixed $key Optional. Key info.
|
|---|
| 170 | * @param mixed $default Optional. Default value.
|
|---|
| 171 | * @param mixed $extra Optional. Extra value.
|
|---|
| 172 | * @return bool True, if matches. False, if not matching.
|
|---|
| 173 | */
|
|---|
| 174 | function check_column($table_name, $col_name, $col_type, $is_null = null, $key = null, $default = null, $extra = null) {
|
|---|
| 175 | global $wpdb, $debug;
|
|---|
| 176 | $diffs = 0;
|
|---|
| 177 | $results = $wpdb->get_results("DESC $table_name");
|
|---|
| 178 |
|
|---|
| 179 | foreach ($results as $row ) {
|
|---|
| 180 | if ($debug > 1) print_r($row);
|
|---|
| 181 |
|
|---|
| 182 | if ($row->Field == $col_name) {
|
|---|
| 183 | // got our column, check the params
|
|---|
| 184 | if ($debug) echo ("checking $row->Type against $col_type\n");
|
|---|
| 185 | if (($col_type != null) && ($row->Type != $col_type)) {
|
|---|
| 186 | ++$diffs;
|
|---|
| 187 | }
|
|---|
| 188 | if (($is_null != null) && ($row->Null != $is_null)) {
|
|---|
| 189 | ++$diffs;
|
|---|
| 190 | }
|
|---|
| 191 | if (($key != null) && ($row->Key != $key)) {
|
|---|
| 192 | ++$diffs;
|
|---|
| 193 | }
|
|---|
| 194 | if (($default != null) && ($row->Default != $default)) {
|
|---|
| 195 | ++$diffs;
|
|---|
| 196 | }
|
|---|
| 197 | if (($extra != null) && ($row->Extra != $extra)) {
|
|---|
| 198 | ++$diffs;
|
|---|
| 199 | }
|
|---|
| 200 | if ($diffs > 0) {
|
|---|
| 201 | if ($debug) echo ("diffs = $diffs returning false\n");
|
|---|
| 202 | return false;
|
|---|
| 203 | }
|
|---|
| 204 | return true;
|
|---|
| 205 | } // end if found our column
|
|---|
| 206 | }
|
|---|
| 207 | return false;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | ?>
|
|---|