ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
Migrator.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
25 
31 class Migrator
32 {
33  protected bool $clean_up = true;
34 
38  public function __construct(private StorageHandlerFactory $handler_factory, protected ResourceBuilder $resource_builder, private \ilDBInterface $database, protected string $filesystem_base_path)
39  {
40  }
41 
42  public function migrate(StorableResource $resource, string $to_handler_id): bool
43  {
44  $existing_handler = $this->handler_factory->getHandlerForResource($resource);
45  $existing_path = $this->filesystem_base_path . '/' . $existing_handler->getFullContainerPath(
46  $resource->getIdentification()
47  );
48 
49  $new_handler = $this->handler_factory->getHandlerForStorageId($to_handler_id);
50  $destination_path = $this->filesystem_base_path . '/' . $new_handler->getFullContainerPath(
51  $resource->getIdentification()
52  );
53 
54  if (!file_exists($existing_path)) {
55  // File is not existing, we MUST delete the resource
56  $this->resource_builder->remove($resource);
57  return false;
58  }
59 
60  if (!is_dir(dirname($destination_path)) && !mkdir(dirname($destination_path), 0777, true)) {
61  return false;
62  }
63  if (file_exists($destination_path)) {
64  // target exists, we have to merge the folders.
65  $result = $this->mergeDirectories($existing_path, $destination_path);
66  } else {
67  $result = rename($existing_path, $destination_path);
68  }
69 
70  if ($result) {
71  $this->database->manipulateF(
72  "UPDATE il_resource SET storage_id = %s WHERE rid = %s LIMIT 1",
73  ['text', 'text'],
74  [$to_handler_id, $resource->getIdentification()->serialize()]
75  );
76 
77  // remove old
78  if ($this->clean_up) {
79  $existing_handler->cleanUpContainer($resource);
80  }
81 
82  return true;
83  }
84  return false;
85  }
86 
87  public function removeEmptySubFolders(string $path): bool
88  {
89  $empty = true;
90  foreach (glob($path . DIRECTORY_SEPARATOR . "*") as $file) {
91  $empty &= is_dir($file) && $this->removeEmptySubFolders($file);
92  }
93  return $empty && rmdir($path);
94  }
95 
96  private function mergeDirectories(string $path_to_source_dir, string $path_to_destination_dir): bool
97  {
98  $dir = opendir($path_to_source_dir);
99 
100  while (($name = readdir($dir)) !== false) {
101  if ($name === '.') {
102  continue;
103  }
104  if ($name === '..') {
105  continue;
106  }
107  $src_path = $path_to_source_dir . '/' . $name;
108  $dest_path = $path_to_destination_dir . '/' . $name;
109 
110  if (is_dir($src_path)) {
111  // If it's a directory, create destination and then recurse
112  if (!file_exists($dest_path)) {
113  mkdir($dest_path, 0777, true);
114  }
115  $this->mergeDirectories($src_path, $dest_path);
116  } elseif (file_exists($dest_path)) {
117  unlink($dest_path);
118  rename($src_path, $dest_path);
119  } elseif (!file_exists($dest_path)) {
120  rename($src_path, $dest_path);
121  }
122  }
123  closedir($dir);
124 
125  return true;
126  }
127 }
migrate(StorableResource $resource, string $to_handler_id)
Definition: Migrator.php:42
__construct(private StorageHandlerFactory $handler_factory, protected ResourceBuilder $resource_builder, private \ilDBInterface $database, protected string $filesystem_base_path)
Migrator constructor.
Definition: Migrator.php:38
$path
Definition: ltiservices.php:29
mergeDirectories(string $path_to_source_dir, string $path_to_destination_dir)
Definition: Migrator.php:96