ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
CollectionWrapper.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
32 
34 {
36  protected \ILIAS\FileUpload\FileUpload $upload;
37  protected \ILIAS\ResourceStorage\Services $irss;
38 
39  public function __construct(
41  ) {
42  global $DIC;
43 
44  $this->irss = $DIC->resourceStorage();
45  $this->upload = $DIC->upload();
46  $this->data = $data;
47  }
48 
50  {
51  return $this->irss->collection()->id();
52  }
53 
54  protected function getNewCollectionIdAsString(): string
55  {
56  return $this->getNewCollectionId()->serialize();
57  }
58 
59  public function createEmptyCollection(): string
60  {
61  $new_id = $this->getNewCollectionId();
62  $new_collection = $this->irss->collection()->get($new_id);
63  $this->irss->collection()->store($new_collection);
64  return $new_id->serialize();
65  }
66 
67  public function getCollectionForIdString(string $rcid): ResourceCollection
68  {
69  return $this->irss->collection()->get($this->irss->collection()->id($rcid));
70  }
71 
72  public function deleteCollectionForIdString(
73  string $rcid,
74  ResourceStakeholder $stakeholder
75  ): void {
76  $id = $this->irss->collection()->id($rcid);
77  $this->irss->collection()->remove($id, $stakeholder, true);
78  }
79 
80  public function copyResourcesToDir(
81  string $rcid,
82  ResourceStakeholder $stakeholder,
83  string $dir
84  ) {
85  $collection = $this->irss->collection()->get($this->irss->collection()->id($rcid));
86  foreach ($collection->getResourceIdentifications() as $rid) {
87  $info = $this->irss->manage()->getResource($rid)
88  ->getCurrentRevision()
89  ->getInformation();
90  $stream = $this->irss->consume()->stream($rid);
91  $stream->getContents();
92  }
93  }
94 
96  ResourceCollection $collection,
97  array $file_input,
98  ResourceStakeholder $stakeholder
99  ): void {
100  $upload = $this->upload;
101 
102  if (is_array($file_input)) {
103  if (!$upload->hasBeenProcessed()) {
104  $upload->process();
105  }
106  foreach ($upload->getResults() as $name => $result) {
107  // we must check if these are files from this input
108  if (!in_array($name, $file_input["tmp_name"] ?? [], true)) {
109  continue;
110  }
111  // if the result is not OK, we skip it
112  if (!$result->isOK()) {
113  continue;
114  }
115 
116  // we store the file in the IRSS
117  $rid = $this->irss->manage()->upload(
118  $result,
119  $stakeholder
120  );
121  // and add its identification to the collection
122  $collection->add($rid);
123  }
124  // we store the collection after all files have been added
125  $this->irss->collection()->store($collection);
126  }
127  }
128 
130  ResourceCollection $collection,
131  string $directory,
132  ResourceStakeholder $stakeholder
133  ): void {
134  $sourceFS = LegacyPathHelper::deriveFilesystemFrom($directory);
135  $sourceDir = LegacyPathHelper::createRelativePath($directory);
136 
137  // check if arguments are directories
138  if (!$sourceFS->hasDir($sourceDir)) {
139  return;
140  }
141 
142  $sourceList = $sourceFS->listContents($sourceDir, false);
143 
144  foreach ($sourceList as $item) {
145  if ($item->isDir()) {
146  continue;
147  }
148  try {
149  $stream = $sourceFS->readStream($item->getPath());
150  $rid = $this->irss->manage()->stream(
151  $stream,
152  $stakeholder
153  );
154  $collection->add($rid);
155  } catch (\ILIAS\Filesystem\Exception\FileAlreadyExistsException $e) {
156  }
157  }
158  $this->irss->collection()->store($collection);
159  }
160 
161  protected function getResourceIdForIdString(string $rid): ?ResourceIdentification
162  {
163  return $this->irss->manage()->find($rid);
164  }
165 
166  public function importFileFromLegacyUpload(
167  array $file_input,
168  ResourceStakeholder $stakeholder
169  ): string {
170  $upload = $this->upload;
171 
172  if (is_array($file_input)) {
173  if (!$upload->hasBeenProcessed()) {
174  $upload->process();
175  }
176  foreach ($upload->getResults() as $name => $result) {
177  // we must check if these are files from this input
178  if ($name !== ($file_input["tmp_name"] ?? "")) {
179  continue;
180  }
181  // if the result is not OK, we skip it
182  if (!$result->isOK()) {
183  continue;
184  }
185 
186  // we store the file in the IRSS
187  $rid = $this->irss->manage()->upload(
188  $result,
189  $stakeholder
190  );
191  return $rid->serialize();
192  }
193  }
194  return "";
195  }
196 
197  public function importFileFromUploadResult(
198  UploadResult $result,
199  ResourceStakeholder $stakeholder
200  ): string {
201  // if the result is not OK, we skip it
202  if (!$result->isOK()) {
203  return "";
204  }
205 
206  // we store the file in the IRSS
207  $rid = $this->irss->manage()->upload(
208  $result,
209  $stakeholder
210  );
211  return $rid->serialize();
212  }
213 
214  public function deliverFile(string $rid): void
215  {
216  $id = $this->getResourceIdForIdString($rid);
217  if ($id) {
218  $this->irss->consume()->download($id)->run();
219  }
220  }
221 
222  public function stream(string $rid): ?FileStream
223  {
224  $id = $this->getResourceIdForIdString($rid);
225  if ($id) {
226  return $this->irss->consume()->stream($id)->getStream();
227  }
228  return null;
229  }
230 
231  public function getCollectionResourcesInfo(
232  ResourceCollection $collection
233  ): \Generator {
234  foreach ($collection->getResourceIdentifications() as $rid) {
235  $info = $this->irss->manage()->getResource($rid)
236  ->getCurrentRevision()
237  ->getInformation();
238  $src = $this->irss->consume()->src($rid)->getSrc();
239  yield $this->data->resourceInformation(
240  $rid->serialize(),
241  $info->getTitle(),
242  $info->getSize(),
243  $info->getCreationDate()->getTimestamp(),
244  $info->getMimeType(),
245  $src
246  );
247  }
248  }
249 
250  public function clone(
251  string $from_rc_id
252  ): string {
253  if ($from_rc_id !== "") {
254  $cloned_rcid = $this->irss->collection()->clone($this->irss->collection()->id($from_rc_id));
255  return $cloned_rcid->serialize();
256  }
257  return "";
258  }
259 
260  public function cloneResource(
261  string $from_rid
262  ): string {
263  if ($from_rid !== "") {
264  $cloned_rid = $this->irss->manage()->clone($this->getResourceIdForIdString($from_rid));
265  return $cloned_rid->serialize();
266  }
267  return "";
268  }
269 
270  public function deleteResource(string $rid, ResourceStakeholder $stakeholder): void
271  {
272  if ($rid !== "") {
273  $res = $this->getResourceIdForIdString($rid);
274  if ($res) {
275  $this->irss->manage()->remove($this->getResourceIdForIdString($rid), $stakeholder);
276  }
277  }
278  }
279 
281  string $rid,
282  string $entry,
283  ResourceCollection $target_collection,
284  ResourceStakeholder $target_stakeholder
285  ) {
286  $entry_parts = explode("/", $entry);
287  $zip_path = $this->stream($rid)->getMetadata("uri");
288 
289  $stream = Streams::ofFileInsideZIP(
290  $zip_path,
291  $entry
292  );
293  $feedback_rid = $this->irss->manage()->stream(
294  $stream,
295  $target_stakeholder,
296  $entry_parts[2]
297  );
298  $target_collection->add($feedback_rid);
299  $this->irss->collection()->store($target_collection);
300  }
301 }
$res
Definition: ltiservices.php:69
static createRelativePath(string $absolute_path)
Creates a relative path from an absolute path which starts with a valid storage location.
importFileFromLegacyUpload(array $file_input, ResourceStakeholder $stakeholder)
__construct(InternalDataService $data)
copyResourcesToDir(string $rcid, ResourceStakeholder $stakeholder, string $dir)
Class ChatMainBarProvider .
static ofFileInsideZIP(string $path_to_zip, string $path_inside_zip)
Definition: Streams.php:84
deleteCollectionForIdString(string $rcid, ResourceStakeholder $stakeholder)
global $DIC
Definition: feed.php:28
addEntryOfZipResourceToCollection(string $rid, string $entry, ResourceCollection $target_collection, ResourceStakeholder $target_stakeholder)
add(ResourceIdentification $identification)
Internal factory for data objects.
importFilesFromLegacyUploadToCollection(ResourceCollection $collection, array $file_input, ResourceStakeholder $stakeholder)
importFilesFromDirectoryToCollection(ResourceCollection $collection, string $directory, ResourceStakeholder $stakeholder)
static deriveFilesystemFrom(string $absolute_path)
Tries to fetch the filesystem responsible for the absolute path.
deleteResource(string $rid, ResourceStakeholder $stakeholder)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
importFileFromUploadResult(UploadResult $result, ResourceStakeholder $stakeholder)
getCollectionResourcesInfo(ResourceCollection $collection)
ILIAS FileUpload FileUpload $upload
ILIAS ResourceStorage Services $irss
The base interface for all filesystem streams.
Definition: FileStream.php:31