2b1995437e2905a4734f5c107c81faab5a18f589
[yaffs-website] / web / core / modules / file / src / FileUsage / DatabaseFileUsageBackend.php
1 <?php
2
3 namespace Drupal\file\FileUsage;
4
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Database\Connection;
7 use Drupal\file\FileInterface;
8
9 /**
10  * Defines the database file usage backend. This is the default Drupal backend.
11  */
12 class DatabaseFileUsageBackend extends FileUsageBase {
13
14   /**
15    * The database connection used to store file usage information.
16    *
17    * @var \Drupal\Core\Database\Connection
18    */
19   protected $connection;
20
21   /**
22    * The name of the SQL table used to store file usage information.
23    *
24    * @var string
25    */
26   protected $tableName;
27
28   /**
29    * Construct the DatabaseFileUsageBackend.
30    *
31    * @param \Drupal\Core\Database\Connection $connection
32    *   The database connection which will be used to store the file usage
33    *   information.
34    * @param string $table
35    *   (optional) The table to store file usage info. Defaults to 'file_usage'.
36    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
37    *   (optional) The config factory.
38    */
39   public function __construct(Connection $connection, $table = 'file_usage', ConfigFactoryInterface $config_factory = NULL) {
40     parent::__construct($config_factory);
41     $this->connection = $connection;
42
43     $this->tableName = $table;
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   public function add(FileInterface $file, $module, $type, $id, $count = 1) {
50     $this->connection->merge($this->tableName)
51       ->keys([
52         'fid' => $file->id(),
53         'module' => $module,
54         'type' => $type,
55         'id' => $id,
56       ])
57       ->fields(['count' => $count])
58       ->expression('count', 'count + :count', [':count' => $count])
59       ->execute();
60
61     parent::add($file, $module, $type, $id, $count);
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function delete(FileInterface $file, $module, $type = NULL, $id = NULL, $count = 1) {
68     // Delete rows that have a exact or less value to prevent empty rows.
69     $query = $this->connection->delete($this->tableName)
70       ->condition('module', $module)
71       ->condition('fid', $file->id());
72     if ($type && $id) {
73       $query
74         ->condition('type', $type)
75         ->condition('id', $id);
76     }
77     if ($count) {
78       $query->condition('count', $count, '<=');
79     }
80     $result = $query->execute();
81
82     // If the row has more than the specified count decrement it by that number.
83     if (!$result && $count > 0) {
84       $query = $this->connection->update($this->tableName)
85         ->condition('module', $module)
86         ->condition('fid', $file->id());
87       if ($type && $id) {
88         $query
89           ->condition('type', $type)
90           ->condition('id', $id);
91       }
92       $query->expression('count', 'count - :count', [':count' => $count]);
93       $query->execute();
94     }
95
96     parent::delete($file, $module, $type, $id, $count);
97   }
98
99   /**
100    * {@inheritdoc}
101    */
102   public function listUsage(FileInterface $file) {
103     $result = $this->connection->select($this->tableName, 'f')
104       ->fields('f', ['module', 'type', 'id', 'count'])
105       ->condition('fid', $file->id())
106       ->condition('count', 0, '>')
107       ->execute();
108     $references = [];
109     foreach ($result as $usage) {
110       $references[$usage->module][$usage->type][$usage->id] = $usage->count;
111     }
112     return $references;
113   }
114
115 }