ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
ResourceDBRepository.php
Go to the documentation of this file.
1 <?php
2 
20 
26 
33 {
34  public const TABLE_NAME = 'il_resource';
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(
60  ResourceIdentification $identification,
61  ResourceType $type = ResourceType::SINGLE_FILE
62  ): StorableResource {
63  return match ($type) {
64  ResourceType::SINGLE_FILE => new StorableFileResource($identification),
65  ResourceType::CONTAINER => new StorableContainerResource($identification),
66  };
67  }
68 
72  public function get(ResourceIdentification $identification): StorableResource
73  {
74  if (isset($this->cache[$identification->serialize()])) {
75  return $this->cache[$identification->serialize()];
76  }
77 
78  $q = "SELECT " . self::IDENTIFICATION . ", storage_id, rtype FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s";
79  $r = $this->db->queryF($q, ['text'], [$identification->serialize()]);
80  $d = $this->db->fetchObject($r);
81 
82  $resource = $this->blank($identification, ResourceType::from($d->rtype));
83  $resource->setStorageID($d->storage_id);
84 
85  $this->cache[$identification->serialize()] = $resource;
86 
87  return $resource;
88  }
89 
93  public function has(ResourceIdentification $identification): bool
94  {
95  if (isset($this->cache[$identification->serialize()])) {
96  return true;
97  }
98  $q = "SELECT " . self::IDENTIFICATION . " FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s";
99  $r = $this->db->queryF($q, ['text'], [$identification->serialize()]);
100 
101  return (bool)$r->numRows() > 0;
102  }
103 
107  public function store(StorableResource $resource): void
108  {
109  $rid = $resource->getIdentification()->serialize();
110  if ($this->has($resource->getIdentification())) {
111  // UPDATE
112  $this->db->update(
113  self::TABLE_NAME,
114  [
115  self::IDENTIFICATION => ['text', $rid],
116  'storage_id' => ['text', $resource->getStorageID()],
117  'rtype' => ['text', $resource->getType()->value],
118  ],
119  [
120  self::IDENTIFICATION => ['text', $rid],
121  ]
122  );
123  } else {
124  // CREATE
125  $this->db->insert(
126  self::TABLE_NAME,
127  [
128  self::IDENTIFICATION => ['text', $rid],
129  'storage_id' => ['text', $resource->getStorageID()],
130  'rtype' => ['text', $resource->getType()->value],
131  ]
132  );
133  }
134  $this->cache[$rid] = $resource;
135  }
136 
140  public function delete(StorableResource $resource): void
141  {
142  $rid = $resource->getIdentification()->serialize();
143  $this->db->manipulateF(
144  "DELETE FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s",
145  ['text'],
146  [$rid]
147  );
148  unset($this->cache[$rid]);
149  }
150 
154  public function getAll(): \Generator
155  {
156  yield from [];
157  }
158 
159  public function preload(array $identification_strings): void
160  {
161  $r = $this->db->query(
162  "SELECT rid, storage_id, rtype FROM " . self::TABLE_NAME . " WHERE "
163  . $this->db->in(self::IDENTIFICATION, $identification_strings, false, 'text')
164  );
165  while ($d = $this->db->fetchAssoc($r)) {
166  $this->populateFromArray($d);
167  }
168  }
169 
170  public function populateFromArray(array $data): void
171  {
172  $resource = $this->blank(
173  new ResourceIdentification($data['rid']),
174  ResourceType::from($data['rtype'] ?? ResourceType::SINGLE_FILE->value)
175  );
176  $resource->setStorageID($data['storage_id']);
177  $this->cache[$data['rid']] = $resource;
178  }
179 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
blank(ResourceIdentification $identification, ResourceType $type=ResourceType::SINGLE_FILE)
$q
Definition: shib_logout.php:21
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$r