ILIAS  trunk Revision v11.0_alpha-1753-gb21ca8c4367
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilCachedObjectDefinition.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
28 {
30  private \ILIAS\Cache\Container\Container $cache;
31 
32  protected array $cached_results = [];
33  protected bool $changed = false;
34  protected array $il_object_def = [];
35  protected array $subobj_for_parent = [];
36  protected array $grouped_rep_obj_types = [];
37  protected array $il_object_group = [];
38  protected array $il_object_sub_type = [];
39 
40  protected function __construct()
41  {
42  global $DIC;
43  $this->cache = $DIC->globalCache()->get($this);
44  $this->init();
45  }
46 
47  public function getContainerKey(): string
48  {
49  return 'objdef';
50  }
51 
52 
53  public function isForced(): bool
54  {
55  return false;
56  }
57 
58  public function init(): void
59  {
60  if ($this->cache->has('data')) {
61  $this->readFromCache();
62  } else {
63  $this->readFromDB();
64  $this->saveToCache();
65  }
66  }
67 
68  protected function loadFromRawData(array $data): void
69  {
70  $this->il_object_def = $data['il_object_def'];
71  $this->subobj_for_parent = $data['subobj_for_parent'];
72  $this->grouped_rep_obj_types = $data['grouped_rep_obj_types'];
73  $this->il_object_group = $data['il_object_group'];
74  $this->il_object_sub_type = $data['il_object_sub_type'];
75  }
76 
77  protected function getRawData(): array
78  {
79  return [
80  'il_object_def' => $this->il_object_def,
81  'subobj_for_parent' => $this->subobj_for_parent,
82  'grouped_rep_obj_types' => $this->grouped_rep_obj_types,
83  'il_object_group' => $this->il_object_group,
84  'il_object_sub_type' => $this->il_object_sub_type,
85  ];
86  }
87 
88  protected function saveToCache(): void
89  {
90  $this->cache->set('data', $this->getRawData());
91  }
92 
93  protected function readFromCache(): void
94  {
95  // This is a workaround for the fact that transformatuin cannot be created by
96  // $DIC->refinery()->xy() since we are in a hell of dependencies. E.g. we cant instantiate the
97  // caching service with $DIC->refinery() since the Refinery needs ilLanguage, but ilLanguage
98  // needs the caching service and so on...
99  $cached_results = $this->cache->get('data', new Transformation(function ($data) {
100  return $data;
101  }));
102  if (is_array($cached_results)) {
103  $this->loadFromRawData($cached_results);
104  } else {
105  $this->readFromDB();
106  }
107  }
108 
109  protected function readFromDB(): void
110  {
111  global $DIC;
112  $db = $DIC->database();
113 
114  $data = [];
115 
116  $sql =
117  "SELECT id, class_name, component, location, checkbox, inherit, translate, devmode, allow_link," . PHP_EOL
118  . "allow_copy, rbac, `system`, sideblock, default_pos, grp, default_pres_pos, `export`, repository," . PHP_EOL
119  . "workspace, administration, amet, orgunit_permissions, lti_provider, offline_handling" . PHP_EOL
120  . "FROM il_object_def" . PHP_EOL;
121  $set = $db->query($sql);
122  while ($rec = $db->fetchAssoc($set)) {
123  $data['il_object_def'][$rec['id']] = $rec;
124  }
125 
126  $sql =
127  "SELECT parent, subobj, mmax" . PHP_EOL
128  . "FROM il_object_subobj" . PHP_EOL;
129  $set = $db->query($sql);
130  while ($rec = $db->fetchAssoc($set)) {
131  $parent = $rec['parent'];
132  $data['subobj_for_parent'][$parent][] = $rec;
133  }
134 
135  $sql =
136  "SELECT DISTINCT(id) AS sid, parent, id, class_name, component, location, checkbox, inherit," . PHP_EOL
137  . "translate, devmode, allow_link, allow_copy, rbac, `system`, sideblock, default_pos, grp," . PHP_EOL
138  . "default_pres_pos, `export`, repository, workspace, administration, amet, orgunit_permissions," . PHP_EOL
139  . "lti_provider, offline_handling" . PHP_EOL
140  . "FROM il_object_def, il_object_subobj" . PHP_EOL
141  . "WHERE NOT (" . $db->quoteIdentifier('system') . " = 1)" . PHP_EOL
142  . "AND NOT (sideblock = 1)" . PHP_EOL
143  . "AND subobj = id" . PHP_EOL;
144  $set = $db->query($sql);
145  while ($rec = $db->fetchAssoc($set)) {
146  $data['grouped_rep_obj_types'][$rec['parent']][] = $rec;
147  }
148 
149  $sql =
150  "SELECT id, name, default_pres_pos" . PHP_EOL
151  . "FROM il_object_group" . PHP_EOL;
152  $set = $db->query($sql);
153  while ($rec = $db->fetchAssoc($set)) {
154  $data['il_object_group'][$rec['id']] = $rec;
155  }
156 
157  $sql =
158  "SELECT obj_type, sub_type, amet" . PHP_EOL
159  . "FROM il_object_sub_type" . PHP_EOL;
160  $set = $db->query($sql);
161  while ($rec = $db->fetchAssoc($set)) {
162  $data['il_object_sub_type'][$rec['obj_type']][] = $rec;
163  }
164  $this->loadFromRawData($data);
165  }
166 
167  public function getIlObjectDef(): array
168  {
169  return $this->il_object_def;
170  }
171 
172  public function getIlObjectGroup(): array
173  {
174  return $this->il_object_group;
175  }
176 
177  public function getIlObjectSubType(): array
178  {
180  }
181 
182  public static function getInstance(): ilCachedObjectDefinition
183  {
184  if (!isset(self::$instance)) {
185  return self::$instance = new self();
186  }
187 
188  return self::$instance;
189  }
190 
191 
197  public function lookupSubObjForParent($parent)
198  {
199  if (is_array($parent)) {
200  $index = md5(serialize($parent));
201  if (isset($this->cached_results['subop_par'][$index])) {
202  return $this->cached_results['subop_par'][$index];
203  }
204 
205  $return = [];
206  foreach ($parent as $p) {
207  if (isset($this->subobj_for_parent[$p]) && is_array($this->subobj_for_parent[$p])) {
208  foreach ($this->subobj_for_parent[$p] as $rec) {
209  $return[] = $rec;
210  }
211  }
212  }
213 
214  $this->cached_results['subop_par'][$index] = $return;
215  $this->changed = true;
216 
217  return $return;
218  }
219 
220  return $this->subobj_for_parent[$parent];
221  }
222 
223  public function __destruct()
224  {
225  if ($this->changed) {
226  $this->changed = false;
227  $this->cache->set('data', $this->getRawData());
228  }
229  }
230 
236  public function lookupGroupedRepObj($parent)
237  {
238  if (is_array($parent)) {
239  $index = md5(serialize($parent));
240  if (isset($this->cached_results['grpd_repo'][$index])) {
241  return $this->cached_results['grpd_repo'][$index];
242  }
243 
244  $return = [];
245  $sids = [];
246  foreach ($parent as $p) {
247  $s = $this->grouped_rep_obj_types[$p];
248  foreach ($s as $child) {
249  if (!in_array($child['sid'], $sids)) {
250  $sids[] = $child['sid'];
251  $return[] = $child;
252  }
253  }
254  }
255  $this->changed = true;
256  $this->cached_results['grpd_repo'][$index] = $return;
257 
258  return $return;
259  } else {
260  return $this->grouped_rep_obj_types[$parent] ?? null;
261  }
262  }
263 }
Cache for object definitions, based on ilGlobalCache.
Transform values according to custom configuration.
static ilCachedObjectDefinition $instance
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
global $DIC
Definition: shib_login.php:22
ILIAS Cache Container Container $cache