您在這裡

討論區的主題都不見了 顯示不出來

Hipfox's 的頭像
Hipfox 在 2007-11-05 (週一) 12:40 發表

最近在調整一個站的效能,動了很多東西,包括「Apache」、「MySQL」、「eAccelerator」、「Drupal 5.2 -> 5.3」...

最後發現討論區的主題都不顯示了,只呈現 xx 篇新文章而已,我重貼新帖就又出現 @@

參照 jimmy 的建議,去追蹤 forum 的程式碼,把幾個重要的節點,dump 出一些變數資料,特別是 SQL 語法在篩選資料的過程。

總算發現 node_comment_statistics 資料表早就空空如也,也才想起我除了用 devel 清空 cache 之外,還按到一個致命的 Rebuild NCS table,它的原始碼是

function devel_rebuild_node_comment_statistics_page() {
devel_rebuild_node_comment_statistics();
drupal_set_message('node_comment_statistics table has been rebuilt.');
drupal_goto('admin');
}
function devel_rebuild_node_comment_statistics() {
$sql = "DELETE FROM {node_comment_statistics}";
db_query($sql);
$sql = "INSERT INTO {node_comment_statistics} (nid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) (select nid, c.timestamp, name, uid, comment_count FROM {comments} c INNER JOIN (SELECT MAX(timestamp) AS timestamp, COUNT(*) AS comment_count FROM {comments} WHERE status=%d GROUP BY nid) as c2 ON c.timestamp=c2.timestamp)";
db_query($sql, COMMENT_PUBLISHED);
}

node_comment_statistics 被刪了,難怪影響到 forum 的顯示。 devel 的 Rebuild NCS table 功能並沒有把 forum 重建回來,因此必須要想想辦法,果然有這個案例 -- http://drupal.org/node/137458
用這段 code 就可以輕鬆搞定,如下

<?php
db_query("DELETE FROM {node_comment_statistics}");

$nodes = db_query("SELECT nid,uid,changed FROM {node} WHERE status=1");
while ($m = db_fetch_object($nodes)) {
$nid = $m->nid;

$comments=db_query("SELECT nid,max(cid) FROM {comments} WHERE status=0 AND nid=$nid GROUP by nid");
if($l = db_fetch_object($comments)) {
$cid = $l->{"max(cid)"};

$lastcomments = db_query("SELECT uid,name,timestamp FROM {comments} WHERE cid=$cid");
$n = db_fetch_object($lastcomments);
$uid = $n->uid;
$name = $n->name;
$timestamp = $n->timestamp;

$counts = db_query("SELECT count(*) FROM {comments} WHERE status=0 AND nid=$nid");
$n = db_fetch_object($counts);
$count = $n->{"count(*)"};
} else {
$timestamp = $m->changed;
$name = NULL;
$uid = $m->uid;
$count = 0;
}
db_query("INSERT INTO {node_comment_statistics} (nid,last_comment_timestamp,last_comment_name,last_comment_uid,comment_count) VALUES ('$nid','$timestamp','$name','$uid','$count')");
}
?>

真慘,斷斷續續除錯了三天,寫在這裡參考參考。