4 |
david |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/***************************************************************************\
|
|
|
4 |
* SPIP, Systeme de publication pour l'internet *
|
|
|
5 |
* *
|
|
|
6 |
* Copyright (c) 2001-2005 *
|
|
|
7 |
* Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
|
|
|
8 |
* *
|
|
|
9 |
* Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
|
|
|
10 |
* Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
|
|
|
11 |
\***************************************************************************/
|
|
|
12 |
|
|
|
13 |
|
|
|
14 |
//
|
|
|
15 |
// Ce fichier ne sera execute qu'une fois
|
|
|
16 |
if (defined("_ECRIRE_INC_OBJET_BASE")) return;
|
|
|
17 |
define("_ECRIRE_INC_OBJET_BASE", "1");
|
|
|
18 |
|
|
|
19 |
|
|
|
20 |
class _Abstract {
|
|
|
21 |
function abstract_error($str) {
|
|
|
22 |
die ("<h4>".$str."<br>"._T('info_contact_developpeur')."</h4>");
|
|
|
23 |
}
|
|
|
24 |
function abstract_func() {
|
|
|
25 |
$this->abstract_error(_T('avis_erreur_fonction_contexte'));
|
|
|
26 |
}
|
|
|
27 |
function _Abstract() { $this->abstract_func(); }
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
class ObjectCacheInstance extends _Abstract {
|
|
|
32 |
// Variable values (array)
|
|
|
33 |
var $fast_vars;
|
|
|
34 |
var $slow_vars;
|
|
|
35 |
|
|
|
36 |
// Variable status
|
|
|
37 |
var $fast_vars_loaded = false;
|
|
|
38 |
var $slow_vars_loaded = false;
|
|
|
39 |
|
|
|
40 |
// Is modified ?
|
|
|
41 |
var $dirty = false;
|
|
|
42 |
|
|
|
43 |
function ObjectCacheInstance() {
|
|
|
44 |
$this->fast_vars = array();
|
|
|
45 |
$this->slow_vars = array();
|
|
|
46 |
}
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
|
|
|
50 |
class _ObjectFactory extends _Abstract {
|
|
|
51 |
// Factory ID
|
|
|
52 |
var $id_factory;
|
|
|
53 |
|
|
|
54 |
// Object class name (for instantiation)
|
|
|
55 |
var $object_class;
|
|
|
56 |
|
|
|
57 |
// SQL table name/pattern
|
|
|
58 |
var $sql_table;
|
|
|
59 |
var $sql_id;
|
|
|
60 |
|
|
|
61 |
// Plain array
|
|
|
62 |
var $fast_vars_list, $nb_fast_vars;
|
|
|
63 |
var $slow_vars_list, $nb_slow_vars;
|
|
|
64 |
|
|
|
65 |
// Associative array
|
|
|
66 |
var $fast_vars_array;
|
|
|
67 |
var $slow_vars_array;
|
|
|
68 |
|
|
|
69 |
// SQL field names
|
|
|
70 |
var $fast_vars_sql;
|
|
|
71 |
var $slow_vars_sql;
|
|
|
72 |
|
|
|
73 |
// Object cache
|
|
|
74 |
var $cache;
|
|
|
75 |
|
|
|
76 |
// ---------------------------------------------------------
|
|
|
77 |
|
|
|
78 |
//
|
|
|
79 |
// Init factory helper variables and constants
|
|
|
80 |
//
|
|
|
81 |
function init_factory($id_factory) {
|
|
|
82 |
$this->id_factory = $id_factory;
|
|
|
83 |
|
|
|
84 |
// Store different representations of fast vars
|
|
|
85 |
if (is_array($this->fast_vars_list)) {
|
|
|
86 |
reset($this->fast_vars_list);
|
|
|
87 |
while (list($key, $val) = each($this->fast_vars_list)) {
|
|
|
88 |
$this->fast_vars_array[$val] = $val;
|
|
|
89 |
$this->fast_vars_sql[] = $this->sql_table.'.'.$val;
|
|
|
90 |
}
|
|
|
91 |
$this->fast_vars_sql = join(', ', $this->fast_vars_sql);
|
|
|
92 |
$this->nb_fast_vars = count($this->fast_vars_list);
|
|
|
93 |
}
|
|
|
94 |
else $this->nb_fast_vars = 0;
|
|
|
95 |
|
|
|
96 |
// Store different representations of slow vars
|
|
|
97 |
if (is_array($this->slow_vars_list)) {
|
|
|
98 |
reset($this->slow_vars_list);
|
|
|
99 |
while (list($key, $val) = each($this->slow_vars_list)) {
|
|
|
100 |
$this->slow_vars_array[$val] = $val;
|
|
|
101 |
$this->slow_vars_sql[] = $this->sql_table.'.'.$val;
|
|
|
102 |
}
|
|
|
103 |
$this->slow_vars_sql = join(', ', $this->slow_vars_sql);
|
|
|
104 |
$this->nb_slow_vars = count($this->slow_vars_list);
|
|
|
105 |
}
|
|
|
106 |
else $this->nb_slow_vars = 0;
|
|
|
107 |
|
|
|
108 |
// Default value for object id in database
|
|
|
109 |
if (!$this->sql_id) {
|
|
|
110 |
$this->sql_id = 'id_'.strtolower($this->object_class);
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
|
|
|
115 |
//
|
|
|
116 |
// Object management methods
|
|
|
117 |
//
|
|
|
118 |
|
|
|
119 |
function new_object($id) { $this->abstract(); }
|
|
|
120 |
|
|
|
121 |
function create_object_cache_instance($id) {
|
|
|
122 |
if (!($g = $this->cache[$id])) {
|
|
|
123 |
$g = '_'.$this->object_class.'_'.$id;
|
|
|
124 |
$GLOBALS[$g] = new ObjectCacheInstance;
|
|
|
125 |
$this->cache[$id] = $g;
|
|
|
126 |
}
|
|
|
127 |
return $g;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
// Create a new alias for an object
|
|
|
131 |
// (aliases are the only way by which user code sees an object)
|
|
|
132 |
function create_object_alias($id) {
|
|
|
133 |
$class = $this->object_class;
|
|
|
134 |
$alias = new $class;
|
|
|
135 |
$alias->init_object($this->id_factory, $id);
|
|
|
136 |
return $alias;
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
// Get field of an object (by ID)
|
|
|
140 |
function get_object_field($id, $name) {
|
|
|
141 |
$g = $this->cache[$id];
|
|
|
142 |
if ($v = $this->fast_vars_array[$name]) {
|
|
|
143 |
if (!$GLOBALS[$g]->fast_vars_loaded)
|
|
|
144 |
$this->load_object_id($id, true);
|
|
|
145 |
return $GLOBALS[$g]->fast_vars[$v];
|
|
|
146 |
}
|
|
|
147 |
else if ($v = $this->slow_vars_array[$name]) {
|
|
|
148 |
if (!$GLOBALS[$g]->slow_vars_loaded)
|
|
|
149 |
$this->load_object_id($id, false);
|
|
|
150 |
return $GLOBALS[$g]->slow_vars[$v];
|
|
|
151 |
}
|
|
|
152 |
else {
|
|
|
153 |
$this->abstract_error(_T('avis_champ_incorrect_type_objet', array('name' => $name, 'type' => $this->object_class)));
|
|
|
154 |
}
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
// Set field of an object (by ID)
|
|
|
158 |
function set_object_field($id, $name, $value) {
|
|
|
159 |
$g = $this->cache[$id];
|
|
|
160 |
if ($v = $this->fast_vars_array[$name]) {
|
|
|
161 |
if (!$GLOBALS[$g]->fast_vars_loaded)
|
|
|
162 |
$this->load_object_id($id, true);
|
|
|
163 |
$GLOBALS[$g]->fast_vars[$v] = $value;
|
|
|
164 |
$GLOBALS[$g]->dirty = true;
|
|
|
165 |
}
|
|
|
166 |
else if ($v = $this->slow_vars_array[$name]) {
|
|
|
167 |
if (!$GLOBALS[$g]->slow_vars_loaded)
|
|
|
168 |
$this->load_object_id($id, false);
|
|
|
169 |
$GLOBALS[$g]->slow_vars[$v] = $value;
|
|
|
170 |
$GLOBALS[$g]->dirty = true;
|
|
|
171 |
}
|
|
|
172 |
else {
|
|
|
173 |
$this->abstract_error(_T('avis_champ_incorrect_type_objet', array('name' => $name)).$this->object_class);
|
|
|
174 |
}
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
|
|
|
178 |
//
|
|
|
179 |
// Load object by SQL query
|
|
|
180 |
//
|
|
|
181 |
function load_object_sql($query, $fast, $multiple = false) {
|
|
|
182 |
$cols = $this->fast_vars_sql;
|
|
|
183 |
if (!$fast && $this->slow_vars_sql) {
|
|
|
184 |
if ($cols) $cols .= ', ';
|
|
|
185 |
$cols .= $this->slow_vars_sql;
|
|
|
186 |
}
|
|
|
187 |
// Replace generic names by actual ones
|
|
|
188 |
$query = ereg_replace('#cols', $cols, $query);
|
|
|
189 |
$query = ereg_replace('#table', $this->sql_table, $query);
|
|
|
190 |
$query = ereg_replace('#id', $this->sql_table.'.'.$this->sql_id, $query);
|
|
|
191 |
$result = spip_query($query);
|
|
|
192 |
// If multiple results expected, create a result array
|
|
|
193 |
if ($multiple) $r = array();
|
|
|
194 |
if ($result) while ($row = spip_fetch_array($result)) {
|
|
|
195 |
$id = $row[$this->sql_id];
|
|
|
196 |
$g = $this->create_object_cache_instance($id);
|
|
|
197 |
// Read fast vars
|
|
|
198 |
for ($i = 0; $i < $this->nb_fast_vars; $i++) {
|
|
|
199 |
$var = $this->fast_vars_list[$i];
|
|
|
200 |
$GLOBALS[$g]->fast_vars[$var] = $row[$var];
|
|
|
201 |
}
|
|
|
202 |
$GLOBALS[$g]->fast_vars_loaded = true;
|
|
|
203 |
if (!$fast) {
|
|
|
204 |
// Read slow vars
|
|
|
205 |
for ($i = 0; $i < $this->nb_slow_vars; $i++) {
|
|
|
206 |
$var = $this->slow_vars_list[$i];
|
|
|
207 |
$GLOBALS[$g]->slow_vars[$var] = $row[$var];
|
|
|
208 |
}
|
|
|
209 |
$GLOBALS[$g]->slow_vars_loaded = true;
|
|
|
210 |
}
|
|
|
211 |
if ($multiple) $r[$id] = $id;
|
|
|
212 |
else break;
|
|
|
213 |
}
|
|
|
214 |
if ($multiple) return $r;
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
//
|
|
|
218 |
// Load object by ID
|
|
|
219 |
//
|
|
|
220 |
function load_object_id($id, $fast = true) {
|
|
|
221 |
$query = "SELECT #cols FROM #table WHERE #id=$id";
|
|
|
222 |
$this->load_object_sql($query, $fast);
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
//
|
|
|
226 |
// Fetch object only if not in cache
|
|
|
227 |
//
|
|
|
228 |
function fetch_object_id($id, $fast = true) {
|
|
|
229 |
if ($g = $this->cache[$id]) {
|
|
|
230 |
if (!$GLOBALS[$g]->dirty) return;
|
|
|
231 |
}
|
|
|
232 |
else {
|
|
|
233 |
$g = $this->create_object_cache_instance($id);
|
|
|
234 |
}
|
|
|
235 |
$this->load_object_id($id, $fast);
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
//
|
|
|
239 |
// Create new object
|
|
|
240 |
//
|
|
|
241 |
function create_object() {
|
|
|
242 |
static $new_id = 0;
|
|
|
243 |
$id = 'new_'.(++$new_id);
|
|
|
244 |
$g = $this->create_object_cache_instance($id);
|
|
|
245 |
$GLOBALS[$g]->dirty = true;
|
|
|
246 |
$GLOBALS[$g]->fast_vars_loaded = true;
|
|
|
247 |
$GLOBALS[$g]->slow_vars_loaded = true;
|
|
|
248 |
$this->new_object($id);
|
|
|
249 |
return $id;
|
|
|
250 |
}
|
|
|
251 |
|
|
|
252 |
//
|
|
|
253 |
// Main load function : fetch object by generic criterium
|
|
|
254 |
//
|
|
|
255 |
function fetch_object($critere, $fast = true) {
|
|
|
256 |
if ($critere == 'new') {
|
|
|
257 |
// create object
|
|
|
258 |
$id = $this->create_object();
|
|
|
259 |
}
|
|
|
260 |
else if ($critere > 0) {
|
|
|
261 |
// get object by id
|
|
|
262 |
$id = intval($critere);
|
|
|
263 |
$this->fetch_object_id($id, $fast);
|
|
|
264 |
}
|
|
|
265 |
else {
|
|
|
266 |
// get object list by sql
|
|
|
267 |
return $this->load_object_sql($critere, $fast, true);
|
|
|
268 |
}
|
|
|
269 |
return $this->create_object_alias($id);
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
//
|
|
|
273 |
// Main save function : update object by ID
|
|
|
274 |
//
|
|
|
275 |
function update_object($id) {
|
|
|
276 |
$g = $this->cache[$id];
|
|
|
277 |
if ($GLOBALS[$g]->dirty) {
|
|
|
278 |
// generate INSERT query (penser au addslashes)
|
|
|
279 |
}
|
|
|
280 |
}
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
|
|
|
284 |
class _Object extends _Abstract {
|
|
|
285 |
// Factory ID
|
|
|
286 |
var $id_factory;
|
|
|
287 |
|
|
|
288 |
// Object ID
|
|
|
289 |
var $id = 0;
|
|
|
290 |
|
|
|
291 |
function init_object($id_factory, $id = 0) {
|
|
|
292 |
$this->id_factory = $id_factory;
|
|
|
293 |
if ($id) $this->id = $id;
|
|
|
294 |
}
|
|
|
295 |
|
|
|
296 |
function get($var) {
|
|
|
297 |
return $GLOBALS[$this->id_factory]->get_object_field($this->id, $var);
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
function set($var, $value) {
|
|
|
301 |
return $GLOBALS[$this->id_factory]->set_object_field($this->id, $var, $value);
|
|
|
302 |
}
|
|
|
303 |
|
|
|
304 |
function commit() {
|
|
|
305 |
return $GLOBALS[$this->id_factory]->update_object($this->id);
|
|
|
306 |
}
|
|
|
307 |
}
|
|
|
308 |
|
|
|
309 |
|
|
|
310 |
//
|
|
|
311 |
// Create a factory of a given type, and register it
|
|
|
312 |
//
|
|
|
313 |
|
|
|
314 |
function add_factory($type) {
|
|
|
315 |
global $factories;
|
|
|
316 |
$class = ucfirst($type).'Factory';
|
|
|
317 |
$id_factory = $type.'_factory';
|
|
|
318 |
$GLOBALS[$id_factory] = new $class;
|
|
|
319 |
$GLOBALS[$id_factory]->init_factory($id_factory);
|
|
|
320 |
return $id_factory;
|
|
|
321 |
}
|
|
|
322 |
|
|
|
323 |
|
|
|
324 |
|
|
|
325 |
?>
|