ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
RevisionDBRepository.php
Go to the documentation of this file.
1 <?php declare(strict_types=1);
2 
4 
16 
23 {
24  const TABLE_NAME = 'il_resource_revision';
25  const IDENTIFICATION = 'rid';
29  protected $db;
33  protected $cache = [];
34 
38  public function __construct(\ilDBInterface $db)
39  {
40  $this->db = $db;
41  }
42 
43  public function getNamesForLocking() : array
44  {
45  return [self::TABLE_NAME];
46  }
47 
48  public function blankFromUpload(
49  InfoResolver $info_resolver,
50  StorableResource $resource,
53  $new_version_number = $info_resolver->getNextVersionNumber();
54  $revision = new UploadedFileRevision($resource->getIdentification(), $result);
55  $revision->setVersionNumber($new_version_number);
56 
57  return $revision;
58  }
59 
60  public function blankFromStream(
61  InfoResolver $info_resolver,
62  StorableResource $resource,
63  FileStream $stream,
64  bool $keep_original = false
65  ) : FileStreamRevision {
66  $new_version_number = $info_resolver->getNextVersionNumber();
67  $revision = new FileStreamRevision($resource->getIdentification(), $stream, $keep_original);
68  $revision->setVersionNumber($new_version_number);
69 
70  return $revision;
71  }
72 
73  public function blankFromClone(
74  InfoResolver $info_resolver,
75  StorableResource $resource,
76  FileRevision $revision_to_clone
77  ) : CloneRevision {
78  $new_version_number = $info_resolver->getNextVersionNumber();
79  $revision = new CloneRevision($resource->getIdentification(), $revision_to_clone);
80  $revision->setVersionNumber($new_version_number);
81 
82  return $revision;
83  }
84 
88  public function store(Revision $revision) : void
89  {
90  $rid = $revision->getIdentification()->serialize();
91  $r = $this->db->queryF(
92  "SELECT " . self::IDENTIFICATION . " FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s AND version_number = %s",
93  ['text', 'integer'],
94  [$rid, $revision->getVersionNumber()]
95  );
96 
97  if ($r->numRows() > 0) {
98  // UPDATE
99  $this->db->update(
100  self::TABLE_NAME,
101  [
102  'available' => ['integer', true],
103  'owner_id' => ['integer', $revision->getOwnerId()],
104  'title' => ['text', $revision->getTitle()]
105  ],
106  [
107  self::IDENTIFICATION => ['text', $rid],
108  'version_number' => ['integer', $revision->getVersionNumber()],
109  ]
110  );
111  } else {
112  // CREATE
113  $this->db->insert(
114  self::TABLE_NAME,
115  [
116  self::IDENTIFICATION => ['text', $rid],
117  'version_number' => ['integer', $revision->getVersionNumber()],
118  'available' => ['integer', true],
119  'owner_id' => ['integer', $revision->getOwnerId()],
120  'title' => ['text', $revision->getTitle()]
121  ]
122  );
123  }
124  $this->cache[$rid][$revision->getVersionNumber()] = $revision;
125  }
126 
130  public function get(StorableResource $resource) : RevisionCollection
131  {
132  $collection = new RevisionCollection($resource->getIdentification());
133 
134  $rid = $resource->getIdentification()->serialize();
135  if (isset($this->cache[$rid]) && is_array($this->cache[$rid])) {
136  foreach ($this->cache[$rid] as $rev) {
137  $collection->add($rev);
138  }
139  return $collection;
140  }
141  $r = $this->db->queryF(
142  "SELECT * FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s",
143  ['text'],
144  [$rid]
145  );
146  while ($d = $this->db->fetchObject($r)) {
147  $revision = new FileRevision(new ResourceIdentification($d->rid));
148  $revision->setVersionNumber((int) $d->version_number);
149  $revision->setOwnerId((int) $d->owner_id);
150  $revision->setTitle((string) $d->title);
151  $collection->add($revision);
152  $this->cache[$d->rid][(int) $d->version_number] = $revision;
153  }
154 
155  return $collection;
156  }
157 
161  public function delete(Revision $revision) : void
162  {
163  $rid = $revision->getIdentification()->serialize();
164  $this->db->manipulateF(
165  "DELETE FROM " . self::TABLE_NAME . " WHERE " . self::IDENTIFICATION . " = %s AND version_number = %s",
166  ['text', 'integer'],
167  [$rid, $revision->getVersionNumber()]
168  );
169  unset($this->cache[$rid][$revision->getVersionNumber()]);
170  }
171 
172  public function preload(array $identification_strings) : void
173  {
174  $r = $this->db->query(
175  "SELECT * FROM " . self::TABLE_NAME . " WHERE " . $this->db->in(
176  self::IDENTIFICATION,
177  $identification_strings,
178  false,
179  'text'
180  )
181  );
182  while ($d = $this->db->fetchAssoc($r)) {
183  $this->populateFromArray($d);
184  }
185  }
186 
187  public function populateFromArray(array $data) : void
188  {
189  $revision = new FileRevision(new ResourceIdentification($data['rid']));
190  $revision->setVersionNumber((int) $data['version_number']);
191  $revision->setOwnerId((int) $data['owner_id']);
192  $revision->setTitle((string) $data['revision_title']);
193  $this->cache[$data['rid']][(int) $data['version_number']] = $revision;
194  }
195 }
blankFromUpload(InfoResolver $info_resolver, StorableResource $resource, UploadResult $result)
$data
Definition: storeScorm.php:23
$result
blankFromStream(InfoResolver $info_resolver, StorableResource $resource, FileStream $stream, bool $keep_original=false)
Interface FileStream The base interface for all filesystem streams.
Definition: FileStream.php:17
for($i=6; $i< 13; $i++) for($i=1; $i< 13; $i++) $d
Definition: date.php:296
blankFromClone(InfoResolver $info_resolver, StorableResource $resource, FileRevision $revision_to_clone)