ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
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;
36  private \ilDBInterface $database;
37  protected string $filesystem_base_path;
38 
42  public function __construct(
43  StorageHandlerFactory $handler_factory,
44  ResourceBuilder $resource_builder,
45  \ilDBInterface $database,
46  string $filesystem_base_path
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 (file_exists($destination_path)) {
76  // target exists, we have to merge the folders.
77  $result = $this->mergeDirectories($existing_path, $destination_path);
78  } else {
79  $result = rename($existing_path, $destination_path);
80  }
81 
82  if ($result) {
83  $this->database->manipulateF(
84  "UPDATE il_resource SET storage_id = %s WHERE rid = %s LIMIT 1",
85  ['text', 'text'],
86  [$to_handler_id, $resource->getIdentification()->serialize()]
87  );
88 
89  // remove old
90  if ($this->clean_up) {
91  $existing_handler->cleanUpContainer($resource);
92  }
93 
94  return true;
95  }
96  return false;
97  }
98 
99  public function removeEmptySubFolders(string $path): bool
100  {
101  $empty = true;
102  foreach (glob($path . DIRECTORY_SEPARATOR . "*") as $file) {
103  $empty &= is_dir($file) && $this->removeEmptySubFolders($file);
104  }
105  return $empty && rmdir($path);
106  }
107 
108  private function mergeDirectories(string $path_to_source_dir, string $path_to_destination_dir): bool
109  {
110  $dir = opendir($path_to_source_dir);
111 
112  while (($name = readdir($dir)) !== false) {
113  if ($name === '.' || $name === '..') {
114  continue;
115  }
116 
117  $src_path = $path_to_source_dir . '/' . $name;
118  $dest_path = $path_to_destination_dir . '/' . $name;
119 
120  if (is_dir($src_path)) {
121  // If it's a directory, create destination and then recurse
122  if (!file_exists($dest_path)) {
123  mkdir($dest_path, 0777, true);
124  }
125  $this->mergeDirectories($src_path, $dest_path);
126  } elseif (file_exists($dest_path)) {
127  unlink($dest_path);
128  rename($src_path, $dest_path);
129  } elseif (!file_exists($dest_path)) {
130  rename($src_path, $dest_path);
131  }
132  }
133  closedir($dir);
134 
135  return true;
136  }
137 }
migrate(StorableResource $resource, string $to_handler_id)
Definition: Migrator.php:54
$path
Definition: ltiservices.php:30
__construct(StorageHandlerFactory $handler_factory, ResourceBuilder $resource_builder, \ilDBInterface $database, string $filesystem_base_path)
Migrator constructor.
Definition: Migrator.php:42
mergeDirectories(string $path_to_source_dir, string $path_to_destination_dir)
Definition: Migrator.php:108