class NodeStatisticsDatabaseStorage
Same name in other branches
- 9 core/modules/statistics/src/NodeStatisticsDatabaseStorage.php \Drupal\statistics\NodeStatisticsDatabaseStorage
- 8.9.x core/modules/statistics/src/NodeStatisticsDatabaseStorage.php \Drupal\statistics\NodeStatisticsDatabaseStorage
- 10 core/modules/statistics/src/NodeStatisticsDatabaseStorage.php \Drupal\statistics\NodeStatisticsDatabaseStorage
Provides the default database storage backend for statistics.
Hierarchy
- class \Drupal\statistics\NodeStatisticsDatabaseStorage implements \Drupal\statistics\StatisticsStorageInterface
Expanded class hierarchy of NodeStatisticsDatabaseStorage
1 string reference to 'NodeStatisticsDatabaseStorage'
- statistics.services.yml in core/
modules/ statistics/ statistics.services.yml - core/modules/statistics/statistics.services.yml
1 service uses NodeStatisticsDatabaseStorage
- statistics.storage.node in core/
modules/ statistics/ statistics.services.yml - Drupal\statistics\NodeStatisticsDatabaseStorage
File
-
core/
modules/ statistics/ src/ NodeStatisticsDatabaseStorage.php, line 14
Namespace
Drupal\statisticsView source
class NodeStatisticsDatabaseStorage implements StatisticsStorageInterface {
/**
* The database connection used.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* The state service.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* 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.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
*/
public function __construct(Connection $connection, StateInterface $state, RequestStack $request_stack) {
$this->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([
$id,
]);
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');
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
NodeStatisticsDatabaseStorage::$connection | protected | property | The database connection used. |
NodeStatisticsDatabaseStorage::$requestStack | protected | property | The request stack. |
NodeStatisticsDatabaseStorage::$state | protected | property | The state service. |
NodeStatisticsDatabaseStorage::deleteViews | public | function | |
NodeStatisticsDatabaseStorage::fetchAll | public | function | |
NodeStatisticsDatabaseStorage::fetchView | public | function | |
NodeStatisticsDatabaseStorage::fetchViews | public | function | |
NodeStatisticsDatabaseStorage::getRequestTime | protected | function | Get current request time. |
NodeStatisticsDatabaseStorage::maxTotalCount | public | function | |
NodeStatisticsDatabaseStorage::recordView | public | function | |
NodeStatisticsDatabaseStorage::resetDayCount | public | function | |
NodeStatisticsDatabaseStorage::__construct | public | function | Constructs the statistics storage. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.