View source
<?php
function comment_notify_install() {
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
$status[] = db_query("CREATE TABLE {comment_notify} (\n cid int NOT NULL auto_increment,\n notify tinyint unsigned NOT NULL default 0,\n notify_hash varchar(32) NOT NULL default '',\n PRIMARY KEY (cid),\n KEY notify_hash (notify_hash)\n ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
$status[] = db_query("CREATE TABLE {comment_notify_user_settings} (\n uid int unsigned NOT NULL default 0,\n node_notify tinyint unsigned NOT NULL default 0,\n comment_notify tinyint unsigned NOT NULL default 0,\n PRIMARY KEY (uid)\n ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
$status[] = 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");
break;
case 'pgsql':
$status[] = db_query("CREATE TABLE {comment_notify} (\n cid integer NOT NULL,\n notify smallint_unsigned NOT NULL default '0',\n notify_hash varchar(32) NOT NULL default '',\n PRIMARY KEY (cid)\n )");
$status[] = db_query("CREATE TABLE {comment_notify_user_settings} (\n uid serial CHECK (uid >= 0),\n node_notify smallint_unsigned NOT NULL default '0',\n comment_notify smallint_unsigned NOT NULL default '0',\n PRIMARY KEY (uid)\n )");
$status[] = db_query("CREATE INDEX {comment_notify}_notify_hash_idx ON {comment_notify} (notify_hash)");
$status[] = 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");
break;
}
$status[] = db_query("UPDATE {system} SET weight = 10 WHERE name = 'comment_notify'");
if (array_search(FALSE, $status) !== FALSE) {
drupal_set_message(t('Database modifications for the comment_notify module were unsuccessful. The modifications may need to be made by hand.'), 'error');
}
}
function comment_notify_uninstall() {
if (db_table_exists('comment_notify')) {
db_query("DROP TABLE {comment_notify}");
}
if (db_table_exists('comment_notify_user_settings')) {
db_query("DROP TABLE {comment_notify_user_settings}");
}
variable_del('node_notify_default_mailtext');
db_query("DELETE FROM {variable} WHERE name LIKE 'comment_notify_%'");
}
function comment_notify_update_1() {
comment_notify_install();
}
function comment_notify_update_2() {
$result = db_query("UPDATE {system} SET weight = 10 WHERE name = 'comment_notify'");
drupal_set_message($results);
if ($result) {
drupal_set_message(t('comment_notify module weight config update successfully.'));
}
else {
drupal_set_message(t('comment_notify module weight config update unsuccessful.'), 'error');
}
$ret[] = $result;
return $ret;
}
function comment_notify_update_3() {
$result = db_query("UPDATE {variable} SET value = replace(value, '%', '!') WHERE name = 'comment_notify_default_mailtext'");
drupal_set_message($results);
if ($result) {
drupal_set_message(t('comment_notify mail text migration successful.'));
}
else {
drupal_set_message(t('comment_notify mail text migration unsuccessful.'), 'error');
}
$ret[] = $result;
return $ret;
}
function comment_notify_update_4() {
$ret = array();
$ret[] = update_sql("ALTER TABLE {comments} MODIFY notify tinyint(1) NOT NULL DEFAULT '0'");
return $ret;
}
function comment_notify_update_5() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
db_query("CREATE TABLE {comment_notify} (\n cid int NOT NULL auto_increment,\n notify tinyint unsigned NOT NULL default 0,\n notify_hash varchar(32) NOT NULL default '',\n PRIMARY KEY (cid),\n KEY notify_hash (notify_hash)\n ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
$ret[] = update_sql("INSERT INTO {comment_notify} (cid, notify, notify_hash) SELECT c.cid, c.notify, md5(concat(c.mail, ifnull(u.mail, u.init), c.uid, c.name, c.nid)) FROM {comments} c INNER JOIN {users} u on c.uid = u.uid");
break;
case 'pgsql':
db_query("CREATE TABLE {comment_notify} (\n cid serial,\n notify smallint_unsigned NOT NULL default '0',\n notify_hash varchar(32) NOT NULL default '',\n PRIMARY KEY (cid)\n )");
db_query("CREATE INDEX {comment_notify}_notify_hash_idx ON {comment_notify} (notify_hash)");
$ret[] = update_sql("INSERT INTO {comment_notify} (cid, notify, notify_hash) SELECT c.cid, c.notify, md5(c.mail || COALESCE(u.mail, u.init) || c.uid || c.name || c.nid) FROM {comments} c INNER JOIN {users} u on c.uid = u.uid");
break;
}
return $ret;
}
function comment_notify_update_5000() {
$return[] = update_sql("DELETE FROM {comment_notify} WHERE cid NOT IN (SELECT cid FROM {comments})");
return $return;
}
function comment_notify_update_5200() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
if (db_fetch_object(db_query("SHOW COLUMNS FROM {comments} LIKE 'notify'"))) {
$ret[] = update_sql('ALTER TABLE {comments} DROP COLUMN notify');
}
break;
case 'pgsql':
if (db_result(db_query("SELECT COUNT(pg_attribute.attname) FROM pg_class, pg_attribute WHERE pg_attribute.attrelid = pg_class.oid AND pg_class.relname = '{comments}' AND attname = 'notify'"))) {
$ret[] = update_sql('ALTER TABLE {comments} DROP COLUMN notify');
}
break;
}
return $ret;
}
function comment_notify_update_5201() {
$ret = array();
$num_users_per_batch = 100;
if (!isset($_SESSION['comment_notify_update_5202'])) {
$_SESSION['comment_notify_update_5202'] = 1;
$_SESSION['comment_notify_update_5202_max'] = db_result(db_query("SELECT MAX(uid) FROM {users}"));
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
$ret[] = update_sql("CREATE TABLE {comment_notify_user_settings} (\n uid int unsigned NOT NULL default 0,\n node_notify tinyint unsigned NOT NULL default 0,\n comment_notify tinyint unsigned NOT NULL default 0,\n PRIMARY KEY (uid)\n ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
break;
case 'pgsql':
$ret[] = update_sql("CREATE TABLE {comment_notify_user_settings} (\n uid serial CHECK (uid >= 0),\n node_notify smallint_unsigned NOT NULL default '0',\n comment_notify smallint_unsigned NOT NULL default '0',\n PRIMARY KEY (uid)\n )");
break;
}
}
$next = min($_SESSION['comment_notify_update_5202'] + $num_users_per_batch, $_SESSION['comment_notify_update_5202_max']);
if (!db_table_exists('comment_notify_user_settings')) {
unset($_SESSION['comment_notify_update_5202']);
unset($_SESSION['comment_notify_update_5202_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_5202'];
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_5202'] = $next;
if ($_SESSION['comment_notify_update_5202'] == $_SESSION['comment_notify_update_5202_max']) {
unset($_SESSION['comment_notify_update_5202']);
unset($_SESSION['comment_notify_update_5202_max']);
$ret[] = array(
'success' => TRUE,
'query' => t('Moved comment_notify user settings data from the {users} table into the {comment_notify_user_settings} table.'),
);
}
else {
$ret['#finished'] = $_SESSION['comment_notify_update_5202'] / $_SESSION['comment_notify_update_5202_max'];
}
return $ret;
}