forum.install

  1. drupal
    1. 5 modules/forum/forum.install
    2. 6 modules/forum/forum.install
    3. 7 modules/forum/forum.install
    4. 8 core/modules/forum/forum.install

Install, update and uninstall functions for the forum module.

Functions & methods

NameDescription
forum_enableImplements hook_enable().
forum_installImplements hook_install().
forum_schemaImplements hook_schema().
forum_uninstallImplements hook_uninstall().
forum_update_7000Add new index to forum table.
forum_update_7001Create new {forum_index} table.
forum_update_7002Add new index to forum_index table.
forum_update_7003Rename field to 'taxonomy_forums'.
forum_update_7011Update {forum_index} so that only published nodes are indexed.
forum_update_dependenciesImplements hook_update_dependencies().

File

modules/forum/forum.install
View source
  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. ),
  211. 'foreign keys' => array(
  212. 'tracked_node' => array(
  213. 'table' => 'node',
  214. 'columns' => array('nid' => 'nid'),
  215. ),
  216. 'term' => array(
  217. 'table' => 'taxonomy_term_data',
  218. 'columns' => array(
  219. 'tid' => 'tid',
  220. ),
  221. ),
  222. ),
  223. );
  224. return $schema;
  225. }
  226. /**
  227. * Implements hook_update_dependencies().
  228. */
  229. function forum_update_dependencies() {
  230. $dependencies['forum'][7003] = array(
  231. // Forum update 7003 uses field API update functions, so must run after
  232. // Field API has been enabled.
  233. 'system' => 7020,
  234. // Forum update 7003 relies on updated taxonomy module schema. Ensure it
  235. // runs after all taxonomy updates.
  236. 'taxonomy' => 7010,
  237. );
  238. return $dependencies;
  239. }
  240. /**
  241. * Add new index to forum table.
  242. */
  243. function forum_update_7000() {
  244. db_drop_index('forum', 'nid');
  245. db_add_index('forum', 'forum_topic', array('nid', 'tid'));
  246. }
  247. /**
  248. * Create new {forum_index} table.
  249. */
  250. function forum_update_7001() {
  251. $forum_index = array(
  252. 'description' => 'Maintains denormalized information about node/term relationships.',
  253. 'fields' => array(
  254. 'nid' => array(
  255. 'description' => 'The {node}.nid this record tracks.',
  256. 'type' => 'int',
  257. 'unsigned' => TRUE,
  258. 'not null' => TRUE,
  259. 'default' => 0,
  260. ),
  261. 'title' => array(
  262. 'description' => 'The title of this node, always treated as non-markup plain text.',
  263. 'type' => 'varchar',
  264. 'length' => 255,
  265. 'not null' => TRUE,
  266. 'default' => '',
  267. ),
  268. 'tid' => array(
  269. 'description' => 'The term ID.',
  270. 'type' => 'int',
  271. 'unsigned' => TRUE,
  272. 'not null' => TRUE,
  273. 'default' => 0,
  274. ),
  275. 'sticky' => array(
  276. 'description' => 'Boolean indicating whether the node is sticky.',
  277. 'type' => 'int',
  278. 'not null' => FALSE,
  279. 'default' => 0,
  280. 'size' => 'tiny',
  281. ),
  282. 'created' => array(
  283. 'description' => 'The Unix timestamp when the node was created.',
  284. 'type' => 'int',
  285. 'unsigned' => TRUE,
  286. 'not null' => TRUE,
  287. 'default'=> 0,
  288. ),
  289. 'last_comment_timestamp' => array(
  290. 'type' => 'int',
  291. 'not null' => TRUE,
  292. 'default' => 0,
  293. 'description' => 'The Unix timestamp of the last comment that was posted within this node, from {comment}.timestamp.',
  294. ),
  295. 'comment_count' => array(
  296. 'type' => 'int',
  297. 'unsigned' => TRUE,
  298. 'not null' => TRUE,
  299. 'default' => 0,
  300. 'description' => 'The total number of comments on this node.',
  301. ),
  302. ),
  303. 'indexes' => array(
  304. 'forum_topics' => array('tid', 'sticky', 'last_comment_timestamp'),
  305. ),
  306. 'foreign keys' => array(
  307. 'tracked_node' => array(
  308. 'table' => 'node',
  309. 'columns' => array('nid' => 'nid'),
  310. ),
  311. 'term' => array(
  312. 'table' => 'taxonomy_term_data',
  313. 'columns' => array(
  314. 'tid' => 'tid',
  315. ),
  316. ),
  317. ),
  318. );
  319. db_create_table('forum_index', $forum_index);
  320. $select = db_select('node', 'n');
  321. $forum_alias = $select->join('forum', 'f', 'n.vid = f.vid');
  322. $ncs_alias = $select->join('node_comment_statistics', 'ncs', 'n.nid = ncs.nid');
  323. $select
  324. ->fields('n', array('nid', 'title', 'sticky', 'created'))
  325. ->fields($forum_alias, array('tid'))
  326. ->fields($ncs_alias, array('last_comment_timestamp', 'comment_count'));
  327. db_insert('forum_index')
  328. ->fields(array('nid', 'title', 'sticky', 'created', 'tid', 'last_comment_timestamp', 'comment_count'))
  329. ->from($select)
  330. ->execute();
  331. }
  332. /**
  333. * Add new index to forum_index table.
  334. */
  335. function forum_update_7002() {
  336. db_drop_index('forum_index', 'forum_topics');
  337. db_add_index('forum_index', 'forum_topics', array('nid', 'tid', 'sticky', 'last_comment_timestamp'));
  338. }
  339. /**
  340. * @addtogroup updates-7.x-extra
  341. * @{
  342. */
  343. /**
  344. * Rename field to 'taxonomy_forums'.
  345. */
  346. function forum_update_7003() {
  347. $messages = array();
  348. $new_field_name = 'taxonomy_forums';
  349. // Test to see if the taxonomy_forums field exists.
  350. $fields = _update_7000_field_read_fields(array('field_name' => $new_field_name));
  351. if ($fields) {
  352. // Since the field exists, we're done.
  353. return;
  354. }
  355. // Calculate the old field name.
  356. $vid = variable_get('forum_nav_vocabulary', 0);
  357. $vocabulary_machine_name = db_select('taxonomy_vocabulary', 'tv')
  358. ->fields('tv', array('machine_name'))
  359. ->condition('vid', $vid)
  360. ->execute()
  361. ->fetchField();
  362. $old_field_name = 'taxonomy_' . $vocabulary_machine_name;
  363. // Read the old fields.
  364. $old_fields = _update_7000_field_read_fields(array('field_name' => $old_field_name));
  365. foreach ($old_fields as $old_field) {
  366. if ($old_field['storage']['type'] != 'field_sql_storage') {
  367. $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(
  368. '%id' => $old_field['id'],
  369. '%old_field_name' => $old_field_name,
  370. '%new_field_name' => $new_field_name,
  371. ));
  372. continue;
  373. }
  374. // Update {field_config}.
  375. db_update('field_config')
  376. ->fields(array('field_name' => $new_field_name))
  377. ->condition('id', $old_field['id'])
  378. ->execute();
  379. // Update {field_config_instance}.
  380. db_update('field_config_instance')
  381. ->fields(array('field_name' => $new_field_name))
  382. ->condition('field_id', $old_field['id'])
  383. ->execute();
  384. // The tables that need updating in the form 'old_name' => 'new_name'.
  385. $tables = array(
  386. 'field_data_' . $old_field_name => 'field_data_' . $new_field_name,
  387. 'field_revision_' . $old_field_name => 'field_revision_' . $new_field_name,
  388. );
  389. foreach ($tables as $old_table => $new_table) {
  390. $old_column_name = $old_field_name . '_tid';
  391. $new_column_name = $new_field_name . '_tid';
  392. // Rename the column.
  393. db_drop_index($old_table, $old_column_name);
  394. db_change_field($old_table, $old_column_name, $new_column_name, array(
  395. 'type' => 'int',
  396. 'unsigned' => TRUE,
  397. 'not null' => FALSE,
  398. ));
  399. db_drop_index($old_table, $new_column_name);
  400. db_add_index($old_table, $new_column_name, array($new_column_name));
  401. // Rename the table.
  402. db_rename_table($old_table, $new_table);
  403. }
  404. }
  405. cache_clear_all('*', 'cache_field', TRUE);
  406. return $messages;
  407. }
  408. /**
  409. * @} End of "addtogroup updates-7.x-extra"
  410. */
  411. /**
  412. * Update {forum_index} so that only published nodes are indexed.
  413. */
  414. function forum_update_7011() {
  415. $select = db_select('node', 'n')
  416. ->fields('n', array('nid'))
  417. ->condition('status', 0 );
  418. db_delete('forum_index')
  419. ->condition('nid', $select, 'IN')
  420. ->execute();
  421. }
Login or register to post comments