ILIAS  release_8 Revision v8.24
Migrator.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25
32{
33 protected bool $clean_up = true;
36 private \ilDBInterface $database;
37 protected string $filesystem_base_path;
38
42 public function __construct(
47 ) {
48 $this->handler_factory = $handler_factory;
49 $this->resource_builder = $resource_builder;
50 $this->database = $database;
51 $this->filesystem_base_path = $filesystem_base_path;
52 }
53
54 public function migrate(StorableResource $resource, string $to_handler_id): bool
55 {
56 $existing_handler = $this->handler_factory->getHandlerForResource($resource);
57 $existing_path = $this->filesystem_base_path . '/' . $existing_handler->getFullContainerPath(
58 $resource->getIdentification()
59 );
60
61 $new_handler = $this->handler_factory->getHandlerForStorageId($to_handler_id);
62 $destination_path = $this->filesystem_base_path . '/' . $new_handler->getFullContainerPath(
63 $resource->getIdentification()
64 );
65
66 if (!file_exists($existing_path)) {
67 // File is not existing, we MUST delete the resource
68 $this->resource_builder->remove($resource);
69 return false;
70 }
71
72 if (!is_dir(dirname($destination_path)) && !mkdir(dirname($destination_path), 0777, true)) {
73 return false;
74 }
75 if (rename($existing_path, $destination_path)) {
76 $this->database->manipulateF(
77 "UPDATE il_resource SET storage_id = %s WHERE identification = %s LIMIT 1",
78 ['text', 'text'],
79 [$to_handler_id, $resource->getIdentification()->serialize()]
80 );
81
82 // remove old
83 if ($this->clean_up) {
84 $existing_handler->cleanUpContainer($resource);
85 }
86
87 return true;
88 }
89 return false;
90 }
91
92 public function removeEmptySubFolders(string $path): bool
93 {
94 $empty = true;
95 foreach (glob($path . DIRECTORY_SEPARATOR . "*") as $file) {
96 $empty &= is_dir($file) && $this->removeEmptySubFolders($file);
97 }
98 return $empty && rmdir($path);
99 }
100}
migrate(StorableResource $resource, string $to_handler_id)
Definition: Migrator.php:54
__construct(StorageHandlerFactory $handler_factory, ResourceBuilder $resource_builder, \ilDBInterface $database, string $filesystem_base_path)
Migrator constructor.
Definition: Migrator.php:42
Interface ilDBInterface.
$path
Definition: ltiservices.php:32