diff --git a/core/modules/statistics/src/NodeStatisticsDatabaseStorage.php b/core/modules/statistics/src/NodeStatisticsDatabaseStorage.php new file mode 100644 index 0000000..3d215ff --- /dev/null +++ b/core/modules/statistics/src/NodeStatisticsDatabaseStorage.php @@ -0,0 +1,153 @@ +connection = $connection; + $this->state = $state; + $this->requestStack = $request_stack; + } + + /** + * {@inheritdoc} + */ + public function recordView($id) { + return (bool) $this->connection + ->merge('node_counter') + ->key('nid', $id) + ->fields([ + 'daycount' => 1, + 'totalcount' => 1, + 'timestamp' => $this->getRequestTime(), + ]) + ->expression('daycount', 'daycount + 1') + ->expression('totalcount', 'totalcount + 1') + ->execute(); + } + + /** + * {@inheritdoc} + */ + public function fetchViews($ids) { + $views = $this->connection + ->select('node_counter', 'nc') + ->fields('nc', ['totalcount', 'daycount', 'timestamp']) + ->condition('nid', $ids, 'IN') + ->execute() + ->fetchAll(); + foreach ($views as $id => $view) { + $views[$id] = new StatisticsViewsResult($view->totalcount, $view->daycount, $view->timestamp); + } + return $views; + } + + /** + * {@inheritdoc} + */ + public function fetchView($id) { + $views = $this->fetchViews(array($id)); + var_dump(reset($views)); + return reset($views); + } + + /** + * {@inheritdoc} + */ + public function fetchAll($order = 'totalcount', $limit = 5) { + assert(in_array($order, ['totalcount', 'daycount', 'timestamp']), "Invalid order argument."); + + return $this->connection + ->select('node_counter', 'nc') + ->fields('nc', ['nid']) + ->orderBy($order, 'DESC') + ->range(0, $limit) + ->execute() + ->fetchCol(); + } + + /** + * {@inheritdoc} + */ + public function deleteViews($id) { + return (bool) $this->connection + ->delete('node_counter') + ->condition('nid', $id) + ->execute(); + } + + /** + * {@inheritdoc} + */ + 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()); + $this->connection->update('node_counter') + ->fields(['daycount' => 0]) + ->execute(); + } + } + + /** + * {@inheritdoc} + */ + public function maxTotalCount() { + $query = $this->connection->select('node_counter', 'nc'); + $query->addExpression('MAX(totalcount)'); + $max_total_count = (int)$query->execute()->fetchField(); + 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/StatisticsDatabaseStorage.php b/core/modules/statistics/src/StatisticsDatabaseStorage.php deleted file mode 100644 index ae07c76..0000000 --- a/core/modules/statistics/src/StatisticsDatabaseStorage.php +++ /dev/null @@ -1,153 +0,0 @@ -connection = $connection; - $this->state = $state; - $this->requestStack = $request_stack; - } - - /** - * {@inheritdoc} - */ - public function recordView($id) { - return (bool) $this->connection - ->merge('node_counter') - ->key('nid', $id) - ->fields([ - 'daycount' => 1, - 'totalcount' => 1, - 'timestamp' => $this->getRequestTime(), - ]) - ->expression('daycount', 'daycount + 1') - ->expression('totalcount', 'totalcount + 1') - ->execute(); - } - - /** - * {@inheritdoc} - */ - public function fetchViews($ids) { - $views = $this->connection - ->select('node_counter', 'nc') - ->fields('nc', ['totalcount', 'daycount', 'timestamp']) - ->condition('nid', $ids, 'IN') - ->execute() - ->fetchAll(); - foreach ($views as $id => $view) { - $views[$id] = new StatisticsViewsResult($view->totalcount, $view->daycount, $view->timestamp); - } - return $views; - } - - /** - * {@inheritdoc} - */ - public function fetchView($id) { - $views = $this->fetchViews(array($id)); - var_dump(reset($views)); - return reset($views); - } - - /** - * {@inheritdoc} - */ - public function fetchAll($order = 'totalcount', $limit = 5) { - assert(in_array($order, ['totalcount', 'daycount', 'timestamp']), "Invalid order argument."); - - return $this->connection - ->select('node_counter', 'nc') - ->fields('nc', ['nid']) - ->orderBy($order, 'DESC') - ->range(0, $limit) - ->execute() - ->fetchCol(); - } - - /** - * {@inheritdoc} - */ - public function deleteViews($id) { - return (bool) $this->connection - ->delete('node_counter') - ->condition('nid', $id) - ->execute(); - } - - /** - * {@inheritdoc} - */ - 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()); - $this->connection->update('node_counter') - ->fields(['daycount' => 0]) - ->execute(); - } - } - - /** - * {@inheritdoc} - */ - public function maxTotalCount() { - $query = $this->connection->select('node_counter', 'nc'); - $query->addExpression('MAX(totalcount)'); - $max_total_count = (int)$query->execute()->fetchField(); - 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/statistics.services.yml b/core/modules/statistics/statistics.services.yml index b034780..95f6307 100644 --- a/core/modules/statistics/statistics.services.yml +++ b/core/modules/statistics/statistics.services.yml @@ -1,6 +1,6 @@ services: statistics.storage: - class: Drupal\statistics\StatisticsDatabaseStorage + class: Drupal\statistics\NodeStatisticsDatabaseStorage arguments: ['@database', '@state', '@request_stack'] tags: - { name: backend_overridable }