[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/modules/statistics/ -> statistics.module (source)

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


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7