make it a bit cleared: users should not modify the file
[lhc/web/wiklou.git] / includes / BagOStuff.php
1 <?php
2 #
3 # Copyright (C) 2003-2004 Brion Vibber <brion@pobox.com>
4 # http://www.mediawiki.org/
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with this program; if not, write to the Free Software Foundation, Inc.,
18 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 # http://www.gnu.org/copyleft/gpl.html
20 /**
21 *
22 * @package MediaWiki
23 */
24
25 /**
26 * Simple generic object store
27 *
28 * interface is intended to be more or less compatible with
29 * the PHP memcached client.
30 *
31 * backends for local hash array and SQL table included:
32 * $bag = new HashBagOStuff();
33 * $bag = new MysqlBagOStuff($tablename); # connect to db first
34 *
35 * @package MediaWiki
36 * @abstract
37 */
38 class BagOStuff {
39 var $debugmode;
40
41 function BagOStuff() {
42 $this->set_debug( false );
43 }
44
45 function set_debug($bool) {
46 $this->debugmode = $bool;
47 }
48
49 /* *** THE GUTS OF THE OPERATION *** */
50 /* Override these with functional things in subclasses */
51
52 function get($key) {
53 /* stub */
54 return false;
55 }
56
57 function set($key, $value, $exptime=0) {
58 /* stub */
59 return false;
60 }
61
62 function delete($key, $time=0) {
63 /* stub */
64 return false;
65 }
66
67 function lock($key, $timeout = 0) {
68 /* stub */
69 return true;
70 }
71
72 function unlock($key) {
73 /* stub */
74 return true;
75 }
76
77 /* *** Emulated functions *** */
78 /* Better performance can likely be got with custom written versions */
79 function get_multi($keys) {
80 $out = array();
81 foreach($keys as $key)
82 $out[$key] = $this->get($key);
83 return $out;
84 }
85
86 function set_multi($hash, $exptime=0) {
87 foreach($hash as $key => $value)
88 $this->set($key, $value, $exptime);
89 }
90
91 function add($key, $value, $exptime=0) {
92 if( $this->get($key) == false ) {
93 $this->set($key, $value, $exptime);
94 return true;
95 }
96 }
97
98 function add_multi($hash, $exptime=0) {
99 foreach($hash as $key => $value)
100 $this->add($key, $value, $exptime);
101 }
102
103 function delete_multi($keys, $time=0) {
104 foreach($keys as $key)
105 $this->delete($key, $time);
106 }
107
108 function replace($key, $value, $exptime=0) {
109 if( $this->get($key) !== false )
110 $this->set($key, $value, $exptime);
111 }
112
113 function incr($key, $value=1) {
114 if ( !$this->lock($key) ) {
115 return false;
116 }
117 $value = intval($value);
118 if($value < 0) $value = 0;
119
120 $n = false;
121 if( ($n = $this->get($key)) !== false ) {
122 $n += $value;
123 $this->set($key, $n); // exptime?
124 }
125 $this->unlock($key);
126 return $n;
127 }
128
129 function decr($key, $value=1) {
130 if ( !$this->lock($key) ) {
131 return false;
132 }
133 $value = intval($value);
134 if($value < 0) $value = 0;
135
136 $m = false;
137 if( ($n = $this->get($key)) !== false ) {
138 $m = $n - $value;
139 if($m < 0) $m = 0;
140 $this->set($key, $m); // exptime?
141 }
142 $this->unlock($key);
143 return $m;
144 }
145
146 function _debug($text) {
147 if($this->debugmode)
148 wfDebug("BagOStuff debug: $text\n");
149 }
150 }
151
152
153 /**
154 * Functional versions!
155 * @todo document
156 * @package MediaWiki
157 */
158 class HashBagOStuff extends BagOStuff {
159 /*
160 This is a test of the interface, mainly. It stores
161 things in an associative array, which is not going to
162 persist between program runs.
163 */
164 var $bag;
165
166 function HashBagOStuff() {
167 $this->bag = array();
168 }
169
170 function _expire($key) {
171 $et = $this->bag[$key][1];
172 if(($et == 0) || ($et > time()))
173 return false;
174 $this->delete($key);
175 return true;
176 }
177
178 function get($key) {
179 if(!$this->bag[$key])
180 return false;
181 if($this->_expire($key))
182 return false;
183 return $this->bag[$key][0];
184 }
185
186 function set($key,$value,$exptime=0) {
187 if(($exptime != 0) && ($exptime < 3600*24*30))
188 $exptime = time() + $exptime;
189 $this->bag[$key] = array( $value, $exptime );
190 }
191
192 function delete($key,$time=0) {
193 if(!$this->bag[$key])
194 return false;
195 unset($this->bag[$key]);
196 return true;
197 }
198 }
199
200 /*
201 CREATE TABLE objectcache (
202 keyname char(255) binary not null default '',
203 value mediumblob,
204 exptime datetime,
205 unique key (keyname),
206 key (exptime)
207 );
208 */
209
210 /**
211 * @todo document
212 * @abstract
213 * @package MediaWiki
214 */
215 class SqlBagOStuff extends BagOStuff {
216 var $table;
217 var $lastexpireall = 0;
218
219 function SqlBagOStuff($tablename = 'objectcache') {
220 $this->table = $tablename;
221 }
222
223 function get($key) {
224 /* expire old entries if any */
225 $this->garbageCollect();
226
227 $res = $this->_query(
228 "SELECT value,exptime FROM $0 WHERE keyname='$1'", $key);
229 if(!$res) {
230 $this->_debug("get: ** error: " . $this->_dberror($res) . " **");
231 return false;
232 }
233 if($row=$this->_fetchobject($res)) {
234 $this->_debug("get: retrieved data; exp time is " . $row->exptime);
235 return $this->_unserialize($row->value);
236 } else {
237 $this->_debug('get: no matching rows');
238 }
239 return false;
240 }
241
242 function set($key,$value,$exptime=0) {
243 $exptime = intval($exptime);
244 if($exptime < 0) $exptime = 0;
245 if($exptime == 0) {
246 $exp = $this->_maxdatetime();
247 } else {
248 if($exptime < 3600*24*30)
249 $exptime += time();
250 $exp = $this->_fromunixtime($exptime);
251 }
252 $this->delete( $key );
253 $this->_doinsert($this->getTableName(), array(
254 'keyname' => $key,
255 'value' => $this->_blobencode($this->_serialize($value)),
256 'exptime' => $exp
257 ));
258 return true; /* ? */
259 }
260
261 function delete($key,$time=0) {
262 $this->_query(
263 "DELETE FROM $0 WHERE keyname='$1'", $key );
264 return true; /* ? */
265 }
266
267 function getTableName() {
268 return $this->table;
269 }
270
271 function _query($sql) {
272 $reps = func_get_args();
273 $reps[0] = $this->getTableName();
274 // ewwww
275 for($i=0;$i<count($reps);$i++) {
276 $sql = str_replace(
277 '$' . $i,
278 $i > 0 ? $this->_strencode($reps[$i]) : $reps[$i],
279 $sql);
280 }
281 $res = $this->_doquery($sql);
282 if($res == false) {
283 $this->_debug('query failed: ' . $this->_dberror($res));
284 }
285 return $res;
286 }
287
288 function _strencode($str) {
289 /* Protect strings in SQL */
290 return str_replace( "'", "''", $str );
291 }
292 function _blobencode($str) {
293 return $str;
294 }
295 function _doinsert($table, $vals) {
296 die( 'abstract function SqlBagOStuff::_doinsert() must be defined' );
297 }
298 function _doquery($sql) {
299 die( 'abstract function SqlBagOStuff::_doquery() must be defined' );
300 }
301
302 function _fetchrow($res) {
303 die( 'abstract function SqlBagOStuff::_fetchrow() must be defined' );
304 }
305
306 function _freeresult($result) {
307 /* stub */
308 return false;
309 }
310
311 function _dberror($result) {
312 /* stub */
313 return 'unknown error';
314 }
315
316 function _maxdatetime() {
317 die( 'abstract function SqlBagOStuff::_maxdatetime() must be defined' );
318 }
319
320 function _fromunixtime() {
321 die( 'abstract function SqlBagOStuff::_fromunixtime() must be defined' );
322 }
323
324 function garbageCollect() {
325 /* Ignore 99% of requests */
326 if ( !mt_rand( 0, 100 ) ) {
327 $nowtime = time();
328 /* Avoid repeating the delete within a few seconds */
329 if ( $nowtime > ($this->lastexpireall + 1) ) {
330 $this->lastexpireall = $nowtime;
331 $this->expireall();
332 }
333 }
334 }
335
336 function expireall() {
337 /* Remove any items that have expired */
338 $now = $this->_fromunixtime( time() );
339 $this->_query( "DELETE FROM $0 WHERE exptime < '$now'" );
340 }
341
342 function deleteall() {
343 /* Clear *all* items from cache table */
344 $this->_query( "DELETE FROM $0" );
345 }
346
347 /**
348 * Serialize an object and, if possible, compress the representation.
349 * On typical message and page data, this can provide a 3X decrease
350 * in storage requirements.
351 *
352 * @param mixed $data
353 * @return string
354 */
355 function _serialize( &$data ) {
356 $serial = serialize( $data );
357 if( function_exists( 'gzdeflate' ) ) {
358 return gzdeflate( $serial );
359 } else {
360 return $serial;
361 }
362 }
363
364 /**
365 * Unserialize and, if necessary, decompress an object.
366 * @param string $serial
367 * @return mixed
368 */
369 function _unserialize( $serial ) {
370 if( function_exists( 'gzinflate' ) ) {
371 $decomp = @gzinflate( $serial );
372 if( false !== $decomp ) {
373 $serial = $decomp;
374 }
375 }
376 $ret = unserialize( $serial );
377 return $ret;
378 }
379 }
380
381 /**
382 * @todo document
383 * @package MediaWiki
384 */
385 class MediaWikiBagOStuff extends SqlBagOStuff {
386 var $tableInitialised = false;
387
388 function _doquery($sql) {
389 $dbw =& wfGetDB( DB_MASTER );
390 return $dbw->query($sql, 'MediaWikiBagOStuff::_doquery');
391 }
392 function _doinsert($t, $v) {
393 $dbw =& wfGetDB( DB_MASTER );
394 return $dbw->insert($t, $v, 'MediaWikiBagOStuff::_doinsert');
395 }
396 function _fetchobject($result) {
397 $dbw =& wfGetDB( DB_MASTER );
398 return $dbw->fetchObject($result);
399 }
400 function _freeresult($result) {
401 $dbw =& wfGetDB( DB_MASTER );
402 return $dbw->freeResult($result);
403 }
404 function _dberror($result) {
405 $dbw =& wfGetDB( DB_MASTER );
406 return $dbw->lastError();
407 }
408 function _maxdatetime() {
409 $dbw =& wfGetDB(DB_MASTER);
410 return $dbw->timestamp('9999-12-31 12:59:59');
411 }
412 function _fromunixtime($ts) {
413 $dbw =& wfGetDB(DB_MASTER);
414 return $dbw->timestamp($ts);
415 }
416 function _strencode($s) {
417 $dbw =& wfGetDB( DB_MASTER );
418 return $dbw->strencode($s);
419 }
420 function _blobencode($s) {
421 $dbw =& wfGetDB( DB_MASTER );
422 return $dbw->encodeBlob($s);
423 }
424 function getTableName() {
425 if ( !$this->tableInitialised ) {
426 $dbw =& wfGetDB( DB_MASTER );
427 /* This is actually a hack, we should be able
428 to use Language classes here... or not */
429 if (!$dbw)
430 die("Could not connect to database");
431 $this->table = $dbw->tableName( $this->table );
432 $this->tableInitialised = true;
433 }
434 return $this->table;
435 }
436 }
437
438 /**
439 * This is a wrapper for Turck MMCache's shared memory functions.
440 *
441 * You can store objects with mmcache_put() and mmcache_get(), but Turck seems
442 * to use a weird custom serializer that randomly segfaults. So we wrap calls
443 * with serialize()/unserialize().
444 *
445 * The thing I noticed about the Turck serialized data was that unlike ordinary
446 * serialize(), it contained the names of methods, and judging by the amount of
447 * binary data, perhaps even the bytecode of the methods themselves. It may be
448 * that Turck's serializer is faster, so a possible future extension would be
449 * to use it for arrays but not for objects.
450 *
451 * @package MediaWiki
452 */
453 class TurckBagOStuff extends BagOStuff {
454 function get($key) {
455 $val = mmcache_get( $key );
456 if ( is_string( $val ) ) {
457 $val = unserialize( $val );
458 }
459 return $val;
460 }
461
462 function set($key, $value, $exptime=0) {
463 mmcache_put( $key, serialize( $value ), $exptime );
464 return true;
465 }
466
467 function delete($key, $time=0) {
468 mmcache_rm( $key );
469 return true;
470 }
471
472 function lock($key, $waitTimeout = 0 ) {
473 mmcache_lock( $key );
474 return true;
475 }
476
477 function unlock($key) {
478 mmcache_unlock( $key );
479 return true;
480 }
481 }
482
483 /**
484 * This is a wrapper for eAccelerator's shared memory functions.
485 *
486 * This is basically identical to the Turck MMCache version,
487 * mostly because eAccelerator is based on Turck MMCache.
488 *
489 * @package MediaWiki
490 */
491 class eAccelBagOStuff extends BagOStuff {
492 function get($key) {
493 $val = eaccelerator_get( $key );
494 if ( is_string( $val ) ) {
495 $val = unserialize( $val );
496 }
497 return $val;
498 }
499
500 function set($key, $value, $exptime=0) {
501 eaccelerator_put( $key, serialize( $value ), $exptime );
502 return true;
503 }
504
505 function delete($key, $time=0) {
506 eaccelerator_rm( $key );
507 return true;
508 }
509
510 function lock($key, $waitTimeout = 0 ) {
511 eaccelerator_lock( $key );
512 return true;
513 }
514
515 function unlock($key) {
516 eaccelerator_unlock( $key );
517 return true;
518 }
519 }
520 ?>