diff --git a/core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php b/core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php index 439d97f..aab210c 100644 --- a/core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php +++ b/core/modules/statistics/src/Plugin/Block/StatisticsPopularBlock.php @@ -99,11 +99,11 @@ public static function create(ContainerInterface $container, array $configuratio * {@inheritdoc} */ public function defaultConfiguration() { - return array( + return [ 'top_day_num' => 0, 'top_all_num' => 0, 'top_last_num' => 0 - ); + ]; } /** @@ -134,29 +134,29 @@ protected function blockAccess(AccountInterface $account) { */ public function blockForm($form, FormStateInterface $form_state) { // Popular content block settings. - $numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 40); - $numbers = array('0' => $this->t('Disabled')) + array_combine($numbers, $numbers); - $form['statistics_block_top_day_num'] = array( + $numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 40]; + $numbers = ['0' => $this->t('Disabled')] + array_combine($numbers, $numbers); + $form['statistics_block_top_day_num'] = [ '#type' => 'select', '#title' => $this->t("Number of day's top views to display"), '#default_value' => $this->configuration['top_day_num'], '#options' => $numbers, '#description' => $this->t('How many content items to display in "day" list.'), - ); - $form['statistics_block_top_all_num'] = array( + ]; + $form['statistics_block_top_all_num'] = [ '#type' => 'select', '#title' => $this->t('Number of all time views to display'), '#default_value' => $this->configuration['top_all_num'], '#options' => $numbers, '#description' => $this->t('How many content items to display in "all time" list.'), - ); - $form['statistics_block_top_last_num'] = array( + ]; + $form['statistics_block_top_last_num'] = [ '#type' => 'select', '#title' => $this->t('Number of most recent views to display'), '#default_value' => $this->configuration['top_last_num'], '#options' => $numbers, '#description' => $this->t('How many content items to display in "recently viewed" list.'), - ); + ]; return $form; } @@ -173,7 +173,7 @@ public function blockSubmit($form, FormStateInterface $form_state) { * {@inheritdoc} */ public function build() { - $content = array(); + $content = []; if ($this->day_list) { $content['top_day'] = $this->nodeTitleList($this->day_list, $this->t("Today's:")); @@ -197,39 +197,40 @@ public function build() { * Generates the render array for the block. * * @param array $counts - * An ordered array of node ids. + * An ordered array of node ids. * * @param string $title - * The title for the list. + * The title for the list. * * @return array - * A render array for the list. + * A render array for the list. */ protected function nodeTitleList($counts, $title) { $nodes = $this->entityManager->getStorage('node')->loadMultiple($counts); - $items = array(); + $items = []; foreach ($counts as $count) { - $items [] = array( + $node = $this->entityManager->getTranslationFromContext($nodes[$count]); + $items[] = [ '#type' => 'link', - '#title' => $nodes[$count]->getTitle(), - '#url' => $nodes[$count]->urlInfo('canonical'), - '#cache' => array( - 'context' => $nodes[$count]->getCacheContexts(), - 'tags' => $nodes[$count]->getCacheTags(), - ), - ); + '#title' => $node->getTitle(), + '#url' => $node->urlInfo('canonical'), + '#cache' => [ + 'context' => $node->getCacheContexts(), + 'tags' => $node->getCacheTags(), + ], + ]; } - return array( + return [ '#theme' => 'item_list__node', '#items' => $items, '#title' => $title, - '#cache' => array( - 'context' => 'user.permissions', - 'tags' => $this->entityManager->getDefinition('node')->getListCacheTags(), - ), - ); + '#cache' => [ + 'context' => 'user.permissions', + 'tags' => $this->entityManager->getDefinition('node')->getListCacheTags(), + ], + ]; } } diff --git a/core/modules/statistics/src/StatisticsDatabaseStorage.php b/core/modules/statistics/src/StatisticsDatabaseStorage.php index 296795e..612ad1b 100644 --- a/core/modules/statistics/src/StatisticsDatabaseStorage.php +++ b/core/modules/statistics/src/StatisticsDatabaseStorage.php @@ -8,6 +8,7 @@ use Drupal\Core\Database\Connection; use Drupal\Core\State\StateInterface; +use Symfony\Component\HttpFoundation\RequestStack; /** * Provides the default database storage backend for statistics module. @@ -29,16 +30,24 @@ class StatisticsDatabaseStorage implements StatisticsStorageInterface { protected $state; /** - * Construct the statistics storage. + * The request stack. + * + * @var \Symfony\Component\HttpFoundation\RequestStack + */ + protected $requestStack; + + /** + * Constructs the statistics storage. * * @param \Drupal\Core\Database\Connection $connection * The database connection for the node view storage. * @param \Drupal\Core\State\StateInterface $state * The state service. */ - public function __construct(Connection $connection, StateInterface $state) { + public function __construct(Connection $connection, StateInterface $state, RequestStack $request_stack) { $this->connection = $connection; $this->state = $state; + $this->requestStack = $request_stack; } /** @@ -47,11 +56,11 @@ public function __construct(Connection $connection, StateInterface $state) { public function recordHit($nid) { return (bool) $this->connection->merge('node_counter') ->key('nid', $nid) - ->fields(array( + ->fields([ 'daycount' => 1, 'totalcount' => 1, - 'timestamp' => REQUEST_TIME, - )) + 'timestamp' => $this->getRequestTime(), + ]) ->expression('daycount', 'daycount + 1') ->expression('totalcount', 'totalcount + 1') ->execute(); @@ -63,7 +72,7 @@ public function recordHit($nid) { public function fetchViews($nid) { // Retrieve an array, which includes totalcount, daycount, and timestamp. return $this->connection->select('node_counter', 'nc') - ->fields('nc', array('totalcount', 'daycount', 'timestamp')) + ->fields('nc', ['totalcount', 'daycount', 'timestamp']) ->condition('nid', $nid, '=')->execute()->fetchAssoc(); } @@ -77,7 +86,7 @@ public function fetchAll($order = 'totalcount', $limit = 5) { } return $this->connection->select('node_counter', 'nc') - ->fields('nc', array('nid')) + ->fields('nc', ['nid']) ->orderBy($order, 'DESC')->range(0, $limit) ->execute()->fetchCol(); } @@ -96,16 +105,16 @@ public function clean($nid) { */ public function needsReset() { $statistics_timestamp = $this->state->get('statistics.day_timestamp') ?: 0; - return (REQUEST_TIME - $statistics_timestamp) >= 86400; + return ($this->getRequestTime() - $statistics_timestamp) >= 86400; } /** * {@inheritdoc} */ public function resetDayCount() { - $this->state->set('statistics.day_timestamp', REQUEST_TIME); + $this->state->set('statistics.day_timestamp', $this->getRequestTime()); return (bool) $this->connection->update('node_counter') - ->fields(array('daycount' => 0)) + ->fields(['daycount' => 0]) ->execute(); } @@ -120,4 +129,14 @@ public function maxTotalCount() { return $max_total_count; } + /** + * Get current request time. + * + * @return int + * Unix timestamp for current server request time. + */ + protected function getRequestTime() { + return $this->requestStack->getCurrentRequest()->server->get('REQUEST_TIME'); + } + } diff --git a/core/modules/statistics/src/StatisticsStorageInterface.php b/core/modules/statistics/src/StatisticsStorageInterface.php index ab65dae..feddc66 100644 --- a/core/modules/statistics/src/StatisticsStorageInterface.php +++ b/core/modules/statistics/src/StatisticsStorageInterface.php @@ -8,7 +8,8 @@ namespace Drupal\statistics; /** - * Provides an interface defining Statistics Storage + * Provides an interface defining Statistics Storage. + * * Stores the views per day, total views and timestamp of last view * for all nodes on the site. */ @@ -18,7 +19,7 @@ * Count a node view. * * @param int $nid - * The id of the node to count. + * The ID of the node to count. * * @return bool * TRUE if the node view has been counted. @@ -29,7 +30,7 @@ public function recordHit($nid); * Returns the number of times a node has been viewed. * * @param int $nid - * The id of the node to fetch the views for. + * The ID of the node to fetch the views for. * * @return array * An associative array containing: @@ -51,10 +52,10 @@ public function fetchViews($nid); * - 'timestamp' The unix timestamp of the last view. * * @param int $limit - * The number of node ids to return. + * The number of node IDs to return. * * @return array - * An ordered array of node ids. + * An ordered array of node IDs. */ public function fetchAll($order = 'totalcount', $limit = 5); @@ -62,7 +63,7 @@ public function fetchAll($order = 'totalcount', $limit = 5); * Delete counts for a specific node. * * @param int $nid - * The id of the node which views to delete. + * The ID of the node which views to delete. * * @return bool * TRUE if the node views have been deleted. diff --git a/core/modules/statistics/src/Tests/StatisticsReportsTest.php b/core/modules/statistics/src/Tests/StatisticsReportsTest.php index 7f4d3b5..161c6c5 100644 --- a/core/modules/statistics/src/Tests/StatisticsReportsTest.php +++ b/core/modules/statistics/src/Tests/StatisticsReportsTest.php @@ -7,6 +7,9 @@ namespace Drupal\statistics\Tests; +use Drupal\Core\Cache\Cache; +use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait; + /** * Tests display of statistics report blocks. * @@ -14,6 +17,8 @@ */ class StatisticsReportsTest extends StatisticsTestBase { + use AssertPageCacheContextsAndTagsTrait; + /** * Tests the "popular content" block. */ @@ -35,7 +40,7 @@ function testPopularContentBlock() { $client->post($stats_path, array('headers' => $headers, 'body' => $post)); // Configure and save the block. - $this->drupalPlaceBlock('statistics_popular_block', array( + $block = $this->drupalPlaceBlock('statistics_popular_block', array( 'label' => 'Popular content', 'top_day_num' => 3, 'top_all_num' => 3, @@ -48,6 +53,14 @@ function testPopularContentBlock() { $this->assertText("Today's", "Found today's popular content."); $this->assertText('All time', 'Found the all time popular content.'); $this->assertText('Last viewed', 'Found the last viewed popular content.'); + + $this->assertCacheTags(array_merge( + $node->getCacheTags(), + $block->getCacheTags(), + $this->blockingUser->getCacheTags(), + ['block_view', 'config:block_list', 'node_list', 'rendered', 'user_view'] + )); + $this->assertCacheContexts($node->getCacheContexts()); // Check if the node link is displayed. $this->assertRaw(\Drupal::l($node->label(), $node->urlInfo('canonical')), 'Found link to visited node.'); diff --git a/core/modules/statistics/statistics.module b/core/modules/statistics/statistics.module index 76a4f9e..0ab5f6e 100644 --- a/core/modules/statistics/statistics.module +++ b/core/modules/statistics/statistics.module @@ -19,13 +19,13 @@ function statistics_help($route_name, RouteMatchInterface $route_match) { case 'help.page.statistics': $output = ''; $output .= '
' . 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 online documentation for the Statistics module.', array('!statistics_do' => 'https://www.drupal.org/documentation/modules/statistics/')) . '
'; + $output .= '' . 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 online documentation for the Statistics module.', ['!statistics_do' => 'https://www.drupal.org/documentation/modules/statistics/']) . '
'; $output .= '