You are here

function user_relationship_migrate_migrate in User Relationships 5

Actually migrate the data

1 string reference to 'user_relationship_migrate_migrate'
user_relationship_migrate_menu in plugins/user_relationship_migrate/user_relationship_migrate.module
hook_menu()

File

plugins/user_relationship_migrate/user_relationship_migrate.module, line 211
Drupal Module: User Relationship Migrate

Code

function user_relationship_migrate_migrate() {
  $status = variable_get('user_relationship_migrate_status', 'N/A');
  if ($status == 'IN PROGRESS') {

    // protect against clicking 'Migrate' twice (the button stays up and active during the whole migration)
    $output = t("Migration is already in progress. You don't want to run this twice!") . "<br />";
    $output .= "<br />" . t("Return to ") . l(t('User Relationships'), 'admin/user/relationships');
    return $output;
  }
  if ($status == 'COMPLETE') {
    $output = t("Migration has already completed successfully. You don't want to run this twice!") . "<br />";
    $output .= "<br />" . t("Return to ") . l(t('User Relationships'), 'admin/user/relationships');
    return $output;
  }

  // find the appropriate relationship type id
  $rtype_name = variable_get('user_relationship_migrate_rtype', '');
  $rtype = user_relationships_relationship_type_load(array(
    'name' => $rtype_name,
  ));
  if (!$rtype) {
    drupal_set_message(t('@type is not a valid relationship type name', array(
      '@type' => $rtype_name,
    )), 'error');
    return 'admin/user/relationships/migrate';
  }
  $rtid = $rtype->rtid;

  // start the migration
  variable_set('user_relationship_migrate_status', 'IN PROGRESS');
  $successes = 0;
  $errors = 0;
  $insert_query = "INSERT INTO {user_relationships} (rid, requester_id, requestee_id, rtid, approved, created_at) ";
  $insert_query .= "VALUES (%d, %d, %d, %d, %d, '%s')";

  // if there's alot of data to migrate, this process may have failed partway through;
  // if so, pick up where we left off
  $checkpoint = array(
    'migrated' => 0,
    'last_uid' => -1,
    'last_buddy' => -1,
    'last_pending_uid' => -1,
    'last_pending_buddy' => -1,
    'last_email_uid' => -1,
  );
  $checkpoint = variable_get('user_relationship_migrate_checkpoint', $checkpoint);
  $buddy_query = "SELECT * FROM {buddylist} WHERE (uid = %d AND buddy > %d) OR uid > %d ORDER BY uid, buddy";
  $buddy_args = array(
    $checkpoint['last_uid'],
    $checkpoint['last_buddy'],
    $checkpoint['last_uid'],
  );

  // migrate the approved relationships
  $buddies = db_query($buddy_query, $buddy_args);
  while ($buddy = db_fetch_object($buddies)) {

    // 2 rows in the buddylist table map to 1 row in the user_relationships table,
    // so only enter the relationship when uid < buddy
    if ($buddy->uid < $buddy->buddy) {
      $rid = db_next_id('{user_relationships}_id');
      $timestamp = date('Y-m-d H:i:s', $buddy->timestamp);
      $insert_args = array(
        $rid,
        $buddy->uid,
        $buddy->buddy,
        $rtid,
        1,
        $timestamp,
      );

      // start a transaction so that the insert and the variable_set either both happen or neither happen
      db_query("START TRANSACTION");
      if (db_query($insert_query, $insert_args)) {
        $checkpoint['migrated'] += 2;

        // 2 rows in the buddylist table have been accounted for
        $checkpoint['last_uid'] = (int) $buddy->uid;
        $checkpoint['last_buddy'] = (int) $buddy->buddy;
        variable_set('user_relationship_migrate_checkpoint', $checkpoint);
        $successes++;
      }
      else {
        $output .= t("ERROR: Unable to insert rid @rid between @uid and @buddy", array(
          '@rid' => $rid,
          '@uid' => $buddy->uid,
          '@buddy' => $buddy->buddy,
        )) . "<br />";
        $errors++;
      }
      db_query("COMMIT");

      // end the transaction
      if ($errors > 100) {
        drupal_set_message(t("More than 100 errors inserting relationships - aborting migration."), 'error');
        break;
      }
    }
  }

  // migrate the pending relationships, if admin requested it
  if ($errors <= 100 && variable_get('user_relationship_migrate_pending', 0)) {
    $timestamp = date('Y-m-d H:i:s');

    // if there's alot of data to migrate, this process may have failed partway through;
    // if so, pick up where we left off
    $pending_query = "SELECT * FROM {buddylist_pending_requests} ";
    $pending_query .= "WHERE (requester_uid = %d AND requestee_uid > %d) OR requester_uid > %d ";
    $pending_query .= "ORDER BY requester_uid, requestee_uid";
    $pending_args = array(
      $checkpoint['last_pending_uid'],
      $checkpoint['last_pending_buddy'],
      $checkpoint['last_pending_uid'],
    );
    $pendings = db_query($pending_query, $pending_args);
    while ($pending = db_fetch_object($pendings)) {
      $rid = db_next_id('{user_relationships}_id');
      $insert_args = array(
        $rid,
        $pending->requester_uid,
        $pending->requestee_uid,
        $rtid,
        0,
        $timestamp,
      );

      // start a transaction so that the insert and the variable set either both happen or neither happen
      db_query("START TRANSACTION");
      if (db_query($insert_query, $insert_args)) {
        $checkpoint['migrated']++;
        $checkpoint['last_pending_uid'] = (int) $pending->requester_uid;
        $checkpoint['last_pending_buddy'] = (int) $pending->requestee_uid;
        variable_set('user_relationship_migrate_checkpoint', $checkpoint);
        $successes++;
      }
      else {
        $output .= t("ERROR: Unable to insert rid @rid between @uid and @buddy", array(
          '@rid' => $rid,
          '@uid' => $pending->requester_uid,
          '@buddy' => $pending->requester_uid,
        )) . "<br />";
        $errors++;
      }
      db_query("COMMIT");

      // end the transaction
      if ($errors > 100) {
        drupal_set_message(t("More than 100 errors inserting relationships - aborting migration."), 'error');
        break;
      }
    }
  }

  // migrate the email settings, if admin requested it
  if ($errors <= 100 && variable_get('user_relationship_migrate_email', 0)) {
    $users = db_query("SELECT uid, data FROM {users} WHERE uid > %d", $checkpoint['last_email_uid']);
    while ($user_object = db_fetch_object($users)) {
      $user_data = unserialize($user_object->data);
      if (isset($user_data['buddylist_mail'])) {
        $user_data['user_relationship_mailer_send_mail'] = $user_data['buddylist_mail'];
        unset($user_data['buddylist_mail']);
        $user_data = serialize($user_data);

        // start a transaction so that the update and the variable set either both happen or neither happen
        db_query("START TRANSACTION");
        if (db_query("UPDATE {users} SET data = '%s' WHERE uid = %d", $user_data, $user_object->uid)) {
          $checkpoint['last_email_uid'] = (int) $user_object->uid;
          variable_set('user_relationship_migrate_checkpoint', $checkpoint);
          $updated_users++;
        }
        else {
          $output .= t("ERROR: Unable to update email settings for user @uid", array(
            '@uid' => $user_object->uid,
          )) . "<br />";
          $errors++;
        }
        db_query("COMMIT");

        // end the transaction
      }
    }
  }

  // Tidy up
  $output .= "<p>";
  $output .= t('Successfully migrated @count buddies to relationship type @type.', array(
    '@count' => $checkpoint['migrated'],
    '@type' => $rtype_name,
  ));
  if ($updated_users > 0) {
    $output .= t("  @count users' email settings were updated.", array(
      '@count' => $updated_users,
    ));
  }
  $output .= "</p>";
  if (!$errors) {
    variable_set('user_relationship_migrate_status', 'COMPLETE');
    module_disable(array(
      'user_relationship_migrate',
    ));
    $output .= "<p>";
    $output .= t("Buddy List data has been successfully migrated. The User Relationship Migrate plug in has been disabled. Don't forget to disable the Buddy List module.");
    $output .= "</p>";
  }
  else {
    variable_set('user_relationship_migrate_status', 'PARTIALLY COMPLETE');
    $output .= "<p>";
    $output .= t("Unable to migrate {$errors} buddies.");
    $output .= "</p>";
  }
  $output .= "<p>";
  $output .= t("Return to ") . l(t('User Relationships'), 'admin/user/relationships');
  $output .= "</p>";
  return $output;
}