You are here

privatemsg.install in Privatemsg 5

File

privatemsg.install
View source
<?php

function privatemsg_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {privatemsg} (\n          id            int unsigned NOT NULL primary key,\n          author        int unsigned NOT NULL,\n          recipient     int unsigned NOT NULL,\n          subject       varchar(255) NOT NULL,\n          message       text NOT NULL,\n          timestamp     int unsigned NOT NULL,\n          newmsg        tinyint unsigned NOT NULL,\n          hostname      varchar(255) NOT NULL,\n          folder        int unsigned NOT NULL DEFAULT 0,\n          author_del    tinyint unsigned NOT NULL DEFAULT 0,\n          recipient_del tinyint unsigned NOT NULL DEFAULT 0,\n          format        int NOT NULL DEFAULT 0,\n          thread        int NOT NULL DEFAULT 0,\n          type          varchar(255) NOT NULL,\n          variables     longtext,\n          key (recipient),\n          key (folder),\n          key(type),\n          key(author)\n        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      db_query("CREATE TABLE {privatemsg_folder} (\n          fid           int unsigned NOT NULL primary key,\n          uid           int unsigned NOT NULL,\n          name          varchar(255) NOT NULL\n        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      db_query("CREATE TABLE {privatemsg_archive} (\n          id            int unsigned NOT NULL primary key,\n          author        int unsigned NOT NULL,\n          recipient     int unsigned NOT NULL,\n          subject       VARCHAR(64) NOT NULL,\n          message       text NOT NULL,\n          timestamp     int unsigned NOT NULL,\n          hostname      varchar(255) NOT NULL,\n          folder        int unsigned NOT NULL,\n          format        int NOT NULL DEFAULT 0,\n          thread        int NOT NULL DEFAULT 0,\n          key (recipient)\n        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      db_query('CREATE TABLE {privatemsg_block_user} (
          author int unsigned NOT NULL,
          recipient int unsigned NOT NULL,
          PRIMARY KEY (author, recipient)
        )  /*!40100 DEFAULT CHARACTER SET utf8 */');
      break;

    /*
                    notification_subject varchar(255) NOT NULL,
              notification_text_body varchar(255) NOT NULL,
              notification_html_body varchar(255) NOT NULL,
              notification_group_text_body varchar(255) NOT NULL,
              notification_group_html_body varchar(255) NOT NULL,
    */
    case 'pgsql':
      db_query("CREATE TABLE {privatemsg} (\n          id            integer NOT NULL,\n          author        integer NOT NULL,\n          recipient     integer NOT NULL,\n          subject       varchar(255) NOT NULL,\n          message       text NOT NULL,\n          timestamp     integer NOT NULL,\n          newmsg        smallint NOT NULL,\n          hostname      varchar(255) NOT NULL,\n          format        smallint NOT NULL DEFAULT 0,\n          folder        integer NOT NULL DEFAULT 0,\n          author_del    smallint NOT NULL DEFAULT 0,\n          recipient_del smallint NOT NULL DEFAULT 0,\n          thread        int NOT NULL DEFAULT 0,\n          type          varchar(255) NOT NULL,\n          variables     text,\n          PRIMARY KEY (id)\n        )");
      db_query("CREATE INDEX {privatemsg_folder_index} ON {privatemsg}(folder)");
      db_query("CREATE INDEX {privatemsg_folder_recipient} ON {privatemsg}(recipient)");
      db_query("CREATE INDEX {privatemsg_folder_type} ON {privatemsg}(type)");
      db_query("CREATE INDEX {privatemsg_folder_author} ON {privatemsg}(author)");
      db_query("CREATE TABLE {privatemsg_folder} (\n          fid           integer NOT NULL,\n          uid           integer NOT NULL,\n          name          varchar(255) not null,\n          PRIMARY KEY (fid)\n        )");
      db_query("CREATE TABLE {privatemsg_archive} (\n          id            integer NOT NULL,\n          author        integer NOT NULL,\n          recipient     integer NOT NULL,\n          subject       varchar(64) NOT NULL,\n          message       text NOT NULL,\n          timestamp     integer NOT NULL,\n          hostname      varchar(255) NOT NULL,\n          format        smallint NOT NULL DEFAULT 0,\n          folder        integer NOT NULL,\n          thread        int NOT NULL DEFAULT 0,\n          PRIMARY KEY (id)\n        )");
      db_query("CREATE INDEX {privatemsg_archive_recipient} ON {privatemsg_archive}(recipient)");
      db_query('CREATE SEQUENCE {privatemsg}_id_seq INCREMENT 1 START 1');
      db_query('CREATE SEQUENCE {privatemsg_folder}_fid_seq INCREMENT 1 START 1');
      db_query('CREATE TABLE {privatemsg_block_user} (
          author int unsigned NOT NULL,
          recipient int unsigned NOT NULL,
          PRIMARY KEY (author, recipient)
        )');
      db_query("create or replace function unix_timestamp(timestamp with time zone)\n        returns int as '\n        declare\n           date alias for \$1;\n           timezero timestamp;\n           offset interval;\n        begin\n           timezero := timestamp ''1970-1-1 00:00'' at time zone ''utc'';\n           offset := date-timezero;\n\n           return (extract(''days'' from offset)*86400+\n                   extract(''hours'' from offset)*3600+\n                   extract(''minutes'' from offset)*60+\n                   extract(''seconds'' from offset))::int;\n        end;\n        ' language 'plpgsql'");
      db_query("create or replace function unix_timestamp(timestamp without time zone)\n        returns int as '\n        declare\n           date alias for \$1;\n           timezero timestamp;\n           offset interval;\n        begin\n           timezero := timestamp ''1970-1-1 00:00'' at time zone ''utc'';\n           offset := date-timezero;\n\n           return (extract(''days'' from offset)*86400+\n                   extract(''hours'' from offset)*3600+\n                   extract(''minutes'' from offset)*60+\n                   extract(''seconds'' from offset))::int;\n        end;\n        ' language 'plpgsql'");
      break;
  }

  // Sent messages folder
  db_query("INSERT INTO {privatemsg_folder} (fid, uid, name) VALUES (1, 0, 'Sent')");
  do {
    $i = db_next_id('{privatemsg_folder}_fid');
  } while ($i < 1);

  // In case this api ever changes to start at zero..
}
function privatemsg_uninstall() {
  db_query("DROP TABLE {privatemsg}");
  db_query("DROP TABLE {privatemsg_folder}");
  db_query("DROP TABLE {privatemsg_archive}");
  if (db_table_exists('privatemsg_block_user')) {
    db_query("DROP TABLE {privatemsg_block_user}");
  }

  // The following two tables are now a part of the pm_subscriptions module,
  // but have been retained to handle updated installs that do not use the
  // pm_subscriptions module.
  if (db_table_exists('privatemsg_mails')) {
    db_query("DROP TABLE {privatemsg_mails}");
  }
  if (db_table_exists('privatemsg_mail_edit')) {
    db_query("DROP TABLE {privatemsg_mail_edit}");
  }
  db_query("DELETE FROM {variable} WHERE name LIKE 'privatemsg_%'");
  cache_clear_all('variables', 'cache');
}

/* Upgrade on mysql from versions before 22-May-2003:
   Create privatemsg_archive/privatemsg_folder tables and insert one row, shown above^
   ALTER TABLE privatemsg ADD folder int unsigned NOT NULL;
   ALTER TABLE privatemsg ADD author_del tinyint unsigned NOT NULL;
   ALTER TABLE privatemsg ADD recipient_del tinyint unsigned NOT NULL;
   ALTER TABLE privatemsg ADD INDEX(folder);
   ALTER TABLE privatemsg CHANGE hostname hostname varchar(255) NOT NULL;
   Continue with steps below, but skip ALTER line for privatemsg_archive..
 *
 * Upgrade on mysql from versions before 29-Apr-2005:
   ALTER TABLE privatemsg CHANGE new newmsg tinyint UNSIGNED NOT NULL;
   ALTER TABLE privatemsg ADD format int NOT NULL DEFAULT '0';
   ALTER TABLE privatemsg_archive ADD format int NOT NULL DEFAULT '0';
   UPDATE privatemsg SET format=1;
   UPDATE privatemsg_archive SET format=1;
 */
function privatemsg_update_1() {
  return _system_update_utf8(array(
    'privatemsg',
    'privatemsg_archive',
    'privatemsg_folder',
  ));
}
function privatemsg_update_2() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $sql = 'ALTER TABLE {%s} MODIFY %s int unsigned NOT NULL';
      $seq = "INSERT INTO {sequences} (name, id) VALUES ('%s', %d)";
      break;
    case 'pgsql':
      $sql = 'ALTER TABLE {%s} ALTER COLUMN %s SET DEFAULT NULL';
      $seq = 'CREATE SEQUENCE %s_seq INCREMENT 1 START %d';
      break;
    default:
      return $ret;
  }
  foreach (array(
    'privatemsg' => 'id',
    'privatemsg_folder' => 'fid',
  ) as $table => $id) {
    $ret[] = update_sql(sprintf($sql, $table, $id));
    $max = db_result(db_query('SELECT max(' . $id . ') FROM {' . $table . '}'));
    if ($table == 'privatemsg') {
      $max = max($max, db_result(db_query('SELECT max(id) FROM {privatemsg_archive}')));
    }
    $ret[] = update_sql(sprintf($seq, '{' . $table . '}_' . $id, $max));
  }
  $ret[] = update_sql(sprintf($sql, 'privatemsg_archive', 'id'));
  return $ret;
}
function privatemsg_update_3() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql('ALTER TABLE {privatemsg} ADD thread int NOT NULL');
      $ret[] = update_sql('ALTER TABLE {privatemsg_archive} ADD thread int NOT NULL');
      break;
    case 'pgsql':
      foreach (array(
        'privatemsg',
        'privatemsg_archive',
      ) as $table) {
        $ret[] = update_sql("ALTER TABLE {$table} ADD thread int");
        $ret[] = update_sql("ALTER TABLE {$table} ALTER COLUMN thread SET DEFAULT 0");
        $ret[] = update_sql("UPDATE {$table} SET thread = 0");
        $ret[] = update_sql("ALTER TABLE {$table} ALTER COLUMN thread SET NOT NULL");
      }
      break;
  }
  return $ret;
}
function privatemsg_update_4() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {privatemsg} ADD type varchar(255) NOT NULL default '', ADD KEY (type)");
      $ret[] = update_sql("ALTER TABLE {privatemsg} CHANGE subject subject varchar(255) NOT NULL default ''");
      if (!db_table_exists('privatemsg_block_user')) {
        $ret[] = update_sql('CREATE TABLE {privatemsg_block_user} (
            author int unsigned NOT NULL,
            recipient int unsigned NOT NULL,
            PRIMARY KEY (author, recipient)
          )  /*!40100 DEFAULT CHARACTER SET utf8 */');
      }
      break;
    case 'pgsql':
      db_add_column($ret, 'privatemsg', 'type', 'varchar(255)', array(
        'not null' => TRUE,
        'default' => "''",
      ));
      $ret[] = update_sql("CREATE INDEX {privatemsg_folder_type} ON {privatemsg}(type)");
      db_change_column($ret, 'privatemsg', 'subject', 'subject', 'varchar(255)', array(
        'not null' => TRUE,
        'default' => "''",
      ));
      if (!db_table_exists('privatemsg_block_user')) {
        $ret[] = update_sql('CREATE TABLE {privatemsg_block_user} (
            author int unsigned NOT NULL,
            recipient int unsigned NOT NULL,
            PRIMARY KEY (author, recipient)
          )');
      }
      break;
  }
  return $ret;
}

/**
 * Add variables column for use by the subscriptions module.
 */
function privatemsg_update_5() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {privatemsg} ADD variables longtext");
      break;
    case 'pgsql':
      db_add_column($ret, 'privatemsg', 'variables', 'text');
      break;
  }
  return $ret;
}