You are here

function system_update_124 in Drupal 4

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

File

database/updates.inc, line 342

Code

function system_update_124() {
  $ret = array();
  if ($GLOBALS['db_type'] == 'mysql') {

    // redo update_105, correctly creating node_comment_statistics
    $ret[] = update_sql("DROP TABLE IF EXISTS {node_comment_statistics}");
    $ret[] = update_sql("CREATE TABLE {node_comment_statistics} (\n      nid int(10) unsigned NOT NULL auto_increment,\n      last_comment_timestamp int(11) NOT NULL default '0',\n      last_comment_name varchar(60) default NULL,\n      last_comment_uid int(10) NOT NULL default '0',\n      comment_count int(10) unsigned NOT NULL default '0',\n      PRIMARY KEY (nid),\n      KEY node_comment_timestamp (last_comment_timestamp)\n      )");
  }
  else {

    // also drop incorrectly named table for PostgreSQL
    $ret[] = update_sql("DROP TABLE {node}_comment_statistics");
    $ret[] = update_sql("CREATE TABLE {node_comment_statistics} (\n      nid integer NOT NULL,\n      last_comment_timestamp integer NOT NULL default '0',\n      last_comment_name varchar(60)  default NULL,\n      last_comment_uid integer NOT NULL default '0',\n      comment_count integer NOT NULL default '0',\n      PRIMARY KEY (nid)\n    )");
    $ret[] = update_sql("CREATE INDEX {node_comment_statistics}_timestamp_idx ON {node_comment_statistics}(last_comment_timestamp);\n");
  }

  // initialize table
  $ret[] = update_sql("INSERT INTO {node_comment_statistics} (nid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) SELECT n.nid, n.changed, NULL, 0, 0 FROM {node} n");

  // fill table
  $result = db_query("SELECT c.nid, c.timestamp, c.name, c.uid, COUNT(c.nid) as comment_count FROM {node} n LEFT JOIN {comments} c ON c.nid = n.nid WHERE c.status = 0 GROUP BY c.nid, c.timestamp, c.name, c.uid");
  while ($comment_record = db_fetch_object($result)) {
    $count = db_result(db_query('SELECT COUNT(cid) FROM {comments} WHERE nid = %d AND status = 0', $comment_record->nid));
    db_query("UPDATE {node_comment_statistics} SET comment_count = %d, last_comment_timestamp = %d, last_comment_name = '%s', last_comment_uid = %d WHERE nid = %d", $count, $comment_record->timestamp, $comment_record->name, $comment_record->uid, $comment_record->nid);
  }
  return $ret;
}