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