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