ILIAS  release_8 Revision v8.23
class.ilExportContainer.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
26 {
27  private string $cont_export_dir = '';
30 
35  public function __construct(ilExportOptions $eo)
36  {
37  $this->eo = $eo;
39  }
40 
44  public function exportObject(string $a_type, int $a_id, string $a_target_release = ""): array
45  {
46  $log = $GLOBALS['DIC']->logger()->exp();
47 
48  // if no target release specified, use latest major release number
49  if ($a_target_release == "") {
50  $v = explode(".", ILIAS_VERSION_NUMERIC);
51  $a_target_release = $v[0] . "." . $v[1] . ".0";
52  }
53 
54  // Create base export directory
55  ilExport::_createExportDirectory($a_id, "xml", $a_type);
56  $export_dir = ilExport::_getExportDirectory($a_id, "xml", $a_type);
57  $ts = time();
58  $sub_dir = $ts . "__" . IL_INST_ID . "__" . $a_type . "_" . $a_id;
59 
60  $this->cont_export_dir = $export_dir . DIRECTORY_SEPARATOR . $sub_dir;
61  ilFileUtils::makeDirParents($this->cont_export_dir);
62 
63  $log->debug('Using base directory: ' . $this->export_run_dir);
64 
65  $this->manifestWriterBegin($a_type, $a_id, $a_target_release);
66  $this->addContainer();
67  $this->addSubitems($a_id, $a_type, $a_target_release);
68  $this->manifestWriterEnd($a_type, $a_id, $a_target_release);
69 
70  ilFileUtils::zip($this->cont_export_dir, $this->cont_export_dir . '.zip');
71  ilFileUtils::delDir($this->cont_export_dir);
72  return [];
73  }
74 
75  protected function manifestWriterBegin(string $a_type, int $a_id, string $a_target_release): void
76  {
77  $this->cont_manifest_writer = new ilXmlWriter();
78  $this->cont_manifest_writer->xmlHeader();
79  $this->cont_manifest_writer->xmlStartTag(
80  'Manifest',
81  array(
82  "MainEntity" => $a_type,
83  "Title" => ilObject::_lookupTitle($a_id),
84  /* "TargetRelease" => $a_target_release, */
85  "InstallationId" => IL_INST_ID,
86  "InstallationUrl" => ILIAS_HTTP_PATH
87  )
88  );
89  }
90 
91  protected function addContainer(): void
92  {
93  }
94 
95  protected function addSubitems(int $a_id, string $a_type, string $a_target_release): void
96  {
97  $set_number = 1;
98  foreach ($this->eo->getSubitemsForExport() as $ref_id) {
99  // get last export file
100  $obj_id = ilObject::_lookupObjId($ref_id);
101 
102  $expi = ilExportFileInfo::lookupLastExport($obj_id, 'xml', $a_target_release);
103 
104  if (!$expi instanceof ilExportFileInfo) {
105  $this->log->warning('Cannot find export file for refId ' . $ref_id . ', type ' . ilObject::_lookupType($a_id));
106  continue;
107  }
108 
109  $exp_dir = ilExport::_getExportDirectory($obj_id, 'xml', ilObject::_lookupType($obj_id));
110  $exp_full = $exp_dir . DIRECTORY_SEPARATOR . $expi->getFilename();
111 
112  $this->log->debug('Zip path ' . $exp_full);
113 
114  // Unzip
115  ilFileUtils::unzip($exp_full, true, false);
116 
117  // create set directory
118  ilFileUtils::makeDirParents($this->cont_export_dir . DIRECTORY_SEPARATOR . 'set_' . $set_number);
119 
120  // cut .zip
121  $new_path_rel = 'set_' . $set_number . DIRECTORY_SEPARATOR . $expi->getBasename();
122  $new_path_abs = $this->cont_export_dir . DIRECTORY_SEPARATOR . $new_path_rel;
123 
124  $this->log->debug($new_path_rel . ' ' . $new_path_abs);
125 
126  // Move export
127  rename(
128  $exp_dir . DIRECTORY_SEPARATOR . $expi->getBasename(),
129  $new_path_abs
130  );
131 
132  $this->log->debug($exp_dir . DIRECTORY_SEPARATOR . $expi->getBasename() . ' -> ' . $new_path_abs);
133 
134  // Delete latest container xml of source
135  if ($a_id == $obj_id) {
136  $expi->delete();
137  if (file_exists($exp_full)) {
138  $this->log->info('Deleting' . $exp_full);
139  unlink($exp_full);
140  }
141  }
142 
143  $this->cont_manifest_writer->xmlElement(
144  'ExportSet',
145  array(
146  'Path' => $new_path_rel,
147  'Type' => ilObject::_lookupType($obj_id)
148  )
149  );
150  ++$set_number;
151  }
152  }
153 
154  protected function manifestWriterEnd(string $a_type, int $a_id, string $a_target_release): void
155  {
156  $this->cont_manifest_writer->xmlEndTag('Manifest');
157  $this->log->debug($this->cont_export_dir . DIRECTORY_SEPARATOR . 'manifest.xml');
158  $this->cont_manifest_writer->xmlDumpFile($this->cont_export_dir . DIRECTORY_SEPARATOR . 'manifest.xml', true);
159  }
160 }
exportObject(string $a_type, int $a_id, string $a_target_release="")
const IL_INST_ID
Definition: constants.php:40
__construct(ilExportOptions $eo)
Constructor.
const ILIAS_VERSION_NUMERIC
debug(string $a_message, array $a_context=array())
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static lookupLastExport(int $a_obj_id, string $a_type, string $a_version='')
Lookup last export.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
manifestWriterEnd(string $a_type, int $a_id, string $a_target_release)
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
static _getExportDirectory(int $a_obj_id, string $a_type="xml", string $a_obj_type="", string $a_entity="")
Get export directory for an repository object.
static unzip(string $path_to_zip_file, bool $overwrite_existing=false, bool $unpack_flat=false)
static _lookupObjId(int $ref_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$ref_id
Definition: ltiauth.php:67
static _lookupTitle(int $obj_id)
manifestWriterBegin(string $a_type, int $a_id, string $a_target_release)
static _createExportDirectory(int $a_obj_id, string $a_export_type="xml", string $a_obj_type="")
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
addSubitems(int $a_id, string $a_type, string $a_target_release)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(Container $dic, ilPlugin $plugin)
static zip(string $a_dir, string $a_file, bool $compress_content=false)
zips given directory/file into given zip.file
ilLogger $log
static _lookupType(int $id, bool $reference=false)