diff --git a/core/modules/statistics/statistics.info b/core/modules/statistics/statistics.info index e7add60..840a647 100644 --- a/core/modules/statistics/statistics.info +++ b/core/modules/statistics/statistics.info @@ -4,4 +4,5 @@ package = Core version = VERSION core = 8.x files[] = statistics.test +scripts[] = statistics.js configure = admin/config/system/statistics diff --git a/core/modules/statistics/statistics.js b/core/modules/statistics/statistics.js new file mode 100644 index 0000000..b56f4ab --- /dev/null +++ b/core/modules/statistics/statistics.js @@ -0,0 +1,16 @@ +(function ($) { + + Drupal.behaviors.statisticsModule = { + attach: function (context, settings) { + var nid = settings.statistics.nid; + var basePath = settings.basePath + $.ajax({ + type: "POST", + cache: false, + url: basePath+"core/modules/statistics/statistics.php", + data: "nid="+nid, + }); + } + }; + +})(jQuery); \ No newline at end of file diff --git a/core/modules/statistics/statistics.module b/core/modules/statistics/statistics.module index eda808f..98cb5d8 100644 --- a/core/modules/statistics/statistics.module +++ b/core/modules/statistics/statistics.module @@ -41,6 +41,14 @@ function statistics_help($path, $arg) { } } +/** + * Implements hook_page_alter() + */ +function statistics_page_alter() { + if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == NULL) { + drupal_add_js(array('statistics' => array('nid' => arg(1))), 'setting'); + } +} /** * Implements hook_exit(). @@ -57,22 +65,6 @@ function statistics_exit() { // in which case we need to bootstrap to the session phase anyway. drupal_bootstrap(DRUPAL_BOOTSTRAP_VARIABLES); - if (variable_get('statistics_count_content_views', 0)) { - // We are counting content views. - if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == NULL) { - // A node has been viewed, so update the node's counters. - db_merge('node_counter') - ->key(array('nid' => arg(1))) - ->fields(array( - 'daycount' => 1, - 'totalcount' => 1, - 'timestamp' => REQUEST_TIME, - )) - ->expression('daycount', 'daycount + 1') - ->expression('totalcount', 'totalcount + 1') - ->execute(); - } - } if (variable_get('statistics_enable_access_log', 0)) { drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION); @@ -230,6 +222,9 @@ function statistics_user_predelete($account) { * Implements hook_cron(). */ function statistics_cron() { + drupal_static_reset('statistics_get'); + cache()->deletePrefix('statistics_data'); + $statistics_timestamp = variable_get('statistics_day_timestamp', ''); if ((REQUEST_TIME - $statistics_timestamp) >= 86400) { @@ -298,10 +293,19 @@ function statistics_title_list($dbfield, $dbrows) { * - timestamp: timestamp of when that node was last viewed. */ function statistics_get($nid) { - if ($nid > 0) { - // Retrieve an array with both totalcount and daycount. - return db_query('SELECT totalcount, daycount, timestamp FROM {node_counter} WHERE nid = :nid', array(':nid' => $nid), array('target' => 'slave'))->fetchAssoc(); + $stats_data = &drupal_static(__FUNCTION__); + if (!isset($stats_data)) { + if ($cache = cache()->get('statistics_data')) { + $stats_data = $cache->data; + } + else { + // Retrieve an array with both totalcount and daycount. + $stats_data = db_query('SELECT totalcount, daycount, timestamp FROM {node_counter} WHERE nid = :nid', array(':nid' => $nid), array('target' => 'slave'))->fetchAssoc(); + cache()->set('statistics_data', $stats_data); + } + } + return $stats_data; } } diff --git a/core/modules/statistics/statistics.php b/core/modules/statistics/statistics.php new file mode 100644 index 0000000..b0b7610 --- /dev/null +++ b/core/modules/statistics/statistics.php @@ -0,0 +1,28 @@ +key(array('nid' => $nid)) + ->fields(array( + 'daycount' => 1, + 'totalcount' => 1, + 'timestamp' => REQUEST_TIME, + )) + ->expression('daycount', 'daycount + 1') + ->expression('totalcount', 'totalcount + 1') + ->execute(); +} + +?> \ No newline at end of file