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