ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilObjectDataCache.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
30{
31 protected ilDBInterface $db;
32
34 protected array $trans_loaded = [];
35 protected array $reference_cache = [];
36 protected array $object_data_cache = [];
37 protected array $description_trans = [];
38
39 public function __construct()
40 {
41 global $DIC;
42 $this->db = $DIC->database();
43 }
44
45 public function deleteCachedEntry(int $obj_id): void
46 {
47 if (isset($this->object_data_cache[$obj_id])) {
48 unset($this->object_data_cache[$obj_id]);
49 }
50 }
51
52 public function lookupObjId(int $ref_id): int
53 {
54 if (!$this->__isReferenceCached($ref_id)) {
55 $obj_id = $this->__storeReference($ref_id);
56 $this->__storeObjectData($obj_id);
57 }
58
59 return (int) ($this->reference_cache[$ref_id] ?? 0);
60 }
61
62 public function lookupTitle(int $obj_id): string
63 {
64 if (!$this->__isObjectCached($obj_id)) {
65 $this->__storeObjectData($obj_id);
66 }
67
68 return (string) ($this->object_data_cache[$obj_id]['title'] ?? '');
69 }
70
71 public function lookupType(int $obj_id): string
72 {
73 if (!$this->__isObjectCached($obj_id)) {
74 $this->__storeObjectData($obj_id);
75 }
76
77 return (string) ($this->object_data_cache[$obj_id]['type'] ?? '');
78 }
79
80 public function lookupOwner(int $obj_id): int
81 {
82 if (!$this->__isObjectCached($obj_id)) {
83 $this->__storeObjectData($obj_id);
84 }
85
86 return (int) ($this->object_data_cache[$obj_id]['owner'] ?? 0);
87 }
88
89 public function lookupDescription(int $obj_id): string
90 {
91 if (!$this->__isObjectCached($obj_id)) {
92 $this->__storeObjectData($obj_id);
93 }
94
95 return (string) ($this->object_data_cache[$obj_id]['description'] ?? '');
96 }
97
98 public function lookupLastUpdate(int $obj_id): string
99 {
100 if (!$this->__isObjectCached($obj_id)) {
101 $this->__storeObjectData($obj_id);
102 }
103 return (string) ($this->object_data_cache[$obj_id]['last_update']);
104 }
105
109 public function lookupOfflineStatus(int $obj_id): bool
110 {
111 if (!$this->__isObjectCached($obj_id)) {
112 $this->__storeObjectData($obj_id);
113 }
114
115 return (bool) ($this->object_data_cache[$obj_id]['offline'] ?? false);
116 }
117
118 // PRIVATE
119
123 private function __isReferenceCached(int $ref_id): bool
124 {
125 if (isset($this->reference_cache[$ref_id])) {
126 return true;
127 }
128
129 return false;
130 }
131
135 private function __isObjectCached(int $obj_id): bool
136 {
137 if (isset($this->object_data_cache[$obj_id])) {
138 return true;
139 }
140
141 return false;
142 }
143
149 private function __storeReference(int $ref_id): int
150 {
151 $sql =
152 "SELECT obj_id" . PHP_EOL
153 . "FROM object_reference" . PHP_EOL
154 . "WHERE ref_id = " . $this->db->quote($ref_id, 'integer') . PHP_EOL
155 ;
156 $result = $this->db->query($sql);
157 while ($row = $result->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
158 $this->reference_cache[$ref_id] = (int) $row['obj_id'];
159 }
160
161 return (int) ($this->reference_cache[$ref_id] ?? 0);
162 }
163
167 private function __storeObjectData(int $obj_id): void
168 {
169 global $DIC;
170
171 $obj_definition = $DIC["objDefinition"];
172 $user = $DIC["ilUser"];
173
174 $sql =
175 "SELECT object_data.obj_id, object_data.type, object_data.title, object_data.description, " . PHP_EOL
176 . "object_data.owner, object_data.create_date, object_data.last_update, object_data.import_id, " . PHP_EOL
177 . "object_data.offline, object_description.description as long_description " . PHP_EOL
178 . "FROM object_data LEFT JOIN object_description ON object_data.obj_id = object_description.obj_id " . PHP_EOL
179 . "WHERE object_data.obj_id = " . $this->db->quote($obj_id, 'integer') . PHP_EOL
180 ;
181 $res = $this->db->query($sql);
182 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
183 $this->object_data_cache[$obj_id]['title'] = $row->title;
184 $this->object_data_cache[$obj_id]['description'] = $row->description;
185 if ($row->long_description !== null) {
186 $this->object_data_cache[$row->obj_id]['description'] = $row->long_description;
187 }
188 $this->object_data_cache[$obj_id]['type'] = $row->type;
189 $this->object_data_cache[$obj_id]['owner'] = $row->owner;
190 $this->object_data_cache[$obj_id]['last_update'] = $row->last_update;
191 $this->object_data_cache[$obj_id]['offline'] = $row->offline;
192
193 $translation_type = $obj_definition->getTranslationType($row->type);
194
195 if ($translation_type === "db" && !isset($this->trans_loaded[$obj_id])) {
196 $sql =
197 "SELECT title, description" . PHP_EOL
198 . "FROM object_translation" . PHP_EOL
199 . "WHERE obj_id = " . $this->db->quote($obj_id, 'integer') . PHP_EOL
200 . "AND lang_code = " . $this->db->quote($user->getLanguage(), 'text') . PHP_EOL
201 ;
202 $trans_res = $this->db->query($sql);
203
204 $trans_row = $trans_res->fetchRow(ilDBConstants::FETCHMODE_OBJECT);
205 if ($trans_row) {
206 $this->object_data_cache[$obj_id]['title'] = $trans_row->title;
207 $this->object_data_cache[$obj_id]['description'] = $trans_row->description;
208 $this->description_trans[] = $obj_id;
209 }
210 $this->trans_loaded[$obj_id] = true;
211 }
212 }
213 }
214
215 public function isTranslatedDescription(int $obj_id): bool
216 {
217 return in_array($obj_id, $this->description_trans);
218 }
219
225 public function preloadObjectCache(array $obj_ids, string $lang = ''): void
226 {
227 global $DIC;
228
229 $obj_definition = $DIC["objDefinition"];
230 $user = $DIC["ilUser"];
231
232 if ($lang == "") {
233 $lang = $user->getLanguage();
234 }
235
236 if ($obj_ids === []) {
237 return;
238 }
239
240 $sql =
241 "SELECT object_data.obj_id, object_data.type, object_data.title, object_data.description, " . PHP_EOL
242 . "object_data.owner, object_data.create_date, object_data.last_update, object_data.import_id, " . PHP_EOL
243 . "object_data.offline, object_description.description as long_description " . PHP_EOL
244 . "FROM object_data LEFT JOIN object_description ON object_data.obj_id = object_description.obj_id " . PHP_EOL
245 . "WHERE " . $this->db->in('object_data.obj_id', $obj_ids, false, 'integer') . PHP_EOL
246 ;
247 $res = $this->db->query($sql);
248 $db_trans = [];
249 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
250 $obj_id = (int) $row->obj_id;
251
252 if (!isset($this->trans_loaded[$obj_id])) {
253 $this->object_data_cache[$obj_id]['title'] = $row->title;
254 $this->object_data_cache[$obj_id]['description'] = $row->description;
255 if ($row->long_description !== null) {
256 $this->object_data_cache[$row->obj_id]['description'] = $row->long_description;
257 }
258 }
259 $this->object_data_cache[$obj_id]['type'] = $row->type;
260 $this->object_data_cache[$obj_id]['owner'] = $row->owner;
261 $this->object_data_cache[$obj_id]['last_update'] = $row->last_update;
262 $this->object_data_cache[$obj_id]['offline'] = $row->offline;
263
264 $translation_type = $obj_definition->getTranslationType($row->type);
265
266 if ($translation_type === "db") {
267 $db_trans[$obj_id] = $obj_id;
268 }
269 }
270
271 if (count($db_trans) > 0) {
272 $this->preloadTranslations($db_trans, $lang);
273 }
274 }
275
281 public function preloadTranslations(array $obj_ids, string $lang): void
282 {
283 $ids = [];
284 foreach ($obj_ids as $id) {
285 // do not load an id more than one time
286 if (!isset($this->trans_loaded[$id])) {
287 $ids[] = $id;
288 $this->trans_loaded[$id] = true;
289 }
290 }
291
292 if ($ids !== []) {
293 $sql =
294 "SELECT obj_id, title, description" . PHP_EOL
295 . "FROM object_translation" . PHP_EOL
296 . "WHERE " . $this->db->in('obj_id', $ids, false, 'integer') . PHP_EOL
297 . "AND lang_code = " . $this->db->quote($lang, 'text') . PHP_EOL
298 ;
299 $result = $this->db->query($sql);
300 while ($row = $result->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
301 $obj_id = (int) $row->obj_id;
302
303 $this->object_data_cache[$obj_id]['title'] = $row->title;
304 $this->object_data_cache[$obj_id]['description'] = $row->description;
305 $this->description_trans[] = $obj_id;
306 }
307 }
308 }
309
314 public function preloadReferenceCache(array $ref_ids, bool $incl_obj = true): void
315 {
316 if ($ref_ids === []) {
317 return;
318 }
319
320 $sql =
321 "SELECT ref_id, obj_id" . PHP_EOL
322 . "FROM object_reference" . PHP_EOL
323 . "WHERE " . $this->db->in('ref_id', $ref_ids, false, 'integer') . PHP_EOL
324 ;
325 $res = $this->db->query($sql);
326
327 $obj_ids = [];
328 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
329 $this->reference_cache[(int) $row['ref_id']] = (int) $row['obj_id'];
330 $obj_ids[] = (int) $row['obj_id'];
331 }
332
333 if ($incl_obj) {
334 $this->preloadObjectCache($obj_ids);
335 }
336 }
337}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
class ilObjectDataCache
__storeReference(int $ref_id)
Stores Reference in cache.
lookupOfflineStatus(int $obj_id)
Check if supports centralized offline handling and is offline.
__storeObjectData(int $obj_id)
Stores object data in cache.
__isReferenceCached(int $ref_id)
checks whether a reference id is already in cache or not
isTranslatedDescription(int $obj_id)
__isObjectCached(int $obj_id)
checks whether an object is already in cache or not
preloadReferenceCache(array $ref_ids, bool $incl_obj=true)
preloadObjectCache(array $obj_ids, string $lang='')
Stores object data in cache.
preloadTranslations(array $obj_ids, string $lang)
Preload translation information.
Interface ilDBInterface.
$ref_id
Definition: ltiauth.php:66
$res
Definition: ltiservices.php:69
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26
$lang
Definition: xapiexit.php:25