Table prefix bugs
[lhc/web/wiklou.git] / includes / ObjectCache.php
1 <?php
2 # $Id$
3 #
4 # Copyright (C) 2003-2004 Brion Vibber <brion@pobox.com>
5 # http://www.mediawiki.org/
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 # http://www.gnu.org/copyleft/gpl.html
21
22 # Simple generic object store
23 # interface is intended to be more or less compatible with
24 # the PHP memcached client.
25 #
26 # backends for local hash array and SQL table included:
27 # $bag = new HashBagOStuff();
28 # $bag = new MysqlBagOStuff($tablename); # connect to db first
29
30 class /* abstract */ BagOStuff {
31 var $debugmode;
32
33 function BagOStuff() {
34 $this->set_debug( false );
35 }
36
37 function set_debug($bool) {
38 $this->debugmode = $bool;
39 }
40
41 /* *** THE GUTS OF THE OPERATION *** */
42 /* Override these with functional things in subclasses */
43
44 function get($key) {
45 /* stub */
46 return false;
47 }
48
49 function set($key, $value, $exptime=0) {
50 /* stub */
51 return false;
52 }
53
54 function delete($key, $time=0) {
55 /* stub */
56 return false;
57 }
58
59 function lock($key, $timeout = 0) {
60 /* stub */
61 return true;
62 }
63
64 function unlock($key) {
65 /* stub */
66 return true;
67 }
68
69 /* *** Emulated functions *** */
70 /* Better performance can likely be got with custom written versions */
71 function get_multi($keys) {
72 $out = array();
73 foreach($keys as $key)
74 $out[$key] = $this->get($key);
75 return $out;
76 }
77
78 function set_multi($hash, $exptime=0) {
79 foreach($hash as $key => $value)
80 $this->set($key, $value, $exptime);
81 }
82
83 function add($key, $value, $exptime=0) {
84 if( $this->get($key) == false ) {
85 $this->set($key, $value, $exptime);
86 return true;
87 }
88 }
89
90 function add_multi($hash, $exptime=0) {
91 foreach($hash as $key => $value)
92 $this->add($key, $value, $exptime);
93 }
94
95 function delete_multi($keys, $time=0) {
96 foreach($keys as $key)
97 $this->delete($key, $time);
98 }
99
100 function replace($key, $value, $exptime=0) {
101 if( $this->get($key) !== false )
102 $this->set($key, $value, $exptime);
103 }
104
105 function incr($key, $value=1) {
106 if ( !$this->lock($key) ) {
107 return false;
108 }
109 $value = intval($value);
110 if($value < 0) $value = 0;
111
112 $n = false;
113 if( ($n = $this->get($key)) !== false ) {
114 $n += $value;
115 $this->set($key, $n); // exptime?
116 }
117 $this->unlock($key);
118 return $n;
119 }
120
121 function decr($key, $value=1) {
122 if ( !$this->lock($key) ) {
123 return false;
124 }
125 $value = intval($value);
126 if($value < 0) $value = 0;
127
128 $m = false;
129 if( ($n = $this->get($key)) !== false ) {
130 $m = $n - $value;
131 if($m < 0) $m = 0;
132 $this->set($key, $m); // exptime?
133 }
134 $this->unlock($key);
135 return $m;
136 }
137
138 function _debug($text) {
139 if($this->debugmode)
140 wfDebug("BagOStuff debug: $text\n");
141 }
142 }
143
144
145 /* Functional versions! */
146 class HashBagOStuff extends BagOStuff {
147 /*
148 This is a test of the interface, mainly. It stores
149 things in an associative array, which is not going to
150 persist between program runs.
151 */
152 var $bag;
153
154 function HashBagOStuff() {
155 $this->bag = array();
156 }
157
158 function _expire($key) {
159 $et = $this->bag[$key][1];
160 if(($et == 0) || ($et > time()))
161 return false;
162 $this->delete($key);
163 return true;
164 }
165
166 function get($key) {
167 if(!$this->bag[$key])
168 return false;
169 if($this->_expire($key))
170 return false;
171 return $this->bag[$key][0];
172 }
173
174 function set($key,$value,$exptime=0) {
175 if(($exptime != 0) && ($exptime < 3600*24*30))
176 $exptime = time() + $exptime;
177 $this->bag[$key] = array( $value, $exptime );
178 }
179
180 function delete($key,$time=0) {
181 if(!$this->bag[$key])
182 return false;
183 unset($this->bag[$key]);
184 return true;
185 }
186 }
187
188 /*
189 CREATE TABLE objectcache (
190 keyname char(255) binary not null default '',
191 value mediumblob,
192 exptime datetime,
193 unique key (keyname),
194 key (exptime)
195 );
196 */
197 class /* abstract */ SqlBagOStuff extends BagOStuff {
198 var $table;
199
200 function SqlBagOStuff($tablename = 'objectcache') {
201 $this->table = $tablename;
202 }
203
204 function get($key) {
205 /* expire old entries if any */
206 $this->expireall();
207
208 $res = $this->_query(
209 "SELECT value,exptime FROM $0 WHERE keyname='$1'", $key);
210 if(!$res) {
211 $this->_debug("get: ** error: " . $this->_dberror($res) . " **");
212 return false;
213 }
214 if($row=$this->_fetchobject($res)) {
215 $this->_debug("get: retrieved data; exp time is " . $row->exptime);
216 return unserialize($row->value);
217 } else {
218 $this->_debug('get: no matching rows');
219 }
220 return false;
221 }
222
223 function set($key,$value,$exptime=0) {
224 $exptime = intval($exptime);
225 if($exptime < 0) $exptime = 0;
226 if($exptime == 0) {
227 $exp = $this->_maxdatetime();
228 } else {
229 if($exptime < 3600*24*30)
230 $exptime += time();
231 $exp = $this->_fromunixtime($exptime);
232 }
233 $this->delete( $key );
234 $this->_query(
235 "INSERT INTO $0 (keyname,value,exptime) VALUES('$1','$2','$exp')",
236 $key, serialize($value));
237 return true; /* ? */
238 }
239
240 function delete($key,$time=0) {
241 $this->_query(
242 "DELETE FROM $0 WHERE keyname='$1'", $key );
243 return true; /* ? */
244 }
245
246 function getTableName() {
247 return $this->table;
248 }
249
250 function _query($sql) {
251 $reps = func_get_args();
252 $reps[0] = $this->getTableName();
253 // ewwww
254 for($i=0;$i<count($reps);$i++) {
255 $sql = str_replace(
256 '$' . $i,
257 $this->_strencode($reps[$i]),
258 $sql);
259 }
260 $res = $this->_doquery($sql);
261 if($res == false) {
262 $this->_debug('query failed: ' . $this->_dberror($res));
263 }
264 return $res;
265 }
266
267 function _strencode($str) {
268 /* Protect strings in SQL */
269 return str_replace( "'", "''", $str );
270 }
271
272 function _doquery($sql) {
273 die( 'abstract function SqlBagOStuff::_doquery() must be defined' );
274 }
275
276 function _fetchrow($res) {
277 die( 'abstract function SqlBagOStuff::_fetchrow() must be defined' );
278 }
279
280 function _freeresult($result) {
281 /* stub */
282 return false;
283 }
284
285 function _dberror($result) {
286 /* stub */
287 return 'unknown error';
288 }
289
290 function _maxdatetime() {
291 die( 'abstract function SqlBagOStuff::_maxdatetime() must be defined' );
292 }
293
294 function _fromunixtime() {
295 die( 'abstract function SqlBagOStuff::_fromunixtime() must be defined' );
296 }
297
298 function expireall() {
299 /* Remove any items that have expired */
300 $now=$this->_fromunixtime(time());
301 $this->_query( "DELETE FROM $0 WHERE exptime<'$now'" );
302 }
303
304 function deleteall() {
305 /* Clear *all* items from cache table */
306 $this->_query( "DELETE FROM $0" );
307 }
308 }
309
310 class MediaWikiBagOStuff extends SqlBagOStuff {
311 var $tableInitialised = false;
312
313 function _doquery($sql) {
314 $dbw =& wfGetDB( DB_MASTER );
315 return $dbw->query($sql, 'MediaWikiBagOStuff:_doquery');
316 }
317 function _fetchobject($result) {
318 $dbw =& wfGetDB( DB_MASTER );
319 return $dbw->fetchObject($result);
320 }
321 function _freeresult($result) {
322 $dbw =& wfGetDB( DB_MASTER );
323 return $dbw->freeResult($result);
324 }
325 function _dberror($result) {
326 $dbw =& wfGetDB( DB_MASTER );
327 return $dbw->lastError();
328 }
329 function _maxdatetime() {
330 return '9999-12-31 12:59:59';
331 }
332 function _fromunixtime($ts) {
333 return gmdate( 'Y-m-d H:i:s', $ts );
334 }
335 function _strencode($s) {
336 $dbw =& wfGetDB( DB_MASTER );
337 return $dbw->strencode($s);
338 }
339 function getTableName() {
340 if ( !$this->tableInitialised ) {
341 $dbw =& wfGetDB( DB_MASTER );
342 $this->table = $dbw->tableName( $this->table );
343 $this->tableInitialised = true;
344 }
345 return $this->table;
346 }
347 }
348
349 class TurckBagOStuff extends BagOStuff {
350 function get($key) {
351 return mmcache_get( $key );
352 }
353
354 function set($key, $value, $exptime=0) {
355 mmcache_put( $key, $value, $exptime );
356 return true;
357 }
358
359 function delete($key, $time=0) {
360 mmcache_rm( $key );
361 return true;
362 }
363
364 function lock($key, $waitTimeout = 0 ) {
365 mmcache_lock( $key );
366 return true;
367 }
368
369 function unlock($key) {
370 mmcache_unlock( $key );
371 return true;
372 }
373 }
374 ?>