This repository has been archived by the owner on May 7, 2022. It is now read-only.
forked from fbrnc/Realurl
-
Notifications
You must be signed in to change notification settings - Fork 3
/
class.tx_realurl_tcemain.php
375 lines (346 loc) · 10.9 KB
/
class.tx_realurl_tcemain.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
<?php
/***************************************************************
* Copyright notice
*
* (c) 2010 Dmitry Dulepov ([email protected])
* 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!
***************************************************************/
/**
* $Id$
*/
/**
* TCEmain hook to update various caches when data is modified in TYPO3 Backend
*
* @author Dmitry Dulepov <[email protected]>
*/
class tx_realurl_tcemain {
/** @var tx_realurl_apiwrapper */
protected $apiWrapper;
/**
* RealURL configuration for the current host
*
* @var array
*/
protected $config;
public function __construct() {
$this->apiWrapper = tx_realurl_apiwrapper::getInstance();
}
/**
* Removes autoconfiguration file if table name is sys_domain
*
* @param string $tableName
* @return void
*/
protected function clearAutoConfiguration($tableName) {
if ($tableName == 'sys_domain') {
@unlink(PATH_site . TX_REALURL_AUTOCONF_FILE);
}
}
/**
* Clears RealURL caches if necessary
*
* @param string $command
* @param string $tableName
* @param int $recordId
* @return void
*/
protected function clearCaches($command, $tableName, $recordId) {
if ($this->isTableForCache($tableName)) {
if ($command == 'delete' || $command == 'move') {
list($pageId, ) = $this->getPageData($tableName, $recordId);
$this->fetchRealURLConfiguration($pageId);
if ($command == 'delete') {
$this->clearPathCache($pageId);
}
else {
$this->expirePathCacheForAllLanguages($pageId);
}
$this->clearOtherCaches($pageId);
}
}
}
/**
* Clears URL decode and encode caches for the given page
*
* @param $pageId
* @return void
*/
protected function clearOtherCaches($pageId) {
/** @noinspection PhpUndefinedMethodInspection */
$GLOBALS['TYPO3_DB']->exec_DELETEquery('tx_realurl_urldecodecache',
'page_id=' . intval($pageId));
/** @noinspection PhpUndefinedMethodInspection */
$GLOBALS['TYPO3_DB']->exec_DELETEquery('tx_realurl_urlencodecache',
'page_id=' . intval($pageId));
}
/**
* Clears path cache for the given page id
*
* @param int $pageId
* @return void
*/
protected function clearPathCache($pageId) {
/** @noinspection PhpUndefinedMethodInspection */
$GLOBALS['TYPO3_DB']->exec_DELETEquery('tx_realurl_pathcache',
'page_id=' . intval($pageId));
}
/**
* Removes unique alias in case if the record is deleted from the table
*
* @param string $command
* @param string $tableName
* @param mixed $recordId
* @return void
*/
protected function clearUniqueAlias($command, $tableName, $recordId) {
if ($command == 'delete') {
/** @noinspection PhpUndefinedMethodInspection */
$GLOBALS['TYPO3_DB']->exec_DELETEquery('tx_realurl_uniqalias',
'tablename=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($tableName, 'tx_realurl_uniqalias') .
' AND value_id=' . intval($recordId));
}
}
/**
* Expires record in the path cache
*
* @param int $pageId
* @param int $languageId
* @return void
*/
protected function expirePathCache($pageId, $languageId) {
$expirationTime = $this->getExpirationTime();
$pageIds = $this->getChildPages($pageId);
$pageIds[] = intval($pageId);
/** @noinspection PhpUndefinedMethodInspection */
$GLOBALS['TYPO3_DB']->exec_UPDATEquery('tx_realurl_pathcache',
'page_id IN (' . implode(',', $pageIds) . ') AND language_id=' . intval($languageId) . ' AND expire=0',
array(
'expire' => $expirationTime
));
}
/**
* Expires record in the path cache
*
* @param int $pageId
* @return void
*/
protected function expirePathCacheForAllLanguages($pageId) {
$expirationTime = $this->getExpirationTime();
$pageIds = $this->getChildPages($pageId);
$pageIds[] = intval($pageId);
/** @noinspection PhpUndefinedMethodInspection */
$GLOBALS['TYPO3_DB']->exec_UPDATEquery('tx_realurl_pathcache',
'page_id IN (' . implode(',', $pageIds) . ') AND expire=0',
array(
'expire' => $expirationTime
));
}
/**
* Fetches RealURl configuration for the given page
*
* @param int $pageId
* @return void
*/
protected function fetchRealURLConfiguration($pageId) {
$rootLine = $this->apiWrapper->BEgetRootLine($pageId);
$rootPageId = $rootLine[1]['uid'];
$this->config = array();
if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['realurl'])) {
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['realurl'] as $config) {
if (is_array($config) && $config['pagePath']['rootpage_id'] == $rootPageId) {
$this->config = $config;
return;
}
}
if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['realurl']['_DEFAULT'])) {
$this->config = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['realurl']['_DEFAULT'];
}
}
else {
$this->apiWrapper->sysLog('RealURL is not configured! Please, configure it or uninstall.', 'RealURL', 3);
}
}
/**
* Returns the IDs of all child pages of a given $pageID.
*
* @param $pageId integer Page ID to start searching
* @return int[] Child pages
*/
protected function getChildPages($pageId) {
$children = array();
$tree = $this->apiWrapper->makePageTreeInstance();
/** @noinspection PhpUndefinedMethodInspection */
$tree->init('AND ' . $GLOBALS['BE_USER']->getPagePermsClause(1));
$tree->makeHTML = FALSE;
$tree->getTree($pageId, 99, '');
foreach ($tree->tree as $data) {
$children[] = intval($data['row']['uid']);
}
return $children;
}
/**
* Obtains expiration time for the path cache records
*
* @return int
*/
protected function getExpirationTime() {
$timeOffset = (isset($this->config['pagePath']['expireDays']) ? $this->config['pagePath']['expireDays'] : 60) * 24 * 3600;
$date = getdate(time() + $timeOffset);
return mktime(0, 0, 0, $date['mon'], $date['mday'], $date['year']);
}
/**
* Obtains real page id and language from the table name and passed id of the record in the table.
*
* @param $tableName
* @param $id
* @return array First member is page id, second is language
*/
protected static function getPageData($tableName, $id) {
if ($tableName == 'pages_language_overlay') {
$result = self::getInfoFromOverlayPid($id);
}
else {
$result = array($id, 0);
}
return $result;
}
/**
* Retrieves field list to check for modification
*
* @param string $tableName
* @return array
*/
protected function getFieldList($tableName) {
if ($tableName == 'pages_language_overlay') {
$fieldList = TX_REALURL_SEGTITLEFIELDLIST_PLO;
}
else {
if (isset($this->config['pagePath']['segTitleFieldList'])) {
$fieldList = $this->config['pagePath']['segTitleFieldList'];
}
else {
$fieldList = TX_REALURL_SEGTITLEFIELDLIST_DEFAULT;
}
}
$fieldList .= ',hidden';
return array_unique($this->apiWrapper->trimExplode(',', $fieldList, true));
}
/**
* Retrieves real page id given its overlay id
*
* @param int $pid Page id
* @return array Array with two members: real page uid and sys_language_uid
*/
protected static function getInfoFromOverlayPid($pid) {
/** @noinspection PhpUndefinedMethodInspection */
list($rec) = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('pid,sys_language_uid',
'pages_language_overlay', 'uid=' . intval($pid));
return array($rec['pid'], $rec['sys_language_uid']);
}
/**
* Checks if the update table can affect cache entries
*
* @param string $tableName
* @return boolean
*/
protected static function isTableForCache($tableName) {
return ($tableName == 'pages' || $tableName == 'pages_language_overlay');
}
/**
* A TCEMain hook to update caches when something happens to a page or
* language overlay.
*
* @param string $command
* @param string $tableName
* @param mixed $recordId
* @return void
*/
public function processCmdmap_postProcess($command, $tableName, $recordId) {
$this->clearCaches($command, $tableName, $recordId);
$this->clearAutoConfiguration($tableName);
$this->clearUniqueAlias($command, $tableName, $recordId);
}
/**
* A TCEmain hook to expire old records and add new ones
*
* @param string $status 'new' (ignoring) or 'update'
* @param string $tableName
* @param int $recordId
* @param array $databaseData
* @param t3lib_TCEmain $pObj
* @return void
* @todo Expire unique alias cache: how to get the proper timeout value easily here?
*/
public function processDatamap_afterDatabaseOperations($status, $tableName, $recordId, array $databaseData, $pObj) {
$this->processContentUpdates($status, $tableName, $recordId, $databaseData, $pObj);
$this->clearAutoConfiguration($tableName);
}
/**
* Processes page and content changes in regard to RealURL caches.
*
* @param string $status
* @param string $tableName
* @param int $recordId
* @param array $databaseData
* @param t3lib_TCEmain $pObj
* @return void
* @todo Handle changes to tx_realurl_exclude recursively
*/
protected function processContentUpdates($status, $tableName, $recordId, array $databaseData, $pObj) {
if ($tableName !== 'pages' || $status == 'update') {
if (is_numeric($recordId)) {
$recordId = intval($pObj->substNEWwithIDs[$recordId]);
}
list($pageId, $languageId) = $this->getPageData($tableName, $recordId);
$this->fetchRealURLConfiguration($pageId);
if ($this->shouldFixCaches($tableName, $databaseData)) {
if (isset($databaseData['alias'])) {
$this->expirePathCacheForAllLanguages($pageId);
}
else {
$this->expirePathCache($pageId, $languageId);
}
$this->clearOtherCaches($pageId);
}
$this->clearOtherCaches($pageId);
}
}
/**
* Checks if we need to fix caches
*
* @param string $tableName
* @param array $databaseData
* @return boolean
*/
protected function shouldFixCaches($tableName, array $databaseData) {
$result = false;
if (self::isTableForCache($tableName)) {
$interestingFields = $this->getFieldList($tableName);
$result = count(array_intersect($interestingFields, array_keys($databaseData))) > 0;
}
return $result;
}
}
if (defined('TYPO3_MODE') && $GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/realurl/class.tx_realurl_tcemain.php']) {
/** @noinspection PhpIncludeInspection */
include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/realurl/class.tx_realurl_tcemain.php']);
}