drupal/modules/statistics.module

958 lines
50 KiB
Plaintext
Raw Normal View History

<?php
global $id, $mod, $nid, $user, $recent_activity;
if (variable_get("statistics_enable_node_counter", 0)) {
/* node view counters are enabled */
2002-10-24 17:45:37 +00:00
if (isset($id) && empty($mod)) {
/* a node has been viewed, so updated the node's counters */
db_query("UPDATE statistics SET daycount = daycount + 1, totalcount = totalcount + 1, timestamp = '%s' WHERE nid = '%s'", time(), $id);
/* 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 statistics (nid, daycount, totalcount) VALUES('%s', daycount + 1, totalcount + 1)", $id);
}
}
}
if ((variable_get("statistics_enable_access_log", 0)) && (throttle_status() < 5)) {
/* statistical logs are enabled */
$referrer = getenv("HTTP_REFERER");
$hostname = getenv("REMOTE_ADDR");
/* log this page access */
db_query("INSERT INTO accesslog (nid, url, hostname, uid, timestamp) values('%s', '%s', '%s', '%s', '%s')", $id, $referrer, $hostname, $user->uid, time());
}
/*
** The following logic determines what the current throttle level should
** be, and can be disabled by the admin. If enabled, the rand() function
** returns a number between 0 and N, N being specified by the admin. If
** 0 is returned, the throttle logic is run, adding on additional database
** query. Otherwise, the following logic is skipped. This mechanism is
** referred to in the admin page as the 'probability limiter', roughly
** limiting throttle related database calls to 1 in N.
*/
if ((variable_get("statistics_enable_auto_throttle", 0)) && (!rand(0, variable_get("statistics_probability_limiter", 9)))) {
/*
** Note: The rand() function is supported by PHP 3+. However, prior to
** PHP 4.2.0 it needs to be seeded with a call to srand(). It is important
** that this only happens once, so this should be managed by the Drupal
** engine, not this module. The Drupal engine should use phpversion() to
** detect and automatically seed pre-4.2.0 systems.
*/
$throttle = throttle_status();
/* if we're at throttle level 5, we don't do anything */
if ($throttle < 5) {
$multiplier = variable_get("statistics_throttle_multiplier", 60);
/* count all hits in past sixty seconds */
$result = db_query("SELECT COUNT(timestamp) AS hits FROM accesslog WHERE timestamp >= %s", (time() - 60));
$recent_activity = db_fetch_array($result);
throttle_update($recent_activity["hits"]);
}
}
/* System hook, sets description of module in admin page */
function statistics_system($field) {
$system["description"] = t("Logs access statistics for your site.");
return $system[$field];
}
/* Permissions hook, defines module's permissions */
function statistics_perm() {
/*
** statistics module defines the following permissions:
** administer statistics module - full administrative control of module
** administer statistics - view statistics / referrers
** access statistics - see counts per node (if enabled)
** access userlist - see list of online users
*/
return array("administer statistics module", "administer statistics", "access statistics", "access userlist");
}
/* Link hook, defines module's links */
function statistics_link($type, $node = 0, $main = 0) {
global $id;
if ($type == "admin" && (user_access("administer statistics module") || (user_access("administer statistics")))) {
$links[] = la(t("statistics"), array("mod" => "statistics"));
}
if ($type == "node" && user_access("access statistics") && variable_get("statistics_display_counter", 0)) {
$statistics = statistics_get($node->nid);
if ($statistics) {
if (user_access("administer statistics")) {
$links[] = la(format_plural($statistics[totalcount], t("read"), t("reads")), array("mod" => "statistics", "op" => "referrers", "nid" => $node->nid));
}
else {
$links[] = format_plural($statistics[totalcount], t("read"), t("reads"));
}
}
}
if ($type == "page" && user_access("access content")) {
$userlink = variable_get("statistics_userpage_link", "");
if ($userlink) {
$links[] = lm(t($userlink), array("mod" => "statistics"), "", array("title" => t("View the top nodes for this site.")));
}
}
return $links ? $links : array();
}
/* Administrative help page */
function statistics_help() {
?>
<h3>Introduction</h3>
<p>This Drupal module keeps track of numerous statistics for your site. Be warned, statistical collection does cause a little overhead, thus everything is disabled by default when this module is first installed.</p>
<p>The statistics module counts how many times each of your nodes is viewed, also tracking where each link to the node came from (HTTP referrer). The number of times a node has been viewed can be displayed in the node's link section (next to "# comments" etc).</p>
<p>This module also adds a configurable block that can display the day's top stories, the all time top stories, and the last stories read. Each of these supports a configurable header and number of stories.</p>
<p>This module also adds a configurable user page that can display the day's top stories, the all time top stories, and the last stories read. Each of these supports a configurable header and number of stories.</p>
<p>This module also adds a configurable block that displays counts of how many users and guests are currently accessing your site, as well as a list of the names of the users currently accessing your site.</p>
<p>If you enable the node view counters, this adds 1 database query for each node that is viewed (2 queries if it's the first time the node has ever been viewed).</p>
<p>Finally, the statistics.module allows for a congestion controlling auto-throttle mechanism. If you have enabled the throttle.module, you can read more about this mechanism <?php print l("here", array("mod" => "help"), "admin", "throttle"); ?>.</p>
<p>If you enable the access log, this adds 1 database query for each page that Drupal displays. Logged information includes: HTTP referrer (if any), node being accessed (if any), user ID (if any), the IP address of the user, and the time the page was viewed.</p>
<p>As with any new module, <i>statistics.module</i> needs to be enabled <?php print l("here", array("mod" => "system", "op" => "modules"), "admin"); ?> before you can use it. Also refer to the permissions section below, as this module supports four separate permissions.</p>
<h3>View statistics</h3>
<p>This admin page gives you an at-a-glance look at your top nodes. It is useful for understanding what content on your Drupal site is the most popular. Also on this page are links to the referrer statistics for each listed node.</p>
<h3>View referrers</h3>
<p>This admin page shows you site-wide referrer statistics. You can see '<i>all</i>' statistics, '<i>external</i>' statistics or '<i>internal</i>' statistics. Default is 'external'.</p>
<h3>Configuring the statistics module</h3>
<p>There are a couple of configuration options added to the main <?php print l("site configuration", array("mod" => "system"), "admin"); ?> admin page.
<p>The first option, <i>enable access log</i>, allows you to turn the access log on and off. This log is used to store things like referrers and who's online. Enabling the log adds one database call per page displayed by Drupal.</p>
<p>The second option, <i>discard access logs older than</i>, allows you to configure how long an access log entry is saved, after which time it is deleted from the database table.</p>
<p>The next option, <i>enable node view counter</i>, allows you to turn on and off the node-counting functionality of this module. If it is turned on, an extra database query is added for each node displayed, as a counter is incremented with each node view.</p>
<p>The next option, <i>display node view counters</i>, allows you to globally disable the displaying of node view counters. Additionally, a user group must have 'access statistics' permissions to view the counters.</p>
<p>The final option is to <i>reset the day counter</i>. Every twenty four hours, all the day's totals are automatically reset to 0, and started again. Whatever time you click this link is the time each day that the day's totals will be reset. This requires cron. Note that clicking this link will reload the site configuration page <i>without</i> saving any other changes you might have made.</p>
<h3>Top nodes block</h3>
<p>This module creates a block that can display the day's top viewed nodes, the all time top viewed nodes and the last nodes viewed. Each of these links can be enabled or disabled individually, and the number of nodes displayed for each can be configured with a drop down menu. If you disable all sections of this block, it will not appear.</p>
<p>The administrative "top nodes block" screen also allows you to assign a name to the block.</p>
<p>Don't forget to enable the block <?php print l("here", array("mod" => "block"), "admin"); ?>.</p>
<h3>Top nodes page</h3>
<p>This module creates a user page that can display summaries of the day's top viewed nodes, the all time top nodes and the last nodes viewed. Each of these summaries can be enabled or disabled individually, and the number of nodes displayed for each can be configured with a drop down menu.</p>
<p>The administrative "top nodes page" screen also allows you to assign a name for the automatically generated link to the user page. If no name is set, the link will not be displayed.</p>
<h3>Who's online block</h3>
<p>This module creates a block that can display how many user's and guests are currently online. You are able to configure the name of the block, the name of a sub-block for displaying names of user's currently online, how recently a user must have been active to be considered online, the maximum characters to display from a user's name and the maximum number of user names to display.</p>
<p>Don't forget to enable the block <?php print l("here", array("mod" => "block"), "admin"); ?>.</p>
<h3>Permissions</h3>
<p>This module has four permissions that need to be configured in
<?php print l("user permissions", array("mod" => "user", "op" => "permission"), "admin"); ?>.</p>
<ul>
<li><i>access statistics</i> - enable for user roles that get to see individual node counts. (This does not define access to the block)</li>
<li><i>access userlist</i> - enable for user roles that get to see the list of user's that are currently online within the "Who's online" block.</li>
<li><i>administer statistics module</i> - enable for user roles that get to configure the statistics module.</li>
<li><i>administer statistics</i> - enable for user roles that get to view the referrer statistics.</li>
</ul>
If '<i>administer statistics</i>' and '<i>access statistics</i>' are both enabled, the user will see a link from each node to that node's referrer statistics (if enabled).
<h2>Statistics module (for developers)</h2>
<h3>Accessing statistics</h3>
<p>To get a node's view statistics make a call to the function <i>statistics_get($nid)</i>. When you pass in a $nid, the function returns an array with three entires: [0]=totalcount, [1]=daycount, [2]=timestamp. For example, you could use this function call to add node view counts to your theme.</p>
<ul>
<li>The <i>totalcount</i> is a count of the total number of times that node has been viewed.</li>
<li>The <i>daycount</i> is a count of the total number of times that node has been viewed "today". For the daycount to be reset, cron must be enabled.</li>
<li>The <i>timestamp</i> is a timestamp of when that node was last viewed.</li>
</ul>
<p>The module automatically adds '# reads' to each node's link section (if enabled).</p>
<h3>Top stories</h3>
<p>The statistics.module provides a function '<i>statistics_title_list($type)</i>' to return an array of links to any of the following: the top viewed nodes of all time, the top viewed nodes of today, the last viewed nodes. You can pass in:
<ul>
<li><i>totalcount</i> - This will return an array with links to the top viewed nodes of all time.<br />
Example: <code>statistics_title_list("totalcount");</code><br /><br /></li>
<li><i>daycount</i> - This will return an array with links to the top viewed nodes for today.<br />
Example: <code>statistics_title_list("daycount");</code><br /><br /></li>
<li><i>timestamp</i> - This will return a array with links to the last viewed node.<br />
Example: <code>statistics_title_list("timestamp");</code></li>
</ul>
<h3>Throttle</h3>
<p>The function <code>throttle_status()</code> will return a number from 0 to 5. 0 means that there is no throttle enabled at this time. Each number above that is a progressively more throttled system... To disable a feature when a site first begins to get busy, disable it at a throttle of 2 or 3. To hold on to the bitter end, wait until 4 or 5.</p>
<p>To implement the throttle, you should do something like this:
<pre> $throttle = 0;
/* verify that the statitistics module is installed */
if (function_exists(throttle_status) {
$throttle = throttle_status()
}
if ($throttle >= $my_throttle_value) {
// throttle limit reached, disable stuff
}
else {
// throttle limit not reached, execute normally
}</pre></p>
<p>Note: Even though the configuration for the throttle is handled by the 'throttle.module', the throttle logic itself is part of the 'statistics.module'. The configuration has been separated in order to make things easier for the average site that will not be utilizing the throttling mechanism. More information about how the throttle works can be found on the throttle.module help page. Find the throttle help page <?php print l("here", array("mod" => "help"), "admin", "throttle"); ?> if you have enabled the throttle.module).</p>
<?php
}
/* Administration hook, defines module's administrative page */
function statistics_admin() {
global $op, $id, $edit, $nid;
/* Only allow people with sufficient access. */
if ((user_access("administer statistics module")) || (user_access("administer statistics"))) {
/* Display the second level admin links */
$links[] = la(t("view statistics"), array("mod" => "statistics", "op" => "statistics"));
$links[] = la(t("view referrers"), array("mod" => "statistics", "op" => "referrers"));
$links[] = la(t("view access log"), array("mod" => "statistics", "op" => "log"));
if (user_access("administer statistics module")) {
$links[] = la(t("top nodes block"), array("mod" => "statistics", "op" => "top nodes block"));
$links[] = la(t("top nodes page"), array("mod" => "statistics", "op" => "top nodes page"));
$links[] = la(t("who's online block"), array("mod" => "statistics", "op" => "whos online block"));
}
$links[] = la(t("help"), array("mod" => "statistics", "op" => "help"));
print "<small>". implode(" &middot; ", $links) ."</small><hr />";
/* non-configuration admin pages */
switch ($op) {
case "help":
print statistics_help();
exit;
case "statistics":
print statistics_admin_displaycounts();
exit;
case "referrers":
print statistics_recent_refer($nid);
print statistics_top_refer($nid);
exit;
case "log":
print statistics_admin_displaylog();
exit;
}
/* configuration admin pages */
if (user_access("administer statistics module")) {
switch (stripslashes($op)) {
case "Submit \"top nodes\" block changes":
statistics_save_topnodes_block($edit);
print status(t("Saved top nodes block changes"));
case "top nodes block":
print statistics_config_topnodes_block(array(
"statistics_block_top_title" => variable_get("statistics_block_top_title", "Top nodes"),
"statistics_block_top_day_head" => variable_get("statistics_block_top_day_head", "<b>Todays top:</b>"),
"statistics_block_top_day_num" => variable_get("statistics_block_top_day_num", 0),
"statistics_block_top_all_head" => variable_get("statistics_block_top_all_head", "<b>All time top:</b>"),
"statistics_block_top_all_num" => variable_get("statistics_block_top_all_num", 0),
"statistics_block_top_last_head" => variable_get("statistics_block_top_last_head" ,"<b>Last:</b>"),
"statistics_block_top_last_num" => variable_get("statistics_block_top_last_num", 0)
));
break;
case "Submit \"who's online\" block changes":
statistics_save_online_block($edit);
print status(t("Saved who's online block changes"));
case "whos online block":
print statistics_config_online_block(array(
"statistics_block_online_title" => variable_get("statistics_block_online_title", "Whos online"),
"statistics_block_online_subtitle" => variable_get("statistics_block_online_subtitle", "Online users:"),
"statistics_block_online_time" => variable_get("statistics_block_online_time", 2700),
"statistics_block_online_max_len" => variable_get("statistics_block_online_max_len", 15),
"statistics_block_online_max_cnt" => variable_get("statistics_block_online_max_cnt", 10)
));
break;
case "Submit \"top nodes\" page changes":
statistics_save_userconfig($edit);
print status(t("Saved top nodes page changes"));
case "top nodes page":
print statistics_admin_userpage_config(array(
"statistics_userpage_link" => variable_get("statistics_userpage_link", ""),
"statistics_userpage_day_head" => variable_get("statistics_userpage_day_head", "Todays top"),
"statistics_userpage_day_cnt" => variable_get("statistics_userpage_day_cnt", 0),
"statistics_userpage_all_head" => variable_get("statistics_userpage_all_head", "All time top"),
"statistics_userpage_all_cnt" => variable_get("statistics_userpage_all_cnt", 0),
"statistics_userpage_last_head" => variable_get("statistics_userpage_last_head", "Last read"),
"statistics_userpage_last_cnt" => variable_get("statistics_userpage_last_cnt", 0)
));
break;
default:
print statistics_admin_displaycounts();
break;
}
}
}
}
/* Displays the various admin tables */
function statistics_admin_count_table($dbfield, $dbrows) {
$result = db_query("SELECT statistics.nid, statistics.daycount, statistics.totalcount, statistics.timestamp, node.title FROM statistics LEFT JOIN node USING (nid) WHERE statistics.%s <> '0' ORDER BY statistics.%s DESC LIMIT %s", $dbfield, $dbfield, $dbrows);
$output = "<table border=\"1\" cellpadding=\"3\" cellspacing =\"0\"><tr><th>". t("title") ."</th><th>". t("today") ."</th><th>". t("all time") ."</th><th>". t("last hit") ."</th><th>". t("referrers") ."</th></tr>\n";
while ($nid = db_fetch_array($result)) {
$output .= "<tr><td>". l($nid["title"], array("id" => $nid["nid"]), "node", "", array("title" => t("View this posting."))) ."</td><td>". $nid["daycount"] ."</td><td>". $nid["totalcount"] ."</td><td>". format_date($nid["timestamp"], "small") ."</td><td><center>";
$output .= la("view", array("mod" => "statistics", "op" => "referrers", "nid" => $nid["nid"], "title" => "$nid[title]"));
$output .= "</center></td></tr>";
}
$output .= "</table>\n";
return $output;
}
function statistics_admin_accesslog_table($type, $id) {
/* overview limit */
$limit0 = 50;
/* detail limit */
$limit1 = 500;
if ($type == 1) {
/* retrieve user access logs */
if ($id) {
/* retrieve recent access logs for user $id */
$result = db_query("SELECT nid, url, hostname, uid, timestamp FROM accesslog WHERE uid = '%s' ORDER BY timestamp DESC LIMIT %s", $id, $limit1);
}
else {
/* retrieve recent access logs for all users */
$result = db_query("SELECT nid, url, hostname, uid, MAX(timestamp) AS timestamp FROM accesslog WHERE uid <> '0' GROUP BY uid ORDER BY timestamp DESC LIMIT %s", $limit1);
}
}
else if ($type == 2) {
/* retrieve recent access logs for node $id */
$result = db_query("SELECT nid, url, hostname, uid, timestamp FROM accesslog WHERE nid = '%s' ORDER BY timestamp DESC LIMIT %s", $id, $limit1);
}
else if ($type == 3) {
/* retrieve recent access logs for hostname $id */
$result = db_query("SELECT nid, url, hostname, uid, timestamp FROM accesslog WHERE hostname = '%s' ORDER BY timestamp DESC LIMIT %s", $id, $limit1);
}
else {
/* retrieve all recent access logs */
$result = db_query("SELECT nid, url, hostname, uid, timestamp FROM accesslog ORDER BY timestamp DESC LIMIT %s", $limit0);
}
$output = "<table border=\"1\" cellpadding=\"3\" cellspacing =\"0\"><tr><th>". t("timestamp") ."</th><th>". t("title") ."</th><th>". t("user") ."</th><th>". t("hostname") ."</th><th>". t("referrer") ."</th></tr>\n";
while ($log = db_fetch_array($result)) {
if (!$node = node_load(array("nid" => $log["nid"]))) {
$node->nid = 0;
}
$user = user_load(array("uid" => $log["uid"]));
$output .= "<tr><td>". format_date($log["timestamp"], "small") ."</td><td>". ($node->nid ? la($node->title, array("mod" => "statistics", "op" => "log", "nid" => $node->nid), "") : "n/a") ."</td><td>". ($user->name ? la((strlen($user->name) > 18 ? substr($user->name, 0, 18) . '...' : $user->name), array("mod" => "statistics", "op" => "log", "uid" => $user->uid), "") : "n/a") ."</td><td>". ($log["hostname"] ? la($log["hostname"], array("mod" => "statistics", "op" => "log", "hostname" => $log["hostname"]), "") : "n/a") ."</td><td>";
if ($log["url"]) {
$output .= "<a href=\"$log[url]\" title=\"$log[url]\">". (strlen($log["url"]) > 28 ? substr($log["url"], 0, 28) . '...' : $log["url"]) ."</a></td></tr>";
}
else {
$output .= "n/a</td></tr>";
}
}
$output .= "</table>";
return $output;
}
function statistics_recent_refer($nid = 0) {
global $HTTP_HOST, $view;
$node = node_load(array("nid" => $nid));
/* allow viewing of all, internal or external referrers */
$links[] = la(t("all"), array("mod" => "statistics", "op" => "referrers", "view" => "all", "nid" => "$nid"));
$links[] = la(t("external"), array("mod" => "statistics", "op" => "referrers", "view" => "external", "nid" => "$nid"));
$links[] = la(t("internal"), array("mod" => "statistics", "op" => "referrers", "view" => "internal", "nid" => "$nid"));
print "<small>". implode(" :: ", $links) ."</small><hr />";
if ($nid > 0) {
if ($view == "all") {
$query = "SELECT url,timestamp FROM accesslog WHERE nid='$nid' AND url <> '' ORDER BY timestamp DESC LIMIT 15";
}
elseif ($view == "internal") {
$query = "SELECT url,timestamp FROM accesslog WHERE nid='$nid' AND url LIKE '%". check_input($HTTP_HOST) ."%' ORDER BY timestamp DESC LIMIT 15";
$describe = "internal ";
}
else {
/* default to external referrers */
$query = "SELECT url,timestamp FROM accesslog WHERE nid='$nid' AND url NOT LIKE '%". check_input($HTTP_HOST) ."%' AND url <> '' ORDER BY timestamp DESC LIMIT 15";
$describe = "external ";
}
$result = db_query($query);
$output = "<h3>". t("Most recent %describe referrers for node", array("%describe" => $describe)) ."\"". l($node->title, array("id" => "$nid"), "node", "", array("title" => t("View this posting."))) ."\"</h3>";
}
else {
if ($view == "all") {
$query = "SELECT url,timestamp FROM accesslog WHERE url <> '' ORDER BY timestamp DESC LIMIT 15";
}
elseif ($view == "internal") {
$query = "SELECT url,timestamp FROM accesslog WHERE url LIKE '%". check_input($HTTP_HOST) ."%' ORDER BY timestamp DESC LIMIT 15";
$describe = "internal ";
}
else {
$query = "SELECT url,timestamp FROM accesslog WHERE url NOT LIKE '%". check_input($HTTP_HOST) ."%' AND url <> '' ORDER BY timestamp DESC LIMIT 15";
$describe = "external ";
}
$result = db_query($query);
$output = "<h3>". t("Most recent %describe referrers", array("%describe" => $describe)) ."</h3>";
}
2001-06-24 12:06:40 +00:00
$output .= "<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\">\n";
$output .= " <tr><th>URL</th><th>date</th></tr>\n";
while ($referrer = db_fetch_array($result)) {
$output .= "<tr><td><a href=\"". check_output($referrer["url"]) ."\">". substr(check_output($referrer["url"]), 0, 100) ."</a></td><td>". format_date($referrer["timestamp"], "small") ."</td></tr>";
2001-06-24 12:06:40 +00:00
}
$output .= "</table>\n";
2001-06-24 12:06:40 +00:00
return $output;
}
function statistics_top_refer($nid = 0) {
global $HTTP_HOST, $view;
$node = node_load(array("nid" => $nid));
if ($nid > 0) {
if ($view == "all") {
$query = "SELECT url, COUNT(url) AS count FROM accesslog WHERE nid = '$nid' AND url <> '' GROUP BY url ORDER BY count DESC";
}
elseif ($view == "internal") {
$query = "SELECT url, COUNT(url) AS count FROM accesslog WHERE nid = '$nid' AND url LIKE '%". check_input($HTTP_HOST) ."%' GROUP BY url ORDER BY count DESC";
$describe = "internal ";
}
else {
/* default to external */
$query = "SELECT url, COUNT(url) AS count FROM accesslog WHERE nid = '$nid' AND url NOT LIKE '%". check_input($HTTP_HOST) ."%' AND url <> '' GROUP BY url ORDER BY count DESC";
$describe = "external ";
}
$output = "<h3>Top ". $describe ."referrers of the past ". format_interval(variable_get("statistics_flush_accesslog_timer", 259200)) ." for node \"". l($node->title, array("id" => "$nid"), "node", "", array("title" => t("View this posting."))) ."\"</h3>";
}
else {
if ($view == "all") {
$query = "SELECT url, COUNT(url) AS count FROM accesslog WHERE url <> '' GROUP BY url ORDER BY count DESC";
}
elseif ($view == "internal") {
$query = "SELECT url, COUNT(url) AS count FROM accesslog WHERE url LIKE '%". check_input($HTTP_HOST) ."%' GROUP BY url ORDER BY count DESC";
$describe = "internal ";
}
else {
/* default to external */
$query = "SELECT url, COUNT(url) AS count FROM accesslog WHERE url NOT LIKE '%". check_input($HTTP_HOST) ."%' AND url <> '' GROUP BY url ORDER BY count DESC";
$describe = "external ";
}
$output = "<h3>". t("Top %describe referrers of the past ", array("%describe" => $describe));
$output .= format_interval(variable_get("statistics_flush_accesslog_timer", 259200)) ."</h3>";
}
$result = db_query($query);
2001-06-24 12:06:40 +00:00
$output .= "<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\">\n";
$output .= " <tr><th>". t("URL") ."</th><th>". t("count") ."</th></tr>\n";
while ($referrer = db_fetch_array($result)) {
$output .= "<tr><td><a href=\"". check_output($referrer["url"]) ."\">". substr(check_output($referrer["url"]), 0, 100) ."</a></td><td>". $referrer["count"] ."</td></tr>";
}
$output .= "</table>\n";
return $output;
}
function statistics_admin_displaycounts() {
$output = "<h3>". t("Today's top nodes") ."</h3>\n";
$output .= statistics_admin_count_table("daycount", 15);
$output .= "<br />";
$output .= "<h3>". t("All time top nodes") ."</h3>\n";
$output .= statistics_admin_count_table("totalcount", 15);
$output .= "<br />";
$output .= "<h3>". t("Last nodes viewed") ."</h3>\n";
$output .= statistics_admin_count_table("timestamp", 15);
return $output;
}
function statistics_admin_displaylog() {
global $uid, $nid, $hostname;
if ($uid) {
$user = user_load(array("uid" => $uid));
$output = "<h3>". t("Recent access logs for '%name'", array("%name" => $user->name)) ."</h3>\n";
$output .= statistics_admin_accesslog_table(1, $user->uid);
}
else if ($nid) {
$node = node_load(array("nid" => $nid));
$output = "<h3>". t("Recent access logs for '%title'", array("%title" => $node->title)) ."</h3>\n";
$output .= statistics_admin_accesslog_table(2, $node->nid);
}
else if ($hostname) {
$output = "<h3>". t("Recent access logs for '%hostname'", array("%hostname" => $hostname)) ."</h3>\n";
$output .= statistics_admin_accesslog_table(3, $hostname);
}
else {
$output = "<h3>". t("Recent access logs") ."</h3>\n";
$output .= statistics_admin_accesslog_table(0, 0);
$output .= "<br />";
$output .= "<h3>". t("Recent user logs") ."</h3>\n";
$output .= statistics_admin_accesslog_table(1, 0);
$output .= "<br />";
}
return $output;
}
/* Displays the block configuration administration form */
function statistics_config_topnodes_block($edit) {
$form = form_textfield(t("Block name"), "statistics_block_top_title", $edit["statistics_block_top_title"], 20, 40, t("This module generates a block with top nodes. You may assign a name for this block."));
$form .= "<hr />";
$form .= form_textfield(t("Today's top nodes title"), "statistics_block_top_day_head", $edit["statistics_block_top_day_head"], 20, 40, t("Specify a name for the \"day's top\" section of the block generated by this module. HTML tags permitted."));
$form .= form_select(t("Number of day's top views to display"), "statistics_block_top_day_num", $edit["statistics_block_top_day_num"], array("0" => t("Disabled"), "1" => "1", "2" => "2", "3" => "3", "4" => "4", "5" => "5", "6" => "6", "7" => "7", "8" => "8", "9" => "9", "10" => "10", "15" => "15", "20" => "20", "25" => "25", "30" => "30", "40" => "40"), t("Set how many \"today's top\" nodes to display on the block generated by this module."));
$form .= "<hr />";
$form .= form_textfield(t("All time top nodes title"), "statistics_block_top_all_head", $edit["statistics_block_top_all_head"], 20, 40, t("Specify a name for the \"all time top\" section of the block generated by this module. HTML tags permitted."));
$form .= form_select(t("Number of all time views to display"), "statistics_block_top_all_num", $edit["statistics_block_top_all_num"], array("0" => t("Disabled"), "1" => "1", "2" => "2", "3" => "3", "4" => "4", "5" => "5", "6" => "6", "7" => "7", "8" => "8", "9" => "9", "10" => "10", "15" => "15", "20" => "20", "25" => "25", "30" => "30", "40" => "40"), t("Set how many \"all time top\" nodes to display on the block generated by this module."));
$form .= "<hr>";
$form .= form_textfield(t("Most recent views heading"), "statistics_block_top_last_head", $edit["statistics_block_top_last_head"], 20, 40, t("Specify a name for the \"last views\" section of the block generated by this module. HTML tags permitted."));
$form .= form_select(t("Number of most recent views to display"), "statistics_block_top_last_num", $edit["statistics_block_top_last_num"], array("0" => t("Disabled"), "1" => "1", "2" => "2", "3" => "3", "4" => "4", "5" => "5", "6" => "6", "7" => "7", "8" => "8", "9" => "9", "10" => "10", "15" => "15", "20" => "20", "25" => "25", "30" => "30", "40" => "40"), t("Set how many \"last viewed\" nodes to display on the block generated by this module."));
$form .= "<hr>";
$form .= form_submit("Submit \"top nodes\" block changes");
return form($form);
}
/* Displays the block configuration administration form */
function statistics_config_online_block($edit) {
$form = form_textfield(t("Block name"), "statistics_block_online_title", $edit["statistics_block_online_title"], 20, 40, t("This module generates a block displaying how many users/guests are online. You may assign a name for this block."));
$form .= form_textfield(t("Sub-block name"), "statistics_block_online_subtitle", $edit["statistics_block_online_subtitle"], 20, 40, t("This module generates a sub-block listing the names of currently online users. You may assign a name for this block."));
$period = array(30 => format_interval(30), 60 => format_interval(60), 120 => format_interval(120), 180 => format_interval(180), 300 => format_interval(300), 600 => format_interval(600), 900 => format_interval(900), 1800 => format_interval(1800), 2700 => format_interval(2700), 3600 => format_interval(3600), 5400 => format_interval(5400), 7200 => format_interval(7200), 10800 => format_interval(10800), 21600 => format_interval(21600), 43200 => format_interval(43200), 86400 => format_interval(86400));
$form .= form_select(t("Activity threshold"), "statistics_block_online_time", $edit["statistics_block_online_time"], $period, "How long ago a user (or guest) must have been active to be considered online.");
$form .= form_select(t("Maximum characters of user's name to display"), "statistics_block_online_max_len", $edit["statistics_block_online_max_len"], array("1" => "1", "5" => "5", "10" => "10", "15" => "15", "20" => "20", "25" => "25", "30" => "30", "35" => "35", "40" => "40", "45" => "45", "50" => "50", "55" => "55", "60" => "60"), t("What is the maximum characters of a user's name to to display in the sub-block."));
$form .= form_select(t("How many online users to list"), "statistics_block_online_max_cnt", $edit["statistics_block_online_max_cnt"], array("0" => "0", "1" => "1", "2" => "2", "3" => "3", "4" => "4", "5" => "5", "6" => "6", "7" => "7", "8" => "8", "9" => "9", "10" => "10", "15" => "15", "20" => "20", "50" => "50", "100" => "100"), t("How many online user's names to display in the sub-block."));
$form .= form_submit("Submit \"who's online\" block changes");
return form($form);
}
/* Displays the user page configuration administration form */
function statistics_admin_userpage_config($edit) {
$form = form_textfield(t("Name for link to user page"), "statistics_userpage_link", $edit["statistics_userpage_link"], 20, 40, t("This node generates a user page with top nodes. If you wish a link added automatically, specify a name."));
$form .= "<hr />";
$form .= form_textfield(t("Today's top nodes title"), "statistics_userpage_day_head", $edit["statistics_userpage_day_head"], 20, 40, t("Specify a name for the \"day's top\" section of the user page generated by this module."));
$form .= form_select(t("Number of nodes to display for \"day's top\""), "statistics_userpage_day_cnt", $edit["statistics_userpage_day_cnt"], array("0" => t("Disabled"), "1" => "1", "2" => "2", "3" => "3", "4" => "4", "5" => "5", "6" => "6", "7" => "7", "8" => "8", "9" => "9", "10" => "10", "15" => "15", "20" => "20", "25" => "25"), t("Set how many \"day's top\" nodes to display on the user page generated by this module."));
$form .= "<hr />";
$form .= form_textfield(t("All time top nodes title"), "statistics_userpage_all_head", $edit["statistics_userpage_all_head"], 20, 40, "Specify a name for the \"all time top\" section of the user page generated by this module.");
$form .= form_select(t("Number of nodes to display for \"all time\""), "statistics_userpage_all_cnt", $edit["statistics_userpage_all_cnt"], array("0" => t("Disabled"), "1" => "1", "2" => "2", "3" => "3", "4" => "4", "5" => "5", "6" => "6", "7" => "7", "8" => "8", "9" => "9", "10" => "10", "15" => "15", "20" => "20", "25" => "25"), t("Set how many \"all time top\" nodes to display on the user page generated by this module."));
$form .= "<hr />";
$form .= form_textfield(t("Last viewed nodes title"), "statistics_userpage_last_head", $edit["statistics_userpage_last_head"], 20, 40, "Specify a name for the \"last viewed\" section of the user page generated by this module.");
$form .= form_select(t("Number of nodes to display for \"last views\""), "statistics_userpage_last_cnt", $edit["statistics_userpage_last_cnt"], array("0" => t("Disabled"), "1" => "1", "2" => "2", "3" => "3", "4" => "4", "5" => "5", "6" => "6", "7" => "7", "8" => "8", "9" => "9", "10" => "10", "15" => "15", "20" => "20", "25" => "25"), t("Set how many \"last viewed\" nodes to display on the user page generated by this module."));
$form .= "<hr />";
$form .= form_submit("Submit \"top nodes\" page changes");
return form($form);
}
/* Adds configure option to the main configure site admin page */
function statistics_conf_options() {
/* access log options */
$output = form_select(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 = array(3600 => format_interval(3600), 10800 => format_interval(10800), 21600 => format_interval(21600), 32400 => format_interval(32400), 43200 => format_interval(43200), 86400 => format_interval(86400), 172800 => format_interval(172800), 259200 => format_interval(259200), 604800 => format_interval(604800), 1209600 => format_interval(1209600), 2419200 => format_interval(2419200), 4838400 => format_interval(4838400), 9676800 => format_interval(9676800));
$output .= 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_select(t("Enable node view counters"), "statistics_enable_node_counter", variable_get("statistics_enable_node_counter", 0), array("1" => t("Enabled"), "0" => t("Disabled")), t("Increment node view counter each time a node is viewed."));
$output .= form_select(t("Display node view counters"), "statistics_display_counter", variable_get("statistics_display_counter", ""), array("1" => t("Enabled"), "0" => t("Disabled")), t("Display how many times each node has been viewed. User must have the 'access statistics' permissions."));
return $output;
}
/* Saves the values entered in the "blockconfig" administration form */
function statistics_save_topnodes_block($edit) {
variable_set("statistics_block_top_title", $edit["statistics_block_top_title"]);
variable_set("statistics_block_top_day_head", $edit["statistics_block_top_day_head"]);
variable_set("statistics_block_top_day_num", $edit["statistics_block_top_day_num"]);
variable_set("statistics_block_top_all_head", $edit["statistics_block_top_all_head"]);
variable_set("statistics_block_top_all_num", $edit["statistics_block_top_all_num"]);
variable_set("statistics_block_top_last_head", $edit["statistics_block_top_last_head"]);
variable_set("statistics_block_top_last_num", $edit["statistics_block_top_last_num"]);
}
/* Saves the values entered in the "blockconfig" administration form */
function statistics_save_online_block($edit) {
variable_set("statistics_block_online_title", $edit["statistics_block_online_title"]);
variable_set("statistics_block_online_subtitle", $edit["statistics_block_online_subtitle"]);
variable_set("statistics_block_online_time", $edit["statistics_block_online_time"]);
variable_set("statistics_block_online_max_len", $edit["statistics_block_online_max_len"]);
variable_set("statistics_block_online_max_cnt", $edit["statistics_block_online_max_cnt"]);
}
/* Saves the values entered in the "userpage" administration form */
function statistics_save_userconfig($edit) {
variable_set("statistics_userpage_link", $edit["statistics_userpage_link"]);
variable_set("statistics_userpage_day_head", $edit["statistics_userpage_day_head"]);
variable_set("statistics_userpage_day_cnt", $edit["statistics_userpage_day_cnt"]);
variable_set("statistics_userpage_all_head", $edit["statistics_userpage_all_head"]);
variable_set("statistics_userpage_all_cnt", $edit["statistics_userpage_all_cnt"]);
variable_set("statistics_userpage_last_head", $edit["statistics_userpage_last_head"]);
variable_set("statistics_userpage_last_cnt", $edit["statistics_userpage_last_cnt"]);
}
/* Saves the values entered in the "config statistics" administration form */
function statistics_save_statistics($edit) {
variable_set("statistics_display_counter", $edit["statistics_display_counter"]);
}
/* cron hook, performs automatic functions */
function statistics_cron() {
$statistics_timestamp = variable_get("statistics_day_timestamp", "");
if ((time() - $statistics_timestamp) >= 86400) {
/* reset day counts */
db_query("UPDATE statistics 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));
$throttle = variable_get("statistics_throttle_level", 0);
/* check if throttle is currently on and if it's time to drop level */
if (($throttle) && ((time() - variable_get("statistics_throttle_cron_timer", 10800)) > variable_get("statistics_throttle_cron_timestamp", 0))) {
/* If throttle is on, back off one notch to test server load */
variable_set("statistics_throttle_level", $throttle - 1);
variable_set("statistics_throttle_cron_timestamp", time());
watchdog("warning", t("cron: decreasing throttle to level '%level' to test server load.", array("%level" => ($throttle - 1))));
}
}
/* Displays the "Top nodes" block */
function statistics_display_topnodes_block() {
$content = array();
$daytop = variable_get("statistics_block_top_day_num", "");
if ($daytop) {
$content[] = node_title_list(statistics_title_list("daycount", $daytop), variable_get("statistics_block_top_day_head", ""));
}
$alltimetop = variable_get("statistics_block_top_all_num", "");
if ($alltimetop) {
$content[] = node_title_list(statistics_title_list("totalcount", $alltimetop), variable_get("statistics_block_top_all_head", ""));
}
$lasttop = variable_get("statistics_block_top_last_num", "");
if ($lasttop) {
$content[] = node_title_list(statistics_title_list("timestamp", $lasttop), variable_get("statistics_block_top_last_head", ""));
}
$output = implode($content, "<br />");
return $output;
}
/* This displays the "Who's online" block */
function statistics_display_online_block() {
global $id, $recent_activity;
$throttle = throttle_status();
$multiplier = variable_get("statistics_throttle_multiplier", 60);
/* don't do any database lookups if on maximum throttle */
if ($throttle < 5) {
/* count users with activity in the past defined period */
$time_period = variable_get("statistics_block_online_time", 2700);
/*
** This call gathers all the info we need on users/guests in a single
** database call, thus is quite efficient.
*/
$result = db_query("SELECT COUNT(DISTINCT hostname) AS count, uid, MAX(timestamp) AS max_timestamp FROM accesslog WHERE timestamp >= '%s' GROUP BY uid ORDER BY max_timestamp DESC", (time() - $time_period));
$users = $guests = 0;
/* Count number of users & guests currently online based on db query */
while ($users_online = db_fetch_array($result)) {
if ($users_online["uid"]) {
/* Has uid, so is a registered user */
$user_list[$users] = $users_online[uid];
$users++;
}
else {
/*
** There's only going to be one return with a uid of 0, and that's
** the guest(s). Hence, the count of this field is the total number
** of guests currently online.
*/
$guests = $users_online["count"];
}
}
/* format the output with proper grammar */
$output .= t("There %verb currently %members and %visitors online.", array("%verb" => ($users == 1 ? "is" : "are"), "%members" => format_plural($users, "user", "users"), "%visitors" => format_plural($guests, "guest", "guests")));
if (user_access("access userlist") && $users) {
/* Display a list of currently online users */
$max_users = variable_get("statistics_block_online_max_cnt", 10);
$max_name_len = variable_get("statistics_block_online_max_len", 15);
$uid = reset($user_list);
while (($uid) && ($max_users)) {
$user = user_load(array("uid" => $uid));
/* When displaying name, be sure it's not more than defined max length */
$items[] = lm((strlen($user->name) > $max_name_len ? substr($user->name, 0, $max_name_len) . '...' : $user->name), array("mod" => "user", "op" => "view", "id" => $user->uid));
$uid = next($user_list);
/*
** When $max_users reaches zero, we break out even if there are
** more online (as defined by the admin)
*/
$max_users--;
}
$output .= "<br /><br />";
$output .= theme_invoke("theme_item_list", $items, variable_get("statistics_block_online_subtitle", "Online users:"));
}
}
else {
/* default message when fully throttled */
$output = t("This site is currently sustaining more than %total page views a minute.", array("%total" => ($throttle * $multiplier)));
}
return $output;
}
/* Display linked title based on field name */
function statistics_title_list($dbfield, $dbrows) {
/* valid dbfields: totalcount, daycount, timestamp */
return db_query("SELECT statistics.nid, node.title, u.uid, u.name FROM statistics LEFT JOIN node ON statistics.nid = node.nid LEFT JOIN users u ON node.uid = u.uid WHERE %s <> '0' ORDER BY %s DESC LIMIT %s", "statistics.". $dbfield, "statistics.". $dbfield, $dbrows);
}
/* Function to retreive current page counter value. */
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 statistics WHERE nid = '%s'", $nid));
}
return $statistics;
}
/* Block hook */
function statistics_block($op = "list", $delta = 0) {
if ($op == "list") {
$blocks[0]["info"] = t("Top nodes");
$blocks[1]["info"] = t("Who's online");
return $blocks;
}
else {
switch($delta) {
case 0:
$block["subject"] = variable_get("statistics_block_top_title", t("Popular content"));
$block["content"] = statistics_display_topnodes_block();
break;
case 1:
$block["subject"] = variable_get("statistics_block_online_title", t("Who's online"));
$block["content"] = statistics_display_online_block();
break;
}
return $block;
}
}
function statistics_page() {
global $theme;
if (user_access("access content")) {
$theme->header();
statistics_page_user();
$theme->footer();
}
else {
$theme->header();
$theme->box(t("Access denied"), message_access());
$theme->footer();
}
}
/* Generates the statistics user page */
function statistics_page_user($uid = 0, $date = 0, $all = 0) {
global $theme;
if (!$date) {
$date = time();
}
if ($displaycount = variable_get("statistics_userpage_day_cnt", 10)) {
$output = "<table border=\"0\" cellpadding=\"4\" cellspacing=\"4\" width=\"100%\">";
$output .= statistics_summary("daycount", $displaycount);
$output .= "</table>";
$theme->box(t(variable_get("statistics_userpage_day_head", "")), $output, "main");
}
if ($displaycount = variable_get("statistics_userpage_all_cnt", "10")) {
$output = "<table border=\"0\" cellpadding=\"4\" cellspacing=\"4\" width=\100%\">";
$output .= statistics_summary("totalcount", $displaycount);
$output .= "</table>";
$theme->box(t(variable_get("statistics_userpage_all_head", "")), $output, "main");
}
if ($displaycount = variable_get("statistics_userpage_last_cnt", "10")) {
$output = "<table border=\"0\" cellpadding=\"4\" cellspacing=\"4\" width=\100%\">";
$output .= statistics_summary("timestamp", $displaycount);
$output .= "</table>";
$theme->box(t(variable_get("statistics_userpage_last_head", "")), $output, "main");
}
}
function statistics_summary($dbfield, $dbrows) {
/* valid dbfields: totalcount, daycount, timestamp */
global $theme;
$output = "";
$result = db_query("SELECT statistics.nid,node.title FROM statistics LEFT JOIN node ON statistics.nid = node.nid ORDER BY %s DESC LIMIT %s", $dbfield, $dbrows);
while ($nid = db_fetch_array($result)) {
$content = node_load(array("nid" => $nid["nid"]));
$links = link_node($content, 1);
$output .= "<tr><td><b>". l($nid["title"], array("id" => $nid["nid"]), "node", "", array("title" => t("View this posting."))) ."</b></td><td align=\"right\"><small>". t("Submitted by %a on %b", array("%a" => format_name($content), "%b" => format_date($content->created, "large"))) ."</td></tr>";
$output .= "</small><tr><td colspan=\"2\"><div style=\"margin-left: 20px;\">". check_output($content->teaser, 1) ."</div></td></tr>";
$output .= "<tr><td align=\"right\" colspan=\"2\">[ ". $theme->links($links) ." ]<br /><br /></td></tr>";
}
return $output;
}
// clean up statistics table when node is deleted
function statistics_node_delete($nid) {
db_query("DELETE FROM statistics WHERE nid = '%d'", $nid);
}
/* internal throttle function - do not call from other modules */
function throttle_update($recent_activity) {
$throttle = throttle_status();
$multiplier = variable_get("statistics_throttle_multiplier", 60);
for ($i = 0; $i <= 5; $i++) {
if (($i * $multiplier) <= $recent_activity) {
$throttle_new = $i;
}
}
if ($throttle_new != $throttle) {
/*
** reduce throttle if new throttle would be 3+ less than current throttle,
** (all other throttle reduction done by _cron hook), increase throttle if
** new throttle would be greater than current throttle.
*/
if (($throttle_new < ($throttle - 2)) || ($throttle_new > $throttle)) {
/* update throttle level */
variable_set("statistics_throttle_level", $throttle_new);
/*
** update the global timestamp, preventing cron.php from jumping in
** too quickly, allowing for user defined period to first pass.
*/
variable_set("statistics_throttle_cron_timestamp", time());
/* log the change */
if ($throttle_new < $throttle) {
watchdog("message", "message: '". $recent_activity ."' hits in past minute; throttle decreased to level ". $throttle_new);
}
else {
watchdog("warning", "warning: '". $recent_activity ."' hits in past minute; throttle increased to level ". $throttle_new);
}
}
}
}
/***********************
* Auto-throttle API *
***********************/
/* external throttle functions - call this from other modules, themes, etc */
function throttle_status() {
static $throttle;
$throttle = variable_get("statistics_throttle_level", 0);
return $throttle;
}
?>