comment.install 18 KB

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