ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilExportZipBuiltObjective.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 use ILIAS\Setup;
23 
25 {
26  protected const FILENAME = "ILIAS_EXPORT.zip";
27 
28  protected string $cwd;
29 
30  public function __construct(Setup\Config $config)
31  {
32  parent::__construct($config);
33  $this->tmp_dir = $this->createTempDir();
34  if (is_null($this->tmp_dir)) {
35  throw new RuntimeException("Can't create temporary directory!");
36  }
37  if(! array_key_exists('PWD', $_SERVER)) {
38  throw new RuntimeException("Can't get to current directory. \nIf you ran the script using 'sudo', please try using 'sudo -s'");
39  }
40  $this->cwd = $_SERVER["PWD"];
41  }
42 
46  public function getHash(): string
47  {
48  return hash("sha256", self::class);
49  }
50 
54  public function getLabel(): string
55  {
56  return "Export ILIAS to $this->cwd/" . self::FILENAME;
57  }
58 
62  public function isNotable(): bool
63  {
64  return true;
65  }
66 
70  public function getPreconditions(Setup\Environment $environment): array
71  {
72  $dumper = new MysqlIfsnopDumper($this->config->getExportHooksPath());
73 
74  return [
76  new Setup\ObjectiveCollection(
77  "",
78  false,
81  $this->tmp_dir . "/public/data/"
82  ),
83  new ilFileSystemDirectoryCopiedRecursivelyObjective("", $this->tmp_dir . "/public/data", true)
84  ),
87  $this->tmp_dir . "/web_data/"
88  ),
89  new ilFileSystemDirectoryCopiedRecursivelyObjective("public/data", $this->tmp_dir . "/web_data"),
90  ),
91  ),
92  new ilFileSystemDirectoryCopiedRecursivelyObjective("Customizing", $this->tmp_dir . "/Customizing"),
93  new ilDatabaseDumpedToDirectoryObjective($this->tmp_dir . "/dump", $dumper)
94  ];
95  }
96 
100  public function achieve(Setup\Environment $environment): Setup\Environment
101  {
103  file_put_contents($this->tmp_dir . "/meta.txt", implode("\n", $meta) . "\n", FILE_APPEND);
104 
105  // This will be recreated during import with new data for the imported instance.
106  $this->deleteDirRecursive($this->tmp_dir . "/web_data/default/client.ini.php");
107 
108  $this->addFolderToZip($this->tmp_dir . "/web_data", $this->tmp_dir . "/web_data.zip");
109  $this->addFolderToZip($this->tmp_dir . "/Customizing", $this->tmp_dir . "/Customizing.zip");
110  $this->addFolderToZip($this->tmp_dir . "/public/data", $this->tmp_dir . "/data.zip");
111  $this->addFolderToZip($this->tmp_dir . "/dump", $this->tmp_dir . "/dump.zip");
112 
113  $this->deleteDirRecursive($this->tmp_dir . "/web_data");
114  $this->deleteDirRecursive($this->tmp_dir . "/Customizing");
115  $this->deleteDirRecursive($this->tmp_dir . "/public/data");
116  $this->deleteDirRecursive($this->tmp_dir . "/dump");
117 
118  $this->addFolderToZip($this->tmp_dir, $this->cwd . "/" . self::FILENAME);
119 
120  $this->deleteDirRecursive($this->tmp_dir);
121 
122  return $environment;
123  }
124 
128  public function isApplicable(Setup\Environment $environment): bool
129  {
130  return is_writable($this->cwd);
131  }
132 
133  protected function addFolderToZip($source, $destination, $flags = ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE): bool
134  {
135  if (!file_exists($source)) {
136  throw new RuntimeException("File does not exist: " . $source);
137  }
138 
139  $zip = new ZipArchive();
140  if (!$zip->open($destination, $flags)) {
141  throw new RuntimeException("Cannot open zip archive: " . $destination);
142  }
143 
144 
145  $files = new RecursiveIteratorIterator(
146  new RecursiveDirectoryIterator($source, FilesystemIterator::SKIP_DOTS),
147  RecursiveIteratorIterator::SELF_FIRST
148  );
149 
150  $sourceWithSeparator = $source . DIRECTORY_SEPARATOR;
151 
153  foreach ($files as $file) {
154  if ($file->isDir()) {
155  $zip->addEmptyDir(str_replace($sourceWithSeparator, '', $file . DIRECTORY_SEPARATOR));
156  }
157  if ($file->isFile()) {
158  $zip->addFile($file->getPathname(), str_replace($sourceWithSeparator, '', $file->getPathname()));
159  }
160  }
161 
162  return $zip->close();
163  }
164 
165  protected function deleteDirRecursive(string $path): void
166  {
167  if (is_file($path)) {
168  unlink($path);
169  return;
170  }
171 
172  $files = new RecursiveIteratorIterator(
173  new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
174  RecursiveIteratorIterator::CHILD_FIRST
175  );
176 
177  foreach ($files as $file_info) {
178  if ($file_info->isDir()) {
179  rmdir($file_info->getRealPath());
180  continue;
181  }
182  unlink($file_info->getRealPath());
183  }
184 
185  rmdir($path);
186  }
187 
188  public function createTempDir(): ?string
189  {
190  $path = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . mt_rand() . microtime(true);
191  if (mkdir($path)) {
192  return $path;
193  }
194  return null;
195  }
196 }
getPreconditions(Setup\Environment $environment)
isApplicable(Setup\Environment $environment)
$path
Definition: ltiservices.php:29
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
A wrapper around an objective that adds some preconditions.
$_SERVER['HTTP_HOST']
Definition: raiseError.php:26
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
An environment holds resources to be used in the setup process.
Definition: Environment.php:27
__construct(Container $dic, ilPlugin $plugin)
getConfigFor(string $component)
A configuration for the setup.
Definition: Config.php:26
achieve(Setup\Environment $environment)