comment.install 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the comment module.
  5. */
  6. /**
  7. * Implements hook_uninstall().
  8. */
  9. function comment_uninstall() {
  10. // Delete comment_body field.
  11. field_delete_field('comment_body');
  12. // Remove variables.
  13. variable_del('comment_block_count');
  14. $node_types = array_keys(node_type_get_types());
  15. foreach ($node_types as $node_type) {
  16. field_attach_delete_bundle('comment', 'comment_node_' . $node_type);
  17. variable_del('comment_' . $node_type);
  18. variable_del('comment_anonymous_' . $node_type);
  19. variable_del('comment_controls_' . $node_type);
  20. variable_del('comment_default_mode_' . $node_type);
  21. variable_del('comment_default_order_' . $node_type);
  22. variable_del('comment_default_per_page_' . $node_type);
  23. variable_del('comment_form_location_' . $node_type);
  24. variable_del('comment_preview_' . $node_type);
  25. variable_del('comment_subject_field_' . $node_type);
  26. }
  27. }
  28. /**
  29. * Implements hook_enable().
  30. */
  31. function comment_enable() {
  32. // Insert records into the node_comment_statistics for nodes that are missing.
  33. $query = db_select('node', 'n');
  34. $query->leftJoin('node_comment_statistics', 'ncs', 'ncs.nid = n.nid');
  35. $query->addField('n', 'created', 'last_comment_timestamp');
  36. $query->addField('n', 'uid', 'last_comment_uid');
  37. $query->addField('n', 'nid');
  38. $query->addExpression('0', 'comment_count');
  39. $query->addExpression('NULL', 'last_comment_name');
  40. $query->isNull('ncs.comment_count');
  41. db_insert('node_comment_statistics')
  42. ->from($query)
  43. ->execute();
  44. }
  45. /**
  46. * Implements hook_modules_enabled().
  47. *
  48. * Creates comment body fields for node types existing before the comment module
  49. * is enabled. We use hook_modules_enabled() rather than hook_enable() so we can
  50. * react to node types of existing modules, and those of modules being enabled
  51. * both before and after comment module in the loop of module_enable().
  52. *
  53. * There is a separate comment bundle for each node type to allow for
  54. * per-node-type customization of comment fields. Each one of these bundles
  55. * needs a comment body field instance. A comment bundle is needed even for
  56. * node types whose comments are disabled by default, because individual nodes
  57. * may override that default.
  58. *
  59. * @see comment_node_type_insert()
  60. */
  61. function comment_modules_enabled($modules) {
  62. // Only react if comment module is one of the modules being enabled.
  63. // hook_node_type_insert() is used to create body fields while the comment
  64. // module is enabled.
  65. if (in_array('comment', $modules)) {
  66. // Ensure that the list of node types reflects newly enabled modules.
  67. node_types_rebuild();
  68. // Create comment body fields for each node type, if needed.
  69. foreach (node_type_get_types() as $type => $info) {
  70. _comment_body_field_create($info);
  71. }
  72. }
  73. }
  74. /**
  75. * Implements hook_update_dependencies().
  76. */
  77. function comment_update_dependencies() {
  78. // comment_update_7005() creates the comment body field and therefore must
  79. // run after all Field modules have been enabled, which happens in
  80. // system_update_7027().
  81. $dependencies['comment'][7005] = array(
  82. 'system' => 7027,
  83. );
  84. // comment_update_7006() needs to query the {filter_format} table to get a
  85. // list of existing text formats, so it must run after filter_update_7000(),
  86. // which creates that table.
  87. $dependencies['comment'][7006] = array(
  88. 'filter' => 7000,
  89. );
  90. return $dependencies;
  91. }
  92. /**
  93. * @addtogroup updates-6.x-to-7.x
  94. * @{
  95. */
  96. /**
  97. * Rename comment display setting variables.
  98. */
  99. function comment_update_7000() {
  100. $types = _update_7000_node_get_types();
  101. foreach ($types as $type => $type_object) {
  102. variable_del('comment_default_order' . $type);
  103. // Drupal 6 had four display modes:
  104. // - COMMENT_MODE_FLAT_COLLAPSED = 1
  105. // - COMMENT_MODE_FLAT_EXPANDED = 2
  106. // - COMMENT_MODE_THREADED_COLLAPSED = 3
  107. // - COMMENT_MODE_THREADED_EXPANDED = 4
  108. //
  109. // Drupal 7 doesn't support collapsed/expanded modes anymore, so we
  110. // migrate all the flat modes to COMMENT_MODE_FLAT (0) and all the threaded
  111. // modes to COMMENT_MODE_THREADED (1).
  112. $setting = variable_get('comment_default_mode_' . $type, 4);
  113. if ($setting == 3 || $setting == 4) {
  114. variable_set('comment_default_mode_' . $type, 1);
  115. }
  116. else {
  117. variable_set('comment_default_mode_' . $type, 0);
  118. }
  119. // There were only two comment modes in the past:
  120. // - 1 was 'required' previously, convert into DRUPAL_REQUIRED (2).
  121. // - 0 was 'optional' previously, convert into DRUPAL_OPTIONAL (1).
  122. $preview = variable_get('comment_preview_' . $type, 1) ? 2 : 1;
  123. variable_set('comment_preview_' . $type, $preview);
  124. }
  125. }
  126. /**
  127. * Change comment status from published being 0 to being 1
  128. */
  129. function comment_update_7001() {
  130. // Choose a temporary status value different from the existing status values.
  131. $tmp_status = db_query('SELECT MAX(status) FROM {comments}')->fetchField() + 1;
  132. $changes = array(
  133. 0 => $tmp_status,
  134. 1 => 0,
  135. $tmp_status => 1,
  136. );
  137. foreach ($changes as $old => $new) {
  138. db_update('comments')
  139. ->fields(array('status' => $new))
  140. ->condition('status', $old)
  141. ->execute();
  142. }
  143. }
  144. /**
  145. * Rename {comments} table to {comment} and upgrade it.
  146. */
  147. function comment_update_7002() {
  148. db_rename_table('comments', 'comment');
  149. // Add user-related indexes. These may already exist from Drupal 6.
  150. if (!db_index_exists('comment', 'comment_uid')) {
  151. db_add_index('comment', 'comment_uid', array('uid'));
  152. db_add_index('node_comment_statistics', 'last_comment_uid', array('last_comment_uid'));
  153. }
  154. // Create a language column.
  155. db_add_field('comment', 'language', array(
  156. 'type' => 'varchar',
  157. 'length' => 12,
  158. 'not null' => TRUE,
  159. 'default' => '',
  160. ));
  161. db_add_index('comment', 'comment_nid_language', array('nid', 'language'));
  162. }
  163. /**
  164. * Split {comment}.timestamp into 'created' and 'changed', improve indexing on {comment}.
  165. */
  166. function comment_update_7003() {
  167. // Drop the old indexes.
  168. db_drop_index('comment', 'status');
  169. db_drop_index('comment', 'pid');
  170. // Create a created column.
  171. db_add_field('comment', 'created', array(
  172. 'type' => 'int',
  173. 'not null' => TRUE,
  174. 'default' => 0,
  175. ));
  176. // Rename the timestamp column to changed.
  177. db_change_field('comment', 'timestamp', 'changed', array(
  178. 'type' => 'int',
  179. 'not null' => TRUE,
  180. 'default' => 0,
  181. ));
  182. // Migrate the data.
  183. // @todo db_update() should support this.
  184. db_query('UPDATE {comment} SET created = changed');
  185. // Recreate the indexes.
  186. // The 'comment_num_new' index is optimized for comment_num_new()
  187. // and comment_new_page_count().
  188. db_add_index('comment', 'comment_num_new', array('nid', 'status', 'created', 'cid', 'thread'));
  189. db_add_index('comment', 'comment_pid_status', array('pid', 'status'));
  190. }
  191. /**
  192. * Upgrade the {node_comment_statistics} table.
  193. */
  194. function comment_update_7004() {
  195. db_add_field('node_comment_statistics', 'cid', array(
  196. 'type' => 'int',
  197. 'not null' => TRUE,
  198. 'default' => 0,
  199. 'description' => 'The {comment}.cid of the last comment.',
  200. ));
  201. db_add_index('node_comment_statistics', 'cid', array('cid'));
  202. // The comment_count index may have been added in Drupal 6.
  203. if (!db_index_exists('node_comment_statistics', 'comment_count')) {
  204. // Add an index on the comment_count.
  205. db_add_index('node_comment_statistics', 'comment_count', array('comment_count'));
  206. }
  207. }
  208. /**
  209. * Create the comment_body field.
  210. */
  211. function comment_update_7005() {
  212. // Create comment body field.
  213. $field = array(
  214. 'field_name' => 'comment_body',
  215. 'type' => 'text_long',
  216. 'module' => 'text',
  217. 'entity_types' => array(
  218. 'comment',
  219. ),
  220. 'settings' => array(),
  221. 'cardinality' => 1,
  222. );
  223. _update_7000_field_create_field($field);
  224. // Add the field to comments for all existing bundles.
  225. $generic_instance = array(
  226. 'entity_type' => 'comment',
  227. 'label' => t('Comment'),
  228. 'settings' => array(
  229. 'text_processing' => 1,
  230. ),
  231. 'required' => TRUE,
  232. 'display' => array(
  233. 'default' => array(
  234. 'label' => 'hidden',
  235. 'type' => 'text_default',
  236. 'weight' => 0,
  237. 'settings' => array(),
  238. 'module' => 'text',
  239. ),
  240. ),
  241. 'widget' => array(
  242. 'type' => 'text_textarea',
  243. 'settings' => array(
  244. 'rows' => 5,
  245. ),
  246. 'weight' => 0,
  247. 'module' => 'text',
  248. ),
  249. 'description' => '',
  250. );
  251. $types = _update_7000_node_get_types();
  252. foreach ($types as $type => $type_object) {
  253. $instance = $generic_instance;
  254. $instance['bundle'] = 'comment_node_' . $type;
  255. _update_7000_field_create_instance($field, $instance);
  256. }
  257. }
  258. /**
  259. * Migrate data from the comment field to field storage.
  260. */
  261. function comment_update_7006(&$sandbox) {
  262. // This is a multipass update. First set up some comment variables.
  263. if (empty($sandbox['total'])) {
  264. $comments = (bool) db_query_range('SELECT 1 FROM {comment}', 0, 1)->fetchField();
  265. $sandbox['types'] = array();
  266. if ($comments) {
  267. $sandbox['types'] = array_keys(_update_7000_node_get_types());
  268. }
  269. $sandbox['total'] = count($sandbox['types']);
  270. }
  271. if (!empty($sandbox['types'])) {
  272. $type = array_shift($sandbox['types']);
  273. $query = db_select('comment', 'c');
  274. $query->innerJoin('node', 'n', 'c.nid = n.nid AND n.type = :type', array(':type' => $type));
  275. $query->addField('c', 'cid', 'entity_id');
  276. $query->addExpression("'comment_node_$type'", 'bundle');
  277. $query->addExpression("'comment'", 'entity_type');
  278. $query->addExpression('0', 'deleted');
  279. $query->addExpression("'" . LANGUAGE_NONE . "'", 'language');
  280. $query->addExpression('0', 'delta');
  281. $query->addField('c', 'comment', 'comment_body_value');
  282. $query->addField('c', 'format', 'comment_body_format');
  283. db_insert('field_data_comment_body')
  284. ->from($query)
  285. ->execute();
  286. $sandbox['#finished'] = 1 - count($sandbox['types']) / $sandbox['total'];
  287. }
  288. // On the last pass of the update, $sandbox['types'] will be empty.
  289. if (empty($sandbox['types'])) {
  290. // Update the comment body text formats. For an explanation of these
  291. // updates, see the code comments in user_update_7010().
  292. db_update('field_data_comment_body')
  293. ->fields(array('comment_body_format' => NULL))
  294. ->condition('comment_body_value', '')
  295. ->condition('comment_body_format', 0)
  296. ->execute();
  297. $existing_formats = db_query("SELECT format FROM {filter_format}")->fetchCol();
  298. $default_format = variable_get('filter_default_format', 1);
  299. db_update('field_data_comment_body')
  300. ->fields(array('comment_body_format' => $default_format))
  301. ->isNotNull('comment_body_format')
  302. ->condition('comment_body_format', $existing_formats, 'NOT IN')
  303. ->execute();
  304. // Finally, remove the old comment data.
  305. db_drop_field('comment', 'comment');
  306. db_drop_field('comment', 'format');
  307. }
  308. }
  309. /**
  310. * @} End of "addtogroup updates-6.x-to-7.x".
  311. */
  312. /**
  313. * @addtogroup updates-7.x-extra
  314. * @{
  315. */
  316. /**
  317. * Add an index to the created column.
  318. */
  319. function comment_update_7007() {
  320. db_add_index('comment', 'comment_created', array('created'));
  321. }
  322. /**
  323. * Update database to match Drupal 7 schema.
  324. */
  325. function comment_update_7008() {
  326. // Update default status to 1.
  327. db_change_field('comment', 'status', 'status', array(
  328. 'type' => 'int',
  329. 'unsigned' => TRUE,
  330. 'not null' => TRUE,
  331. 'default' => 1,
  332. 'size' => 'tiny',
  333. ));
  334. // Realign indexes.
  335. db_drop_index('comment', 'comment_status_pid');
  336. db_add_index('comment', 'comment_status_pid', array('pid', 'status'));
  337. db_drop_index('comment', 'comment_pid_status');
  338. db_drop_index('comment', 'nid');
  339. }
  340. /**
  341. * Change the last_comment_timestamp column description.
  342. */
  343. function comment_update_7009() {
  344. db_change_field('node_comment_statistics', 'last_comment_timestamp', 'last_comment_timestamp', array(
  345. 'type' => 'int',
  346. 'not null' => TRUE,
  347. 'default' => 0,
  348. 'description' => 'The Unix timestamp of the last comment that was posted within this node, from {comment}.changed.',
  349. ));
  350. }
  351. /**
  352. * @} End of "addtogroup updates-7.x-extra".
  353. */
  354. /**
  355. * Implements hook_schema().
  356. */
  357. function comment_schema() {
  358. $schema['comment'] = array(
  359. 'description' => 'Stores comments and associated data.',
  360. 'fields' => array(
  361. 'cid' => array(
  362. 'type' => 'serial',
  363. 'not null' => TRUE,
  364. 'description' => 'Primary Key: Unique comment ID.',
  365. ),
  366. 'pid' => array(
  367. 'type' => 'int',
  368. 'not null' => TRUE,
  369. 'default' => 0,
  370. 'description' => 'The {comment}.cid to which this comment is a reply. If set to 0, this comment is not a reply to an existing comment.',
  371. ),
  372. 'nid' => array(
  373. 'type' => 'int',
  374. 'not null' => TRUE,
  375. 'default' => 0,
  376. 'description' => 'The {node}.nid to which this comment is a reply.',
  377. ),
  378. 'uid' => array(
  379. 'type' => 'int',
  380. 'not null' => TRUE,
  381. 'default' => 0,
  382. 'description' => 'The {users}.uid who authored the comment. If set to 0, this comment was created by an anonymous user.',
  383. ),
  384. 'subject' => array(
  385. 'type' => 'varchar',
  386. 'length' => 64,
  387. 'not null' => TRUE,
  388. 'default' => '',
  389. 'description' => 'The comment title.',
  390. ),
  391. 'hostname' => array(
  392. 'type' => 'varchar',
  393. 'length' => 128,
  394. 'not null' => TRUE,
  395. 'default' => '',
  396. 'description' => "The author's host name.",
  397. ),
  398. 'created' => array(
  399. 'type' => 'int',
  400. 'not null' => TRUE,
  401. 'default' => 0,
  402. 'description' => 'The time that the comment was created, as a Unix timestamp.',
  403. ),
  404. 'changed' => array(
  405. 'type' => 'int',
  406. 'not null' => TRUE,
  407. 'default' => 0,
  408. 'description' => 'The time that the comment was last edited, as a Unix timestamp.',
  409. ),
  410. 'status' => array(
  411. 'type' => 'int',
  412. 'unsigned' => TRUE,
  413. 'not null' => TRUE,
  414. 'default' => 1,
  415. 'size' => 'tiny',
  416. 'description' => 'The published status of a comment. (0 = Not Published, 1 = Published)',
  417. ),
  418. 'thread' => array(
  419. 'type' => 'varchar',
  420. 'length' => 255,
  421. 'not null' => TRUE,
  422. 'description' => "The vancode representation of the comment's place in a thread.",
  423. ),
  424. 'name' => array(
  425. 'type' => 'varchar',
  426. 'length' => 60,
  427. 'not null' => FALSE,
  428. 'description' => "The comment author's name. Uses {users}.name if the user is logged in, otherwise uses the value typed into the comment form.",
  429. ),
  430. 'mail' => array(
  431. 'type' => 'varchar',
  432. 'length' => 64,
  433. 'not null' => FALSE,
  434. 'description' => "The comment author's e-mail address from the comment form, if user is anonymous, and the 'Anonymous users may/must leave their contact information' setting is turned on.",
  435. ),
  436. 'homepage' => array(
  437. 'type' => 'varchar',
  438. 'length' => 255,
  439. 'not null' => FALSE,
  440. 'description' => "The comment author's home page address from the comment form, if user is anonymous, and the 'Anonymous users may/must leave their contact information' setting is turned on.",
  441. ),
  442. 'language' => array(
  443. 'description' => 'The {languages}.language of this comment.',
  444. 'type' => 'varchar',
  445. 'length' => 12,
  446. 'not null' => TRUE,
  447. 'default' => '',
  448. ),
  449. ),
  450. 'indexes' => array(
  451. 'comment_status_pid' => array('pid', 'status'),
  452. 'comment_num_new' => array('nid', 'status', 'created', 'cid', 'thread'),
  453. 'comment_uid' => array('uid'),
  454. 'comment_nid_language' => array('nid', 'language'),
  455. 'comment_created' => array('created'),
  456. ),
  457. 'primary key' => array('cid'),
  458. 'foreign keys' => array(
  459. 'comment_node' => array(
  460. 'table' => 'node',
  461. 'columns' => array('nid' => 'nid'),
  462. ),
  463. 'comment_author' => array(
  464. 'table' => 'users',
  465. 'columns' => array('uid' => 'uid'),
  466. ),
  467. ),
  468. );
  469. $schema['node_comment_statistics'] = array(
  470. 'description' => 'Maintains statistics of node and comments posts to show "new" and "updated" flags.',
  471. 'fields' => array(
  472. 'nid' => array(
  473. 'type' => 'int',
  474. 'unsigned' => TRUE,
  475. 'not null' => TRUE,
  476. 'default' => 0,
  477. 'description' => 'The {node}.nid for which the statistics are compiled.',
  478. ),
  479. 'cid' => array(
  480. 'type' => 'int',
  481. 'not null' => TRUE,
  482. 'default' => 0,
  483. 'description' => 'The {comment}.cid of the last comment.',
  484. ),
  485. 'last_comment_timestamp' => array(
  486. 'type' => 'int',
  487. 'not null' => TRUE,
  488. 'default' => 0,
  489. 'description' => 'The Unix timestamp of the last comment that was posted within this node, from {comment}.changed.',
  490. ),
  491. 'last_comment_name' => array(
  492. 'type' => 'varchar',
  493. 'length' => 60,
  494. 'not null' => FALSE,
  495. 'description' => 'The name of the latest author to post a comment on this node, from {comment}.name.',
  496. ),
  497. 'last_comment_uid' => array(
  498. 'type' => 'int',
  499. 'not null' => TRUE,
  500. 'default' => 0,
  501. 'description' => 'The user ID of the latest author to post a comment on this node, from {comment}.uid.',
  502. ),
  503. 'comment_count' => array(
  504. 'type' => 'int',
  505. 'unsigned' => TRUE,
  506. 'not null' => TRUE,
  507. 'default' => 0,
  508. 'description' => 'The total number of comments on this node.',
  509. ),
  510. ),
  511. 'primary key' => array('nid'),
  512. 'indexes' => array(
  513. 'node_comment_timestamp' => array('last_comment_timestamp'),
  514. 'comment_count' => array('comment_count'),
  515. 'last_comment_uid' => array('last_comment_uid'),
  516. ),
  517. 'foreign keys' => array(
  518. 'statistics_node' => array(
  519. 'table' => 'node',
  520. 'columns' => array('nid' => 'nid'),
  521. ),
  522. 'last_comment_author' => array(
  523. 'table' => 'users',
  524. 'columns' => array(
  525. 'last_comment_uid' => 'uid',
  526. ),
  527. ),
  528. ),
  529. );
  530. return $schema;
  531. }