ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ilCachedObjectDefinition.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23
28{
29 protected static ?ilCachedObjectDefinition $instance = null;
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 $this->cached_results = $data['cached_results'];
76 }
77
78 protected function getRawData(): array
79 {
80 return [
81 'il_object_def' => $this->il_object_def,
82 'subobj_for_parent' => $this->subobj_for_parent,
83 'grouped_rep_obj_types' => $this->grouped_rep_obj_types,
84 'il_object_group' => $this->il_object_group,
85 'il_object_sub_type' => $this->il_object_sub_type,
86 'cached_results' => $this->cached_results,
87 ];
88 }
89
90 protected function saveToCache(): void
91 {
92 $this->cache->set('data', $this->getRawData());
93 }
94
95 protected function readFromCache(): void
96 {
97 // This is a workaround for the fact that transformatuin cannot be created by
98 // $DIC->refinery()->xy() since we are in a hell of dependencies. E.g. we cant instantiate the
99 // caching service with $DIC->refinery() since the Refinery needs ilLanguage, but ilLanguage
100 // needs the caching service and so on...
101 $cached_results = $this->cache->get('data', new Transformation(function ($data) {
102 return $data;
103 }));
104 if (is_array($cached_results)) {
105 $this->loadFromRawData($cached_results);
106 } else {
107 $this->readFromDB();
108 }
109 }
110
111 protected function readFromDB(): void
112 {
113 global $DIC;
114 $db = $DIC->database();
115
116 $data = [];
117
118 $sql =
119 "SELECT id, class_name, component, location, checkbox, inherit, translate, devmode, allow_link," . PHP_EOL
120 . "allow_copy, rbac, `system`, sideblock, default_pos, grp, default_pres_pos, `export`, repository," . PHP_EOL
121 . "workspace, administration, amet, orgunit_permissions, lti_provider, offline_handling" . PHP_EOL
122 . "FROM il_object_def" . PHP_EOL;
123 $set = $db->query($sql);
124 while ($rec = $db->fetchAssoc($set)) {
125 $data['il_object_def'][$rec['id']] = $rec;
126 }
127
128 $sql =
129 "SELECT parent, subobj, mmax" . PHP_EOL
130 . "FROM il_object_subobj" . PHP_EOL;
131 $set = $db->query($sql);
132 while ($rec = $db->fetchAssoc($set)) {
133 $parent = $rec['parent'];
134 $data['subobj_for_parent'][$parent][] = $rec;
135 }
136
137 $sql =
138 "SELECT DISTINCT(id) AS sid, parent, id, class_name, component, location, checkbox, inherit," . PHP_EOL
139 . "translate, devmode, allow_link, allow_copy, rbac, `system`, sideblock, default_pos, grp," . PHP_EOL
140 . "default_pres_pos, `export`, repository, workspace, administration, amet, orgunit_permissions," . PHP_EOL
141 . "lti_provider, offline_handling" . PHP_EOL
142 . "FROM il_object_def, il_object_subobj" . PHP_EOL
143 . "WHERE NOT (" . $db->quoteIdentifier('system') . " = 1)" . PHP_EOL
144 . "AND NOT (sideblock = 1)" . PHP_EOL
145 . "AND subobj = id" . PHP_EOL;
146 $set = $db->query($sql);
147 while ($rec = $db->fetchAssoc($set)) {
148 $data['grouped_rep_obj_types'][$rec['parent']][] = $rec;
149 }
150
151 $sql =
152 "SELECT id, name, default_pres_pos" . PHP_EOL
153 . "FROM il_object_group" . PHP_EOL;
154 $set = $db->query($sql);
155 while ($rec = $db->fetchAssoc($set)) {
156 $data['il_object_group'][$rec['id']] = $rec;
157 }
158
159 $sql =
160 "SELECT obj_type, sub_type, amet" . PHP_EOL
161 . "FROM il_object_sub_type" . PHP_EOL;
162 $set = $db->query($sql);
163 while ($rec = $db->fetchAssoc($set)) {
164 $data['il_object_sub_type'][$rec['obj_type']][] = $rec;
165 }
166 $data['cached_results'] = [];
167 $this->loadFromRawData($data);
168 }
169
170 public function getIlObjectDef(): array
171 {
173 }
174
175 public function getIlObjectGroup(): array
176 {
178 }
179
180 public function getIlObjectSubType(): array
181 {
183 }
184
185 public static function getInstance(): ilCachedObjectDefinition
186 {
187 if (!isset(self::$instance)) {
188 return self::$instance = new self();
189 }
190
191 return self::$instance;
192 }
193
194
200 public function lookupSubObjForParent($parent)
201 {
202 if (is_array($parent)) {
203 $index = md5(serialize($parent));
204 if (isset($this->cached_results['subop_par'][$index])) {
205 return $this->cached_results['subop_par'][$index];
206 }
207
208 $return = [];
209 foreach ($parent as $p) {
210 if (isset($this->subobj_for_parent[$p]) && is_array($this->subobj_for_parent[$p])) {
211 foreach ($this->subobj_for_parent[$p] as $rec) {
212 $return[] = $rec;
213 }
214 }
215 }
216
217 $this->cached_results['subop_par'][$index] = $return;
218 $this->changed = true;
219
220 return $return;
221 }
222
223 return $this->subobj_for_parent[$parent];
224 }
225
226 public function __destruct()
227 {
228 if ($this->changed) {
229 $this->changed = false;
230 $this->cache->set('data', $this->getRawData());
231 }
232 }
233
239 public function lookupGroupedRepObj($parent)
240 {
241 if (is_array($parent)) {
242 $index = md5(serialize($parent));
243 if (isset($this->cached_results['grpd_repo'][$index])) {
244 return $this->cached_results['grpd_repo'][$index];
245 }
246
247 $return = [];
248 $sids = [];
249 foreach ($parent as $p) {
250 $s = $this->grouped_rep_obj_types[$p];
251 foreach ($s as $child) {
252 if (!in_array($child['sid'], $sids)) {
253 $sids[] = $child['sid'];
254 $return[] = $child;
255 }
256 }
257 }
258 $this->changed = true;
259 $this->cached_results['grpd_repo'][$index] = $return;
260
261 return $return;
262 } else {
263 return $this->grouped_rep_obj_types[$parent] ?? null;
264 }
265 }
266}
Transform values according to custom configuration.
Cache for object definitions, based on ilGlobalCache.
ILIAS Cache Container Container $cache
static ilCachedObjectDefinition $instance
global $DIC
Definition: shib_login.php:26