You are here

comment_notify.install in Comment Notify 5.2

File

comment_notify.install
View source
<?php

/**
 * @file
 * comment_notify.install.
 */

/**
 * Implementation of hook_install().
 */
function comment_notify_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':

      // Create a table to hold the data.
      $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 */ ");

      // Create a table to store the per-user notification settings.
      $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 */ ");

      // Insert a record for each existing comment.
      $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':

      // Do the same for PostgreSQL.
      $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;
  }

  // Set module weight low so that other modules act on the comment first.
  $status[] = db_query("UPDATE {system} SET weight = 10 WHERE name = 'comment_notify'");

  // If there is one FALSE value in the status array, there was an error.
  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_%'");
}

// Table creation called in update_1 to allow for the case
// where a user is upgrading from a previous version of
// the comment_notify module that did not use tables.
function comment_notify_update_1() {
  comment_notify_install();
}

// Table creation called in update_1 to allow for the case
function comment_notify_update_2() {

  // Set module weight for my module
  $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;
}

// modify default mailtext to contain ! instead of % for placeholders
function comment_notify_update_3() {

  // Set module weight for my module
  $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;
}

/*
 * Set the default to 0 for upgrades so users must opt in to get emails.
 */
function comment_notify_update_4() {
  $ret = array();
  $ret[] = update_sql("ALTER TABLE {comments} MODIFY notify tinyint(1) NOT NULL DEFAULT '0'");
  return $ret;
}

/*
 * Break out to our own table.
 */
function comment_notify_update_5() {
  $ret = array();

  // Create the new tables...
  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 */ ");

      // Move the data over there from the previous location.
      $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)");

      // Move the data over there from the previous location.
      $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;
}

/**
 * Sync up the two tables after as part of http://drupal.org/node/297791
 */
function comment_notify_update_5000() {
  $return[] = update_sql("DELETE FROM {comment_notify} WHERE cid NOT IN (SELECT cid FROM {comments})");
  return $return;
}

/**
 * Drop the notify column from the {comments} table.  This column will only exist
 * if the 5.x-1.x version of comment_notify was installed at some point.  Since
 * the 5.x-2.x version of the module {comment_notify}.notify has been used instead.
 */
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;
}

/**
 * Copy settings from {users}.data into {comment_notify_user_settings}.
 */
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;
}

Functions

Namesort descending Description
comment_notify_install Implementation of hook_install().
comment_notify_uninstall
comment_notify_update_1
comment_notify_update_2
comment_notify_update_3
comment_notify_update_4
comment_notify_update_5
comment_notify_update_5000 Sync up the two tables after as part of http://drupal.org/node/297791
comment_notify_update_5200 Drop the notify column from the {comments} table. This column will only exist if the 5.x-1.x version of comment_notify was installed at some point. Since the 5.x-2.x version of the module {comment_notify}.notify has been used instead.
comment_notify_update_5201 Copy settings from {users}.data into {comment_notify_user_settings}.