935944bb3ddda184ff8506f4c0e3c98e1c687cc4
[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('The specified data is of type "' . gettype($data) . '" but a string is expected.', 1236518298);
138 }
139 if (is_null($lifetime)) {
140 $lifetime = $this->defaultLifetime;
141 }
142 if ($lifetime === 0 || $lifetime > $this->maximumLifetime) {
143 $lifetime = $this->maximumLifetime;
144 }
145 $expires = $GLOBALS['EXEC_TIME'] + $lifetime;
146 $this->remove($entryIdentifier);
147 if ($this->compression) {
148 $data = gzcompress($data, $this->compressionLevel);
149 }
150 $GLOBALS['TYPO3_DB']->exec_INSERTquery($this->cacheTable, array(
151 'identifier' => $entryIdentifier,
152 'expires' => $expires,
153 'content' => $data
154 ));
155 if (count($tags)) {
156 $fields = array();
157 $fields[] = 'identifier';
158 $fields[] = 'tag';
159 $tagRows = array();
160 foreach ($tags as $tag) {
161 $tagRow = array();
162 $tagRow[] = $entryIdentifier;
163 $tagRow[] = $tag;
164 $tagRows[] = $tagRow;
165 }
166 $GLOBALS['TYPO3_DB']->exec_INSERTmultipleRows($this->tagsTable, $fields, $tagRows);
167 }
168 }
169
170 /**
171 * Loads data from a cache file.
172 *
173 * @param string $entryIdentifier An identifier which describes the cache entry to load
174 * @return mixed The cache entry's data as a string or FALSE if the cache entry could not be loaded
175 */
176 public function get($entryIdentifier) {
177 $this->throwExceptionIfFrontendDoesNotExist();
178
179 $cacheEntry = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('content', $this->cacheTable, 'identifier = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($entryIdentifier, $this->cacheTable) . ' AND ' . $this->notExpiredStatement);
180 if (is_array($cacheEntry)) {
181 $cacheEntry = $cacheEntry['content'];
182 }
183 if ($this->compression && strlen($cacheEntry)) {
184 $cacheEntry = gzuncompress($cacheEntry);
185 }
186 return $cacheEntry !== NULL ? $cacheEntry : FALSE;
187 }
188
189 /**
190 * Checks if a cache entry with the specified identifier exists.
191 *
192 * @param string $entryIdentifier Specifies the identifier to check for existence
193 * @return boolean TRUE if such an entry exists, FALSE if not
194 */
195 public function has($entryIdentifier) {
196 $this->throwExceptionIfFrontendDoesNotExist();
197 $hasEntry = FALSE;
198 $cacheEntries = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*', $this->cacheTable, 'identifier = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($entryIdentifier, $this->cacheTable) . ' AND ' . $this->notExpiredStatement);
199 if ($cacheEntries >= 1) {
200 $hasEntry = TRUE;
201 }
202 return $hasEntry;
203 }
204
205 /**
206 * Removes all cache entries matching the specified identifier.
207 * Usually this only affects one entry.
208 *
209 * @param string $entryIdentifier Specifies the cache entry to remove
210 * @return boolean TRUE if (at least) an entry could be removed or FALSE if no entry was found
211 */
212 public function remove($entryIdentifier) {
213 $this->throwExceptionIfFrontendDoesNotExist();
214 $entryRemoved = FALSE;
215 $res = $GLOBALS['TYPO3_DB']->exec_DELETEquery($this->cacheTable, 'identifier = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($entryIdentifier, $this->cacheTable));
216 $GLOBALS['TYPO3_DB']->exec_DELETEquery($this->tagsTable, 'identifier = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($entryIdentifier, $this->tagsTable));
217 if ($GLOBALS['TYPO3_DB']->sql_affected_rows($res) == 1) {
218 $entryRemoved = TRUE;
219 }
220 return $entryRemoved;
221 }
222
223 /**
224 * Finds and returns all cache entries which are tagged by the specified tag.
225 *
226 * @param string $tag The tag to search for
227 * @return array An array with identifiers of all matching entries. An empty array if no entries matched
228 */
229 public function findIdentifiersByTag($tag) {
230 $this->throwExceptionIfFrontendDoesNotExist();
231 $cacheEntryIdentifiers = array();
232 $cacheEntryIdentifierRows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows($this->identifierField, $this->tableList, $this->tagsTable . '.tag = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($tag, $this->tagsTable) . ' AND ' . $this->tableJoin . ' AND ' . $this->notExpiredStatement, $this->identifierField);
233 foreach ($cacheEntryIdentifierRows as $cacheEntryIdentifierRow) {
234 $cacheEntryIdentifiers[$cacheEntryIdentifierRow['identifier']] = $cacheEntryIdentifierRow['identifier'];
235 }
236 return $cacheEntryIdentifiers;
237 }
238
239 /**
240 * Removes all cache entries of this cache.
241 *
242 * @return void
243 */
244 public function flush() {
245 $this->throwExceptionIfFrontendDoesNotExist();
246 $GLOBALS['TYPO3_DB']->exec_TRUNCATEquery($this->cacheTable);
247 $GLOBALS['TYPO3_DB']->exec_TRUNCATEquery($this->tagsTable);
248 }
249
250 /**
251 * Removes all cache entries of this cache which are tagged by the specified tag.
252 *
253 * @param string $tag The tag the entries must have
254 * @return void
255 */
256 public function flushByTag($tag) {
257 $this->throwExceptionIfFrontendDoesNotExist();
258 $tagsTableWhereClause = $this->tagsTable . '.tag = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($tag, $this->tagsTable);
259 $this->deleteCacheTableRowsByTagsTableWhereClause($tagsTableWhereClause);
260 $GLOBALS['TYPO3_DB']->exec_DELETEquery($this->tagsTable, $tagsTableWhereClause);
261 }
262
263 /**
264 * Does garbage collection
265 *
266 * @return void
267 */
268 public function collectGarbage() {
269 $this->throwExceptionIfFrontendDoesNotExist();
270 // Get identifiers of expired cache entries
271 $tagsEntryIdentifierRowsResource = $GLOBALS['TYPO3_DB']->exec_SELECTquery('identifier', $this->cacheTable, $this->expiredStatement);
272 $tagsEntryIdentifiers = array();
273 while ($tagsEntryIdentifierRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($tagsEntryIdentifierRowsResource)) {
274 $tagsEntryIdentifiers[] = $GLOBALS['TYPO3_DB']->fullQuoteStr($tagsEntryIdentifierRow['identifier'], $this->tagsTable);
275 }
276 $GLOBALS['TYPO3_DB']->sql_free_result($tagsEntryIdentifierRowsResource);
277 // Delete tag rows connected to expired cache entries
278 if (count($tagsEntryIdentifiers)) {
279 $GLOBALS['TYPO3_DB']->exec_DELETEquery($this->tagsTable, 'identifier IN (' . implode(', ', $tagsEntryIdentifiers) . ')');
280 }
281 // Delete expired cache rows
282 $GLOBALS['TYPO3_DB']->exec_DELETEquery($this->cacheTable, $this->expiredStatement);
283 }
284
285 /**
286 * Returns the table where the cache entries are stored.
287 *
288 * @return string The cache table.
289 */
290 public function getCacheTable() {
291 $this->throwExceptionIfFrontendDoesNotExist();
292 return $this->cacheTable;
293 }
294
295 /**
296 * Gets the table where cache tags are stored.
297 *
298 * @return string Name of the table storing tags
299 */
300 public function getTagsTable() {
301 $this->throwExceptionIfFrontendDoesNotExist();
302 return $this->tagsTable;
303 }
304
305 /**
306 * Enable data compression
307 *
308 * @param boolean $compression TRUE to enable compression
309 */
310 public function setCompression($compression) {
311 $this->compression = $compression;
312 }
313
314 /**
315 * Set data compression level.
316 * If compression is enabled and this is not set,
317 * gzcompress default level will be used
318 *
319 * @param integer -1 to 9: Compression level
320 */
321 public function setCompressionLevel($compressionLevel) {
322 if ($compressionLevel >= -1 && $compressionLevel <= 9) {
323 $this->compressionLevel = $compressionLevel;
324 }
325 }
326
327 /**
328 * Check if required frontend instance exists
329 *
330 * @throws \TYPO3\CMS\Core\Cache\Exception If there is no frontend instance in $this->cache
331 * @return void
332 */
333 protected function throwExceptionIfFrontendDoesNotExist() {
334 if (!$this->cache instanceof \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface) {
335 throw new \TYPO3\CMS\Core\Cache\Exception('No cache frontend has been set via setCache() yet.', 1236518288);
336 }
337 }
338
339 /**
340 * Calculate needed table definitions for this cache.
341 * This helper method is used by install tool and extension manager
342 * and is not part of the public API!
343 *
344 * @return string SQL of table definitions
345 */
346 public function getTableDefinitions() {
347 $cacheTableSql = file_get_contents(
348 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('core') .
349 'Resources/Private/Sql/Cache/Backend/Typo3DatabaseBackendCache.sql'
350 );
351 $requiredTableStructures = str_replace('###CACHE_TABLE###', $this->cacheTable, $cacheTableSql) . LF . LF;
352 $tagsTableSql = file_get_contents(
353 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('core') .
354 'Resources/Private/Sql/Cache/Backend/Typo3DatabaseBackendTags.sql'
355 );
356 $requiredTableStructures .= str_replace('###TAGS_TABLE###', $this->tagsTable, $tagsTableSql) . LF;
357 return $requiredTableStructures;
358 }
359
360 /**
361 * Deletes rows in cache table found by where clause on tags table
362 *
363 * @param string $tagsTableWhereClause The where clause for the tags table
364 * @return void
365 */
366 protected function deleteCacheTableRowsByTagsTableWhereClause($tagsTableWhereClause) {
367 $cacheEntryIdentifierRowsResource = $GLOBALS['TYPO3_DB']->exec_SELECTquery('DISTINCT identifier', $this->tagsTable, $tagsTableWhereClause);
368 $cacheEntryIdentifiers = array();
369 while ($cacheEntryIdentifierRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($cacheEntryIdentifierRowsResource)) {
370 $cacheEntryIdentifiers[] = $GLOBALS['TYPO3_DB']->fullQuoteStr($cacheEntryIdentifierRow['identifier'], $this->cacheTable);
371 }
372 $GLOBALS['TYPO3_DB']->sql_free_result($cacheEntryIdentifierRowsResource);
373 if (count($cacheEntryIdentifiers)) {
374 $GLOBALS['TYPO3_DB']->exec_DELETEquery($this->cacheTable, 'identifier IN (' . implode(', ', $cacheEntryIdentifiers) . ')');
375 }
376 }
377
378 }
379
380
381 ?>