ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilCronPersonalWorkspaceRecalculateQuota.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2017 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4include_once "Services/Cron/classes/class.ilCronJob.php";
5
15{
19 protected $lng;
20
24 protected $db;
25
29 protected $job_status;
30
34 public function __construct()
35 {
36 global $DIC;
37
38 $this->lng = $DIC->language();
39 $ilDB = $DIC->database();
40
41 $this->db = $ilDB;
42 }
43
44
48 public function getId()
49 {
50 return "pwsp_recalc_quota";
51 }
52
56 public function getTitle()
57 {
59
60 return $lng->txt("pwsp_recalculate_disk_quota");
61 }
62
66 public function getDescription()
67 {
69
70 return $lng->txt("pwsp_recalculate_disk_quota_desc");
71 }
72
76 public function getDefaultScheduleType()
77 {
79 }
80
84 public function getDefaultScheduleValue()
85 {
86 return;
87 }
88
92 public function hasAutoActivation()
93 {
94 return false;
95 }
96
100 public function hasFlexibleSchedule()
101 {
102 return true;
103 }
104
108 public function hasCustomSettings()
109 {
110 return true;
111 }
112
116 public function run()
117 {
118 $this->job_status = ilCronJobResult::STATUS_NO_ACTION;
119
120 $this->recalculate();
121
122 $this->job_status = ilCronJobResult::STATUS_OK;
123
124 $result = new ilCronJobResult();
125 $result->setStatus($this->job_status);
126 return $result;
127 }
128
132 public function recalculate()
133 {
135
136 //
137 // Files (workspace, blogs and portfolios)
138 //
139
140 $ilDB->manipulate("DELETE FROM il_disk_quota" .
141 " WHERE src_type = " . $ilDB->quote("file", "text"));
142
143 $quota_done = array();
144
145 // get all workspace files
146 $set = $ilDB->query("SELECT od.owner, od.obj_id" .
147 " FROM object_data od" .
148 " JOIN object_reference_ws ref ON (ref.obj_id = od.obj_id)" .
149 " JOIN tree_workspace t ON (t.child = ref.wsp_id)" .
150 " WHERE od.type = " . $ilDB->quote("file", "text") .
151 " AND t.tree = od.owner");
152 while ($row = $ilDB->fetchAssoc($set)) {
153 $id = $row["owner"] . "-" . $row["obj_id"];
154 if (!in_array($id, $quota_done)) {
155 $this->quotaHandleFile($row["obj_id"], $row["owner"]);
156 $quota_done[] = $id;
157 }
158 }
159
160 // get all file usage for workspace blogs
161 $set = $ilDB->query("SELECT od.owner, fu.id" .
162 " FROM object_data od" .
163 " JOIN object_reference_ws ref ON (ref.obj_id = od.obj_id)" .
164 " JOIN tree_workspace t ON (t.child = ref.wsp_id)" .
165 " JOIN il_blog_posting blp ON (blp.blog_id = od.obj_id)" .
166 " JOIN file_usage fu ON (fu.usage_id = blp.id)" .
167 " WHERE fu.usage_type = " . $ilDB->quote("blp:pg", "text") .
168 " AND fu.usage_hist_nr = " . $ilDB->quote(0, "integer"));
169 while ($row = $ilDB->fetchAssoc($set)) {
170 $id = $row["owner"] . "-" . $row["id"];
171 if (!in_array($id, $quota_done)) {
172 $this->quotaHandleFile($row["id"], $row["owner"]);
173 $quota_done[] = $id;
174 }
175 }
176
177 // get all file usage for portfolios
178 $set = $ilDB->query($q = "SELECT od.owner, fu.id" .
179 " FROM object_data od" .
180 " JOIN usr_portfolio_page prtf ON (prtf.portfolio_id = od.obj_id)" .
181 " JOIN file_usage fu ON (fu.usage_id = prtf.id)" .
182 " WHERE fu.usage_type = " . $ilDB->quote("prtf:pg", "text") .
183 " AND fu.usage_hist_nr = " . $ilDB->quote(0, "integer"));
184
185 while ($row = $ilDB->fetchAssoc($set)) {
186 $id = $row["owner"] . "-" . $row["id"];
187 if (!in_array($id, $quota_done)) {
188 $this->quotaHandleFile($row["id"], $row["owner"]);
189 $quota_done[] = $id;
190 }
191 }
192
193
194 //
195 // Media objects (blogs and portfolios)
196 //
197
198 $ilDB->manipulate("DELETE FROM il_disk_quota" .
199 " WHERE src_type = " . $ilDB->quote("mob", "text"));
200
201 $quota_done = array();
202
203 // get all mob usage for workspace blogs
204 $set = $ilDB->query("SELECT od.owner, mu.id" .
205 " FROM object_data od" .
206 " JOIN object_reference_ws ref ON (ref.obj_id = od.obj_id)" .
207 " JOIN tree_workspace t ON (t.child = ref.wsp_id)" .
208 " JOIN il_blog_posting blp ON (blp.blog_id = od.obj_id)" .
209 " JOIN mob_usage mu ON (mu.usage_id = blp.id)" .
210 " WHERE mu.usage_type = " . $ilDB->quote("blp:pg", "text") .
211 " AND mu.usage_hist_nr = " . $ilDB->quote(0, "integer"));
212 while ($row = $ilDB->fetchAssoc($set)) {
213 $id = $row["owner"] . "-" . $row["id"];
214 if (!in_array($id, $quota_done)) {
215 $this->quotaHandleMob($row["id"], $row["owner"]);
216 $quota_done[] = $id;
217 }
218 }
219
220 // get all mob usage for portfolios
221 $set = $ilDB->query("SELECT od.owner, mu.id" .
222 " FROM object_data od" .
223 " JOIN usr_portfolio_page prtf ON (prtf.portfolio_id = od.obj_id)" .
224 " JOIN mob_usage mu ON (mu.usage_id = prtf.id)" .
225 " WHERE mu.usage_type = " . $ilDB->quote("prtf:pg", "text") .
226 " AND mu.usage_hist_nr = " . $ilDB->quote(0, "integer"));
227 while ($row = $ilDB->fetchAssoc($set)) {
228 $id = $row["owner"] . "-" . $row["id"];
229 if (!in_array($id, $quota_done)) {
230 $this->quotaHandleMob($row["id"], $row["owner"]);
231 $quota_done[] = $id;
232 }
233 }
234
235 //
236 // Portfolio / Blog images
237 //
238
239 $ilDB->manipulate("DELETE FROM il_disk_quota" .
240 " WHERE src_type = " . $ilDB->quote("prtf", "text"));
241 $ilDB->manipulate("DELETE FROM il_disk_quota" .
242 " WHERE src_type = " . $ilDB->quote("blog", "text"));
243
244 // portfolios
245 $set = $ilDB->query("SELECT od.owner, od.obj_id" .
246 " FROM object_data od" .
247 " JOIN usr_portfolio prtf ON (prtf.id = od.obj_id)" .
248 " WHERE od.type = " . $ilDB->quote("prtf", "text") .
249 " AND prtf.img IS NOT NULL");
250 while ($row = $ilDB->fetchAssoc($set)) {
251 $this->quotaHandleFileStorage("prtf", $row["obj_id"], $row["owner"], "sec/ilPortfolio");
252 }
253
254 // (workspace) blogs
255 $set = $ilDB->query("SELECT od.owner, od.obj_id" .
256 " FROM object_data od" .
257 " JOIN object_reference_ws ref ON (ref.obj_id = od.obj_id)" .
258 " JOIN tree_workspace t ON (t.child = ref.wsp_id)" .
259 " JOIN il_blog blog ON (blog.id = od.obj_id)" .
260 " WHERE od.type = " . $ilDB->quote("blog", "text") .
261 " AND blog.img IS NOT NULL" .
262 " AND t.tree = od.owner");
263 while ($row = $ilDB->fetchAssoc($set)) {
264 $this->quotaHandleFileStorage("blog", $row["obj_id"], $row["owner"], "sec/ilBlog");
265 }
266
267 return;
268
269 //
270 // Verifications
271 //
272
273 $ilDB->manipulate("DELETE FROM il_disk_quota" .
274 " WHERE src_type = " . $ilDB->quote("tstv", "text"));
275 $ilDB->manipulate("DELETE FROM il_disk_quota" .
276 " WHERE src_type = " . $ilDB->quote("excv", "text"));
277
278 // (workspace) verifications
279 $set = $ilDB->query("SELECT od.owner, od.obj_id, od.type" .
280 " FROM object_data od" .
281 " JOIN object_reference_ws ref ON (ref.obj_id = od.obj_id)" .
282 " JOIN tree_workspace t ON (t.child = ref.wsp_id)" .
283 " WHERE " . $ilDB->in("od.type", array("tstv", "excv"), "", "text") .
284 " AND t.tree = od.owner");
285 while ($row = $ilDB->fetchAssoc($set)) {
286 $this->quotaHandleVerification($row["type"], $row["obj_id"], $row["owner"]);
287 }
288 }
289
294 public function quotaHandleFile($a_obj_id, $a_owner_id)
295 {
297
298 // see ilFileSystemStorage::_createPathFromId()
299 $tpath = array();
300 $tfound = false;
301 $tnum = $a_obj_id;
302 for ($i = 3; $i > 0; $i--) {
303 $factor = pow(100, $i);
304 if (($tmp = (int) ($tnum / $factor)) or $tfound) {
305 $tpath[] = $tmp;
306 $tnum = $tnum % $factor;
307 $tfound = true;
308 }
309 }
310
311 $file_path = ilUtil::getDataDir() . "/ilFile/";
312 if (count($tpath)) {
313 $file_path .= (implode('/', $tpath) . '/');
314 }
315 $file_path .= "file_" . $a_obj_id;
316 if (file_exists($file_path)) {
317 $file_size = (int) ilUtil::dirsize($file_path);
318 if ($file_size > 0) {
319 $ilDB->manipulate("INSERT INTO il_disk_quota" .
320 " (owner_id, src_type, src_obj_id, src_size)" .
321 " VALUES (" . $ilDB->quote($a_owner_id, "integer") .
322 ", " . $ilDB->quote("file", "text") .
323 ", " . $ilDB->quote($a_obj_id, "integer") .
324 ", " . $ilDB->quote($file_size, "integer") . ")");
325 }
326 }
327 }
328
333 public function quotaHandleMob($a_obj_id, $a_owner_id)
334 {
336
337 $file_path = CLIENT_WEB_DIR . "/mobs/mm_" . $a_obj_id;
338 if (file_exists($file_path)) {
339 $file_size = (int) ilUtil::dirsize($file_path);
340 if ($file_size > 0) {
341 $ilDB->manipulate("INSERT INTO il_disk_quota" .
342 " (owner_id, src_type, src_obj_id, src_size)" .
343 " VALUES (" . $ilDB->quote($a_owner_id, "integer") .
344 ", " . $ilDB->quote("mob", "text") .
345 ", " . $ilDB->quote($a_obj_id, "integer") .
346 ", " . $ilDB->quote($file_size, "integer") . ")");
347 }
348 }
349 }
350
357 public function quotaHandleFileStorage($a_type, $a_obj_id, $a_owner_id, $a_dir)
358 {
360
361 // see ilFileSystemStorage::_createPathFromId()
362 $tpath = array();
363 $tfound = false;
364 $tnum = $a_obj_id;
365 for ($i = 3; $i > 0; $i--) {
366 $factor = pow(100, $i);
367 if (($tmp = (int) ($tnum / $factor)) or $tfound) {
368 $tpath[] = $tmp;
369 $tnum = $tnum % $factor;
370 $tfound = true;
371 }
372 }
373
374 $file_path = CLIENT_WEB_DIR . "/" . $a_dir . "/";
375 if (count($tpath)) {
376 $file_path .= (implode('/', $tpath) . '/');
377 }
378 $file_path .= $a_type . "_" . $a_obj_id;
379
380 if (file_exists($file_path)) {
381 $file_size = (int) ilUtil::dirsize($file_path);
382 if ($file_size > 0) {
383 $ilDB->manipulate("INSERT INTO il_disk_quota" .
384 " (owner_id, src_type, src_obj_id, src_size)" .
385 " VALUES (" . $ilDB->quote($a_owner_id, "integer") .
386 ", " . $ilDB->quote($a_type, "text") .
387 ", " . $ilDB->quote($a_obj_id, "integer") .
388 ", " . $ilDB->quote($file_size, "integer") . ")");
389 }
390 }
391 }
392
398 public function quotaHandleVerification($a_type, $a_obj_id, $a_owner_id)
399 {
401
402 // see ilFileSystemStorage::_createPathFromId()
403 $tpath = array();
404 $tfound = false;
405 $tnum = $a_obj_id;
406 for ($i = 3; $i > 0;$i--) {
407 $factor = pow(100, $i);
408 if (($tmp = (int) ($tnum / $factor)) or $tfound) {
409 $tpath[] = $tmp;
410 $tnum = $tnum % $factor;
411 $tfound = true;
412 }
413 }
414
415 $file_path = ilUtil::getDataDir() . "/ilVerification/";
416 if (count($tpath)) {
417 $file_path .= (implode('/', $tpath) . '/');
418 }
419 $file_path .= "vrfc_" . $a_obj_id;
420 if (file_exists($file_path)) {
421 $file_size = (int) ilUtil::dirsize($file_path);
422 if ($file_size > 0) {
423 $ilDB->manipulate("INSERT INTO il_disk_quota" .
424 " (owner_id, src_type, src_obj_id, src_size)" .
425 " VALUES (" . $ilDB->quote($a_owner_id, "integer") .
426 ", " . $ilDB->quote($a_type, "text") .
427 ", " . $ilDB->quote($a_obj_id, "integer") .
428 ", " . $ilDB->quote($file_size, "integer") . ")");
429 }
430 }
431 }
432}
$result
An exception for terminatinating execution or to throw for unit testing.
Cron job result data container.
Cron job application base class.
const SCHEDULE_TYPE_DAILY
hasCustomSettings()
Has cron job any custom setting which can be edited?boolean
hasAutoActivation()
Is to be activated on "installation".boolean
static getDataDir()
get data directory (outside webspace)
static dirsize($directory)
get size of a directory or a file.
$i
Definition: metadata.php:24
global $ilDB
$a_type
Definition: workflow.php:92
$DIC
Definition: xapitoken.php:46