You are here

function spam_update_1 in Spam 5

Updates

File

./spam.install, line 179

Code

function spam_update_1() {
  $ret = array();
  if ($GLOBALS['db_type'] == 'mysql') {
    $ret[] = update_sql("ALTER TABLE {spam_tokens} RENAME TO {spam_tokens_old}");
    $ret[] = update_sql("CREATE TABLE {spam_tokens} (\n      tid int(10) unsigned NOT NULL auto_increment,\n      token varchar(255) NOT NULL default '',\n      spam int(10) unsigned default '0',\n      notspam int(10) unsigned default '0',\n      probability int(10) unsigned default '0',\n      last int(11) unsigned default '0',\n      PRIMARY KEY tid (tid),\n      UNIQUE KEY token (token),\n      KEY spam (spam),\n      KEY notspam (notspam),\n      KEY probability (probability),\n      KEY last (last)\n    ) TYPE=MyISAM;");
  }
  else {
    $ret[] = update_sql("ALTER TABLE {spam_tokens} RENAME TO {spam_tokens_old}");
    $ret[] = update_sql("ALTER TABLE {spam_tokens_tid_seq} RENAME TO {spam_tokens_old_tid_seq}");
    $ret[] = update_sql("ALTER TABLE {spam_tokens_pkey} RENAME TO {spam_tokens_old_pkey}");
    $ret[] = update_sql("ALTER TABLE {spam_tokens_token_key} RENAME TO {spam_tokens_old_token_key}");
    $ret[] = update_sql("ALTER TABLE {spam_tokens_spam_key} RENAME TO {spam_tokens_old_spam_key}");
    $ret[] = update_sql("ALTER TABLE {spam_tokens_notspam_key} RENAME TO {spam_tokens_old_notspam_key}");
    $ret[] = update_sql("ALTER TABLE {spam_tokens_probability_key} RENAME TO {spam_tokens_old_probability_key}");
    $ret[] = update_sql("ALTER TABLE {spam_tokens_last_key} RENAME TO {spam_tokens_old_last_key}");
    $ret[] = update_sql("CREATE TABLE {spam_tokens} (\n      tid serial PRIMARY KEY,\n      token varchar(255) UNIQUE NOT NULL DEFAULT '',\n      spam integer default 0,\n      notspam integer default 0,\n      probability integer default 0,\n      last integer default 0 )");
    $ret[] = update_sql("CREATE INDEX {spam_tokens_spam_key} ON {spam_tokens} (spam)");
    $ret[] = update_sql("CREATE INDEX {spam_tokens_notspam_key} ON {spam_tokens} (notspam)");
    $ret[] = update_sql("CREATE INDEX {spam_tokens_probability_key} ON {spam_tokens} (probability)");
    $ret[] = update_sql("CREATE INDEX {spam_tokens_last_key} ON {spam_tokens} (last)");
  }
  $orig = $new = $url = 0;
  $result = db_query('SELECT * FROM {spam_tokens_old}');
  while ($old = db_fetch_object($result)) {
    $orig++;

    // test if URI, if so, just save it
    if (substr($old->token, 0, 4) == 'URL*') {
      $url++;
      $new += spam_save_token($old->token, $old);
    }
    else {
      $token = preg_replace('/^header\\*/', '', $old->token, 1);
      if ($token != $old->token) {
        $header = 'header*';
      }
      else {
        $header = '';
      }
      $tokens = array();
      $sanitized = preg_replace('(http://|https://|ftp://|mailto:)', '', $token);
      $sanitized = preg_replace("'(www\\.)|(</a>)|(href=)|(target=)|(src=)'i", '', $sanitized);
      $sanitized = preg_replace("/[()\\{\\}\\[\\]#.,]/", '', $sanitized);
      $sanitized = strtolower($sanitized);
      $delimiters = " \t\n\r-_<>'\"`/|*%^&+=~:;?";
      $tok = strtok($sanitized, $delimiters);
      while ($tok !== FALSE) {

        // if longer than 255 characters, truncate the token
        $tokens[] = htmlspecialchars(substr("{$header}{$tok}", 0, 254));
        $tok = strtok($delimiters);
      }
      foreach ($tokens as $t) {
        if ($t != '') {
          $new += spam_save_token($t, $old);
        }
      }
    }
  }
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql('DROP TABLE {spam_tokens_old}');
      break;
    case 'pgsql':
      $ret[] = update_sql('DROP TABLE {spam_tokens_old}');
      break;
  }
  print "<p>" . t("Converting %original tokens into %new tokens.", array(
    '%original' => $orig,
    '%new' => $new,
  )) . "</p>";
  return $ret;
}