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