ILIAS  release_8 Revision v8.24
class.ilResourceStorageMigrationHelper.php
Go to the documentation of this file.
1<?php
2
35
41{
42 protected string $client_data_dir;
47 protected Manager $manager;
48
54 public function __construct(
56 Environment $environment
57 ) {
58 $this->stakeholder = $stakeholder;
60 $db = $environment->getResource(Environment::RESOURCE_DATABASE);
61 $ilias_ini = $environment->getResource(Environment::RESOURCE_ILIAS_INI);
62 $client_id = $environment->getResource(Environment::RESOURCE_CLIENT_ID);
63 $data_dir = $ilias_ini->readVariable('clients', 'datadir');
64 $client_data_dir = "{$data_dir}/{$client_id}";
65 if (!defined("CLIENT_WEB_DIR")) {
66 define("CLIENT_WEB_DIR", dirname(__DIR__, 4) . "/data/" . $client_id);
67 }
68 if (!defined("ILIAS_WEB_DIR")) {
69 define("ILIAS_WEB_DIR", dirname(__DIR__, 4));
70 }
71 if (!defined("CLIENT_ID")) {
72 define("CLIENT_ID", $client_id);
73 }
74 $this->client_data_dir = $client_data_dir;
75 $this->database = $db;
76
77 // Build Container
78 $init = new InitResourceStorage();
79 $container = new Container();
80 $container['ilDB'] = $db;
81 $storageConfiguration = new LocalConfig($client_data_dir);
83 $container['filesystem.storage'] = $f->getLocal($storageConfiguration);
84
85 $this->resource_builder = $init->getResourceBuilder($container);
86 $this->collection_builder = new CollectionBuilder(
88 );
89
90 $this->manager = new Manager(
91 $this->resource_builder,
92 $this->collection_builder,
94 );
95 }
96
100 public static function getPreconditions(): array
101 {
102 return [
106 ];
107 }
108
109 public function getClientDataDir(): string
110 {
112 }
113
114 public function getDatabase(): ilDBInterface
115 {
116 return $this->database;
117 }
118
120 {
121 return $this->stakeholder;
122 }
123
125 {
127 }
128
130 {
132 }
133
134 public function getManager(): Manager
135 {
136 return $this->manager;
137 }
138
139 public function moveFilesOfPathToCollection(
140 string $absolute_path,
141 int $resource_owner_id,
142 int $collection_owner_user_id = ResourceCollection::NO_SPECIFIC_OWNER,
143 ?Closure $file_name_callback = null,
144 ?Closure $revision_name_callback = null
146 $collection = $this->getCollectionBuilder()->new($collection_owner_user_id);
148 foreach (new DirectoryIterator($absolute_path) as $file_info) {
149 if (!$file_info->isFile()) {
150 continue;
151 }
152 $resource_id = $this->movePathToStorage(
153 $file_info->getRealPath(),
154 $resource_owner_id,
155 $file_name_callback,
156 $revision_name_callback
157 );
158 if ($resource_id !== null) {
159 $collection->add($resource_id);
160 }
161 }
162 if ($collection->count() === 0) {
163 return null;
164 }
165
166 if ($this->getCollectionBuilder()->store($collection)) {
167 return $collection->getIdentification();
168 }
169 return null;
170 }
171
173 string $absolute_base_path,
174 string $pattern,
175 int $resource_owner_id,
176 int $collection_owner_user_id = ResourceCollection::NO_SPECIFIC_OWNER,
177 ?Closure $file_name_callback = null,
178 ?Closure $revision_name_callback = null
180 $collection = $this->getCollectionBuilder()->new($collection_owner_user_id);
181
182 $regex_iterator = new RecursiveRegexIterator(
183 new RecursiveDirectoryIterator($absolute_base_path),
184 $pattern,
185 RecursiveRegexIterator::MATCH
186 );
187
188 foreach ($regex_iterator as $file_info) {
189 if (!$file_info->isFile()) {
190 continue;
191 }
192 $resource_id = $this->movePathToStorage(
193 $file_info->getRealPath(),
194 $resource_owner_id,
195 $file_name_callback,
196 $revision_name_callback
197 );
198 if ($resource_id !== null) {
199 $collection->add($resource_id);
200 }
201 }
202 if ($collection->count() === 0) {
203 return null;
204 }
205
206 if ($this->getCollectionBuilder()->store($collection)) {
207 return $collection->getIdentification();
208 }
209 return null;
210 }
211
212 public function movePathToStorage(
213 string $absolute_path,
214 int $owner_user_id,
215 ?Closure $file_name_callback = null,
216 ?Closure $revision_name_callback = null
218 try {
219 // in some cases fopen throws a warning instead of returning false
220 $open_path = fopen($absolute_path, 'rb');
221 } catch (Throwable $e) {
222 return null;
223 }
224
225 if ($open_path === false) {
226 return null;
227 }
228 $stream = Streams::ofResource($open_path);
229
230 // create new resource from legacy files stream
231 $revision_title = $revision_name_callback !== null
232 ? $revision_name_callback(basename($absolute_path))
233 : basename($absolute_path);
234
235 $file_name = $file_name_callback !== null
236 ? $file_name_callback(basename($absolute_path))
237 : null;
238
239 $resource = $this->resource_builder->newFromStream(
240 $stream,
242 $stream,
243 1,
244 $owner_user_id,
245 $revision_title,
246 $file_name
247 ),
248 false
249 );
250
251 // add bibliographic stakeholder and store resource
252 $resource->addStakeholder($this->stakeholder);
253 $this->resource_builder->store($resource);
254
255 return $resource->getIdentification();
256 }
257}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:32
Responsible for loading the Resource Storage into the dependency injection container of ILIAS.
movePathToStorage(string $absolute_path, int $owner_user_id, ?Closure $file_name_callback=null, ?Closure $revision_name_callback=null)
__construct(ResourceStakeholder $stakeholder, Environment $environment)
ilResourceStorageMigrationHelper constructor.
moveFilesOfPatternToCollection(string $absolute_base_path, string $pattern, int $resource_owner_id, int $collection_owner_user_id=ResourceCollection::NO_SPECIFIC_OWNER, ?Closure $file_name_callback=null, ?Closure $revision_name_callback=null)
try
Definition: cron.php:23
An environment holds resources to be used in the setup process.
Definition: Environment.php:28
getResource(string $id)
Consumers of this method should check if the result is what they expect, e.g.
Interface ilDBInterface.
$client_id
Definition: ltiauth.php:68
$container
@noRector
Definition: wac.php:14