ILIAS  trunk Revision v11.0_alpha-1702-gfd3ecb7f852
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
RevisionDBRepository.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
35 
42 {
43  public const TABLE_NAME = 'il_resource_revision';
44  public const IDENTIFICATION = 'rid';
48  protected array $cache = [];
49 
50  public function __construct(protected \ilDBInterface $db)
51  {
52  }
53 
57  public function getNamesForLocking(): array
58  {
59  return [self::TABLE_NAME];
60  }
61 
62  public function blankFromUpload(
63  InfoResolver $info_resolver,
64  StorableResource $resource,
65  UploadResult $result,
66  RevisionStatus $status
68  $new_version_number = $info_resolver->getNextVersionNumber();
69  $revision = new UploadedFileRevision($resource->getIdentification(), $result);
70  $revision->setStorageID($resource->getStorageID());
71  $revision->setVersionNumber($new_version_number);
72  $revision->setStatus($status);
73 
74  return $revision;
75  }
76 
77  public function blankFromStream(
78  InfoResolver $info_resolver,
79  StorableResource $resource,
80  FileStream $stream,
81  RevisionStatus $status,
82  bool $keep_original = false
84  $new_version_number = $info_resolver->getNextVersionNumber();
85  $revision = new FileStreamRevision($resource->getIdentification(), $stream, $keep_original);
86  $revision->setStorageID($resource->getStorageID());
87  $revision->setVersionNumber($new_version_number);
88  $revision->setStatus($status);
89 
90  return $revision;
91  }
92 
93  public function blankFromClone(
94  InfoResolver $info_resolver,
95  StorableResource $resource,
96  FileRevision $revision_to_clone
97  ): CloneRevision {
98  $new_version_number = $info_resolver->getNextVersionNumber();
99  $revision = new CloneRevision($resource->getIdentification(), $revision_to_clone);
100  $revision->setStorageID($revision_to_clone->getStorageID());
101  $revision->setVersionNumber($new_version_number);
102  $revision->setStatus(RevisionStatus::PUBLISHED);
103 
104  return $revision;
105  }
106 
107  public function store(Revision $revision): void
108  {
109  $rid = $revision->getIdentification()->serialize();
110  $r = $this->db->queryF(
111  "SELECT " . self::IDENTIFICATION . " FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s AND version_number = %s",
112  ['text', 'integer'],
113  [$rid, $revision->getVersionNumber()]
114  );
115 
116  if ($r->numRows() > 0) {
117  // UPDATE
118  $this->db->update(
119  self::TABLE_NAME,
120  [
121  'available' => ['integer', true],
122  'owner_id' => ['integer', $revision->getOwnerId()],
123  'title' => ['text', $revision->getTitle()],
124  'status' => ['text', $revision->getStatus()->value],
125  ],
126  [
127  self::IDENTIFICATION => ['text', $rid],
128  'version_number' => ['integer', $revision->getVersionNumber()],
129  ]
130  );
131  } else {
132  // CREATE
133  $this->db->insert(
134  self::TABLE_NAME,
135  [
136  self::IDENTIFICATION => ['text', $rid],
137  'version_number' => ['integer', $revision->getVersionNumber()],
138  'available' => ['integer', true],
139  'owner_id' => ['integer', $revision->getOwnerId()],
140  'title' => ['text', $revision->getTitle()],
141  'status' => ['text', $revision->getStatus()->value],
142  ]
143  );
144  }
145  $this->cache[$rid][$revision->getVersionNumber()] = $revision;
146  }
147 
151  public function get(StorableResource $resource): RevisionCollection
152  {
153  $collection = new RevisionCollection($resource->getIdentification());
154 
155  $rid = $resource->getIdentification()->serialize();
156  if (isset($this->cache[$rid]) && is_array($this->cache[$rid])) {
157  foreach ($this->cache[$rid] as $rev) {
158  $collection->add($rev);
159  }
160  return $collection;
161  }
162  $r = $this->db->queryF(
163  "SELECT * FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s",
164  ['text'],
165  [$rid]
166  );
167  while ($d = $this->db->fetchObject($r)) {
168  $revision = new FileRevision(new ResourceIdentification($d->rid));
169  $revision->setVersionNumber((int) $d->version_number);
170  $revision->setOwnerId((int) $d->owner_id);
171  $revision->setTitle((string) $d->title);
172  $revision->setStatus(RevisionStatus::from((int) $d->status));
173  $collection->add($revision);
174  $this->cache[$d->rid][(int) $d->version_number] = $revision;
175  }
176 
177  return $collection;
178  }
179 
183  public function delete(Revision $revision): void
184  {
185  $rid = $revision->getIdentification()->serialize();
186  $this->db->manipulateF(
187  "DELETE FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s AND version_number = %s",
188  ['text', 'integer'],
189  [$rid, $revision->getVersionNumber()]
190  );
191  unset($this->cache[$rid][$revision->getVersionNumber()]);
192  }
193 
194  public function preload(array $identification_strings): void
195  {
196  $r = $this->db->query(
197  "SELECT * FROM " . self::TABLE_NAME . " WHERE " . $this->db->in(
198  self::IDENTIFICATION,
199  $identification_strings,
200  false,
201  'text'
202  )
203  );
204  while ($d = $this->db->fetchAssoc($r)) {
205  $this->populateFromArray($d);
206  }
207  }
208 
209  public function populateFromArray(array $data): void
210  {
211  $revision = new FileRevision(new ResourceIdentification($data['rid']));
212  $revision->setVersionNumber((int) $data['version_number']);
213  $revision->setOwnerId((int) $data['owner_id']);
214  $revision->setTitle((string) $data['revision_title']);
215  $revision->setStatus(RevisionStatus::from((int) $data['status']));
216  $this->cache[$data['rid']][(int) $data['version_number']] = $revision;
217  }
218 }
blankFromStream(InfoResolver $info_resolver, StorableResource $resource, FileStream $stream, RevisionStatus $status, bool $keep_original=false)
blankFromUpload(InfoResolver $info_resolver, StorableResource $resource, UploadResult $result, RevisionStatus $status)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
The base interface for all filesystem streams.
Definition: FileStream.php:31
blankFromClone(InfoResolver $info_resolver, StorableResource $resource, FileRevision $revision_to_clone)
$r