-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.tx_realurl_cachemgmt.php
467 lines (426 loc) · 12.1 KB
/
class.tx_realurl_cachemgmt.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
<?php
/***************************************************************
* Copyright notice
*
* (c) 2008 AOE media
* All rights reserved
*
* This script is part of the Typo3 project. The Typo3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
* A copy is found in the textfile GPL.txt and important notices to the license
* from the author is found in LICENSE.txt distributed with these scripts.
*
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
*
* @author Daniel Poetzinger
* @author Tolleiv Nietsch
*/
/**
* TODO:
- check if internal cache array can improve speed
- move oldlinks to redirects
- check last updatetime of pages
**/
/**
*
* @author Daniel Poetzinger
* @package realurl
* @subpackage realurl
*/
class tx_realurl_cachemgmt {
//cache key values
var $workspaceId;
var $languageId;
//unique path check
var $rootPid;
var $cacheTimeOut = 1000; //timeout in seconds for cache entries
var $useUnstrictCacheWhere = FALSE;
/**
* @var t3lib_DB
*/
protected $dbObj;
static $cache = array();
/**
* Class constructor (PHP4 style)
*
* @param int $workspace
* @param int $languageid
*/
function __construct($workspace, $languageid) {
$this->workspaceId = $workspace;
$this->languageId = $languageid;
$this->useUnstrictCacheWhere = FALSE;
$confArr = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['realurl']);
if (isset($confArr['defaultCacheTimeOut'])) {
$this->setCacheTimeOut($confArr['defaultCacheTimeOut']);
}
$this->dbObj = $GLOBALS['TYPO3_DB'];
}
/**
*
* @param int $rootpid
*/
function setRootPid($rootpid) {
$this->rootPid = $rootpid;
}
/**
*
* @param int $languageid
*/
function setLanguageId($languageid) {
$this->languageId = $languageid;
}
/**
*
* @param int $time - in secounds
*/
function setCacheTimeOut($time) {
$this->cacheTimeOut = intval($time);
}
/**
* @return void
*/
function useUnstrictCacheWhere() {
$this->useUnstrictCacheWhere = TRUE;
}
/**
* @return void
*/
function doNotUseUnstrictCacheWhere() {
$this->useUnstrictCacheWhere = FALSE;
}
/**
* @return int
*/
function getWorkspaceId() {
return $this->workspaceId;
}
/**
* @return int
*/
function getLanguageId() {
return $this->languageId;
}
/**
* @return int
*/
function getRootPid() {
return $this->rootPid;
}
/**
* important function: checks the path in the cache: if not found the check against cache is repeted without the last pathpart
* @param array $pagePathOrigin the path which should be searched in cache
* @param &$keepPath -> passed by reference -> array with the n last pathparts which could not retrieved from cache -> they are propably preVars from translated parameters (like tt_news is etc...)
*
* @return pagid or false
**/
function checkCacheWithDecreasingPath($pagePathOrigin, &$keepPath) {
return $this->_checkACacheTableWithDecreasingPath($pagePathOrigin, $keepPath, FALSE);
}
/**
* important function: checks the path in the cache: if not found the check against cache is repeated without the last pathpart
*
* @param array $pagePathOrigin the path which should be searched in cache
* @param &$keepPath -> passed by reference -> array with the n last pathparts which could not retrieved from cache -> they are propably preVars from translated parameters (like tt_news is etc...) *
* @return pagid or false
**/
function checkHistoryCacheWithDecreasingPath($pagePathOrigin, &$keepPath) {
return $this->_checkACacheTableWithDecreasingPath($pagePathOrigin, $keepPath, TRUE);
}
/**
*
* @see checkHistoryCacheWithDecreasingPath
* @param array $pagePathOrigin
* @param array $keepPath
* @param boolean $inHistoryTable
* @return int
*/
function _checkACacheTableWithDecreasingPath($pagePathOrigin, &$keepPath, $inHistoryTable = FALSE) {
$sizeOfPath = count($pagePathOrigin);
$pageId = false;
for($i = $sizeOfPath; $i > 0; $i--) {
if (!$inHistoryTable) {
$pageId = $this->_readCacheForPath(implode("/", $pagePathOrigin));
} else {
$pageId = $this->_readHistoryCacheForPath(implode("/", $pagePathOrigin));
}
if ($pageId !== false) {
//found something => break;
break;
} else {
array_unshift($keepPath, array_pop($pagePathOrigin));
}
}
return $pageId;
}
/**
* Stores the path in cache and checks if that path is unique, if not this function makes the path unique by adding some numbers
* (throws error if caching fails)
*
* @param string Path
* @return string unique path in cache
*/
function storeUniqueInCache($pid, $buildedPath, $disableCollisionDetection = false) {
$this->dbObj->sql_query('BEGIN');
if ($this->isInCache($pid) === false) {
$this->_checkForCleanupCache($pid, $buildedPath);
//do cleanup of old cache entries:
$_ignore = $pid;
$_workspace = $this->getWorkspaceId();
if ($_workspace > 0) {
$record = t3lib_BEfunc::getLiveVersionOfRecord('pages', $pid, 'uid');
if (!is_array($record)) {
$record = t3lib_BEfunc::getWorkspaceVersionOfRecord($_workspace, 'pages', $pid, '*');
}
if (is_array($record)) {
$_ignore = $record['uid'];
}
}
if ($this->_readCacheForPath($buildedPath, $_ignore) && !$disableCollisionDetection) {
$buildedPath .= '_' . $pid;
}
//do insert
$data['tstamp'] = $GLOBALS['EXEC_TIME'];
$data['path'] = $buildedPath;
$data['mpvar'] = "";
$data['workspace'] = $this->getWorkspaceId();
$data['languageid'] = $this->getLanguageId();
$data['rootpid'] = $this->getRootPid();
$data['pageid'] = $pid;
if ($this->dbObj->exec_INSERTquery("tx_realurl_cache", $data)) {
//TODO ... yeah we saved something in the database - any further problems?
} else {
//TODO ... d'oh database didn't like use - what's next?
}
}
$this->dbObj->sql_query('COMMIT');
return $buildedPath;
}
/**
* checks cache and looks if a path exist (in workspace, rootpid, language)
*
* @param string Path
* @return string unique path in cache
**/
function _readCacheForPath($pagePath, $ignoreUid = null) {
if (is_numeric($ignoreUid)) {
$where = 'path="' . $pagePath . '" AND pageid != "' . intval($ignoreUid) . '" ';
} else {
$where = 'path="' . $pagePath . '" ';
}
$where .= $this->_getAddCacheWhere(TRUE);
if (method_exists($this->dbObj, 'exec_SELECTquery_master')) {
// Force select to use master server in t3p_scalable
$res = $this->dbObj->exec_SELECTquery_master("*", "tx_realurl_cache", $where);
} else {
$res = $this->dbObj->exec_SELECTquery("*", "tx_realurl_cache", $where);
}
if ($res)
$result = $this->dbObj->sql_fetch_assoc($res);
if ($result['pageid']) {
return $result['pageid'];
} else {
return false;
}
}
/**
* checks cache and looks if a path exist (in workspace, rootpid, language)
*
* @param string Path
* @return string unique path in cache
**/
function _readHistoryCacheForPath($pagePath) {
$where = "path=\"" . $pagePath . '"' . $this->_getAddCacheWhere(TRUE);
$res = $this->dbObj->exec_SELECTquery("*", "tx_realurl_cachehistory", $where);
if ($res)
$result = $this->dbObj->sql_fetch_assoc($res);
if ($result['pageid']) {
return $result['pageid'];
} else {
return false;
}
}
/**
* check if a pid has allready a builded path in cache (for workspace,language, rootpid)
*
* @param int $pid
* @return mixed - false or pagepath
*/
function isInCache($pid) {
$row = $this->getCacheRowForPid($pid);
if (!is_array($row)) {
return false;
} else {
if ($this->_isCacheRowStillValid($row)) {
return $row['path'];
} else {
return false;
}
}
}
/**
*
* @param int $pid
* @return array
*/
function getCacheRowForPid($pid) {
$cacheKey = $this->getCacheKey($pid);
if (isset($this->cache[$cacheKey]) && is_array($this->cache[$cacheKey])) {
return $this->cache[$cacheKey];
}
$row = false;
$where = 'pageid=' . intval($pid) . $this->_getAddCacheWhere();
if (method_exists($this->dbObj, 'exec_SELECTquery_master')) {
// Force select to use master server in t3p_scalable
$query = $this->dbObj->exec_SELECTquery_master('*', 'tx_realurl_cache', $where);
} else {
$query = $this->dbObj->exec_SELECTquery('*', 'tx_realurl_cache', $where);
}
if ($query) {
$row = $this->dbObj->sql_fetch_assoc($query);
}
if (is_array($row)) {
$this->cache[$cacheKey] = $row;
}
return $row;
}
/**
*
* @param int $pid
* @return array
*/
function getCacheHistoryRowsForPid($pid) {
$rows = array();
$where = "pageid=" . intval($pid) . $this->_getAddCacheWhere();
$query = $this->dbObj->exec_SELECTquery("*", "tx_realurl_cachehistory", $where);
while ( $row = $this->dbObj->sql_fetch_assoc($query) ) {
$rows[] = $row;
}
return $rows;
}
/**
*
* @param integer $pid
* @param string $newPath
* @return void
*/
function _checkForCleanupCache($pid, $newPath) {
$row = $this->getCacheRowForPid($pid);
if (!is_array($row)) {
return false;
} elseif (!$this->_isCacheRowStillValid($row)) {
if ($newPath != $row['path']) {
$this->insertInCacheHistory($row);
}
$this->_delCacheForPid($row['pageid']);
}
}
/**
*
* @param array $row
* @return boolean
*/
function _isCacheRowStillValid($row) {
$rowIsValid = TRUE;
if ($row['dirty'] == 1) {
$rowIsValid = FALSE;
} elseif (($this->cacheTimeOut > 0) && (($row['tstamp'] + $this->cacheTimeOut) < $GLOBALS['EXEC_TIME'])) {
$rowIsValid = FALSE;
}
return $rowIsValid;
}
/**
*
* @param int $pid
* @return void
*/
function _delCacheForPid($pid) {
$this->cache[$this->getCacheKey($pid)] = false;
$where = "pageid=" . intval($pid) . $this->_getAddCacheWhere();
$this->dbObj->exec_DELETEquery("tx_realurl_cache", $where);
}
/**
*
* @param int $pid
* @return void
*/
function delCacheForCompletePid($pid) {
$where = "pageid=" . intval($pid) . ' AND workspace=' . intval($this->getWorkspaceId());
$this->dbObj->exec_DELETEquery("tx_realurl_cache", $where);
}
/**
*
* @param int $pid
* @return void
*/
function markAsDirtyCompletePid($pid) {
$where = "pageid=" . intval($pid) . ' AND workspace=' . intval($this->getWorkspaceId());
$this->dbObj->exec_UPDATEquery("tx_realurl_cache", $where, array('dirty' => 1));
}
/**
*
* @param array $row
* @return void
*/
function insertInCacheHistory($row) {
unset($row['dirty']);
$row['tstamp'] = $GLOBALS['EXEC_TIME'];
$this->dbObj->exec_INSERTquery("tx_realurl_cachehistory", $row);
}
/**
*
* @return void
*/
function clearAllCache() {
$this->dbObj->exec_DELETEquery("tx_realurl_cache", '1=1');
$this->dbObj->exec_DELETEquery("tx_realurl_cachehistory", '1=1');
}
/**
*
* @return void
*/
function clearAllCacheHistory() {
$this->dbObj->exec_DELETEquery("tx_realurl_cachehistory", '1=1');
}
/**
* get where for cache table selects based on internal vars
*
* @param boolean $withRootPidCheck - is required when selecting for paths -> which should be unique for RootPid
* @return string -where clause
*/
function _getAddCacheWhere($withRootPidCheck = FALSE) {
if ($this->useUnstrictCacheWhere) {
//without the additional keys, thats for compatibility reasons
$where = '';
} else {
$where = ' AND workspace IN (0,' . intval($this->getWorkspaceId()) . ') AND languageid=' . intval($this->getLanguageId());
}
if ($withRootPidCheck) {
$where .= ' AND rootpid=' . intval($this->getRootPid());
}
return $where;
}
/**
* Get cache key
*
* @param int $pid
*/
protected function getCacheKey($pid) {
return implode('-', array($pid, $this->getRootPid(), $this->getWorkspaceId(), $this->getLanguageId()));
}
}
?>