[HACK]在 node_counter 資料表加計每週與每月瀏覽次數

edited 十月 2013 in Drupal
*如果真的改了,千萬記得做個記錄,否則下次升級你就知道了... ;)

1. 資料庫結構調整
ALTER TABLE `node_counter` ADD `weekcount` INT( 11 ) UNSIGNED NOT NULL AFTER `timestamp` ,
ADD `monthcount` INT( 11 ) UNSIGNED NOT NULL AFTER `weekcount`;
*如果希望安裝的時候就自動建立,可以將上述欄位加入 modules/system/system.install 建立該資料表的位置

2. 找到 modules/statistics/statistics.module 的 function statistics_cron() ,改成這樣:
function statistics_cron() {
  $statistics_timestamp = variable_get('statistics_day_timestamp', '');

  if ((time() - $statistics_timestamp) >= 86400) {
    /* reset day counts */
    db_query('UPDATE {node_counter} SET daycount = 0');
    variable_set('statistics_day_timestamp', time());
  }
  $statistics_week = variable_get('statistics_week_number', 0);
  if($statistics_week == 0) {
      variable_set('statistics_week_number', date('W'));
  } else if($statistics_week != date('W')) {
      db_query('UPDATE {node_counter} SET weekcount = 0');
      variable_set('statistics_week_number', date('W'));
  }
  $statistics_month = variable_get('statistics_month_number', 0);
  if($statistics_month == 0) {
      variable_set('statistics_month_number', date('n'));
  } else if($statistics_month != date('n')) {
      db_query('UPDATE {node_counter} SET monthcount = 0');
      variable_set('statistics_month_number', date('n'));
  }

  /* clean expired access logs */
  db_query('DELETE FROM {accesslog} WHERE timestamp < %d', time() - variable_get('statistics_flush_accesslog_timer', 259200));
}

完成後系統在每次排程時就會同時記錄當週與當月點閱次數,顯示的部份我是直接查資料庫 ;)

原始討論: http://twpug.net/x/modules/newbb/viewtopic.php?topic_id=4096
Sign In or Register to comment.