ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilFolderDownloadBackgroundTaskHandler.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once "Services/BackgroundTask/classes/class.ilZipBackgroundTaskHandler.php";
5 
13 {
14  protected $settings; // [ilSetting]
15  protected $ref_ids = array(); // [array]
16 
17  protected static $initialized; // [bool]
18 
19  //
20  // constructor
21  //
22 
28  public function __construct()
29  {
30  parent::__construct();
31  $this->settings = new ilSetting("fold");
32  }
33 
34  public static function getInstanceFromTask(ilBackgroundTask $a_task)
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  }
51 
52 
53  //
54  // setter/getter/status
55  //
56 
62  public static function isActive()
63  {
64  $settings = new ilSetting("fold");
65  return (bool)$settings->get("bgtask_download", false);
66  }
67 
73  public function getRefIds()
74  {
75  return $this->ref_ids;
76  }
77 
83  public function setRefIds($a_val)
84  {
85  $this->ref_ids = $a_val;
86  }
87 
88 
89  //
90  // gui integration
91  //
92 
100  public static function getObjectListAction($a_ref_id)
101  {
102  self::initObjectListAction();
103 
104  return "il.BgTask.init('" . static::class . "', " . $a_ref_id . ");";
105  }
106 
107 
111  public static function initObjectListAction() {
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  }
129 
130 
131  //
132  // handler interface
133  //
134 
135  public function init($a_params = null)
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  }
215 
216  protected function gatherFiles()
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  }
251 
252 
253  //
254  // processing
255  //
256 
264  protected function calculateRecursive($a_ref_ids, &$a_file_count, &$a_file_size)
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  }
302 
311  protected function recurseFolder($a_ref_id, $a_title, $a_tmpdir, &$a_current_step)
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  }
344 
353  protected function copyFile($a_obj_id, $a_title, $a_tmpdir, &$a_current_step)
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  }
371 
378  protected function validateAccess($ref_id)
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  }
394 
395 
396  //
397  // settings
398  //
399 
405  protected function getDownloadSizeLimit()
406  {
407  return (int)$this->settings->get("bgtask_download_limit", 0);
408  }
409 
415  protected function getFileCountThreshold()
416  {
417  return (int)$this->settings->get("bgtask_download_tcount", 0);
418  }
419 
425  protected function getTotalSizeThreshold()
426  {
427  return (int)$this->settings->get("bgtask_download_tsize", 0);
428  }
429 }
ILIAS Setting Class.
Background task handler for folder downloads.
static getFinishedJson($a_task_id, $a_cmd, $a_result)
Get json for finished task.
getParams()
Gets the params.
static initObjectListAction()
init js for background download
copyFile($a_obj_id, $a_title, $a_tmpdir, &$a_current_step)
Copies a file to the specified temporary directory.
Background task handler for zip creation.
static _isInTrash($a_ref_id)
checks wether object is in trash
static getProcessingJson($a_task_id, $a_message, $a_steps)
Get json for processing task.
static getActiveByUserId($a_user_id)
static _lookupTitle($a_id)
lookup object title
calculateRecursive($a_ref_ids, &$a_file_count, &$a_file_size)
Calculates the number and size of the files being downloaded recursively.
$url
Definition: shib_logout.php:72
static getASCIIFilename($a_filename)
convert utf8 to ascii filename
static _lookupFileSize($a_id)
Quickly looks up the file size from the database and returns the number of bytes. ...
global $tpl
Definition: ilias.php:8
static initJS()
Init javascript.
global $ilCtrl
Definition: ilias.php:18
static getFailedJson($a_message)
Get json for failed task.
static _lookupObjId($a_id)
$ilUser
Definition: imgupload.php:18
Class to report exception.
static isActive()
Is folder background download active?
Create styles array
The data for the language used.
static _lookupType($a_id, $a_reference=false)
lookup object type
static makeDir($a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
getTempFolderPath()
Gets the temporary folder path to copy the files and folders to.
settings()
Definition: settings.php:2
static getBlockedJson($a_task_id)
Get json for blocked task.
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
setTask(ilBackgroundTask $a_task)
Set current task instance.
global $lng
Definition: privfeed.php:17
static _lookupAbsolutePath($obj_id, $a_version=null)
return absolute path for version
static getInstanceByRefId($a_ref_id, $stop_on_error=true)
get an instance of an Ilias object by reference id
static getInstanceFromTask(ilBackgroundTask $a_task)
Constructor/Factory.
static formatSize($size, $a_mode='short', $a_lng=null)
Returns the specified file size value in a human friendly form.
static getObjectListAction($a_ref_id)
Get object list action.
$params
Definition: example_049.php:96