[BUGFIX] Handle cache tables in install tool and em
[Packages/TYPO3.CMS.git] / t3lib / cache / backend / class.t3lib_cache_backend_dbbackend.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2008-2011 Ingo Renner <ingo@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * A caching backend which stores cache entries in database tables
27 *
28 * @package TYPO3
29 * @subpackage t3lib_cache
30 * @author Christian Kuhn <lolli@schwarzbu.ch>
31 * @author Ingo Renner <ingo@typo3.org>
32 * @api
33 * @scope prototype
34 */
35 class t3lib_cache_backend_DbBackend extends t3lib_cache_backend_AbstractBackend {
36
37 /**
38 * @var integer Timestamp of 2038-01-01)
39 */
40 const FAKED_UNLIMITED_EXPIRE = 2145909600;
41
42 /**
43 * @var string Name of the cache data table
44 */
45 protected $cacheTable;
46
47 /**
48 * @var string Name of the cache tags table
49 */
50 protected $tagsTable;
51
52 /**
53 * @var boolean Indicates wether data is compressed or not (requires php zlib)
54 */
55 protected $compression = FALSE;
56
57 /**
58 * @var integer -1 to 9, indicates zlib compression level: -1 = default level 6, 0 = no compression, 9 maximum compression
59 */
60 protected $compressionLevel = -1;
61
62 /**
63 * @var string Name of the identifier field, 'table_name.identifier'
64 */
65 protected $identifierField;
66
67 /**
68 * @var string Name of the expire field, 'table_name.expires'
69 */
70 protected $expiresField;
71
72 /**
73 * @var integer Maximum lifetime to stay with expire field below FAKED_UNLIMITED_LIFETIME
74 */
75 protected $maximumLifetime;
76
77 /**
78 * @var string SQL where for a not expired entry
79 */
80 protected $notExpiredStatement;
81
82 /**
83 * @var string Opposite of notExpiredStatement
84 */
85 protected $expiredStatement;
86
87 /**
88 * @var string Data and tags table name comma separated
89 */
90 protected $tableList;
91
92 /**
93 * @var string Join condition for data and tags table
94 */
95 protected $tableJoin;
96
97 /**
98 * Set cache frontend instance and calculate data and tags table name
99 *
100 * @param t3lib_cache_frontend_Frontend $cache The frontend for this backend
101 * @return void
102 * @api
103 */
104 public function setCache(t3lib_cache_frontend_Frontend $cache) {
105 parent::setCache($cache);
106
107 $this->cacheTable = 'cf_' .$this->cacheIdentifier;
108 $this->tagsTable = 'cf_' . $this->cacheIdentifier . '_tags';
109 $this->initializeCommonReferences();
110 }
111
112 /**
113 * Initializes common references used in this backend.
114 *
115 * @return void
116 */
117 protected function initializeCommonReferences() {
118 $this->identifierField = $this->cacheTable . '.identifier';
119 $this->expiresField = $this->cacheTable . '.expires';
120 $this->maximumLifetime = self::FAKED_UNLIMITED_EXPIRE - $GLOBALS['EXEC_TIME'];
121 $this->tableList = $this->cacheTable . ', ' . $this->tagsTable;
122 $this->tableJoin = $this->identifierField . ' = ' . $this->tagsTable . '.identifier';
123 $this->expiredStatement = $this->expiresField . ' < ' . $GLOBALS['EXEC_TIME'];
124 $this->notExpiredStatement = $this->expiresField . ' >= ' . $GLOBALS['EXEC_TIME'];
125 }
126
127 /**
128 * Saves data in a cache file.
129 *
130 * @param string An identifier for this specific cache entry
131 * @param string The data to be stored
132 * @param array Tags to associate with this cache entry
133 * @param integer Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited liftime.
134 * @return void
135 * @throws t3lib_cache_Exception if no cache frontend has been set.
136 * @throws t3lib_cache_exception_InvalidData if the data to be stored is not a string.
137 */
138 public function set($entryIdentifier, $data, array $tags = array(), $lifetime = NULL) {
139 $this->throwExceptionIfFrontendDoesNotExist();
140
141 if (!is_string($data)) {
142 throw new t3lib_cache_exception_InvalidData(
143 'The specified data is of type "' . gettype($data) . '" but a string is expected.',
144 1236518298
145 );
146 }
147
148 if (is_null($lifetime)) {
149 $lifetime = $this->defaultLifetime;
150 }
151 if ($lifetime === 0 || $lifetime > $this->maximumLifetime) {
152 $lifetime = $this->maximumLifetime;
153 }
154 $expires = $GLOBALS['EXEC_TIME'] + $lifetime;
155
156 $this->remove($entryIdentifier);
157
158 if ($this->compression) {
159 $data = gzcompress($data, $this->compressionLevel);
160 }
161
162 $GLOBALS['TYPO3_DB']->exec_INSERTquery(
163 $this->cacheTable,
164 array(
165 'identifier' => $entryIdentifier,
166 'expires' => $expires,
167 'content' => $data,
168 )
169 );
170
171 if (count($tags)) {
172 $fields = array();
173 $fields[] = 'identifier';
174 $fields[] = 'tag';
175
176 $tagRows = array();
177 foreach ($tags as $tag) {
178 $tagRow = array();
179 $tagRow[] = $entryIdentifier;
180 $tagRow[] = $tag;
181 $tagRows[] = $tagRow;
182 }
183
184 $GLOBALS['TYPO3_DB']->exec_INSERTmultipleRows(
185 $this->tagsTable,
186 $fields,
187 $tagRows
188 );
189 }
190 }
191
192 /**
193 * Loads data from a cache file.
194 *
195 * @param string An identifier which describes the cache entry to load
196 * @return mixed The cache entry's data as a string or FALSE if the cache entry could not be loaded
197 */
198 public function get($entryIdentifier) {
199 $this->throwExceptionIfFrontendDoesNotExist();
200
201 $cacheEntry = FALSE;
202
203 $cacheEntry = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
204 'content',
205 $this->cacheTable,
206 'identifier = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($entryIdentifier, $this->cacheTable) .
207 ' AND ' . $this->notExpiredStatement
208 );
209
210 if (is_array($cacheEntry)) {
211 $cacheEntry = $cacheEntry['content'];
212 }
213
214 if ($this->compression && strlen($cacheEntry)) {
215 $cacheEntry = gzuncompress($cacheEntry);
216 }
217
218 return $cacheEntry;
219 }
220
221 /**
222 * Checks if a cache entry with the specified identifier exists.
223 *
224 * @param string Specifies the identifier to check for existence
225 * @return boolean TRUE if such an entry exists, FALSE if not
226 */
227 public function has($entryIdentifier) {
228 $this->throwExceptionIfFrontendDoesNotExist();
229
230 $hasEntry = FALSE;
231
232 $cacheEntries = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows(
233 '*',
234 $this->cacheTable,
235 'identifier = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($entryIdentifier, $this->cacheTable) .
236 ' AND ' . $this->notExpiredStatement
237 );
238 if ($cacheEntries >= 1) {
239 $hasEntry = TRUE;
240 }
241
242 return $hasEntry;
243 }
244
245 /**
246 * Removes all cache entries matching the specified identifier.
247 * Usually this only affects one entry.
248 *
249 * @param string Specifies the cache entry to remove
250 * @return boolean TRUE if (at least) an entry could be removed or FALSE if no entry was found
251 */
252 public function remove($entryIdentifier) {
253 $this->throwExceptionIfFrontendDoesNotExist();
254
255 $entryRemoved = FALSE;
256
257 $res = $GLOBALS['TYPO3_DB']->exec_DELETEquery(
258 $this->cacheTable,
259 'identifier = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($entryIdentifier, $this->cacheTable)
260 );
261
262 $GLOBALS['TYPO3_DB']->exec_DELETEquery(
263 $this->tagsTable,
264 'identifier = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($entryIdentifier, $this->tagsTable)
265 );
266
267 if ($GLOBALS['TYPO3_DB']->sql_affected_rows($res) == 1) {
268 $entryRemoved = TRUE;
269 }
270
271 return $entryRemoved;
272 }
273
274 /**
275 * Finds and returns all cache entries which are tagged by the specified tag.
276 *
277 * @param string The tag to search for
278 * @return array An array with identifiers of all matching entries. An empty array if no entries matched
279 */
280 public function findIdentifiersByTag($tag) {
281 $this->throwExceptionIfFrontendDoesNotExist();
282
283 $cacheEntryIdentifiers = array();
284
285 $cacheEntryIdentifierRows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
286 $this->identifierField,
287 $this->tableList,
288 $this->tagsTable . '.tag = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($tag, $this->tagsTable) .
289 ' AND ' . $this->tableJoin .
290 ' AND ' . $this->notExpiredStatement,
291 $this->identifierField
292 );
293
294 foreach ($cacheEntryIdentifierRows as $cacheEntryIdentifierRow) {
295 $cacheEntryIdentifiers[$cacheEntryIdentifierRow['identifier']] = $cacheEntryIdentifierRow['identifier'];
296 }
297
298 return $cacheEntryIdentifiers;
299 }
300
301 /**
302 * Removes all cache entries of this cache.
303 *
304 * @return void
305 */
306 public function flush() {
307 $this->throwExceptionIfFrontendDoesNotExist();
308
309 $GLOBALS['TYPO3_DB']->exec_TRUNCATEquery($this->cacheTable);
310 $GLOBALS['TYPO3_DB']->exec_TRUNCATEquery($this->tagsTable);
311 }
312
313 /**
314 * Removes all cache entries of this cache which are tagged by the specified tag.
315 *
316 * @param string The tag the entries must have
317 * @return void
318 */
319 public function flushByTag($tag) {
320 $this->throwExceptionIfFrontendDoesNotExist();
321
322 $tagsTableWhereClause = $this->tagsTable . '.tag = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($tag, $this->tagsTable);
323
324 $this->deleteCacheTableRowsByTagsTableWhereClause($tagsTableWhereClause);
325
326 $GLOBALS['TYPO3_DB']->exec_DELETEquery(
327 $this->tagsTable,
328 $tagsTableWhereClause
329 );
330 }
331
332 /**
333 * Does garbage collection
334 *
335 * @return void
336 */
337 public function collectGarbage() {
338 $this->throwExceptionIfFrontendDoesNotExist();
339
340 // Get identifiers of expired cache entries
341 $tagsEntryIdentifierRowsResource = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
342 'identifier',
343 $this->cacheTable,
344 $this->expiredStatement
345 );
346
347 $tagsEntryIdentifiers = array();
348 while ($tagsEntryIdentifierRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($tagsEntryIdentifierRowsResource)) {
349 $tagsEntryIdentifiers[] = $GLOBALS['TYPO3_DB']->fullQuoteStr(
350 $tagsEntryIdentifierRow['identifier'],
351 $this->tagsTable
352 );
353 }
354 $GLOBALS['TYPO3_DB']->sql_free_result($tagsEntryIdentifierRowsResource);
355
356 // Delete tag rows connected to expired cache entries
357 if (count($tagsEntryIdentifiers)) {
358 $GLOBALS['TYPO3_DB']->exec_DELETEquery(
359 $this->tagsTable,
360 'identifier IN (' . implode(', ', $tagsEntryIdentifiers) . ')'
361 );
362 }
363
364 // Delete expired cache rows
365 $GLOBALS['TYPO3_DB']->exec_DELETEquery(
366 $this->cacheTable,
367 $this->expiredStatement
368 );
369 }
370
371 /**
372 * Sets the table where the cache entries are stored.
373 *
374 * @deprecated since TYPO3 4.6: The backend calculates the table name internally, this method does nothing anymore
375 * @param string $cacheTable Table name
376 * @return void
377 */
378 public function setCacheTable($cacheTable) {
379 t3lib_div::logDeprecatedFunction();
380 }
381
382 /**
383 * Returns the table where the cache entries are stored.
384 *
385 * @return string The cache table.
386 */
387 public function getCacheTable() {
388 $this->throwExceptionIfFrontendDoesNotExist();
389
390 return $this->cacheTable;
391 }
392
393 /**
394 * Sets the table where cache tags are stored.
395 *
396 * @deprecated since TYPO3 4.6: The backend calculates the table name internally, this method does nothing anymore
397 * @param string $tagsTable: Tags table name
398 * @return void
399 */
400 public function setTagsTable($tagsTable) {
401 t3lib_div::logDeprecatedFunction();
402 }
403
404 /**
405 * Gets the table where cache tags are stored.
406 *
407 * @return string Name of the table storing tags
408 */
409 public function getTagsTable() {
410 $this->throwExceptionIfFrontendDoesNotExist();
411
412 return $this->tagsTable;
413 }
414
415 /**
416 * Enable data compression
417 *
418 * @param boolean TRUE to enable compression
419 */
420 public function setCompression($compression) {
421 $this->compression = $compression;
422 }
423
424 /**
425 * Set data compression level.
426 * If compression is enabled and this is not set,
427 * gzcompress default level will be used
428 *
429 * @param integer -1 to 9: Compression level
430 */
431 public function setCompressionLevel($compressionLevel) {
432 if ($compressionLevel >= -1 && $compressionLevel <= 9) {
433 $this->compressionLevel = $compressionLevel;
434 }
435 }
436
437 /**
438 * Check if required frontend instance exists
439 *
440 * @throws t3lib_cache_Exception If there is no frontend instance in $this->cache
441 * @return void
442 */
443 protected function throwExceptionIfFrontendDoesNotExist() {
444 if (!$this->cache instanceof t3lib_cache_frontend_Frontend) {
445 throw new t3lib_cache_Exception(
446 'No cache frontend has been set via setCache() yet.',
447 1236518288
448 );
449 }
450 }
451
452 /**
453 * Calculate needed table definitions for this cache.
454 * This helper method is used by install tool and extension manager
455 * and is not part of the public API!
456 *
457 * @return string SQL of table definitions
458 */
459 public function getTableDefinitions() {
460 $cacheTableSql = file_get_contents(PATH_t3lib . 'cache/backend/resources/dbbackend-layout-cache.sql');
461 $requiredTableStructures = str_replace('###CACHE_TABLE###', $this->cacheTable, $cacheTableSql) . LF . LF;
462 $tagsTableSql = file_get_contents(PATH_t3lib . 'cache/backend/resources/dbbackend-layout-tags.sql');
463 $requiredTableStructures .= str_replace('###TAGS_TABLE###', $this->tagsTable, $tagsTableSql) . LF;
464 return $requiredTableStructures;
465 }
466
467 /**
468 * Deletes rows in cache table found by where clause on tags table
469 *
470 * @param string The where clause for the tags table
471 * @return void
472 */
473 protected function deleteCacheTableRowsByTagsTableWhereClause($tagsTableWhereClause) {
474 $cacheEntryIdentifierRowsRessource = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
475 'DISTINCT identifier',
476 $this->tagsTable,
477 $tagsTableWhereClause
478 );
479
480 $cacheEntryIdentifiers = array();
481 while ($cacheEntryIdentifierRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($cacheEntryIdentifierRowsRessource)) {
482 $cacheEntryIdentifiers[] = $GLOBALS['TYPO3_DB']->fullQuoteStr(
483 $cacheEntryIdentifierRow['identifier'],
484 $this->cacheTable
485 );
486 }
487 $GLOBALS['TYPO3_DB']->sql_free_result($cacheEntryIdentifierRowsRessource);
488
489 if (count($cacheEntryIdentifiers)) {
490 $GLOBALS['TYPO3_DB']->exec_DELETEquery(
491 $this->cacheTable,
492 'identifier IN (' . implode(', ', $cacheEntryIdentifiers) . ')'
493 );
494 }
495 }
496 }
497 ?>