ILIAS  trunk Revision v11.0_alpha-1861-g09f3d197f78
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
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, 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...
 

Private Member Functions

 getFileDirs ($a_ref_id, string $a_file_name, string $a_temp_dir)
 
 recurseFolder ($a_ref_id, string $a_folder_name, string $a_temp_dir, int|float $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 (array $values)
 
 extractType ($value)
 
- Protected Attributes inherited from ILIAS\BackgroundTasks\Implementation\Tasks\AbstractTask
array $input = []
 
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 32 of file class.ilCollectFilesJob.php.

Constructor & Destructor Documentation

◆ __construct()

ilCollectFilesJob::__construct ( )

Construct.

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

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

48  {
49  global $DIC;
50  $this->logger = $DIC->logger()->cal();
51  }
global $DIC
Definition: shib_login.php:22
+ 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 238 of file class.ilCollectFilesJob.php.

238  : int
239  {
240  return 30;
241  }

◆ getFileDirs()

ilCollectFilesJob::getFileDirs (   $a_ref_id,
string  $a_file_name,
string  $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 143 of file class.ilCollectFilesJob.php.

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

Referenced by recurseFolder(), and run().

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

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

◆ getOutputType()

ilCollectFilesJob::getOutputType ( )

Implements ILIAS\BackgroundTasks\Task.

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

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

◆ isStateless()

ilCollectFilesJob::isStateless ( )

Implements ILIAS\BackgroundTasks\Task\Job.

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

79  : bool
80  {
81  return true;
82  }

◆ recurseFolder()

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

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

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

Referenced by run().

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

◆ run()

ilCollectFilesJob::run ( array  $input,
Observer  $observer 
)

Todo:
use filsystem service

Implements ILIAS\BackgroundTasks\Task\Job.

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

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

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

Field Documentation

◆ $logger

ilLogger ilCollectFilesJob::$logger
private

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

◆ $targets

array ilCollectFilesJob::$targets = []
staticprivate

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


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