ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
ilCollectFilesJob Class Reference

Description of class class. More...

+ Inheritance diagram for ilCollectFilesJob:
+ Collaboration diagram for ilCollectFilesJob:

Public Member Functions

 __construct ()
 Construct. More...
 
 getInputTypes ()
 
 getOutputType ()
 
 isStateless ()
 
 run (array $input, \ILIAS\BackgroundTasks\Observer $observer)
 
 getExpectedTimeOfTaskInSeconds ()
 
Returns
int the amount of seconds this task usually taskes. If your task-duration scales with the the amount of data, try to set a possible high value of try to calculate it. If a task duration exceeds this value, it will be displayed as "possibly failed" to the user
More...
 
- Public Member Functions inherited from ILIAS\BackgroundTasks\Implementation\Tasks\AbstractJob
 getInput ()
 
Returns
array returns the input array
More...
 
- Public Member Functions inherited from ILIAS\BackgroundTasks\Implementation\Tasks\AbstractTask
 setInput (array $values)
 
 getOutput ()
 
 getInput ()
 
 getType ()
 
 unfoldTask ()
 Unfold the task. More...
 
 getRemoveOption ()
 
Returns
Option An Option to remove the current task and do some cleanup if possible. This Option is displayed if the Bucket is completed. You do not have to provide an additional Option to remove in your UserInteraction, the remove-Option is added to the list of Options (last position)
See also
self::getAbortOption();
More...
 
 getAbortOption ()
 
Returns
Option In case a Job is failed or did not respond for some time, an Abort-Option is displayed. There is already a Standard-Abort-Option registered, you can override with your own and do some cleanup if possible.
More...
 
- Public Member Functions inherited from ILIAS\BackgroundTasks\Task\Job
 run (array $input, Observer $observer)
 

Private Member Functions

 getFileDirs ($a_ref_id, $a_file_name, $a_temp_dir)
 
 recurseFolder ($a_ref_id, $a_folder_name, $a_temp_dir, $a_num_recursions, $a_initiated_by_folder_action)
 

Private Attributes

ilLogger $logger
 

Static Private Attributes

static array $targets = []
 

Additional Inherited Members

- Data Fields inherited from ILIAS\BackgroundTasks\Implementation\Tasks\AbstractTask
const MAIN_REMOVE = 'bt_main_remove'
 
const MAIN_ABORT = 'bt_main_abort'
 
- Protected Member Functions inherited from ILIAS\BackgroundTasks\Implementation\Tasks\AbstractTask
 checkTypes ($values)
 
 extractType ($value)
 
- Protected Attributes inherited from ILIAS\BackgroundTasks\Implementation\Tasks\AbstractTask
array $input = []
 
ILIAS BackgroundTasks Value $output
 

Detailed Description

Description of class class.

Author
Lukas Zehnder lz@st.nosp@m.uder.nosp@m.-raim.nosp@m.ann..nosp@m.ch

Definition at line 31 of file class.ilCollectFilesJob.php.

Constructor & Destructor Documentation

◆ __construct()

ilCollectFilesJob::__construct ( )

Construct.

Definition at line 46 of file class.ilCollectFilesJob.php.

References $DIC, and ILIAS\Repository\logger().

47  {
48  global $DIC;
49  $this->logger = $DIC->logger()->cal();
50  }
global $DIC
Definition: feed.php:28
+ Here is the call graph for this function:

Member Function Documentation

◆ getExpectedTimeOfTaskInSeconds()

ilCollectFilesJob::getExpectedTimeOfTaskInSeconds ( )

Returns
int the amount of seconds this task usually taskes. If your task-duration scales with the the amount of data, try to set a possible high value of try to calculate it. If a task duration exceeds this value, it will be displayed as "possibly failed" to the user

Implements ILIAS\BackgroundTasks\Task\Job.

Definition at line 239 of file class.ilCollectFilesJob.php.

239  : int
240  {
241  return 30;
242  }

◆ getFileDirs()

ilCollectFilesJob::getFileDirs (   $a_ref_id,
  $a_file_name,
  $a_temp_dir 
)
private
Returns
bool|array<string, string> Please note that this method must only be called ONCE in order to detect duplicate entries. DO NOT call this method e.g. in an if condition and then again in its body.

Definition at line 142 of file class.ilCollectFilesJob.php.

References $DIC, $filename, and ensureSuffix().

Referenced by recurseFolder(), and run().

143  {
144  global $DIC;
145 
146  $user = $DIC->user();
147  $ilAccess = $DIC->access();
148  if ($ilAccess->checkAccessOfUser($user->getId(), "read", "", $a_ref_id)) {
149  $file = new ilObjFile($a_ref_id);
150  $source_dir = $file->getFile();
151 
152  if (@!is_file($source_dir)) {
153  return false;
154  }
155 
156  $filname_with_suffix = $this->ensureSuffix($a_file_name, $file->getFileExtension());
157 
158  $target_dir = $a_temp_dir . '/' . $filname_with_suffix;
159 
160  // #25025: allow duplicate filenames by appending an incrementing
161  // number per duplicate in brackets to the name.
162  // Example: test.txt, test (1).txt, test (2).txt, ...
163  if (isset(self::$targets[$target_dir])) {
164  $target_info = pathinfo($target_dir);
165  $filename = $target_info["filename"];
166  $extension = isset($target_info["extension"]) ? "." . $target_info["extension"] : "";
167  $target_dir = $a_temp_dir . $filename . " (" . ++self::$targets[$target_dir] . ")" . $extension;
168  } else {
169  self::$targets[$target_dir] = 0;
170  }
171 
172  return [
173  "source_dir" => $source_dir,
174  "target_dir" => $target_dir,
175  ];
176  }
177 
178  return false;
179  }
global $DIC
Definition: feed.php:28
Class ilObjFile.
$filename
Definition: buildRTE.php:78
ensureSuffix(string $title, ?string $suffix=null)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getInputTypes()

ilCollectFilesJob::getInputTypes ( )

Implements ILIAS\BackgroundTasks\Task.

Definition at line 56 of file class.ilCollectFilesJob.php.

56  : array
57  {
58  return
59  [
60  new SingleType(ilCopyDefinition::class),
61  new SingleType(BooleanValue::class),
62  ];
63  }

◆ getOutputType()

ilCollectFilesJob::getOutputType ( )

Implements ILIAS\BackgroundTasks\Task.

Definition at line 69 of file class.ilCollectFilesJob.php.

69  : Type
70  {
71  return new SingleType(ilCopyDefinition::class);
72  }

◆ isStateless()

ilCollectFilesJob::isStateless ( )

Implements ILIAS\BackgroundTasks\Task\Job.

Definition at line 78 of file class.ilCollectFilesJob.php.

78  : bool
79  {
80  return true;
81  }

◆ recurseFolder()

ilCollectFilesJob::recurseFolder (   $a_ref_id,
  $a_folder_name,
  $a_temp_dir,
  $a_num_recursions,
  $a_initiated_by_folder_action 
)
private
Parameters
$ref_id
$title
$tmpdir
Returns
mixed[]

Definition at line 189 of file class.ilCollectFilesJob.php.

References $DIC, ilObject\_isInTrash(), ilFileUtils\getASCIIFilename(), and getFileDirs().

Referenced by run().

189  : array
190  {
191  global $DIC;
192 
193  $num_recursions = $a_num_recursions + 1;
194  $tree = $DIC->repositoryTree();
195  $ilAccess = $DIC->access();
196  $files = array();
197 
198  // Avoid the duplication of the uppermost folder when the download is initiated via a folder's action drop-down
199  // by not including said folders name in the temp_dir path.
200  if ($num_recursions <= 1 && $a_initiated_by_folder_action) {
201  $temp_dir = $a_temp_dir;
202  } else {
203  $temp_dir = $a_temp_dir . '/' . ilFileUtils::getASCIIFilename($a_folder_name);
204  }
205 
206  $subtree = $tree->getChildsByTypeFilter($a_ref_id, array("fold", "file"));
207 
208  foreach ($subtree as $child) {
209  if (!$ilAccess->checkAccess("read", "", $child["ref_id"])) {
210  continue;
211  }
212  if (ilObject::_isInTrash($child["ref_id"])) {
213  continue;
214  }
215  if ($child["type"] == "fold") {
216  $files_from_folder = $this->recurseFolder($child["ref_id"], $child['title'], $temp_dir, $num_recursions, $a_initiated_by_folder_action);
217  $files = array_merge($files, $files_from_folder);
218  } else {
219  if (($child["type"] === "file") && (($dirs = $this->getFileDirs($child["ref_id"], $child['title'], $temp_dir)) !== false)) {
220  $files[] = $dirs;
221  }
222  }
223  }
224  // ensure that empty folders are also contained in the downloaded zip
225  if (empty($subtree)) {
226  $files[] = [
227  "source_dir" => "",
228  "target_dir" => $temp_dir . '/',
229  ];
230  }
231 
232  return $files;
233  }
getFileDirs($a_ref_id, $a_file_name, $a_temp_dir)
static getASCIIFilename(string $a_filename)
global $DIC
Definition: feed.php:28
recurseFolder($a_ref_id, $a_folder_name, $a_temp_dir, $a_num_recursions, $a_initiated_by_folder_action)
static _isInTrash(int $ref_id)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ run()

ilCollectFilesJob::run ( array  $input,
\ILIAS\BackgroundTasks\Observer  $observer 
)

Todo:
use filsystem service

Definition at line 88 of file class.ilCollectFilesJob.php.

References getFileDirs(), ilObjectFactory\getInstanceByRefId(), ILIAS\Repository\logger(), and recurseFolder().

88  : Value
89  {
90  $this->logger->debug('Start collecting files!');
91  $this->logger->dump($input);
92  $definition = $input[0];
93  $initiated_by_folder_action = $input[1]->getValue();
94  $object_ref_ids = $definition->getObjectRefIds();
95  $files = array();
96 
97  foreach ($object_ref_ids as $object_ref_id) {
98  $object = ilObjectFactory::getInstanceByRefId($object_ref_id);
99  $object_type = $object->getType();
100  $object_name = $object->getTitle();
101  $object_temp_dir = ""; // empty as content will be added in recurseFolder and getFileDirs
102 
103  if ($object_type === "fold" || $object_type === "crs") {
104  $num_recursions = 0;
105  $files_from_folder = $this->recurseFolder($object_ref_id, $object_name, $object_temp_dir, $num_recursions, $initiated_by_folder_action);
106  $files = array_merge($files, $files_from_folder);
107  } elseif (($object_type === "file") && (($file_dirs = $this->getFileDirs(
108  $object_ref_id,
109  $object_name,
110  $object_temp_dir
111  )) !== false)) {
112  $files[] = $file_dirs;
113  }
114  }
115  $this->logger->debug('Collected files:');
116  $this->logger->dump($files);
117 
118  $num_files = 0;
119  foreach ($files as $file) {
120  $definition->addCopyDefinition($file['source_dir'], $file['target_dir']);
121  $this->logger->debug('Added new copy definition: ' . $file['source_dir'] . ' -> ' . $file['target_dir']);
122 
123  // count files only (without empty directories)
124  $is_empty_folder = preg_match_all("/\/$/", $file['target_dir']);
125  if (!$is_empty_folder) {
126  $num_files++;
127  }
128  }
129  $definition->setObjectRefIds($object_ref_ids);
130  $definition->setNumFiles($num_files);
131 
132  return $definition;
133  }
getFileDirs($a_ref_id, $a_file_name, $a_temp_dir)
recurseFolder($a_ref_id, $a_folder_name, $a_temp_dir, $a_num_recursions, $a_initiated_by_folder_action)
static getInstanceByRefId(int $ref_id, bool $stop_on_error=true)
get an instance of an Ilias object by reference id
+ Here is the call graph for this function:

Field Documentation

◆ $logger

ilLogger ilCollectFilesJob::$logger
private

Definition at line 35 of file class.ilCollectFilesJob.php.

◆ $targets

array ilCollectFilesJob::$targets = []
staticprivate

Definition at line 41 of file class.ilCollectFilesJob.php.


The documentation for this class was generated from the following file: