diff --git a/core/modules/node/src/Tests/NodeLoadMultipleTest.php b/core/modules/node/src/Tests/NodeLoadMultipleTest.php index c758b3d..4add3bd 100644 --- a/core/modules/node/src/Tests/NodeLoadMultipleTest.php +++ b/core/modules/node/src/Tests/NodeLoadMultipleTest.php @@ -1,6 +1,7 @@ 0, 'top_all_num' => 0, 'top_last_num' => 0 - ]; + ); } /** @@ -112,29 +112,29 @@ protected function blockAccess(AccountInterface $account) { */ public function blockForm($form, FormStateInterface $form_state) { // Popular content block settings. - $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'] = [ + $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( '#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'] = [ + ); + $form['statistics_block_top_all_num'] = array( '#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'] = [ + ); + $form['statistics_block_top_last_num'] = array( '#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; } @@ -151,7 +151,7 @@ public function blockSubmit($form, FormStateInterface $form_state) { * {@inheritdoc} */ public function build() { - $content = []; + $content = array(); if ($this->configuration['top_day_num'] > 0) { $result = $this->statisticsStorage->fetchAll('daycount', $this->configuration['top_day_num']); @@ -190,7 +190,7 @@ public function build() { * A render array for the list. */ protected function nodeTitleList(array $nids, $title) { - $nodes = Node::loadMultiple($nids); + $nodes = $this->entityTypeManager->getStorage('node')->loadMultiple($nids); $items = []; foreach ($nids as $nid) { diff --git a/core/modules/statistics/src/StatisticsDatabaseStorage.php b/core/modules/statistics/src/StatisticsDatabaseStorage.php index 32d0871..4173179 100644 --- a/core/modules/statistics/src/StatisticsDatabaseStorage.php +++ b/core/modules/statistics/src/StatisticsDatabaseStorage.php @@ -53,7 +53,7 @@ public function __construct(Connection $connection, StateInterface $state, Reque /** * {@inheritdoc} */ - public function recordHit($id) { + public function recordView($id) { return (bool) $this->connection ->merge('node_counter') ->key('nid', $id) @@ -77,7 +77,7 @@ public function fetchViews($id) { ->condition('nid', $id, '=') ->execute() ->fetchAssoc(); - return new StatisticsViews($views['totalcount'], $views['daycount'], $views['timestamp']); + return new StatisticsViewsResult($views['totalcount'], $views['daycount'], $views['timestamp']); } /** @@ -98,7 +98,7 @@ public function fetchAll($order = 'totalcount', $limit = 5) { /** * {@inheritdoc} */ - public function clean($id) { + public function deleteViews($id) { return (bool) $this->connection ->delete('node_counter') ->condition('nid', $id) @@ -112,11 +112,10 @@ public function resetDayCount() { $statistics_timestamp = $this->state->get('statistics.day_timestamp') ?: 0; if (($this->getRequestTime() - $statistics_timestamp) >= 86400) { $this->state->set('statistics.day_timestamp', $this->getRequestTime()); - return (bool) $this->connection->update('node_counter') + $this->connection->update('node_counter') ->fields(['daycount' => 0]) ->execute(); } - return FALSE; } /** @@ -126,7 +125,6 @@ public function maxTotalCount() { $query = $this->connection->select('node_counter', 'nc'); $query->addExpression('MAX(totalcount)'); $max_total_count = (int)$query->execute()->fetchField(); - $this->state->set('statistics.node_counter_scale', 1.0 / max(1.0, $max_total_count)); return $max_total_count; } diff --git a/core/modules/statistics/src/StatisticsStorageInterface.php b/core/modules/statistics/src/StatisticsStorageInterface.php index b12165b..39dee70 100644 --- a/core/modules/statistics/src/StatisticsStorageInterface.php +++ b/core/modules/statistics/src/StatisticsStorageInterface.php @@ -1,10 +1,5 @@ totalCount = $total_count; - $this->dayCount = $day_count; - $this->timestamp = $timestamp; - } - - /** - * {@inheritdoc} - */ - public function getTotalCount() { - return $this->totalCount; - } - - /** - * {@inheritdoc} - */ - public function getDayCount() { - return $this->dayCount; - } - - /** - * {@inheritdoc} - */ - public function getTimestamp() { - return $this->timestamp; - } -} diff --git a/core/modules/statistics/src/StatisticsViewsInterface.php b/core/modules/statistics/src/StatisticsViewsInterface.php deleted file mode 100644 index 2c72184..0000000 --- a/core/modules/statistics/src/StatisticsViewsInterface.php +++ /dev/null @@ -1,28 +0,0 @@ -totalCount = $total_count; + $this->dayCount = $day_count; + $this->timestamp = $timestamp; + } + + /** + * {@inheritdoc} + */ + public function getTotalCount() { + return $this->totalCount; + } + + /** + * {@inheritdoc} + */ + public function getDayCount() { + return $this->dayCount; + } + + /** + * {@inheritdoc} + */ + public function getTimestamp() { + return $this->timestamp; + } +} diff --git a/core/modules/statistics/statistics.module b/core/modules/statistics/statistics.module index fe55853..69d783b 100644 --- a/core/modules/statistics/statistics.module +++ b/core/modules/statistics/statistics.module @@ -53,15 +53,14 @@ function statistics_node_links_alter(array &$links, NodeInterface $entity, array $links['#cache']['contexts'][] = 'user.permissions'; if (\Drupal::currentUser()->hasPermission('view post access counter')) { $statistics = \Drupal::service('statistics.storage')->fetchViews($entity->id()); - if ($statistics instanceof \Drupal\statistics\StatisticsViewsInterface) { - $statistics_links['statistics_counter']['title'] = \Drupal::translation()->formatPlural($statistics->getTotalCount(), '1 view', '@count views'); - $links['statistics'] = [ - '#theme' => 'links__node__statistics', - '#links' => $statistics_links, - '#attributes' => array('class' => array('links', 'inline')), - ]; - } - $links['#cache']['max-age'] = \Drupal::config('statistics.settings')->get('display_max_age'); + $statistics_links['statistics_counter']['title'] = \Drupal::translation()->formatPlural($statistics->getTotalCount(), '1 view', '@count views'); + $links['statistics'] = array( + '#theme' => 'links__node__statistics', + '#links' => $statistics_links, + '#attributes' => array('class' => array('links', 'inline')), + '#cache' => array('max-age' => \Drupal::config('statistics.settings')->get('display_max_age')), + ); + } } } @@ -72,7 +71,47 @@ function statistics_node_links_alter(array &$links, NodeInterface $entity, array function statistics_cron() { $storage = \Drupal::service('statistics.storage'); $storage->resetDayCount(); - $storage->maxTotalCount(); + $max_total_count = $storage->maxTotalCount(); + $this->state->set('statistics.node_counter_scale', 1.0 / max(1.0, $max_total_count)); +} + +/** + * Returns the most viewed content of all time, today, or the last-viewed node. + * + * @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. + * + * @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. + */ +function statistics_title_list($dbfield, $dbrows) { + if (in_array($dbfield, array('totalcount', 'daycount', 'timestamp'))) { + $query = db_select('node_field_data', 'n'); + $query->addTag('node_access'); + $query->join('node_counter', 's', 'n.nid = s.nid'); + $query->join('users_field_data', 'u', 'n.uid = u.uid'); + + return $query + ->fields('n', array('nid', 'title')) + ->fields('u', array('uid', 'name')) + ->condition($dbfield, 0, '<>') + ->condition('n.status', 1) + // @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) + ->condition('u.default_langcode', 1) + ->orderBy($dbfield, 'DESC') + ->range(0, $dbrows) + ->execute(); + } + return FALSE; } /** @@ -83,7 +122,7 @@ function statistics_cron() { */ function statistics_get($id) { if ($id > 0) { - /** @var \Drupal\statistics\StatisticsViewsInterface $statistics */ + /** @var \Drupal\statistics\StatisticsViewsResult $statistics */ $statistics = \Drupal::service('statistics.storage')->fetchViews($id); return [ 'totalcount' => $statistics->getTotalCount(), @@ -99,7 +138,7 @@ function statistics_get($id) { function statistics_node_predelete(EntityInterface $node) { // Clean up statistics table when node is deleted. $id = $node->id(); - return \Drupal::service('statistics.storage')->clean($id); + return \Drupal::service('statistics.storage')->deleteViews($id); } /** @@ -107,15 +146,15 @@ function statistics_node_predelete(EntityInterface $node) { */ function statistics_ranking() { if (\Drupal::config('statistics.settings')->get('count_content_views')) { - return [ - 'views' => [ + return array( + 'views' => array( 'title' => t('Number of views'), - 'join' => [ + 'join' => array( 'type' => 'LEFT', 'table' => 'node_counter', 'alias' => 'node_counter', 'on' => 'node_counter.nid = i.sid', - ], + ), // Inverse law that maps the highest view count on the site to 1 and 0 // 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 @@ -123,9 +162,9 @@ function statistics_ranking() { // 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)))', - 'arguments' => [':statistics_scale' => \Drupal::state()->get('statistics.node_counter_scale') ?: 0], - ], - ]; + 'arguments' => array(':statistics_scale' => \Drupal::state()->get('statistics.node_counter_scale') ?: 0), + ), + ); } } diff --git a/core/modules/statistics/statistics.php b/core/modules/statistics/statistics.php index 9b861e8..896e956 100644 --- a/core/modules/statistics/statistics.php +++ b/core/modules/statistics/statistics.php @@ -24,7 +24,6 @@ if ($views) { $id = filter_input(INPUT_POST, 'id', FILTER_VALIDATE_INT); if ($id) { - $container->get('request_stack')->push(Request::createFromGlobals()); - $container->get('statistics.storage')->recordHit($id); + $container->get('statistics.storage')->recordView($id); } }