summaryrefslogtreecommitdiff
path: root/modules/statistics.module
diff options
context:
space:
mode:
Diffstat (limited to 'modules/statistics.module')
-rw-r--r--modules/statistics.module422
1 files changed, 213 insertions, 209 deletions
diff --git a/modules/statistics.module b/modules/statistics.module
index 333f27c73..5cb634fef 100644
--- a/modules/statistics.module
+++ b/modules/statistics.module
@@ -1,12 +1,13 @@
<?php
// $Id$
-function statistics_help($section = "admin/help#statistics") {
- $output = "";
-
+/**
+ * Implementation of hook_help().
+ */
+function statistics_help($section = 'admin/help#statistics') {
switch ($section) {
case 'admin/help#statistics':
- $output .= t("
+ return t("
<h3>Introduction</h3>
<p>The statistics module keeps track of numerous statistics for your site but be warned, statistical collection does cause a little overhead, thus everything comes <strong>disabled</strong> by default.<p>
<p>The module counts how many times, and from where -- using HTTP referrer -- each of your posts is viewed. Once we have that count the module can do the following with it:
@@ -45,60 +46,63 @@ function statistics_help($section = "admin/help#statistics") {
<li><em>administer statistics module</em> - enable for user roles that get to configure the statistics module.</li><li><em>administer statistics</em> - enable for user roles that get to view the referrer statistics.</li>
</ul>
<p>If '<em>administer statistics</em>' and '<em>access statistics</em>' are both enabled, the user will see a link from each node to that node's referrer statistics (if enabled).</p>",
- array("%modules" => url("admin/system/modules"), "%permissions" => url("admin/user/permission"), "%referer" => url("admin/logs/referrer"), "%access" => url("admin/logs/access"), "%configuration" => url("admin/system/modules/statistics"), "%block" => url("admin/system/block")));
+ array('%modules' => url('admin/system/modules'), '%permissions' => url('admin/user/permission'), '%referer' => url('admin/logs/referrer'), '%access' => url('admin/logs/access'), '%configuration' => url('admin/system/modules/statistics'), '%block' => url('admin/system/block')));
break;
case 'admin/system/modules#description':
- $output = t("Logs access statistics for your site.");
+ return t('Logs access statistics for your site.');
break;
case 'admin/system/modules/statistics':
- $output = t("Settings for the statistical information that Drupal will keep about the site. See <a href=\"%statistics\">site statistics</a> for the actual information.", array("%statistics" => url("admin/logs/topnodes")));
+ return t('Settings for the statistical information that Drupal will keep about the site. See <a href="%statistics">site statistics</a> for the actual information.', array('%statistics' => url('admin/logs/topnodes')));
break;
case 'admin/logs/topnodes':
- $output = t("This page gives you an at-a-glance look at your most popular content.");
+ return t('This page gives you an at-a-glance look at your most popular content.');
break;
case 'admin/logs/referrer':
- $output = t("This page shows you site-wide referrer statistics. You can see 'all referrers', 'external referrers' or 'internal referrers'. Referrers are web sites, both your site, and other peoples, that point to your web site.");
+ return t('This page shows your site-wide referrer statistics. You can see "all referrers", "external referrers" or "internal referrers". Referrers are web pages, both local and on other sites, that point to your web site.');
break;
case 'admin/logs/referrer/internal':
- $output = t("This page shows you only 'internal referrers'. Links pointing to your web site, from within your web site.");
+ return t('This page shows you only "internal referrers". These are links pointing to your web site from within your web site.');
break;
case 'admin/logs/referrer/external':
- $output = t("This page shows you only 'external referrers'. Links pointing to your web site from outside your web site.");
+ return t('This page shows you only "external referrers". These are links pointing to your web site from outside your web site.');
break;
case 'admin/logs/access':
case 'admin/logs/access/node':
case 'admin/logs/access/user':
case 'admin/logs/access/host':
- $output = t("This pages shows you who is accessing your web site. You can see the hostnames, referrers. In particular, it is easy to inspect a user's navigation history/trail by clicking on <em>track user</em>.");
+ return t("This page shows you who is accessing your web site. You can see the hostnames and referrers. In particular, it is easy to inspect a user's navigation history/trail by clicking on <em>track user</em>.");
break;
}
- return $output;
}
-// Exit hook, runs at the end of every page request
+/**
+ * Implementation of hook_exit().
+ *
+ * This is where statistics are gathered on page accesses.
+ */
function statistics_exit() {
global $user, $recent_activity;
- if (variable_get("statistics_count_content_views", 0)) {
- // we are counting content views
- if ((arg(0) == "node") && (arg(1) == "view") && arg(2)) {
- // a node has been viewed, so updated the node's counters
- db_query("UPDATE {node_counter} SET daycount = daycount + 1, totalcount = totalcount + 1, timestamp = %d WHERE nid = %d", time(), arg(2));
- // if we affected 0 rows, this is the first time viewing the node
+ if (variable_get('statistics_count_content_views', 0)) {
+ // We are counting content views.
+ if ((arg(0) == 'node') && (arg(1) == 'view') && arg(2)) {
+ // A node has been viewed, so update the node's counters.
+ db_query('UPDATE {node_counter} SET daycount = daycount + 1, totalcount = totalcount + 1, timestamp = %d WHERE nid = %d', time(), arg(2));
+ // If we affected 0 rows, this is the first time viewing the node.
if (!db_affected_rows()) {
- // must create a new row to store counter's for new node
- db_query("INSERT INTO {node_counter} (nid, daycount, totalcount, timestamp) VALUES(%d, 1, 1, %d)", arg(2), time());
+ // We must create a new row to store counters for the new node.
+ db_query('INSERT INTO {node_counter} (nid, daycount, totalcount, timestamp) VALUES(%d, 1, 1, %d)', arg(2), time());
}
}
}
- if ((variable_get("statistics_enable_access_log", 0)) && (module_invoke("throttle", "status") < 5)) {
- // statistical logs are enabled
+ if ((variable_get('statistics_enable_access_log', 0)) && (module_invoke('throttle', 'status') < 5)) {
+ // Statistical logs are enabled.
$referrer = referer_uri();
$hostname = $_SERVER['REMOTE_ADDR'];
- // log this page access
- if ((arg(0) == "node") && (arg(1) == "view") && arg(2)) {
+ // Log this page access.
+ if ((arg(0) == 'node') && (arg(1) == 'view') && arg(2)) {
db_query("INSERT INTO {accesslog} (nid, url, hostname, uid, timestamp) values(%d, '%s', '%s', %d, %d)", arg(2), $referrer, $hostname, $user->uid, time());
}
else {
@@ -107,16 +111,17 @@ function statistics_exit() {
}
}
-/* Permissions hook, defines module's permissions */
+/**
+ * Implementation of hook_perm().
+ *
+ * The following permissions are defined:
+ * - "administer statistics module": full administrative control of module
+ * - "administer statistics": view statistics / referrer log
+ * - "access statistics": see how many times individual nodes have been
+ * viewed (if enabled)
+ */
function statistics_perm() {
- /*
- ** statistics module defines the following permissions:
- ** administer statistics module - full administrative control of module
- ** administer statistics - view statistics / referrer log
- ** access statistics - see how many times individual content has
- ** been viewed (if enabled)
- */
- return array("administer statistics module", "administer statistics", "access statistics");
+ return array('administer statistics module', 'administer statistics', 'access statistics');
}
/**
@@ -147,67 +152,41 @@ function statistics_link($type, $node = 0, $main = 0) {
}
if ($type == 'system') {
+ menu('statistics', t('most popular content'), user_access('access content') ? 'statistics_page' : MENU_DENIED, 0, MENU_HIDE);
+
$access = user_access('administer statistics module') || user_access('administer statistics');
- menu('admin/logs/topnodes', t('top nodes'), $access ? 'statistics_admin' : MENU_DENIED, 1);
- menu('admin/logs/referrer', t('referrer'), $access ? 'statistics_admin' : MENU_DENIED, 2);
- menu('admin/logs/referrer/internal', t('internal referrers only'), $access ? 'statistics_admin' : MENU_DENIED);
- menu('admin/logs/referrer/external', t('external referrers only'), $access ? 'statistics_admin' : MENU_DENIED);
- menu('admin/logs/access', t('access'), $access ? 'statistics_admin' : MENU_DENIED, 3);
- menu('admin/logs/access/node', t('track node'), $access ? 'statistics_admin' : MENU_DENIED, 0, MENU_HIDE);
- menu('admin/logs/access/user', t('track user'), $access ? 'statistics_admin' : MENU_DENIED, 0, MENU_HIDE);
- menu('admin/logs/access/host', t('track host'), $access ? 'statistics_admin' : MENU_DENIED, 0, MENU_HIDE);
+ menu('admin/logs/topnodes', t('top nodes'), $access ? 'statistics_admin_topnodes' : MENU_DENIED, 1);
+ menu('admin/logs/referrer', t('referrer'), $access ? 'statistics_top_refer' : MENU_DENIED, 2);
+ menu('admin/logs/referrer/internal', t('internal referrers only'), MENU_FALLTHROUGH);
+ menu('admin/logs/referrer/external', t('external referrers only'), MENU_FALLTHROUGH);
+ menu('admin/logs/access', t('access'), $access ? 'statistics_admin_displaylog' : MENU_DENIED, 3);
}
return $links;
}
-/* Administration hook, defines module's administrative page */
-function statistics_admin() {
- $op = $_POST["op"];
- $edit = $_POST["edit"];
-
- if (empty($op)) {
- $op = arg(2);
- }
-
- /* non-configuration admin pages */
- switch ($op) {
- case "referrer":
- $output = statistics_top_refer();
- break;
- case "access":
- $output = statistics_admin_displaylog();
- break;
- case "topnodes":
- default:
- $output = statistics_admin_topnodes();
- }
-
- print theme("page", $output);
-}
-
function statistics_admin_topnodes_table() {
$header = array(
- array("data" => t("title"), "field" => "n.title"),
- array("data" => t("today"), "field" => "s.daycount", "sort" => "desc"),
- array("data" => t("all time"), "field" => "s.totalcount"),
- array("data" => t("last hit"), "field" => "s.timestamp"),
- array("data" => t("operations"))
+ array('data' => t('title'), 'field' => 'n.title'),
+ array('data' => t('today'), 'field' => 's.daycount', 'sort' => 'desc'),
+ array('data' => t('all time'), 'field' => 's.totalcount'),
+ array('data' => t('last hit'), 'field' => 's.timestamp'),
+ array('data' => t('operations'))
);
- $sql = "SELECT s.nid, s.daycount, s.totalcount, s.timestamp, n.title FROM {node_counter} s INNER JOIN {node} n ON s.nid = n.nid";
+ $sql = 'SELECT s.nid, s.daycount, s.totalcount, s.timestamp, n.title FROM {node_counter} s INNER JOIN {node} n ON s.nid = n.nid';
$sql .= tablesort_sql($header);
$result = pager_query($sql, 20); // WHERE s.%s <> '0'
while ($nid = db_fetch_array($result)) {
- $rows[] = array(l($nid["title"], "node/view/". $nid["nid"], array("title" => t("View this posting."))), $nid["daycount"], $nid["totalcount"], format_date($nid["timestamp"], "small"), l("track node", "admin/logs/access/node/$nid[nid]"));
+ $rows[] = array(l($nid['title'], 'node/view/'. $nid['nid'], array('title' => t('View this posting.'))), $nid['daycount'], $nid['totalcount'], format_date($nid['timestamp'], 'small'), l('track node', "admin/logs/access/node/$nid[nid]"));
}
- if ($pager = theme("pager", NULL, 20, 0, tablesort_pager())) {
- $rows[] = array(array("data" => $pager, "colspan" => 5));
+ if ($pager = theme('pager', NULL, 20, 0, tablesort_pager())) {
+ $rows[] = array(array('data' => $pager, 'colspan' => 5));
}
- return theme("table", $header, $rows);
+ return theme('table', $header, $rows);
}
function statistics_admin_accesslog_table($type, $id) {
@@ -216,16 +195,16 @@ function statistics_admin_accesslog_table($type, $id) {
/* retrieve user access logs */
if ($id) {
/* retrieve recent access logs for user $id */
- $sql = "SELECT a.nid, a.url, a.hostname, a.uid, a.timestamp, n.title FROM {accesslog} a LEFT JOIN {node} n ON a.nid = n.nid WHERE a.uid = ". check_query($id);
+ $sql = 'SELECT a.nid, a.url, a.hostname, a.uid, a.timestamp, n.title FROM {accesslog} a LEFT JOIN {node} n ON a.nid = n.nid WHERE a.uid = '. check_query($id);
}
else {
/* retrieve recent access logs for all users */
- $sql = "SELECT a.nid, a.url, a.hostname, a.uid, MAX(a.timestamp) AS timestamp, n.title FROM {accesslog} a LEFT JOIN {node} n ON a.nid = n.nid WHERE a.uid <> '0' GROUP BY a.uid, a.nid, a.url, a.hostname";
+ $sql = 'SELECT a.nid, a.url, a.hostname, a.uid, MAX(a.timestamp) AS timestamp, n.title FROM {accesslog} a LEFT JOIN {node} n ON a.nid = n.nid WHERE a.uid <> 0 GROUP BY a.uid, a.nid, a.url, a.hostname';
}
}
else if ($type == 2) {
/* retrieve recent access logs for node $id */
- $sql = "SELECT a.nid, a.url, a.hostname, a.uid, a.timestamp, n.title FROM {accesslog} a INNER JOIN {node} n ON a.nid = n.nid WHERE a.nid = ". check_query($id);
+ $sql = 'SELECT a.nid, a.url, a.hostname, a.uid, a.timestamp, n.title FROM {accesslog} a INNER JOIN {node} n ON a.nid = n.nid WHERE a.nid = '. check_query($id);
}
else if ($type == 3) {
/* retrieve recent access logs for hostname $id */
@@ -233,171 +212,188 @@ function statistics_admin_accesslog_table($type, $id) {
}
else {
/* retrieve all recent access logs */
- $sql = "SELECT a.nid, a.url, a.hostname, a.uid, a.timestamp, n.title FROM {accesslog} a LEFT JOIN {node} n ON a.nid = n.nid";
+ $sql = 'SELECT a.nid, a.url, a.hostname, a.uid, a.timestamp, n.title FROM {accesslog} a LEFT JOIN {node} n ON a.nid = n.nid';
}
$header = array(
- array("data" => t("timestamp"), "field" => "timestamp", "sort" => "desc"),
- array("data" => t("post"), "field" => "nid"),
- array("data" => t("user"), "field" => "uid"),
- array("data" => t("hostname"), "field" => "hostname"),
- array("data" => t("referrer"), "field" => "url"),
- array("data" => t("operations"), "colspan" => "3")
+ array('data' => t('timestamp'), 'field' => 'timestamp', 'sort' => 'desc'),
+ array('data' => t('post'), 'field' => 'nid'),
+ array('data' => t('user'), 'field' => 'uid'),
+ array('data' => t('hostname'), 'field' => 'hostname'),
+ array('data' => t('referrer'), 'field' => 'url'),
+ array('data' => t('operations'), 'colspan' => '3')
);
$sql .= tablesort_sql($header);
$result = pager_query($sql, 50);
while ($log = db_fetch_object($result)) {
- $user = user_load(array("uid" => $log->uid));
+ $user = user_load(array('uid' => $log->uid));
if ($log->url) {
- $url = "<a href=\"$log->url\" title=\"$log->url\">". (strlen($log->url) > 28 ? substr($log->url, 0, 28) . '...' : $log->url) ."</a>";
+ $url = "<a href=\"$log->url\" title=\"$log->url\">". (strlen($log->url) > 28 ? substr($log->url, 0, 28) . '...' : $log->url) .'</a>';
}
else {
$url = message_na();
}
- $rows[] = array(array("data" => format_date($log->timestamp, "small"), "nowrap" => "nowrap"), ($log->nid ? l($log->title, "node/view/$log->nid") : message_na()), format_name($user), $log->hostname ? $log->hostname : message_na(), $url, ($log->nid ? l(t("track node"), "admin/logs/access/node/$log->nid") : ""), ($user->uid ? l(t("track user"), "admin/logs/access/user/$user->uid") : ""), ($log->hostname ? l(t("track host"), "admin/logs/access/host/$log->hostname") : ""));
+ $rows[] = array(array('data' => format_date($log->timestamp, 'small'), 'nowrap' => 'nowrap'), ($log->nid ? l($log->title, "node/view/$log->nid") : message_na()), format_name($user), $log->hostname ? $log->hostname : message_na(), $url, ($log->nid ? l(t('track node'), "admin/logs/access/node/$log->nid") : ''), ($user->uid ? l(t('track user'), "admin/logs/access/user/$user->uid") : ''), ($log->hostname ? l(t('track host'), "admin/logs/access/host/$log->hostname") : ''));
}
- if ($pager = theme("pager", NULL, 50, 0, tablesort_pager())) {
- $rows[] = array(array("data" => $pager, "colspan" => 8));
+ if ($pager = theme('pager', NULL, 50, 0, tablesort_pager())) {
+ $rows[] = array(array('data' => $pager, 'colspan' => 8));
}
- return theme("table", $header, $rows);
+ return theme('table', $header, $rows);
}
-function statistics_top_refer() {
-
- $view = arg(3) ? arg(3) : "all";
-
- if ($view == "all") {
+/**
+ * Menu callback. Presents the "Top referrers" page.
+ *
+ * @param $view
+ * - "internal": Only display internal links.
+ * - "external": Only display links from off-site.
+ * - "all": Display all referrers.
+ */
+function statistics_top_refer($view = 'all') {
+ if ($view == 'all') {
$query = "SELECT url, MAX(timestamp) AS last_view, COUNT(url) AS count FROM {accesslog} WHERE url <> '' GROUP BY url";
$query_cnt = "SELECT COUNT(DISTINCT(url)) FROM {accesslog} WHERE url <> ''";
+ $describe = t('Top referrers of the past %interval');
}
- elseif ($view == "internal") {
- $query = "SELECT url, MAX(timestamp) AS last_view, COUNT(url) AS count FROM {accesslog} WHERE url LIKE '%". check_query($_SERVER["HTTP_HOST"]) ."%' GROUP BY url";
- $query_cnt = "SELECT COUNT(DISTINCT(url)) FROM {accesslog} WHERE url <> '' AND url LIKE '%". check_query($_SERVER["HTTP_HOST"]) ."%'";
- $describe = t("Top internal referrers of the past %interval");
+ elseif ($view == 'internal') {
+ $query = "SELECT url, MAX(timestamp) AS last_view, COUNT(url) AS count FROM {accesslog} WHERE url LIKE '%". check_query($_SERVER['HTTP_HOST']) ."%' GROUP BY url";
+ $query_cnt = "SELECT COUNT(DISTINCT(url)) FROM {accesslog} WHERE url <> '' AND url LIKE '%". check_query($_SERVER['HTTP_HOST']) ."%'";
+ $describe = t('Top internal referrers of the past %interval');
}
else {
/* default to external */
- $query = "SELECT url, MAX(timestamp) AS last_view, COUNT(url) AS count FROM {accesslog} WHERE url NOT LIKE '%". check_query($_SERVER["HTTP_HOST"]) ."%' AND url <> '' GROUP BY url";
- $query_cnt = "SELECT COUNT(DISTINCT(url)) FROM {accesslog} WHERE url <> '' AND url NOT LIKE '%". check_query($_SERVER["HTTP_HOST"]) ."%'";
- $describe = t("Top external referrers of the past %interval");
+ $query = "SELECT url, MAX(timestamp) AS last_view, COUNT(url) AS count FROM {accesslog} WHERE url NOT LIKE '%". check_query($_SERVER['HTTP_HOST']) ."%' AND url <> '' GROUP BY url";
+ $query_cnt = "SELECT COUNT(DISTINCT(url)) FROM {accesslog} WHERE url <> '' AND url NOT LIKE '%". check_query($_SERVER['HTTP_HOST']) ."%'";
+ $describe = t('Top external referrers of the past %interval');
}
- $output = "<h3>". strtr($describe, array("%interval" => format_interval(variable_get("statistics_flush_accesslog_timer", 259200)))) ."</h3>";
+ $title = strtr($describe, array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200))));
$header = array(
- array("data" => t("URL"), "field" => "url"),
- array("data" => t("last view"), "field" => "last_view"),
- array("data" => t("count"), "field" => "count", "sort" => "desc")
+ array('data' => t('URL'), 'field' => 'url'),
+ array('data' => t('last view'), 'field' => 'last_view'),
+ array('data' => t('count'), 'field' => 'count', 'sort' => 'desc')
);
$query .= tablesort_sql($header);
$result = pager_query($query, 50, 0, $query_cnt);
while ($referrer = db_fetch_array($result)) {
- $rows[] = array("<a href=\"". $referrer["url"] ."\">". substr($referrer["url"], 0, 100) ."</a>", format_date($referrer["last_view"], "small"), $referrer["count"]);
+ $rows[] = array('<a href="'. $referrer['url'] .'">'. substr($referrer['url'], 0, 100) .'</a>', format_date($referrer['last_view'], 'small'), $referrer['count']);
}
- if ($pager = theme("pager", NULL, 50, 0, tablesort_pager())) {
- $rows[] = array(array("data" => $pager, "colspan" => 3));
+ if ($pager = theme('pager', NULL, 50, 0, tablesort_pager())) {
+ $rows[] = array(array('data' => $pager, 'colspan' => 3));
}
- $output .= theme("table", $header, $rows);
-
- return $output;
+ $output .= theme('table', $header, $rows);
+ print theme('page', $output, $title);
}
-
+/**
+ * Menu callback. Presents the "Most Popular Content" page.
+ */
function statistics_admin_topnodes() {
-
- $output = "<h3>". t("Popular content") ."</h3>\n";
- $output .= statistics_admin_topnodes_table();
-
- return $output;
+ print theme('page', statistics_admin_topnodes_table());
}
-function statistics_admin_displaylog() {
-
- $type = arg(3);
- $value = arg(4);
-
+/**
+ * Menu callback. Presents the "Access logs" page.
+ *
+ * @param $type
+ * - "user": display accesses for a particular user.
+ * - "host": display accesses originated at a given IP.
+ * - "node": display only accesses of a particular node.
+ * - "all": display all accesses.
+ *
+ * @param $value
+ * The user, host, or node to filter by.
+ */
+function statistics_admin_displaylog($type = 'all', $value = 0) {
switch ($type) {
- case "user":
- $user = user_load(array("uid" => $value));
- $output = "<h3>". t("Recent access logs for '%name'", array("%name" => $user->name)) ."</h3>\n";
- $output .= statistics_admin_accesslog_table(1, $user->uid);
+ case 'user':
+ $user = user_load(array('uid' => $value));
+ $title = t('Recent access logs for "%name"', array('%name' => $user->name));
+ $output = statistics_admin_accesslog_table(1, $user->uid);
break;
- case "node":
- $node = node_load(array("nid" => $value));
- $output = "<h3>". t("Recent access logs for '%title'", array("%title" => $node->title)) ."</h3>\n";
- $output .= statistics_admin_accesslog_table(2, $value);
+ case 'node':
+ $node = node_load(array('nid' => $value));
+ $title = t('Recent access logs for "%title"', array('%title' => $node->title));
+ $output = statistics_admin_accesslog_table(2, $value);
break;
- case "host":
- $output = "<h3>". t("Recent access logs for '%hostname'", array("%hostname" => $value)) ."</h3>\n";
- $output .= statistics_admin_accesslog_table(3, $value);
+ case 'host':
+ $title = t('Recent access logs for "%hostname"', array('%hostname' => $value));
+ $output = statistics_admin_accesslog_table(3, $value);
break;
default:
- $output = "<h3>". t("Recent access logs") ."</h3>\n";
- $output .= statistics_admin_accesslog_table(0, 0);
+ $title = t('Recent access logs');
+ $output = statistics_admin_accesslog_table(0, 0);
}
- return $output;
+ print theme('page', $output, $title);
}
-// settings for the statistics module
+/**
+ * Implementation of hook_settings().
+ */
function statistics_settings() {
// access log settings:
- $group = form_radios(t("Enable access log"), "statistics_enable_access_log", variable_get("statistics_enable_access_log", 0), array("1" => t("Enabled"), "0" => t("Disabled")), t("Log each page access. Required for referrer statistics."));
+ $group = form_radios(t('Enable access log'), 'statistics_enable_access_log', variable_get('statistics_enable_access_log', 0), array('1' => t('Enabled'), '0' => t('Disabled')), t('Log each page access. Required for referrer statistics.'));
- $period = drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800), "format_interval");
- $group .= form_select(t("Discard access logs older than"), "statistics_flush_accesslog_timer", variable_get("statistics_flush_accesslog_timer", 259200), $period, t("Older access log entries (including referrer statistics) will be automatically discarded. Requires crontab."));
- $output = form_group(t("Access log settings"), $group);
+ $period = drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800), 'format_interval');
+ $group .= form_select(t('Discard access logs older than'), 'statistics_flush_accesslog_timer', variable_get('statistics_flush_accesslog_timer', 259200), $period, t('Older access log entries (including referrer statistics) will be automatically discarded. Requires crontab.'));
+ $output = form_group(t('Access log settings'), $group);
// count content views settings
- $group = form_radios(t("Count content views"), "statistics_count_content_views", variable_get("statistics_count_content_views", 0), array("1" => t("Enabled"), "0" => t("Disabled")), t("Increment a counter each time content is viewed."));
- $group .= form_radios(t("Display counter values"), "statistics_display_counter", variable_get("statistics_display_counter", 0), array("1" => t("Enabled"), "0" => t("Disabled")), t("Display how many times given content has been viewed. User must have the \"access statistics\" permissions to be able to view these counts."));
- $output .= form_group(t("Content viewing counter settings"), $group);
+ $group = form_radios(t('Count content views'), 'statistics_count_content_views', variable_get('statistics_count_content_views', 0), array('1' => t('Enabled'), '0' => t('Disabled')), t('Increment a counter each time content is viewed.'));
+ $group .= form_radios(t('Display counter values'), 'statistics_display_counter', variable_get('statistics_display_counter', 0), array('1' => t('Enabled'), '0' => t('Disabled')), t('Display how many times given content has been viewed. User must have the "access statistics" permissions to be able to view these counts.'));
+ $output .= form_group(t('Content viewing counter settings'), $group);
// Popular content block settings
- $numbers = array("0" => t("Disabled")) + drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 40));
- $group = form_select(t("Number of day's top views to display"), "statistics_block_top_day_num", variable_get("statistics_block_top_day_num", 0), $numbers, t("How many content items to display in \"day\" list. Requires enabled content viewing counters."));
- $group .= form_select(t("Number of all time views to display"), "statistics_block_top_all_num", variable_get("statistics_block_top_all_num", 0), $numbers, t("How many content items to display in \"all time\" list. Requires enabled content viewing counters."));
- $group .= form_select(t("Number of most recent views to display"), "statistics_block_top_last_num", variable_get("statistics_block_top_last_num", 0), $numbers, t("How many content items to display in \"recently viewed\" list. Requires enabled content viewing counters."));
- $output .= form_group(t("\"Popular content\" block settings"), $group);
+ $numbers = array('0' => t('Disabled')) + drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 40));
+ $group = form_select(t("Number of day's top views to display"), 'statistics_block_top_day_num', variable_get('statistics_block_top_day_num', 0), $numbers, t('How many content items to display in "day" list. Requires enabled content viewing counters.'));
+ $group .= form_select(t('Number of all time views to display'), 'statistics_block_top_all_num', variable_get('statistics_block_top_all_num', 0), $numbers, t('How many content items to display in "all time" list. Requires enabled content viewing counters.'));
+ $group .= form_select(t('Number of most recent views to display'), 'statistics_block_top_last_num', variable_get('statistics_block_top_last_num', 0), $numbers, t('How many content items to display in "recently viewed" list. Requires enabled content viewing counters.'));
+ $output .= form_group(t('"Popular content" block settings'), $group);
// Popular content page settings
- $numbers = array("0" => t("Disabled")) + drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25));
- $group = form_textfield(t("Name for link to user page"), "statistics_userpage_link", variable_get("statistics_userpage_link", ""), 20, 40, t("This node generates a user page listing your site's most popular content. If you specify a name here, a link to the \"Popular content\" page will be added automatically."));
- $group .= form_select(t("Number of day's top views to display"), "statistics_userpage_day_cnt", variable_get("statistics_userpage_day_cnt", 0), $numbers, t("How many content items to display in the \"day\" list. Requires enabled content viewing counters."));
- $group .= form_select(t("Number of all time top views to display"), "statistics_userpage_all_cnt", variable_get("statistics_userpage_all_cnt", 0), $numbers, t("How many content items to display in the \"all time\" list. Requires enabled content viewing counters."));
- $group .= form_select(t("Number of most recent views to display"), "statistics_userpage_last_cnt", variable_get("statistics_userpage_last_cnt", 0), $numbers, t("How many posts to display in the \"recently viewed\" list. Requires enabled content viewing counters."));
- $output .= form_group(t("\"Popular content\" page settings"), $group);
+ $numbers = array('0' => t('Disabled')) + drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25));
+ $group = form_textfield(t('Name for link to user page'), 'statistics_userpage_link', variable_get('statistics_userpage_link', ''), 20, 40, t("This node generates a user page listing your site's most popular content. If you specify a name here, a link to the \"Popular content\" page will be added automatically."));
+ $group .= form_select(t("Number of day's top views to display"), 'statistics_userpage_day_cnt', variable_get('statistics_userpage_day_cnt', 0), $numbers, t('How many content items to display in the "day" list. Requires enabled content viewing counters.'));
+ $group .= form_select(t('Number of all time top views to display'), 'statistics_userpage_all_cnt', variable_get('statistics_userpage_all_cnt', 0), $numbers, t('How many content items to display in the "all time" list. Requires enabled content viewing counters.'));
+ $group .= form_select(t('Number of most recent views to display'), 'statistics_userpage_last_cnt', variable_get('statistics_userpage_last_cnt', 0), $numbers, t('How many posts to display in the "recently viewed" list. Requires enabled content viewing counters.'));
+ $output .= form_group(t('"Popular content" page settings'), $group);
return $output;
}
-/* Saves the values entered in the "config statistics" administration form */
+/**
+ * Saves the values entered in the "config statistics" administration form.
+ */
function statistics_save_statistics($edit) {
- variable_set("statistics_display_counter", $edit["statistics_display_counter"]);
+ variable_set('statistics_display_counter', $edit['statistics_display_counter']);
}
-/* cron hook, performs automatic functions */
+/**
+ * Implementation of hook_cron().
+ */
function statistics_cron() {
- $statistics_timestamp = variable_get("statistics_day_timestamp", "");
+ $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());
+ db_query('UPDATE {node_counter} SET daycount = 0');
+ variable_set('statistics_day_timestamp', time());
}
/* clean expired access logs */
- db_query("DELETE FROM {accesslog} WHERE ". time() ." - timestamp > ". variable_get("statistics_flush_accesslog_timer", 259200));
+ db_query('DELETE FROM {accesslog} WHERE '. time() .' - timestamp > '. variable_get('statistics_flush_accesslog_timer', 259200));
}
/**
@@ -417,7 +413,7 @@ function statistics_cron() {
* or FALSE if the query could not be executed correctly.
*/
function statistics_title_list($dbfield, $dbrows) {
- return db_query_range("SELECT s.nid, n.title, u.uid, u.name FROM {node_counter} s INNER JOIN {node} n ON s.nid = n.nid INNER JOIN {users} u ON n.uid = u.uid WHERE %s <> '0' AND n.status = 1 ORDER BY %s DESC", "s.". $dbfield, "s.". $dbfield, 0, $dbrows);
+ return db_query_range("SELECT s.nid, n.title, u.uid, u.name FROM {node_counter} s INNER JOIN {node} n ON s.nid = n.nid INNER JOIN {users} u ON n.uid = u.uid WHERE %s <> '0' AND n.status = 1 ORDER BY %s DESC", 's.'. $dbfield, 's.'. $dbfield, 0, $dbrows);
}
/**
@@ -437,43 +433,45 @@ function statistics_get($nid) {
if ($nid > 0) {
/* retrieves an array with both totalcount and daycount */
- $statistics = db_fetch_array(db_query("SELECT totalcount, daycount, timestamp FROM {node_counter} WHERE nid = %d", $nid));
+ $statistics = db_fetch_array(db_query('SELECT totalcount, daycount, timestamp FROM {node_counter} WHERE nid = %d', $nid));
}
return $statistics;
}
-/* Block hook */
-function statistics_block($op = "list", $delta = 0) {
- if ($op == "list") {
- if (variable_get("statistics_count_content_views", 0)) {
- $blocks[0]["info"] = t("Popular content");
+/**
+ * Implementation of hook_block().
+ */
+function statistics_block($op = 'list', $delta = 0) {
+ if ($op == 'list') {
+ if (variable_get('statistics_count_content_views', 0)) {
+ $blocks[0]['info'] = t('Popular content');
}
return $blocks;
}
- else if (user_access("access content")) {
+ else if (user_access('access content')) {
switch ($delta) {
case 0:
$content = array();
- $daytop = variable_get("statistics_block_top_day_num", 0);
+ $daytop = variable_get('statistics_block_top_day_num', 0);
if ($daytop) {
- $content[] = node_title_list(statistics_title_list("daycount", $daytop), t("Today's:"));
+ $content[] = node_title_list(statistics_title_list('daycount', $daytop), t("Today's:"));
}
- $alltimetop = variable_get("statistics_block_top_all_num", 0);
+ $alltimetop = variable_get('statistics_block_top_all_num', 0);
if ($alltimetop) {
- $content[] = node_title_list(statistics_title_list("totalcount", $alltimetop), t("All time:"));
+ $content[] = node_title_list(statistics_title_list('totalcount', $alltimetop), t('All time:'));
}
- $lasttop = variable_get("statistics_block_top_last_num", 0);
+ $lasttop = variable_get('statistics_block_top_last_num', 0);
if ($lasttop) {
- $content[] = node_title_list(statistics_title_list("timestamp", $lasttop), t("Last viewed:"));
+ $content[] = node_title_list(statistics_title_list('timestamp', $lasttop), t('Last viewed:'));
}
- $output = implode($content, "<br />");
+ $output = implode($content, '<br />');
- $block["subject"] = variable_get("statistics_block_top_title", t("Popular content"));
- $block["content"] = $output;
+ $block['subject'] = variable_get('statistics_block_top_title', t('Popular content'));
+ $block['content'] = $output;
break;
}
@@ -481,61 +479,67 @@ function statistics_block($op = "list", $delta = 0) {
}
}
+/**
+ * Menu callback. Presents the "Top Nodes" summary page.
+ */
function statistics_page() {
- $output = "";
+ $output = '';
// build day's most popular content list if enabled
- if ($displaycount = variable_get("statistics_userpage_day_cnt", 0)) {
- $table = "<table border=\"0\" cellpadding=\"4\" cellspacing=\"4\" style=\"width: 100%;\">";
- $table .= statistics_summary("daycount", $displaycount);
- $table .= "</table>";
+ if ($displaycount = variable_get('statistics_userpage_day_cnt', 0)) {
+ $table = '<table border="0" cellpadding="4" cellspacing="4" style="width: 100%;">';
+ $table .= statistics_summary('daycount', $displaycount);
+ $table .= '</table>';
- $output .= theme("box", t("Day's most popular content:"), $table, "main");
+ $output .= theme('box', t("Day's most popular content:"), $table, 'main');
}
// build all time most popular content list if enabled
- if ($displaycount = variable_get("statistics_userpage_all_cnt", 0)) {
- $table = "<table border=\"0\" cellpadding=\"4\" cellspacing=\"4\" style=\"width: 100%;\">";
- $table .= statistics_summary("totalcount", $displaycount);
- $table .= "</table>";
+ if ($displaycount = variable_get('statistics_userpage_all_cnt', 0)) {
+ $table = '<table border="0" cellpadding="4" cellspacing="4" style="width: 100%;">';
+ $table .= statistics_summary('totalcount', $displaycount);
+ $table .= '</table>';
- $output .= theme("box", t("All time most popular content:"), $table, "main");
+ $output .= theme('box', t('All time most popular content:'), $table, 'main');
}
// build last viewed content list if enabled
- if ($displaycount = variable_get("statistics_userpage_last_cnt", 0)) {
- $table = "<table border=\"0\" cellpadding=\"4\" cellspacing=\"4\" style=\"width: 100%;\">";
- $table .= statistics_summary("timestamp", $displaycount);
- $table .= "</table>";
+ if ($displaycount = variable_get('statistics_userpage_last_cnt', 0)) {
+ $table = '<table border="0" cellpadding="4" cellspacing="4" style="width: 100%;">';
+ $table .= statistics_summary('timestamp', $displaycount);
+ $table .= '</table>';
- $output .= theme("box", t("Last viewed content:"), $table, "main");
+ $output .= theme('box', t('Last viewed content:'), $table, 'main');
}
- print theme("page", $output);
+ print theme('page', $output);
}
function statistics_summary($dbfield, $dbrows) {
/* valid dbfields: totalcount, daycount, timestamp */
- $output = "";
- $result = db_query_range("SELECT n.nid, n.title FROM {node_counter} s INNER JOIN {node} n ON s.nid = n.nid ORDER BY %s DESC", $dbfield, 0, $dbrows);
+ $output = '';
+ $result = db_query_range('SELECT n.nid, n.title FROM {node_counter} s INNER JOIN {node} n ON s.nid = n.nid ORDER BY %s DESC', $dbfield, 0, $dbrows);
while ($nid = db_fetch_array($result)) {
- $content = node_load(array("nid" => $nid["nid"]));
+ $content = node_load(array('nid' => $nid['nid']));
$links = link_node($content, 1);
- $output .= "<tr><td><strong>". l($nid["title"], "node/view/". $nid["nid"], array("title" => t("View this posting."))) ."</strong></td><td style=\"text-align: right;\"><small>". t("Submitted by %a on %b", array("%a" => format_name($content), "%b" => format_date($content->created, "large"))) ."</small></td></tr>";
- $output .= "<tr><td colspan=\"2\"><div style=\"margin-left: 20px;\">". check_output($content->teaser) ."</div></td></tr>";
- $output .= "<tr><td style=\"text-align: right;\" colspan=\"2\">[ ". theme("links", $links) ." ]<br /><br /></td></tr>";
+ $output .= '<tr><td><strong>'. l($nid['title'], 'node/view/'. $nid['nid'], array('title' => t('View this posting.'))) .'</strong></td><td style="text-align: right;"><small>'. t('Submitted by %a on %b', array('%a' => format_name($content), '%b' => format_date($content->created, 'large'))) .'</small></td></tr>';
+ $output .= '<tr><td colspan="2"><div style="margin-left: 20px;">'. check_output($content->teaser) .'</div></td></tr>';
+ $output .= '<tr><td style="text-align: right;" colspan="2">[ '. theme('links', $links) .' ]<br /><br /></td></tr>';
}
return $output;
}
+/**
+ * Implementation of hook_nodeapi().
+ */
function statistics_nodeapi(&$node, $op, $arg = 0) {
switch ($op) {
- case "delete":
+ case 'delete':
// clean up statistics table when node is deleted
- db_query("DELETE FROM {node_counter} WHERE nid = %d", $node->nid);
+ db_query('DELETE FROM {node_counter} WHERE nid = %d', $node->nid);
}
}