Skip to content
privatemsg.install 16.3 KiB
Newer Older
// $Id$

/**
 * @file
 * Install file for privatemsg.module
 */

litwol's avatar
litwol committed
function privatemsg_schema() {
  $schema = array();
  $schema['pm_index'] = array(
    'description'       => '{pm_index} holds indexing information about messages and recepients for fast retrieval',
litwol's avatar
litwol committed
    'fields'        => array(
litwol's avatar
litwol committed
      'mid'    => array(
        'description'   => 'Private Message ID',
litwol's avatar
litwol committed
        'type'          => 'int',
        'not null'      => TRUE,
        'unsigned'      => TRUE,
      ),
      'thread_id'     => array(
        'description'   => 'Messages thread ID',
litwol's avatar
litwol committed
        'type'          => 'int',
        'not null'      => TRUE,
        'unsigned'      => TRUE,
      ),
litwol's avatar
litwol committed
      'uid'     => array(
        'description'   => 'UID of either the author or the recipient',
litwol's avatar
litwol committed
        'type'          => 'int',
        'not null'      => TRUE,
        'unsigned'      => TRUE,
      ),
litwol's avatar
litwol committed
      'is_new'     => array(
        'description'   => 'Whether the user has read this message',
litwol's avatar
litwol committed
        'type'          => 'int',
litwol's avatar
litwol committed
        'default'       => 1,
litwol's avatar
litwol committed
        'not null'      => TRUE,
        'unsigned'      => TRUE,
      ),
litwol's avatar
litwol committed
      'deleted' => array(
        'description'   => 'Whether the user has deleted this message',
litwol's avatar
litwol committed
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0
litwol's avatar
litwol committed
      ),
litwol's avatar
litwol committed

litwol's avatar
litwol committed
    ),
    'indexes'         => array(
litwol's avatar
litwol committed
      'mid'               => array('mid'),
      'thread_id'         => array('thread_id'),
      'uid'         => array('uid'),
      'is_new'              => array('mid', 'uid', 'is_new', ),
litwol's avatar
litwol committed
    ),
  );
litwol's avatar
litwol committed
  $schema['pm_message'] = array(
    'description'       => '{pm_messages} holds the message information',
litwol's avatar
litwol committed
    'fields'        => array(
litwol's avatar
litwol committed
      'mid'    => array(
        'description'   => 'Private Message ID',
litwol's avatar
litwol committed
        'type'          => 'serial',
        'not null'      => TRUE,
        'unsigned'      => TRUE,
      ),
      'author'     => array(
        'description'   => 'UID of the author',
litwol's avatar
litwol committed
        'type'          => 'int',
        'not null'      => TRUE,
        'unsigned'      => TRUE,
      ),
      'subject'     => array(
        'description'   => 'Subject text of the message',
litwol's avatar
litwol committed
        'type'          => 'varchar',
        'length'        => 255,
        'not null'      => TRUE,
      ),
      'body'     => array(
        'description'   => 'Body of the message',
litwol's avatar
litwol committed
        'type'          => 'text',
        'not null'      => TRUE,
        'size'          => 'big',
      ),
      'format'        => array(
        'type'          => 'int',
        'size'          => 'small',
        'not null'      => TRUE,
        'default'       => FILTER_FORMAT_DEFAULT,
        'description'   => 'The {filter_formats}.format of the message text.',
      ),
litwol's avatar
litwol committed
      'timestamp'     => array(
        'description'   => 'Time when the message was sent',
litwol's avatar
litwol committed
        'type'          => 'int',
        'not null'      => TRUE,
        'unsigned'      => TRUE,
      ),
litwol's avatar
litwol committed
    ),
litwol's avatar
litwol committed
    'primary key'     => array('mid'),
litwol's avatar
litwol committed
    'indexes'         => array(
litwol's avatar
litwol committed
      'author'            => array('author'),
      'subject'           => array(array('subject', 20)),
      'timestamp'         => array('timestamp'),
litwol's avatar
litwol committed
    ),
  );
litwol's avatar
litwol committed
  return $schema;
}
function privatemsg_install() {
litwol's avatar
litwol committed
  drupal_install_schema('privatemsg');

function privatemsg_uninstall() {
  variable_del('private_message_view_template');
  variable_del('privatemsg_per_page');
  variable_del('privatemsg_display_loginmessage');
  variable_del('privatemsg_display_fields');
litwol's avatar
litwol committed
  drupal_uninstall_schema('privatemsg');
litwol's avatar
litwol committed


function privatemsg_update_6000() {
  // Give update unlimited time to complete.
  set_time_limit(0);
litwol's avatar
litwol committed
  // Update the database schema and transfer data to new tables.
  $schema = array();
  $schema['pm_index'] = array(
    'description'       => '{pm_index} holds indexing information about messages and recepients for fast retrieval',
litwol's avatar
litwol committed
    'fields'        => array(
      'mid'    => array(
        'description'   => 'Private Message ID',
litwol's avatar
litwol committed
        'type'          => 'int',
        'not null'      => TRUE,
        'unsigned'      => TRUE,
      ),
      'thread_id'     => array(
        'description'   => 'Messages thread ID',
litwol's avatar
litwol committed
        'type'          => 'int',
        'not null'      => TRUE,
        'unsigned'      => TRUE,
      ),
      'uid'     => array(
        'description'   => 'UID of either the author or the recipient',
litwol's avatar
litwol committed
        'type'          => 'int',
        'not null'      => TRUE,
        'unsigned'      => TRUE,
      ),
      'is_new'     => array(
        'description'   => 'Whether the user has read this message',
litwol's avatar
litwol committed
        'type'          => 'int',
        'default'       => 1,
        'not null'      => TRUE,
        'unsigned'      => TRUE,
      ),
      'deleted' => array(
        'description'   => 'Whether the user has deleted this message',
litwol's avatar
litwol committed
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0
      ),

    ),
    'indexes'         => array(
      'mid'               => array('mid'),
      'thread_id'         => array('thread_id'),
      'uid'         => array('uid'),
      'is_new'              => array('mid', 'uid', 'is_new', ),
litwol's avatar
litwol committed
    ),
  );
litwol's avatar
litwol committed
  $schema['temp_pm_index'] = array(
    'description'       => '{pm_index} holds indexing information about messages and recepients for fast retrieval',
litwol's avatar
litwol committed
    'fields'        => array(
      'mid'    => array(
        'description'   => 'Private Message ID',
litwol's avatar
litwol committed
        'type'          => 'int',
        'not null'      => TRUE,
        'unsigned'      => TRUE,
      ),
      'folder'     => array(
        'description'   => 'ID of drupal 5 folder',
litwol's avatar
litwol committed
        'type'          => 'int',
        'not null'      => TRUE,
        'unsigned'      => TRUE,
      ),
      'thread'     => array(
        'description'   => 'Messages old thread ID',
litwol's avatar
litwol committed
        'type'          => 'int',
        'not null'      => TRUE,
        'unsigned'      => TRUE,
      ),
      'thread_id'     => array(
        'description'   => 'Messages new thread ID',
litwol's avatar
litwol committed
        'type'          => 'int',
        'not null'      => TRUE,
        'unsigned'      => TRUE,
      ),
      'uid'     => array(
        'description'   => 'UID of either the author or the recipient',
litwol's avatar
litwol committed
        'type'          => 'int',
        'not null'      => TRUE,
        'unsigned'      => TRUE,
      ),
      'is_new'     => array(
        'description'   => 'Whether the user has read this message',
litwol's avatar
litwol committed
        'type'          => 'int',
        'default'       => 1,
        'not null'      => TRUE,
        'unsigned'      => TRUE,
      ),
      'deleted' => array(
        'description'   => 'Whether the user has deleted this message',
litwol's avatar
litwol committed
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0
      ),
    ),
  );
litwol's avatar
litwol committed
  $schema['pm_message'] = array(
    'description'       => '{pm_messages} holds the message information',
litwol's avatar
litwol committed
    'fields'        => array(
      'mid'    => array(
        'description'   => 'Private Message ID',
litwol's avatar
litwol committed
        'type'          => 'serial',
        'not null'      => TRUE,
        'unsigned'      => TRUE,
      ),
      'author'     => array(
        'description'   => 'UID of the author',
litwol's avatar
litwol committed
        'type'          => 'int',
        'not null'      => TRUE,
        'unsigned'      => TRUE,
      ),
      'subject'     => array(
        'description'   => 'Subject text of the message',
litwol's avatar
litwol committed
        'type'          => 'varchar',
        'length'        => 255,
        'not null'      => TRUE,
      ),
      'body'     => array(
        'description'   => 'Body of the message',
litwol's avatar
litwol committed
        'type'          => 'text',
        'not null'      => TRUE,
        'size'          => 'big',
      ),
      'timestamp'     => array(
        'description'   => 'Time when the message was sent',
litwol's avatar
litwol committed
        'type'          => 'int',
        'not null'      => TRUE,
        'unsigned'      => TRUE,
      ),
    ),
    'primary key'     => array('mid'),
    'indexes'         => array(
      'author'            => array('author'),
      'subject'           => array(array('subject', 20)),
      'timestamp'         => array('timestamp'),
    ),
  );
  $ret = array();
litwol's avatar
litwol committed
  // Step 1: Create tables.
  if (!(db_table_exists('pm_message'))) {
    db_create_table($ret, 'pm_message', $schema['pm_message']);
  }
  if (!(db_table_exists('pm_index'))) {
    db_create_table($ret, 'pm_index', $schema['pm_index']);
  }
  if (!(db_table_exists('temp_pm_index'))) {
    db_create_table($ret, 'temp_pm_index', $schema['temp_pm_index']);
  }
  $modules = array();
  if (!(module_exists('privatemsg_filter'))) {
litwol's avatar
litwol committed
    $modules[] = 'privatemsg_filter';
  }
  if (!(module_exists('pm_block_user'))) {
litwol's avatar
litwol committed
    $modules[] = 'pm_block_user';
  }
  if (count($modules) > 0) {
    drupal_install_modules($modules);
  }
litwol's avatar
litwol committed
  // Step 2: Get the data
litwol's avatar
litwol committed
  // Step 2a: get the folder/tagging data first.
  if (db_table_exists('privatemsg_folder')) {
    $data = db_query("SELECT * FROM {privatemsg_folder}");
    while ($result = db_fetch_array($data)) {
      if (db_result(db_query("SELECT COUNT(*) FROM {pm_tags} WHERE tag = '%s'", $result->name)) == 0) {
litwol's avatar
litwol committed
        db_query("INSERT INTO {pm_tags} (tag) VALUES ('%s')", $result->name);
      }
    }
  }
litwol's avatar
litwol committed
  //Step 2b: Next, copy the user blocking data.
  if (db_table_exists('privatemsg_block_user')) {
    $data = db_query("SELECT * FROM {privatemsg_block_user}");
    while ($result = db_fetch_array($data)) {
      db_query("INSERT INTO {pm_block_user} (author, recipient) VALUES ( %d, %d )", $result['author'], $result['recipient']);
    }
  }
litwol's avatar
litwol committed
  //Step 2c: Next the data from the archive table - notice all these messages have been deleted both by the author and the recipient.
  if (db_table_exists('privatemsg_archive')) {
    $data = db_query("SELECT * FROM {privatemsg_archive}");
    while ($result = db_fetch_array($data)) {
      if ($result['thread'] == 0) {
        $result['thread_id'] = $result['id'];
      }
      else{
        $result['thread_id'] = 0;
      }
      if ($result['author'] <> $result['recipient']) {
        db_query("INSERT INTO {pm_message} (mid, author, subject, body, timestamp) VALUES ( %d, %d, '%s', '%s', %d )", $result['id'], $result['author'], $result['subject'], $result['message'], $result['timestamp']);
        db_query("INSERT INTO {temp_pm_index} (mid, thread, folder, thread_id, uid, is_new, deleted) VALUES ( %d, %d, %d, %d, %d, %d, %d )", $result['id'], $result['thread'], $result['folder'], $result['thread_id'], $result['recipient'], 0, 1);
      }
      db_query("INSERT INTO {temp_pm_index} (mid, thread, folder, thread_id, uid, is_new, deleted) VALUES ( %d, %d, %d, %d, %d, %d, %d )", $result['id'], $result['thread'], 0, $result['thread_id'], $result['author'], 0, 1);
    }
  }

  //Step 2d: Finally, get the data from the privatemsg table.
  if (db_table_exists('privatemsg')) {
    $data = db_query("SELECT * FROM {privatemsg}");
    while ($result = db_fetch_array($data)) {
      if ($result['thread'] == 0) {
        $result['thread_id'] = $result['id'];
      }
      else{
        $result['thread_id'] = 0;
      }
      if ($result['author'] <> $result['recipient']) {
        db_query("INSERT INTO {pm_message} (mid, author, subject, body, timestamp) VALUES ( %d, %d, '%s', '%s', %d )", $result['id'], $result['author'], $result['subject'], $result['message'], $result['timestamp']);
        db_query("INSERT INTO {temp_pm_index} (mid, thread, folder, thread_id, uid, is_new, deleted) VALUES ( %d, %d, %d, %d, %d, %d, %d )", $result['id'], $result['thread'], $result['folder'], $result['thread_id'], $result['recipient'], $result['newmsg'], $result['recipient_del']);
      }
      db_query("INSERT INTO {temp_pm_index} (mid, thread, folder, thread_id, uid, is_new, deleted) VALUES ( %d, %d, %d, %d, %d, %d, %d )", $result['id'], $result['thread'], 0, $result['thread_id'], $result['author'], 0, $result['author_del']);
    }
  }
litwol's avatar
litwol committed
  // Step 3: Process the Data.
  // Step 3a: Fix the thread data.
  $data = db_query("SELECT thread, MIN(mid) as new_thread FROM {temp_pm_index} WHERE thread_id = %d GROUP BY thread", 0);
  while ($result = db_fetch_array($data)) {
    db_query("UPDATE {temp_pm_index} SET thread_id = %d WHERE thread = %d", $result['new_thread'], $result['thread']);
  }
litwol's avatar
litwol committed
  // Step 3b: Fix and import the tagging data.
  $data = db_query("SELECT thread_id, folder, uid FROM {temp_pm_index} WHERE folder <> %d", 0);
  while ($result = db_fetch_array($data)) {
    $tag_id = db_result(db_query('SELECT pmt.tag_id FROM {pm_tags} pmt INNER JOIN {privatemsg_folder} pmf ON pmt.tag = pmf.name WHERE pmf.fid = %d', $result->folder));
    if (db_result(db_query("SELECT COUNT(*) FROM {pm_tags_index} WHERE uid = %d AND (thread_id = %d AND tag_id = %d)", $result->uid, $result->thread_id, $tag_id)) == 0) {
      db_query("INSERT INTO {pm_tags_index} (uid, tag_id, thread_id) VALUES (%d, %d, %d)", $result->uid, $tag_id, $result->thread_id);
    }
  }
litwol's avatar
litwol committed
  // Step 3c: Copy the index data.
  $data = db_query("SELECT * FROM {temp_pm_index}");
  while ($result = db_fetch_array($data)) {
    db_query("INSERT INTO {pm_index} (mid, thread_id, uid, is_new, deleted) VALUES ( %d, %d, %d, %d, %d )", $result['mid'], $result['thread_id'], $result['uid'], $result['is_new'], $result['deleted']);
  }
litwol's avatar
litwol committed
  // Step 4: Clean up.
  db_drop_table($ret, 'privatemsg');
  db_drop_table($ret, 'privatemsg_archive');
  db_drop_table($ret, 'privatemsg_folder');
  db_drop_table($ret, 'privatemsg_block_user');
  db_drop_table($ret, 'temp_pm_index');
function privatemsg_update_6001() {
  $ret = array();

  if (!db_column_exists('pm_index', 'is_new')) {

    if (db_column_exists('pm_index', 'new')) {
      $old_column = 'new';
    }
    elseif (db_column_exists('pm_index', 'new_flag')) {
      $old_column = 'new_flag';
    }
    else {
      return $ret;
    }

    db_drop_index($ret, 'pm_index', $old_column);
    db_change_field($ret, 'pm_index', $old_column, 'is_new', array(
          'description'   => 'Whether the user has read this message',
          'type'          => 'int',
          'default'       => 1,
          'not null'      => TRUE,
          'unsigned'      => TRUE));
    db_add_index($ret, 'pm_index', 'is_new', array('mid', 'uid', 'is_new'));
  }

  return $ret;
}

function privatemsg_update_6002() {
  $ret = array();
  // update_sql does not support parameters, we need to use db_query
  $sql = "UPDATE {blocks} SET cache = %d WHERE module='privatemsg'";
  $result = db_query($sql, BLOCK_NO_CACHE);
  $ret[] = array('success' => $result !== FALSE, 'query' => check_plain($sql));
litwol's avatar
litwol committed
  return $ret;
}

/**
 * Update function to resolve "forever new" messages
 * as described in http://drupal.org/node/490650
 */
function privatemsg_update_6003() {
  $ret = array();
  // Find messages that have aformentioned problem
  $sql = "SELECT DISTINCT p1.mid, p1.uid FROM {pm_index} p1 INNER JOIN {pm_index} p2 ON p1.thread_id = p2.thread_id AND p1.mid = p2.mid INNER JOIN {pm_message} pm ON p1.uid = pm.author AND p2.uid = pm.author WHERE p1.is_new != p2.is_new";
  $result = db_query($sql);
  while($row = db_fetch_object($result)) {
      privatemsg_message_change_status($row->mid, PRIVATEMSG_READ, $row );
  }
  $ret[] = array('success' => $result !== FALSE, 'query' => check_plain($sql));
  return $ret;
}

/**
 * Create a format column.
 *
 * Copied from system_update_6051
 */
function privatemsg_update_6004() {
  $ret = array();
  if (!db_column_exists('pm_message', 'format')) {
    $schema = array(
      'type' => 'int',
      'size' => 'small',
      'not null' => TRUE,
      'default' => FILTER_FORMAT_DEFAULT,
      'description' => 'The {filter_formats}.format of the message text.',
    );

    db_add_field($ret, 'pm_message', 'format', $schema);

    // Set the format of existing signatures to the current default input format.
    if ($current_default_filter = (int)variable_get('filter_default_format', 1)) {
      $ret[] = update_sql("UPDATE {pm_message} SET format = ". $current_default_filter);
    }
  }
  return $ret;
}

/**
 * Enable delete permission for all users that are allowed to read them.
 */
function privatemsg_update_6005() {
  $ret = array();
  $ret[] = update_sql("UPDATE {permission} SET perm = REPLACE(perm, 'read privatemsg', 'read privatemsg, delete privatemsg') WHERE perm LIKE '%read privatemsg%'");
  return $ret;
}

/**
 * Set the deleted timestamp of all messages to now.
 */
function privatemsg_update_6006() {
  $ret = array();

  $sql = "UPDATE {pm_index} SET deleted = %d WHERE deleted = 1";
  $result = db_query($sql, time());
  $ret[] = array('success' => $result !== FALSE, 'query' => check_plain($sql));
  return $ret;