ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilResourceStorageMigrationHelper.php
Go to the documentation of this file.
1<?php
2
40
46{
47 protected string $client_data_dir;
53 protected Manager $manager;
54
60 public function __construct(
61 protected ResourceStakeholder $stakeholder,
62 Environment $environment
63 ) {
65 $db = $environment->getResource(Environment::RESOURCE_DATABASE);
66 $ilias_ini = $environment->getResource(Environment::RESOURCE_ILIAS_INI);
67 $client_id = $environment->getResource(Environment::RESOURCE_CLIENT_ID);
68 $data_dir = $ilias_ini->readVariable('clients', 'datadir');
69 $client_data_dir = "{$data_dir}/{$client_id}";
70 if (!defined("CLIENT_WEB_DIR")) {
71 define("CLIENT_WEB_DIR", dirname(__DIR__, 5) . "/public/data/" . $client_id);
72 }
73 if (!defined("ILIAS_WEB_DIR")) {
74 define("ILIAS_WEB_DIR", 'public/data');
75 }
76 if (!defined("CLIENT_ID")) {
77 define("CLIENT_ID", $client_id);
78 }
79 if (!defined("ILIAS_DATA_DIR")) {
80 define("ILIAS_DATA_DIR", $data_dir);
81 }
82 $this->client_data_dir = $client_data_dir;
83 $this->database = $db;
84
85 if (!is_writable("{$data_dir}/{$client_id}/storage/fsv2")) {
86 throw new Exception('storage directory is not writable, abort...');
87 }
88
89 // Build Container
90 $init = new InitResourceStorage();
91 $container = new Container();
92 $container['ilDB'] = $db;
93 $storageConfiguration = new LocalConfig($client_data_dir);
95 $container['filesystem.storage'] = $f->getLocal($storageConfiguration);
96
97 $this->resource_builder = $init->getResourceBuilder($container);
98 $this->flavour_builder = $init->getFlavourBuilder($container);
99 $this->collection_builder = new CollectionBuilder(
100 new CollectionDBRepository($db),
101 new Subject()
102 );
103
104 $this->repositories = $container[InitResourceStorage::D_REPOSITORIES];
105
106 $this->manager = new Manager(
107 $this->resource_builder,
108 $this->collection_builder,
110 );
111 }
112
116 public static function getPreconditions(): array
117 {
118 return [
125 ];
126 }
127
128 public function getClientDataDir(): string
129 {
131 }
132
133 public function getDatabase(): ilDBInterface
134 {
135 return $this->database;
136 }
137
139 {
140 return $this->stakeholder;
141 }
142
144 {
146 }
148 {
150 }
151
153 {
155 }
156
157 public function getManager(): Manager
158 {
159 return $this->manager;
160 }
161
163 ResourceIdentification $resource_identification,
164 ResourceStakeholder $old_stakeholder,
165 ResourceStakeholder $new_stakeholder,
166 ?int $new_owner_id = null
167 ): void {
168 $resource = $this->manager->getResource($resource_identification);
169 $resource->removeStakeholder($old_stakeholder);
170 $this->repositories->getStakeholderRepository()->deregister($resource_identification, $old_stakeholder);
171 $resource->addStakeholder($new_stakeholder);
172 $this->repositories->getStakeholderRepository()->register($resource_identification, $new_stakeholder);
173
174 if ($new_owner_id !== null) {
175 foreach ($resource->getAllRevisionsIncludingDraft() as $revision) {
176 $revision->setOwnerId($new_owner_id);
177 }
178 }
179
180 $this->resource_builder->store($resource);
181 }
182
183
184 public function moveFilesOfPathToCollection(
185 string $absolute_path,
186 int $resource_owner_id,
187 int $collection_owner_user_id = ResourceCollection::NO_SPECIFIC_OWNER,
188 ?Closure $file_name_callback = null,
189 ?Closure $revision_name_callback = null
191 $collection = $this->getCollectionBuilder()->new($collection_owner_user_id);
193 foreach (new DirectoryIterator($absolute_path) as $file_info) {
194 if (!$file_info->isFile()) {
195 continue;
196 }
197 $resource_id = $this->movePathToStorage(
198 $file_info->getRealPath(),
199 $resource_owner_id,
200 $file_name_callback,
201 $revision_name_callback
202 );
203 if ($resource_id !== null) {
204 $collection->add($resource_id);
205 }
206 }
207
208 if ($this->getCollectionBuilder()->store($collection)) {
209 return $collection->getIdentification();
210 }
211 return null;
212 }
213
215 string $absolute_base_path,
216 string $pattern,
217 int $resource_owner_id,
218 int $collection_owner_user_id = ResourceCollection::NO_SPECIFIC_OWNER,
219 ?Closure $file_name_callback = null,
220 ?Closure $revision_name_callback = null
222 $collection = $this->getCollectionBuilder()->new($collection_owner_user_id);
223
224 $regex_iterator = $this->buildRecursivePatternIterator($absolute_base_path, $pattern);
225
226 foreach ($regex_iterator as $file_info) {
227 if (!$file_info->isFile()) {
228 continue;
229 }
230 $resource_id = $this->movePathToStorage(
231 $file_info->getRealPath(),
232 $resource_owner_id,
233 $file_name_callback,
234 $revision_name_callback
235 );
236 if ($resource_id !== null) {
237 $collection->add($resource_id);
238 }
239 }
240 if ($collection->count() === 0) {
241 return null;
242 }
243
244 if ($this->getCollectionBuilder()->store($collection)) {
245 return $collection->getIdentification();
246 }
247 return null;
248 }
249
251 string $absolute_base_path,
252 string $pattern,
253 int $resource_owner_id,
254 ?Closure $file_name_callback = null,
255 ?Closure $revision_name_callback = null
257 $regex_iterator = $this->buildRecursivePatternIterator($absolute_base_path, $pattern);
258
259 foreach ($regex_iterator as $file_info) {
260 if (!$file_info->isFile()) {
261 continue;
262 }
263 $resource_id = $this->movePathToStorage(
264 $file_info->getRealPath(),
265 $resource_owner_id,
266 $file_name_callback,
267 $revision_name_callback
268 );
269 if ($resource_id !== null) {
270 return $resource_id; // stop after first file
271 }
272 }
273
274 return null;
275 }
276
277 public function movePathToStorage(
278 string $absolute_path,
279 int $owner_user_id,
280 ?Closure $file_name_callback = null,
281 ?Closure $revision_name_callback = null,
282 ?bool $copy_instead_of_move = false
284 try {
285 // in some cases fopen throws a warning instead of returning false
286 $open_path = fopen($absolute_path, 'rb');
287 } catch (Throwable) {
288 return null;
289 }
290
291 if ($open_path === false) {
292 return null;
293 }
294 $stream = Streams::ofResource($open_path);
295
296 // create new resource from legacy files stream
297 $revision_title = $revision_name_callback !== null
298 ? $revision_name_callback(basename($absolute_path))
299 : basename($absolute_path);
300
301 $file_name = $file_name_callback !== null
302 ? $file_name_callback(basename($absolute_path))
303 : null;
304
305 $resource = $this->resource_builder->newFromStream(
306 $stream,
308 $stream,
309 1,
310 $owner_user_id,
311 $revision_title,
312 $file_name
313 ),
314 $copy_instead_of_move ?? false
315 );
316
317 // add stakeholder and store resource
318 $resource->addStakeholder($this->stakeholder);
319 $this->resource_builder->store($resource);
320
321 return $resource->getIdentification();
322 }
323
325 string $absolute_path_to_directory,
326 int $owner_user_id,
328 // check if directory exists
329 if (!is_dir($absolute_path_to_directory)) {
330 return null;
331 }
332
333 $zip = new Zip(
334 (new ZipOptions())->withDirectoryHandling(ZipDirectoryHandling::KEEP_STRUCTURE)
335 );
336 $zip->addDirectory($absolute_path_to_directory);
337 try {
338 $zip_stream = $zip->get();
339 } catch (Throwable $e) {
340 $zip->destroy();
341 return null; // could not create zip
342 }
343
344 $resource = $this->resource_builder->newFromStream(
345 $zip_stream,
347 $zip_stream,
348 1,
349 $owner_user_id,
350 basename($absolute_path_to_directory),
351 basename($absolute_path_to_directory)
352 ),
353 true,
355 );
356
357 // add stakeholder and store resource
358 $resource->addStakeholder($this->stakeholder);
359 $this->resource_builder->store($resource);
360
361 // ZIP archive is not needed anymore
362 $zip->destroy();
363
364 return $resource->getIdentification();
365 }
366
368 string $absolute_base_path,
369 string $pattern = '.*'
370 ): RecursiveRegexIterator {
371 return new RecursiveRegexIterator(
372 new RecursiveDirectoryIterator($absolute_base_path),
373 $pattern,
374 RecursiveRegexIterator::MATCH
375 );
376 }
377}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
This class is used to configure the local filesystem adapter.
Definition: LocalConfig.php:30
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
Responsible for loading the Resource Storage into the dependency injection container of ILIAS.
This class attempt to achieve a set of database update steps.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilResourceStorageDB90.
buildRecursivePatternIterator(string $absolute_base_path, string $pattern='.*')
moveResourceToNewStakeholderAndOwner(ResourceIdentification $resource_identification, ResourceStakeholder $old_stakeholder, ResourceStakeholder $new_stakeholder, ?int $new_owner_id=null)
__construct(protected ResourceStakeholder $stakeholder, Environment $environment)
ilResourceStorageMigrationHelper constructor.
movePathToStorage(string $absolute_path, int $owner_user_id, ?Closure $file_name_callback=null, ?Closure $revision_name_callback=null, ?bool $copy_instead_of_move=false)
moveDirectoryToContainerResource(string $absolute_path_to_directory, int $owner_user_id,)
moveFilesOfPatternToCollection(string $absolute_base_path, string $pattern, int $resource_owner_id, int $collection_owner_user_id=ResourceCollection::NO_SPECIFIC_OWNER, ?Closure $file_name_callback=null, ?Closure $revision_name_callback=null)
moveFirstFileOfPatternToStorage(string $absolute_base_path, string $pattern, int $resource_owner_id, ?Closure $file_name_callback=null, ?Closure $revision_name_callback=null)
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
An environment holds resources to be used in the setup process.
Definition: Environment.php:28
getResource(string $id)
Consumers of this method should check if the result is what they expect, e.g.
Interface ilDBInterface.
$client_id
Definition: ltiauth.php:67
try
handle Lrs Init
Definition: xapiproxy.php:86
if(!file_exists('../ilias.ini.php'))
$container
@noRector
Definition: wac.php:37