ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilFileDataForumRCImplementation.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
24 
26 {
27  public const FORUM_PATH_RCID = 'RCID';
28 
29  private \ILIAS\ResourceStorage\Services $irss;
30  private \ILIAS\FileUpload\FileUpload $upload;
32  private array $collection_cache = [];
34  private array $posting_cache = [];
36 
37 
38  public function __construct(private readonly int $obj_id = 0, private int $pos_id = 0)
39  {
40  global $DIC;
41  $this->irss = $DIC->resourceStorage();
42  $this->upload = $DIC->upload();
43  $this->stakeholder = new ilForumPostingFileStakeholder();
44  }
45 
46  private function getCurrentPosting(bool $use_cache = true): ilForumPost
47  {
48  return $this->getPostingById($this->pos_id, $use_cache);
49  }
50 
51  private function getPostingById(int $posting_id, bool $use_cache = true): ilForumPost
52  {
53  if ($use_cache && isset($this->posting_cache[$posting_id])) {
54  return $this->posting_cache[$posting_id];
55  }
56 
57  return $this->posting_cache[$posting_id] = new ilForumPost($posting_id);
58  }
59 
61  {
62  if (isset($this->collection_cache[$this->pos_id])) {
63  return $this->collection_cache[$this->pos_id];
64  }
65 
66  return $this->collection_cache[$this->pos_id] = $this->irss->collection()->get(
67  $this->irss->collection()->id(
68  $this->getCurrentPosting()->getRCID()
69  )
70  );
71  }
72 
73  private function getResourceIdByHash(string $hash): ?ResourceIdentification
74  {
75  foreach ($this->getCurrentCollection()->getResourceIdentifications() as $identification) {
76  $revision = $this->irss->manage()->getCurrentRevision($identification);
77  if ($revision->getTitle() === $hash) {
78  return $identification;
79  }
80  }
81 
82  return null;
83  }
84 
85  public function getObjId(): int
86  {
87  return $this->obj_id;
88  }
89 
90  public function getPosId(): int
91  {
92  return $this->pos_id;
93  }
94 
95  public function setPosId(int $posting_id): void
96  {
97  $this->pos_id = $posting_id;
98  }
99 
100  public function getForumPath(): string
101  {
102  return self::FORUM_PATH_RCID;
103  }
104 
108  public function getFilesOfPost(): array
109  {
110  $files = [];
111  foreach ($this->getCurrentCollection()->getResourceIdentifications() as $identification) {
112  $revision = $this->irss->manage()->getCurrentRevision($identification);
113  $info = $revision->getInformation();
114  $files[$info->getTitle()] = [
115  'path' => $this->irss->consume()->stream($identification)->getStream()->getMetadata('uri'),
116  'md5' => $revision->getTitle(),
117  'name' => $info->getTitle(),
118  'size' => $info->getSize(),
119  'ctime' => $info->getCreationDate()->format('Y-m-d H:i:s')
120  ];
121  }
122 
123  return $files;
124  }
125 
126  public function moveFilesOfPost(int $new_frm_id = 0): bool
127  {
128  // nothing to do here since collections are related to the post
129  return true;
130  }
131 
132  public function ilClone(int $new_obj_id, int $new_posting_id): bool
133  {
134  $current_collection_id = $this->getCurrentCollection()->getIdentification();
135  $new_collection_id = $this->irss->collection()->clone($current_collection_id);
136  $new_posting = $this->getPostingById($new_posting_id);
137  $new_posting->setRCID($new_collection_id->serialize());
138  $new_posting->update();
139 
140  return true;
141  }
142 
143  public function delete(array $posting_ids_to_delete = null): bool
144  {
145  if ($posting_ids_to_delete === null) {
146  return true;
147  }
148 
149  foreach ($posting_ids_to_delete as $post_id) {
150  $this->irss->collection()->remove(
151  $this->irss->collection()->id(
152  $this->getPostingById($post_id)->getRCID()
153  ),
155  true
156  );
157  }
158 
159  return true;
160  }
161 
162  public function storeUploadedFiles(): bool
163  {
164  if (!$this->upload->hasBeenProcessed()) {
165  $this->upload->process();
166  }
167  $collection = $this->getCurrentCollection();
168 
169  foreach ($this->upload->getResults() as $result) {
170  if (!$result->isOK()) {
171  continue;
172  }
173  $rid = $this->irss->manage()->upload(
174  $result,
175  $this->stakeholder,
176  md5($result->getName())
177  );
178  $collection->add($rid);
179  }
180  $this->irss->collection()->store($collection);
181  $posting = $this->getCurrentPosting(false);
182  $posting->setRCID($collection->getIdentification()->serialize());
183  $posting->update();
184 
185  return true;
186  }
187 
188  public function unlinkFile(string $filename): bool
189  {
190  foreach ($this->getCurrentCollection()->getResourceIdentifications() as $identification) {
191  $revision = $this->irss->manage()->getCurrentRevision($identification);
192  if ($revision->getTitle() === md5($filename)) {
193  $this->irss->manage()->remove($identification, $this->stakeholder);
194  }
195  }
196 
197  return true;
198  }
199 
203  public function getFileDataByMD5Filename(string $hashed_filename): ?array
204  {
205  foreach ($this->getCurrentCollection()->getResourceIdentifications() as $identification) {
206  $revision = $this->irss->manage()->getCurrentRevision($identification);
207  if ($revision->getTitle() === $hashed_filename) {
208  $info = $revision->getInformation();
209  return [
210  'path' => '',
211  'filename' => $info->getTitle(),
212  'clean_filename' => $info->getTitle()
213  ];
214  }
215  }
216 
217  return null;
218  }
219 
223  public function unlinkFilesByMD5Filenames($hashed_filename_or_filenames): bool
224  {
225  $hashes = is_array($hashed_filename_or_filenames)
226  ? $hashed_filename_or_filenames
227  : [$hashed_filename_or_filenames];
228 
229  foreach ($hashes as $hash) {
230  $identification = $this->getResourceIdByHash($hash);
231  if ($identification !== null) {
232  $this->irss->manage()->remove($identification, $this->stakeholder);
233  }
234  }
235 
236  return true;
237  }
238 
239  public function deliverFile(string $file): void
240  {
241  $rid = $this->getResourceIdByHash($file);
242  if ($rid !== null) {
243  $this->irss->consume()->download($rid)->run();
244  }
245  }
246 
247  public function deliverZipFile(): bool
248  {
249  // https://mantis.ilias.de/view.php?id=39910
251  $this->getCurrentPosting()->getSubject() . '.zip'
252  );
253  $rcid = $this->getCurrentCollection()->getIdentification();
254 
255  $this->irss->consume()->downloadCollection($rcid, $zip_filename)
256  ->useRevisionTitlesForFileNames(false)
257  ->run();
258 
259  return true;
260  }
261 
262  public function importFileToCollection(string $path_to_file, ilForumPost $post): void
263  {
264  if ($post->getRCID() === ilForumPost::NO_RCID || empty($post->getRCID())) {
265  $rcid = $this->irss->collection()->id();
266  $post->setRCID($rcid->serialize());
267  $post->update();
268  } else {
269  $rcid = $this->irss->collection()->id($post->getRCID());
270  }
271 
272  $collection = $this->irss->collection()->get($rcid);
273  $rid = $this->irss->manage()->stream(
274  Streams::ofResource(fopen($path_to_file, 'rb')),
275  $this->stakeholder,
276  md5(basename($path_to_file))
277  );
278  $collection->add($rid);
279  $this->irss->collection()->store($collection);
280  }
281 }
static returnASCIIFileName(string $original_filename)
Converts a UTF-8 filename to ASCII.
Definition: Delivery.php:498
importFileToCollection(string $path_to_file, ilForumPost $post)
ilClone(int $new_obj_id, int $new_posting_id)
setRCID(string $rcid)
unlinkFilesByMD5Filenames($hashed_filename_or_filenames)
__construct(private readonly int $obj_id=0, private int $pos_id=0)
global $DIC
Definition: feed.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getPostingById(int $posting_id, bool $use_cache=true)
$filename
Definition: buildRTE.php:78
$post
Definition: ltitoken.php:49