/wp-content/plugins/buddypress/bp-forums/bbpress/bb-admin/includes/defaults.bb-schema.php

https://github.com/jazbek/nycga2 · PHP · 235 lines · 188 code · 23 blank · 24 comment · 26 complexity · 04900a9ba77a5f7177c5027c096358e8 MD5 · raw file

  1. <?php
  2. // Globalise as this file is included within the functions bb_install() and bb_upgrade_all()
  3. global $bb_queries, $bbdb, $bb_schema_ignore;
  4. // Die if no database class is loaded
  5. if ( !isset($bbdb) || ( !is_a( $bbdb, 'BPDB' ) && !is_a( $bbdb, 'db' ) ) )
  6. die( __('Database class not loaded.') );
  7. // Initialise the query array
  8. $bb_queries = array();
  9. // forums
  10. $bb_queries['forums'] = "CREATE TABLE IF NOT EXISTS `$bbdb->forums` (
  11. `forum_id` int(10) NOT NULL auto_increment,
  12. `forum_name` varchar(150) NOT NULL default '',
  13. `forum_slug` varchar(255) NOT NULL default '',
  14. `forum_desc` text NOT NULL,
  15. `forum_parent` int(10) NOT NULL default 0,
  16. `forum_order` int(10) NOT NULL default 0,
  17. `topics` bigint(20) NOT NULL default 0,
  18. `posts` bigint(20) NOT NULL default 0,
  19. PRIMARY KEY (`forum_id`),
  20. KEY `forum_slug` (`forum_slug`)
  21. );";
  22. // meta
  23. $bb_queries['meta'] = "CREATE TABLE IF NOT EXISTS `$bbdb->meta` (
  24. `meta_id` bigint(20) NOT NULL auto_increment,
  25. `object_type` varchar(16) NOT NULL default 'bb_option',
  26. `object_id` bigint(20) NOT NULL default 0,
  27. `meta_key` varchar(255) default NULL,
  28. `meta_value` longtext default NULL,
  29. PRIMARY KEY (`meta_id`),
  30. KEY `object_type__meta_key` (`object_type`, `meta_key`),
  31. KEY `object_type__object_id__meta_key` (`object_type`, `object_id`, `meta_key`)
  32. );";
  33. // posts
  34. $bb_queries['posts'] = "CREATE TABLE IF NOT EXISTS `$bbdb->posts` (
  35. `post_id` bigint(20) NOT NULL auto_increment,
  36. `forum_id` int(10) NOT NULL default 1,
  37. `topic_id` bigint(20) NOT NULL default 1,
  38. `poster_id` int(10) NOT NULL default 0,
  39. `post_text` text NOT NULL,
  40. `post_time` datetime NOT NULL default '0000-00-00 00:00:00',
  41. `poster_ip` varchar(15) NOT NULL default '',
  42. `post_status` tinyint(1) NOT NULL default 0,
  43. `post_position` bigint(20) NOT NULL default 0,
  44. PRIMARY KEY (`post_id`),
  45. KEY `topic_time` (`topic_id`, `post_time`),
  46. KEY `poster_time` (`poster_id`, `post_time`),
  47. KEY `post_time` (`post_time`),
  48. FULLTEXT KEY `post_text` (`post_text`)
  49. ) ENGINE = MYISAM;";
  50. // terms
  51. $bb_queries['terms'] = "CREATE TABLE IF NOT EXISTS `$bbdb->terms` (
  52. `term_id` bigint(20) NOT NULL auto_increment,
  53. `name` varchar(55) NOT NULL default '',
  54. `slug` varchar(200) NOT NULL default '',
  55. `term_group` bigint(10) NOT NULL default 0,
  56. PRIMARY KEY (`term_id`),
  57. UNIQUE KEY `slug` (`slug`),
  58. KEY name (name)
  59. );";
  60. // term_relationships
  61. $bb_queries['term_relationships'] = "CREATE TABLE IF NOT EXISTS `$bbdb->term_relationships` (
  62. `object_id` bigint(20) NOT NULL default 0,
  63. `term_taxonomy_id` bigint(20) NOT NULL default 0,
  64. `user_id` bigint(20) NOT NULL default 0,
  65. `term_order` int(11) NOT NULL default 0,
  66. PRIMARY KEY (`object_id`, `term_taxonomy_id`),
  67. KEY `term_taxonomy_id` (`term_taxonomy_id`)
  68. );";
  69. // term_taxonomy
  70. $bb_queries['term_taxonomy'] = "CREATE TABLE IF NOT EXISTS `$bbdb->term_taxonomy` (
  71. `term_taxonomy_id` bigint(20) NOT NULL auto_increment,
  72. `term_id` bigint(20) NOT NULL default 0,
  73. `taxonomy` varchar(32) NOT NULL default '',
  74. `description` longtext NOT NULL,
  75. `parent` bigint(20) NOT NULL default 0,
  76. `count` bigint(20) NOT NULL default 0,
  77. PRIMARY KEY (`term_taxonomy_id`),
  78. UNIQUE KEY `term_id_taxonomy` (`term_id`, `taxonomy`),
  79. KEY `taxonomy` (`taxonomy`)
  80. );";
  81. // topics
  82. $bb_queries['topics'] = "CREATE TABLE IF NOT EXISTS `$bbdb->topics` (
  83. `topic_id` bigint(20) NOT NULL auto_increment,
  84. `topic_title` varchar(100) NOT NULL default '',
  85. `topic_slug` varchar(255) NOT NULL default '',
  86. `topic_poster` bigint(20) NOT NULL default 0,
  87. `topic_poster_name` varchar(40) NOT NULL default 'Anonymous',
  88. `topic_last_poster` bigint(20) NOT NULL default 0,
  89. `topic_last_poster_name` varchar(40) NOT NULL default '',
  90. `topic_start_time` datetime NOT NULL default '0000-00-00 00:00:00',
  91. `topic_time` datetime NOT NULL default '0000-00-00 00:00:00',
  92. `forum_id` int(10) NOT NULL default 1,
  93. `topic_status` tinyint(1) NOT NULL default 0,
  94. `topic_open` tinyint(1) NOT NULL default 1,
  95. `topic_last_post_id` bigint(20) NOT NULL default 1,
  96. `topic_sticky` tinyint(1) NOT NULL default 0,
  97. `topic_posts` bigint(20) NOT NULL default 0,
  98. `tag_count` bigint(20) NOT NULL default 0,
  99. PRIMARY KEY (`topic_id`),
  100. KEY `topic_slug` (`topic_slug`),
  101. KEY `forum_time` (`forum_id`, `topic_time`),
  102. KEY `user_start_time` (`topic_poster`, `topic_start_time`),
  103. KEY `stickies` (`topic_status`, `topic_sticky`, `topic_time`)
  104. );";
  105. if ( bb_get_option( 'wp_table_prefix' ) || ( defined( 'BB_SCHEMA_IGNORE_WP_USERS_TABLES' ) && BB_SCHEMA_IGNORE_WP_USERS_TABLES ) ) {
  106. // Don't add user tables
  107. } else {
  108. // users - 'user_login', 'user_nicename' and 'user_registered' indices are inconsistent with WordPress
  109. $bb_queries['users'] = "CREATE TABLE IF NOT EXISTS `$bbdb->users` (
  110. `ID` bigint(20) unsigned NOT NULL auto_increment,
  111. `user_login` varchar(60) NOT NULL default '',
  112. `user_pass` varchar(64) NOT NULL default '',
  113. `user_nicename` varchar(50) NOT NULL default '',
  114. `user_email` varchar(100) NOT NULL default '',
  115. `user_url` varchar(100) NOT NULL default '',
  116. `user_registered` datetime NOT NULL default '0000-00-00 00:00:00',
  117. `user_status` int(11) NOT NULL default 0,
  118. `display_name` varchar(250) NOT NULL default '',
  119. PRIMARY KEY (`ID`),
  120. UNIQUE KEY `user_login` (`user_login`),
  121. UNIQUE KEY `user_nicename` (`user_nicename`),
  122. KEY `user_registered` (`user_registered`)
  123. );";
  124. // usermeta
  125. $bb_queries['usermeta'] = "CREATE TABLE IF NOT EXISTS `$bbdb->usermeta` (
  126. `umeta_id` bigint(20) NOT NULL auto_increment,
  127. `user_id` bigint(20) NOT NULL default 0,
  128. `meta_key` varchar(255),
  129. `meta_value` longtext,
  130. PRIMARY KEY (`umeta_id`),
  131. KEY `user_id` (`user_id`),
  132. KEY `meta_key` (`meta_key`)
  133. );";
  134. }
  135. $bb_queries = apply_filters( 'bb_schema_pre_charset', $bb_queries );
  136. // Set the charset and collation on each table
  137. foreach ($bb_queries as $_table_name => $_sql) {
  138. // Skip SQL that isn't creating a table
  139. if (!preg_match('@^\s*CREATE\s+TABLE\s+@im', $_sql)) {
  140. continue;
  141. }
  142. // Skip if the table's database doesn't support collation
  143. if (!$bbdb->has_cap('collation', $bbdb->$_table_name)) {
  144. continue;
  145. }
  146. // Find out if the table has a custom database set
  147. if (
  148. isset($bbdb->db_tables) &&
  149. is_array($bbdb->db_tables) &&
  150. isset($bbdb->db_tables[$bbdb->$_table_name])
  151. ) {
  152. // Set the database for this table
  153. $_database = $bbdb->db_tables[$bbdb->$_table_name];
  154. } else {
  155. // Set the default global database
  156. $_database = 'dbh_global';
  157. }
  158. // Make sure the database exists
  159. if (
  160. isset($bbdb->db_servers) &&
  161. is_array($bbdb->db_servers) &&
  162. isset($bbdb->db_servers[$_database]) &&
  163. is_array($bbdb->db_servers[$_database])
  164. ) {
  165. $_charset_collate = '';
  166. if (isset($bbdb->db_servers[$_database]['charset']) && !empty($bbdb->db_servers[$_database]['charset'])) {
  167. // Add a charset if set
  168. $_charset_collate .= ' DEFAULT CHARACTER SET \'' . $bbdb->db_servers[$_database]['charset'] . '\'';
  169. }
  170. if (isset($bbdb->db_servers[$_database]['collate']) && !empty($bbdb->db_servers[$_database]['collate'])) {
  171. // Add a collation if set
  172. $_charset_collate .= ' COLLATE \'' . $bbdb->db_servers[$_database]['collate'] . '\'';
  173. }
  174. if ($_charset_collate) {
  175. // Modify the SQL
  176. $bb_queries[$_table_name] = str_replace(';', $_charset_collate . ';', $_sql);
  177. }
  178. }
  179. unset($_database, $_charset_collate);
  180. }
  181. unset($_table_name, $_sql);
  182. $bb_queries = apply_filters( 'bb_schema', $bb_queries );
  183. // These elements in the schema may need to be ignored when doing comparisons due to inconsistencies with WordPress
  184. if ( bb_get_option('wp_table_prefix') || ( defined( 'BB_SCHEMA_IGNORE_WP_USERS_KEYS' ) && BB_SCHEMA_IGNORE_WP_USERS_KEYS ) ) {
  185. $bb_schema_ignore = array(
  186. 'tables' => array(),
  187. 'columns' => array(),
  188. 'indices' => array(
  189. $bbdb->users => array(
  190. 'user_login',
  191. 'user_nicename',
  192. 'user_registered'
  193. )
  194. )
  195. );
  196. } else {
  197. $bb_schema_ignore = false;
  198. }
  199. if ( bb_get_option('wp_table_prefix') || ( defined( 'BB_SCHEMA_IGNORE_WP_USERS_TABLES' ) && BB_SCHEMA_IGNORE_WP_USERS_TABLES ) ) {
  200. if ( $bb_schema_ignore ) {
  201. $bb_schema_ignore['tables'] = array( $bbdb->users, $bbdb->usermeta );
  202. } else {
  203. $bb_schema_ignore = array(
  204. 'tables' => array( $bbdb->users, $bbdb->usermeta ),
  205. 'columns' => array(),
  206. 'indices' => array()
  207. );
  208. }
  209. }
  210. $bb_schema_ignore = apply_filters( 'bb_schema_ignore', $bb_schema_ignore );
  211. do_action( 'bb_schema_defined' );
  212. ?>