PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/install-helper.php

https://github.com/alx/barceloneta
PHP | 156 lines | 86 code | 7 blank | 63 comment | 42 complexity | 42b1ee6ab423772cc11511d30b8c0ebc MD5 | raw file
  1. <?php
  2. $wp_only_load_config = true;
  3. require_once(dirname(dirname(__FILE__)).'/wp-load.php');
  4. $debug = 0;
  5. /**
  6. ** maybe_create_table()
  7. ** Create db table if it doesn't exist.
  8. ** Returns: true if already exists or on successful completion
  9. ** false on error
  10. */
  11. if ( ! function_exists('maybe_create_table') ) :
  12. function maybe_create_table($table_name, $create_ddl) {
  13. global $wpdb;
  14. foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
  15. if ($table == $table_name) {
  16. return true;
  17. }
  18. }
  19. //didn't find it try to create it.
  20. $wpdb->query($create_ddl);
  21. // we cannot directly tell that whether this succeeded!
  22. foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
  23. if ($table == $table_name) {
  24. return true;
  25. }
  26. }
  27. return false;
  28. }
  29. endif;
  30. /**
  31. ** maybe_add_column()
  32. ** Add column to db table if it doesn't exist.
  33. ** Returns: true if already exists or on successful completion
  34. ** false on error
  35. */
  36. if ( ! function_exists('maybe_add_column') ) :
  37. function maybe_add_column($table_name, $column_name, $create_ddl) {
  38. global $wpdb, $debug;
  39. foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
  40. if ($debug) echo("checking $column == $column_name<br />");
  41. if ($column == $column_name) {
  42. return true;
  43. }
  44. }
  45. //didn't find it try to create it.
  46. $wpdb->query($create_ddl);
  47. // we cannot directly tell that whether this succeeded!
  48. foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
  49. if ($column == $column_name) {
  50. return true;
  51. }
  52. }
  53. return false;
  54. }
  55. endif;
  56. /**
  57. ** maybe_drop_column()
  58. ** Drop column from db table if it exists.
  59. ** Returns: true if it doesn't already exist or on successful drop
  60. ** false on error
  61. */
  62. function maybe_drop_column($table_name, $column_name, $drop_ddl) {
  63. global $wpdb;
  64. foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
  65. if ($column == $column_name) {
  66. //found it try to drop it.
  67. $wpdb->query($drop_ddl);
  68. // we cannot directly tell that whether this succeeded!
  69. foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
  70. if ($column == $column_name) {
  71. return false;
  72. }
  73. }
  74. }
  75. }
  76. // else didn't find it
  77. return true;
  78. }
  79. /**
  80. ** check_column()
  81. ** Check column matches passed in criteria.
  82. ** Pass in null to skip checking that criteria
  83. ** Returns: true if it matches
  84. ** false otherwise
  85. ** (case sensitive) Column names returned from DESC table are:
  86. ** Field
  87. ** Type
  88. ** Null
  89. ** Key
  90. ** Default
  91. ** Extra
  92. */
  93. function check_column($table_name, $col_name, $col_type, $is_null = null, $key = null, $default = null, $extra = null) {
  94. global $wpdb, $debug;
  95. $diffs = 0;
  96. $results = $wpdb->get_results("DESC $table_name");
  97. foreach ($results as $row ) {
  98. if ($debug > 1) print_r($row);
  99. if ($row->Field == $col_name) {
  100. // got our column, check the params
  101. if ($debug) echo ("checking $row->Type against $col_type\n");
  102. if (($col_type != null) && ($row->Type != $col_type)) {
  103. ++$diffs;
  104. }
  105. if (($is_null != null) && ($row->Null != $is_null)) {
  106. ++$diffs;
  107. }
  108. if (($key != null) && ($row->Key != $key)) {
  109. ++$diffs;
  110. }
  111. if (($default != null) && ($row->Default != $default)) {
  112. ++$diffs;
  113. }
  114. if (($extra != null) && ($row->Extra != $extra)) {
  115. ++$diffs;
  116. }
  117. if ($diffs > 0) {
  118. if ($debug) echo ("diffs = $diffs returning false\n");
  119. return false;
  120. }
  121. return true;
  122. } // end if found our column
  123. }
  124. return false;
  125. }
  126. /*
  127. echo "<p>testing</p>";
  128. echo "<pre>";
  129. //check_column('wp_links', 'link_description', 'mediumtext');
  130. //if (check_column($wpdb->comments, 'comment_author', 'tinytext'))
  131. // echo "ok\n";
  132. $error_count = 0;
  133. $tablename = $wpdb->links;
  134. // check the column
  135. if (!check_column($wpdb->links, 'link_description', 'varchar(255)'))
  136. {
  137. $ddl = "ALTER TABLE $wpdb->links MODIFY COLUMN link_description varchar(255) NOT NULL DEFAULT '' ";
  138. $q = $wpdb->query($ddl);
  139. }
  140. if (check_column($wpdb->links, 'link_description', 'varchar(255)')) {
  141. $res .= $tablename . ' - ok <br />';
  142. } else {
  143. $res .= 'There was a problem with ' . $tablename . '<br />';
  144. ++$error_count;
  145. }
  146. echo "</pre>";
  147. */
  148. ?>