ILIAS  trunk Revision v11.0_alpha-1702-gfd3ecb7f852
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
BaseManager.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
37 
41 abstract class BaseManager
42 {
46  public function __construct(protected ResourceBuilder $resource_builder, protected CollectionBuilder $collection_builder, protected RepositoryPreloader $preloader)
47  {
48  }
49 
54  protected function checkZIP(bool|string $mimetype): void
55  {
56  if (!in_array($mimetype, ['application/zip', 'application/x-zip-compressed'])) {
57  throw new \LogicException("Cant create container resource since stream is not a ZIP");
58  }
59  }
60 
65  public function publish(ResourceIdentification $rid): void
66  {
67  $this->resource_builder->publish($this->resource_builder->get($rid));
68  }
69 
74  public function unpublish(ResourceIdentification $rid): void
75  {
76  $this->resource_builder->unpublish($this->resource_builder->get($rid));
77  }
78 
79  protected function newStreamBased(
80  FileStream $stream,
81  ResourceStakeholder $stakeholder,
82  ResourceType $type,
83  ?string $revision_title = null
85  $info_resolver = new StreamInfoResolver(
86  $stream,
87  1,
88  $stakeholder->getOwnerOfNewResources(),
89  $revision_title ?? $stream->getMetadata()['uri']
90  );
91 
92  $resource = $this->resource_builder->newFromStream(
93  $stream,
94  $info_resolver,
95  true,
96  $type
97  );
98  $resource->addStakeholder($stakeholder);
99  $this->resource_builder->store($resource);
100 
101  return $resource->getIdentification();
102  }
103 
104  public function find(string $identification): ?ResourceIdentification
105  {
106  $resource_identification = new ResourceIdentification($identification);
107 
108  if ($this->resource_builder->has($resource_identification)) {
109  return $resource_identification;
110  }
111 
112  return null;
113  }
114 
115  // Resources
116 
118  {
119  $this->preloader->preload([$i->serialize()]);
120  return $this->resource_builder->get($i);
121  }
122 
123  public function remove(ResourceIdentification $identification, ResourceStakeholder $stakeholder): void
124  {
125  $this->resource_builder->remove($this->resource_builder->get($identification), $stakeholder);
126  if (!$this->resource_builder->has($identification)) {
127  $this->collection_builder->notififyResourceDeletion($identification);
128  }
129  }
130 
131  public function clone(ResourceIdentification $identification): ResourceIdentification
132  {
133  $resource = $this->resource_builder->clone($this->resource_builder->get($identification));
134 
135  return $resource->getIdentification();
136  }
137 
138  // Revision
139 
146  public function appendNewRevision(
147  ResourceIdentification $identification,
148  UploadResult $result,
149  ResourceStakeholder $stakeholder,
150  ?string $revision_title = null,
151  bool $draft = false
152  ): Revision {
153  if ($result->isOK()) {
154  if (!$this->resource_builder->has($identification)) {
155  throw new \LogicException(
156  "Resource not found, can't append new version in: " . $identification->serialize()
157  );
158  }
159  $resource = $this->resource_builder->get($identification);
160  if ($resource->getType() === ResourceType::CONTAINER) {
161  $this->checkZIP($result->getMimeType());
162  }
163 
164  $info_resolver = new UploadInfoResolver(
165  $result,
166  $resource->getMaxRevision(true) + 1,
167  $stakeholder->getOwnerOfNewResources(),
168  $revision_title ?? $result->getName()
169  );
170 
171  $this->resource_builder->append(
172  $resource,
173  $result,
174  $info_resolver,
175  $draft ? RevisionStatus::DRAFT : RevisionStatus::PUBLISHED
176  );
177  $resource->addStakeholder($stakeholder);
178 
179  $this->resource_builder->store($resource);
180 
181  return $resource->getCurrentRevisionIncludingDraft();
182  }
183  throw new \LogicException("Can't handle UploadResult: " . $result->getStatus()->getMessage());
184  }
185 
192  public function replaceWithUpload(
193  ResourceIdentification $identification,
194  UploadResult $result,
195  ResourceStakeholder $stakeholder,
196  ?string $revision_title = null
197  ): Revision {
198  if ($result->isOK()) {
199  if (!$this->resource_builder->has($identification)) {
200  throw new \LogicException(
201  "Resource not found, can't append new version in: " . $identification->serialize()
202  );
203  }
204  $resource = $this->resource_builder->get($identification);
205  if ($resource->getType() === ResourceType::CONTAINER) {
206  $this->checkZIP($result->getMimeType());
207  }
208  if ($resource->getCurrentRevisionIncludingDraft()->getStatus() === RevisionStatus::DRAFT) {
209  throw new \LogicException(
210  "Can't replace DRAFT revision, use appendNewRevision instead to update the DRAFT"
211  );
212  }
213  $info_resolver = new UploadInfoResolver(
214  $result,
215  $resource->getMaxRevision(true) + 1,
216  $stakeholder->getOwnerOfNewResources(),
217  $revision_title ?? $result->getName()
218  );
219  $this->resource_builder->replaceWithUpload(
220  $resource,
221  $result,
222  $info_resolver
223  );
224  $resource->addStakeholder($stakeholder);
225 
226  $this->resource_builder->store($resource);
227 
228  return $resource->getCurrentRevisionIncludingDraft();
229  }
230  throw new \LogicException("Can't handle UploadResult: " . $result->getStatus()->getMessage());
231  }
232 
240  ResourceIdentification $identification,
241  FileStream $stream,
242  ResourceStakeholder $stakeholder,
243  ?string $revision_title = null,
244  bool $draft = false
245  ): Revision {
246  if (!$this->resource_builder->has($identification)) {
247  throw new \LogicException(
248  "Resource not found, can't append new version in: " . $identification->serialize()
249  );
250  }
251 
252  $resource = $this->resource_builder->get($identification);
253  if ($resource->getType() === ResourceType::CONTAINER) {
254  $this->checkZIP(mime_content_type($stream->getMetadata()['uri']));
255  }
256  $info_resolver = new StreamInfoResolver(
257  $stream,
258  $resource->getMaxRevision(true) + 1,
259  $stakeholder->getOwnerOfNewResources(),
260  $revision_title ?? $stream->getMetadata()['uri']
261  );
262 
263  $this->resource_builder->appendFromStream(
264  $resource,
265  $stream,
266  $info_resolver,
267  $draft ? RevisionStatus::DRAFT : RevisionStatus::PUBLISHED,
268  true
269  );
270 
271  $resource->addStakeholder($stakeholder);
272 
273  $this->resource_builder->store($resource);
274 
275  return $resource->getCurrentRevisionIncludingDraft();
276  }
277 
284  public function replaceWithStream(
285  ResourceIdentification $identification,
286  FileStream $stream,
287  ResourceStakeholder $stakeholder,
288  ?string $revision_title = null
289  ): Revision {
290  if (!$this->resource_builder->has($identification)) {
291  throw new \LogicException(
292  "Resource not found, can't append new version in: " . $identification->serialize()
293  );
294  }
295 
296  $resource = $this->resource_builder->get($identification);
297  if ($resource->getCurrentRevisionIncludingDraft()->getStatus() === RevisionStatus::DRAFT) {
298  throw new \LogicException(
299  "Can't replace DRAFT revision, use appendNewRevisionFromStream instead to update the DRAFT"
300  );
301  }
302  if ($resource->getType() === ResourceType::CONTAINER) {
303  $this->checkZIP(mime_content_type($stream->getMetadata()['uri']));
304  }
305  $info_resolver = new StreamInfoResolver(
306  $stream,
307  $resource->getMaxRevision(true) + 1,
308  $stakeholder->getOwnerOfNewResources(),
309  $revision_title ?? $stream->getMetadata()['uri']
310  );
311 
312  $this->resource_builder->replaceWithStream(
313  $resource,
314  $stream,
315  $info_resolver,
316  true
317  );
318  $resource->addStakeholder($stakeholder);
319 
320  $this->resource_builder->store($resource);
321 
322  return $resource->getCurrentRevisionIncludingDraft();
323  }
324 
325  public function getCurrentRevision(
326  ResourceIdentification $identification
327  ): Revision {
328  return $this->resource_builder->get($identification)->getCurrentRevision();
329  }
330 
332  ResourceIdentification $identification
333  ): Revision {
334  return $this->resource_builder->get($identification)->getCurrentRevisionIncludingDraft();
335  }
336 
337  public function updateRevision(Revision $revision): bool
338  {
339  $this->resource_builder->storeRevision($revision);
340 
341  return true;
342  }
343 
344  public function rollbackRevision(ResourceIdentification $identification, int $revision_number): bool
345  {
346  $resource = $this->resource_builder->get($identification);
347  $this->resource_builder->appendFromRevision($resource, $revision_number);
348  $this->resource_builder->store($resource);
349 
350  return true;
351  }
352 
353  public function removeRevision(ResourceIdentification $identification, int $revision_number): bool
354  {
355  $resource = $this->resource_builder->get($identification);
356  $this->resource_builder->removeRevision($resource, $revision_number);
357  $this->resource_builder->store($resource);
358 
359  return true;
360  }
361 }
publish(ResourceIdentification $rid)
Publish a resource.
Definition: BaseManager.php:65
removeRevision(ResourceIdentification $identification, int $revision_number)
getCurrentRevision(ResourceIdentification $identification)
appendNewRevisionFromStream(ResourceIdentification $identification, FileStream $stream, ResourceStakeholder $stakeholder, ?string $revision_title=null, bool $draft=false)
Append a new revision from a stream.
replaceWithStream(ResourceIdentification $identification, FileStream $stream, ResourceStakeholder $stakeholder, ?string $revision_title=null)
__construct(protected ResourceBuilder $resource_builder, protected CollectionBuilder $collection_builder, protected RepositoryPreloader $preloader)
Manager constructor.
Definition: BaseManager.php:46
unpublish(ResourceIdentification $rid)
Unpublish a resource.
Definition: BaseManager.php:74
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
clone(ResourceIdentification $identification)
replaceWithUpload(ResourceIdentification $identification, UploadResult $result, ResourceStakeholder $stakeholder, ?string $revision_title=null)
getResource(ResourceIdentification $i)
appendNewRevision(ResourceIdentification $identification, UploadResult $result, ResourceStakeholder $stakeholder, ?string $revision_title=null, bool $draft=false)
Append a new revision from an UploadResult.
getCurrentRevisionIncludingDraft(ResourceIdentification $identification)
rollbackRevision(ResourceIdentification $identification, int $revision_number)
The base interface for all filesystem streams.
Definition: FileStream.php:31
newStreamBased(FileStream $stream, ResourceStakeholder $stakeholder, ResourceType $type, ?string $revision_title=null)
Definition: BaseManager.php:79