ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
InformationDBRepository.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
26 
33 {
34  public const TABLE_NAME = 'il_resource_info';
35  public const IDENTIFICATION = 'rid';
36 
40  protected array $cache = [];
41  protected \ilDBInterface $db;
42 
43  public function __construct(\ilDBInterface $db)
44  {
45  $this->db = $db;
46  }
47 
51  public function getNamesForLocking(): array
52  {
53  return [self::TABLE_NAME];
54  }
55 
59  public function blank(): Information
60  {
61  return new FileInformation();
62  }
63 
67  public function store(Information $information, Revision $revision): void
68  {
69  $rid = $revision->getIdentification()->serialize();
70  $version = $revision->getVersionNumber();
71 
72  $this->db->replace(
73  self::TABLE_NAME,
74  [
75  self::IDENTIFICATION => ['text', $rid],
76  'version_number' => ['integer', $version]
77  ],
78  [
79  'title' => ['text', $information->getTitle()],
80  'mime_type' => ['text', $information->getMimeType()],
81  'suffix' => ['text', $information->getSuffix()],
82  'size' => ['integer', $information->getSize()],
83  'creation_date' => ['integer', $information->getCreationDate()->getTimestamp()]
84  ]
85  );
86 
87  $this->cache[$rid][$version] = $information;
88  }
89 
93  public function get(Revision $revision): Information
94  {
95  $rid = $revision->getIdentification()->serialize();
96  if (isset($this->cache[$rid][$revision->getVersionNumber()])) {
97  return $this->cache[$rid][$revision->getVersionNumber()];
98  }
99  $r = $this->db->queryF(
100  "SELECT * FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s AND version_number = %s",
101  [
102  'text',
103  'integer'
104  ],
105  [
106  $rid,
107  $revision->getVersionNumber()
108  ]
109  );
110 
111  $d = $this->db->fetchAssoc($r);
112  $i = $this->getFileInfoFromArrayData($d);
113 
114  $this->cache[$rid][$revision->getVersionNumber()] = $i;
115 
116  return $i;
117  }
118 
119  public function delete(Information $information, Revision $revision): void
120  {
121  $rid = $revision->getIdentification()->serialize();
122  $this->db->manipulateF(
123  "DELETE FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s AND version_number = %s",
124  [
125  'text',
126  'integer'
127  ],
128  [
129  $rid,
130  $revision->getVersionNumber()
131  ]
132  );
133  unset($this->cache[$rid][$revision->getVersionNumber()]);
134  }
135 
136  public function preload(array $identification_strings): void
137  {
138  $r = $this->db->query(
139  "SELECT * FROM " . self::TABLE_NAME . " WHERE " . $this->db->in(
140  self::IDENTIFICATION,
141  $identification_strings,
142  false,
143  'text'
144  )
145  );
146 
147  while ($d = $this->db->fetchAssoc($r)) {
148  $this->populateFromArray($d);
149  }
150  }
151 
152  public function populateFromArray(array $data): void
153  {
154  $this->cache[$data['rid']][$data['version_number']] = $this->getFileInfoFromArrayData($data);
155  }
156 
158  {
159  $i = new FileInformation();
160  $i->setTitle((string) $data['title']);
161  $i->setSize((int) $data['size']);
162  $i->setMimeType((string) $data['mime_type']);
163  $i->setSuffix((string) $data['suffix']);
164  $i->setCreationDate((new \DateTimeImmutable())->setTimestamp((int) ($data['creation_date'] ?? 0)));
165 
166  return $i;
167  }
168 }
$version
Definition: plugin.php:25
$r