WordPress 糟糕的重复评论检测方式

去旧空间清理文件时,用 FTP 拉下一大堆 mysql slow queries 日志。由于我的博客平时几乎没什么人来访问,所以我也没怎么去关心是否存在 mysql slow queries。看了下日志,发现绝大部分 slow query 都是由 WordPress 的重复评论检测造成成的:

wp_comment_mysql_slow_queries

没想到居然直接用 Text 类型的 comment_content 字段来判断评论重复,太衰了。开始还怀疑是不是日志搞错了,于是找到相应的代码确认:

// Simple duplicate check
// expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content)
$dupe = "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND ( comment_author = '$comment_author' ";
if ( $comment_author_email )
  $dupe .= "OR comment_author_email = '$comment_author_email' ";
$dupe .= ") AND comment_content = '$comment_content' LIMIT 1";
if ( $wpdb->get_var($dupe) ) {
  if ( defined('DOING_AJAX') )
    die( __('Duplicate comment detected; it looks as though you\'ve already said that!') );

  wp_die( __('Duplicate comment detected; it looks as though you\'ve already said that!') );
}
// 上述代码位于 wp-includes/comment.php 文件的 wp_allow_comment 函数中

有点奇怪 WP 为什么不考虑加个 comment_hash 字段,将评论内容 hash 一下,然后建个索引,那检测速度肯定杠杠滴。

Leave a Reply

Your email address will not be published. Required fields are marked *