PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/install-helper.php

http://cartonbank.googlecode.com/
PHP | 225 lines | 86 code | 11 blank | 128 comment | 42 complexity | 15027855df958026aceb4daf75f25282 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, LGPL-2.1, AGPL-1.0, LGPL-3.0
  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. * @global bool $wp_only_load_config
  42. * @name $wp_only_load_config
  43. * @var bool
  44. * @since unknown
  45. */
  46. $wp_only_load_config = true;
  47. /** Load WordPress Bootstrap */
  48. require_once(dirname(dirname(__FILE__)).'/wp-load.php');
  49. /**
  50. * Turn debugging on or off.
  51. * @global bool|int $debug
  52. * @name $debug
  53. * @var bool|int
  54. * @since unknown
  55. */
  56. $debug = 0;
  57. if ( ! function_exists('maybe_create_table') ) :
  58. /**
  59. * Create database table, if it doesn't already exist.
  60. *
  61. * @since unknown
  62. * @package WordPress
  63. * @subpackage Plugin
  64. * @uses $wpdb
  65. *
  66. * @param string $table_name Database table name.
  67. * @param string $create_ddl Create database table SQL.
  68. * @return bool False on error, true if already exists or success.
  69. */
  70. function maybe_create_table($table_name, $create_ddl) {
  71. global $wpdb;
  72. foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
  73. if ($table == $table_name) {
  74. return true;
  75. }
  76. }
  77. //didn't find it try to create it.
  78. $wpdb->query($create_ddl);
  79. // we cannot directly tell that whether this succeeded!
  80. foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
  81. if ($table == $table_name) {
  82. return true;
  83. }
  84. }
  85. return false;
  86. }
  87. endif;
  88. if ( ! function_exists('maybe_add_column') ) :
  89. /**
  90. * Add column to database table, if column doesn't already exist in table.
  91. *
  92. * @since unknown
  93. * @package WordPress
  94. * @subpackage Plugin
  95. * @uses $wpdb
  96. * @uses $debug
  97. *
  98. * @param string $table_name Database table name
  99. * @param string $column_name Table column name
  100. * @param string $create_ddl SQL to add column to table.
  101. * @return bool False on failure. True, if already exists or was successful.
  102. */
  103. function maybe_add_column($table_name, $column_name, $create_ddl) {
  104. global $wpdb, $debug;
  105. foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
  106. if ($debug) echo("checking $column == $column_name<br />");
  107. if ($column == $column_name) {
  108. return true;
  109. }
  110. }
  111. //didn't find it try to create it.
  112. $wpdb->query($create_ddl);
  113. // we cannot directly tell that whether this succeeded!
  114. foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
  115. if ($column == $column_name) {
  116. return true;
  117. }
  118. }
  119. return false;
  120. }
  121. endif;
  122. /**
  123. * Drop column from database table, if it exists.
  124. *
  125. * @since unknown
  126. * @package WordPress
  127. * @subpackage Plugin
  128. * @uses $wpdb
  129. *
  130. * @param string $table_name Table name
  131. * @param string $column_name Column name
  132. * @param string $drop_ddl SQL statement to drop column.
  133. * @return bool False on failure, true on success or doesn't exist.
  134. */
  135. function maybe_drop_column($table_name, $column_name, $drop_ddl) {
  136. global $wpdb;
  137. foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
  138. if ($column == $column_name) {
  139. //found it try to drop it.
  140. $wpdb->query($drop_ddl);
  141. // we cannot directly tell that whether this succeeded!
  142. foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
  143. if ($column == $column_name) {
  144. return false;
  145. }
  146. }
  147. }
  148. }
  149. // else didn't find it
  150. return true;
  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 unknown
  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. foreach ($results as $row ) {
  186. if ($debug > 1) print_r($row);
  187. if ($row->Field == $col_name) {
  188. // got our column, check the params
  189. if ($debug) echo ("checking $row->Type against $col_type\n");
  190. if (($col_type != null) && ($row->Type != $col_type)) {
  191. ++$diffs;
  192. }
  193. if (($is_null != null) && ($row->Null != $is_null)) {
  194. ++$diffs;
  195. }
  196. if (($key != null) && ($row->Key != $key)) {
  197. ++$diffs;
  198. }
  199. if (($default != null) && ($row->Default != $default)) {
  200. ++$diffs;
  201. }
  202. if (($extra != null) && ($row->Extra != $extra)) {
  203. ++$diffs;
  204. }
  205. if ($diffs > 0) {
  206. if ($debug) echo ("diffs = $diffs returning false\n");
  207. return false;
  208. }
  209. return true;
  210. } // end if found our column
  211. }
  212. return false;
  213. }
  214. ?>