ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCollectWorkspaceFilesJob.php
Go to the documentation of this file.
1<?php
2
24
31{
32 private ?ilLogger $logger = null;
34
35 public function __construct()
36 {
37 global $DIC;
38
39 $user = $DIC->user();
40
41 $this->logger = ilLoggerFactory::getLogger("pwsp");
42 $this->tree = new ilWorkspaceTree($user->getId());
43 }
44
45 public function getInputTypes(): array
46 {
47 return
48 [
49 new SingleType(ilWorkspaceCopyDefinition::class),
50 new SingleType(BooleanValue::class)
51 ];
52 }
53
54 public function getOutputType(): Type
55 {
56 return new SingleType(ilWorkspaceCopyDefinition::class);
57 }
58
59 public function isStateless(): bool
60 {
61 return true;
62 }
63
64 public function run(array $input, \ILIAS\BackgroundTasks\Observer $observer): Value
65 {
66 $this->logger->debug('Start collecting files!');
67 $this->logger->dump($input);
68 $definition = $input[0];
69 $initiated_by_folder_action = $input[1]->getValue();
70 $object_wps_ids = $definition->getObjectWspIds();
71 $files = array();
72
73 foreach ($object_wps_ids as $object_wps_id) {
74 $obj_id = $this->tree->lookupObjectId($object_wps_id);
75 $object_type = ilObject::_lookupType($obj_id);
76 $object_name = ilObject::_lookupTitle($obj_id);
77 $object_temp_dir = ""; // empty as content will be added in recurseFolder and getFileDirs
78
79 if ($object_type == "wfld") {
80 $num_recursions = 0;
81 $files_from_folder = $this->recurseFolder($object_wps_id, $object_name, $object_temp_dir, $num_recursions, $initiated_by_folder_action);
82 $files = array_merge($files, $files_from_folder);
83 } elseif (($object_type == "file") and ($this->getFileDirs($object_wps_id, $object_name, $object_temp_dir) != false)) {
84 $files[] = $this->getFileDirs($object_wps_id, $object_name, $object_temp_dir);
85 }
86 }
87 $this->logger->debug('Collected files:');
88 $this->logger->dump($files);
89
90 $num_files = 0;
91 foreach ($files as $file) {
92 $definition->addCopyDefinition($file['source_dir'], $file['target_dir']);
93 $this->logger->debug('Added new copy definition: ' . $file['source_dir'] . ' -> ' . $file['target_dir']);
94
95 // count files only (without empty directories)
96 $is_empty_folder = preg_match_all("/\/$/", $file['target_dir']);
97 if (!$is_empty_folder) {
98 $num_files++;
99 }
100 }
101 $definition->setObjectWspIds($object_wps_ids);
102 $definition->setNumFiles($num_files);
103
104 return $definition;
105 }
106
107 private function getFileDirs(
108 int $a_wsp_id,
109 string $a_file_name,
110 string $a_temp_dir
111 ): ?array {
112 global $DIC;
113
114 $user = $DIC->user();
115 $ilAccess = new ilWorkspaceAccessHandler($this->tree);
116 if ($ilAccess->checkAccessOfUser($this->tree, $user->getId(), "read", "", $a_wsp_id)) {
117 $file = new ilObjFile($this->tree->lookupObjectId($a_wsp_id), false);
118 $source_dir = $file->getFile($file->getVersion());
119 if (!is_file($source_dir)) {
120 $source_dir = $file->getFile();
121 }
122 $target_dir = $a_temp_dir . '/' . ilFileUtils::getASCIIFilename($a_file_name);
123
124 return [
125 "source_dir" => $source_dir,
126 "target_dir" => $target_dir
127 ];
128 }
129 return null;
130 }
131
132 private function recurseFolder(
133 int $a_wsp_id,
134 string $a_folder_name,
135 string $a_temp_dir,
136 int $a_num_recursions,
137 bool $a_initiated_by_folder_action
138 ): array {
139 $num_recursions = $a_num_recursions + 1;
140 $tree = $this->tree;
141 $ilAccess = new ilWorkspaceAccessHandler($this->tree);
142 $files = array();
143
144 // Avoid the duplication of the uppermost folder when the download is initiated via a folder's action drop-down
145 // by not including said folders name in the temp_dir path.
146 if (($num_recursions <= 1) and ($a_initiated_by_folder_action)) {
147 $temp_dir = $a_temp_dir;
148 } else {
149 $temp_dir = $a_temp_dir . '/' . ilFileUtils::getASCIIFilename($a_folder_name);
150 }
151
152
153 $subtree = $tree->getChildsByTypeFilter($a_wsp_id, array("wfld","file"));
154
155 foreach ($subtree as $child) {
156 if (!$ilAccess->checkAccess("read", "", $child["child"])) {
157 continue;
158 }
159 if ($child["type"] == "wfld") {
160 $files_from_folder = $this->recurseFolder($child["child"], $child['title'], $temp_dir, $num_recursions, $a_initiated_by_folder_action);
161 $files = array_merge($files, $files_from_folder);
162 } elseif (($child["type"] == "file") and ($this->getFileDirs($child["child"], $child['title'], $temp_dir) != false)) {
163 $files[] = $this->getFileDirs((int) $child["wsp_id"], $child['title'], $temp_dir);
164 }
165 }
166 // ensure that empty folders are also contained in the downloaded zip
167 if (empty($subtree)) {
168 $files[] = [
169 "source_dir" => "",
170 "target_dir" => $temp_dir . '/'
171 ];
172 }
173 return $files;
174 }
175
177 {
178 return 30;
179 }
180}
recurseFolder(int $a_wsp_id, string $a_folder_name, string $a_temp_dir, int $a_num_recursions, bool $a_initiated_by_folder_action)
getFileDirs(int $a_wsp_id, string $a_file_name, string $a_temp_dir)
run(array $input, \ILIAS\BackgroundTasks\Observer $observer)
static getASCIIFilename(string $a_filename)
static getLogger(string $a_component_id)
Get component logger.
Component logger with individual log levels by component id.
Class ilObjFile.
static _lookupType(int $id, bool $reference=false)
static _lookupTitle(int $obj_id)
getChildsByTypeFilter(int $a_node_id, array $a_types, string $a_order="", string $a_direction="ASC")
get child nodes of given node by object type
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $DIC
Definition: shib_login.php:26