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