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>' ;
2019-04-16 05:38:27 +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' => Url :: fromRoute ( 'statistics.settings' ) -> toString (), ':blocks' => ( \Drupal :: moduleHandler () -> moduleExists ( 'block' )) ? Url :: fromRoute ( 'block.admin_display' ) -> toString () : '#' ]) . '</dd>' ;
2009-11-23 01:33:45 +00:00
$output .= '<dt>' . t ( 'Page view counter' ) . '</dt>' ;
2021-10-18 12:08:44 +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' => Url :: fromRoute ( 'statistics.settings' ) -> toString (), ':permissions' => Url :: fromRoute ( 'user.admin_permissions.module' , [ 'modules' => 'statistics' ]) -> toString ()]) . '</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' ;
Issue #2347783 by kim.pepper, andypost, almaudoh, gumanist, aleevas, rodrigoaguilera, Berdir, saurabh-2k17, Spokje, dhirendra.mishra, alexpott, paulocs, izus, Wim Leers, KapilV, naveenvalecha, anmolgoyal74, subson, yo30, harsha012, mrinalini9, daffie, voleger, dww, fietserwin, tim.plunkett, joachim, larowlan: Deprecate drupal_get_path() and drupal_get_filename() and replace with ExtensionList::getPath() and ExtensionList::getPathname()
2021-07-15 10:20:33 +00:00
$settings = [ 'data' => [ 'nid' => $node -> id ()], 'url' => \Drupal :: request () -> getBasePath () . '/' . \Drupal :: service ( 'extension.list.module' ) -> getPath ( 'statistics' ) . '/statistics.php' ];
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
}
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)))' ,
2020-05-02 12:07:36 +00:00
'arguments' => [ ':statistics_scale' => \Drupal :: state () -> get ( 'statistics.node_counter_scale' , 0 )],
2017-03-04 01:20:24 +00:00
],
];
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' ]);
}
}