ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjectDataCache.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
34 {
35  var $db = null;
36  var $reference_cache = array();
37  var $object_data_cache = array();
38 
39  function ilObjectDataCache()
40  {
41  global $ilDB;
42 
43  $this->db =& $ilDB;
44  }
45 
46  function deleteCachedEntry($a_obj_id)
47  {
48  unset($this->object_data_cache[$a_obj_id]);
49  }
50 
51  function lookupObjId($a_ref_id)
52  {
53  if(!$this->__isReferenceCached($a_ref_id))
54  {
55 //echo"-objidmissed-$a_ref_id-";
56  $obj_id = $this->__storeReference($a_ref_id);
57  $this->__storeObjectData($obj_id);
58  }
59  return (int) @$this->reference_cache[$a_ref_id];
60  }
61 
62  function lookupTitle($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]['title'];
69  }
70 
71  function lookupType($a_obj_id)
72  {
73  if(!$this->__isObjectCached($a_obj_id))
74  {
75 //echo"-typemissed-$a_obj_id-";
76  $this->__storeObjectData($a_obj_id);
77  }
78  return @$this->object_data_cache[$a_obj_id]['type'];
79  }
80 
81  function lookupOwner($a_obj_id)
82  {
83  if(!$this->__isObjectCached($a_obj_id))
84  {
85  $this->__storeObjectData($a_obj_id);
86  }
87  return @$this->object_data_cache[$a_obj_id]['owner'];
88  }
89 
90  function lookupDescription($a_obj_id)
91  {
92  if(!$this->__isObjectCached($a_obj_id))
93  {
94  $this->__storeObjectData($a_obj_id);
95  }
96  return @$this->object_data_cache[$a_obj_id]['description'];
97  }
98 
99  function lookupLastUpdate($a_obj_id)
100  {
101  if(!$this->__isObjectCached($a_obj_id))
102  {
103  $this->__storeObjectData($a_obj_id);
104  }
105  return @$this->object_data_cache[$a_obj_id]['last_update'];
106  }
107  // PRIVATE
108 
116  function __isReferenceCached($a_ref_id)
117  {
118  #return false;
119  #static $cached = 0;
120  #static $not_cached = 0;
121 
122  if(@$this->reference_cache[$a_ref_id])
123  {
124  #echo "Reference ". ++$cached ."cached<br>";
125  return true;
126  }
127  #echo "Reference ". ++$not_cached ." not cached<br>";
128  return false;
129 
130  }
131 
139  function __isObjectCached($a_obj_id)
140  {
141  static $cached = 0;
142  static $not_cached = 0;
143 
144 
145  if(@$this->object_data_cache[$a_obj_id])
146  {
147  #echo "Object ". ++$cached ."cached<br>";
148  return true;
149  }
150  #echo "Object ". ++$not_cached ." not cached<br>";
151  return false;
152  }
153 
154 
164  function __storeReference($a_ref_id)
165  {
166  global $ilDB;
167 
168  $query = "SELECT obj_id FROM object_reference WHERE ref_id = ".$ilDB->quote($a_ref_id);
169  $res = $this->db->query($query);
170  while($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
171  {
172  $this->reference_cache[$a_ref_id] = $row['obj_id'];
173  }
174  return (int) @$this->reference_cache[$a_ref_id];
175  }
176 
184  function __storeObjectData($a_obj_id, $a_lang = "")
185  {
186  global $ilDB, $objDefinition, $ilUser;
187 
188  if (is_object($ilUser) && $a_lang == "")
189  {
190  $a_lang = $ilUser->getLanguage();
191  }
192 
193  $query = "SELECT * FROM object_data WHERE obj_id = ".
194  $ilDB->quote($a_obj_id);
195  $res = $this->db->query($query);
196  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
197  {
198  $this->object_data_cache[$a_obj_id]['title'] = $row->title;
199  $this->object_data_cache[$a_obj_id]['description'] = $row->description;
200  $this->object_data_cache[$a_obj_id]['type'] = $row->type;
201  $this->object_data_cache[$a_obj_id]['owner'] = $row->owner;
202  $this->object_data_cache[$a_obj_id]['last_update'] = $row->last_update;
203 
204  //$ilBench->start("Tree", "fetchNodeData_readDefinition");
205  if (is_object($objDefinition))
206  {
207  $translation_type = $objDefinition->getTranslationType($row->type);
208  }
209  //$ilBench->stop("Tree", "fetchNodeData_readDefinition");
210 
211  if ($translation_type == "db")
212  {
213  //$ilBench->start("Tree", "fetchNodeData_getTranslation");
214  $q = "SELECT title,description FROM object_translation ".
215  "WHERE obj_id = ".$ilDB->quote($a_obj_id)." ".
216  "AND lang_code = ".$ilDB->quote($a_lang)." ".
217  "AND NOT lang_default = 1";
218  $r = $ilDB->query($q);
219 
220  $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
221  if ($row)
222  {
223  $this->object_data_cache[$a_obj_id]['title'] = $row->title;
224  $this->object_data_cache[$a_obj_id]['description'] = $row->description;
225  }
226  //$ilBench->stop("Tree", "fetchNodeData_getTranslation");
227  }
228  }
229 
230  return true;
231  }
232 
240  function preloadObjectCache($a_obj_ids, $a_lang = "")
241  {
242  global $ilDB, $objDefinition, $ilUser;
243 
244  if (is_object($ilUser) && $a_lang == "")
245  {
246  $a_lang = $ilUser->getLanguage();
247  }
248 
249 //echo "<br>-preloading-"; var_dump($a_obj_ids);
250  if (!is_array($a_obj_ids)) return;
251  if (count($a_obj_ids) == 0) return;
252 
253  $query = "SELECT * FROM object_data WHERE obj_id IN (".
254  implode(",",ilUtil::quoteArray($a_obj_ids)).")";
255  $res = $ilDB->query($query);
256  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
257  {
258 //echo "<br>store_obj-".$row->obj_id."-".$row->type."-".$row->title."-";
259  $this->object_data_cache[$row->obj_id]['title'] = $row->title;
260  $this->object_data_cache[$row->obj_id]['description'] = $row->description;
261  $this->object_data_cache[$row->obj_id]['type'] = $row->type;
262  $this->object_data_cache[$row->obj_id]['owner'] = $row->owner;
263  $this->object_data_cache[$row->obj_id]['last_update'] = $row->last_update;
264 
265  if (is_object($objDefinition))
266  {
267  $translation_type = $objDefinition->getTranslationType($row->type);
268  }
269  //$ilBench->stop("Tree", "fetchNodeData_readDefinition");
270 
271  if ($translation_type == "db")
272  {
273  //$ilBench->start("Tree", "fetchNodeData_getTranslation");
274  $q = "SELECT title,description FROM object_translation ".
275  "WHERE obj_id = ".$ilDB->quote($row->obj_id)." ".
276  "AND lang_code = ".$ilDB->quote($a_lang)." ".
277  "AND NOT lang_default = 1";
278  $r = $ilDB->query($q);
279 
280  $row2 = $r->fetchRow(DB_FETCHMODE_OBJECT);
281  if ($row2)
282  {
283  $this->object_data_cache[$row->obj_id]['title'] = $row2->title;
284  $this->object_data_cache[$row->obj_id]['description'] = $row2->description;
285  }
286  //$ilBench->stop("Tree", "fetchNodeData_getTranslation");
287  }
288  }
289  }
290 
291  function preloadReferenceCache($a_ref_ids, $a_incl_obj = true)
292  {
293  global $ilDB;
294 
295  if (!is_array($a_ref_ids)) return;
296  if (count($a_ref_ids) == 0) return;
297 
298  $query = "SELECT ref_id, obj_id FROM object_reference WHERE ref_id IN (".
299  implode(",",ilUtil::quoteArray($a_ref_ids)).")";
300  $res = $this->db->query($query);
301  $obj_ids = array();
302  while($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
303  {
304  $this->reference_cache[$row['ref_id']] = $row['obj_id'];
305 //echo "<br>store_ref-".$row['ref_id']."-".$row['obj_id']."-";
306  $obj_ids[] = $row['obj_id'];
307  }
308  if ($a_incl_obj)
309  {
310  $this->preloadObjectCache($obj_ids);
311  }
312  }
313 
314 }
315 ?>