ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilCollectFilesJob.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
7 
15 {
21  private static $targets = [];
22 
23  private $logger = null;
24 
28  public function __construct()
29  {
30  $this->logger = $GLOBALS['DIC']->logger()->cal();
31  }
32 
33 
37  public function getInputTypes()
38  {
39  return
40  [
41  new SingleType(ilCopyDefinition::class),
42  new SingleType(BooleanValue::class),
43  ];
44  }
45 
46 
50  public function getOutputType()
51  {
52  return new SingleType(ilCopyDefinition::class);
53  }
54 
55 
59  public function isStateless()
60  {
61  return true;
62  }
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_ref_ids = $definition->getObjectRefIds();
76  $files = array();
77 
78  foreach ($object_ref_ids as $object_ref_id) {
79  $object = ilObjectFactory::getInstanceByRefId($object_ref_id);
80  $object_type = $object->getType();
81  $object_name = $object->getTitle();
82  $object_temp_dir = ""; // empty as content will be added in recurseFolder and getFileDirs
83 
84  if ($object_type == "fold") {
85  $num_recursions = 0;
86  $files_from_folder = self::recurseFolder($object_ref_id, $object_name, $object_temp_dir, $num_recursions, $initiated_by_folder_action);
87  $files = array_merge($files, $files_from_folder);
88  } else {
89  if (($object_type == "file") and (($file_dirs = self::getFileDirs($object_ref_id, $object_name, $object_temp_dir)) != false)) {
90  $files[] = $file_dirs;
91  }
92  }
93  }
94  $this->logger->debug('Collected files:');
95  $this->logger->dump($files);
96 
97  $num_files = 0;
98  foreach ($files as $file) {
99  $definition->addCopyDefinition($file['source_dir'], $file['target_dir']);
100  $this->logger->debug('Added new copy definition: ' . $file['source_dir'] . ' -> ' . $file['target_dir']);
101 
102  // count files only (without empty directories)
103  $is_empty_folder = preg_match_all("/\/$/", $file['target_dir']);
104  if (!$is_empty_folder) {
105  $num_files++;
106  }
107  }
108  $definition->setObjectRefIds($object_ref_ids);
109  $definition->setNumFiles($num_files);
110 
111  return $definition;
112  }
113 
119  private static function getFileDirs($a_ref_id, $a_file_name, $a_temp_dir)
120  {
121  global $DIC;
122 
123  $user = $DIC->user();
124  $ilAccess = $DIC->access();
125  if ($ilAccess->checkAccessOfUser($user->getId(), "read", "", $a_ref_id)) {
126  $file = new ilObjFile($a_ref_id);
127  $source_dir = $file->getDirectory($file->getVersion()) . "/" . $file->getFileName();
128  if (@!is_file($source_dir)) {
129  $source_dir = $file->getDirectory() . "/" . $file->getFileName();
130  }
131  $target_dir = $a_temp_dir . '/' . ilUtil::getASCIIFilename($a_file_name);
132 
133  // #25025: allow duplicate filenames by appending an incrementing
134  // number per duplicate in brackets to the name.
135  // Example: test.txt, test (1).txt, test (2).txt, ...
136  if (isset(self::$targets[$target_dir])) {
137  $target_info = pathinfo($target_dir);
138  $target_dir = $a_temp_dir . $target_info["filename"] . " (" . ++self::$targets[$target_dir] . ")." . $target_info["extension"];
139  } else {
140  self::$targets[$target_dir] = 0;
141  }
142 
143  return [
144  "source_dir" => $source_dir,
145  "target_dir" => $target_dir,
146  ];
147  }
148 
149  return false;
150  }
151 
152 
160  private static function recurseFolder($a_ref_id, $a_folder_name, $a_temp_dir, $a_num_recursions, $a_initiated_by_folder_action)
161  {
162  global $DIC;
163 
164  $num_recursions = $a_num_recursions + 1;
165  $tree = $DIC->repositoryTree();
166  $ilAccess = $DIC->access();
167  $files = array();
168 
169  // Avoid the duplication of the uppermost folder when the download is initiated via a folder's action drop-down
170  // by not including said folders name in the temp_dir path.
171  if (($num_recursions <= 1) and ($a_initiated_by_folder_action)) {
172  $temp_dir = $a_temp_dir;
173  } else {
174  $temp_dir = $a_temp_dir . '/' . ilUtil::getASCIIFilename($a_folder_name);
175  }
176 
177  $subtree = $tree->getChildsByTypeFilter($a_ref_id, array("fold", "file"));
178 
179  foreach ($subtree as $child) {
180  if (!$ilAccess->checkAccess("read", "", $child["ref_id"])) {
181  continue;
182  }
183  if (ilObject::_isInTrash($child["ref_id"])) {
184  continue;
185  }
186  if ($child["type"] == "fold") {
187  $files_from_folder = self::recurseFolder($child["ref_id"], $child['title'], $temp_dir, $num_recursions, $a_initiated_by_folder_action);
188  $files = array_merge($files, $files_from_folder);
189  } else {
190  if (($child["type"] == "file") and (($dirs = self::getFileDirs($child["ref_id"], $child['title'], $temp_dir)) != false)) {
191  $files[] = $dirs;
192  }
193  }
194  }
195  // ensure that empty folders are also contained in the downloaded zip
196  if (empty($subtree)) {
197  $files[] = [
198  "source_dir" => "",
199  "target_dir" => $temp_dir . '/',
200  ];
201  }
202 
203  return $files;
204  }
205 
206 
211  {
212  return 30;
213  }
214 }
static getFileDirs($a_ref_id, $a_file_name, $a_temp_dir)
Please note that this method must only be called ONCE in order to detect duplicate entries...
static recurseFolder($a_ref_id, $a_folder_name, $a_temp_dir, $a_num_recursions, $a_initiated_by_folder_action)
Description of class class.
Class ChatMainBarProvider .
static _isInTrash($a_ref_id)
checks wether object is in trash
static getASCIIFilename($a_filename)
convert utf8 to ascii filename
run(array $input, \ILIAS\BackgroundTasks\Observer $observer)
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
$DIC
Definition: xapitoken.php:46
static getInstanceByRefId($a_ref_id, $stop_on_error=true)
get an instance of an Ilias object by reference id
getExpectedTimeOfTaskInSeconds()
int the amount of seconds this task usually taskes. If your task-duration scales with the the amount ...