diff --git a/database/database.mysql b/database/database.mysql
index 2ffd4384d29..244e7d405ec 100644
--- a/database/database.mysql
+++ b/database/database.mysql
@@ -203,7 +203,6 @@ CREATE TABLE comments (
CREATE TABLE node_comment_statistics (
nid int(10) unsigned NOT NULL auto_increment,
- cid int(10) unsigned NOT NULL default '0',
last_comment_timestamp int(11) NOT NULL default '0',
last_comment_name varchar(60) default NULL,
last_comment_uid int(10) NOT NULL default '0',
diff --git a/database/database.pgsql b/database/database.pgsql
index de167cd41d3..2d2d8653e46 100644
--- a/database/database.pgsql
+++ b/database/database.pgsql
@@ -206,7 +206,6 @@ CREATE INDEX comments_nid_idx ON comments(nid);
CREATE TABLE node_comment_statistics (
nid integer NOT NULL,
- cid integer NOT NULL default '0',
last_comment_timestamp integer NOT NULL default '0',
last_comment_name varchar(60) default NULL,
last_comment_uid integer NOT NULL default '0',
diff --git a/database/updates.inc b/database/updates.inc
index 60b7e47704d..402ebc125c3 100644
--- a/database/updates.inc
+++ b/database/updates.inc
@@ -99,7 +99,8 @@ $sql_updates = array(
"2005-01-25" => "update_120",
"2005-01-26" => "update_121",
"2005-01-27" => "update_122",
- "2005-01-28" => "update_123"
+ "2005-01-28" => "update_123",
+ "2005-02-11" => "update_124"
);
function update_32() {
@@ -1809,49 +1810,11 @@ function update_105() {
if ($GLOBALS['db_type'] == 'mysql') {
$ret[] = update_sql("ALTER TABLE {forum} DROP shadow");
$ret[] = update_sql('ALTER TABLE {node} ADD INDEX node_status_type (status, type, nid)');
-
- $ret[] = update_sql("CREATE TABLE {node_comment_statistics} (
- nid int(10) unsigned NOT NULL auto_increment,
- cid int(10) unsigned NOT NULL default '0',
- last_comment_timestamp int(11) NOT NULL default '0',
- last_comment_name varchar(60) default NULL,
- last_comment_uid int(10) NOT NULL default '0',
- comment_count int(10) unsigned NOT NULL default '0',
- PRIMARY KEY (nid),
- KEY node_comment_timestamp (last_comment_timestamp)
- ) TYPE=MyISAM");
- $ret[] = update_sql("INSERT INTO {node_comment_statistics} (nid, cid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) SELECT n.nid, 0, n.created, NULL, n.uid, 0 FROM {node} n");
-
- $ret[] = update_sql("CREATE TABLE {forum_conv_temp} (
- nid int(10) unsigned NOT NULL default '0',
- cid int(10) unsigned NOT NULL default '0',
- comment_count int(10) unsigned NOT NULL default '0',
- PRIMARY KEY (nid)
- )");
-
- $ret[] = update_sql('INSERT INTO {forum_conv_temp} SELECT f.nid, MAX(c.cid), COUNT(c.nid) FROM {forum} f INNER JOIN {comments} c ON f.nid = c.nid WHERE c.status = 0 GROUP BY f.nid');
-
- /* This would be faster but only works with MySQL 4.0.4 or higher
- $ret[] = update_sql('UPDATE {node_comment_statistics} n, {forum_conv_temp} t, {comments} c SET n.comment_count = t.comment_count, n.last_comment_timestamp = c.timestamp, n.last_comment_name = c.name, n.last_comment_uid = c.uid, n.cid = t.cid WHERE t.cid = c.cid AND n.nid = t.nid');
- */
}
else {
// PostgreSQL is incapable of dropping columns in all but the latest versions.
$ret[] = update_sql("CREATE INDEX {node}_status_type_idx ON {node} (status, type, nid)");
-
- $ret[] = update_sql("CREATE TABLE {node}_comment_statistics (
- nid integer NOT NULL,
- cid integer NOT NULL default '0',
- last_comment_timestamp integer NOT NULL default '0',
- last_comment_name varchar(60) default NULL,
- last_comment_uid integer NOT NULL default '0',
- comment_count integer NOT NULL default '0',
- PRIMARY KEY (nid)
- )");
-
- $ret[] = update_sql("SELECT f.nid, MAX(c.cid) as cid, COUNT(c.nid) as comment_count INTO TEMPORARY {forum_conv_temp} FROM {forum} f INNER JOIN {comments} c ON f.nid = c.nid WHERE c.status = 0 GROUP BY f.nid");
-
$ret[] = update_sql("CREATE FUNCTION \"if\"(integer, text, text) RETURNS text AS '
BEGIN
IF $1 THEN
@@ -1864,13 +1827,6 @@ function update_105() {
' LANGUAGE 'plpgsql'");
}
- $commentupdates = db_query("SELECT t.nid, t.cid, t.comment_count, c.timestamp, c.name, c.uid FROM {forum_conv_temp} t INNER JOIN {comments} c ON t.cid = c.cid");
- while ($commentrecord = db_fetch_object($commentupdates)) {
- db_query("UPDATE {node_comment_statistics} SET comment_count = %d, last_comment_timestamp = %d, last_comment_name = '%s', last_comment_uid = %d, cid = %d WHERE nid = %d", $commentrecord->comment_count, $commentrecord->timestamp, $commentrecord->name, $commentrecord->uid, $commentrecord->cid, $commentrecord->nid);
- }
-
- $ret[] = update_sql("DROP TABLE {forum_conv_temp}");
-
return $ret;
}
@@ -1921,7 +1877,6 @@ function update_108() {
return $ret;
}
-
function update_109() {
$ret = array();
// This is to fix the PostreSQL locales_source table.
@@ -2192,6 +2147,50 @@ function update_123() {
return $ret;
}
+function 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} (
+ nid int(10) unsigned NOT NULL auto_increment,
+ last_comment_timestamp int(11) NOT NULL default '0',
+ last_comment_name varchar(60) default NULL,
+ last_comment_uid int(10) NOT NULL default '0',
+ comment_count int(10) unsigned NOT NULL default '0',
+ PRIMARY KEY (nid),
+ KEY node_comment_timestamp (last_comment_timestamp)
+ ) TYPE=MyISAM");
+ }
+
+ else {
+ // also drop incorrectly named table for PostgreSQL
+ $ret[] = update_sql("DROP TABLE {node}_comment_statistics");
+
+ $ret[] = update_sql("CREATE TABLE {node_comment_statistics} (
+ nid integer NOT NULL,
+ last_comment_timestamp integer NOT NULL default '0',
+ last_comment_name varchar(60) default NULL,
+ last_comment_uid integer NOT NULL default '0',
+ comment_count integer NOT NULL default '0',
+ PRIMARY KEY (nid)
+ )");
+ }
+
+ // 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, 0, NULL, 0, 0 FROM {node} n");
+
+ // fill table
+ $comment_updates = db_query("SELECT c.nid, c.timestamp, c.name, c.uid, COUNT(c.nid) as comment_count FROM {comments} c INNER JOIN {node} n ON c.nid = n.nid WHERE c.status = 0 GROUP BY c.nid");
+ while ($comment_record = db_fetch_object($comment_updates)) {
+ $ret[] = update_sql("UPDATE {node_comment_statistics} SET comment_count = $comment_record->comment_count, last_comment_timestamp = $comment_record->timestamp, last_comment_name = '$comment_record->name', last_comment_uid = $comment_record->uid WHERE nid = $comment_record->nid");
+ }
+
+ return $ret;
+}
+
function update_sql($sql) {
$edit = $_POST["edit"];
$result = db_query($sql);
diff --git a/modules/comment.module b/modules/comment.module
index 90517617a71..8613601d330 100644
--- a/modules/comment.module
+++ b/modules/comment.module
@@ -246,7 +246,7 @@ function comment_nodeapi(&$node, $op, $arg = 0) {
break;
case 'load':
- return db_fetch_array(db_query("SELECT last_comment_timestamp, last_comment_name, last_comment_name, comment_count, cid as last_comment_cid FROM {node_comment_statistics} WHERE nid = %d", $node->nid));
+ return db_fetch_array(db_query("SELECT last_comment_timestamp, last_comment_name, comment_count FROM {node_comment_statistics} WHERE nid = %d", $node->nid));
case 'validate':
if (!user_access('administer nodes')) {
// Force default for normal users:
@@ -255,12 +255,9 @@ function comment_nodeapi(&$node, $op, $arg = 0) {
break;
case 'insert':
- db_query('INSERT INTO {node_comment_statistics} (nid, cid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) VALUES (%d, 0, %d, NULL, %d, 0)', $node->nid, $node->created, $node->uid);
+ db_query('INSERT INTO {node_comment_statistics} (nid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) VALUES (%d, %d, NULL, %d, 0)', $node->nid, $node->created, $node->uid);
break;
- case 'update':
- db_query('UPDATE {node_comment_statistics} SET last_comment_timestamp = %d WHERE nid = %d AND cid = 0', $node->changed, $node->nid);
- break;
case 'delete':
db_query('DELETE FROM {comments} WHERE nid = %d', $node->nid);
db_query('DELETE FROM {node_comment_statistics} WHERE nid = %d', $node->nid);
@@ -994,6 +991,11 @@ function comment_save($id, $edit) {
db_query("UPDATE {comments} SET subject = '%s', comment = '%s', status = %d, format = '%s', name = '%s', mail = '%s', homepage = '%s' WHERE cid = %d", $edit['subject'], $edit['comment'], $edit['status'], $edit['format'], $edit['name'], $edit['mail'], $edit['homepage'], $id);
watchdog('content', t('Comment: modified %subject.', array('%subject' => ''. $edit['subject'] .'')));
drupal_set_message(t('The comment has been saved.'));
+
+ _comment_update_node_statistics($edit['nid']);
+
+ // Allow modules to respond to the updating of a comment.
+ module_invoke_all('comment', 'update', $edit);
}
/**
@@ -1663,7 +1665,6 @@ function _comment_per_page() {
* time a comment is added, deleted, or updated.
*
* The following fields are contained in the node_comment_statistics table.
- * - cid: cid of the last comment to be created for the node.
* - last_comment_timestamp: the timestamp of the last comment for this node or the node create stamp if no comments exist for the node.
* - last_comment_name: the name of the anonymous poster for the last comment
* - last_comment_uid: the uid of the poster for the last comment for this node or the node authors uid if no comments exists for the node.
@@ -1671,9 +1672,17 @@ function _comment_per_page() {
*/
function _comment_update_node_statistics($nid) {
$count = db_result(db_query('SELECT COUNT(cid) FROM {comments} WHERE nid = %d AND status = 0', $nid));
- $node = node_load(array('nid' => $nid));
- $last_reply = db_fetch_object(db_query_range('SELECT cid, name, timestamp, uid FROM {comments} WHERE nid = %d AND status = 0 ORDER BY cid DESC', $nid, 0, 1));
- db_query("UPDATE {node_comment_statistics} SET comment_count = %d, last_comment_timestamp = '%s', last_comment_name = '%s', last_comment_uid = %d WHERE nid = %d", $count, $last_reply ? $last_reply->timestamp : $node->created, $last_reply->name, $last_reply ? $last_reply->uid : $node->uid, $nid);
-}
+ // comments exist
+ if ($count > 0) {
+ $node = node_load(array('nid' => $nid));
+ $last_reply = db_fetch_object(db_query_range('SELECT cid, name, timestamp, uid FROM {comments} WHERE nid = %d AND status = 0 ORDER BY cid DESC', $nid, 0, 1));
+ 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, $last_reply->timestamp, $last_reply->uid ? NULL : $last_reply->name, $last_reply->uid, $nid);
+ }
+
+ // no comments
+ else {
+ 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", 0, NULL, 0, 0, $nid);
+ }
+}
?>
diff --git a/modules/comment/comment.module b/modules/comment/comment.module
index 90517617a71..8613601d330 100644
--- a/modules/comment/comment.module
+++ b/modules/comment/comment.module
@@ -246,7 +246,7 @@ function comment_nodeapi(&$node, $op, $arg = 0) {
break;
case 'load':
- return db_fetch_array(db_query("SELECT last_comment_timestamp, last_comment_name, last_comment_name, comment_count, cid as last_comment_cid FROM {node_comment_statistics} WHERE nid = %d", $node->nid));
+ return db_fetch_array(db_query("SELECT last_comment_timestamp, last_comment_name, comment_count FROM {node_comment_statistics} WHERE nid = %d", $node->nid));
case 'validate':
if (!user_access('administer nodes')) {
// Force default for normal users:
@@ -255,12 +255,9 @@ function comment_nodeapi(&$node, $op, $arg = 0) {
break;
case 'insert':
- db_query('INSERT INTO {node_comment_statistics} (nid, cid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) VALUES (%d, 0, %d, NULL, %d, 0)', $node->nid, $node->created, $node->uid);
+ db_query('INSERT INTO {node_comment_statistics} (nid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) VALUES (%d, %d, NULL, %d, 0)', $node->nid, $node->created, $node->uid);
break;
- case 'update':
- db_query('UPDATE {node_comment_statistics} SET last_comment_timestamp = %d WHERE nid = %d AND cid = 0', $node->changed, $node->nid);
- break;
case 'delete':
db_query('DELETE FROM {comments} WHERE nid = %d', $node->nid);
db_query('DELETE FROM {node_comment_statistics} WHERE nid = %d', $node->nid);
@@ -994,6 +991,11 @@ function comment_save($id, $edit) {
db_query("UPDATE {comments} SET subject = '%s', comment = '%s', status = %d, format = '%s', name = '%s', mail = '%s', homepage = '%s' WHERE cid = %d", $edit['subject'], $edit['comment'], $edit['status'], $edit['format'], $edit['name'], $edit['mail'], $edit['homepage'], $id);
watchdog('content', t('Comment: modified %subject.', array('%subject' => ''. $edit['subject'] .'')));
drupal_set_message(t('The comment has been saved.'));
+
+ _comment_update_node_statistics($edit['nid']);
+
+ // Allow modules to respond to the updating of a comment.
+ module_invoke_all('comment', 'update', $edit);
}
/**
@@ -1663,7 +1665,6 @@ function _comment_per_page() {
* time a comment is added, deleted, or updated.
*
* The following fields are contained in the node_comment_statistics table.
- * - cid: cid of the last comment to be created for the node.
* - last_comment_timestamp: the timestamp of the last comment for this node or the node create stamp if no comments exist for the node.
* - last_comment_name: the name of the anonymous poster for the last comment
* - last_comment_uid: the uid of the poster for the last comment for this node or the node authors uid if no comments exists for the node.
@@ -1671,9 +1672,17 @@ function _comment_per_page() {
*/
function _comment_update_node_statistics($nid) {
$count = db_result(db_query('SELECT COUNT(cid) FROM {comments} WHERE nid = %d AND status = 0', $nid));
- $node = node_load(array('nid' => $nid));
- $last_reply = db_fetch_object(db_query_range('SELECT cid, name, timestamp, uid FROM {comments} WHERE nid = %d AND status = 0 ORDER BY cid DESC', $nid, 0, 1));
- db_query("UPDATE {node_comment_statistics} SET comment_count = %d, last_comment_timestamp = '%s', last_comment_name = '%s', last_comment_uid = %d WHERE nid = %d", $count, $last_reply ? $last_reply->timestamp : $node->created, $last_reply->name, $last_reply ? $last_reply->uid : $node->uid, $nid);
-}
+ // comments exist
+ if ($count > 0) {
+ $node = node_load(array('nid' => $nid));
+ $last_reply = db_fetch_object(db_query_range('SELECT cid, name, timestamp, uid FROM {comments} WHERE nid = %d AND status = 0 ORDER BY cid DESC', $nid, 0, 1));
+ 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, $last_reply->timestamp, $last_reply->uid ? NULL : $last_reply->name, $last_reply->uid, $nid);
+ }
+
+ // no comments
+ else {
+ 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", 0, NULL, 0, 0, $nid);
+ }
+}
?>