ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilFileObjectToStorageDirectory.php
Go to the documentation of this file.
1 <?php
2 
24 {
25  protected int $object_id;
26  protected string $path;
30  protected array $versions = [];
31 
37  public function __construct(int $object_id, string $path)
38  {
39  $this->object_id = $object_id;
40  $this->path = $path;
41  $this->initVersions();
42  }
43 
44  private function initVersions(): void
45  {
46  $history_data = $this->getHistoryData();
47 
48  $g = new RegexIterator(
51  $this->path,
52  FilesystemIterator::KEY_AS_PATHNAME
53  | FilesystemIterator::CURRENT_AS_FILEINFO
54  | FilesystemIterator::SKIP_DOTS
55  ),
56  RecursiveIteratorIterator::LEAVES_ONLY
57  ),
58  '/.*\/file_[\d]*\/([\d]*)\/(.*)/',
59  RegexIterator::GET_MATCH
60  );
61 
62  $this->versions = [];
63 
64  foreach ($g as $item) {
65  $version = (int) $item[1];
66  $title = $history_data[$version]['filename'] ?? $item[2];
67  $action = $history_data[$version]['action'] ?? 'create';
68  $owner = $history_data[$version]['owner_id'] ?? 13;
69  $creation_date_timestamp = strtotime($history_data[$version]['date'] ?? '0');
70  if ($creation_date_timestamp === false) {
71  $creation_date_timestamp = 0;
72  }
73  $this->versions[$version] = new ilFileObjectToStorageVersion(
74  $version,
75  $item[0],
76  $title,
77  $title,
78  $action,
79  $creation_date_timestamp,
80  $owner
81  );
82  }
83  ksort($this->versions);
84  }
85 
86 
87  private function getHistoryData(): array
88  {
89  $info = ilHistory::_getEntriesForObject($this->object_id, 'file');
90  $history_data = [];
91  foreach ($info as $i) {
92  $parsed_info = self::parseInfoParams($i);
93  $version = (int) $parsed_info['version'];
94  $history_data[$version] = $parsed_info;
95  $history_data[$version]['owner_id'] = (int) $i['user_id'];
96  $history_data[$version]['date'] = (string) $i['date'];
97  $history_data[$version]['action'] = (string) $i['action'];
98  }
99 
100  uasort($history_data, static function ($v1, $v2) {
101  return (int) $v2["version"] - (int) $v1["version"];
102  });
103  return $history_data;
104  }
105 
109  public function getVersions(): Generator
110  {
111  yield from $this->versions;
112  }
113 
117  public function getObjectId(): int
118  {
119  return $this->object_id;
120  }
121 
122  public function tearDown(): void
123  {
124  touch(rtrim($this->path, "/") . "/" . ilFileObjectToStorageMigrationHelper::MIGRATED);
125  }
126 
127  public static function parseInfoParams(array $entry): array
128  {
129  $data = explode(",", $entry["info_params"]);
130 
131  // bugfix: first created file had no version number
132  // this is a workaround for all files created before the bug was fixed
133  if (empty($data[1])) {
134  $data[1] = "1";
135  }
136 
137  if (empty($data[2])) {
138  $data[2] = "1";
139  }
140 
141  // BEGIN bugfix #31730
142  // if more than 2 commas are detected, the need for reassembling the filename is: possible to necessary
143  if (sizeof($data) > 2) {
144  $last = sizeof($data) - 1;
145  for ($n = 1; $n < $last - 1; $n++) {
146  $data[0] .= "," . $data[$n];
147  }
148 
149  // trying to distinguish the next-to-last being a 'last part of the filename'
150  // or a 'version information', based on having a dot included or not
151  if (strpos($data[$last - 1], ".") !== false) {
152  $data[0] .= "," . $data[$last - 1];
153  $data[1] = $data[$last];
154  $data[2] = $data[$last];
155  } else {
156  $data[1] = $data[$last - 1];
157  $data[2] = $data[$last];
158  }
159  }
160  // END bugfix #31730
161 
162  $result = array(
163  "filename" => $data[0],
164  "version" => $data[1],
165  "max_version" => $data[2],
166  "rollback_version" => "",
167  "rollback_user_id" => "",
168  );
169 
170  // if rollback, the version contains the rollback version as well
171  if ($entry["action"] == "rollback") {
172  $tokens = explode("|", $result["max_version"]);
173  if (count($tokens) > 1) {
174  $result["max_version"] = $tokens[0];
175  $result["rollback_version"] = $tokens[1];
176 
177  if (count($tokens) > 2) {
178  $result["rollback_user_id"] = $tokens[2];
179  }
180  }
181  }
182 
183  return $result;
184  }
185 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getEntriesForObject(int $a_obj_id, string $a_obj_type="")
get all history entries for an object
__construct(int $object_id, string $path)
ilFileObjectToStorageDirectory constructor.
$version
Definition: plugin.php:24
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$i
Definition: metadata.php:41