ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
ilFolderDownloadBackgroundTaskHandler Class Reference

Background task handler for folder downloads. More...

+ Inheritance diagram for ilFolderDownloadBackgroundTaskHandler:
+ Collaboration diagram for ilFolderDownloadBackgroundTaskHandler:

Public Member Functions

 __construct ()
 Constructor. More...
 
 getRefIds ()
 Gets the involved reference ids. More...
 
 setRefIds ($a_val)
 Sets the involved reference ids. More...
 
 init ($a_params=null)
 Init background task. More...
 
- Public Member Functions inherited from ilZipBackgroundTaskHandler
 __construct ()
 Constructor. More...
 
 setDeliveryFilename ($a_value)
 Sets the delivery file name. More...
 
 getDeliveryFilename ()
 Gets the delivery file name. More...
 
 getTask ()
 Get current task instance. More...
 
 process ()
 Process the task. More...
 
 cancel ()
 Cancel download. More...
 
 finish ()
 Finish download. More...
 
 deliver ()
 Deliver file. More...
 
 deleteTaskAndFiles ()
 Remove task and its files. More...
 

Static Public Member Functions

static getInstanceFromTask (ilBackgroundTask $a_task)
 Constructor/Factory. More...
 
static isActive ()
 Is folder background download active? More...
 
static getObjectListAction ($a_ref_id)
 Get object list action. More...
 
static initObjectListAction ()
 init js for background download More...
 

Protected Member Functions

 gatherFiles ()
 
 calculateRecursive ($a_ref_ids, &$a_file_count, &$a_file_size)
 Calculates the number and size of the files being downloaded recursively. More...
 
 recurseFolder ($a_ref_id, $a_title, $a_tmpdir, &$a_current_step)
 Copies a folder and its files to the specified temporary directory. More...
 
 copyFile ($a_obj_id, $a_title, $a_tmpdir, &$a_current_step)
 Copies a file to the specified temporary directory. More...
 
 validateAccess ($ref_id)
 Check file access. More...
 
 getDownloadSizeLimit ()
 Get overall download size limit. More...
 
 getFileCountThreshold ()
 Get file count threshold. More...
 
 getTotalSizeThreshold ()
 Get total size threshold. More...
 
- Protected Member Functions inherited from ilZipBackgroundTaskHandler
 setTask (ilBackgroundTask $a_task)
 Set current task instance. More...
 
 gatherFiles ()
 Copy files to target directory. More...
 
 deleteTempFiles ($a_delete_zip=true)
 Deletes the temporary files and folders belonging to this download. More...
 
 getTempFolderPath ()
 Gets the temporary folder path to copy the files and folders to. More...
 
 getTempZipFilePath ()
 Gets the full path of the temporary zip file that gets created. More...
 
 getTempBasePath ()
 Gets the temporary base path for all files and folders related to this download. More...
 

Protected Attributes

 $settings
 
 $ref_ids = array()
 
- Protected Attributes inherited from ilZipBackgroundTaskHandler
 $task
 
 $filename
 

Static Protected Attributes

static $initialized
 

Detailed Description

Background task handler for folder downloads.

Author
Jörg Lützenkirchen luetz.nosp@m.enki.nosp@m.rchen.nosp@m.@lei.nosp@m.fos.c.nosp@m.om

Definition at line 12 of file class.ilFolderDownloadBackgroundTaskHandler.php.

Constructor & Destructor Documentation

◆ __construct()

ilFolderDownloadBackgroundTaskHandler::__construct ( )

Constructor.

Returns
self

Definition at line 28 of file class.ilFolderDownloadBackgroundTaskHandler.php.

References settings().

29  {
30  parent::__construct();
31  $this->settings = new ilSetting("fold");
32  }
ILIAS Setting Class.
settings()
Definition: settings.php:2
+ Here is the call graph for this function:

Member Function Documentation

◆ calculateRecursive()

ilFolderDownloadBackgroundTaskHandler::calculateRecursive (   $a_ref_ids,
$a_file_count,
$a_file_size 
)
protected

Calculates the number and size of the files being downloaded recursively.

Parameters
array$a_ref_ids
int&$a_file_count
int&$a_file_size

Definition at line 264 of file class.ilFolderDownloadBackgroundTaskHandler.php.

References $ref_id, ilObjFileAccess\_lookupFileSize(), ilObject\_lookupObjId(), ilObject\_lookupType(), array, and validateAccess().

Referenced by init().

265  {
266  global $tree;
267 
268  include_once("./Modules/File/classes/class.ilObjFileAccess.php");
269 
270  // parse folders
271  foreach ($a_ref_ids as $ref_id)
272  {
273  if(!$this->validateAccess($ref_id))
274  {
275  continue;
276  }
277 
278  // we are only interested in folders and files
279  switch(ilObject::_lookupType($ref_id, true))
280  {
281  case "fold":
282  // get child objects
283  $subtree = $tree->getChildsByTypeFilter($ref_id, array("fold", "file"));
284  if(count($subtree) > 0)
285  {
286  $child_ref_ids = array();
287  foreach($subtree as $child)
288  {
289  $child_ref_ids[] = $child["ref_id"];
290  }
291  $this->calculateRecursive($child_ref_ids, $a_file_count, $a_file_size);
292  }
293  break;
294 
295  case "file":
297  $a_file_count += 1;
298  break;
299  }
300  }
301  }
calculateRecursive($a_ref_ids, &$a_file_count, &$a_file_size)
Calculates the number and size of the files being downloaded recursively.
static _lookupFileSize($a_id)
Quickly looks up the file size from the database and returns the number of bytes. ...
static _lookupObjId($a_id)
Create styles array
The data for the language used.
static _lookupType($a_id, $a_reference=false)
lookup object type
$ref_id
Definition: sahs_server.php:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ copyFile()

ilFolderDownloadBackgroundTaskHandler::copyFile (   $a_obj_id,
  $a_title,
  $a_tmpdir,
$a_current_step 
)
protected

Copies a file to the specified temporary directory.

Parameters
int$a_obj_id
string$a_title
string$a_tmpdir
int&$a_current_step

Definition at line 353 of file class.ilFolderDownloadBackgroundTaskHandler.php.

References ilObjFile\_lookupAbsolutePath(), and ilUtil\getASCIIFilename().

Referenced by gatherFiles(), and recurseFolder().

354  {
355  // :TODO: every file?
356  $this->task->setCurrentStep(++$a_current_step);
357  $this->task->save();
358 
359  $new_filename = $a_tmpdir . "/" . ilUtil::getASCIIFilename($a_title);
360 
361  // copy to temporary directory
362  include_once "Modules/File/classes/class.ilObjFile.php";
363  $old_filename = ilObjFile::_lookupAbsolutePath($a_obj_id);
364  if(!copy($old_filename, $new_filename))
365  {
366  throw new ilFileException("Could not copy ".$old_filename." to ".$new_filename);
367  }
368 
369  touch($new_filename, filectime($old_filename));
370  }
static getASCIIFilename($a_filename)
convert utf8 to ascii filename
Class to report exception.
static _lookupAbsolutePath($obj_id, $a_version=null)
return absolute path for version
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ gatherFiles()

ilFolderDownloadBackgroundTaskHandler::gatherFiles ( )
protected

Definition at line 216 of file class.ilFolderDownloadBackgroundTaskHandler.php.

References $ref_id, copyFile(), ilObjectFactory\getInstanceByRefId(), getRefIds(), ilZipBackgroundTaskHandler\getTempFolderPath(), recurseFolder(), and validateAccess().

217  {
218  $tmpdir = $this->getTempFolderPath();
219 
220  $current_step = 0;
221 
222  // parse folders
223  foreach($this->getRefIds() as $ref_id)
224  {
225  // has been cancelled: hurry up
226  if($this->task->isToBeCancelled())
227  {
228  return;
229  }
230 
231  if(!$this->validateAccess($ref_id))
232  {
233  continue;
234  }
235 
236  $object = ilObjectFactory::getInstanceByRefId($ref_id);
237  switch($object->getType())
238  {
239  case "fold":
240  $this->recurseFolder($ref_id, $object->getTitle(), $tmpdir, $current_step);
241  break;
242 
243  case "file":
244  $this->copyFile($object->getId(), $object->getTitle(), $tmpdir, $current_step);
245  break;
246  }
247  }
248 
249  return $current_step;
250  }
copyFile($a_obj_id, $a_title, $a_tmpdir, &$a_current_step)
Copies a file to the specified temporary directory.
getTempFolderPath()
Gets the temporary folder path to copy the files and folders to.
recurseFolder($a_ref_id, $a_title, $a_tmpdir, &$a_current_step)
Copies a folder and its files to the specified temporary directory.
$ref_id
Definition: sahs_server.php:39
static getInstanceByRefId($a_ref_id, $stop_on_error=true)
get an instance of an Ilias object by reference id
+ Here is the call graph for this function:

◆ getDownloadSizeLimit()

ilFolderDownloadBackgroundTaskHandler::getDownloadSizeLimit ( )
protected

Get overall download size limit.

Returns
int

Definition at line 405 of file class.ilFolderDownloadBackgroundTaskHandler.php.

References settings().

Referenced by init().

406  {
407  return (int)$this->settings->get("bgtask_download_limit", 0);
408  }
settings()
Definition: settings.php:2
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getFileCountThreshold()

ilFolderDownloadBackgroundTaskHandler::getFileCountThreshold ( )
protected

Get file count threshold.

Returns
int

Definition at line 415 of file class.ilFolderDownloadBackgroundTaskHandler.php.

References settings().

Referenced by init().

416  {
417  return (int)$this->settings->get("bgtask_download_tcount", 0);
418  }
settings()
Definition: settings.php:2
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getInstanceFromTask()

static ilFolderDownloadBackgroundTaskHandler::getInstanceFromTask ( ilBackgroundTask  $a_task)
static

Constructor/Factory.

Parameters
ilBackgroundTask$a_task
Returns

Implements ilBackgroundTaskHandler.

Definition at line 34 of file class.ilFolderDownloadBackgroundTaskHandler.php.

References $params, $ref_id, ilObject\_lookupObjId(), ilObject\_lookupTitle(), and ilBackgroundTask\getParams().

35  {
36  global $tree;
37 
38  $obj = new self();
39  $obj->setTask($a_task);
40 
41  $params = $a_task->getParams();
42  $obj->setRefIds($params["ref_ids"]);
43 
44  $ref_id = (sizeof($params["ref_ids"]) == 1)
45  ? $params["ref_ids"][0]
46  : $tree->getParentId($params["ref_ids"][0]);
47  $obj->setDeliveryFilename(ilObject::_lookupTitle(ilObject::_lookupObjId($ref_id)));
48 
49  return $obj;
50  }
getParams()
Gets the params.
static _lookupTitle($a_id)
lookup object title
static _lookupObjId($a_id)
$ref_id
Definition: sahs_server.php:39
$params
Definition: example_049.php:96
+ Here is the call graph for this function:

◆ getObjectListAction()

static ilFolderDownloadBackgroundTaskHandler::getObjectListAction (   $a_ref_id)
static

Get object list action.

See also
ilObjectListGUI::insertCommand()
Parameters
int$a_ref_id
Returns
string

Definition at line 100 of file class.ilFolderDownloadBackgroundTaskHandler.php.

Referenced by ilObjectListGUI\insertCommand().

101  {
102  self::initObjectListAction();
103 
104  return "il.BgTask.init('" . static::class . "', " . $a_ref_id . ");";
105  }
+ Here is the caller graph for this function:

◆ getRefIds()

ilFolderDownloadBackgroundTaskHandler::getRefIds ( )

Gets the involved reference ids.

Returns
array

Definition at line 73 of file class.ilFolderDownloadBackgroundTaskHandler.php.

References $ref_ids.

Referenced by gatherFiles(), and init().

+ Here is the caller graph for this function:

◆ getTotalSizeThreshold()

ilFolderDownloadBackgroundTaskHandler::getTotalSizeThreshold ( )
protected

Get total size threshold.

Returns
int

Definition at line 425 of file class.ilFolderDownloadBackgroundTaskHandler.php.

References settings().

Referenced by init().

426  {
427  return (int)$this->settings->get("bgtask_download_tsize", 0);
428  }
settings()
Definition: settings.php:2
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ init()

ilFolderDownloadBackgroundTaskHandler::init (   $a_params = null)

Init background task.

Parameters
mixed$a_params
Returns
json

Implements ilBackgroundTaskHandler.

Definition at line 135 of file class.ilFolderDownloadBackgroundTaskHandler.php.

References $ilUser, $lng, $res, ilZipBackgroundTaskHandler\$task, array, calculateRecursive(), ilZipBackgroundTaskHandler\finish(), ilUtil\formatSize(), ilBackgroundTask\getActiveByUserId(), ilBackgroundTaskJson\getBlockedJson(), getDownloadSizeLimit(), ilBackgroundTaskJson\getFailedJson(), getFileCountThreshold(), ilBackgroundTaskJson\getFinishedJson(), ilBackgroundTaskJson\getProcessingJson(), getRefIds(), getTotalSizeThreshold(), ilZipBackgroundTaskHandler\process(), setRefIds(), ilZipBackgroundTaskHandler\setTask(), ilBackgroundTask\STATUS_FINISHED, and ilBackgroundTask\STATUS_INITIALIZED.

136  {
137  global $lng, $ilUser;
138 
139  if($a_params)
140  {
141  $this->setRefIds(explode(",", $a_params));
142  }
143 
144  $file_count = $total_bytes = 0;
145  $this->calculateRecursive($this->getRefIds(), $file_count, $total_bytes);
146 
147  include_once "Services/BackgroundTask/classes/class.ilBackgroundTaskJson.php";
148 
149  // empty folder - nothing to do
150  if(!$file_count)
151  {
152  $json = ilBackgroundTaskJson::getFailedJson($lng->txt("bgtask_empty_folder"));
153  }
154  else
155  {
156  // check if below download size limit
157  $size_limit_mb = $this->getDownloadSizeLimit() * 1024 * 1024;
158  if($size_limit_mb > 0 && $total_bytes > $size_limit_mb)
159  {
160  $json = ilBackgroundTaskJson::getFailedJson(sprintf($lng->txt("bgtask_download_too_large"), ilUtil::formatSize($size_limit_mb)));
161  }
162  else
163  {
164  // set up task instance
165  include_once "Services/BackgroundTask/classes/class.ilBackgroundTask.php";
166  $task = new ilBackgroundTask();
167  $task->setHandlerId(get_class($this));
168  $task->setUserId($ilUser->getId());
169  $task->setParams(array(
170  "ref_ids" => $this->getRefIds()
171  ));
172  $task->setSteps($file_count+1); // +1 = create zip
174  $task->save();
175 
176  $this->setTask($task);
177 
178  // above thresholds: do background task
179  if($file_count >= $this->getFileCountThreshold()
180  || $total_bytes >= $this->getTotalSizeThreshold() * 1024 * 1024)
181  {
182  // check for other tasks from same user
183  $existing = ilBackgroundTask::getActiveByUserId($ilUser->getId());
184  if(sizeof($existing))
185  {
186  $json = ilBackgroundTaskJson::getBlockedJson($task->getId());
187  }
188  else
189  {
191  $task->getId(),
192  sprintf($lng->txt("bgtask_download_long"), $file_count, ilUtil::formatSize($total_bytes)),
193  $file_count+1
194  );
195  }
196  }
197  // below thresholds: direct download
198  else
199  {
200  $this->process();
201 
203  $task->save();
204 
205  $res = $this->finish();
206 
207  // see ilBackgroundTaskHub::progress()
208  $json = ilBackgroundTaskJson::getFinishedJson($task->getId(), $res[0], $res[1]);
209  }
210  }
211  }
212 
213  return $json;
214  }
static getFinishedJson($a_task_id, $a_cmd, $a_result)
Get json for finished task.
static getProcessingJson($a_task_id, $a_message, $a_steps)
Get json for processing task.
static getActiveByUserId($a_user_id)
calculateRecursive($a_ref_ids, &$a_file_count, &$a_file_size)
Calculates the number and size of the files being downloaded recursively.
static getFailedJson($a_message)
Get json for failed task.
$ilUser
Definition: imgupload.php:18
Create styles array
The data for the language used.
static getBlockedJson($a_task_id)
Get json for blocked task.
setTask(ilBackgroundTask $a_task)
Set current task instance.
global $lng
Definition: privfeed.php:17
static formatSize($size, $a_mode='short', $a_lng=null)
Returns the specified file size value in a human friendly form.
+ Here is the call graph for this function:

◆ initObjectListAction()

static ilFolderDownloadBackgroundTaskHandler::initObjectListAction ( )
static

init js for background download

Definition at line 111 of file class.ilFolderDownloadBackgroundTaskHandler.php.

References $ilCtrl, $tpl, $url, array, and ilModalGUI\initJS().

Referenced by ilContainerContentGUI\renderItem().

111  {
112  // js init only needed once per request
113  if(!self::$initialized)
114  {
115  global $tpl, $ilCtrl;
116 
117  $url = $ilCtrl->getLinkTargetByClass(array("ilrepositorygui", "ilobjfoldergui", "ilbackgroundtaskhub"), "", "", true, false);
118 
119  $tpl->addJavaScript("Services/BackgroundTask/js/BgTask.js");
120  $tpl->addOnLoadCode('il.BgTask.setAjax("'.$url.'");');
121 
122  // enable modals from js
123  include_once "Services/UIComponent/Modal/classes/class.ilModalGUI.php";
125 
126  self::$initialized = true;
127  }
128  }
$url
Definition: shib_logout.php:72
global $tpl
Definition: ilias.php:8
static initJS()
Init javascript.
global $ilCtrl
Definition: ilias.php:18
Create styles array
The data for the language used.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isActive()

static ilFolderDownloadBackgroundTaskHandler::isActive ( )
static

Is folder background download active?

Returns
boolean

Definition at line 62 of file class.ilFolderDownloadBackgroundTaskHandler.php.

References $settings.

Referenced by ilObjectListGUI\insertCommand().

63  {
64  $settings = new ilSetting("fold");
65  return (bool)$settings->get("bgtask_download", false);
66  }
ILIAS Setting Class.
+ Here is the caller graph for this function:

◆ recurseFolder()

ilFolderDownloadBackgroundTaskHandler::recurseFolder (   $a_ref_id,
  $a_title,
  $a_tmpdir,
$a_current_step 
)
protected

Copies a folder and its files to the specified temporary directory.

Parameters
int$a_ref_id
string$a_title
string$a_tmpdir
int&$a_current_step

Definition at line 311 of file class.ilFolderDownloadBackgroundTaskHandler.php.

References array, copyFile(), ilUtil\getASCIIFilename(), ilUtil\makeDir(), and validateAccess().

Referenced by gatherFiles().

312  {
313  global $tree;
314 
315  $tmpdir = $a_tmpdir . "/" . ilUtil::getASCIIFilename($a_title);
316  ilUtil::makeDir($tmpdir);
317 
318  $subtree = $tree->getChildsByTypeFilter($a_ref_id, array("fold", "file"));
319  foreach($subtree as $child)
320  {
321  // has been cancelled: hurry up
322  if($this->task->isToBeCancelled())
323  {
324  return;
325  }
326 
327  if(!$this->validateAccess($child["ref_id"]))
328  {
329  continue;
330  }
331 
332  switch($child["type"])
333  {
334  case "fold":
335  $this->recurseFolder($child["ref_id"], $child["title"], $tmpdir, $a_current_step);
336  break;
337 
338  case "file":
339  $this->copyFile($child["obj_id"], $child["title"], $tmpdir, $a_current_step);
340  break;
341  }
342  }
343  }
copyFile($a_obj_id, $a_title, $a_tmpdir, &$a_current_step)
Copies a file to the specified temporary directory.
static getASCIIFilename($a_filename)
convert utf8 to ascii filename
Create styles array
The data for the language used.
static makeDir($a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
recurseFolder($a_ref_id, $a_title, $a_tmpdir, &$a_current_step)
Copies a folder and its files to the specified temporary directory.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setRefIds()

ilFolderDownloadBackgroundTaskHandler::setRefIds (   $a_val)

Sets the involved reference ids.

Parameters
array$a_val

Definition at line 83 of file class.ilFolderDownloadBackgroundTaskHandler.php.

Referenced by init().

84  {
85  $this->ref_ids = $a_val;
86  }
+ Here is the caller graph for this function:

◆ validateAccess()

ilFolderDownloadBackgroundTaskHandler::validateAccess (   $ref_id)
protected

Check file access.

Parameters
int$ref_id
Returns
boolean

Definition at line 378 of file class.ilFolderDownloadBackgroundTaskHandler.php.

References $ref_id, and ilObject\_isInTrash().

Referenced by calculateRecursive(), gatherFiles(), and recurseFolder().

379  {
380  global $ilAccess;
381 
382  if(!$ilAccess->checkAccess("read", "", $ref_id))
383  {
384  return false;
385  }
386 
388  {
389  return false;
390  }
391 
392  return true;
393  }
static _isInTrash($a_ref_id)
checks wether object is in trash
$ref_id
Definition: sahs_server.php:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $initialized

ilFolderDownloadBackgroundTaskHandler::$initialized
staticprotected

◆ $ref_ids

ilFolderDownloadBackgroundTaskHandler::$ref_ids = array()
protected

Definition at line 15 of file class.ilFolderDownloadBackgroundTaskHandler.php.

Referenced by getRefIds().

◆ $settings

ilFolderDownloadBackgroundTaskHandler::$settings
protected

Definition at line 14 of file class.ilFolderDownloadBackgroundTaskHandler.php.

Referenced by isActive().


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