Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / statistics / src / NodeStatisticsDatabaseStorage.php
1 <?php
2
3 namespace Drupal\statistics;
4
5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\State\StateInterface;
7 use Symfony\Component\HttpFoundation\RequestStack;
8
9 /**
10  * Provides the default database storage backend for statistics.
11  */
12 class NodeStatisticsDatabaseStorage implements StatisticsStorageInterface {
13
14   /**
15   * The database connection used.
16   *
17   * @var \Drupal\Core\Database\Connection
18   */
19   protected $connection;
20
21   /**
22    * The state service.
23    *
24    * @var \Drupal\Core\State\StateInterface
25    */
26   protected $state;
27
28   /**
29    * The request stack.
30    *
31    * @var \Symfony\Component\HttpFoundation\RequestStack
32    */
33   protected $requestStack;
34
35   /**
36    * Constructs the statistics storage.
37    *
38    * @param \Drupal\Core\Database\Connection $connection
39    *   The database connection for the node view storage.
40    * @param \Drupal\Core\State\StateInterface $state
41    *   The state service.
42    */
43   public function __construct(Connection $connection, StateInterface $state, RequestStack $request_stack) {
44     $this->connection = $connection;
45     $this->state = $state;
46     $this->requestStack = $request_stack;
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public function recordView($id) {
53     return (bool) $this->connection
54       ->merge('node_counter')
55       ->key('nid', $id)
56       ->fields([
57         'daycount' => 1,
58         'totalcount' => 1,
59         'timestamp' => $this->getRequestTime(),
60       ])
61       ->expression('daycount', 'daycount + 1')
62       ->expression('totalcount', 'totalcount + 1')
63       ->execute();
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function fetchViews($ids) {
70     $views = $this->connection
71       ->select('node_counter', 'nc')
72       ->fields('nc', ['totalcount', 'daycount', 'timestamp'])
73       ->condition('nid', $ids, 'IN')
74       ->execute()
75       ->fetchAll();
76     foreach ($views as $id => $view) {
77       $views[$id] = new StatisticsViewsResult($view->totalcount, $view->daycount, $view->timestamp);
78     }
79     return $views;
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function fetchView($id) {
86     $views = $this->fetchViews([$id]);
87     return reset($views);
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   public function fetchAll($order = 'totalcount', $limit = 5) {
94     assert(in_array($order, ['totalcount', 'daycount', 'timestamp']), "Invalid order argument.");
95
96     return $this->connection
97       ->select('node_counter', 'nc')
98       ->fields('nc', ['nid'])
99       ->orderBy($order, 'DESC')
100       ->range(0, $limit)
101       ->execute()
102       ->fetchCol();
103   }
104
105   /**
106    * {@inheritdoc}
107    */
108   public function deleteViews($id) {
109     return (bool) $this->connection
110       ->delete('node_counter')
111       ->condition('nid', $id)
112       ->execute();
113   }
114
115   /**
116    * {@inheritdoc}
117    */
118   public function resetDayCount() {
119     $statistics_timestamp = $this->state->get('statistics.day_timestamp') ?: 0;
120     if (($this->getRequestTime() - $statistics_timestamp) >= 86400) {
121       $this->state->set('statistics.day_timestamp', $this->getRequestTime());
122       $this->connection->update('node_counter')
123         ->fields(['daycount' => 0])
124         ->execute();
125     }
126   }
127
128   /**
129    * {@inheritdoc}
130    */
131   public function maxTotalCount() {
132     $query = $this->connection->select('node_counter', 'nc');
133     $query->addExpression('MAX(totalcount)');
134     $max_total_count = (int) $query->execute()->fetchField();
135     return $max_total_count;
136   }
137
138   /**
139    * Get current request time.
140    *
141    * @return int
142    *   Unix timestamp for current server request time.
143    */
144   protected function getRequestTime() {
145     return $this->requestStack->getCurrentRequest()->server->get('REQUEST_TIME');
146   }
147
148 }