-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.tx_realurl_pagepath.php
384 lines (358 loc) · 12.4 KB
/
class.tx_realurl_pagepath.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
<?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 Pötzinger
* @author Tolleiv Nietsch
*/
include_once (t3lib_extMgm::extPath ( 'realurl' ) . 'class.tx_realurl_pathgenerator.php');
include_once (t3lib_extMgm::extPath ( 'realurl' ) . 'class.tx_realurl_cachemgmt.php');
/**
*
* @author Daniel Pötzinger
* @package realurl
* @subpackage realurl
*
* @todo check if internal cache array can improve speed
* @todo move oldlinks to redirects
* @todo check last updatetime of pages
*/
class tx_realurl_pagepath {
var $generator; //help object for generating paths
var $insert = false;
var $pObj;
var $conf;
/**
* @var tx_realurl_cachemgmt
*/
protected $cachemgmt;
/** Main function -> is called from real_url
* parameters and results are in $params (some by reference)
*
* @param array Parameters passed from parent object, "tx_realurl". Some values are passed by reference! (paramKeyValues, pathParts and pObj)
* @param tx_realurl Copy of parent object.
* @return mixed Depends on branching.
*/
public function main($params, $ref) {
// Setting internal variables:
$this->_setParent ( $ref );
$this->_setConf ( $params ['conf'] );
//TODO is this needed ??
srand (); //init rand for cache
$this->initGenerator ();
switch (( string ) $params ['mode']) {
case 'encode' :
$this->initCacheMgm($this->_getLanguageVarEncode());
$path = $this->_id2alias ( $params ['paramKeyValues'] );
$params ['pathParts'] = array_merge ( $params ['pathParts'], $path );
unset ( $params ['paramKeyValues'] ['id'] );
return;
break;
case 'decode' :
$this->initCacheMgm($this->_getLanguageVarDecode());
$id = $this->_alias2id ( $params ['pathParts'] );
return array (
$id,
array ()
);
break;
}
}
/**
* gets the path for a pageid, must store and check the generated path in cache
* (should be aware of workspace)
*
* @param array $paramKeyValues from real_url
* @param array $pathParts from real_url ??
* @return string with path
*/
protected function _id2alias($paramKeyValues) {
$pageId = $paramKeyValues['id'];
if (!is_numeric($pageId) && is_object($GLOBALS ['TSFE']->sys_page)) {
$pageId = $GLOBALS['TSFE']->sys_page->getPageIdFromAlias($pageId );
}
if ($this->_isCrawlerRun() && $GLOBALS['TSFE']->id == $pageId) {
$GLOBALS['TSFE']->applicationData['tx_crawler']['log'][] = 'realurl: _id2alias ' . $pageId . '/' . $this->_getLanguageVarEncode() . '/' . $this->_getWorkspaceId();
//clear this page cache:
$this->cachemgmt->markAsDirtyCompletePid($pageId );
}
$buildedPath = $this->cachemgmt->isInCache($pageId);
if (!$buildedPath) {
$buildPageArray = $this->generator->build($pageId, $this->_getLanguageVarEncode(), $this->_getWorkspaceId() );
$buildedPath = $buildPageArray['path'];
$buildedPath = $this->cachemgmt->storeUniqueInCache($this->generator->getPidForCache(), $buildedPath, $buildPageArray['external'] );
if($this->_isCrawlerRun() && $GLOBALS['TSFE']->id == $pageId) {
$GLOBALS['TSFE']->applicationData['tx_crawler']['log'][] = 'created: ' . $buildedPath . ' pid:' . $pageId . '/' . $this->generator->getPidForCache();
}
}
if ($buildedPath) {
$pagePath_exploded = explode('/', $buildedPath );
return $pagePath_exploded;
} else {
return array();
}
}
/**
* Gets the pageid from a pagepath, needs to check the cache
*
* @param array Array of segments from virtual path
* @return integer Page ID
*/
protected function _alias2id(&$pagePath) {
$pagePathOrigin = $pagePath;
$this->pObj->appendFilePart($pagePathOrigin);
$keepPath = array ();
//Check for redirect
$this->_checkAndDoRedirect ( $pagePathOrigin );
//read cache with the path you get, decrease path if nothing is found
$pageId = $this->cachemgmt->checkCacheWithDecreasingPath ( $pagePathOrigin, $keepPath );
//fallback 1 - use unstrict cache where
/**
* @todo
* @issue http://bugs.aoedev.com/view.php?id=19834
*/
if ($pageId == false) {
$this->cachemgmt->useUnstrictCacheWhere ();
$keepPath = array ();
$pageId = $this->cachemgmt->checkCacheWithDecreasingPath ( $pagePathOrigin, $keepPath );
$this->cachemgmt->doNotUseUnstrictCacheWhere ();
}
//fallback 2 - look in history
if ($pageId == false) {
$keepPath = array ();
$pageId = $this->cachemgmt->checkHistoryCacheWithDecreasingPath ( $pagePathOrigin, $keepPath );
}
$pagePath = $keepPath;
return $pageId;
}
/**
*
* @param string $path
* @return void
*/
protected function _checkAndDoRedirect($path) {
$_params = array();
if (is_array ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['EXT:realurl/class.tx_realurl_pagepath.php']['checkAndDoRedirect'])) {
foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['EXT:realurl/class.tx_realurl_pagepath.php']['checkAndDoRedirect'] as $_funcRef) {
t3lib_div::callUserFunction($_funcRef, $_params, $this);
}
}
}
/**
*
* @return int
*/
protected function _getRootPid() {
// Find the PID where to begin the resolve:
if ($this->conf ['rootpage_id']) { // Take PID from rootpage_id if any:
$pid = intval ( $this->conf ['rootpage_id'] );
} else {
//if not defined in realUrlConfig get 0
$pid = 0;
}
return $pid;
}
/**
* Gets the value of current language
* What needs to happen:
* decode: -the languageid is used by cachemgmt in order to retrieve the correct pid for the given path
* -that means it needs to return the languageid of the current context:
* (means the L parameter value after realurl processing)
*
* encode: - the langugeid is used to build the path + to cache the path
* - if in the url parameters it is forced to generate the url in a specific language it needs to use this (L parameter defined in typolink)
* -
* first it tries to recieve it from the get-parameters directly
* - orig_paramKeyValues is set by realurl during encoding, and it has the L paremeter value that is passed to typolink
*
* @return integer Current language or 0
* @deprecated
* @todo Should be replaced with the new methods - tests "tests/tx_realurl_pagepath_testcase.php"
*/
function _getLanguageVar() {
$lang = FALSE;
$getVarName = $this->conf ['languageGetVar'] ? $this->conf ['languageGetVar'] : 'L';
// Setting the language variable based on GETvar in URL which has been configured to carry the language uid:
if ($getVarName && array_key_exists ( $getVarName, $this->pObj->orig_paramKeyValues )) {
$lang = intval ( $this->pObj->orig_paramKeyValues [$getVarName] );
// Might be excepted (like you should for CJK cases which does not translate to ASCII equivalents)
if (t3lib_div::inList ( $this->conf ['languageExceptionUids'], $lang )) {
$lang = 0;
}
}
if ($lang === FALSE) {
//TODO next line is not covered by a test
$lang = t3lib_div::_GP ( $getVarName );
if ($lang == 0 && method_exists ( $this->pObj, 'getRetrievedPreGetVar' )) {
$lang = intval ( $this->pObj->getRetrievedPreGetVar ( $getVarName ) );
}
}
if ($this->conf ['languageGetVarPostFunc']) {
$lang = t3lib_div::callUserFunction ( $this->conf ['languageGetVarPostFunc'], $lang, $this );
}
return intval ( $lang );
}
/**
* DECODE
* Find the current language id.
*
* The languageid is used by cachemgmt in order to retrieve the correct pid for the given path
* -that means it needs to return the languageid of the current context:
* (means the L parameter value after realurl processing)
*
* @return integer Current language id
*
* @author Michael Klapper <michael.klapper@aoemedia.de>
*/
function _getLanguageVarDecode() {
$getVarName = $this->conf['languageGetVar'] ? $this->conf['languageGetVar'] : 'L';
$lang = $this->pObj->getRetrievedPreGetVar( $getVarName );
if ($this->conf['languageGetVarPostFunc']) {
$lang = t3lib_div::callUserFunction($this->conf['languageGetVarPostFunc'], $lang, $this );
}
return (int)$lang;
}
/**
* ENCODE
* Find the current language id.
*
* The langugeid is used to build the path + to cache the path
* - if in the url parameters it is forced to generate the url in a specific language it needs to use this (L parameter defined in typolink)
*
* - orig_paramKeyValues is set by realurl during encoding, and it has the L paremeter value that is passed to typolink
*
* @return integer Current language id
*
* @author Michael Klapper <michael.klapper@aoemedia.de>
*/
function _getLanguageVarEncode() {
$lang = FALSE;
$getVarName = $this->conf ['languageGetVar'] ? $this->conf ['languageGetVar'] : 'L';
// $orig_paramKeyValues Contains the index of GETvars that the URL had when the encoding began.
// Setting the language variable based on GETvar in URL which has been configured to carry the language uid:
if ($getVarName && array_key_exists($getVarName, $this->pObj->orig_paramKeyValues)) {
$lang = intval($this->pObj->orig_paramKeyValues[$getVarName]);
// Might be excepted (like you should for CJK cases which does not translate to ASCII equivalents)
if (t3lib_div::inList($this->conf['languageExceptionUids'], $lang)) {
$lang = 0;
}
}
if ($this->conf['languageGetVarPostFunc']) {
$lang = t3lib_div::callUserFunction($this->conf['languageGetVarPostFunc'], $lang, $this);
}
return (int)$lang;
}
/**
*
* @return boolean
*/
function _isBELogin() {
if (! is_object ( $GLOBALS ['BE_USER'] ))
return false;
else
return true;
}
/**
* if workspace preview in FE return that workspace
*
* @return int
*/
function _getWorkspaceId() {
if (is_object ( $GLOBALS ['BE_USER'] ) && t3lib_div::_GP ( 'ADMCMD_noBeUser' ) != 1) {
if (is_object ( $GLOBALS ['TSFE']->sys_page )) {
if ($GLOBALS ['TSFE']->sys_page->versioningPreview == 1) {
return $GLOBALS ['TSFE']->sys_page->versioningWorkspaceId;
}
} else {
if ($GLOBALS ['BE_USER']->user ['workspace_preview'] == 1) {
return $GLOBALS ['BE_USER']->workspace;
}
}
}
return 0;
}
/**
* returns true/false if the current context is within a crawler call (procInstr. tx_cachemgm_recache)
* This is used for some logging. The status is cached for performance reasons
*
* @return boolean
*/
function _isCrawlerRun() {
if (
t3lib_extMgm::isLoaded('crawler')
&& $GLOBALS['TSFE']->applicationData['tx_crawler']['running']
&& (
in_array('tx_cachemgm_recache', $GLOBALS['TSFE']->applicationData['tx_crawler']['parameters']['procInstructions'])
|| in_array('tx_realurl_rebuild', $GLOBALS['TSFE']->applicationData['tx_crawler']['parameters']['procInstructions'])
)
) {
return true;
} else {
return false;
}
}
/**
* assigns the configuration
*
* @param $conf
* @return void
*/
function _setConf($conf) {
//TODO: validate the incoming conf
$this->conf = $conf;
}
/**
* assigns the parent object
*
* @param tx_realurl $ref: the parent object
* @return void
*/
function _setParent($ref) {
$this->pObj = &$ref;
}
/**
* Initialize the pathgenerator
*
*/
function initGenerator() {
$this->generator = t3lib_div::makeInstance ( 'tx_realurl_pathgenerator' );
$this->generator->init ( $this->conf );
$this->generator->setRootPid ( $this->_getRootPid () );
}
/**
* Initialize the Cache-Layer
*
* @param integer $lang Current language value
* @return void
*/
function initCacheMgm($lang) {
$this->cachemgmt = t3lib_div::makeInstance('tx_realurl_cachemgmt', $this->_getWorkspaceId (), $lang );
$this->cachemgmt->setCacheTimeout ( $this->conf ['cacheTimeOut'] );
$this->cachemgmt->setRootPid ( $this->_getRootPid () );
}
}
?>