ILIAS  release_7 Revision v7.30-3-g800a261c036
Migrator.php
Go to the documentation of this file.
1<?php declare(strict_types=1);
2
4
7
14{
22 private $database;
27
28 protected $clean_up = true;
33
39 public function __construct(
44 ) {
45 $this->handler_factory = $handler_factory;
46 $this->resource_builder = $resource_builder;
47 $this->database = $database;
48 $this->filesystem_base_path = $filesystem_base_path;
49 }
50
51 public function migrate(StorableResource $resource, string $to_handler_id) : bool
52 {
53 $existing_handler = $this->handler_factory->getHandlerForResource($resource);
54 $existing_path = $this->filesystem_base_path . '/' . $existing_handler->getFullContainerPath($resource->getIdentification());
55
56 $new_handler = $this->handler_factory->getHandlerForStorageId($to_handler_id);
57 $destination_path = $this->filesystem_base_path . '/' . $new_handler->getFullContainerPath($resource->getIdentification());
58
59 if (!file_exists($existing_path)) {
60 // File is not existing, we MUST delete the resource
61 $this->resource_builder->remove($resource);
62 return false;
63 }
64
65 if (!is_dir(dirname($destination_path))) {
66 if (!mkdir(dirname($destination_path), 0777, true)) {
67 return false;
68 }
69 }
70 if (rename($existing_path, $destination_path)) {
71 $r = $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($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}
An exception for terminatinating execution or to throw for unit testing.
migrate(StorableResource $resource, string $to_handler_id)
Definition: Migrator.php:51
__construct(StorageHandlerFactory $handler_factory, ResourceBuilder $resource_builder, \ilDBInterface $database, string $filesystem_base_path)
Migrator constructor.
Definition: Migrator.php:39
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...