-
+ FE45BD9121AB85B9C5B6557658CF803C4431E9160905BF4A9CC0E29B1031C511AE38FE4F159A9CFD4D329869F194F2FF1CD66C641FBD1E14ECF8267BCC686025
mp-wp/wp-admin/install-helper.php
(0 . 0)(1 . 225)
48021 <?php
48022 /**
48023 * Plugins may load this file to gain access to special helper functions for
48024 * plugin installation. This file is not included by WordPress and it is
48025 * recommended, to prevent fatal errors, that this file is included using
48026 * require_once().
48027 *
48028 * These functions are not optimized for speed, but they should only be used
48029 * once in a while, so speed shouldn't be a concern. If it is and you are
48030 * needing to use these functions a lot, you might experience time outs. If you
48031 * do, then it is advised to just write the SQL code yourself.
48032 *
48033 * You can turn debugging on, by setting $debug to 1 after you include this
48034 * file.
48035 *
48036 * <code>
48037 * check_column('wp_links', 'link_description', 'mediumtext');
48038 * if (check_column($wpdb->comments, 'comment_author', 'tinytext'))
48039 * echo "ok\n";
48040 *
48041 * $error_count = 0;
48042 * $tablename = $wpdb->links;
48043 * // check the column
48044 * if (!check_column($wpdb->links, 'link_description', 'varchar(255)')) {
48045 * $ddl = "ALTER TABLE $wpdb->links MODIFY COLUMN link_description varchar(255) NOT NULL DEFAULT '' ";
48046 * $q = $wpdb->query($ddl);
48047 * }
48048 *
48049 * if (check_column($wpdb->links, 'link_description', 'varchar(255)')) {
48050 * $res .= $tablename . ' - ok <br />';
48051 * } else {
48052 * $res .= 'There was a problem with ' . $tablename . '<br />';
48053 * ++$error_count;
48054 * }
48055 * </code>
48056 *
48057 * @package WordPress
48058 * @subpackage Plugin
48059 */
48060
48061 /**
48062 * @global bool $wp_only_load_config
48063 * @name $wp_only_load_config
48064 * @var bool
48065 * @since unknown
48066 */
48067 $wp_only_load_config = true;
48068
48069 /** Load WordPress Bootstrap */
48070 require_once(dirname(dirname(__FILE__)).'/wp-load.php');
48071
48072 /**
48073 * Turn debugging on or off.
48074 * @global bool|int $debug
48075 * @name $debug
48076 * @var bool|int
48077 * @since unknown
48078 */
48079 $debug = 0;
48080
48081 if ( ! function_exists('maybe_create_table') ) :
48082 /**
48083 * Create database table, if it doesn't already exist.
48084 *
48085 * @since unknown
48086 * @package WordPress
48087 * @subpackage Plugin
48088 * @uses $wpdb
48089 *
48090 * @param string $table_name Database table name.
48091 * @param string $create_ddl Create database table SQL.
48092 * @return bool False on error, true if already exists or success.
48093 */
48094 function maybe_create_table($table_name, $create_ddl) {
48095 global $wpdb;
48096 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
48097 if ($table == $table_name) {
48098 return true;
48099 }
48100 }
48101 //didn't find it try to create it.
48102 $wpdb->query($create_ddl);
48103 // we cannot directly tell that whether this succeeded!
48104 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
48105 if ($table == $table_name) {
48106 return true;
48107 }
48108 }
48109 return false;
48110 }
48111 endif;
48112
48113 if ( ! function_exists('maybe_add_column') ) :
48114 /**
48115 * Add column to database table, if column doesn't already exist in table.
48116 *
48117 * @since unknown
48118 * @package WordPress
48119 * @subpackage Plugin
48120 * @uses $wpdb
48121 * @uses $debug
48122 *
48123 * @param string $table_name Database table name
48124 * @param string $column_name Table column name
48125 * @param string $create_ddl SQL to add column to table.
48126 * @return bool False on failure. True, if already exists or was successful.
48127 */
48128 function maybe_add_column($table_name, $column_name, $create_ddl) {
48129 global $wpdb, $debug;
48130 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
48131 if ($debug) echo("checking $column == $column_name<br />");
48132
48133 if ($column == $column_name) {
48134 return true;
48135 }
48136 }
48137 //didn't find it try to create it.
48138 $wpdb->query($create_ddl);
48139 // we cannot directly tell that whether this succeeded!
48140 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
48141 if ($column == $column_name) {
48142 return true;
48143 }
48144 }
48145 return false;
48146 }
48147 endif;
48148
48149 /**
48150 * Drop column from database table, if it exists.
48151 *
48152 * @since unknown
48153 * @package WordPress
48154 * @subpackage Plugin
48155 * @uses $wpdb
48156 *
48157 * @param string $table_name Table name
48158 * @param string $column_name Column name
48159 * @param string $drop_ddl SQL statement to drop column.
48160 * @return bool False on failure, true on success or doesn't exist.
48161 */
48162 function maybe_drop_column($table_name, $column_name, $drop_ddl) {
48163 global $wpdb;
48164 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
48165 if ($column == $column_name) {
48166 //found it try to drop it.
48167 $wpdb->query($drop_ddl);
48168 // we cannot directly tell that whether this succeeded!
48169 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
48170 if ($column == $column_name) {
48171 return false;
48172 }
48173 }
48174 }
48175 }
48176 // else didn't find it
48177 return true;
48178 }
48179
48180 /**
48181 * Check column matches criteria.
48182 *
48183 * Uses the SQL DESC for retrieving the table info for the column. It will help
48184 * understand the parameters, if you do more research on what column information
48185 * is returned by the SQL statement. Pass in null to skip checking that
48186 * criteria.
48187 *
48188 * Column names returned from DESC table are case sensitive and are listed:
48189 * Field
48190 * Type
48191 * Null
48192 * Key
48193 * Default
48194 * Extra
48195 *
48196 * @since unknown
48197 * @package WordPress
48198 * @subpackage Plugin
48199 *
48200 * @param string $table_name Table name
48201 * @param string $col_name Column name
48202 * @param string $col_type Column type
48203 * @param bool $is_null Optional. Check is null.
48204 * @param mixed $key Optional. Key info.
48205 * @param mixed $default Optional. Default value.
48206 * @param mixed $extra Optional. Extra value.
48207 * @return bool True, if matches. False, if not matching.
48208 */
48209 function check_column($table_name, $col_name, $col_type, $is_null = null, $key = null, $default = null, $extra = null) {
48210 global $wpdb, $debug;
48211 $diffs = 0;
48212 $results = $wpdb->get_results("DESC $table_name");
48213
48214 foreach ($results as $row ) {
48215 if ($debug > 1) print_r($row);
48216
48217 if ($row->Field == $col_name) {
48218 // got our column, check the params
48219 if ($debug) echo ("checking $row->Type against $col_type\n");
48220 if (($col_type != null) && ($row->Type != $col_type)) {
48221 ++$diffs;
48222 }
48223 if (($is_null != null) && ($row->Null != $is_null)) {
48224 ++$diffs;
48225 }
48226 if (($key != null) && ($row->Key != $key)) {
48227 ++$diffs;
48228 }
48229 if (($default != null) && ($row->Default != $default)) {
48230 ++$diffs;
48231 }
48232 if (($extra != null) && ($row->Extra != $extra)) {
48233 ++$diffs;
48234 }
48235 if ($diffs > 0) {
48236 if ($debug) echo ("diffs = $diffs returning false\n");
48237 return false;
48238 }
48239 return true;
48240 } // end if found our column
48241 }
48242 return false;
48243 }
48244
48245 ?>