• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tomasnorre / crawler / 11237471329

08 Oct 2024 02:20PM UTC coverage: 68.586% (-1.3%) from 69.862%
11237471329

push

github

web-flow
ci: Update coveralls workflow (#1109)

1834 of 2674 relevant lines covered (68.59%)

3.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.0
/Classes/Service/UrlService.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace AOE\Crawler\Service;
6

7
/*
8
 * (c) 2021 Tomas Norre Mikkelsen <tomasnorre@gmail.com>
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
use Doctrine\DBAL\Driver\Exception;
22
use Psr\Http\Message\UriInterface;
23
use TYPO3\CMS\Core\Database\ConnectionPool;
24
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
25
use TYPO3\CMS\Core\Http\Uri;
26
use TYPO3\CMS\Core\Routing\InvalidRouteArgumentsException;
27
use TYPO3\CMS\Core\Routing\SiteMatcher;
28
use TYPO3\CMS\Core\Site\Entity\Site;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30

31
/**
32
 * @internal since v9.2.5
33
 */
34
class UrlService
35
{
36
    /**
37
     * Build a URL from a Page and the Query String. If the page has a Site configuration, it can be built by using
38
     * the Site instance.
39
     *
40
     * @param int $httpsOrHttp see tx_crawler_configuration.force_ssl
41
     * @throws SiteNotFoundException
42
     * @throws InvalidRouteArgumentsException
43
     * @throws Exception
44
     */
45
    public function getUrlFromPageAndQueryParameters(
46
        int $pageId,
47
        string $queryString,
48
        ?string $alternativeBaseUrl,
49
        int $httpsOrHttp
50
    ): ?UriInterface {
51
        $url = new Uri();
13✔
52
        $site = GeneralUtility::makeInstance(SiteMatcher::class)->matchByPageId($pageId);
13✔
53
        if ($site instanceof Site) {
13✔
54
            $queryString = ltrim($queryString, '?&');
10✔
55
            $queryParts = [];
10✔
56
            parse_str($queryString, $queryParts);
10✔
57
            unset($queryParts['id']);
10✔
58

59
            if (isset($queryParts['L']) && !empty($queryParts['L'])) {
10✔
60
                $requestedLanguage = $site->getLanguageById((int) $queryParts['L']);
1✔
61
                $languages = array_merge([(int) $queryParts['L']], $requestedLanguage->getFallbackLanguageIds());
1✔
62

63
                $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
1✔
64
                $query = $queryBuilder->select('*')
1✔
65
                    ->from('pages')
1✔
66
                    ->orWhere(
1✔
67
                        $queryBuilder->expr()->eq('uid', $pageId),
1✔
68
                        $queryBuilder->expr()->eq('l10n_parent', $pageId)
1✔
69
                    )->andWhere($queryBuilder->expr()->in('sys_language_uid', $languages))->executeQuery();
1✔
70
                $rows = $query->fetchAssociative();
1✔
71

72
                if (empty($rows)) {
1✔
73
                    return null;
×
74
                }
75
            }
76

77
            // workaround as long as we don't have native language support in crawler configurations
78
            if (isset($queryParts['L'])) {
10✔
79
                $queryParts['_language'] = $queryParts['L'];
1✔
80
                unset($queryParts['L']);
1✔
81
                $siteLanguage = $site->getLanguageById((int) $queryParts['_language']);
1✔
82
            } else {
83
                $siteLanguage = $site->getDefaultLanguage();
9✔
84
            }
85
            $url = $site->getRouter()->generateUri($pageId, $queryParts);
10✔
86
            if (!empty($alternativeBaseUrl)) {
10✔
87
                $alternativeBaseUrl = new Uri($alternativeBaseUrl);
7✔
88
                $url = $url->withHost($alternativeBaseUrl->getHost());
7✔
89
                $url = $url->withScheme($alternativeBaseUrl->getScheme());
7✔
90
                $url = $url->withPort($alternativeBaseUrl->getPort());
7✔
91
                if ($userInfo = $alternativeBaseUrl->getUserInfo()) {
7✔
92
                    $url = $url->withUserInfo($userInfo);
×
93
                }
94
            }
95
        }
96

97
        if ($httpsOrHttp === -1) {
13✔
98
            $url = $url->withScheme('http');
×
99
        } elseif ($httpsOrHttp === 1) {
13✔
100
            $url = $url->withScheme('https');
5✔
101
        }
102

103
        return $url;
13✔
104
    }
105

106
    /**
107
     * Compiling URLs from parameter array (output of expandParameters())
108
     * The number of URLs will be the multiplication of the number of parameter values for each key
109
     *
110
     * @param array $paramArray Output of expandParameters(): Array with keys (GET var names) and for each an array of values
111
     * @param array $urls URLs accumulated in this array (for recursion)
112
     */
113
    public function compileUrls(array $paramArray, array $urls, int $maxUrlToCompile = 10): array
114
    {
115
        if (empty($paramArray)) {
13✔
116
            return $urls;
13✔
117
        }
118
        $varName = key($paramArray);
6✔
119
        $valueSet = array_shift($paramArray);
6✔
120

121
        // Traverse value set:
122
        $newUrls = [];
6✔
123
        foreach ($urls as $url) {
6✔
124
            foreach ($valueSet as $val) {
5✔
125
                if (count($newUrls) < $maxUrlToCompile) {
5✔
126
                    $newUrls[] = $url . (strcmp((string) $val, '') ? '&' . rawurlencode($varName) . '=' . rawurlencode(
5✔
127
                        (string) $val
5✔
128
                    ) : '');
5✔
129
                }
130
            }
131
        }
132
        return $this->compileUrls($paramArray, $newUrls, $maxUrlToCompile);
6✔
133
    }
134
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc