ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjectDataCache.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
14 {
15  var $db = null;
16  var $reference_cache = array();
17  var $object_data_cache = array();
18  var $description_trans = array();
19 
20  function ilObjectDataCache()
21  {
22  global $ilDB;
23 
24  $this->db =& $ilDB;
25  }
26 
27  function deleteCachedEntry($a_obj_id)
28  {
29  unset($this->object_data_cache[$a_obj_id]);
30  }
31 
32  function lookupObjId($a_ref_id)
33  {
34  if(!$this->__isReferenceCached($a_ref_id))
35  {
36 //echo"-objidmissed-$a_ref_id-";
37  $obj_id = $this->__storeReference($a_ref_id);
38  $this->__storeObjectData($obj_id);
39  }
40  return (int) @$this->reference_cache[$a_ref_id];
41  }
42 
43  function lookupTitle($a_obj_id)
44  {
45  if(!$this->__isObjectCached($a_obj_id))
46  {
47  $this->__storeObjectData($a_obj_id);
48  }
49  return @$this->object_data_cache[$a_obj_id]['title'];
50  }
51 
52  function lookupType($a_obj_id)
53  {
54  if(!$this->__isObjectCached($a_obj_id))
55  {
56 //echo"-typemissed-$a_obj_id-";
57  $this->__storeObjectData($a_obj_id);
58  }
59  return @$this->object_data_cache[$a_obj_id]['type'];
60  }
61 
62  function lookupOwner($a_obj_id)
63  {
64  if(!$this->__isObjectCached($a_obj_id))
65  {
66  $this->__storeObjectData($a_obj_id);
67  }
68  return @$this->object_data_cache[$a_obj_id]['owner'];
69  }
70 
71  function lookupDescription($a_obj_id)
72  {
73  if(!$this->__isObjectCached($a_obj_id))
74  {
75  $this->__storeObjectData($a_obj_id);
76  }
77  return @$this->object_data_cache[$a_obj_id]['description'];
78  }
79 
80  function lookupLastUpdate($a_obj_id)
81  {
82  if(!$this->__isObjectCached($a_obj_id))
83  {
84  $this->__storeObjectData($a_obj_id);
85  }
86  return @$this->object_data_cache[$a_obj_id]['last_update'];
87  }
88  // PRIVATE
89 
97  function __isReferenceCached($a_ref_id)
98  {
99  #return false;
100  #static $cached = 0;
101  #static $not_cached = 0;
102 
103  if(@$this->reference_cache[$a_ref_id])
104  {
105  #echo "Reference ". ++$cached ."cached<br>";
106  return true;
107  }
108  #echo "Reference ". ++$not_cached ." not cached<br>";
109  return false;
110 
111  }
112 
120  function __isObjectCached($a_obj_id)
121  {
122  static $cached = 0;
123  static $not_cached = 0;
124 
125 
126  if(@$this->object_data_cache[$a_obj_id])
127  {
128  #echo "Object ". ++$cached ."cached<br>";
129  return true;
130  }
131  #echo "Object ". ++$not_cached ." not cached<br>";
132  return false;
133  }
134 
135 
145  function __storeReference($a_ref_id)
146  {
147  global $ilDB;
148 
149  $query = "SELECT obj_id FROM object_reference WHERE ref_id = ".$ilDB->quote($a_ref_id,'integer');
150  $res = $this->db->query($query);
151  while($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
152  {
153  $this->reference_cache[$a_ref_id] = $row['obj_id'];
154  }
155  return (int) @$this->reference_cache[$a_ref_id];
156  }
157 
165  function __storeObjectData($a_obj_id, $a_lang = "")
166  {
167  global $ilDB, $objDefinition, $ilUser;
168 
169  if (is_object($ilUser) && $a_lang == "")
170  {
171  $a_lang = $ilUser->getLanguage();
172  }
173 
174  $query = "SELECT * FROM object_data WHERE obj_id = ".
175  $ilDB->quote($a_obj_id ,'integer');
176  $res = $this->db->query($query);
177  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
178  {
179  $this->object_data_cache[$a_obj_id]['title'] = $row->title;
180  $this->object_data_cache[$a_obj_id]['description'] = $row->description;
181  $this->object_data_cache[$a_obj_id]['type'] = $row->type;
182  $this->object_data_cache[$a_obj_id]['owner'] = $row->owner;
183  $this->object_data_cache[$a_obj_id]['last_update'] = $row->last_update;
184 
185  if (is_object($objDefinition))
186  {
187  $translation_type = $objDefinition->getTranslationType($row->type);
188  }
189 
190  if ($translation_type == "db")
191  {
192  if (!$this->trans_loaded[$a_obj_id])
193  {
194  $q = "SELECT title,description FROM object_translation ".
195  "WHERE obj_id = ".$ilDB->quote($a_obj_id,'integer')." ".
196  "AND lang_code = ".$ilDB->quote($a_lang,'text')." ".
197  "AND NOT lang_default = 1";
198  $r = $ilDB->query($q);
199 
200  $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
201  if ($row)
202  {
203  $this->object_data_cache[$a_obj_id]['title'] = $row->title;
204  $this->object_data_cache[$a_obj_id]['description'] = $row->description;
205  $this->description_trans[] = $a_obj_id;
206  }
207  $this->trans_loaded[$a_obj_id] = true;
208  }
209  }
210  }
211 
212  return true;
213  }
214 
215  function isTranslatedDescription($a_obj_id)
216  {
217  return (is_array($this->description_trans) &&
218  in_array($a_obj_id, $this->description_trans));
219  }
220 
228  function preloadObjectCache($a_obj_ids, $a_lang = "")
229  {
230  global $ilDB, $objDefinition, $ilUser, $tree;
231 
232  if (is_object($ilUser) && $a_lang == "")
233  {
234  $a_lang = $ilUser->getLanguage();
235  }
236 //echo "<br>-preloading-"; var_dump($a_obj_ids);
237  if (!is_array($a_obj_ids)) return;
238  if (count($a_obj_ids) == 0) return;
239 
240 
241  $query = "SELECT * FROM object_data ".
242  "WHERE ".$ilDB->in('obj_id',$a_obj_ids,false,'integer');
243  $res = $ilDB->query($query);
244  $db_trans = array();
245  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
246  {
247 //echo "<br>store_obj-".$row->obj_id."-".$row->type."-".$row->title."-";
248 
249  // this if fixes #9960
250  if (!$this->trans_loaded[$row->obj_id])
251  {
252  $this->object_data_cache[$row->obj_id]['title'] = $row->title;
253  $this->object_data_cache[$row->obj_id]['description'] = $row->description;
254  }
255  $this->object_data_cache[$row->obj_id]['type'] = $row->type;
256  $this->object_data_cache[$row->obj_id]['owner'] = $row->owner;
257  $this->object_data_cache[$row->obj_id]['last_update'] = $row->last_update;
258 
259  if (is_object($objDefinition))
260  {
261  $translation_type = $objDefinition->getTranslationType($row->type);
262  }
263 
264  if ($translation_type == "db")
265  {
266  $db_trans[$row->obj_id] = $row->obj_id;
267  }
268  }
269  if (count($db_trans) > 0)
270  {
271  $this->preloadTranslations($db_trans, $a_lang);
272  }
273  }
274 
280  function preloadTranslations($a_obj_ids, $a_lang)
281  {
282  global $ilDB, $tree;
283 
284  $obj_ids = array();
285  foreach ($a_obj_ids as $id)
286  {
287  // do not load an id more than one time
288  if (!$this->trans_loaded[$id])
289  {
290  $obj_ids[] = $id;
291  $this->trans_loaded[$id] = true;
292  }
293  }
294  if (count($obj_ids) > 0)
295  {
296  $q = "SELECT obj_id, title, description FROM object_translation ".
297  "WHERE ".$ilDB->in('obj_id', $obj_ids, false, 'integer')." ".
298  "AND lang_code = ".$ilDB->quote($a_lang, 'text')." ".
299  "AND NOT lang_default = 1";
300  $r = $ilDB->query($q);
301  while ($row2 = $r->fetchRow(DB_FETCHMODE_OBJECT))
302  {
303  $this->object_data_cache[$row2->obj_id]['title'] = $row2->title;
304  $this->object_data_cache[$row2->obj_id]['description'] = $row2->description;
305  $this->description_trans[] = $row2->obj_id;
306  }
307  }
308  }
309 
310  function preloadReferenceCache($a_ref_ids, $a_incl_obj = true)
311  {
312  global $ilDB;
313 
314  if (!is_array($a_ref_ids)) return;
315  if (count($a_ref_ids) == 0) return;
316 
317  $query = "SELECT ref_id, obj_id FROM object_reference ".
318  "WHERE ".$ilDB->in('ref_id',$a_ref_ids,false,'integer');
319  $res = $ilDB->query($query);
320 
321  $obj_ids = array();
322  while($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
323  {
324  $this->reference_cache[$row['ref_id']] = $row['obj_id'];
325 //echo "<br>store_ref-".$row['ref_id']."-".$row['obj_id']."-";
326  $obj_ids[] = $row['obj_id'];
327  }
328  if ($a_incl_obj)
329  {
330  $this->preloadObjectCache($obj_ids);
331  }
332  }
333 
334 }
335 ?>