comment.install 12.6 KB
Newer Older
1 2
<?php

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 13 14
  // Delete comment_body field.
  field_delete_field('comment_body');

15 16
  // Remove variables.
  variable_del('comment_block_count');
17
  $node_types = array_keys(node_type_get_types());
18
  foreach ($node_types as $node_type) {
19
    entity_invoke_bundle_hook('delete', 'comment', 'comment_node_' . $node_type);
20 21 22 23 24 25 26 27 28 29
    variable_del('comment_' . $node_type);
    variable_del('comment_anonymous_' . $node_type);
    variable_del('comment_controls_' . $node_type);
    variable_del('comment_default_mode_' . $node_type);
    variable_del('comment_default_order_' . $node_type);
    variable_del('comment_default_per_page_' . $node_type);
    variable_del('comment_form_location_' . $node_type);
    variable_del('comment_preview_' . $node_type);
    variable_del('comment_subject_field_' . $node_type);
  }
30 31 32

  // Remove states.
  state()->delete('comment.node_comment_statistics_scale');
33 34
}

35
/**
36
 * Implements hook_enable().
37 38 39
 */
function comment_enable() {
  // Insert records into the node_comment_statistics for nodes that are missing.
40 41
  $query = db_select('node', 'n');
  $query->leftJoin('node_comment_statistics', 'ncs', 'ncs.nid = n.nid');
42
  $query->addField('n', 'created', 'last_comment_timestamp');
43 44 45 46 47 48 49 50 51
  $query->addField('n', 'uid', 'last_comment_uid');
  $query->addField('n', 'nid');
  $query->addExpression('0', 'comment_count');
  $query->addExpression('NULL', 'last_comment_name');
  $query->isNull('ncs.comment_count');

  db_insert('node_comment_statistics')
    ->from($query)
    ->execute();
52 53
}

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

    // Create comment body fields for each node type, if needed.
    foreach (node_type_get_types() as $type => $info) {
      _comment_body_field_create($info);
    }
  }
}

85
/**
86
 * Implements hook_schema().
87 88
 */
function comment_schema() {
89
  $schema['comment'] = array(
90
    'description' => 'Stores comments and associated data.',
91
    'fields' => array(
92 93 94
      'cid' => array(
        'type' => 'serial',
        'not null' => TRUE,
95
        'description' => 'Primary Key: Unique comment ID.',
96
      ),
97 98 99 100 101 102
      'uuid' => array(
        'description' => 'Unique Key: Universally unique identifier for this entity.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => FALSE,
      ),
103 104 105 106
      'pid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
107
        '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.',
108 109 110
      ),
      'nid' => array(
        'type' => 'int',
111
        'unsigned' => TRUE,
112 113
        'not null' => TRUE,
        'default' => 0,
114
        'description' => 'The {node}.nid to which this comment is a reply.',
115 116 117
      ),
      'uid' => array(
        'type' => 'int',
118
        'unsigned' => TRUE,
119 120
        'not null' => TRUE,
        'default' => 0,
121
        'description' => 'The {users}.uid who authored the comment. If set to 0, this comment was created by an anonymous user.',
122 123 124 125 126 127
      ),
      'subject' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
128
        'description' => 'The comment title.',
129 130 131 132 133 134
      ),
      'hostname' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
135
        'description' => "The author's host name.",
136
      ),
137 138 139 140 141 142 143
      'created' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The time that the comment was created, as a Unix timestamp.',
      ),
      'changed' => array(
144 145 146
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
147
        'description' => 'The time that the comment was last edited, as a Unix timestamp.',
148 149 150 151 152
      ),
      'status' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
153
        'default' => 1,
154
        'size' => 'tiny',
155
        'description' => 'The published status of a comment. (0 = Not Published, 1 = Published)',
156 157 158 159 160
      ),
      'thread' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
161
        'description' => "The alphadecimal representation of the comment's place in a thread, consisting of a base 36 string prefixed by an integer indicating its length.",
162 163 164 165 166
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => 60,
        'not null' => FALSE,
167
        'description' => "The comment author's name. Uses {users}.name if the user is logged in, otherwise uses the value typed into the comment form.",
168 169 170 171 172
      ),
      'mail' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => FALSE,
173
        '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.",
174 175 176 177 178
      ),
      'homepage' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
179
        '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.",
180
      ),
181
      'langcode' => array(
182
        'description' => 'The {language}.langcode of this comment.',
183 184 185 186 187
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
      ),
188 189
    ),
    'indexes' => array(
190
      'comment_status_pid' => array('pid', 'status'),
191
      'comment_num_new' => array('nid', 'status', 'created', 'cid', 'thread'),
192
      'comment_uid' => array('uid'),
193
      'comment_nid_langcode' => array('nid', 'langcode'),
194
      'comment_created' => array('created'),
195 196
    ),
    'primary key' => array('cid'),
197 198 199
    'unique keys' => array(
      'uuid' => array('uuid'),
    ),
200
    'foreign keys' => array(
201 202 203 204 205 206 207 208
      'comment_node' => array(
        'table' => 'node',
        'columns' => array('nid' => 'nid'),
      ),
      'comment_author' => array(
        'table' => 'users',
        'columns' => array('uid' => 'uid'),
      ),
209
    ),
210 211
  );

212
  $schema['node_comment_statistics'] = array(
213
    'description' => 'Maintains statistics of node and comments posts to show "new" and "updated" flags.',
214 215 216 217 218 219
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
220
        'description' => 'The {node}.nid for which the statistics are compiled.',
221
      ),
222 223 224 225 226 227
      'cid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The {comment}.cid of the last comment.',
      ),
228 229 230 231
      'last_comment_timestamp' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
232
        'description' => 'The Unix timestamp of the last comment that was posted within this node, from {comment}.changed.',
233 234 235 236 237
      ),
      'last_comment_name' => array(
        'type' => 'varchar',
        'length' => 60,
        'not null' => FALSE,
238
        'description' => 'The name of the latest author to post a comment on this node, from {comment}.name.',
239 240 241
      ),
      'last_comment_uid' => array(
        'type' => 'int',
242
        'unsigned' => TRUE,
243 244
        'not null' => TRUE,
        'default' => 0,
245
        'description' => 'The user ID of the latest author to post a comment on this node, from {comment}.uid.',
246 247 248 249 250 251
      ),
      'comment_count' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
252
        'description' => 'The total number of comments on this node.',
253 254 255
      ),
    ),
    'primary key' => array('nid'),
256
    'indexes' => array(
257
      'node_comment_timestamp' => array('last_comment_timestamp'),
258
      'comment_count' => array('comment_count'),
259
      'last_comment_uid' => array('last_comment_uid'),
260
    ),
261
    'foreign keys' => array(
262 263 264 265 266 267 268 269 270 271
      'statistics_node' => array(
        'table' => 'node',
        'columns' => array('nid' => 'nid'),
      ),
      'last_comment_author' => array(
        'table' => 'users',
        'columns' => array(
          'last_comment_uid' => 'uid',
        ),
      ),
272
    ),
273 274 275
  );

  return $schema;
276
}
277 278 279 280 281 282 283 284 285 286

/**
 * @addtogroup updates-7.x-to-8.x
 * @{
 */

/**
 * Renames {comment}.language to {comment}.langcode.
 */
function comment_update_8000() {
287
  db_drop_index('comment', 'comment_nid_language');
288 289 290 291 292 293 294 295 296 297 298
  $langcode_spec = array(
    'type' => 'varchar',
    'length' => 12,
    'not null' => TRUE,
    'default' => '',
    'description' => "Language code, e.g. 'de' or 'en-US'.",
  );
  db_change_field('comment', 'language', 'langcode', $langcode_spec);
  db_add_index('comment', 'comment_nid_langcode', array('nid', 'langcode'));
}

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
/**
 * Create a UUID column for comments.
 */
function comment_update_8001() {
  $spec = array(
    'description' => 'Unique Key: Universally unique identifier for this entity.',
    'type' => 'varchar',
    'length' => 128,
    'not null' => FALSE,
  );
  $keys = array(
    'unique keys' => array(
      'uuid' => array('uuid'),
    ),
  );
  // Account for sites having the contributed UUID module installed.
  if (db_field_exists('comment', 'uuid')) {
    db_change_field('comment', 'uuid', 'uuid', $spec, $keys);
  }
  else {
    db_add_field('comment', 'uuid', $spec, $keys);
  }
}

323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
/**
 * Make *id fields unsigned.
 */
function comment_update_8002() {
  db_drop_index('comment', 'comment_num_new');
  db_drop_index('comment', 'comment_nid_langcode');
  db_change_field('comment', 'nid', 'nid',
    array(
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
      'description' => 'The {node}.nid to which this comment is a reply.',
    ),
    array('indexes' => array(
      'comment_num_new' => array('nid', 'status', 'created', 'cid', 'thread'),
      'comment_nid_langcode' => array('nid', 'langcode'),
    ))
  );
  db_drop_index('comment', 'comment_uid');
  db_change_field('comment', 'uid', 'uid',
    array(
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
      'description' => 'The {users}.uid who authored the comment. If set to 0, this comment was created by an anonymous user.',
    ),
    array('indexes' => array(
      'comment_uid' => array('uid'),
    ))
  );

  db_drop_index('node_comment_statistics', 'last_comment_uid');
  db_change_field('node_comment_statistics', 'last_comment_uid', 'last_comment_uid',
    array(
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
      'description' => 'The user ID of the latest author to post a comment on this node, from {comment}.uid.',
    ),
    array('indexes' => array(
      'last_comment_uid' => array('last_comment_uid'),
    ))
  );
}

371
/**
372
 * Generate a UUID for all comments.
373 374 375 376 377 378 379 380 381 382 383 384 385 386
 */
function comment_update_8003(&$sandbox) {
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['last'] = 0;
    $sandbox['max'] = db_query('SELECT COUNT(cid) FROM {comment} WHERE uuid IS NULL')->fetchField();
  }

  $cids = db_query_range('SELECT cid FROM {comment} WHERE cid > :cid AND uuid IS NULL ORDER BY cid ASC', 0, 10, array(':cid' => $sandbox['last']))->fetchCol();
  update_add_uuids($sandbox, 'comment', 'cid', $cids);

  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
}

387 388 389 390 391 392 393 394 395
/**
 * Convert variables to state.
 */
function comment_update_8004() {
  update_variables_to_state(array(
    'node_cron_comments_scale' => 'comment.node_comment_statistics_scale',
  ));
}

396
/**
397
 * @} End of "addtogroup updates-7.x-to-8.x".
398 399
 * The next series of updates should start at 9000.
 */