1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AOE\Crawler\Service; |
6
|
|
|
|
7
|
|
|
/* |
8
|
|
|
* (c) 2020 AOE GmbH <[email protected]> |
9
|
|
|
* |
10
|
|
|
* This file is part of the TYPO3 Crawler Extension. |
11
|
|
|
* |
12
|
|
|
* It is free software; you can redistribute it and/or modify it under |
13
|
|
|
* the terms of the GNU General Public License, either version 2 |
14
|
|
|
* of the License, or any later version. |
15
|
|
|
* |
16
|
|
|
* For the full copyright and license information, please read the |
17
|
|
|
* LICENSE.txt file that was distributed with this source code. |
18
|
|
|
* |
19
|
|
|
* The TYPO3 project - inspiring people to share! |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
use Psr\Http\Message\UriInterface; |
23
|
|
|
use TYPO3\CMS\Core\Http\Uri; |
24
|
|
|
use TYPO3\CMS\Core\Routing\SiteMatcher; |
25
|
|
|
use TYPO3\CMS\Core\Site\Entity\Site; |
26
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
27
|
|
|
use TYPO3\CMS\Frontend\Page\CacheHashCalculator; |
28
|
|
|
|
29
|
|
|
class UrlService |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Build a URL from a Page and the Query String. If the page has a Site configuration, it can be built by using |
33
|
|
|
* the Site instance. |
34
|
|
|
* |
35
|
|
|
* @param int $httpsOrHttp see tx_crawler_configuration.force_ssl |
36
|
|
|
* @throws \TYPO3\CMS\Core\Exception\SiteNotFoundException |
37
|
|
|
* @throws \TYPO3\CMS\Core\Routing\InvalidRouteArgumentsException |
38
|
|
|
*/ |
39
|
17 |
|
public function getUrlFromPageAndQueryParameters(int $pageId, string $queryString, ?string $alternativeBaseUrl, int $httpsOrHttp): UriInterface |
40
|
|
|
{ |
41
|
17 |
|
$site = GeneralUtility::makeInstance(SiteMatcher::class)->matchByPageId((int) $pageId); |
42
|
17 |
|
if ($site instanceof Site) { |
43
|
10 |
|
$queryString = ltrim($queryString, '?&'); |
44
|
10 |
|
$queryParts = []; |
45
|
10 |
|
parse_str($queryString, $queryParts); |
46
|
10 |
|
unset($queryParts['id']); |
47
|
|
|
// workaround as long as we don't have native language support in crawler configurations |
48
|
10 |
|
if (isset($queryParts['L'])) { |
49
|
1 |
|
$queryParts['_language'] = $queryParts['L']; |
50
|
1 |
|
unset($queryParts['L']); |
51
|
1 |
|
$siteLanguage = $site->getLanguageById((int) $queryParts['_language']); |
|
|
|
|
52
|
|
|
} else { |
53
|
9 |
|
$siteLanguage = $site->getDefaultLanguage(); |
54
|
|
|
} |
55
|
10 |
|
$url = $site->getRouter()->generateUri($pageId, $queryParts); |
56
|
10 |
|
if (! empty($alternativeBaseUrl)) { |
57
|
5 |
|
$alternativeBaseUrl = new Uri($alternativeBaseUrl); |
58
|
5 |
|
$url = $url->withHost($alternativeBaseUrl->getHost()); |
59
|
5 |
|
$url = $url->withScheme($alternativeBaseUrl->getScheme()); |
60
|
5 |
|
$url = $url->withPort($alternativeBaseUrl->getPort()); |
61
|
5 |
|
if ($userInfo = $alternativeBaseUrl->getUserInfo()) { |
62
|
10 |
|
$url = $url->withUserInfo($userInfo); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} else { |
66
|
|
|
// Technically this is not possible with site handling, but kept for backwards-compatibility reasons |
67
|
|
|
// Once EXT:crawler is v10-only compatible, this should be removed completely |
68
|
7 |
|
$baseUrl = ($alternativeBaseUrl ?: GeneralUtility::getIndpEnv('TYPO3_SITE_URL')); |
69
|
7 |
|
$cacheHashCalculator = GeneralUtility::makeInstance(CacheHashCalculator::class); |
70
|
7 |
|
$queryString .= '&cHash=' . $cacheHashCalculator->generateForParameters($queryString); |
71
|
7 |
|
$url = rtrim($baseUrl, '/') . '/index.php' . $queryString; |
72
|
7 |
|
$url = new Uri($url); |
73
|
|
|
} |
74
|
|
|
|
75
|
17 |
|
if ($httpsOrHttp === -1) { |
76
|
2 |
|
$url = $url->withScheme('http'); |
77
|
15 |
|
} elseif ($httpsOrHttp === 1) { |
78
|
11 |
|
$url = $url->withScheme('https'); |
79
|
|
|
} |
80
|
|
|
|
81
|
17 |
|
return $url; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|