|
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
|
|
|
public function getUrlFromPageAndQueryParameters(int $pageId, string $queryString, ?string $alternativeBaseUrl, int $httpsOrHttp): UriInterface |
|
40
|
|
|
{ |
|
41
|
|
|
$site = GeneralUtility::makeInstance(SiteMatcher::class)->matchByPageId((int) $pageId); |
|
42
|
|
|
if ($site instanceof Site) { |
|
43
|
|
|
$queryString = ltrim($queryString, '?&'); |
|
44
|
|
|
$queryParts = []; |
|
45
|
|
|
parse_str($queryString, $queryParts); |
|
46
|
|
|
unset($queryParts['id']); |
|
47
|
|
|
// workaround as long as we don't have native language support in crawler configurations |
|
48
|
|
|
if (isset($queryParts['L'])) { |
|
49
|
|
|
$queryParts['_language'] = $queryParts['L']; |
|
50
|
|
|
unset($queryParts['L']); |
|
51
|
|
|
$siteLanguage = $site->getLanguageById((int) $queryParts['_language']); |
|
|
|
|
|
|
52
|
|
|
} else { |
|
53
|
|
|
$siteLanguage = $site->getDefaultLanguage(); |
|
54
|
|
|
} |
|
55
|
|
|
$url = $site->getRouter()->generateUri($pageId, $queryParts); |
|
56
|
|
|
if (! empty($alternativeBaseUrl)) { |
|
57
|
|
|
$alternativeBaseUrl = new Uri($alternativeBaseUrl); |
|
58
|
|
|
$url = $url->withHost($alternativeBaseUrl->getHost()); |
|
59
|
|
|
$url = $url->withScheme($alternativeBaseUrl->getScheme()); |
|
60
|
|
|
$url = $url->withPort($alternativeBaseUrl->getPort()); |
|
61
|
|
|
if ($userInfo = $alternativeBaseUrl->getUserInfo()) { |
|
62
|
|
|
$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
|
|
|
$baseUrl = ($alternativeBaseUrl ?: GeneralUtility::getIndpEnv('TYPO3_SITE_URL')); |
|
69
|
|
|
$cacheHashCalculator = GeneralUtility::makeInstance(CacheHashCalculator::class); |
|
70
|
|
|
$queryString .= '&cHash=' . $cacheHashCalculator->generateForParameters($queryString); |
|
71
|
|
|
$url = rtrim($baseUrl, '/') . '/index.php' . $queryString; |
|
72
|
|
|
$url = new Uri($url); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if ($httpsOrHttp === -1) { |
|
76
|
|
|
$url = $url->withScheme('http'); |
|
77
|
|
|
} elseif ($httpsOrHttp === 1) { |
|
78
|
|
|
$url = $url->withScheme('https'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $url; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|