ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
ResourceDBRepository.php
Go to the documentation of this file.
1 <?php
2 
20 
24 
31 {
32  public const TABLE_NAME = 'il_resource';
33  public const IDENTIFICATION = 'rid';
34 
38  protected array $cache = [];
39  protected \ilDBInterface $db;
40 
41  public function __construct(\ilDBInterface $db)
42  {
43  $this->db = $db;
44  }
45 
49  public function getNamesForLocking(): array
50  {
51  return [self::TABLE_NAME];
52  }
53 
57  public function blank(ResourceIdentification $identification): StorableResource
58  {
59  return new StorableFileResource($identification);
60  }
61 
65  public function get(ResourceIdentification $identification): StorableResource
66  {
67  if (isset($this->cache[$identification->serialize()])) {
68  return $this->cache[$identification->serialize()];
69  }
70  $resource = $this->blank($identification);
71 
72  $q = "SELECT " . self::IDENTIFICATION . ", storage_id FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s";
73  $r = $this->db->queryF($q, ['text'], [$identification->serialize()]);
74  $d = $this->db->fetchObject($r);
75 
76  $resource->setStorageID($d->storage_id);
77 
78  $this->cache[$identification->serialize()] = $resource;
79 
80  return $resource;
81  }
82 
86  public function has(ResourceIdentification $identification): bool
87  {
88  if (isset($this->cache[$identification->serialize()])) {
89  return true;
90  }
91  $q = "SELECT " . self::IDENTIFICATION . " FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s";
92  $r = $this->db->queryF($q, ['text'], [$identification->serialize()]);
93 
94  return (bool)$r->numRows() > 0;
95  }
96 
100  public function store(StorableResource $resource): void
101  {
102  $rid = $resource->getIdentification()->serialize();
103  if ($this->has($resource->getIdentification())) {
104  // UPDATE
105  $this->db->update(
106  self::TABLE_NAME,
107  [
108  self::IDENTIFICATION => ['text', $rid],
109  'storage_id' => ['text', $resource->getStorageID()],
110  ],
111  [
112  self::IDENTIFICATION => ['text', $rid],
113  ]
114  );
115  } else {
116  // CREATE
117  $this->db->insert(
118  self::TABLE_NAME,
119  [
120  self::IDENTIFICATION => ['text', $rid],
121  'storage_id' => ['text', $resource->getStorageID()],
122  ]
123  );
124  }
125  $this->cache[$rid] = $resource;
126  }
127 
131  public function delete(StorableResource $resource): void
132  {
133  $rid = $resource->getIdentification()->serialize();
134  $this->db->manipulateF(
135  "DELETE FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s",
136  ['text'],
137  [$rid]
138  );
139  unset($this->cache[$rid]);
140  }
141 
145  public function getAll(): \Generator
146  {
147  yield from [];
148  }
149 
150  public function preload(array $identification_strings): void
151  {
152  $r = $this->db->query(
153  "SELECT rid, storage_id FROM " . self::TABLE_NAME . " WHERE "
154  . $this->db->in(self::IDENTIFICATION, $identification_strings, false, 'text')
155  );
156  while ($d = $this->db->fetchAssoc($r)) {
157  $this->populateFromArray($d);
158  }
159  }
160 
161  public function populateFromArray(array $data): void
162  {
163  $resource = $this->blank(new ResourceIdentification($data['rid']));
164  $resource->setStorageID($data['storage_id']);
165  $this->cache[$data['rid']] = $resource;
166  }
167 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
for($i=6; $i< 13; $i++) for($i=1; $i< 13; $i++) $d
Definition: date.php:296