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