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