function comment_notify_update_5201 in Comment Notify 5.2
Copy settings from {users}.data into {comment_notify_user_settings}.
File
- ./
comment_notify.install, line 194 - comment_notify.install.
Code
function comment_notify_update_5201() {
$ret = array();
// This determines how many users will be processed in each batch run.
$num_users_per_batch = 100;
// Multi-part update.
if (!isset($_SESSION['comment_notify_update_5202'])) {
// We need to start at uid 1, so initialize our variable
// to the value below that.
$_SESSION['comment_notify_update_5202'] = 1;
$_SESSION['comment_notify_update_5202_max'] = db_result(db_query("SELECT MAX(uid) FROM {users}"));
// Create the new table. This will only happen in the first batch of this
// function.
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;
}
}
// Do the next batch of the deed.
// Find the next N records to update, or do the final batch.
$next = min($_SESSION['comment_notify_update_5202'] + $num_users_per_batch, $_SESSION['comment_notify_update_5202_max']);
// Check to make sure that the {comment_notify_user_settings} table exists.
// If for some reason it was not created above, we might lose data when
// we delete the comment_notify data that is currently in {users}.data.
// If the table doesn't exist, then alert the user and don't allow any
// more batches to be processed.
if (!db_table_exists('comment_notify_user_settings')) {
unset($_SESSION['comment_notify_update_5202']);
unset($_SESSION['comment_notify_update_5202_max']);
// Alert the user that there was an error.
$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;
}
// Transfer the data in our specified range of uid values.
$uid = $_SESSION['comment_notify_update_5202'];
while ($uid < $next) {
// Get the value of {users}.data.
$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, ';
}
// Trim off any trailing commas and spaces.
$fields_sql = rtrim($fields_sql, ', ');
$values_sql = rtrim($values_sql, ', ');
// Add this user and settings to {comment_notify_user_settings} only if
// at least one setting was found in {users}.data for this user.
if (count($settings) > 1) {
db_query("INSERT INTO {comment_notify_user_settings} ({$fields_sql}) VALUES ({$values_sql})", $settings);
// Remove this comment_notify data from {users}.data.
db_query("UPDATE {users} SET data = '%s' WHERE uid = %d", serialize($data), $uid);
}
}
$uid++;
}
// Remember where we left off.
$_SESSION['comment_notify_update_5202'] = $next;
if ($_SESSION['comment_notify_update_5202'] == $_SESSION['comment_notify_update_5202_max']) {
// We're done, clear these out.
unset($_SESSION['comment_notify_update_5202']);
unset($_SESSION['comment_notify_update_5202_max']);
// Provide an explaination of what we did.
$ret[] = array(
'success' => TRUE,
'query' => t('Moved comment_notify user settings data from the {users} table into the {comment_notify_user_settings} table.'),
);
}
else {
// Report how much is left to complete.
$ret['#finished'] = $_SESSION['comment_notify_update_5202'] / $_SESSION['comment_notify_update_5202_max'];
}
return $ret;
}