You are here

function system_update_178 in Drupal 4

Same name and namespace in other branches
  1. 5 modules/system/system.install \system_update_178()

Update base paths for relative URLs in node and comment content.

File

database/updates.inc, line 1788

Code

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

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

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

      // Build format query string
      $_SESSION['formats'] = array_keys($formats);
      $_SESSION['format_string'] = '(' . substr(str_repeat('%d, ', count($formats)), 0, -2) . ')';

      // Begin update
      $_SESSION['system_update_178_comment'] = 0;
      $_SESSION['system_update_178_node'] = 0;
      $_SESSION['system_update_178_comment_max'] = db_result(db_query('SELECT MAX(cid) FROM {comments} WHERE format IN ' . $_SESSION['format_string'], $_SESSION['formats']));
      $_SESSION['system_update_178_node_max'] = db_result(db_query('SELECT MAX(vid) FROM {node_revisions} WHERE format IN ' . $_SESSION['format_string'], $_SESSION['formats']));
    }
    $limit = 20;

    // Comments
    if ($_SESSION['system_update_178_comment'] != $_SESSION['system_update_178_comment_max']) {
      $args = array_merge(array(
        $_SESSION['system_update_178_comment'],
      ), $_SESSION['formats']);
      $result = db_query_range("SELECT cid, comment FROM {comments} WHERE cid > %d AND format IN " . $_SESSION['format_string'] . ' ORDER BY cid ASC', $args, 0, $limit);
      while ($comment = db_fetch_object($result)) {
        $_SESSION['system_update_178_comment'] = $comment->cid;
        $comment->comment = _update_178_url_fix($comment->comment);
        if ($comment->comment !== FALSE) {
          db_query("UPDATE {comments} SET comment = '%s' WHERE cid = %d", $comment->comment, $comment->cid);
        }
      }
    }

    // Node revisions
    $args = array_merge(array(
      $_SESSION['system_update_178_node'],
    ), $_SESSION['formats']);
    $result = db_query_range("SELECT vid, teaser, body FROM {node_revisions} WHERE vid > %d AND format IN " . $_SESSION['format_string'] . ' ORDER BY vid ASC', $args, 0, $limit);
    while ($node = db_fetch_object($result)) {
      $_SESSION['system_update_178_node'] = $node->vid;
      $set = array();
      $args = array();
      $node->teaser = _update_178_url_fix($node->teaser);
      if ($node->teaser !== FALSE) {
        $set[] = "teaser = '%s'";
        $args[] = $node->teaser;
      }
      $node->body = _update_178_url_fix($node->body);
      if ($node->body !== FALSE) {
        $set[] = "body = '%s'";
        $args[] = $node->body;
      }
      if (count($set)) {
        $args[] = $node->vid;
        db_query('UPDATE {node_revisions} SET ' . implode(', ', $set) . ' WHERE vid = %d', $args);
      }
    }
    if ($_SESSION['system_update_178_comment'] == $_SESSION['system_update_178_comment_max'] && $_SESSION['system_update_178_node'] == $_SESSION['system_update_178_node_max']) {
      unset($_SESSION['system_update_178_comment']);
      unset($_SESSION['system_update_178_comment_max']);
      unset($_SESSION['system_update_178_node']);
      unset($_SESSION['system_update_178_node_max']);
      return array();
    }
    else {

      // Report percentage finished
      return array(
        '#finished' => ($_SESSION['system_update_178_comment'] + $_SESSION['system_update_178_node']) / ($_SESSION['system_update_178_comment_max'] + $_SESSION['system_update_178_node_max']),
      );
    }
  }
  return array();
}