You are here

function legal_update_2 in Legal 6.7

Same name and namespace in other branches
  1. 5 legal.install \legal_update_2()
  2. 6.8 legal.install \legal_update_2()

Remove conditions and extras from users.data field.

File

./legal.install, line 63

Code

function legal_update_2() {

  // How many users to process each time.
  $limit = 50;

  // Set-up multipart update.
  if (!isset($_SESSION['legal_update_2'])) {
    $conditions = db_fetch_array(db_query_range("SELECT * FROM {legal_conditions} ORDER BY tc_id DESC", 0, 1));
    if (empty($conditions['conditions'])) {
      return;
    }
    $_SESSION['legal_update_2'] = array(
      'uid' => 0,
      'max' => db_result(db_query('SELECT MAX(uid) FROM {users}')),
      'extras' => array_keys(unserialize($conditions['extras'])),
    );
  }

  // Fetch up to N users and fix their data field.
  $result = db_query_range("SELECT uid, data FROM {users} WHERE uid > %d AND data LIKE '%%s:10:\"conditions\"%%'", $_SESSION['legal_update_2']['uid'], 0, $limit);
  $user_count = 0;
  while ($user = db_fetch_object($result)) {
    $data = unserialize($user->data);
    unset($data['conditions']);
    foreach ($_SESSION['legal_update_2']['extras'] as $extra) {
      unset($data[$extra]);
    }
    db_query("UPDATE {users} SET data = '%s' WHERE uid = %d", serialize($data), $user->uid);
    $_SESSION['legal_update_2']['uid'] = $user->uid;
    $user_count++;
  }
  if ($user_count == $limit) {

    // Return progress (never return 100% here to ensure clean-up is still run last).
    return array(
      '#finished' => $_SESSION['legal_update_2']['uid'] / ($_SESSION['legal_update_2']['max'] + 1),
    );
  }
  else {

    // Clean-up.
    unset($_SESSION['legal_update_2']);
    return array();
  }
}