comment_notify.install 4.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php
// $Id$

/**
 * Implementation of hook_install().
 */
function comment_notify_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
11 12 13 14 15 16 17 18 19 20
      // Create a table to hold the data.
      $status[] = db_query("CREATE TABLE {comment_notify} (
        cid int NOT NULL auto_increment,
        notify tinyint unsigned NOT NULL default 0,
        notify_hash varchar(32) NOT NULL default '',
        PRIMARY KEY (cid),
        KEY notify_hash (notify_hash)
      ) /*!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");
21 22
      break;
    case 'pgsql':
23 24 25 26 27 28 29 30 31
      // Do the same for PostgreSQL.
      $status[] = db_query("CREATE TABLE {comments} (
        cid integer NOT NULL,
        notify smallint_unsigned NOT NULL default '0',
        notify_hash varchar(32) NOT NULL default '',
        PRIMARY KEY (cid)
      )");
      $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(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");
32 33 34
      break;
  }
  
35
   // Set module weight low so that other modules act on the comment first.
36 37 38 39
  $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) {
40
    drupal_set_message(t('Database modifications for the comment_notify module were unsuccessful. The modifications may need to be made by hand.'), 'error');
41
  }
42 43
}

44
function comment_notify_uninstall() {
45 46
  if (db_table_exists('comment_notify')) {
    db_query("DROP TABLE {comment_notify}");
47 48 49 50 51 52 53
  }
}

// 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() {
54
  comment_notify_install();
55 56 57 58 59 60
}

// 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'");
61
  drupal_set_message($results);
62 63 64 65 66 67 68 69

  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');
  }
  
70
  $ret[] = $result;
71 72 73
  return $ret;
}

74 75 76 77 78 79 80 81 82 83 84 85
// 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');
  }
86

87 88 89
  $ret[]=$result;
  return $ret;
}
90

91 92 93
/*
 * Set the default to 0 for upgrades so users must opt in to get emails.
 */
94
function comment_notify_update_4() {
95 96 97
  $ret = array();
  $ret[] = update_sql("ALTER TABLE {comments} MODIFY notify tinyint(1) NOT NULL DEFAULT '0'");
  return $ret;
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
}

/*
 * Break out to our own table.
 */
function comment_notify_update_5() {
  // Create the new tables...
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {comment_notify} (
        cid int NOT NULL auto_increment,
        notify tinyint unsigned NOT NULL default 0,
        notify_hash varchar(32) NOT NULL default '',
        PRIMARY KEY (cid),
        KEY notify_hash (notify_hash)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
      break;
    case 'pgsql':
      db_query("CREATE TABLE {comments} (
        cid serial,
        notify smallint_unsigned NOT NULL default '0',
        notify_hash varchar(32) NOT NULL default '',
        PRIMARY KEY (cid)
      )");
      db_query("CREATE INDEX {comment_notify}_notify_hash_idx ON {comment_notify} (notify_hash)");
      break;
  }

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

  // TODO alter the comments table to remove my column?
133
}