ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilFileObjectToStorageMigrationRunner.php
Go to the documentation of this file.
1<?php
2
13use ILIAS\ResourceStorage\StorageHandler\FileSystemStorageHandlerV2;
20
22{
27
47 protected $file_system;
51 protected $database;
55 protected $keep_originals = false;
59 protected $migrate_to_new_object_id = null;
63 protected $stakeholder;
64
70 public function __construct(Filesystem $file_system, ilDBInterface $database, string $log_file_path)
71 {
72 $this->file_system = $file_system;
73 $this->database = $database;
74 $this->migration_log_handle = fopen($log_file_path, 'ab');
75
76 $storage_handler = new MaxNestingFileSystemStorageHandler($this->file_system, Location::STORAGE, true);
77 $storage_handler_factory = new StorageHandlerFactory([
78 $storage_handler
79 ]);
80
81 $this->movement_implementation = $storage_handler->movementImplementation();
82
83 $revisionDBRepository = new RevisionDBRepository($database);
84 $resourceDBRepository = new ResourceDBRepository($database);
85 $informationDBRepository = new InformationDBRepository($database);
86 $stakeholderDBRepository = new StakeholderDBRepository($database);
88 $storage_handler_factory,
89 $revisionDBRepository,
90 $resourceDBRepository,
91 $informationDBRepository,
92 $stakeholderDBRepository,
94 );
95 $this->resource_builder = $builder;
96 $this->storage_manager = new Manager($builder, new StandardRepositoryPreloader(
97 $resourceDBRepository,
98 $revisionDBRepository,
99 $informationDBRepository,
100 $stakeholderDBRepository
101 ));
102 $this->consumer_factory = new ConsumerFactory($storage_handler_factory);
103 $this->stakeholder = new ilObjFileStakeholder();
104 }
105
109 public function migrate(ilFileObjectToStorageDirectory $item) : void
110 {
111 $resource = $this->getResource($item);
112
113 $object_id = $this->getMigrateToNewObjectId() ?? $item->getObjectId();
114 foreach ($item->getVersions() as $version) {
115 try {
116 $status = 'success';
117 $aditional_info = '';
118
119 $stream = Streams::ofResource(fopen($version->getPath(), 'rb'));
120
121 $info_resolver = new ilFileObjectToStorageInfoResolver(
122 $stream,
123 $version->getVersion(),
124 $version->getOwner(),
125 $version->getTitle(),
126 (new DateTimeImmutable())->setTimestamp($version->getCreationDateTimestamp())
127 );
128
129 $this->resource_builder->appendFromStream(
130 $resource,
131 $stream,
132 $info_resolver,
133 $this->keep_originals
134 );
135 } catch (Throwable $t) {
136 $status = 'failed';
137 $aditional_info = $t->getMessage();
138 }
139
140 $this->logMigratedFile(
141 $object_id,
142 $resource->getIdentification()->serialize(),
143 $version->getVersion(),
144 $version->getPath(),
145 $status,
146 $this->movement_implementation,
147 $aditional_info
148 );
149 }
150 $resource->addStakeholder($this->stakeholder);
151 try {
152 $this->resource_builder->store($resource);
153 $this->database->manipulateF(
154 'UPDATE file_data SET rid = %s WHERE file_id = %s',
155 ['text', 'integer'],
156 [$resource->getIdentification()->serialize(), $object_id]
157 );
158 } catch (FileNamePolicyException $e) {
159 // continue
160 }
161
162 if (null === $this->getMigrateToNewObjectId()) {
163 $item->tearDown();
164 }
165 }
166
167 private function logMigratedFile(
168 int $object_id,
169 string $rid,
170 int $version,
171 string $old_path,
172 string $status,
174 string $aditional_info = null
175 ) : void {
176 fputcsv($this->migration_log_handle, [
177 $object_id,
178 $old_path,
179 $rid,
180 $version,
181 $status,
183 $aditional_info
184 ], ";");
185 }
186
192 private function getResource(
194 ) : StorableResource {
195 $r = $this->database->queryF("SELECT rid FROM file_data WHERE file_id = %s", ['integer'],
196 [$item->getObjectId()]);
197 $d = $this->database->fetchObject($r);
198
199 if (isset($d->rid) && $d->rid !== '' && ($resource_identification = $this->storage_manager->find($d->rid)) && $resource_identification !== null) {
200 $resource = $this->resource_builder->get($resource_identification);
201 } else {
202 $resource = $this->resource_builder->newBlank();
203 }
204 return $resource;
205 }
206
210 public function getMigrateToNewObjectId() : ?int
211 {
212 return $this->migrate_to_new_object_id;
213 }
214
219 public function setMigrateToNewObjectId(?int $migrate_to_new_object_id) : ilFileObjectToStorageMigrationRunner
220 {
221 $this->migrate_to_new_object_id = $migrate_to_new_object_id;
222 $this->keep_originals = true;
223 return $this;
224 }
225
226}
An exception for terminatinating execution or to throw for unit testing.
Class Streams Stream factory which enables the user to create streams without the knowledge of the co...
Definition: Streams.php:17
Class ilFileObjectToStorageDirectory.
migrate(ilFileObjectToStorageDirectory $item)
@inheritDoc
logMigratedFile(int $object_id, string $rid, int $version, string $old_path, string $status, string $movement_implementation, string $aditional_info=null)
__construct(Filesystem $file_system, ilDBInterface $database, string $log_file_path)
ilFileObjectToStorageMigration constructor.
Class ilObjFileStakeholder.
for( $i=6;$i< 13;$i++) for($i=1; $i< 13; $i++) $d
Definition: date.php:296
Interface Location.
Definition: Location.php:17
Interface Filesystem The filesystem interface provides the public interface for the Filesystem servic...
Definition: Filesystem.php:22
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class FlySystemFileAccessTest \Provider\FlySystem @runTestsInSeparateProcesses @preserveGlobalState d...
$builder
Definition: parser.php:5