ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilSystemCheckTrash.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
24 {
25  public const MODE_TRASH_RESTORE = 1;
26  public const MODE_TRASH_REMOVE = 2;
27 
28  private int $limit_number = 0;
30  private array $limit_types = array();
31  private int $mode;
32 
33  protected ilLogger $logger;
34  protected ilDBInterface $db;
35  protected ilTree $tree;
36  protected ilRbacAdmin $admin;
37 
38  public function __construct()
39  {
40  global $DIC;
41 
42  $this->logger = $DIC->logger()->sysc();
43  $this->db = $DIC->database();
44  $this->tree = $DIC->repositoryTree();
45  $this->admin = $DIC->rbac()->admin();
46 
47  $this->limit_age = new ilDate(0, IL_CAL_UNIX);
48  }
49 
50  public function setNumberLimit(int $a_limit): void
51  {
52  $this->limit_number = $a_limit;
53  }
54 
55  public function getNumberLimit(): int
56  {
57  return $this->limit_number;
58  }
59 
60  public function setAgeLimit(ilDateTime $dt): void
61  {
62  $this->limit_age = $dt;
63  }
64 
65  public function getAgeLimit(): ilDateTime
66  {
67  return $this->limit_age;
68  }
69 
70  public function setTypesLimit(array $a_types): void
71  {
72  $this->limit_types = $a_types;
73  }
74 
75  public function getTypesLimit(): array
76  {
77  return $this->limit_types;
78  }
79 
80  public function setMode(int $a_mode): void
81  {
82  $this->mode = $a_mode;
83  }
84 
85  public function getMode(): int
86  {
87  return $this->mode;
88  }
89 
90  public function start(): bool
91  {
92  $this->logger->info('Handling delete');
93  switch ($this->getMode()) {
94  case self::MODE_TRASH_RESTORE:
95  $this->logger->info('Restore trash to recovery folder');
96  $this->restore();
97  return true;
98  break;
99 
100  case self::MODE_TRASH_REMOVE:
101  $this->logger->info('Remove selected from system.');
102  $this->logger->info('Type limit: ' . print_r($this->getTypesLimit(), true));
103  $this->logger->info('Age limit: ' . $this->getAgeLimit());
104  $this->logger->info('Number limit: ' . $this->getNumberLimit());
105  $this->removeSelectedFromSystem();
106  return true;
107  break;
108  }
109  return false;
110  }
111 
112  protected function restore(): void
113  {
114  $deleted = $this->readDeleted();
115 
116  $this->logger->info('Found deleted : ' . print_r($deleted, true));
117 
118 
119  foreach ($deleted as $tmp_num => $deleted_info) {
120  $child_id = (int) ($deleted_info['child'] ?? 0);
121  $ref_obj = ilObjectFactory::getInstanceByRefId($child_id, false);
122  if (!$ref_obj instanceof ilObject) {
123  continue;
124  }
125 
126  $this->tree->deleteNode((int) ($deleted_info['tree'] ?? 0), $child_id);
127  $this->logger->info('Object tree entry deleted');
128 
129  if ($ref_obj->getType() !== 'rolf') {
130  $this->admin->revokePermission($child_id);
131  $ref_obj->putInTree(RECOVERY_FOLDER_ID);
132  $ref_obj->setPermissions(RECOVERY_FOLDER_ID);
133  $this->logger->info('Object moved to recovery folder');
134  }
135  }
136  }
137 
138  protected function removeSelectedFromSystem(): void
139  {
140  $deleted = $this->readSelectedDeleted();
141  foreach ($deleted as $del_num => $deleted_info) {
142  $sub_nodes = $this->readDeleted((int) ($deleted_info['tree'] ?? 0));
143 
144  foreach ($sub_nodes as $sub_num => $subnode_info) {
145  $ref_obj = ilObjectFactory::getInstanceByRefId((int) ($subnode_info['child'] ?? 0), false);
146  if (!$ref_obj instanceof ilObject) {
147  continue;
148  }
149 
150  $ref_obj->delete();
151  ilTree::_removeEntry((int) ($subnode_info['tree'] ?? 0), (int) ($subnode_info['child'] ?? 0));
152  }
153  }
154  }
155 
156  protected function readSelectedDeleted(): array
157  {
158  $and_types = '';
159  $this->logger->dump($this->getTypesLimit());
160 
161  $types = array();
162  foreach ($this->getTypesLimit() as $id => $type) {
163  if ($type) {
164  $types[] = $type;
165  }
166  }
167  if (count($types)) {
168  $and_types = 'AND ' . $this->db->in('o.type', $this->getTypesLimit(), false, ilDBConstants::T_TEXT) . ' ';
169  }
170 
171  $and_age = '';
172  $age_limit = $this->getAgeLimit()->get(IL_CAL_UNIX);
173  if ($age_limit > 0) {
174  $and_age = 'AND r.deleted < ' . $this->db->quote(
175  $this->getAgeLimit()->get(IL_CAL_DATETIME),
177  ) . ' ';
178  }
179  $limit = '';
180  if ($this->getNumberLimit()) {
181  $limit = 'LIMIT ' . $this->getNumberLimit();
182  }
183 
184  $query = 'SELECT child,tree FROM tree t JOIN object_reference r ON child = r.ref_id ' .
185  'JOIN object_data o on r.obj_id = o.obj_id ' .
186  'WHERE tree < ' . $this->db->quote(0, ilDBConstants::T_INTEGER) . ' ' .
187  'AND child = -tree ';
188 
189  $query .= $and_age;
190  $query .= $and_types;
191  $query .= 'ORDER BY depth desc ';
192  $query .= $limit;
193 
194  $this->logger->info($query);
195 
196  $deleted = array();
197  $res = $this->db->query($query);
198  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
199  $deleted[] = array(
200  'tree' => $row->tree,
201  'child' => $row->child
202  );
203  }
204  return $deleted;
205  }
206 
207  protected function readDeleted(?int $tree_id = null): array
208  {
209  $query = 'SELECT child,tree FROM tree t JOIN object_reference r ON child = r.ref_id ' .
210  'JOIN object_data o on r.obj_id = o.obj_id ';
211 
212  if ($tree_id === null) {
213  $query .= 'WHERE tree < ' . $this->db->quote(0, 'integer') . ' ';
214  } else {
215  $query .= 'WHERE tree = ' . $this->db->quote($tree_id, 'integer') . ' ';
216  }
217  $query .= 'ORDER BY depth desc';
218 
219  $res = $this->db->query($query);
220 
221  $deleted = array();
222  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
223  $deleted[] = array(
224  'tree' => $row->tree,
225  'child' => $row->child
226  );
227  }
228  return $deleted;
229  }
230 }
$res
Definition: ltiservices.php:69
const IL_CAL_DATETIME
$type
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const IL_CAL_UNIX
global $DIC
Definition: feed.php:28
static getInstanceByRefId(int $ref_id, bool $stop_on_error=true)
get an instance of an Ilias object by reference id
$query
readDeleted(?int $tree_id=null)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
const RECOVERY_FOLDER_ID
Definition: constants.php:37
Class ilRbacAdmin Core functions for role based access control.
static _removeEntry(int $a_tree, int $a_child, string $a_db_table="tree")
STATIC METHOD Removes a single entry from a tree.