You are here

function privatemsg_update_6000 in Privatemsg 6.2

Same name and namespace in other branches
  1. 6 privatemsg.install \privatemsg_update_6000()
1 string reference to 'privatemsg_update_6000'
privatemsg_update_6003 in ./privatemsg.install
Update function to resolve "forever new" messages.

File

./privatemsg.install, line 163
Install file for privatemsg.module

Code

function privatemsg_update_6000() {

  // Give update unlimited time to complete.
  set_time_limit(0);

  // 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',
    'fields' => array(
      'mid' => array(
        'description' => 'Private Message ID',
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
      ),
      'thread_id' => array(
        'description' => 'Messages thread ID',
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
      ),
      'uid' => array(
        'description' => 'UID of either the author or the recipient',
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
      ),
      'is_new' => array(
        'description' => 'Whether the user has read this message',
        'type' => 'int',
        'default' => 1,
        'not null' => TRUE,
        'unsigned' => TRUE,
      ),
      'deleted' => array(
        'description' => 'Whether the user has deleted this message',
        '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',
      ),
    ),
  );
  $schema['temp_pm_index'] = array(
    'description' => '{pm_index} holds indexing information about messages and recepients for fast retrieval',
    'fields' => array(
      'mid' => array(
        'description' => 'Private Message ID',
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
      ),
      'folder' => array(
        'description' => 'ID of drupal 5 folder',
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
      ),
      'thread' => array(
        'description' => 'Messages old thread ID',
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
      ),
      'thread_id' => array(
        'description' => 'Messages new thread ID',
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
      ),
      'uid' => array(
        'description' => 'UID of either the author or the recipient',
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
      ),
      'is_new' => array(
        'description' => 'Whether the user has read this message',
        'type' => 'int',
        'default' => 1,
        'not null' => TRUE,
        'unsigned' => TRUE,
      ),
      'deleted' => array(
        'description' => 'Whether the user has deleted this message',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'indexes' => array(
      'mid' => array(
        'mid',
      ),
      'thread_id' => array(
        'thread_id',
      ),
      'uid' => array(
        'uid',
      ),
    ),
  );
  $schema['pm_message'] = array(
    'description' => '{pm_messages} holds the message information',
    'fields' => array(
      'mid' => array(
        'description' => 'Private Message ID',
        'type' => 'serial',
        'not null' => TRUE,
        'unsigned' => TRUE,
      ),
      'author' => array(
        'description' => 'UID of the author',
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
      ),
      'subject' => array(
        'description' => 'Subject text of the message',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'body' => array(
        'description' => 'Body of the message',
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'big',
      ),
      'timestamp' => array(
        'description' => 'Time when the message was sent',
        '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();

  // Step 1: Preparation
  // Create the privatemsg 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']);
  }

  // Enable the privatemsg module as otherwise the enable box will be unclickable after update.
  if (!module_exists('privatemsg')) {
    module_enable('privatemsg');
  }

  // Install relevant submodules as we need theit tables to drop the data into.
  $modules = array();
  if (!module_exists('privatemsg_filter')) {
    $modules[] = 'privatemsg_filter';
  }
  if (!module_exists('pm_block_user')) {
    $modules[] = 'pm_block_user';
  }
  if (count($modules) > 0) {
    drupal_install_modules($modules);
  }

  // Step 2: Get the data
  // Step 2a: get the folder/tagging data first.
  db_query('INSERT INTO {pm_tags} (tag) SELECT DISTINCT name FROM {privatemsg_folder}');

  // Step 2b: Next, copy the user blocking data.
  if (db_table_exists('privatemsg_block_user')) {
    db_query('INSERT INTO {pm_block_user} (author, recipient) SELECT author, recipient FROM privatemsg_block_user');
  }

  // Step 2c: Copy data from the {privatemsg} and {privatemsg_archive} tables.
  foreach (array(
    "privatemsg_archive",
    "privatemsg",
  ) as $old_message_table) {
    $recipient_del = 'recipient_del';
    $author_del = 'author_del';
    $newmsg = 'newmsg';
    if ($old_message_table == 'privatemsg_archive') {
      $recipient_del = 1;
      $author_del = 1;
      $newmsg = '0';
    }
    if (db_table_exists($old_message_table)) {

      // Insert all message from old table.
      db_query("INSERT INTO {pm_message} (mid, author, subject, body, timestamp)\n                SELECT id,author,subject,message,timestamp\n                FROM {$old_message_table} WHERE author <> recipient");

      // Temporary index for the recipient of the message, with the proper
      // deleted tag if the recipient deleted it. We do two queries, so we
      // capture in the thread_id the id of the message if it was not threaded.
      db_query("INSERT INTO {temp_pm_index} (mid, thread, folder, thread_id, uid,       is_new, deleted)\n                SELECT                        id, thread, folder, id,        recipient, {$newmsg},      {$recipient_del}\n                FROM {$old_message_table} WHERE thread = 0");
      db_query("INSERT INTO {temp_pm_index} (mid, thread, folder, thread_id, uid,       is_new, deleted)\n                SELECT                        id, thread, folder, 0,         recipient, {$newmsg},      {$recipient_del}\n                FROM {$old_message_table} WHERE thread <> 0");

      // Temporary index for the recipient of the message, with the proper
      // deleted tag if the author deleted it. We do two queries, so we capture
      // in the thread_id the id of the message if it was not threaded.
      db_query("INSERT INTO {temp_pm_index}     (mid, thread, folder, thread_id, uid,    is_new, deleted)\n                SELECT                            id, thread, 0,      id,        author, 0,      {$author_del}\n                FROM {$old_message_table} WHERE thread = 0");
      db_query("INSERT INTO {temp_pm_index} (mid, thread, folder, thread_id, uid,    is_new, deleted)\n                SELECT                        id, thread, 0,      0,         author, 0,      {$author_del}\n                FROM {$old_message_table} WHERE thread <> 0");
    }
  }

  // Step 3: Process the Data.
  // Step 3a: Fix the thread data.
  $temptable = 'temp_pm_thread_min';
  db_query_temporary('SELECT thread, min(mid) as mid FROM {temp_pm_index} WHERE thread_id = 0 GROUP BY thread', $temptable);
  db_query("create index t2 on {$temptable} (thread)");
  db_query("create index t1 on temp_pm_index (thread_id)");
  db_query("create index t2 on temp_pm_index (thread)");
  db_query("UPDATE temp_pm_index i, {$temptable} t SET i.thread_id = t.mid WHERE i.thread_id = 0 and i.thread = t.thread");

  // Step 3b: Fix and import the tagging data.
  db_query("INSERT INTO {pm_tags_index}\n            SELECT distinct pmf.uid, t.tag_id, thread_id\n            FROM {pm_tags} t INNER JOIN {privatemsg_folder} pmf ON t.tag = pmf.name, temp_pm_index tmp\n            WHERE pmf.fid = tmp.folder  and tmp.folder <> 0");

  // Step 3c: Copy the index data.
  db_query("INSERT INTO {pm_index} (mid, thread_id, uid, is_new, deleted) SELECT mid, thread_id, uid, is_new, deleted FROM {temp_pm_index}");

  // Step 4: Clean up.
  db_drop_table($ret, 'privatemsg');
  db_drop_table($ret, 'privatemsg_archive');
  db_drop_table($ret, 'privatemsg_folder');
  if (db_table_exists('privatemsg_block_user')) {
    db_drop_table($ret, 'privatemsg_block_user');
  }
  db_drop_table($ret, 'temp_pm_index');

  // Set a variable to indicate that this is a direct upgrade from Drupal 5,
  // this allows to skip the slow 6003() update function.
  variable_set('privatemsg_update_6000', 1);
  return $ret;
}