2003-06-23 22:03:50 +00:00
<?php
2004-08-21 06:42:38 +00:00
/**
* @file
2013-01-17 20:24:40 +00:00
* Logs and displays content statistics for a site.
2004-08-21 06:42:38 +00:00
*/
2013-03-10 19:05:24 +00:00
use Drupal\Core\Entity\EntityInterface;
2013-12-12 23:34:44 +00:00
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
2013-12-10 16:40:21 +00:00
use Drupal\node\NodeInterface;
2012-04-26 16:44:37 +00:00
2004-05-05 21:12:14 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_help().
2004-05-05 21:12:14 +00:00
*/
2007-06-30 19:46:58 +00:00
function statistics_help($path, $arg) {
switch ($path) {
2004-04-18 12:51:55 +00:00
case 'admin/help#statistics':
2009-11-23 01:33:45 +00:00
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
2014-02-13 00:49:42 +00:00
$output .= '<p>' . t('The Statistics module shows you how often content is viewed. This is useful in determining which pages of your site are most popular. For more information, see <a href="!statistics_do">the online documentation for the Statistics module</a>.', array('!statistics_do' => 'https://drupal.org/documentation/modules/statistics/')) . '</p>';
2009-11-23 01:33:45 +00:00
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Displaying popular content') . '</dt>';
2014-02-13 00:49:42 +00:00
$output .= '<dd>' . t('The module includes a <em>Popular content</em> block that displays the most viewed pages today and for all time, and the last content viewed. To use the block, enable <em>Count content views</em> on the <a href="!statistics-settings">statistics settings page</a>, and then you can enable and configure the block on the <a href="!blocks">Block layout page</a>.', array('!statistics-settings' => \Drupal::url('statistics.settings'), '!blocks' => \Drupal::url('block.admin_display'))) . '</dd>';
2009-11-23 01:33:45 +00:00
$output .= '<dt>' . t('Page view counter') . '</dt>';
2014-02-13 00:49:42 +00:00
$output .= '<dd>' . t('The Statistics module includes a counter for each page that increases whenever the page is viewed. To use the counter, enable <em>Count content views</em> on the <a href="!statistics-settings">statistics settings page</a>, and set the necessary <a href="!permissions">permissions</a> (<em>View content hits</em>) so that the counter is visible to the users.', array('!statistics-settings' => \Drupal::url('statistics.settings'), '!permissions' => \Drupal::url('user.admin_permissions', array(), array('fragment' => 'module-statistics')))) . '</dd>';
2009-11-23 01:33:45 +00:00
$output .= '</dl>';
2005-11-01 10:17:34 +00:00
return $output;
2009-09-05 13:49:28 +00:00
case 'admin/config/system/statistics':
2014-03-04 13:08:36 +00:00
return '<p>' . t('Settings for the statistical information that Drupal will keep about the site.') . '</p>';
2003-06-23 22:03:50 +00:00
}
}
2004-05-05 21:12:14 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_permission().
2004-05-05 21:12:14 +00:00
*/
2009-07-05 18:00:11 +00:00
function statistics_permission() {
2008-02-20 13:46:43 +00:00
return array(
2009-01-22 12:01:24 +00:00
'administer statistics' => array(
'title' => t('Administer statistics'),
2009-12-01 13:14:43 +00:00
),
2008-10-09 15:15:55 +00:00
'view post access counter' => array(
2009-12-01 13:14:43 +00:00
'title' => t('View content hits'),
2008-10-09 15:15:55 +00:00
),
2008-02-20 13:46:43 +00:00
);
2003-06-23 22:03:50 +00:00
}
2004-04-21 13:56:38 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_node_view().
2004-04-21 13:56:38 +00:00
*/
2013-12-12 23:34:44 +00:00
function statistics_node_view(EntityInterface $node, EntityViewDisplayInterface $display, $view_mode) {
2013-07-20 12:21:43 +00:00
if (!$node->isNew() && $view_mode == 'full' && node_is_page($node) && empty($node->in_preview)) {
2014-03-09 19:59:45 +00:00
$node->content['statistics_content_counter']['#attached']['library'][] = 'statistics/drupal.statistics';
2013-07-20 12:21:43 +00:00
$settings = array('data' => array('nid' => $node->id()), 'url' => url(drupal_get_path('module', 'statistics') . '/statistics.php'));
2014-01-24 05:59:25 +00:00
$node->content['statistics_content_counter']['#attached']['js'][] = array(
2012-04-26 04:05:27 +00:00
'data' => array('statistics' => $settings),
'type' => 'setting',
);
}
2013-12-10 16:40:21 +00:00
}
2012-04-26 04:05:27 +00:00
2013-12-10 16:40:21 +00:00
/**
* Implements hook_node_links_alter().
*/
function statistics_node_links_alter(array &$node_links, NodeInterface $entity, array &$context) {
if ($context['view_mode'] != 'rss') {
2013-09-21 15:23:51 +00:00
if (\Drupal::currentUser()->hasPermission('view post access counter')) {
2013-12-10 16:40:21 +00:00
$statistics = statistics_get($entity->id());
2009-05-03 10:11:35 +00:00
if ($statistics) {
2012-02-29 17:13:36 +00:00
$links['statistics_counter']['title'] = format_plural($statistics['totalcount'], '1 view', '@count views');
2013-12-10 16:40:21 +00:00
$node_links['statistics'] = array(
2010-11-14 21:04:45 +00:00
'#theme' => 'links__node__statistics',
'#links' => $links,
'#attributes' => array('class' => array('links', 'inline')),
);
2009-05-03 10:11:35 +00:00
}
2003-06-23 22:03:50 +00:00
}
2009-05-03 10:11:35 +00:00
}
2003-06-23 22:03:50 +00:00
}
2004-05-05 21:12:14 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_cron().
2004-05-05 21:12:14 +00:00
*/
2003-06-23 22:03:50 +00:00
function statistics_cron() {
2013-09-16 03:58:06 +00:00
$statistics_timestamp = \Drupal::state()->get('statistics.day_timestamp') ?: 0;
2003-06-23 22:03:50 +00:00
2008-09-17 07:11:59 +00:00
if ((REQUEST_TIME - $statistics_timestamp) >= 86400) {
2007-12-31 08:54:37 +00:00
// Reset day counts.
2009-04-13 10:40:13 +00:00
db_update('node_counter')
->fields(array('daycount' => 0))
->execute();
2013-09-16 03:58:06 +00:00
\Drupal::state()->set('statistics.day_timestamp', REQUEST_TIME);
2003-06-23 22:03:50 +00:00
}
2014-02-12 10:56:06 +00:00
// Calculate the maximum of node views, for node search ranking.
2014-03-04 21:39:18 +00:00
\Drupal::state()->set('statistics.node_counter_scale', 1.0 / max(1.0, db_query('SELECT MAX(totalcount) FROM {node_counter}')->fetchField()));
2003-06-23 22:03:50 +00:00
}
2004-04-18 12:51:55 +00:00
/**
2012-04-19 15:17:35 +00:00
* Returns the most viewed content of all time, today, or the last-viewed node.
2004-04-18 12:51:55 +00:00
*
2012-04-19 15:17:35 +00:00
* @param string $dbfield
* The database field to use, one of:
* - 'totalcount': Integer that shows the top viewed content of all time.
* - 'daycount': Integer that shows the top viewed content for today.
* - 'timestamp': Integer that shows only the last viewed node.
* @param int $dbrows
* The number of rows to be returned.
2004-04-18 12:51:55 +00:00
*
2012-04-19 15:17:35 +00:00
* @return SelectQuery|FALSE
* A query result containing the node ID, title, user ID that owns the node,
* and the username for the selected node(s), or FALSE if the query could not
* be executed correctly.
2004-04-18 12:51:55 +00:00
*/
2003-06-23 22:03:50 +00:00
function statistics_title_list($dbfield, $dbrows) {
2008-01-04 09:31:49 +00:00
if (in_array($dbfield, array('totalcount', 'daycount', 'timestamp'))) {
2013-05-26 20:18:10 +00:00
$query = db_select('node_field_data', 'n');
2009-04-13 10:40:13 +00:00
$query->addTag('node_access');
$query->join('node_counter', 's', 'n.nid = s.nid');
$query->join('users', 'u', 'n.uid = u.uid');
return $query
->fields('n', array('nid', 'title'))
->fields('u', array('uid', 'name'))
->condition($dbfield, 0, '<>')
->condition('n.status', 1)
2013-05-26 20:18:10 +00:00
// @todo This should be actually filtering on the desired node status
// field language and just fall back to the default language.
->condition('n.default_langcode', 1)
2009-04-13 10:40:13 +00:00
->orderBy($dbfield, 'DESC')
->range(0, $dbrows)
->execute();
2008-01-04 09:31:49 +00:00
}
return FALSE;
2003-06-23 22:03:50 +00:00
}
2005-06-07 18:54:37 +00:00
2004-04-18 12:51:55 +00:00
/**
* Retrieves a node's "view statistics".
*
* @param $nid
2012-04-19 15:17:35 +00:00
* The node ID.
2004-04-18 12:51:55 +00:00
*
* @return
2012-04-19 15:17:35 +00:00
* An associative array containing:
* - totalcount: Integer for the total number of times the node has been
* viewed.
* - daycount: Integer for the total number of times the node has been viewed
* "today". For the daycount to be reset, cron must be enabled.
* - timestamp: Integer for the timestamp of when the node was last viewed.
2004-04-18 12:51:55 +00:00
*/
2003-06-23 22:03:50 +00:00
function statistics_get($nid) {
if ($nid > 0) {
2007-12-31 08:54:37 +00:00
// Retrieve an array with both totalcount and daycount.
2009-10-29 07:17:22 +00:00
return db_query('SELECT totalcount, daycount, timestamp FROM {node_counter} WHERE nid = :nid', array(':nid' => $nid), array('target' => 'slave'))->fetchAssoc();
2003-06-23 22:03:50 +00:00
}
}
2004-08-03 09:27:00 +00:00
/**
2012-04-19 15:17:35 +00:00
* Generates a link to a path, truncating the displayed text to a given width.
*
* @param string $path
* The path to generate the link for.
* @param int $width
* The width to set the displayed text of the path.
*
* @return string
* A string as a link, truncated to the width, linked to the given $path.
2004-08-03 09:27:00 +00:00
*/
2006-05-05 11:38:37 +00:00
function _statistics_link($path, $width = 35) {
2013-09-16 03:58:06 +00:00
$title = \Drupal::service('path.alias_manager')->getPathAlias($path);
2006-05-05 11:38:37 +00:00
$title = truncate_utf8($title, $width, FALSE, TRUE);
return l($title, $path);
2004-11-29 09:31:28 +00:00
}
2012-04-19 15:17:35 +00:00
/**
* Formats an item for display, including both the item title and the link.
*
* @param string $title
* The text to link to a path; will be truncated to a maximum width of 35.
* @param string $path
* The path to link to; will default to '/'.
*
* @return string
* An HTML string with $title linked to the $path.
*/
2006-05-05 11:38:37 +00:00
function _statistics_format_item($title, $path) {
$path = ($path ? $path : '/');
2004-11-29 09:31:28 +00:00
$output = ($title ? "$title<br />" : '');
2006-05-05 11:38:37 +00:00
$output .= _statistics_link($path);
2004-11-29 09:31:28 +00:00
return $output;
2004-08-03 09:27:00 +00:00
}
2006-08-31 21:58:36 +00:00
/**
2011-12-17 12:43:37 +00:00
* Implements hook_node_predelete().
2006-08-31 21:58:36 +00:00
*/
2013-03-10 19:05:24 +00:00
function statistics_node_predelete(EntityInterface $node) {
2008-10-06 12:55:56 +00:00
// clean up statistics table when node is deleted
2009-04-13 10:40:13 +00:00
db_delete('node_counter')
2013-07-20 12:21:43 +00:00
->condition('nid', $node->id())
2009-04-13 10:40:13 +00:00
->execute();
2003-06-23 22:03:50 +00:00
}
2008-05-14 11:10:54 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_ranking().
2008-05-14 11:10:54 +00:00
*/
function statistics_ranking() {
2013-09-16 03:58:06 +00:00
if (\Drupal::config('statistics.settings')->get('count_content_views')) {
2008-05-14 11:10:54 +00:00
return array(
'views' => array(
'title' => t('Number of views'),
2009-08-29 10:46:41 +00:00
'join' => array(
'type' => 'LEFT',
'table' => 'node_counter',
'alias' => 'node_counter',
'on' => 'node_counter.nid = i.sid',
),
2014-03-04 21:39:18 +00:00
// Inverse law that maps the highest view count on the site to 1 and 0
// to 0. Note that the CAST here is necessary for PostgreSQL, because
// the PostgreSQL PDO driver sometimes puts values in as strings
// instead of numbers in complex expressions like this.
'score' => '2.0 - 2.0 / (1.0 + node_counter.totalcount * (CAST (:statistics_scale AS DECIMAL(10,4))))',
'arguments' => array(':statistics_scale' => \Drupal::state()->get('statistics.node_counter_scale') ?: 0),
2008-05-14 11:10:54 +00:00
),
);
}
}
2009-04-24 08:23:02 +00:00
2012-01-10 03:22:24 +00:00
/**
2013-10-03 20:55:34 +00:00
* Implements hook_preprocess_HOOK() for block templates.
2012-01-10 03:22:24 +00:00
*/
function statistics_preprocess_block(&$variables) {
2013-05-11 10:01:25 +00:00
if ($variables['configuration']['module'] == 'statistics') {
2012-08-03 15:31:18 +00:00
$variables['attributes']['role'] = 'navigation';
2012-01-10 03:22:24 +00:00
}
}
2012-08-30 19:24:38 +00:00
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
/**
* Implements hook_block_alter().
*
* Removes the "popular" block from display if the module is not configured
* to count content views.
*/
function statistics_block_alter(&$definitions) {
2013-09-16 03:58:06 +00:00
$statistics_count_content_views = \Drupal::config('statistics.settings')->get('count_content_views');
Issue #1535868 by EclipseGc, tim.plunkett, xjm, Jody Lynn, sdboyer, naxoc, tizzo, effulgentsia, dawehner, disasm, beejeebus: Convert all blocks into plugins.
2013-01-04 17:05:13 +00:00
if (empty($statistics_count_content_views)) {
unset($definitions['statistics_popular_block']);
}
}