PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/install-helper.php

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