You are here

function legal_update_2 in Legal 5

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

Remove conditions and extras from users.data field.

File

./legal.install, line 83

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;
    }
    $extras = empty($conditions['extras']) ? NULL : array_keys(unserialize($conditions['extras']));
    $_SESSION['legal_update_2'] = array(
      'uid' => 0,
      'max' => db_result(db_query('SELECT MAX(uid) FROM {users}')),
      'extras' => $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);
  while ($user = db_fetch_object($result)) {
    $data = unserialize($user->data);
    unset($data['conditions']);
    if (!empty($_SESSION['legal_update_2']['extras'])) {
      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;
  }
  if (db_num_rows($result) == $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();
  }
}