You are here

function system_update_179 in Drupal 5

Same name and namespace in other branches
  1. 4 database/updates.inc \system_update_179()

Update base paths for relative URLs in custom blocks, profiles and various variables.

File

modules/system/system.install, line 2931

Code

function system_update_179() {
  if (variable_get('clean_url', 0) == 1) {

    // Multi-part update
    if (!isset($_SESSION['system_update_179_uid'])) {

      // Check which formats need to be converted
      $formats = _update_178_url_formats();
      if (count($formats) == 0) {
        return array();
      }

      // Custom Blocks (too small for multipart)
      $format_string = '(' . substr(str_repeat('%d, ', count($formats)), 0, -2) . ')';
      $result = db_query("SELECT bid, body FROM {boxes} WHERE format IN " . $format_string, array_keys($formats));
      while ($block = db_fetch_object($result)) {
        $block->body = _update_178_url_fix($block->body);
        if ($block->body !== FALSE) {
          db_query("UPDATE {boxes} SET body = '%s' WHERE bid = %d", $block->body, $block->bid);
        }
      }

      // Variables (too small for multipart)
      $vars = array(
        'site_mission',
        'site_footer',
        'user_registration_help',
      );
      foreach (node_get_types() as $type => $name) {
        $vars[] = $type . '_help';
      }
      foreach ($vars as $var) {
        $value = variable_get($var, NULL);
        if (!is_null($value)) {
          $value = _update_178_url_fix($value);
          if ($value !== FALSE) {
            variable_set($var, $value);
          }
        }
      }

      // See if profiles need to be updated: is the default format HTML?
      if (!isset($formats[variable_get('filter_default_format', 1)])) {
        return array();
      }
      $result = db_query("SELECT fid FROM {profile_fields} WHERE type = 'textarea'");
      $fields = array();
      while ($field = db_fetch_object($result)) {
        $fields[] = $field->fid;
      }
      if (count($fields) == 0) {
        return array();
      }

      // Begin multi-part update for profiles
      $_SESSION['system_update_179_fields'] = $fields;
      $_SESSION['system_update_179_field_string'] = '(' . substr(str_repeat('%d, ', count($fields)), 0, -2) . ')';
      $_SESSION['system_update_179_uid'] = 0;
      $_SESSION['system_update_179_fid'] = 0;
      $_SESSION['system_update_179_max'] = db_result(db_query('SELECT MAX(uid) FROM {profile_values} WHERE fid IN ' . $_SESSION['system_update_179_field_string'], $_SESSION['system_update_179_fields']));
    }

    // Fetch next 20 profile values to convert
    $limit = 20;
    $args = array_merge(array(
      $_SESSION['system_update_179_uid'],
      $_SESSION['system_update_179_fid'],
      $_SESSION['system_update_179_uid'],
    ), $_SESSION['system_update_179_fields']);
    $result = db_query_range("SELECT fid, uid, value FROM {profile_values} WHERE ((uid = %d AND fid > %d) OR uid > %d) AND fid IN " . $_SESSION['system_update_179_field_string'] . ' ORDER BY uid ASC, fid ASC', $args, 0, $limit);
    while ($field = db_fetch_object($result)) {
      $_SESSION['system_update_179_uid'] = $field->uid;
      $_SESSION['system_update_179_fid'] = $field->fid;
      $field->value = _update_178_url_fix($field->value);
      if ($field->value !== FALSE) {
        db_query("UPDATE {profile_values} SET value = '%s' WHERE uid = %d AND fid = %d", $field->value, $field->uid, $field->fid);
      }
    }

    // Done?
    if (db_num_rows($result) == 0) {
      unset($_SESSION['system_update_179_uid']);
      unset($_SESSION['system_update_179_fid']);
      unset($_SESSION['system_update_179_max']);
      return array();
    }
    else {

      // Report percentage finished
      // (Note: make sure we complete all fields for the last user by not reporting 100% too early)
      return array(
        '#finished' => $_SESSION['system_update_179_uid'] / ($_SESSION['system_update_179_max'] + 1),
      );
    }
  }
  return array();
}