You are here

function legal_update_2 in Legal 6.8

Same name and namespace in other branches
  1. 5 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 77
Installation and update functions for the Legal module.

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);
  $user_count = 0;
  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]);
      }
    }

    // Don't use update_sql() instead of db_query(), Coder module flags this as a problem but ignore.
    // http://api.drupal.org/api/drupal/includes--database.inc/function/update_sql/6#comment-354
    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();
  }
}