You are here

function comment_notify_update_6003 in Comment Notify 6

Create a new table to store user preferences and move the $user->data there.

File

./comment_notify.install, line 146
comment_notify.install.

Code

function comment_notify_update_6003() {
  $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_6003'])) {

    // We need to start at uid 1, so initialize our variable
    // to the value below that.
    $_SESSION['comment_notify_update_6003'] = 1;
    $_SESSION['comment_notify_update_6003_max'] = db_result(db_query("SELECT MAX(uid) FROM {users}"));

    // Create the table.
    $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']);
  }

  // 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_6003'] + $num_users_per_batch, $_SESSION['comment_notify_update_6003_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_6003']);
    unset($_SESSION['comment_notify_update_6003_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_6003'];
  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_6003'] = $next;
  if ($_SESSION['comment_notify_update_6003'] == $_SESSION['comment_notify_update_6003_max']) {

    // We're done, clear these out.
    unset($_SESSION['comment_notify_update_6003']);
    unset($_SESSION['comment_notify_update_6003_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.'),
    );
  }
  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 {

    // Report how much is left to complete.
    $ret['#finished'] = $_SESSION['comment_notify_update_6003'] / $_SESSION['comment_notify_update_6003_max'];
  }
  return $ret;
}