web/drupal/modules/statistics/statistics.module
branchdrupal
changeset 74 0ff3ba646492
equal deleted inserted replaced
73:fcf75e232c5b 74:0ff3ba646492
       
     1 <?php
       
     2 // $Id: statistics.module,v 1.272.2.1 2009/04/27 12:25:24 goba Exp $
       
     3 
       
     4 /**
       
     5  * @file
       
     6  * Logs access statistics for your site.
       
     7  */
       
     8 
       
     9 /**
       
    10  * Implementation of hook_help().
       
    11  */
       
    12 function statistics_help($path, $arg) {
       
    13   switch ($path) {
       
    14     case 'admin/help#statistics':
       
    15       $output = '<p>'. t('The statistics module keeps track of numerous site usage statistics, including the number of times, and from where, each of your posts is viewed. These statistics are useful in determining how users are interacting with each other and with your site, and are required for the display of some Drupal blocks.') .'</p>';
       
    16       $output .= '<p>'. t('The statistics module provides:') .'</p>';
       
    17       $output .= '<ul><li>'. t('a counter for each post on your site that increments each time the post is viewed. (Enable <em>Count content views</em> on the <a href="@accesslog">access log settings page</a>, and determine if the post access counters should be visible to any user roles on the <a href="@permissions">permissions page</a>.)', array('@accesslog' => url('admin/reports/settings'), '@permissions' => url('admin/user/permissions'))) .'</li>';
       
    18       $output .= '<li>'. t('a <a href="@recent-hits">recent hits</a> log that displays information about the latest activity on your site, including the URL and title of the page accessed, the user name (if available) and IP address of the accessing party.', array('@recent-hits' => url('admin/reports/hits'))) .'</li>';
       
    19       $output .= '<li>'. t('a <a href="@top-referrers">top referrers</a> log that displays the referring parties for your site visits (where your visitors came from).', array('@top-referrers' => url('admin/reports/referrers'))) .'</li>';
       
    20       $output .= '<li>'. t('a <a href="@top-pages">top pages</a> log that displays site content in descending order by number of views.', array('@top-pages' => url('admin/reports/pages'))) .'</li>';
       
    21       $output .= '<li>'. t('a <a href="@top-visitors">top visitors</a> log that displays the most active users on your site.', array('@top-visitors' => url('admin/reports/visitors'))) .'</li>';
       
    22       $output .= '<li>'. t('a <em>Popular content</em> block that displays the day\'s most viewed content, the all-time most viewed content, and the last content viewed. (Enable the <em>Popular content</em> block on the <a href="@blocks">blocks administration page</a>.)', array('@blocks' => url('admin/build/block'))) .'</li></ul>';
       
    23       $output .= '<p>'. t('Configuring the statistics module') .'</p>';
       
    24       $output .= '<ul><li>'. t('When the <em>Enable access log</em> setting on the <a href="@accesslog">access log settings page</a> is enabled, data about every page accessed (including the remote host\'s IP address, referrer, node accessed, and user name) is stored in the access log. The access log must be enabled for the <a href="@recent-hits">recent hits</a>, <a href="@top-referrers">top referrers</a>, <a href="@top-pages">top pages</a>, and <a href="@top-visitors">top visitors</a> log pages to function. Enabling the access log adds one additional database call per page displayed by Drupal.', array('@accesslog' => url('admin/reports/settings'), '@recent-hits' => url('admin/reports/hits'), '@top-referrers' => url('admin/reports/referrers'), '@top-pages' => url('admin/reports/pages'), '@top-visitors' => url('admin/reports/visitors'))) .'</li>';
       
    25       $output .= '<li>'. t('The <em>Discard access logs older than</em> setting on the <a href="@accesslog">access log settings page</a> specifies the length of time entries are retained in the access log before they are deleted. Automatic access log entry deletion requires a correctly configured <a href="@cron">cron maintenance task</a>.', array('@accesslog' => url('admin/reports/settings'), '@cron' => url('admin/reports/status'))) .'</li>';
       
    26       $output .= '<li>'. t('The <em>Count content views</em> setting on the <a href="@accesslog">access log settings page</a> enables a counter for each post on your site that increments each time the post is viewed. This option must be enabled to provide post-specific access counts. Enabling this option adds one additional database call per each post displayed by Drupal.', array('@accesslog' => url('admin/reports/settings'))) .'</li></ul>';
       
    27       $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@statistics">Statistics module</a>.', array('@statistics' => 'http://drupal.org/handbook/modules/statistics/')) .'</p>';
       
    28       return $output;
       
    29     case 'admin/reports/settings':
       
    30       return '<p>'. 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/reports/hits'))) .'</p>';
       
    31     case 'admin/reports/hits':
       
    32       return '<p>'. t("This page displays the site's most recent hits.") .'</p>';
       
    33     case 'admin/reports/referrers':
       
    34       return '<p>'. t('This page displays all external referrers, or external references to your website.') .'</p>';
       
    35     case 'admin/reports/visitors':
       
    36       return '<p>'. t("When you ban a visitor, you prevent the visitor's IP address from accessing your site. Unlike blocking a user, banning a visitor works even for anonymous users. This is most commonly used to block resource-intensive bots or web crawlers.") .'</p>';
       
    37   }
       
    38 }
       
    39 
       
    40 /**
       
    41  * Implementation of hook_exit().
       
    42  *
       
    43  * This is where statistics are gathered on page accesses.
       
    44  */
       
    45 function statistics_exit() {
       
    46   global $user, $recent_activity;
       
    47 
       
    48   drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH);
       
    49 
       
    50   if (variable_get('statistics_count_content_views', 0)) {
       
    51     // We are counting content views.
       
    52     if ((arg(0) == 'node') && is_numeric(arg(1)) && arg(2) == '') {
       
    53       // A node has been viewed, so update the node's counters.
       
    54       db_query('UPDATE {node_counter} SET daycount = daycount + 1, totalcount = totalcount + 1, timestamp = %d WHERE nid = %d', time(), arg(1));
       
    55       // If we affected 0 rows, this is the first time viewing the node.
       
    56       if (!db_affected_rows()) {
       
    57         // We must create a new row to store counters for the new node.
       
    58         db_query('INSERT INTO {node_counter} (nid, daycount, totalcount, timestamp) VALUES (%d, 1, 1, %d)', arg(1), time());
       
    59       }
       
    60     }
       
    61   }
       
    62   if ((variable_get('statistics_enable_access_log', 0)) && (module_invoke('throttle', 'status') == 0)) {
       
    63     // Log this page access.
       
    64     db_query("INSERT INTO {accesslog} (title, path, url, hostname, uid, sid, timer, timestamp) values('%s', '%s', '%s', '%s', %d, '%s', %d, %d)", strip_tags(drupal_get_title()), $_GET['q'], referer_uri(), ip_address(), $user->uid, session_id(), timer_read('page'), time());
       
    65   }
       
    66 }
       
    67 
       
    68 /**
       
    69  * Implementation of hook_perm().
       
    70  */
       
    71 function statistics_perm() {
       
    72   return array('access statistics', 'view post access counter');
       
    73 }
       
    74 
       
    75 /**
       
    76  * Implementation of hook_link().
       
    77  */
       
    78 function statistics_link($type, $node = NULL, $teaser = FALSE) {
       
    79   global $id;
       
    80   $links = array();
       
    81 
       
    82   if ($type == 'node' && user_access('view post access counter')) {
       
    83     $statistics = statistics_get($node->nid);
       
    84     if ($statistics) {
       
    85       $links['statistics_counter']['title'] = format_plural($statistics['totalcount'], '1 read', '@count reads');
       
    86     }
       
    87   }
       
    88 
       
    89   return $links;
       
    90 }
       
    91 
       
    92 /**
       
    93  * Implementation of hook_menu().
       
    94  */
       
    95 function statistics_menu() {
       
    96   $items['admin/reports/hits'] = array(
       
    97     'title' => 'Recent hits',
       
    98     'description' => 'View pages that have recently been visited.',
       
    99     'page callback' => 'statistics_recent_hits',
       
   100     'access arguments' => array('access statistics'),
       
   101     'file' => 'statistics.admin.inc',
       
   102   );
       
   103   $items['admin/reports/pages'] = array(
       
   104     'title' => 'Top pages',
       
   105     'description' => 'View pages that have been hit frequently.',
       
   106     'page callback' => 'statistics_top_pages',
       
   107     'access arguments' => array('access statistics'),
       
   108     'weight' => 1,
       
   109     'file' => 'statistics.admin.inc',
       
   110   );
       
   111   $items['admin/reports/visitors'] = array(
       
   112     'title' => 'Top visitors',
       
   113     'description' => 'View visitors that hit many pages.',
       
   114     'page callback' => 'statistics_top_visitors',
       
   115     'access arguments' => array('access statistics'),
       
   116     'weight' => 2,
       
   117     'file' => 'statistics.admin.inc',
       
   118   );
       
   119   $items['admin/reports/referrers'] = array(
       
   120     'title' => 'Top referrers',
       
   121     'description' => 'View top referrers.',
       
   122     'page callback' => 'statistics_top_referrers',
       
   123     'access arguments' => array('access statistics'),
       
   124     'file' => 'statistics.admin.inc',
       
   125   );
       
   126   $items['admin/reports/access/%'] = array(
       
   127     'title' => 'Details',
       
   128     'description' => 'View access log.',
       
   129     'page callback' => 'statistics_access_log',
       
   130     'page arguments' => array(3),
       
   131     'access arguments' => array('access statistics'),
       
   132     'type' => MENU_CALLBACK,
       
   133     'file' => 'statistics.admin.inc',
       
   134   );
       
   135   $items['admin/reports/settings'] = array(
       
   136     'title' => 'Access log settings',
       
   137     'description' => 'Control details about what and how your site logs.',
       
   138     'page callback' => 'drupal_get_form',
       
   139     'page arguments' => array('statistics_access_logging_settings'),
       
   140     'access arguments' => array('administer site configuration'),
       
   141     'type' => MENU_NORMAL_ITEM,
       
   142     'weight' => 3,
       
   143     'file' => 'statistics.admin.inc',
       
   144   );
       
   145   $items['user/%user/track/navigation'] = array(
       
   146     'title' => 'Track page visits',
       
   147     'page callback' => 'statistics_user_tracker',
       
   148     'access callback' => 'user_access',
       
   149     'access arguments' => array('access statistics'),
       
   150     'type' => MENU_LOCAL_TASK,
       
   151     'weight' => 2,
       
   152     'file' => 'statistics.pages.inc',
       
   153   );
       
   154   $items['node/%node/track'] = array(
       
   155     'title' => 'Track',
       
   156     'page callback' => 'statistics_node_tracker',
       
   157     'access callback' => 'user_access',
       
   158     'access arguments' => array('access statistics'),
       
   159     'type' => MENU_LOCAL_TASK,
       
   160     'weight' => 2,
       
   161     'file' => 'statistics.pages.inc',
       
   162   );
       
   163 
       
   164   return $items;
       
   165 }
       
   166 
       
   167 /**
       
   168  * Implementation of hook_user().
       
   169  */
       
   170 function statistics_user($op, &$edit, &$user) {
       
   171   if ($op == 'delete') {
       
   172     db_query('UPDATE {accesslog} SET uid = 0 WHERE uid = %d', $user->uid);
       
   173   }
       
   174 }
       
   175 
       
   176 /**
       
   177  * Implementation of hook_cron().
       
   178  */
       
   179 function statistics_cron() {
       
   180   $statistics_timestamp = variable_get('statistics_day_timestamp', '');
       
   181 
       
   182   if ((time() - $statistics_timestamp) >= 86400) {
       
   183     // Reset day counts.
       
   184     db_query('UPDATE {node_counter} SET daycount = 0');
       
   185     variable_set('statistics_day_timestamp', time());
       
   186   }
       
   187 
       
   188   // Clean up expired access logs.
       
   189   db_query('DELETE FROM {accesslog} WHERE timestamp < %d', time() - variable_get('statistics_flush_accesslog_timer', 259200));
       
   190 }
       
   191 
       
   192 /**
       
   193  * Returns all time or today top or last viewed node(s).
       
   194  *
       
   195  * @param $dbfield
       
   196  *   one of
       
   197  *   - 'totalcount': top viewed content of all time.
       
   198  *   - 'daycount': top viewed content for today.
       
   199  *   - 'timestamp': last viewed node.
       
   200  *
       
   201  * @param $dbrows
       
   202  *   number of rows to be returned.
       
   203  *
       
   204  * @return
       
   205  *   A query result containing n.nid, n.title, u.uid, u.name of the selected node(s)
       
   206  *   or FALSE if the query could not be executed correctly.
       
   207  */
       
   208 function statistics_title_list($dbfield, $dbrows) {
       
   209   if (in_array($dbfield, array('totalcount', 'daycount', 'timestamp'))) {
       
   210     return db_query_range(db_rewrite_sql("SELECT n.nid, n.title, u.uid, u.name FROM {node} n INNER JOIN {node_counter} s ON n.nid = s.nid INNER JOIN {users} u ON n.uid = u.uid WHERE s.". $dbfield ." != 0 AND n.status = 1 ORDER BY s.". $dbfield ." DESC"), 0, $dbrows);
       
   211   }
       
   212   return FALSE;
       
   213 }
       
   214 
       
   215 
       
   216 /**
       
   217  * Retrieves a node's "view statistics".
       
   218  *
       
   219  * @param $nid
       
   220  *   node ID
       
   221  *
       
   222  * @return
       
   223  *   An array with three entries: [0]=totalcount, [1]=daycount, [2]=timestamp
       
   224  *   - totalcount: count of the total number of times that node has been viewed.
       
   225  *   - daycount: count of the total number of times that node has been viewed "today".
       
   226  *     For the daycount to be reset, cron must be enabled.
       
   227  *   - timestamp: timestamp of when that node was last viewed.
       
   228  */
       
   229 function statistics_get($nid) {
       
   230 
       
   231   if ($nid > 0) {
       
   232     // Retrieve an array with both totalcount and daycount.
       
   233     $statistics = db_fetch_array(db_query('SELECT totalcount, daycount, timestamp FROM {node_counter} WHERE nid = %d', $nid));
       
   234   }
       
   235 
       
   236   return $statistics;
       
   237 }
       
   238 
       
   239 /**
       
   240  * Implementation of hook_block().
       
   241  */
       
   242 function statistics_block($op = 'list', $delta = 0, $edit = array()) {
       
   243   switch ($op) {
       
   244     case 'list':
       
   245       if (variable_get('statistics_count_content_views', 0)) {
       
   246         $blocks[0]['info'] = t('Popular content');
       
   247         // Too dynamic to cache.
       
   248         $blocks[0]['cache'] = BLOCK_NO_CACHE;
       
   249         return $blocks;
       
   250       }
       
   251       break;
       
   252 
       
   253     case 'configure':
       
   254       // Popular content block settings
       
   255       $numbers = array('0' => t('Disabled')) + drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 40));
       
   256       $form['statistics_block_top_day_num'] = array('#type' => 'select', '#title' => t("Number of day's top views to display"), '#default_value' => variable_get('statistics_block_top_day_num', 0), '#options' => $numbers, '#description' => t('How many content items to display in "day" list.'));
       
   257       $form['statistics_block_top_all_num'] = array('#type' => 'select', '#title' => t('Number of all time views to display'), '#default_value' => variable_get('statistics_block_top_all_num', 0), '#options' => $numbers, '#description' => t('How many content items to display in "all time" list.'));
       
   258       $form['statistics_block_top_last_num'] = array('#type' => 'select', '#title' => t('Number of most recent views to display'), '#default_value' => variable_get('statistics_block_top_last_num', 0), '#options' => $numbers, '#description' => t('How many content items to display in "recently viewed" list.'));
       
   259       return $form;
       
   260 
       
   261     case 'save':
       
   262       variable_set('statistics_block_top_day_num', $edit['statistics_block_top_day_num']);
       
   263       variable_set('statistics_block_top_all_num', $edit['statistics_block_top_all_num']);
       
   264       variable_set('statistics_block_top_last_num', $edit['statistics_block_top_last_num']);
       
   265       break;
       
   266 
       
   267     case 'view':
       
   268       if (user_access('access content')) {
       
   269         $content = array();
       
   270 
       
   271         $daytop = variable_get('statistics_block_top_day_num', 0);
       
   272         if ($daytop && ($result = statistics_title_list('daycount', $daytop)) && ($node_title_list = node_title_list($result, t("Today's:")))) {
       
   273           $content[] = $node_title_list;
       
   274         }
       
   275 
       
   276         $alltimetop = variable_get('statistics_block_top_all_num', 0);
       
   277         if ($alltimetop && ($result = statistics_title_list('totalcount', $alltimetop)) && ($node_title_list = node_title_list($result, t('All time:')))) {
       
   278           $content[] = $node_title_list;
       
   279         }
       
   280 
       
   281         $lasttop = variable_get('statistics_block_top_last_num', 0);
       
   282         if ($lasttop && ($result = statistics_title_list('timestamp', $lasttop)) && ($node_title_list = node_title_list($result, t('Last viewed:')))) {
       
   283           $content[] = $node_title_list;
       
   284         }
       
   285 
       
   286         if (count($content)) {
       
   287           $block['content'] = implode('<br />', $content);
       
   288           $block['subject'] = t('Popular content');
       
   289           return $block;
       
   290         }
       
   291       }
       
   292   }
       
   293 }
       
   294 
       
   295 /**
       
   296  * It is possible to adjust the width of columns generated by the
       
   297  * statistics module.
       
   298  */
       
   299 function _statistics_link($path, $width = 35) {
       
   300   $title = drupal_get_path_alias($path);
       
   301   $title = truncate_utf8($title, $width, FALSE, TRUE);
       
   302   return l($title, $path);
       
   303 }
       
   304 
       
   305 function _statistics_format_item($title, $path) {
       
   306   $path = ($path ? $path : '/');
       
   307   $output  = ($title ? "$title<br />" : '');
       
   308   $output .= _statistics_link($path);
       
   309   return $output;
       
   310 }
       
   311 
       
   312 /**
       
   313  * Implementation of hook_nodeapi().
       
   314  */
       
   315 function statistics_nodeapi(&$node, $op, $arg = 0) {
       
   316   switch ($op) {
       
   317     case 'delete':
       
   318       // clean up statistics table when node is deleted
       
   319       db_query('DELETE FROM {node_counter} WHERE nid = %d', $node->nid);
       
   320   }
       
   321 }