forum.install 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <?php
  2. /**
  3. * @file
  4. * Install, update, and uninstall functions for the Forum module.
  5. */
  6. /**
  7. * Implements hook_install().
  8. */
  9. function forum_install() {
  10. // Set the weight of the forum.module to 1 so it is loaded after the taxonomy.module.
  11. db_update('system')
  12. ->fields(array('weight' => 1))
  13. ->condition('name', 'forum')
  14. ->execute();
  15. // Forum topics are published by default, but do not have any other default
  16. // options set (for example, they are not promoted to the front page).
  17. variable_set('node_options_forum', array('status'));
  18. }
  19. /**
  20. * Implements hook_enable().
  21. */
  22. function forum_enable() {
  23. // If we enable forum at the same time as taxonomy we need to call
  24. // field_associate_fields() as otherwise the field won't be enabled until
  25. // hook modules_enabled is called which takes place after hook_enable events.
  26. field_associate_fields('taxonomy');
  27. // Create the forum vocabulary if it does not exist.
  28. $vocabulary = taxonomy_vocabulary_load(variable_get('forum_nav_vocabulary', 0));
  29. if (!$vocabulary) {
  30. $edit = array(
  31. 'name' => t('Forums'),
  32. 'machine_name' => 'forums',
  33. 'description' => t('Forum navigation vocabulary'),
  34. 'hierarchy' => 1,
  35. 'module' => 'forum',
  36. 'weight' => -10,
  37. );
  38. $vocabulary = (object) $edit;
  39. taxonomy_vocabulary_save($vocabulary);
  40. variable_set('forum_nav_vocabulary', $vocabulary->vid);
  41. }
  42. // Create the 'taxonomy_forums' field if it doesn't already exist.
  43. if (!field_info_field('taxonomy_forums')) {
  44. $field = array(
  45. 'field_name' => 'taxonomy_forums',
  46. 'type' => 'taxonomy_term_reference',
  47. 'settings' => array(
  48. 'allowed_values' => array(
  49. array(
  50. 'vocabulary' => $vocabulary->machine_name,
  51. 'parent' => 0,
  52. ),
  53. ),
  54. ),
  55. );
  56. field_create_field($field);
  57. // Create a default forum so forum posts can be created.
  58. $edit = array(
  59. 'name' => t('General discussion'),
  60. 'description' => '',
  61. 'parent' => array(0),
  62. 'vid' => $vocabulary->vid,
  63. );
  64. $term = (object) $edit;
  65. taxonomy_term_save($term);
  66. // Create the instance on the bundle.
  67. $instance = array(
  68. 'field_name' => 'taxonomy_forums',
  69. 'entity_type' => 'node',
  70. 'label' => $vocabulary->name,
  71. 'bundle' => 'forum',
  72. 'required' => TRUE,
  73. 'widget' => array(
  74. 'type' => 'options_select',
  75. ),
  76. 'display' => array(
  77. 'default' => array(
  78. 'type' => 'taxonomy_term_reference_link',
  79. 'weight' => 10,
  80. ),
  81. 'teaser' => array(
  82. 'type' => 'taxonomy_term_reference_link',
  83. 'weight' => 10,
  84. ),
  85. ),
  86. );
  87. field_create_instance($instance);
  88. }
  89. // Ensure the forum node type is available.
  90. node_types_rebuild();
  91. $types = node_type_get_types();
  92. node_add_body_field($types['forum']);
  93. }
  94. /**
  95. * Implements hook_uninstall().
  96. */
  97. function forum_uninstall() {
  98. // Load the dependent Taxonomy module, in case it has been disabled.
  99. drupal_load('module', 'taxonomy');
  100. variable_del('forum_containers');
  101. variable_del('forum_hot_topic');
  102. variable_del('forum_per_page');
  103. variable_del('forum_order');
  104. variable_del('forum_block_num_active');
  105. variable_del('forum_block_num_new');
  106. variable_del('node_options_forum');
  107. field_delete_field('taxonomy_forums');
  108. // Purge field data now to allow taxonomy module to be uninstalled
  109. // if this is the only field remaining.
  110. field_purge_batch(10);
  111. }
  112. /**
  113. * Implements hook_schema().
  114. */
  115. function forum_schema() {
  116. $schema['forum'] = array(
  117. 'description' => 'Stores the relationship of nodes to forum terms.',
  118. 'fields' => array(
  119. 'nid' => array(
  120. 'type' => 'int',
  121. 'unsigned' => TRUE,
  122. 'not null' => TRUE,
  123. 'default' => 0,
  124. 'description' => 'The {node}.nid of the node.',
  125. ),
  126. 'vid' => array(
  127. 'type' => 'int',
  128. 'unsigned' => TRUE,
  129. 'not null' => TRUE,
  130. 'default' => 0,
  131. 'description' => 'Primary Key: The {node}.vid of the node.',
  132. ),
  133. 'tid' => array(
  134. 'type' => 'int',
  135. 'unsigned' => TRUE,
  136. 'not null' => TRUE,
  137. 'default' => 0,
  138. 'description' => 'The {taxonomy_term_data}.tid of the forum term assigned to the node.',
  139. ),
  140. ),
  141. 'indexes' => array(
  142. 'forum_topic' => array('nid', 'tid'),
  143. 'tid' => array('tid'),
  144. ),
  145. 'primary key' => array('vid'),
  146. 'foreign keys' => array(
  147. 'forum_node' => array(
  148. 'table' => 'node',
  149. 'columns' => array(
  150. 'nid' => 'nid',
  151. 'vid' => 'vid',
  152. ),
  153. ),
  154. ),
  155. );
  156. $schema['forum_index'] = array(
  157. 'description' => 'Maintains denormalized information about node/term relationships.',
  158. 'fields' => array(
  159. 'nid' => array(
  160. 'description' => 'The {node}.nid this record tracks.',
  161. 'type' => 'int',
  162. 'unsigned' => TRUE,
  163. 'not null' => TRUE,
  164. 'default' => 0,
  165. ),
  166. 'title' => array(
  167. 'description' => 'The title of this node, always treated as non-markup plain text.',
  168. 'type' => 'varchar',
  169. 'length' => 255,
  170. 'not null' => TRUE,
  171. 'default' => '',
  172. ),
  173. 'tid' => array(
  174. 'description' => 'The term ID.',
  175. 'type' => 'int',
  176. 'unsigned' => TRUE,
  177. 'not null' => TRUE,
  178. 'default' => 0,
  179. ),
  180. 'sticky' => array(
  181. 'description' => 'Boolean indicating whether the node is sticky.',
  182. 'type' => 'int',
  183. 'not null' => FALSE,
  184. 'default' => 0,
  185. 'size' => 'tiny',
  186. ),
  187. 'created' => array(
  188. 'description' => 'The Unix timestamp when the node was created.',
  189. 'type' => 'int',
  190. 'unsigned' => TRUE,
  191. 'not null' => TRUE,
  192. 'default'=> 0,
  193. ),
  194. 'last_comment_timestamp' => array(
  195. 'type' => 'int',
  196. 'not null' => TRUE,
  197. 'default' => 0,
  198. 'description' => 'The Unix timestamp of the last comment that was posted within this node, from {comment}.timestamp.',
  199. ),
  200. 'comment_count' => array(
  201. 'type' => 'int',
  202. 'unsigned' => TRUE,
  203. 'not null' => TRUE,
  204. 'default' => 0,
  205. 'description' => 'The total number of comments on this node.',
  206. ),
  207. ),
  208. 'indexes' => array(
  209. 'forum_topics' => array('nid', 'tid', 'sticky', 'last_comment_timestamp'),
  210. 'created' => array('created'),
  211. 'last_comment_timestamp' => array('last_comment_timestamp'),
  212. ),
  213. 'foreign keys' => array(
  214. 'tracked_node' => array(
  215. 'table' => 'node',
  216. 'columns' => array('nid' => 'nid'),
  217. ),
  218. 'term' => array(
  219. 'table' => 'taxonomy_term_data',
  220. 'columns' => array(
  221. 'tid' => 'tid',
  222. ),
  223. ),
  224. ),
  225. );
  226. return $schema;
  227. }
  228. /**
  229. * Implements hook_update_dependencies().
  230. */
  231. function forum_update_dependencies() {
  232. $dependencies['forum'][7003] = array(
  233. // Forum update 7003 uses field API update functions, so must run after
  234. // Field API has been enabled.
  235. 'system' => 7020,
  236. // Forum update 7003 relies on updated taxonomy module schema. Ensure it
  237. // runs after all taxonomy updates.
  238. 'taxonomy' => 7010,
  239. );
  240. return $dependencies;
  241. }
  242. /**
  243. * Add new index to forum table.
  244. */
  245. function forum_update_7000() {
  246. db_drop_index('forum', 'nid');
  247. db_add_index('forum', 'forum_topic', array('nid', 'tid'));
  248. }
  249. /**
  250. * Create new {forum_index} table.
  251. */
  252. function forum_update_7001() {
  253. $forum_index = array(
  254. 'description' => 'Maintains denormalized information about node/term relationships.',
  255. 'fields' => array(
  256. 'nid' => array(
  257. 'description' => 'The {node}.nid this record tracks.',
  258. 'type' => 'int',
  259. 'unsigned' => TRUE,
  260. 'not null' => TRUE,
  261. 'default' => 0,
  262. ),
  263. 'title' => array(
  264. 'description' => 'The title of this node, always treated as non-markup plain text.',
  265. 'type' => 'varchar',
  266. 'length' => 255,
  267. 'not null' => TRUE,
  268. 'default' => '',
  269. ),
  270. 'tid' => array(
  271. 'description' => 'The term ID.',
  272. 'type' => 'int',
  273. 'unsigned' => TRUE,
  274. 'not null' => TRUE,
  275. 'default' => 0,
  276. ),
  277. 'sticky' => array(
  278. 'description' => 'Boolean indicating whether the node is sticky.',
  279. 'type' => 'int',
  280. 'not null' => FALSE,
  281. 'default' => 0,
  282. 'size' => 'tiny',
  283. ),
  284. 'created' => array(
  285. 'description' => 'The Unix timestamp when the node was created.',
  286. 'type' => 'int',
  287. 'unsigned' => TRUE,
  288. 'not null' => TRUE,
  289. 'default'=> 0,
  290. ),
  291. 'last_comment_timestamp' => array(
  292. 'type' => 'int',
  293. 'not null' => TRUE,
  294. 'default' => 0,
  295. 'description' => 'The Unix timestamp of the last comment that was posted within this node, from {comment}.timestamp.',
  296. ),
  297. 'comment_count' => array(
  298. 'type' => 'int',
  299. 'unsigned' => TRUE,
  300. 'not null' => TRUE,
  301. 'default' => 0,
  302. 'description' => 'The total number of comments on this node.',
  303. ),
  304. ),
  305. 'indexes' => array(
  306. 'forum_topics' => array('tid', 'sticky', 'last_comment_timestamp'),
  307. ),
  308. 'foreign keys' => array(
  309. 'tracked_node' => array(
  310. 'table' => 'node',
  311. 'columns' => array('nid' => 'nid'),
  312. ),
  313. 'term' => array(
  314. 'table' => 'taxonomy_term_data',
  315. 'columns' => array(
  316. 'tid' => 'tid',
  317. ),
  318. ),
  319. ),
  320. );
  321. db_create_table('forum_index', $forum_index);
  322. $select = db_select('node', 'n');
  323. $forum_alias = $select->join('forum', 'f', 'n.vid = f.vid');
  324. $ncs_alias = $select->join('node_comment_statistics', 'ncs', 'n.nid = ncs.nid');
  325. $select
  326. ->fields('n', array('nid', 'title', 'sticky', 'created'))
  327. ->fields($forum_alias, array('tid'))
  328. ->fields($ncs_alias, array('last_comment_timestamp', 'comment_count'));
  329. db_insert('forum_index')
  330. ->fields(array('nid', 'title', 'sticky', 'created', 'tid', 'last_comment_timestamp', 'comment_count'))
  331. ->from($select)
  332. ->execute();
  333. }
  334. /**
  335. * @addtogroup updates-7.x-extra
  336. * @{
  337. */
  338. /**
  339. * Add new index to forum_index table.
  340. */
  341. function forum_update_7002() {
  342. db_drop_index('forum_index', 'forum_topics');
  343. db_add_index('forum_index', 'forum_topics', array('nid', 'tid', 'sticky', 'last_comment_timestamp'));
  344. }
  345. /**
  346. * Rename field to 'taxonomy_forums'.
  347. */
  348. function forum_update_7003() {
  349. $messages = array();
  350. $new_field_name = 'taxonomy_forums';
  351. // Test to see if the taxonomy_forums field exists.
  352. $fields = _update_7000_field_read_fields(array('field_name' => $new_field_name));
  353. if ($fields) {
  354. // Since the field exists, we're done.
  355. return;
  356. }
  357. // Calculate the old field name.
  358. $vid = variable_get('forum_nav_vocabulary', 0);
  359. $vocabulary_machine_name = db_select('taxonomy_vocabulary', 'tv')
  360. ->fields('tv', array('machine_name'))
  361. ->condition('vid', $vid)
  362. ->execute()
  363. ->fetchField();
  364. $old_field_name = 'taxonomy_' . $vocabulary_machine_name;
  365. // Read the old fields.
  366. $old_fields = _update_7000_field_read_fields(array('field_name' => $old_field_name));
  367. foreach ($old_fields as $old_field) {
  368. if ($old_field['storage']['type'] != 'field_sql_storage') {
  369. $messages[] = t('Cannot rename field %id (%old_field_name) to %new_field_name because it does not use the field_sql_storage storage type.', array(
  370. '%id' => $old_field['id'],
  371. '%old_field_name' => $old_field_name,
  372. '%new_field_name' => $new_field_name,
  373. ));
  374. continue;
  375. }
  376. // Update {field_config}.
  377. db_update('field_config')
  378. ->fields(array('field_name' => $new_field_name))
  379. ->condition('id', $old_field['id'])
  380. ->execute();
  381. // Update {field_config_instance}.
  382. db_update('field_config_instance')
  383. ->fields(array('field_name' => $new_field_name))
  384. ->condition('field_id', $old_field['id'])
  385. ->execute();
  386. // The tables that need updating in the form 'old_name' => 'new_name'.
  387. $tables = array(
  388. 'field_data_' . $old_field_name => 'field_data_' . $new_field_name,
  389. 'field_revision_' . $old_field_name => 'field_revision_' . $new_field_name,
  390. );
  391. foreach ($tables as $old_table => $new_table) {
  392. $old_column_name = $old_field_name . '_tid';
  393. $new_column_name = $new_field_name . '_tid';
  394. // Rename the column.
  395. db_drop_index($old_table, $old_column_name);
  396. db_change_field($old_table, $old_column_name, $new_column_name, array(
  397. 'type' => 'int',
  398. 'unsigned' => TRUE,
  399. 'not null' => FALSE,
  400. ));
  401. db_drop_index($old_table, $new_column_name);
  402. db_add_index($old_table, $new_column_name, array($new_column_name));
  403. // Rename the table.
  404. db_rename_table($old_table, $new_table);
  405. }
  406. }
  407. cache_clear_all('*', 'cache_field', TRUE);
  408. return $messages;
  409. }
  410. /**
  411. * Update {forum_index} so that only published nodes are indexed.
  412. */
  413. function forum_update_7011() {
  414. $select = db_select('node', 'n')
  415. ->fields('n', array('nid'))
  416. ->condition('status', 0 );
  417. db_delete('forum_index')
  418. ->condition('nid', $select, 'IN')
  419. ->execute();
  420. }
  421. /**
  422. * Add 'created' and 'last_comment_timestamp' indexes.
  423. */
  424. function forum_update_7012() {
  425. db_add_index('forum_index', 'created', array('created'));
  426. db_add_index('forum_index', 'last_comment_timestamp', array('last_comment_timestamp'));
  427. }
  428. /**
  429. * @} End of "addtogroup updates-7.x-extra".
  430. */