Projet

Général

Profil

Paste
Télécharger (17,8 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / comment / comment.install @ 01dfd3b5

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
  // 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
/**
30
 * Implements hook_enable().
31
 */
32
function comment_enable() {
33
  // Insert records into the node_comment_statistics for nodes that are missing.
34
  $query = db_select('node', 'n');
35
  $query->leftJoin('node_comment_statistics', 'ncs', 'ncs.nid = n.nid');
36
  $query->addField('n', 'created', 'last_comment_timestamp');
37
  $query->addField('n', 'uid', 'last_comment_uid');
38
  $query->addField('n', 'nid');
39
  $query->addExpression('0', 'comment_count');
40
  $query->addExpression('NULL', 'last_comment_name');
41
  $query->isNull('ncs.comment_count');
42

    
43
  db_insert('node_comment_statistics')
44
    ->from($query)
45
    ->execute();
46
}
47

    
48
/**
49
 * Implements hook_modules_enabled().
50
 *
51
 * Creates comment body fields for node types existing before the comment module
52
 * is enabled. We use hook_modules_enabled() rather than hook_enable() so we can
53
 * react to node types of existing modules, and those of modules being enabled
54
 * both before and after comment module in the loop of module_enable().
55
 *
56
 * There is a separate comment bundle for each node type to allow for
57
 * per-node-type customization of comment fields. Each one of these bundles
58
 * needs a comment body field instance. A comment bundle is needed even for
59
 * node types whose comments are disabled by default, because individual nodes
60
 * may override that default.
61
 *
62
 * @see comment_node_type_insert()
63
 */
64
function comment_modules_enabled($modules) {
65
  // Only react if comment module is one of the modules being enabled.
66
  // hook_node_type_insert() is used to create body fields while the comment
67
  // module is enabled.
68
  if (in_array('comment', $modules)) {
69
    // Ensure that the list of node types reflects newly enabled modules.
70
    node_types_rebuild();
71

    
72
    // Create comment body fields for each node type, if needed.
73
    foreach (node_type_get_types() as $type => $info) {
74
      _comment_body_field_create($info);
75
    }
76
  }
77
}
78

    
79
/**
80
 * Implements hook_update_dependencies().
81
 */
82
function comment_update_dependencies() {
83
  // comment_update_7005() creates the comment body field and therefore must
84
  // run after all Field modules have been enabled, which happens in
85
  // system_update_7027().
86
  $dependencies['comment'][7005] = array(
87
    'system' => 7027,
88
  );
89

    
90
  // comment_update_7006() needs to query the {filter_format} table to get a
91
  // list of existing text formats, so it must run after filter_update_7000(),
92
  // which creates that table.
93
  $dependencies['comment'][7006] = array(
94
    'filter' => 7000,
95
  );
96

    
97
  return $dependencies;
98
}
99

    
100
/**
101
 * @addtogroup updates-6.x-to-7.x
102
 * @{
103
 */
104

    
105
/**
106
 * Rename comment display setting variables.
107
 */
108
function comment_update_7000() {
109
  $types = _update_7000_node_get_types();
110
  foreach ($types as $type => $type_object) {
111
    variable_del('comment_default_order' . $type);
112

    
113
    // Drupal 6 had four display modes:
114
    // - COMMENT_MODE_FLAT_COLLAPSED = 1
115
    // - COMMENT_MODE_FLAT_EXPANDED = 2
116
    // - COMMENT_MODE_THREADED_COLLAPSED = 3
117
    // - COMMENT_MODE_THREADED_EXPANDED = 4
118
    //
119
    // Drupal 7 doesn't support collapsed/expanded modes anymore, so we
120
    // migrate all the flat modes to COMMENT_MODE_FLAT (0) and all the threaded
121
    // modes to COMMENT_MODE_THREADED (1).
122
    $setting = variable_get('comment_default_mode_' . $type, 4);
123
    if ($setting == 3 || $setting == 4) {
124
      variable_set('comment_default_mode_' . $type, 1);
125
    }
126
    else {
127
      variable_set('comment_default_mode_' . $type, 0);
128
    }
129

    
130
    // There were only two comment modes in the past:
131
    // - 1 was 'required' previously, convert into DRUPAL_REQUIRED (2).
132
    // - 0 was 'optional' previously, convert into DRUPAL_OPTIONAL (1).
133
    $preview = variable_get('comment_preview_' . $type, 1) ? 2 : 1;
134
    variable_set('comment_preview_' . $type, $preview);
135
  }
136
}
137

    
138
/**
139
 * Change comment status from published being 0 to being 1
140
 */
141
function comment_update_7001() {
142
  // Choose a temporary status value different from the existing status values.
143
  $tmp_status = db_query('SELECT MAX(status) FROM {comments}')->fetchField() + 1;
144

    
145
  $changes = array(
146
    0 => $tmp_status,
147
    1 => 0,
148
    $tmp_status => 1,
149
  );
150

    
151
  foreach ($changes as $old => $new) {
152
    db_update('comments')
153
      ->fields(array('status' => $new))
154
      ->condition('status', $old)
155
      ->execute();
156
  }
157
}
158

    
159
/**
160
 * Rename {comments} table to {comment} and upgrade it.
161
 */
162
function comment_update_7002() {
163
  db_rename_table('comments', 'comment');
164

    
165
  // Add user-related indexes. These may already exist from Drupal 6.
166
  if (!db_index_exists('comment', 'comment_uid')) {
167
    db_add_index('comment', 'comment_uid', array('uid'));
168
    db_add_index('node_comment_statistics', 'last_comment_uid', array('last_comment_uid'));
169
  }
170

    
171
  // Create a language column.
172
  db_add_field('comment', 'language', array(
173
    'type' => 'varchar',
174
    'length' => 12,
175
    'not null' => TRUE,
176
    'default' => '',
177
  ));
178
  db_add_index('comment', 'comment_nid_language', array('nid', 'language'));
179
}
180

    
181
/**
182
 * Split {comment}.timestamp into 'created' and 'changed', improve indexing on {comment}.
183
 */
184
function comment_update_7003() {
185
  // Drop the old indexes.
186
  db_drop_index('comment', 'status');
187
  db_drop_index('comment', 'pid');
188

    
189
  // Create a created column.
190
  db_add_field('comment', 'created', array(
191
    'type' => 'int',
192
    'not null' => TRUE,
193
    'default' => 0,
194
  ));
195

    
196
  // Rename the timestamp column to changed.
197
  db_change_field('comment', 'timestamp', 'changed', array(
198
    'type' => 'int',
199
    'not null' => TRUE,
200
    'default' => 0,
201
  ));
202

    
203
  // Migrate the data.
204
  // @todo db_update() should support this.
205
  db_query('UPDATE {comment} SET created = changed');
206

    
207
  // Recreate the indexes.
208
  // The 'comment_num_new' index is optimized for comment_num_new()
209
  // and comment_new_page_count().
210
  db_add_index('comment', 'comment_num_new', array('nid', 'status', 'created', 'cid', 'thread'));
211
  db_add_index('comment', 'comment_pid_status', array('pid', 'status'));
212
}
213

    
214
/**
215
 * Upgrade the {node_comment_statistics} table.
216
 */
217
function comment_update_7004() {
218
  db_add_field('node_comment_statistics', 'cid', array(
219
    'type' => 'int',
220
    'not null' => TRUE,
221
    'default' => 0,
222
    'description' => 'The {comment}.cid of the last comment.',
223
  ));
224
  db_add_index('node_comment_statistics', 'cid', array('cid'));
225

    
226
  // The comment_count index may have been added in Drupal 6.
227
  if (!db_index_exists('node_comment_statistics', 'comment_count')) {
228
    // Add an index on the comment_count.
229
    db_add_index('node_comment_statistics', 'comment_count', array('comment_count'));
230
  }
231
}
232

    
233
/**
234
 * Create the comment_body field.
235
 */
236
function comment_update_7005() {
237
  // Create comment body field.
238
  $field = array(
239
    'field_name' => 'comment_body',
240
    'type' => 'text_long',
241
    'module' => 'text',
242
    'entity_types' => array(
243
      'comment',
244
    ),
245
    'settings' => array(),
246
    'cardinality' => 1,
247
  );
248
  _update_7000_field_create_field($field);
249

    
250
  // Add the field to comments for all existing bundles.
251
  $generic_instance = array(
252
    'entity_type' => 'comment',
253
    'label' => t('Comment'),
254
    'settings' => array(
255
      'text_processing' => 1,
256
    ),
257
    'required' => TRUE,
258
    'display' => array(
259
      'default' => array(
260
        'label' => 'hidden',
261
        'type' => 'text_default',
262
        'weight' => 0,
263
        'settings' => array(),
264
        'module' => 'text',
265
      ),
266
    ),
267
    'widget' => array(
268
      'type' => 'text_textarea',
269
      'settings' => array(
270
        'rows' => 5,
271
      ),
272
      'weight' => 0,
273
      'module' => 'text',
274
    ),
275
    'description' => '',
276
  );
277

    
278
  $types = _update_7000_node_get_types();
279
  foreach ($types as $type => $type_object) {
280
    $instance = $generic_instance;
281
    $instance['bundle'] = 'comment_node_' . $type;
282
    _update_7000_field_create_instance($field, $instance);
283
  }
284
}
285

    
286
/**
287
 * Migrate data from the comment field to field storage.
288
 */
289
function comment_update_7006(&$sandbox) {
290
  // This is a multipass update. First set up some comment variables.
291
  if (empty($sandbox['total'])) {
292
    $comments = (bool) db_query_range('SELECT 1 FROM {comment}', 0, 1)->fetchField();
293
    $sandbox['types'] = array();
294
    if ($comments) {
295
      $sandbox['types'] = array_keys(_update_7000_node_get_types());
296
    }
297
    $sandbox['total'] = count($sandbox['types']);
298
  }
299

    
300
  if (!empty($sandbox['types'])) {
301
    $type = array_shift($sandbox['types']);
302

    
303
    $query = db_select('comment', 'c');
304
    $query->innerJoin('node', 'n', 'c.nid = n.nid AND n.type = :type', array(':type' => $type));
305
    $query->addField('c', 'cid', 'entity_id');
306
    $query->addExpression("'comment_node_$type'", 'bundle');
307
    $query->addExpression("'comment'", 'entity_type');
308
    $query->addExpression('0', 'deleted');
309
    $query->addExpression("'" . LANGUAGE_NONE . "'", 'language');
310
    $query->addExpression('0', 'delta');
311
    $query->addField('c', 'comment', 'comment_body_value');
312
    $query->addField('c', 'format', 'comment_body_format');
313

    
314
    db_insert('field_data_comment_body')
315
      ->from($query)
316
      ->execute();
317

    
318
    $sandbox['#finished'] = 1 - count($sandbox['types']) / $sandbox['total'];
319
  }
320

    
321
  // On the last pass of the update, $sandbox['types'] will be empty.
322
  if (empty($sandbox['types'])) {
323
    // Update the comment body text formats. For an explanation of these
324
    // updates, see the code comments in user_update_7010().
325
    db_update('field_data_comment_body')
326
      ->fields(array('comment_body_format' => NULL))
327
      ->condition('comment_body_value', '')
328
      ->condition('comment_body_format', 0)
329
      ->execute();
330
    $existing_formats = db_query("SELECT format FROM {filter_format}")->fetchCol();
331
    $default_format = variable_get('filter_default_format', 1);
332
    db_update('field_data_comment_body')
333
      ->fields(array('comment_body_format' => $default_format))
334
      ->isNotNull('comment_body_format')
335
      ->condition('comment_body_format', $existing_formats, 'NOT IN')
336
      ->execute();
337

    
338
    // Finally, remove the old comment data.
339
    db_drop_field('comment', 'comment');
340
    db_drop_field('comment', 'format');
341
  }
342
}
343

    
344
/**
345
 * @} End of "addtogroup updates-6.x-to-7.x".
346
 */
347

    
348
/**
349
 * @addtogroup updates-7.x-extra
350
 * @{
351
 */
352

    
353
/**
354
 * Add an index to the created column.
355
 */
356
function comment_update_7007() {
357
  db_add_index('comment', 'comment_created', array('created'));
358
}
359

    
360
/**
361
 * Update database to match Drupal 7 schema.
362
 */
363
function comment_update_7008() {
364
  // Update default status to 1.
365
  db_change_field('comment', 'status', 'status', array(
366
    'type' => 'int',
367
    'unsigned' => TRUE,
368
    'not null' => TRUE,
369
    'default' => 1,
370
    'size' => 'tiny',
371
  ));
372

    
373
  // Realign indexes.
374
  db_drop_index('comment', 'comment_status_pid');
375
  db_add_index('comment', 'comment_status_pid', array('pid', 'status'));
376
  db_drop_index('comment', 'comment_pid_status');
377
  db_drop_index('comment', 'nid');
378
}
379

    
380
/**
381
 * Change the last_comment_timestamp column description.
382
 */
383
function comment_update_7009() {
384
  db_change_field('node_comment_statistics', 'last_comment_timestamp', 'last_comment_timestamp', array(
385
    'type' => 'int',
386
    'not null' => TRUE,
387
    'default' => 0,
388
    'description' => 'The Unix timestamp of the last comment that was posted within this node, from {comment}.changed.',
389
  ));
390
}
391

    
392
/**
393
 * @} End of "addtogroup updates-7.x-extra".
394
 */
395

    
396
/**
397
 * Implements hook_schema().
398
 */
399
function comment_schema() {
400
  $schema['comment'] = array(
401
    'description' => 'Stores comments and associated data.',
402
    'fields' => array(
403
      'cid' => array(
404
        'type' => 'serial',
405
        'not null' => TRUE,
406
        'description' => 'Primary Key: Unique comment ID.',
407
      ),
408
      'pid' => array(
409
        'type' => 'int',
410
        'not null' => TRUE,
411
        'default' => 0,
412
        '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.',
413
      ),
414
      'nid' => array(
415
        'type' => 'int',
416
        'not null' => TRUE,
417
        'default' => 0,
418
        'description' => 'The {node}.nid to which this comment is a reply.',
419
      ),
420
      'uid' => array(
421
        'type' => 'int',
422
        'not null' => TRUE,
423
        'default' => 0,
424
        'description' => 'The {users}.uid who authored the comment. If set to 0, this comment was created by an anonymous user.',
425
      ),
426
      'subject' => array(
427
        'type' => 'varchar',
428
        'length' => 64,
429
        'not null' => TRUE,
430
        'default' => '',
431
        'description' => 'The comment title.',
432
      ),
433
      'hostname' => array(
434
        'type' => 'varchar',
435
        'length' => 128,
436
        'not null' => TRUE,
437
        'default' => '',
438
        'description' => "The author's host name.",
439
      ),
440
      'created' => array(
441
        'type' => 'int',
442
        'not null' => TRUE,
443
        'default' => 0,
444
        'description' => 'The time that the comment was created, as a Unix timestamp.',
445
      ),
446
      'changed' => array(
447
        'type' => 'int',
448
        'not null' => TRUE,
449
        'default' => 0,
450
        'description' => 'The time that the comment was last edited, as a Unix timestamp.',
451
      ),
452
      'status' => array(
453
        'type' => 'int',
454
        'unsigned' => TRUE,
455
        'not null' => TRUE,
456
        'default' => 1,
457
        'size' => 'tiny',
458
        'description' => 'The published status of a comment. (0 = Not Published, 1 = Published)',
459
      ),
460
      'thread' => array(
461
        'type' => 'varchar',
462
        'length' => 255,
463
        'not null' => TRUE,
464
        'description' => "The vancode representation of the comment's place in a thread.",
465
      ),
466
      'name' => array(
467
        'type' => 'varchar',
468
        'length' => 60,
469
        'not null' => FALSE,
470
        'description' => "The comment author's name. Uses {users}.name if the user is logged in, otherwise uses the value typed into the comment form.",
471
      ),
472
      'mail' => array(
473
        'type' => 'varchar',
474
        'length' => 64,
475
        'not null' => FALSE,
476
        '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.",
477
      ),
478
      'homepage' => array(
479
        'type' => 'varchar',
480
        'length' => 255,
481
        'not null' => FALSE,
482
        '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.",
483
      ),
484
      'language' => array(
485
        'description' => 'The {languages}.language of this comment.',
486
        'type' => 'varchar',
487
        'length' => 12,
488
        'not null' => TRUE,
489
        'default' => '',
490
      ),
491
    ),
492
    'indexes' => array(
493
      'comment_status_pid' => array('pid', 'status'),
494
      'comment_num_new' => array('nid', 'status', 'created', 'cid', 'thread'),
495
      'comment_uid' => array('uid'),
496
      'comment_nid_language' => array('nid', 'language'),
497
      'comment_created' => array('created'),
498
    ),
499
    'primary key' => array('cid'),
500
    'foreign keys' => array(
501
      'comment_node' => array(
502
        'table' => 'node',
503
        'columns' => array('nid' => 'nid'),
504
      ),
505
      'comment_author' => array(
506
        'table' => 'users',
507
        'columns' => array('uid' => 'uid'),
508
      ),
509
    ),
510
  );
511

    
512
  $schema['node_comment_statistics'] = array(
513
    'description' => 'Maintains statistics of node and comments posts to show "new" and "updated" flags.',
514
    'fields' => array(
515
      'nid' => array(
516
        'type' => 'int',
517
        'unsigned' => TRUE,
518
        'not null' => TRUE,
519
        'default' => 0,
520
        'description' => 'The {node}.nid for which the statistics are compiled.',
521
      ),
522
      'cid' => array(
523
        'type' => 'int',
524
        'not null' => TRUE,
525
        'default' => 0,
526
        'description' => 'The {comment}.cid of the last comment.',
527
      ),
528
      'last_comment_timestamp' => array(
529
        'type' => 'int',
530
        'not null' => TRUE,
531
        'default' => 0,
532
        'description' => 'The Unix timestamp of the last comment that was posted within this node, from {comment}.changed.',
533
      ),
534
      'last_comment_name' => array(
535
        'type' => 'varchar',
536
        'length' => 60,
537
        'not null' => FALSE,
538
        'description' => 'The name of the latest author to post a comment on this node, from {comment}.name.',
539
      ),
540
      'last_comment_uid' => array(
541
        'type' => 'int',
542
        'not null' => TRUE,
543
        'default' => 0,
544
        'description' => 'The user ID of the latest author to post a comment on this node, from {comment}.uid.',
545
      ),
546
      'comment_count' => array(
547
        'type' => 'int',
548
        'unsigned' => TRUE,
549
        'not null' => TRUE,
550
        'default' => 0,
551
        'description' => 'The total number of comments on this node.',
552
      ),
553
    ),
554
    'primary key' => array('nid'),
555
    'indexes' => array(
556
      'node_comment_timestamp' => array('last_comment_timestamp'),
557
      'comment_count' => array('comment_count'),
558
      'last_comment_uid' => array('last_comment_uid'),
559
    ),
560
    'foreign keys' => array(
561
      'statistics_node' => array(
562
        'table' => 'node',
563
        'columns' => array('nid' => 'nid'),
564
      ),
565
      'last_comment_author' => array(
566
        'table' => 'users',
567
        'columns' => array(
568
          'last_comment_uid' => 'uid',
569
        ),
570
      ),
571
    ),
572
  );
573

    
574
  return $schema;
575
}