ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
Handler.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use ilAccessHandler;
24 use ilFileUtils;
29 use ILIAS\Export\ExportHandler\I\FactoryInterface as ilExportHandlerFactoryInterface;
30 use ILIAS\Export\ExportHandler\I\Info\Export\Container\HandlerInterface as ilExportHandlerContainerExportInfoInterface;
31 use ILIAS\Export\ExportHandler\I\Info\Export\Container\ObjectId\CollectionBuilderInterface as ilExportHandlerContainerExportInfoObjectIdCollectionBuilderInterface;
32 use ILIAS\Export\ExportHandler\I\Info\Export\Container\ObjectId\CollectionInterface as ilExportHandlerContainerExportInfoObjectIdCollectionInterface;
33 use ILIAS\Export\ExportHandler\I\Info\Export\HandlerInterface as ilExportHandlerExportInfoInterface;
34 use ILIAS\Export\ExportHandler\I\Manager\HandlerInterface as ilExportHandlerManagerInterface;
35 use ILIAS\Export\ExportHandler\I\Repository\Element\HandlerInterface as ilExportHandlerRepositoryElementInterface;
36 use ILIAS\Export\ExportHandler\I\Target\HandlerInterface as ilExportHandlerTargetInterface;
37 use ILIAS\Export\ExportHandler\I\Wrapper\DataFactory\HandlerInterface as ilExportHandlerDataFactoryWrapperInterface;
38 use ILIAS\Export\ExportHandler\Info\Export\Handler as ilExportHandlerExportInfo;
41 use ilObject;
43 use ilObjFileAccess;
44 use ilTree;
45 
46 class Handler implements ilExportHandlerManagerInterface
47 {
48  protected ilExportHandlerFactoryInterface $export_handler;
49  protected ilTree $tree;
52  protected ilExportHandlerDataFactoryWrapperInterface $data_factory_wrapper;
53 
54  public function __construct(
55  ilExportHandlerFactoryInterface $export_handler,
56  ilObjectDefinition $obj_definition,
57  ilTree $tree,
58  ilAccessHandler $access,
59  ilExportHandlerDataFactoryWrapperInterface $data_factory_wrapper
60  ) {
61  $this->export_handler = $export_handler;
62  $this->obj_definition = $obj_definition;
63  $this->tree = $tree;
64  $this->access = $access;
65  $this->data_factory_wrapper = $data_factory_wrapper;
66  }
67 
68  protected function getExportTargetWithObject(
69  ilObject $export_object
70  ) {
71  $obj_id = $export_object->getId();
72  $type = $export_object->getType();
73  $class = ilImportExportFactory::getExporterClass($type);
74  $comp = ilImportExportFactory::getComponentForExport($type);
75  $v = explode(".", ILIAS_VERSION_NUMERIC);
76  $target_release = $v[0] . "." . $v[1] . ".0";
77  return $this->export_handler->target()->handler()
78  ->withTargetRelease($target_release)
79  ->withType($type)
80  ->withObjectIds([$obj_id])
81  ->withClassname($class)
82  ->withComponent($comp);
83  }
84 
85  protected function getExportTarget(
86  ObjectId $object_id
87  ): ilExportHandlerTargetInterface {
88  $obj_id = $object_id->toInt();
89  $type = ilObject::_lookupType($obj_id);
90  $class = ilImportExportFactory::getExporterClass($type);
91  $comp = ilImportExportFactory::getComponentForExport($type);
92  $v = explode(".", ILIAS_VERSION_NUMERIC);
93  $target_release = $v[0] . "." . $v[1] . ".0";
94  return $this->export_handler->target()->handler()
95  ->withTargetRelease($target_release)
96  ->withType($type)
97  ->withObjectIds([$obj_id])
98  ->withClassname($class)
99  ->withComponent($comp);
100  }
101 
102  protected function writeToElement(
103  string $path_in_container,
104  ilExportHandlerExportInfo $export_info,
105  ilExportHandlerRepositoryElementInterface $element
106  ): void {
107  $export_info = $export_info->withCurrentElement($element);
108  $manifest = $this->export_handler->part()->manifest()->handler()
109  ->withInfo($export_info);
110  $element->getIRSS()->write(
111  Streams::ofString($manifest->getXML(false)),
112  $path_in_container . DIRECTORY_SEPARATOR . $export_info->getExportFolderName() . DIRECTORY_SEPARATOR . "manifest.xml"
113  );
114  foreach ($export_info->getComponentInfos() as $component_info) {
115  $component = $this->export_handler->part()->component()->handler()
116  ->withExportInfo($export_info)
117  ->withComponentInfo($component_info);
118  $element->getIRSS()->write(
119  Streams::ofString($component->getXML(false)),
120  $path_in_container . DIRECTORY_SEPARATOR . $component_info->getExportFilePathInContainer()
121  );
122  }
123  }
124 
125  public function createContainerExport(
126  int $user_id,
127  ilExportHandlerContainerExportInfoInterface $container_export_info,
128  ): ilExportHandlerRepositoryElementInterface {
129  $main_export_info = $container_export_info->getMainEntityExportInfo();
130  $main_element = $this->createExport(
131  $user_id,
132  $main_export_info,
133  "set_" . $main_export_info->getSetNumber()
134  );
135  $repository = $this->export_handler->repository();
136  foreach ($container_export_info->getExportInfos() as $export_info) {
137  $stream = null;
138  $keys = $repository->key()->collection()
139  ->withElement($repository->key()->handler()->withObjectId($export_info->getTargetObjectId()));
140  $element = $export_info->getReuseExport()
141  ? $this->export_handler->repository()->handler()->getElements($keys)->newest()
142  : $this->createExport($user_id, $export_info, "");
143  $element = $element->getIRSS()->isContainerExport()
144  ? $this->createExport($user_id, $export_info, "")
145  : $element;
146  $stream = $element->getIRSSInfo()->getStream();
147  $zip_reader = new ZipReader($stream);
148  $zip_structure = $zip_reader->getStructure();
149  foreach ($zip_structure as $path_inside_zip => $item) {
150  if ($item['is_dir']) {
151  continue;
152  }
156  $stream = $zip_reader->getItem($path_inside_zip, $zip_structure)[0];
157  # Contents of the stream $stream are lost later (possible incompatible stream object), therefore a new
158  # stream with the content of $stream is created and passed on.
159  $main_element->getIRSS()->write(
160  Streams::ofString($stream->getContents()),
161  "set_" . $export_info->getSetNumber() . DIRECTORY_SEPARATOR . $path_inside_zip
162  );
163  }
164  }
165  $container = $this->export_handler->part()->container()->handler()
166  ->withExportInfos($container_export_info->getExportInfos()->withElementAtHead($main_export_info))
167  ->withMainEntityExportInfo($main_export_info);
168  $main_element->getIRSS()->write(
169  Streams::ofString($container->getXML(false)),
170  "manifest.xml"
171  );
172  return $main_element;
173  }
174 
175  public function createExport(
176  int $user_id,
177  ilExportHandlerExportInfoInterface $export_info,
178  string $path_in_container
179  ): ilExportHandlerRepositoryElementInterface {
180  # make legacy export run dir
181  # tmp solution, remove later if no longer needed
182  ilFileUtils::makeDirParents($export_info->getLegacyExportRunDir());
183 
184  $stakeholder = $this->export_handler->repository()->stakeholder()->handler()->withOwnerId($user_id);
185  $object_id = $this->data_factory_wrapper->objId($export_info->getTarget()->getObjectIds()[0]);
186  $element = $this->export_handler->repository()->handler()->createElement(
187  $object_id,
188  $export_info,
189  $stakeholder
190  );
191  $this->writeToElement($path_in_container, $export_info, $element);
192 
193  # write files from legacy export run dir to irss
194  # tmp solution, remove later if no longer needed
195  $writer = $this->export_handler->consumer()->handler()->exportWriter($element);
196  $writer->writeDirectoryRecursive(
197  $export_info->getLegacyExportRunDir(),
198  $export_info->getExportFolderName()
199  );
200 
201  # delete legacy export run dir
202  # tmp solution, remove if no longer needed
203  ilFileUtils::delDir($export_info->getLegacyExportRunDir());
204  return $element;
205  }
206 
207  public function createExportOfObject(
208  ilObject $export_object,
209  int $user_id,
210  ExportConfigCollectionInterface $export_configs
211  ): void {
212  $export_info = $this->getExportInfoWithObject(
213  $export_object,
214  time(),
215  $export_configs
216  );
217  $element = $this->createExport(
218  $user_id,
219  $export_info,
220  ""
221  );
222  }
223 
224  public function getExportInfo(
225  ObjectId $object_id,
226  int $time_stamp,
227  ExportConfigCollectionInterface $export_configs
228  ): ilExportHandlerExportInfoInterface {
229  return $this->export_handler->info()->export()->handler()
230  ->withTarget($this->getExportTarget($object_id), $time_stamp)
231  ->withExportConfigs($export_configs);
232  }
233 
234  public function getExportInfoWithObject(
235  ilObject $export_object,
236  int $time_stamp,
237  ExportConfigCollectionInterface $export_configs
238  ): ilExportHandlerExportInfoInterface {
239  return $this->export_handler->info()->export()->handler()
240  ->withTarget($this->getExportTargetWithObject($export_object), $time_stamp)
241  ->withExportConfigs($export_configs);
242  }
243 
244  public function getContainerExportInfo(
245  ObjectId $main_entity_object_id,
246  ilExportHandlerContainerExportInfoObjectIdCollectionInterface $object_ids,
247  ExportConfigCollectionInterface $export_configs
248  ): ilExportHandlerContainerExportInfoInterface {
249  return $this->export_handler->info()->export()->container()->handler()
250  ->withMainExportEntity($main_entity_object_id)
251  ->withObjectIds($object_ids)
252  ->withTimestamp(time())
253  ->withExportConfigs($export_configs);
254  }
255 
256  public function getObjectIdCollectioBuilder(): ilExportHandlerContainerExportInfoObjectIdCollectionBuilderInterface
257  {
258  return $this->export_handler->info()->export()->container()->objectId()->collectionBuilder();
259  }
260 
262  ReferenceId $container_reference_id,
263  bool $public_access = false
264  ): ilExportHandlerContainerExportInfoObjectIdCollectionBuilderInterface {
265  $id_collection_builder = $this->export_handler->info()->export()->container()->objectId()->collectionBuilder();
266  $tree_nodes = $this->tree->getSubTree($this->tree->getNodeData($container_reference_id->toInt()));
267  foreach ($tree_nodes as $node) {
268  if (
269  $node['type'] == 'rolf' or
270  !$this->obj_definition->allowExport($node['type']) or
271  ($node['type'] == "file" && ilObjFileAccess::_isFileHidden($node['title'])) or
272  !$this->access->checkAccess('write', '', (int) $node['ref_id'])
273  ) {
274  continue;
275  }
276  $reference_id = new ReferenceId((int) $node['ref_id']);
277  $object_id = $reference_id->toObjectId();
278  $keys = $this->export_handler->repository()->key()->collection()->withElement(
279  $this->export_handler->repository()->key()->handler()->withObjectId($object_id)
280  );
281  $elements = $this->export_handler->repository()->handler()->getElements($keys);
282  $create_new_export = (
283  ($public_access and !$this->export_handler->publicAccess()->handler()->hasPublicAccessFile($object_id)) or
284  !$public_access and $elements->count() == 0
285  );
286  $id_collection_builder = $id_collection_builder->addObjectId($object_id, $create_new_export);
287  }
288  return $id_collection_builder;
289  }
290 }
getContainerExportInfo(ObjectId $main_entity_object_id, ilExportHandlerContainerExportInfoObjectIdCollectionInterface $object_ids, ExportConfigCollectionInterface $export_configs)
Definition: Handler.php:244
getExportInfoWithObject(ilObject $export_object, int $time_stamp, ExportConfigCollectionInterface $export_configs)
Definition: Handler.php:234
getExportTargetWithObject(ilObject $export_object)
Definition: Handler.php:68
writeToElement(string $path_in_container, ilExportHandlerExportInfo $export_info, ilExportHandlerRepositoryElementInterface $element)
Definition: Handler.php:102
static _isFileHidden(string $a_file_name)
Returns true, if a file with the specified name, is usually hidden from the user. ...
createExportOfObject(ilObject $export_object, int $user_id, ExportConfigCollectionInterface $export_configs)
Definition: Handler.php:207
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
createContainerExport(int $user_id, ilExportHandlerContainerExportInfoInterface $container_export_info)
$container
Definition: wac.php:36
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
const ILIAS_VERSION_NUMERIC
__construct(ilExportHandlerFactoryInterface $export_handler, ilObjectDefinition $obj_definition, ilTree $tree, ilAccessHandler $access, ilExportHandlerDataFactoryWrapperInterface $data_factory_wrapper)
Definition: Handler.php:54
ilExportHandlerFactoryInterface $export_handler
Definition: Handler.php:48
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static ofString(string $string)
Creates a new stream with an initial value.
Definition: Streams.php:41
getExportInfo(ObjectId $object_id, int $time_stamp, ExportConfigCollectionInterface $export_configs)
Definition: Handler.php:224
ilExportHandlerDataFactoryWrapperInterface $data_factory_wrapper
Definition: Handler.php:52
getExportTarget(ObjectId $object_id)
Definition: Handler.php:85
createExport(int $user_id, ilExportHandlerExportInfoInterface $export_info, string $path_in_container)
Definition: Handler.php:175
static _lookupType(int $id, bool $reference=false)
getObjectIdCollectionBuilderFrom(ReferenceId $container_reference_id, bool $public_access=false)
Definition: Handler.php:261