~canonical-sysadmins/wordpress/4.7.2

« back to all changes in this revision

Viewing changes to wp-admin/install-helper.php

  • Committer: Jacek Nykis
  • Date: 2015-01-05 16:17:05 UTC
  • Revision ID: jacek.nykis@canonical.com-20150105161705-w544l1h5mcg7u4w9
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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, so try to create it.
 
61
        $wpdb->query($create_ddl);
 
62
 
 
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
 *
 
79
 * @uses $wpdb
 
80
 *
 
81
 * @param string $table_name Database table name
 
82
 * @param string $column_name Table column name
 
83
 * @param string $create_ddl SQL to add column to table.
 
84
 * @return bool False on failure. True, if already exists or was successful.
 
85
 */
 
86
function maybe_add_column($table_name, $column_name, $create_ddl) {
 
87
        global $wpdb;
 
88
        foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
 
89
 
 
90
                if ($column == $column_name) {
 
91
                        return true;
 
92
                }
 
93
        }
 
94
 
 
95
        // Didn't find it, so try to create it.
 
96
        $wpdb->query($create_ddl);
 
97
 
 
98
        // We cannot directly tell that whether this succeeded!
 
99
        foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
 
100
                if ($column == $column_name) {
 
101
                        return true;
 
102
                }
 
103
        }
 
104
        return false;
 
105
}
 
106
endif;
 
107
 
 
108
/**
 
109
 * Drop column from database table, if it exists.
 
110
 *
 
111
 * @since 1.0.0
 
112
 *
 
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
 
 
125
                        // Found it, so try to drop it.
 
126
                        $wpdb->query($drop_ddl);
 
127
 
 
128
                        // We cannot directly tell that whether this succeeded!
 
129
                        foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
 
130
                                if ($column == $column_name) {
 
131
                                        return false;
 
132
                                }
 
133
                        }
 
134
                }
 
135
        }
 
136
        // Else didn't find it.
 
137
        return true;
 
138
}
 
139
 
 
140
/**
 
141
 * Check column matches criteria.
 
142
 *
 
143
 * Uses the SQL DESC for retrieving the table info for the column. It will help
 
144
 * understand the parameters, if you do more research on what column information
 
145
 * is returned by the SQL statement. Pass in null to skip checking that
 
146
 * criteria.
 
147
 *
 
148
 * Column names returned from DESC table are case sensitive and are listed:
 
149
 *      Field
 
150
 *      Type
 
151
 *      Null
 
152
 *      Key
 
153
 *      Default
 
154
 *      Extra
 
155
 *
 
156
 * @since 1.0.0
 
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
 
 
176
                        // Got our column, check the params.
 
177
                        if (($col_type != null) && ($row->Type != $col_type)) {
 
178
                                ++$diffs;
 
179
                        }
 
180
                        if (($is_null != null) && ($row->Null != $is_null)) {
 
181
                                ++$diffs;
 
182
                        }
 
183
                        if (($key != null) && ($row->Key  != $key)) {
 
184
                                ++$diffs;
 
185
                        }
 
186
                        if (($default != null) && ($row->Default != $default)) {
 
187
                                ++$diffs;
 
188
                        }
 
189
                        if (($extra != null) && ($row->Extra != $extra)) {
 
190
                                ++$diffs;
 
191
                        }
 
192
                        if ($diffs > 0) {
 
193
                                return false;
 
194
                        }
 
195
                        return true;
 
196
                } // end if found our column
 
197
        }
 
198
        return false;
 
199
}