2 $wp_only_load_config = true;
3 require_once(dirname(dirname(__FILE__)).'/wp-load.php');
7 ** maybe_create_table()
8 ** Create db table if it doesn't exist.
9 ** Returns: true if already exists or on successful completion
12 if ( ! function_exists('maybe_create_table') ) :
13 function maybe_create_table($table_name, $create_ddl) {
15 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
16 if ($table == $table_name) {
20 //didn't find it try to create it.
21 $wpdb->query($create_ddl);
22 // we cannot directly tell that whether this succeeded!
23 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
24 if ($table == $table_name) {
34 ** Add column to db table if it doesn't exist.
35 ** Returns: true if already exists or on successful completion
38 if ( ! function_exists('maybe_add_column') ) :
39 function maybe_add_column($table_name, $column_name, $create_ddl) {
41 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
42 if ($debug) echo("checking $column == $column_name<br />");
43 if ($column == $column_name) {
47 //didn't find it try to create it.
48 $wpdb->query($create_ddl);
49 // we cannot directly tell that whether this succeeded!
50 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
51 if ($column == $column_name) {
60 ** maybe_drop_column()
61 ** Drop column from db table if it exists.
62 ** Returns: true if it doesn't already exist or on successful drop
65 function maybe_drop_column($table_name, $column_name, $drop_ddl) {
67 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
68 if ($column == $column_name) {
69 //found it try to drop it.
70 $wpdb->query($drop_ddl);
71 // we cannot directly tell that whether this succeeded!
72 foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
73 if ($column == $column_name) {
79 // else didn't find it
86 ** Check column matches passed in criteria.
87 ** Pass in null to skip checking that criteria
88 ** Returns: true if it matches
90 ** (case sensitive) Column names returned from DESC table are:
98 function check_column($table_name, $col_name, $col_type, $is_null = null, $key = null, $default = null, $extra = null) {
101 $results = $wpdb->get_results("DESC $table_name");
103 foreach ($results as $row ) {
104 if ($debug > 1) print_r($row);
105 if ($row->Field == $col_name) {
106 // got our column, check the params
107 if ($debug) echo ("checking $row->Type against $col_type\n");
108 if (($col_type != null) && ($row->Type != $col_type)) {
111 if (($is_null != null) && ($row->Null != $is_null)) {
114 if (($key != null) && ($row->Key != $key)) {
117 if (($default != null) && ($row->Default != $default)) {
120 if (($extra != null) && ($row->Extra != $extra)) {
124 if ($debug) echo ("diffs = $diffs returning false\n");
128 } // end if found our column
134 echo "<p>testing</p>";
137 //check_column('wp_links', 'link_description', 'mediumtext');
138 //if (check_column($wpdb->comments, 'comment_author', 'tinytext'))
141 $tablename = $wpdb->links;
143 if (!check_column($wpdb->links, 'link_description', 'varchar(255)'))
145 $ddl = "ALTER TABLE $wpdb->links MODIFY COLUMN link_description varchar(255) NOT NULL DEFAULT '' ";
146 $q = $wpdb->query($ddl);
148 if (check_column($wpdb->links, 'link_description', 'varchar(255)')) {
149 $res .= $tablename . ' - ok <br />';
151 $res .= 'There was a problem with ' . $tablename . '<br />';