[ XREF Home ] [ Index ]

PHP Cross Reference of WordPress Trunk

Provided by Yoast

title

Body

[close]

/wp-admin/ -> install-helper.php (source)

   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   * You can turn debugging on, by setting $debug to 1 after you include this
  14   * file.
  15   *
  16   * <code>
  17   * check_column('wp_links', 'link_description', 'mediumtext');
  18   * if (check_column($wpdb->comments, 'comment_author', 'tinytext'))
  19   *     echo "ok\n";
  20   *
  21   * $error_count = 0;
  22   * $tablename = $wpdb->links;
  23   * // check the column
  24   * if (!check_column($wpdb->links, 'link_description', 'varchar(255)')) {
  25   *     $ddl = "ALTER TABLE $wpdb->links MODIFY COLUMN link_description varchar(255) NOT NULL DEFAULT '' ";
  26   *     $q = $wpdb->query($ddl);
  27   * }
  28   *
  29   * if (check_column($wpdb->links, 'link_description', 'varchar(255)')) {
  30   *     $res .= $tablename . ' - ok <br />';
  31   * } else {
  32   *     $res .= 'There was a problem with ' . $tablename . '<br />';
  33   *     ++$error_count;
  34   * }
  35   * </code>
  36   *
  37   * @package WordPress
  38   * @subpackage Plugin
  39   */
  40  
  41  /** Load WordPress Bootstrap */
  42  require_once(dirname(dirname(__FILE__)).'/wp-load.php');
  43  
  44  /**
  45   * Turn debugging on or off.
  46   * @global bool|int $debug
  47   * @name $debug
  48   * @var bool|int
  49   * @since 1.0.0
  50   */
  51  $debug = 0;
  52  
  53  if ( ! function_exists('maybe_create_table') ) :
  54  /**
  55   * Create database table, if it doesn't already exist.
  56   *
  57   * @since 1.0.0
  58   * @package WordPress
  59   * @subpackage Plugin
  60   * @uses $wpdb
  61   *
  62   * @param string $table_name Database table name.
  63   * @param string $create_ddl Create database table SQL.
  64   * @return bool False on error, true if already exists or success.
  65   */
  66  function maybe_create_table($table_name, $create_ddl) {
  67      global $wpdb;
  68      foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
  69          if ($table == $table_name) {
  70              return true;
  71          }
  72      }
  73      //didn't find it try to create it.
  74      $wpdb->query($create_ddl);
  75      // we cannot directly tell that whether this succeeded!
  76      foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
  77          if ($table == $table_name) {
  78              return true;
  79          }
  80      }
  81      return false;
  82  }
  83  endif;
  84  
  85  if ( ! function_exists('maybe_add_column') ) :
  86  /**
  87   * Add column to database table, if column doesn't already exist in table.
  88   *
  89   * @since 1.0.0
  90   * @package WordPress
  91   * @subpackage Plugin
  92   * @uses $wpdb
  93   * @uses $debug
  94   *
  95   * @param string $table_name Database table name
  96   * @param string $column_name Table column name
  97   * @param string $create_ddl SQL to add column to table.
  98   * @return bool False on failure. True, if already exists or was successful.
  99   */
 100  function maybe_add_column($table_name, $column_name, $create_ddl) {
 101      global $wpdb, $debug;
 102      foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
 103          if ($debug) echo("checking $column == $column_name<br />");
 104  
 105          if ($column == $column_name) {
 106              return true;
 107          }
 108      }
 109      //didn't find it try to create it.
 110      $wpdb->query($create_ddl);
 111      // we cannot directly tell that whether this succeeded!
 112      foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
 113          if ($column == $column_name) {
 114              return true;
 115          }
 116      }
 117      return false;
 118  }
 119  endif;
 120  
 121  /**
 122   * Drop column from database table, if it exists.
 123   *
 124   * @since 1.0.0
 125   * @package WordPress
 126   * @subpackage Plugin
 127   * @uses $wpdb
 128   *
 129   * @param string $table_name Table name
 130   * @param string $column_name Column name
 131   * @param string $drop_ddl SQL statement to drop column.
 132   * @return bool False on failure, true on success or doesn't exist.
 133   */
 134  function maybe_drop_column($table_name, $column_name, $drop_ddl) {
 135      global $wpdb;
 136      foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
 137          if ($column == $column_name) {
 138              //found it try to drop it.
 139              $wpdb->query($drop_ddl);
 140              // we cannot directly tell that whether this succeeded!
 141              foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
 142                  if ($column == $column_name) {
 143                      return false;
 144                  }
 145              }
 146          }
 147      }
 148      // else didn't find it
 149      return true;
 150  }
 151  
 152  /**
 153   * Check column matches criteria.
 154   *
 155   * Uses the SQL DESC for retrieving the table info for the column. It will help
 156   * understand the parameters, if you do more research on what column information
 157   * is returned by the SQL statement. Pass in null to skip checking that
 158   * criteria.
 159   *
 160   * Column names returned from DESC table are case sensitive and are listed:
 161   *      Field
 162   *      Type
 163   *      Null
 164   *      Key
 165   *      Default
 166   *      Extra
 167   *
 168   * @since 1.0.0
 169   * @package WordPress
 170   * @subpackage Plugin
 171   *
 172   * @param string $table_name Table name
 173   * @param string $col_name Column name
 174   * @param string $col_type Column type
 175   * @param bool $is_null Optional. Check is null.
 176   * @param mixed $key Optional. Key info.
 177   * @param mixed $default Optional. Default value.
 178   * @param mixed $extra Optional. Extra value.
 179   * @return bool True, if matches. False, if not matching.
 180   */
 181  function check_column($table_name, $col_name, $col_type, $is_null = null, $key = null, $default = null, $extra = null) {
 182      global $wpdb, $debug;
 183      $diffs = 0;
 184      $results = $wpdb->get_results("DESC $table_name");
 185  
 186      foreach ($results as $row ) {
 187          if ($debug > 1) print_r($row);
 188  
 189          if ($row->Field == $col_name) {
 190              // got our column, check the params
 191              if ($debug) echo ("checking $row->Type against $col_type\n");
 192              if (($col_type != null) && ($row->Type != $col_type)) {
 193                  ++$diffs;
 194              }
 195              if (($is_null != null) && ($row->Null != $is_null)) {
 196                  ++$diffs;
 197              }
 198              if (($key != null) && ($row->Key  != $key)) {
 199                  ++$diffs;
 200              }
 201              if (($default != null) && ($row->Default != $default)) {
 202                  ++$diffs;
 203              }
 204              if (($extra != null) && ($row->Extra != $extra)) {
 205                  ++$diffs;
 206              }
 207              if ($diffs > 0) {
 208                  if ($debug) echo ("diffs = $diffs returning false\n");
 209                  return false;
 210              }
 211              return true;
 212          } // end if found our column
 213      }
 214      return false;
 215  }
 216  
 217  ?>


Generated: Wed Jun 1 08:30:02 2011 Cross-referenced by PHPXref 0.7
Provided by Yoast and awesome WordPress Hosting