comment_notify.install 5.84 KB
Newer Older
1
<?php
2 3 4 5
/**
 * @file
 * comment_notify.install.
 */
6 7

/**
8
 * Implements hook_install().
9 10
 */
function comment_notify_install() {
11

12 13
  // Create entries for existing comments.

14 15 16 17
  $comments_select = db_select('comment', 'c');
  $comments_select->join('users', 'u', 'c.uid = u.uid');
  $comments_select->addField('c', 'cid');
  $comments_select->addExpression('0', 'notify');
18
  // Mix in a random string to all values.
19
  $salt = uniqid(mt_rand(), TRUE);
20
  if (db_driver() == 'pgsql') {
21
    $comments_select->addExpression("MD5(:salt || c.mail || COALESCE(u.mail, u.init) || c.uid || c.name || c.nid || c.hostname || c.cid)", 'notify_hash', array(':salt' => $salt));
22 23
  }
  else {
24
    $comments_select->addExpression("MD5(CONCAT(:salt, c.mail, COALESCE(u.mail, u.init), c.uid, c.name, c.nid, c.hostname, c.cid))", 'notify_hash', array(':salt' => $salt));
25
  }
26

27
  // Set module weight low so that other modules act on the comment first.
28 29
  db_insert('comment_notify')->from($comments_select)->execute();
  db_update('system')->
30 31 32 33
    fields(array(
    'weight' => 10
    ))
    ->condition('name', 'comment_notify');
34 35
}

36
/**
37
 * Implements hook_uninstall().
38
 */
39
function comment_notify_uninstall() {
40
  variable_del('node_notify_default_mailtext');
41
  db_delete('variable')
42
    ->where('name', "comment_notify_%", 'LIKE');
43
}
44

45
/**
46
 * Implements hook_schema().
47
 */
48 49 50 51 52
function comment_notify_schema() {
  $schema['comment_notify'] = array(
    'description' => t('Stores information about which commenters on the site have subscriped to followup emails.'),
    'fields' => array(
      'cid' => array(
53 54
        'type' => 'int',
        'unsigned' => TRUE,
55 56 57 58 59
        'description' => 'The comment id from {comments}.cid',
        'not null' => TRUE,
        'disp-width' => '11'),
      'notify' => array(
        'type' => 'int',
60
        'description' => 'An integer indicating the type of subscription: 0 means not subscribed, 1 means subscribed to all comments, and 2 means only subscribed to replies of this comment.',
61 62 63 64 65
        'size' => 'tiny',
        'not null' => TRUE,
        'disp-width' => '11'),
      'notify_hash' => array(
        'type' => 'varchar',
66 67
        'description' => 'A hash of unique information about the commenter.  Used for unsubscribing users.',
        'length' => '128',
68 69
        'not null' => TRUE,
        'default' => ''),
70 71 72 73 74
      'notified' => array(
        'type' => 'int',
        'description' => 'A boolean indicator for whether or not a notification for the comment has been sent: 1 means yes, 0 means no.',
        'size' => 'tiny',
        'not null' => TRUE,
75
        'default' => 0,
76
        'disp-width' => '11'),
77 78 79 80 81
    ),
    'primary key' => array('cid'),
    'indexes' => array(
      'notify_hash' => array('notify_hash')),
  );
82 83 84
  $schema['comment_notify_user_settings'] = array(
    'fields' => array(
        'uid' => array(
85
          'type' => 'int',
86 87 88 89 90 91
          'unsigned' => TRUE,
          'description' => 'The user id from {users}.cid',
          'not null' => TRUE,
          'disp-width' => '11'),
        'node_notify' => array(
          'type' => 'int',
92
          'description' => 'An integer indicating the default type of subscription: 0 means not subscribed, 1 means subscribed to all comments, and 2 means only subscribed to replies of this comment.',
93 94 95 96 97 98
          'size' => 'tiny',
          'not null' => TRUE,
          'default' => 0,
          'disp-width' => '11'),
        'comment_notify' => array(
          'type' => 'int',
99
          'description' => 'An integer indicating the default type of subscription: 0 means not subscribed, 1 means subscribed to all comments, and 2 means only subscribed to replies of this comment.',
100 101 102 103 104 105 106 107
          'size' => 'tiny',
          'not null' => TRUE,
          'default' => 0,
          'disp-width' => '11'),
    ),
    'primary key' => array('uid'),
  );

108
  return $schema;
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
}

/**
 * Head 2 head update for the new size of the hash field.
 */
function comment_notify_update_7002() {
  $new_spec = array(
    'type' => 'varchar',
    'description' => 'A hash of unique information about the commenter.  Used for unsubscribing users.',
    'length' => '128',
    'not null' => TRUE,
    'default' => ''
  );
  $keys = array(
    'indexes' => array('notify_hash' => array('notify_hash')),
  );
  db_drop_index('comment_notify', 'notify_hash');
  db_change_field('comment_notify', 'notify_hash', 'notify_hash', $new_spec, $keys);
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
}

/**
 * Fix tokens in comment subscriber e-mail template.
 */
function comment_notify_update_7003() {
  $variables = array(
    'comment_notify_author_subject' => 'author',
    'comment_notify_node_notify_default_mailtext' => 'author',
    'comment_notify_watcher_subject' => 'commenter',
    'comment_notify_comment_notify_default_mailtext' => 'commenter',
  );
  foreach ($variables as $variable => $context) {
    // If the variable is using the default value, this will return NULL and we
    // can skip it.
    if ($text = variable_get($variable)) {
      $replacements = array(
        '[comment:unsubscribe-url]' => '[comment-subscribed:unsubscribe-url]',
        '[comment:name]' => '[comment:author]',
        '[node:' => '[comment:node:', // Replace all node tokens with comment:node.
      );
      if ($context == 'author') {
        $replacements['[user:name]'] = '[comment:node:author]';
        $replacements['[user:'] = '[comment:node:author:';
      }
      elseif ($context == 'commenter') {
        $replacements['[user:name]'] = '[comment-subscribed:author]';
        $replacements['[user:'] = '[comment-subscribed:author:';
      }
      $text = strtr($text, $replacements);
      variable_set($variable, $text);
    }
  }
  return 'Comment notify token strings updated.';
}
162 163 164 165 166 167 168

/**
 * Fix a missing default causes warnings for Postgresql and some MySQL.
 */
function comment_notify_update_7004() {
  db_change_field('comment_notify', 'notified', 'notified',  array('type' => 'int', 'size' => 'small', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
}