home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Blogs / wordpress2.6.exe / wordpress2.6 / wp-admin / install-helper.php < prev    next >
Encoding:
PHP Script  |  2008-08-11  |  4.1 KB  |  157 lines

  1. <?php
  2. $wp_only_load_config = true;
  3. require_once(dirname(dirname(__FILE__)).'/wp-load.php');
  4. $debug = 0;
  5.  
  6. /**
  7.  ** maybe_create_table()
  8.  ** Create db table if it doesn't exist.
  9.  ** Returns:  true if already exists or on successful completion
  10.  **           false on error
  11.  */
  12. if ( ! function_exists('maybe_create_table') ) :
  13. function maybe_create_table($table_name, $create_ddl) {
  14.     global $wpdb;
  15.     foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
  16.         if ($table == $table_name) {
  17.             return true;
  18.         }
  19.     }
  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) {
  25.             return true;
  26.         }
  27.     }
  28.     return false;
  29. }
  30. endif;
  31.  
  32. /**
  33.  ** maybe_add_column()
  34.  ** Add column to db table if it doesn't exist.
  35.  ** Returns:  true if already exists or on successful completion
  36.  **           false on error
  37.  */
  38. if ( ! function_exists('maybe_add_column') ) :
  39. function maybe_add_column($table_name, $column_name, $create_ddl) {
  40.     global $wpdb, $debug;
  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) {
  44.                 return true;
  45.             }
  46.     }
  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) {
  52.             return true;
  53.         }
  54.     }
  55.     return false;
  56. }
  57. endif;
  58.  
  59. /**
  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
  63.  **           false on error
  64.  */
  65. function maybe_drop_column($table_name, $column_name, $drop_ddl) {
  66.     global $wpdb;
  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) {
  74.                     return false;
  75.                 }
  76.             }
  77.         }
  78.     }
  79.     // else didn't find it
  80.     return true;
  81. }
  82.  
  83.  
  84. /**
  85.  ** check_column()
  86.  ** Check column matches passed in criteria.
  87.  ** Pass in null to skip checking that criteria
  88.  ** Returns:  true if it matches
  89.  **           false otherwise
  90.  ** (case sensitive) Column names returned from DESC table are:
  91.  **      Field
  92.  **      Type
  93.  **      Null
  94.  **      Key
  95.  **      Default
  96.  **      Extra
  97.  */
  98. function check_column($table_name, $col_name, $col_type, $is_null = null, $key = null, $default = null, $extra = null) {
  99.     global $wpdb, $debug;
  100.     $diffs = 0;
  101.     $results = $wpdb->get_results("DESC $table_name");
  102.  
  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)) {
  109.                     ++$diffs;
  110.                 }
  111.                 if (($is_null != null) && ($row->Null != $is_null)) {
  112.                     ++$diffs;
  113.                 }
  114.                 if (($key != null) && ($row->Key  != $key)) {
  115.                     ++$diffs;
  116.                 }
  117.                 if (($default != null) && ($row->Default != $default)) {
  118.                     ++$diffs;
  119.                 }
  120.                 if (($extra != null) && ($row->Extra != $extra)) {
  121.                     ++$diffs;
  122.                 }
  123.                 if ($diffs > 0) {
  124.                     if ($debug) echo ("diffs = $diffs returning false\n");
  125.                     return false;
  126.                 }
  127.                 return true;
  128.             } // end if found our column
  129.     }
  130.     return false;
  131. }
  132.  
  133. /*
  134. echo "<p>testing</p>";
  135. echo "<pre>";
  136.  
  137. //check_column('wp_links', 'link_description', 'mediumtext');
  138. //if (check_column($wpdb->comments, 'comment_author', 'tinytext'))
  139. //    echo "ok\n";
  140. $error_count = 0;
  141. $tablename = $wpdb->links;
  142. // check the column
  143. if (!check_column($wpdb->links, 'link_description', 'varchar(255)'))
  144. {
  145.     $ddl = "ALTER TABLE $wpdb->links MODIFY COLUMN link_description varchar(255) NOT NULL DEFAULT '' ";
  146.     $q = $wpdb->query($ddl);
  147. }
  148. if (check_column($wpdb->links, 'link_description', 'varchar(255)')) {
  149.     $res .= $tablename . ' - ok <br />';
  150. } else {
  151.     $res .= 'There was a problem with ' . $tablename . '<br />';
  152.     ++$error_count;
  153. }
  154. echo "</pre>";
  155. */
  156. ?>
  157.