You are here

function user_revision_table_init_data_batch_process in User Revision 7.2

Same name and namespace in other branches
  1. 7 user_revision.batch.inc \user_revision_table_init_data_batch_process()

Batch process callback.

Parameters

$total: Approximate total count of users in database.

$context:

1 string reference to 'user_revision_table_init_data_batch_process'
user_revision_install in ./user_revision.install
Implements hook_install().

File

./user_revision.batch.inc, line 15
Batch API functions, callbacks.

Code

function user_revision_table_init_data_batch_process($total, &$context) {

  // Count of rows that will be processed per iteration.
  $limit = USER_REVISION_BATCH_USERS_LIMIT;

  // Batch default values.
  if (empty($context['sandbox']['processed'])) {
    $context['sandbox']['max'] = $total;
    $context['sandbox']['processed'] = 0;
  }

  // Fetches $limit rows from {users} table that haven't
  // already were added to {user_revision} table.
  $query = db_select('users', 'u');
  $query
    ->leftJoin('user_revision', 'ur', 'u.uid = ur.uid');
  $query
    ->fields('u', array(
    'uid',
    'vid',
    'name',
    'mail',
    'theme',
    'signature',
    'signature_format',
    'status',
    'timezone',
    'language',
    'picture',
    'data',
  ))
    ->condition('u.uid', 0, '!=')
    ->isNull('ur.uid')
    ->orderBy('u.uid')
    ->range(0, $limit);
  $data = $query
    ->execute()
    ->fetchAllAssoc('uid');
  if (!empty($data)) {

    // Build query to insert data.
    $query = db_insert('user_revision')
      ->fields(array(
      'uid',
      'vid',
      'log',
      'timestamp',
      'authorid',
      'name',
      'mail',
      'theme',
      'signature',
      'signature_format',
      'status',
      'timezone',
      'language',
      'picture',
      'data',
    ));
    foreach ($data as $row) {

      // Lookup the user's roles and add to the role revision table.
      $roles = db_select('users_roles', 'ur')
        ->fields('ur', array(
        'rid',
      ))
        ->condition('uid', $row->uid)
        ->execute()
        ->fetchAll();
      foreach ($roles as $role) {

        // Add roles to user_revision_roles table.
        // Note that as this is a new install. vid = uid.
        user_revision_add_role($row->uid, $row->uid, $role->rid);
      }
      $query
        ->values(array(
        'uid' => $row->uid,
        'vid' => $row->vid,
        'log' => '',
        'timestamp' => REQUEST_TIME,
        'authorid' => $row->uid,
        'name' => $row->name,
        'mail' => $row->mail,
        'theme' => $row->theme,
        'signature' => $row->signature,
        'signature_format' => $row->signature_format,
        'status' => $row->status,
        'timezone' => $row->timezone,
        'language' => $row->language,
        'picture' => $row->picture,
        'data' => $row->data,
      ));
    }
    $query
      ->execute();

    // Updates Batch message, sandbox and result data.
    $context['sandbox']['processed'] += count($data);
    $context['results']['processed'] = $context['sandbox']['processed'];
    $context['message'] = t('Processed %processed rows of %count.', array(
      '%processed' => $context['sandbox']['processed'],
      '%count' => $context['sandbox']['max'],
    ));
    $context['finished'] = 0;
  }
  else {
    $context['finished'] = 1;
  }
}