You are here

function spam_update_5300 in Spam 5.3

Upgrade websites that were running old Spam 2.0 modules.

File

./spam.install, line 250

Code

function spam_update_5300() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
    default:
      $tables = array(
        'spam_tracker',
        'spam_log',
        'spam_tokens',
        'spam_custom',
        'spam_reported',
      );
      foreach ($tables as $table) {
        if ($result = db_result(db_query("SHOW TABLES LIKE '%s'", $table))) {
          if ($table == 'spam_log') {
            $ret[] = update_sql('DROP TABLE {' . $table . '}');
          }
          else {
            $ret[] = update_sql('RENAME TABLE {' . $table . '} TO {old_' . $table . '}');
          }
        }
      }

      /**
       * Provides global and granular per-content-type configurations for all
       * enabled spam filters.  The status allows a filter to be enabled or
       * disabled.  The weight allows filters to be ordered.  The gain allows
       * you to minimize or amplify the effect of a given filter.
       */
      $ret[] = update_sql("CREATE TABLE {spam_filters} (\n        fid INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,\n        gid INT(11) UNSIGNED DEFAULT '0' NOT NULL,\n        name VARCHAR(128) NOT NULL DEFAULT '',\n        module VARCHAR(128) NOT NULL DEFAULT '',\n        status TINYINT UNSIGNED DEFAULT '0' NOT NULL,\n        weight TINYINT DEFAULT '0' NOT NULL,\n        gain TINYINT UNSIGNED DEFAULT '0' NOT NULL,\n        PRIMARY KEY fid (fid),\n        KEY gid (gid),\n        KEY name (name),\n        KEY module (module),\n        KEY status (status),\n        KEY weight (weight)\n      ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");
      $ret[] = update_sql("CREATE TABLE {spam_filters_groups} (\n        gid INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,\n        name VARCHAR(255) NOT NULL DEFAULT '',\n        weight TINYINT DEFAULT '0' NOT NULL,\n        PRIMARY KEY gid (gid),\n        KEY weight (weight)\n      ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");
      $ret[] = update_sql("CREATE TABLE {spam_filters_groups_data} (\n        gid INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,\n        content_type VARCHAR(64) NOT NULL DEFAULT '',\n        PRIMARY KEY gid_content_type (gid,content_type),\n        KEY content_type (content_type)\n      ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");

      /**
       * Tracks all filtered site content, included both spam and non-spam.
       */
      $ret[] = update_sql("CREATE TABLE {spam_tracker} (\n        sid INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,\n        content_type VARCHAR(128) NOT NULL DEFAULT '',\n        content_id INT(11) UNSIGNED DEFAULT '0',\n        score INT(4) UNSIGNED DEFAULT '0',\n        hostname VARCHAR(15) NOT NULL DEFAULT '',\n        timestamp INT(11) UNSIGNED DEFAULT '0',\n        PRIMARY KEY sid (sid),\n        UNIQUE KEY content_id_content_type (content_id,content_type),\n        KEY content_type (content_type),\n        KEY score (score),\n        KEY hostname (hostname),\n        KEY timestamp (timestamp)\n      ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");

      /**
       *
       */
      $ret[] = update_sql("CREATE TABLE {spam_filters_errors} (\n        bid INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,\n        uid INT(11) UNSIGNED NOT NULL DEFAULT '0',\n        content_type VARCHAR(128) NOT NULL DEFAULT '',\n        content_id INT(11) UNSIGNED DEFAULT '0',\n        content_hash CHAR(32) NOT NULL DEFAULT '',\n        content TEXT NOT NULL,\n        form TEXT NOT NULL,\n        feedback TEXT NOT NULL,\n        hostname VARCHAR(15) NOT NULL DEFAULT '',\n        timestamp INT(11) UNSIGNED DEFAULT '0',\n        PRIMARY KEY bid (bid),\n        KEY id_type (content_id,content_type),\n        UNIQUE KEY content_hash (content_hash),\n        KEY content_type (content_type),\n        KEY hostname (hostname),\n        KEY timestamp (timestamp)\n      ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");

      /**
       * Logging mechanism similar to watchdog, but provides additional
       * information specific to spam tracking.
       */
      $ret[] = update_sql("CREATE TABLE {spam_log} (\n        lid int(11) UNSIGNED NOT NULL AUTO_INCREMENT,\n        trid int(11) UNSIGNED NOT NULL DEFAULT '0',\n        level int(2) UNSIGNED NOT NULL DEFAULT '0',\n        content_type VARCHAR(128) NULL DEFAULT '',\n        content_id INT(11) UNSIGNED DEFAULT '0',\n        uid int(10) UNSIGNED NOT NULL DEFAULT '0',\n        function varchar(255) NOT NULL DEFAULT '',\n        message varchar(255) NOT NULL DEFAULT '',\n        hostname VARCHAR(15) NOT NULL DEFAULT '',\n        timestamp int(11) UNSIGNED DEFAULT '0',\n        PRIMARY KEY lid (lid),\n        KEY trid (trid),\n        KEY content_type_content_id (content_type, content_id),\n        KEY message (message),\n        KEY uid (uid),\n        KEY hostname (hostname),\n        KEY timestamp (timestamp)\n      ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");

      /**
       * Spam statistics.
       */
      $ret[] = update_sql("CREATE TABLE {spam_statistics} (\n        stid int(11) UNSIGNED NOT NULL AUTO_INCREMENT,\n        name varchar(64) NOT NULL DEFAULT '',\n        count int(11) UNSIGNED NOT NULL DEFAULT '0',\n        timestamp int(11) UNSIGNED DEFAULT '0',\n        PRIMARY KEY stid (stid),\n        UNIQUE KEY name (name)\n      ) TYPE=MyISAM /* 40100 DEFAULT CHARACTER SET utf8 */;");

      // Extract data from old_spam_tracker table, then drop.
      $result = db_query('SELECT * FROM {old_spam_tracker}');
      while ($spam = db_fetch_object($result)) {
        $ret[] = update_sql("INSERT INTO {spam_tracker} (content_type, content_id, score, hostname, timestamp) VALUES('{$spam->source}', {$spam->id}, {$spam->probability}, '{$spam->hostname}', {$spam->timestamp})");
      }
      $ret[] = update_sql('DROP TABLE {old_spam_tracker}');
      spam_skip_update(TRUE);
      break;
  }
  return $ret;
}