/bp-forums/bbpress/bb-admin/includes/defaults.bb-schema.php

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