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;
2014-06-30 03:33:08 +00:00
use Drupal\Core\Routing\RouteMatchInterface;
2015-01-30 04:56:19 +00:00
use Drupal\Core\Url;
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
*/
2014-06-30 03:33:08 +00:00
function statistics_help($route_name, RouteMatchInterface $route_match) {
2014-05-07 02:04:53 +00:00
switch ($route_name) {
case 'help.page.statistics':
2009-11-23 01:33:45 +00:00
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
2017-03-04 01:20:24 +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 the <a href=":statistics_do">online documentation for the Statistics module</a>.', [':statistics_do' => 'https://www.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>';
2017-03-04 01:20:24 +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 page</a>, and then you can enable and configure the block on the <a href=":blocks">Block layout page</a>.', [':statistics-settings' => \Drupal::url('statistics.settings'), ':blocks' => (\Drupal::moduleHandler()->moduleExists('block')) ? \Drupal::url('block.admin_display') : '#']) . '</dd>';
2009-11-23 01:33:45 +00:00
$output .= '<dt>' . t('Page view counter') . '</dt>';
2017-03-04 01:20:24 +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 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.', [':statistics-settings' => \Drupal::url('statistics.settings'), ':permissions' => \Drupal::url('user.admin_permissions', [], ['fragment' => 'module-statistics'])]) . '</dd>';
2009-11-23 01:33:45 +00:00
$output .= '</dl>';
2005-11-01 10:17:34 +00:00
return $output;
2014-05-07 02:04:53 +00:00
case 'statistics.settings':
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-04-21 13:56:38 +00:00
/**
2014-07-11 12:04:53 +00:00
* Implements hook_ENTITY_TYPE_view() for node entities.
2004-04-21 13:56:38 +00:00
*/
2014-05-02 21:43:59 +00:00
function statistics_node_view(array &$build, 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)) {
2015-10-06 01:00:42 +00:00
$build['#attached']['library'][] = 'statistics/drupal.statistics';
2017-03-04 01:20:24 +00:00
$settings = ['data' => ['nid' => $node->id()], 'url' => Url::fromUri('base:' . drupal_get_path('module', 'statistics') . '/statistics.php')->toString()];
2015-10-06 01:00:42 +00:00
$build['#attached']['drupalSettings']['statistics'] = $settings;
2012-04-26 04:05:27 +00:00
}
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().
*/
2016-02-03 08:42:10 +00:00
function statistics_node_links_alter(array &$links, NodeInterface $entity, array &$context) {
2013-12-10 16:40:21 +00:00
if ($context['view_mode'] != 'rss') {
2016-02-03 08:42:10 +00:00
$links['#cache']['contexts'][] = 'user.permissions';
2013-09-21 15:23:51 +00:00
if (\Drupal::currentUser()->hasPermission('view post access counter')) {
Issue #1446932 by timmillwood, mallezie, RobLoach, hussainweb, Wim Leers, rpayanm, kostyashupenko, mparker17, dawehner, Berdir, Crell: Improve statistics performance by adding a swappable backend
2016-08-03 11:04:48 +00:00
$statistics = \Drupal::service('statistics.storage.node')->fetchView($entity->id());
2009-05-03 10:11:35 +00:00
if ($statistics) {
Issue #1446932 by timmillwood, mallezie, RobLoach, hussainweb, Wim Leers, rpayanm, kostyashupenko, mparker17, dawehner, Berdir, Crell: Improve statistics performance by adding a swappable backend
2016-08-03 11:04:48 +00:00
$statistics_links['statistics_counter']['title'] = \Drupal::translation()->formatPlural($statistics->getTotalCount(), '1 view', '@count views');
2017-03-04 01:20:24 +00:00
$links['statistics'] = [
2010-11-14 21:04:45 +00:00
'#theme' => 'links__node__statistics',
2016-02-03 08:42:10 +00:00
'#links' => $statistics_links,
2017-03-04 01:20:24 +00:00
'#attributes' => ['class' => ['links', 'inline']],
];
2009-05-03 10:11:35 +00:00
}
2016-02-03 08:42:10 +00:00
$links['#cache']['max-age'] = \Drupal::config('statistics.settings')->get('display_max_age');
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() {
Issue #1446932 by timmillwood, mallezie, RobLoach, hussainweb, Wim Leers, rpayanm, kostyashupenko, mparker17, dawehner, Berdir, Crell: Improve statistics performance by adding a swappable backend
2016-08-03 11:04:48 +00:00
$storage = \Drupal::service('statistics.storage.node');
$storage->resetDayCount();
$max_total_count = $storage->maxTotalCount();
\Drupal::state()->set('statistics.node_counter_scale', 1.0 / max(1.0, $max_total_count));
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
*
2016-08-28 11:25:45 +00:00
* @return SelectQuery|false
2012-04-19 15:17:35 +00:00
* 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) {
2017-03-04 01:20:24 +00:00
if (in_array($dbfield, ['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');
2014-08-14 05:21:28 +00:00
$query->join('users_field_data', 'u', 'n.uid = u.uid');
2009-04-13 10:40:13 +00:00
return $query
2017-03-04 01:20:24 +00:00
->fields('n', ['nid', 'title'])
->fields('u', ['uid', 'name'])
2009-04-13 10:40:13 +00:00
->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)
2014-08-14 05:21:28 +00:00
->condition('u.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
}
2004-04-18 12:51:55 +00:00
/**
* Retrieves a node's "view statistics".
*
Issue #1446932 by timmillwood, mallezie, RobLoach, hussainweb, Wim Leers, rpayanm, kostyashupenko, mparker17, dawehner, Berdir, Crell: Improve statistics performance by adding a swappable backend
2016-08-03 11:04:48 +00:00
* @deprecated in Drupal 8.2.x, will be removed before Drupal 9.0.0.
* Use \Drupal::service('statistics.storage.node')->fetchView($id).
2004-04-18 12:51:55 +00:00
*/
Issue #1446932 by timmillwood, mallezie, RobLoach, hussainweb, Wim Leers, rpayanm, kostyashupenko, mparker17, dawehner, Berdir, Crell: Improve statistics performance by adding a swappable backend
2016-08-03 11:04:48 +00:00
function statistics_get($id) {
if ($id > 0) {
/** @var \Drupal\statistics\StatisticsViewsResult $statistics */
$statistics = \Drupal::service('statistics.storage.node')->fetchView($id);
return [
'totalcount' => $statistics->getTotalCount(),
'daycount' => $statistics->getDayCount(),
'timestamp' => $statistics->getTimestamp(),
];
2003-06-23 22:03:50 +00:00
}
}
2006-08-31 21:58:36 +00:00
/**
2014-07-11 12:04:53 +00:00
* Implements hook_ENTITY_TYPE_predelete() for node entities.
2006-08-31 21:58:36 +00:00
*/
2013-03-10 19:05:24 +00:00
function statistics_node_predelete(EntityInterface $node) {
2015-05-02 19:19:58 +00:00
// Clean up statistics table when node is deleted.
Issue #1446932 by timmillwood, mallezie, RobLoach, hussainweb, Wim Leers, rpayanm, kostyashupenko, mparker17, dawehner, Berdir, Crell: Improve statistics performance by adding a swappable backend
2016-08-03 11:04:48 +00:00
$id = $node->id();
return \Drupal::service('statistics.storage.node')->deleteViews($id);
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')) {
2017-03-04 01:20:24 +00:00
return [
'views' => [
2008-05-14 11:10:54 +00:00
'title' => t('Number of views'),
2017-03-04 01:20:24 +00:00
'join' => [
2009-08-29 10:46:41 +00:00
'type' => 'LEFT',
'table' => 'node_counter',
'alias' => 'node_counter',
'on' => 'node_counter.nid = i.sid',
2017-03-04 01:20:24 +00:00
],
2014-03-04 21:39:18 +00:00
// Inverse law that maps the highest view count on the site to 1 and 0
2015-04-01 11:13:09 +00:00
// to 0. Note that the ROUND here is necessary for PostgreSQL and SQLite
// in order to ensure that the :statistics_scale argument is treated as
// a numeric type, 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 * (ROUND(:statistics_scale, 4)))',
2017-03-04 01:20:24 +00:00
'arguments' => [':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) {
2014-04-01 21:14:13 +00:00
if ($variables['configuration']['provider'] == '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']);
}
}