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