ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilCollectWorkspaceFilesJob.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4
8
16{
17 private $logger = null;
18
22 protected $tree;
23
27 public function __construct()
28 {
29 global $DIC;
30
31 $user = $DIC->user();
32
33 $this->logger = ilLoggerFactory::getLogger("pwsp");
34 $this->tree = new ilWorkspaceTree($user->getId());
35 }
36
40 public function getInputTypes()
41 {
42 return
43 [
44 new SingleType(ilWorkspaceCopyDefinition::class),
45 new SingleType(BooleanValue::class)
46 ];
47 }
48
52 public function getOutputType()
53 {
54 return new SingleType(ilWorkspaceCopyDefinition::class);
55 }
56
60 public function isStateless()
61 {
62 return true;
63 }
64
69 public function run(array $input, \ILIAS\BackgroundTasks\Observer $observer)
70 {
71 $this->logger->debug('Start collecting files!');
72 $this->logger->dump($input);
73 $definition = $input[0];
74 $initiated_by_folder_action = $input[1]->getValue();
75 $object_wps_ids = $definition->getObjectWspIds();
76 $files = array();
77
78 foreach ($object_wps_ids as $object_wps_id) {
79 $obj_id = $this->tree->lookupObjectId($object_wps_id);
80 $object_type = ilObject::_lookupType($obj_id);
81 $object_name = ilObject::_lookupTitle($obj_id);
82 $object_temp_dir = ""; // empty as content will be added in recurseFolder and getFileDirs
83
84 if ($object_type == "wfld") {
85 $num_recursions = 0;
86 $files_from_folder = $this->recurseFolder($object_wps_id, $object_name, $object_temp_dir, $num_recursions, $initiated_by_folder_action);
87 $files = array_merge($files, $files_from_folder);
88 } elseif (($object_type == "file") and ($this->getFileDirs($object_wps_id, $object_name, $object_temp_dir) != false)) {
89 $files[] = $this->getFileDirs($object_wps_id, $object_name, $object_temp_dir);
90 }
91 }
92 $this->logger->debug('Collected files:');
93 $this->logger->dump($files);
94
95 $num_files = 0;
96 foreach ($files as $file) {
97 $definition->addCopyDefinition($file['source_dir'], $file['target_dir']);
98 $this->logger->debug('Added new copy definition: ' . $file['source_dir'] . ' -> ' . $file['target_dir']);
99
100 // count files only (without empty directories)
101 $is_empty_folder = preg_match_all("/\/$/", $file['target_dir']);
102 if (!$is_empty_folder) {
103 $num_files++;
104 }
105 }
106 $definition->setObjectWspIds($object_wps_ids);
107 $definition->setNumFiles($num_files);
108
109 return $definition;
110 }
111
112 private function getFileDirs($a_wsp_id, $a_file_name, $a_temp_dir)
113 {
114 global $DIC;
115
116 $user = $DIC->user();
117 $ilAccess = new ilWorkspaceAccessHandler($this->tree);
118 if ($ilAccess->checkAccessOfUser($this->tree, $user->getId(), "read", "", $a_wsp_id)) {
119 $file = new ilObjFile($this->tree->lookupObjectId($a_wsp_id), false);
120 $source_dir = $file->getFile($file->getVersion());
121 if (@!is_file($source_dir)) {
122 $source_dir = $file->getFile();
123 }
124 $target_dir = $a_temp_dir . '/' . ilUtil::getASCIIFilename($a_file_name);
125
126 return $file_dirs = [
127 "source_dir" => $source_dir,
128 "target_dir" => $target_dir
129 ];
130 }
131 return false;
132 }
133
142 private function recurseFolder($a_wsp_id, $a_folder_name, $a_temp_dir, $a_num_recursions, $a_initiated_by_folder_action)
143 {
144 $num_recursions = $a_num_recursions + 1;
146 $ilAccess = new ilWorkspaceAccessHandler($this->tree);
147 $files = array();
148
149 // Avoid the duplication of the uppermost folder when the download is initiated via a folder's action drop-down
150 // by not including said folders name in the temp_dir path.
151 if (($num_recursions <= 1) and ($a_initiated_by_folder_action)) {
152 $temp_dir = $a_temp_dir;
153 } else {
154 $temp_dir = $a_temp_dir . '/' . ilUtil::getASCIIFilename($a_folder_name);
155 }
156
157
158 $subtree = $tree->getChildsByTypeFilter($a_wsp_id, array("wfld","file"));
159
160 foreach ($subtree as $child) {
161 if (!$ilAccess->checkAccess("read", "", $child["child"])) {
162 continue;
163 }
164 if ($child["type"] == "wfld") {
165 $files_from_folder = $this->recurseFolder($child["child"], $child['title'], $temp_dir, $num_recursions, $a_initiated_by_folder_action);
166 $files = array_merge($files, $files_from_folder);
167 } elseif (($child["type"] == "file") and ($this->getFileDirs($child["child"], $child['title'], $temp_dir) != false)) {
168 $files[] = $this->getFileDirs($child["ref_id"], $child['title'], $temp_dir);
169 }
170 }
171 // ensure that empty folders are also contained in the downloaded zip
172 if (empty($subtree)) {
173 $files[] = [
174 "source_dir" => "",
175 "target_dir" => $temp_dir . '/'
176 ];
177 }
178 return $files;
179 }
180
185 {
186 return 30;
187 }
188}
An exception for terminatinating execution or to throw for unit testing.
getFileDirs($a_wsp_id, $a_file_name, $a_temp_dir)
run(array $input, \ILIAS\BackgroundTasks\Observer $observer)
@inheritDoc
recurseFolder($a_wsp_id, $a_folder_name, $a_temp_dir, $a_num_recursions, $a_initiated_by_folder_action)
getExpectedTimeOfTaskInSeconds()
int the amount of seconds this task usually taskes. If your task-duration scales with the the amount ...
static getLogger($a_component_id)
Get component logger.
Class ilObjFile.
static _lookupTitle($a_id)
lookup object title
static _lookupType($a_id, $a_reference=false)
lookup object type
static getASCIIFilename($a_filename)
convert utf8 to ascii filename
Access handler for personal workspace.
Tree handler for personal workspace.
global $DIC
Definition: goto.php:24
Class ChatMainBarProvider \MainMenu\Provider.