Backup of db before drupal security update
[yaffs-website] / web / core / lib / Drupal / Core / KeyValueStore / DatabaseStorageExpirable.php
1 <?php
2
3 namespace Drupal\Core\KeyValueStore;
4
5 use Drupal\Component\Serialization\SerializationInterface;
6 use Drupal\Core\Database\Connection;
7 use Drupal\Core\Database\Query\Merge;
8
9 /**
10  * Defines a default key/value store implementation for expiring items.
11  *
12  * This key/value store implementation uses the database to store key/value
13  * data with an expire date.
14  */
15 class DatabaseStorageExpirable extends DatabaseStorage implements KeyValueStoreExpirableInterface {
16
17   /**
18    * Overrides Drupal\Core\KeyValueStore\StorageBase::__construct().
19    *
20    * @param string $collection
21    *   The name of the collection holding key and value pairs.
22    * @param \Drupal\Component\Serialization\SerializationInterface $serializer
23    *   The serialization class to use.
24    * @param \Drupal\Core\Database\Connection $connection
25    *   The database connection to use.
26    * @param string $table
27    *   The name of the SQL table to use, defaults to key_value_expire.
28    */
29   public function __construct($collection, SerializationInterface $serializer, Connection $connection, $table = 'key_value_expire') {
30     parent::__construct($collection, $serializer, $connection, $table);
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public function has($key) {
37     return (bool) $this->connection->query('SELECT 1 FROM {' . $this->connection->escapeTable($this->table) . '} WHERE collection = :collection AND name = :key AND expire > :now', [
38       ':collection' => $this->collection,
39       ':key' => $key,
40       ':now' => REQUEST_TIME,
41     ])->fetchField();
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public function getMultiple(array $keys) {
48     $values = $this->connection->query(
49       'SELECT name, value FROM {' . $this->connection->escapeTable($this->table) . '} WHERE expire > :now AND name IN ( :keys[] ) AND collection = :collection',
50       [
51         ':now' => REQUEST_TIME,
52         ':keys[]' => $keys,
53         ':collection' => $this->collection,
54       ])->fetchAllKeyed();
55     return array_map([$this->serializer, 'decode'], $values);
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public function getAll() {
62     $values = $this->connection->query(
63       'SELECT name, value FROM {' . $this->connection->escapeTable($this->table) . '} WHERE collection = :collection AND expire > :now',
64       [
65         ':collection' => $this->collection,
66         ':now' => REQUEST_TIME
67       ])->fetchAllKeyed();
68     return array_map([$this->serializer, 'decode'], $values);
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function setWithExpire($key, $value, $expire) {
75     $this->connection->merge($this->table)
76       ->keys([
77         'name' => $key,
78         'collection' => $this->collection,
79       ])
80       ->fields([
81         'value' => $this->serializer->encode($value),
82         'expire' => REQUEST_TIME + $expire,
83       ])
84       ->execute();
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public function setWithExpireIfNotExists($key, $value, $expire) {
91     $result = $this->connection->merge($this->table)
92       ->insertFields([
93         'collection' => $this->collection,
94         'name' => $key,
95         'value' => $this->serializer->encode($value),
96         'expire' => REQUEST_TIME + $expire,
97       ])
98       ->condition('collection', $this->collection)
99       ->condition('name', $key)
100       ->execute();
101     return $result == Merge::STATUS_INSERT;
102   }
103
104   /**
105    * {@inheritdoc}
106    */
107   public function setMultipleWithExpire(array $data, $expire) {
108     foreach ($data as $key => $value) {
109       $this->setWithExpire($key, $value, $expire);
110     }
111   }
112
113   /**
114    * {@inheritdoc}
115    */
116   public function deleteMultiple(array $keys) {
117     parent::deleteMultiple($keys);
118   }
119
120 }