View source
<?php
function comment_notify_install() {
drupal_install_schema('comment_notify');
if ($GLOBALS['db_type'] == 'pgsql') {
db_query("INSERT INTO {comment_notify} (cid, notify, notify_hash) SELECT c.cid, 0, md5(c.mail || coalesce(u.mail, u.init) || c.uid || c.name || c.nid) FROM {comments} c LEFT OUTER JOIN {users} u on c.uid = u.uid");
}
else {
db_query("INSERT INTO {comment_notify} (cid, notify, notify_hash) SELECT c.cid, 0, md5(concat(c.mail, ifnull(u.mail, u.init), c.uid, c.name, c.nid)) FROM {comments} c LEFT OUTER JOIN {users} u on c.uid = u.uid");
}
db_query("UPDATE {system} SET weight = 10 WHERE name = 'comment_notify'");
}
function comment_notify_uninstall() {
drupal_uninstall_schema('comment_notify');
variable_del('node_notify_default_mailtext');
db_query("DELETE FROM {variable} WHERE name LIKE 'comment_notify_%'");
}
function comment_notify_schema() {
$schema['comment_notify'] = array(
'description' => 'Stores information about which commenters on the site have subscriped to followup emails.',
'fields' => array(
'cid' => array(
'type' => 'int',
'unsigned' => TRUE,
'description' => 'The comment id from {comments}.cid',
'not null' => TRUE,
'disp-width' => '11',
),
'notify' => array(
'type' => 'int',
'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.',
'size' => 'tiny',
'not null' => TRUE,
'disp-width' => '11',
),
'notify_hash' => array(
'type' => 'varchar',
'description' => 'An md5 hash of unique information about the commenter. Used for unsubscribing users.',
'length' => '32',
'not null' => TRUE,
'default' => '',
),
'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,
'default' => 0,
'disp-width' => '11',
),
),
'primary key' => array(
'cid',
),
'indexes' => array(
'notify_hash' => array(
'notify_hash',
),
),
);
$schema['comment_notify_user_settings'] = array(
'fields' => array(
'uid' => array(
'type' => 'int',
'unsigned' => TRUE,
'description' => 'The user id from {users}.cid',
'not null' => TRUE,
'disp-width' => '11',
),
'node_notify' => array(
'type' => 'int',
'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.',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
'disp-width' => '11',
),
'comment_notify' => array(
'type' => 'int',
'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.',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
'disp-width' => '11',
),
),
'primary key' => array(
'uid',
),
);
return $schema;
}
function comment_notify_update_6000() {
$return[] = update_sql("DELETE FROM {comment_notify} WHERE cid NOT IN (SELECT cid FROM {comments})");
return $return;
}
function comment_notify_update_6001() {
$ret = array();
$result = db_query("SELECT pid, perm FROM {permission}");
while ($row = db_fetch_object($result)) {
$permissions = explode(', ', $row->perm);
if (in_array('Subscribe to comments', $permissions) && !in_array('subscribe to comments', $permissions)) {
$permissions[] = 'subscribe to comments';
}
if (in_array('Administer comment notify', $permissions) && !in_array('administer comment notify', $permissions)) {
$permissions[] = 'administer comment notify';
}
$permissions = implode(', ', $permissions);
$ret[] = update_sql("UPDATE {permission} SET perm = '%s' WHERE pid = %d", $permissions, $row->pid);
}
return $ret;
}
function comment_notify_update_6002() {
$ret = array();
if (db_column_exists('comments', 'notify')) {
db_drop_field($ret, 'comments', 'notify');
}
return $ret;
}
function comment_notify_update_6003() {
$ret = array();
$num_users_per_batch = 100;
if (!isset($_SESSION['comment_notify_update_6003'])) {
$_SESSION['comment_notify_update_6003'] = 1;
$_SESSION['comment_notify_update_6003_max'] = db_result(db_query("SELECT MAX(uid) FROM {users}"));
$schema['comment_notify_user_settings'] = array(
'fields' => array(
'uid' => array(
'type' => 'serial',
'unsigned' => TRUE,
'description' => 'The user id from {users}.cid',
'not null' => TRUE,
'disp-width' => '11',
),
'node_notify' => array(
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
'disp-width' => '11',
),
'comment_notify' => array(
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
'disp-width' => '11',
),
),
'primary key' => array(
'uid',
),
);
db_create_table($ret, 'comment_notify_user_settings', $schema['comment_notify_user_settings']);
}
$next = min($_SESSION['comment_notify_update_6003'] + $num_users_per_batch, $_SESSION['comment_notify_update_6003_max']);
if (!db_table_exists('comment_notify_user_settings')) {
unset($_SESSION['comment_notify_update_6003']);
unset($_SESSION['comment_notify_update_6003_max']);
$ret[] = array(
'success' => FALSE,
'query' => t('For some reason the {comment_notify_user_settings} table was not properly created, and so per-user comment_notify settings could not be copied from {users}.data. You will need to run this update again.'),
);
return $ret;
}
$uid = $_SESSION['comment_notify_update_6003'];
while ($uid <= $next) {
$data = db_result(db_query('SELECT data FROM {users} WHERE uid = %d', $uid));
$settings = array(
'uid' => $uid,
);
if (!empty($data)) {
$data = unserialize($data);
if (isset($data['node_notify_mailalert'])) {
$settings['node_notify'] = $data['node_notify_mailalert'];
unset($data['node_notify_mailalert']);
}
if (isset($data['comment_notify_mailalert'])) {
$settings['comment_notify'] = $data['comment_notify_mailalert'];
unset($data['comment_notify_mailalert']);
}
$fields_sql = '';
$values_sql = '';
foreach ($settings as $field => $value) {
$fields_sql .= "{$field}, ";
$values_sql .= '%d, ';
}
$fields_sql = rtrim($fields_sql, ', ');
$values_sql = rtrim($values_sql, ', ');
if (count($settings) > 1) {
db_query("INSERT INTO {comment_notify_user_settings} ({$fields_sql}) VALUES ({$values_sql})", $settings);
db_query("UPDATE {users} SET data = '%s' WHERE uid = %d", serialize($data), $uid);
}
}
$uid++;
}
$_SESSION['comment_notify_update_6003'] = $next;
if ($_SESSION['comment_notify_update_6003'] == $_SESSION['comment_notify_update_6003_max']) {
unset($_SESSION['comment_notify_update_6003']);
unset($_SESSION['comment_notify_update_6003_max']);
$ret[] = array(
'success' => TRUE,
'query' => t('Moved comment_notify user settings data from the {users} table into the {comment_notify_user_settings} table.'),
);
}
elseif ($uid == $next) {
unset($_SESSION['comment_notify_update_6003']);
unset($_SESSION['comment_notify_update_6003_max']);
$ret[] = array(
'success' => FALSE,
'query' => t('Something is maybe not right.'),
);
}
else {
$ret['#finished'] = $_SESSION['comment_notify_update_6003'] / $_SESSION['comment_notify_update_6003_max'];
}
return $ret;
}
function comment_notify_update_6004() {
$ret = array();
db_add_field($ret, 'comment_notify', 'notified', array(
'type' => 'int',
'size' => 'small',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
));
$ret[] = update_sql('UPDATE {comment_notify} SET notified = 1');
return $ret;
}
function comment_notify_update_6005() {
$ret = array();
db_change_field($ret, 'comment_notify', 'cid', 'cid', array(
'type' => 'int',
'unsigned' => TRUE,
'description' => 'The comment id from {comments}.cid',
'not null' => TRUE,
'disp-width' => '11',
));
db_change_field($ret, 'comment_notify_user_settings', 'uid', 'uid', array(
'type' => 'int',
'unsigned' => TRUE,
'description' => 'The user id from {users}.cid',
'not null' => TRUE,
'disp-width' => '11',
));
return $ret;
}
function comment_notify_update_6006() {
$ret = array();
db_change_field($ret, 'comment_notify', 'notified', 'notified', array(
'type' => 'int',
'size' => 'small',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
));
return $ret;
}