Passed
Pull Request — release-11.2.x (#3604)
by Markus
32:12 queued 27:55
created

RoutingService::cleanUpQueryParameters()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
rs 10
c 0
b 0
f 0
ccs 0
cts 6
cp 0
cc 3
nc 4
nop 1
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace ApacheSolrForTypo3\Solr\Routing;
19
20
use ApacheSolrForTypo3\Solr\Routing\Enhancer\SolrRouteEnhancerInterface;
21
use Psr\Http\Message\ServerRequestInterface;
22
use Psr\Http\Message\UriInterface;
23
use Psr\Log\LoggerAwareInterface;
24
use Psr\Log\LoggerAwareTrait;
25
use TYPO3\CMS\Core\Context\Context;
26
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
27
use TYPO3\CMS\Core\Http\Uri;
28
use TYPO3\CMS\Core\Routing\PageSlugCandidateProvider;
29
use TYPO3\CMS\Core\Routing\SiteMatcher;
30
use TYPO3\CMS\Core\Site\Entity\NullSite;
31
use TYPO3\CMS\Core\Site\Entity\Site;
32
use TYPO3\CMS\Core\Site\Entity\SiteInterface;
33
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
34
use TYPO3\CMS\Core\Site\SiteFinder;
35
use TYPO3\CMS\Core\Utility\GeneralUtility;
36
37
/**
38
 * This service class bundles method required to process and manipulate routes.
39
 *
40
 * @author Lars Tode <[email protected]>
41
 */
42
class RoutingService implements LoggerAwareInterface
43
{
44
    use LoggerAwareTrait;
45
46
    /**
47
     * Default plugin namespace
48
     */
49
    const PLUGIN_NAMESPACE = 'tx_solr';
50
51
    /**
52
     * Settings from routing configuration
53
     *
54
     * @var array
55
     */
56
    protected $settings = [];
57
58
    /**
59
     * List of filter that are placed as path arguments
60
     *
61
     * @var array
62
     */
63
    protected $pathArguments = [];
64
65
    /**
66
     * Plugin/extension namespace
67
     *
68
     * @var string
69
     */
70
    protected $pluginNamespace = 'tx_solr';
71
72
    /**
73
     * List of TYPO3 core parameters, that we should ignore
74
     *
75
     * @see \TYPO3\CMS\Frontend\Page\CacheHashCalculator::isCoreParameter
76
     * @var string[]
77
     */
78
    protected $coreParameters = ['no_cache', 'cHash', 'id', 'MP', 'type'];
79
80
    /**
81
     * @var UrlFacetService
82
     */
83
    protected $urlFacetPathService;
84
85
    /**
86
     * @var UrlFacetService
87
     */
88
    protected $urlFacetQueryService;
89
90
    /**
91
     * RoutingService constructor.
92
     *
93
     * @param array $settings
94
     * @param string $pluginNamespace
95
     */
96
    public function __construct(array $settings = [], string $pluginNamespace = self::PLUGIN_NAMESPACE)
97
    {
98
        $this->settings = $settings;
99
        $this->pluginNamespace = $pluginNamespace;
100
        if (empty($this->pluginNamespace)) {
101
            $this->pluginNamespace = self::PLUGIN_NAMESPACE;
102
        }
103
        $this->initUrlFacetService();
104
    }
105
106
    /**
107
     * Creates a clone of the current service and replace the settings inside
108
     *
109
     * @param array $settings
110
     * @return RoutingService
111
     */
112
    public function withSettings(array $settings): RoutingService
113
    {
114
        $service = clone $this;
115
        $service->settings = $settings;
116
        $service->initUrlFacetService();
117
        return $service;
118
    }
119
120
    /**
121
     * Creates a clone of the current service and replace the settings inside
122
     *
123
     * @param array $pathArguments
124
     * @return RoutingService
125
     */
126
    public function withPathArguments(array $pathArguments): RoutingService
127
    {
128
        $service = clone $this;
129
        $service->pathArguments = $pathArguments;
130
        $service->initUrlFacetService();
131
        return $service;
132
    }
133
134
    /**
135
     * Load configuration from routing configuration
136
     *
137
     * @param array $routingConfiguration
138
     * @return $this
139
     */
140
    public function fromRoutingConfiguration(array $routingConfiguration): RoutingService
141
    {
142
        if (empty($routingConfiguration) ||
143
            empty($routingConfiguration['type']) ||
144
            !$this->isRouteEnhancerForSolr((string)$routingConfiguration['type'])) {
145
            return $this;
146
        }
147
148
        if (isset($routingConfiguration['solr'])) {
149
            $this->settings = $routingConfiguration['solr'];
150
            $this->initUrlFacetService();
151
        }
152
153
        if (isset($routingConfiguration['_arguments'])) {
154
            $this->pathArguments = $routingConfiguration['_arguments'];
155
        }
156
157
        return $this;
158
    }
159
160
    /**
161
     * Reset the routing service
162
     *
163
     * @return $this
164
     */
165
    public function reset(): RoutingService
166
    {
167
        $this->settings = [];
168
        $this->pathArguments = [];
169
        $this->pluginNamespace = self::PLUGIN_NAMESPACE;
170
        return $this;
171
    }
172
173
    /**
174
     * Initialize url facet services for different types
175
     *
176
     * @return $this
177
     */
178
    protected function initUrlFacetService(): RoutingService
179
    {
180
        $this->urlFacetPathService = new UrlFacetService('path', $this->settings);
181
        $this->urlFacetQueryService = new UrlFacetService('query', $this->settings);
182
183
        return $this;
184
    }
185
186
    /**
187
     * @return UrlFacetService
188
     */
189
    public function getUrlFacetPathService(): UrlFacetService
190
    {
191
        return $this->urlFacetPathService;
192
    }
193
194
    /**
195
     * @return UrlFacetService
196
     */
197
    public function getUrlFacetQueryService(): UrlFacetService
198
    {
199
        return $this->urlFacetQueryService;
200
    }
201
202
    /**
203
     * Test if the given parameter is a Core parameter
204
     *
205
     * @see \TYPO3\CMS\Frontend\Page\CacheHashCalculator::isCoreParameter
206
     * @param string $parameterName
207
     * @return bool
208
     */
209
    public function isCoreParameter(string $parameterName): bool
210
    {
211
        return in_array($parameterName, $this->coreParameters);
212
    }
213
214
    /**
215
     * This returns the plugin namespace
216
     * @see https://docs.typo3.org/p/apache-solr-for-typo3/solr/master/en-us/Configuration/Reference/TxSolrView.html#pluginnamespace
217
     *
218
     * @return string
219
     */
220
    public function getPluginNamespace(): string
221
    {
222
        return $this->pluginNamespace;
223
    }
224
225
    /**
226
     * Determine if an enhancer is in use for Solr
227
     *
228
     * @param string $enhancerName
229
     * @return bool
230
     */
231
    public function isRouteEnhancerForSolr(string $enhancerName): bool
232
    {
233
        if (empty($enhancerName)) {
234
            return false;
235
        }
236
237
        if (!isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['enhancers'][$enhancerName])) {
238
            return false;
239
        }
240
        $className = $GLOBALS['TYPO3_CONF_VARS']['SYS']['routing']['enhancers'][$enhancerName];
241
242
        if (!class_exists($className)) {
243
            return false;
244
        }
245
246
        $interfaces = class_implements($className);
247
248
        return in_array(SolrRouteEnhancerInterface::class, $interfaces);
249
    }
250
251
    /**
252
     * Masks Solr filter inside of the query parameters
253
     *
254
     * @param string $uriPath
255
     * @return string
256
     */
257
    public function finalizePathQuery(string $uriPath): string
258
    {
259
        $pathSegments = explode('/', $uriPath);
260
        $query = array_pop($pathSegments);
261
        $queryValues = explode($this->urlFacetPathService->getMultiValueSeparator(), $query);
262
        $queryValues = array_map([$this->urlFacetPathService, 'decodeSingleValue'], $queryValues);
263
        /*
264
         * In some constellations the path query contains the facet type in front.
265
         * This leads to the result, that the query values could contain the same facet value multiple times.
266
         *
267
         * In order to avoid this behaviour, the query values need to be checked and clean up.
268
         * 1. Remove possible prefix information
269
         * 2. Apply character replacements
270
         * 3. Filter duplicate values
271
         */
272
        $queryValuesCount = count($queryValues);
273
        for ($i = 0; $i < $queryValuesCount; $i++) {
274
            $queryValues[$i] = urldecode($queryValues[$i]);
275
            if ($this->containsFacetAndValueSeparator((string)$queryValues[$i])) {
276
                [$facetName, $facetValue] = explode(
277
                    $this->detectFacetAndValueSeparator((string)$queryValues[$i]),
278
                    (string)$queryValues[$i],
279
                    2
280
                );
281
282
                if ($this->isPathArgument((string)$facetName)) {
283
                    $queryValues[$i] = $facetValue;
284
                }
285
            }
286
            $queryValues[$i] = $this->urlFacetPathService->applyCharacterMap($queryValues[$i]);
287
        }
288
289
        $queryValues = array_unique($queryValues);
290
        $queryValues = array_map([$this->urlFacetPathService, 'encodeSingleValue'], $queryValues);
291
        sort($queryValues);
292
        $pathSegments[] = implode(
293
            $this->urlFacetPathService->getMultiValueSeparator(),
294
            $queryValues
295
        );
296
        return implode('/', $pathSegments);
297
    }
298
299
    /**
300
     * This method checks if the query parameter should be masked.
301
     *
302
     * @return bool
303
     */
304
    public function shouldMaskQueryParameter(): bool
305
    {
306
        if (!isset($this->settings['query']['mask']) ||
307
            !(bool)$this->settings['query']['mask']) {
308
            return false;
309
        }
310
311
        $targetFields = $this->getQueryParameterMap();
312
313
        return !empty($targetFields);
314
    }
315
316
    /**
317
     * Masks Solr filter inside of the query parameters
318
     *
319
     * @param array $queryParams
320
     * @return array
321
     */
322 1
    public function maskQueryParameters(array $queryParams): array
323
    {
324 1
        if (!$this->shouldMaskQueryParameter()) {
325
            return $queryParams;
326
        }
327
328 1
        if (!isset($queryParams[$this->getPluginNamespace()])) {
329
            $this->logger
330
                ->/** @scrutinizer ignore-call */
331
                error('Mask error: Query parameters has no entry for namespace ' . $this->getPluginNamespace());
332
            return $queryParams;
333
        }
334
335 1
        if (!isset($queryParams[$this->getPluginNamespace()]['filter']) ||
336 1
            empty($queryParams[$this->getPluginNamespace()]['filter'])) {
337
            $this->logger
338
                ->/** @scrutinizer ignore-call */
339
                info('Mask info: Query parameters has no filter in namespace ' . $this->getPluginNamespace());
340
            return $queryParams;
341
        }
342
343 1
        if (!is_array($queryParams[$this->getPluginNamespace()]['filter'])) {
344
            $this->logger
345
                ->/** @scrutinizer ignore-call */
346
                warning('Mask info: Filter within the Query parameters is not an array');
347
            return $queryParams;
348
        }
349
350 1
        $queryParameterMap = $this->getQueryParameterMap();
351 1
        $newQueryParams = $queryParams;
352
353 1
        $newFilterArray = [];
354 1
        foreach ($newQueryParams[$this->getPluginNamespace()]['filter'] as $queryParamName => $queryParamValue) {
355 1
            $defaultSeparator = $this->detectFacetAndValueSeparator((string)$queryParamValue);
356 1
            [$facetName, $facetValue] = explode($defaultSeparator, $queryParamValue, 2);
357 1
            $keep = false;
358 1
            if (isset($queryParameterMap[$facetName]) &&
359 1
                isset($newQueryParams[$queryParameterMap[$facetName]])) {
360
                $this->logger->/** @scrutinizer ignore-call */error(
361
                    'Mask error: Facet "' . $facetName . '" as "' . $queryParameterMap[$facetName] .
362
                    '" already in query!'
363
                );
364
                $keep = true;
365
            }
366 1
            if (!isset($queryParameterMap[$facetName]) || $keep) {
367
                $newFilterArray[] = $queryParamValue;
368
                continue;
369
            }
370
371 1
            $newQueryParams[$queryParameterMap[$facetName]] = $facetValue;
372
        }
373
374 1
        $newQueryParams[$this->getPluginNamespace()]['filter'] = $newFilterArray;
375
376 1
        return $this->cleanUpQueryParameters($newQueryParams);
377
    }
378
379
    /**
380
     * Unmask incoming parameters if needed
381
     *
382
     * @param array $queryParams
383
     * @return array
384
     */
385
    public function unmaskQueryParameters(array $queryParams): array
386
    {
387
        if (!$this->shouldMaskQueryParameter()) {
388
            return $queryParams;
389
        }
390
391
        /*
392
         * The array $queryParameterMap contains the mapping of
393
         * facet name to new url name. In order to unmask we need to switch key and values.
394
         */
395
        $queryParameterMap = $this->getQueryParameterMap();
396
        $queryParameterMapSwitched = [];
397
        foreach ($queryParameterMap as $value => $key) {
398
            $queryParameterMapSwitched[$key] = $value;
399
        }
400
401
        $newQueryParams = [];
402
        foreach ($queryParams as $queryParamName => $queryParamValue) {
403
            // A merge is needed!
404
            if (!isset($queryParameterMapSwitched[$queryParamName])) {
405
                if (isset($newQueryParams[$queryParamName])) {
406
                    $newQueryParams[$queryParamName] = array_merge_recursive(
407
                        $newQueryParams[$queryParamName],
408
                        $queryParamValue
409
                    );
410
                } else {
411
                    $newQueryParams[$queryParamName] = $queryParamValue;
412
                }
413
                continue;
414
            }
415
            if (!isset($newQueryParams[$this->getPluginNamespace()])) {
416
                $newQueryParams[$this->getPluginNamespace()] = [];
417
            }
418
            if (!isset($newQueryParams[$this->getPluginNamespace()]['filter'])) {
419
                $newQueryParams[$this->getPluginNamespace()]['filter'] = [];
420
            }
421
422
            $newQueryParams[$this->getPluginNamespace()]['filter'][] =
423
                $queryParameterMapSwitched[$queryParamName] . ':' . $queryParamValue;
424
        }
425
426
        return $this->cleanUpQueryParameters($newQueryParams);
427
    }
428
429
    /**
430
     * This method check if the query parameters should be touched or not.
431
     *
432
     * There are following requirements:
433
     * - Masking is activated and the mal is valid or
434
     * - Concat is activated
435
     *
436
     * @return bool
437
     */
438
    public function shouldConcatQueryParameters(): bool
439
    {
440
        /*
441
         * The concat will activate automatically if parameters should be masked.
442
         * This solution is less complex since not every mapping parameter needs to be tested
443
         */
444
        if ($this->shouldMaskQueryParameter()) {
445
            return true;
446
        }
447
448
        return isset($this->settings['query']['concat']) && (bool)$this->settings['query']['concat'];
449
    }
450
451
    /**
452
     * Returns the query parameter map
453
     *
454
     * Note TYPO3 core query arguments removed from the configured map!
455
     *
456
     * @return array
457
     */
458
    public function getQueryParameterMap(): array
459
    {
460
        if (!isset($this->settings['query']['map']) ||
461
            !is_array($this->settings['query']['map']) ||
462
            empty($this->settings['query']['map'])) {
463
            return [];
464
        }
465
        // TODO: Test if there is more than one value!
466
        $self = $this;
467
        return array_filter(
468
            $this->settings['query']['map'],
469
            function ($value) use ($self) {
470
                return !$self->isCoreParameter($value);
471
            }
472
        );
473
    }
474
475
    /**
476
     * Group all filter values together and concat e
477
     * Note: this will just handle filter values
478
     *
479
     * IN:
480
     * tx_solr => [
481
     *   filter => [
482
     *      color:red
483
     *      product:candy
484
     *      color:blue
485
     *      taste:sour
486
     *   ]
487
     * ]
488
     *
489
     * OUT:
490
     * tx_solr => [
491
     *   filter => [
492
     *      color:blue,red
493
     *      product:candy
494
     *      taste:sour
495
     *   ]
496
     * ]
497
     * @param array $queryParams
498
     * @return array
499
     */
500 1
    public function concatQueryParameter(array $queryParams = []): array
501
    {
502 1
        if (!$this->shouldConcatQueryParameters()) {
503
            return $queryParams;
504
        }
505
506 1
        if (!isset($queryParams[$this->getPluginNamespace()])) {
507
            $this->logger
508
                ->error('Mask error: Query parameters has no entry for namespace ' . $this->getPluginNamespace());
0 ignored issues
show
Bug introduced by
The method error() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

508
                ->/** @scrutinizer ignore-call */ 
509
                  error('Mask error: Query parameters has no entry for namespace ' . $this->getPluginNamespace());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
509
            return $queryParams;
510
        }
511
512 1
        if (!isset($queryParams[$this->getPluginNamespace()]['filter']) ||
513 1
            empty($queryParams[$this->getPluginNamespace()]['filter'])) {
514
            $this->logger
515
                ->info('Mask info: Query parameters has no filter in namespace ' . $this->getPluginNamespace());
516
            return $queryParams;
517
        }
518
519 1
        if (!is_array($queryParams[$this->getPluginNamespace()]['filter'])) {
520
            $this->logger
521
                ->/** @scrutinizer ignore-call */
522
                warning('Mask info: Filter within the Query parameters is not an array');
523
            return $queryParams;
524
        }
525
526 1
        $queryParams[$this->getPluginNamespace()]['filter'] =
527 1
            $this->concatFilterValues($queryParams[$this->getPluginNamespace()]['filter']);
528
529 1
        return $this->cleanUpQueryParameters($queryParams);
530
    }
531
532
    /**
533
     * This method expect a filter array that should be concat instead of the whole query
534
     *
535
     * @param array $filterArray
536
     * @return array
537
     */
538
    public function concatFilterValues(array $filterArray): array
539
    {
540
        if (empty($filterArray) || !$this->shouldConcatQueryParameters()) {
541
            return $filterArray;
542
        }
543
544
        $queryParameterMap = $this->getQueryParameterMap();
545
        $newFilterArray = [];
546
        $defaultSeparator = $this->detectFacetAndValueSeparator((string)$filterArray[0]);
547
        // Collect parameter names and rename parameter if required
548
        foreach ($filterArray as $set) {
549
            $separator = $this->detectFacetAndValueSeparator((string)$set);
550
            [$facetName, $facetValue] = explode($separator, $set, 2);
551
            if (isset($queryParameterMap[$facetName])) {
552
                $facetName = $queryParameterMap[$facetName];
553
            }
554
            if (!isset($newFilterArray[$facetName])) {
555
                $newFilterArray[$facetName] = [$facetValue];
556
            } else {
557
                $newFilterArray[$facetName][] = $facetValue;
558
            }
559
        }
560
561
        foreach ($newFilterArray as $facetName => $facetValues) {
562
            $newFilterArray[$facetName] = $facetName . $defaultSeparator . $this->queryParameterFacetsToString($facetValues);
563
        }
564
565
        return array_values($newFilterArray);
566
    }
567
568
    /**
569
     * Inflate given query parameters if configured
570
     * Note: this will just combine filter values
571
     *
572
     * IN:
573
     * tx_solr => [
574
     *   filter => [
575
     *      color:blue,red
576
     *      product:candy
577
     *      taste:sour
578
     *   ]
579
     * ]
580
     *
581
     * OUT:
582
     * tx_solr => [
583
     *   filter => [
584
     *      color:red
585
     *      product:candy
586
     *      color:blue
587
     *      taste:sour
588
     *   ]
589
     * ]
590
     *
591
     * @param array $queryParams
592
     * @return array
593
     */
594 1
    public function inflateQueryParameter(array $queryParams = []): array
595
    {
596 1
        if (!$this->shouldConcatQueryParameters()) {
597
            return $queryParams;
598
        }
599
600 1
        if (!isset($queryParams[$this->getPluginNamespace()])) {
601
            $queryParams[$this->getPluginNamespace()] = [];
602
        }
603
604 1
        if (!isset($queryParams[$this->getPluginNamespace()]['filter'])) {
605
            $queryParams[$this->getPluginNamespace()]['filter'] = [];
606
        }
607
608 1
        $newQueryParams = [];
609 1
        foreach ($queryParams[$this->getPluginNamespace()]['filter'] as $set) {
610 1
            $separator = $this->detectFacetAndValueSeparator((string)$set);
611 1
            [$facetName, $facetValuesString] = explode($separator, $set, 2);
612 1
            if ($facetValuesString == null) {
613
                continue;
614
            }
615 1
            $facetValues = explode($this->urlFacetQueryService->getMultiValueSeparator(), $facetValuesString);
616
617
            /**
618
             * A facet value could contain the multi value separator. This value is masked in order to
619
             * avoid problems during separation of the values (line above).
620
             *
621
             * After splitting the values, the character inside the value need to be restored
622
             *
623
             * @see RoutingService::queryParameterFacetsToString
624
             */
625 1
            $facetValues = array_map([$this->urlFacetQueryService, 'decodeSingleValue'], $facetValues);
626
627 1
            foreach ($facetValues as $facetValue) {
628 1
                $newQueryParams[] = $facetName . $separator . $facetValue;
629
            }
630
        }
631 1
        $queryParams[$this->getPluginNamespace()]['filter'] = array_values($newQueryParams);
632
633 1
        return $this->cleanUpQueryParameters($queryParams);
634
    }
635
636
    /**
637
     * Cleanup the query parameters, to avoid empty solr arguments
638
     *
639
     * @param array $queryParams
640
     * @return array
641
     */
642
    public function cleanUpQueryParameters(array $queryParams): array
643
    {
644
        if (empty($queryParams[$this->getPluginNamespace()]['filter'])) {
645
            unset($queryParams[$this->getPluginNamespace()]['filter']);
646
        }
647
648
        if (empty($queryParams[$this->getPluginNamespace()])) {
649
            unset($queryParams[$this->getPluginNamespace()]);
650
        }
651
        return $queryParams;
652
    }
653
654
    /**
655
     * Builds a string out of multiple facet values
656
     *
657
     * A facet value could contain the multi value separator. This value have to masked in order to
658
     * avoid problems during separation of the values later.
659
     *
660
     * This mask have to apply before contact the values
661
     *
662
     * @param array $facets
663
     * @return string
664
     */
665
    public function queryParameterFacetsToString(array $facets): string
666
    {
667
        $facets = array_map([$this->urlFacetQueryService, 'encodeSingleValue'], $facets);
668
        sort($facets);
669
        return implode($this->urlFacetQueryService->getMultiValueSeparator(), $facets);
670
    }
671
672
    /**
673
     * Returns the string which separates the facet from the value
674
     *
675
     * @param string $facetWithValue
676
     * @return string
677
     */
678
    public function detectFacetAndValueSeparator(string $facetWithValue): string
679
    {
680
        $separator = ':';
681
        if (mb_strpos($facetWithValue, '%3A') !== false) {
682
            $separator = '%3A';
683
        }
684
685
        return $separator;
686
    }
687
688
    /**
689
     * Check if given facet value combination contains a separator
690
     *
691
     * @param string $facetWithValue
692
     * @return bool
693
     */
694
    public function containsFacetAndValueSeparator(string $facetWithValue): bool
695
    {
696
        if (mb_strpos($facetWithValue, ':') === false && mb_strpos($facetWithValue, '%3A') === false) {
697
            return false;
698
        }
699
700
        return true;
701
    }
702
703
    /**
704
     * Cleanup facet values (strip type if needed)
705
     *
706
     * @param array $facetValues
707
     * @return array
708
     */
709
    public function cleanupFacetValues(array $facetValues): array
710
    {
711
        $facetValuesCount = count($facetValues);
712
        for ($i = 0; $i < $facetValuesCount; $i++) {
713
            if (!$this->containsFacetAndValueSeparator((string)$facetValues[$i])) {
714
                continue;
715
            }
716
717
            $separator = $this->detectFacetAndValueSeparator((string)$facetValues[$i]);
718
            [$type, $value] = explode($separator, $facetValues[$i]);
719
720
            if ($this->isMappingArgument($type) || $this->isPathArgument($type)) {
721
                $facetValues[$i] = $value;
722
            }
723
        }
724
        return $facetValues;
725
    }
726
727
    /**
728
     * Builds a string out of multiple facet values
729
     *
730
     * @param array $facets
731
     * @return string
732
     */
733
    public function pathFacetsToString(array $facets): string
734
    {
735
        $facets = $this->cleanupFacetValues($facets);
736
        sort($facets);
737
        $facets = array_map([$this->urlFacetPathService, 'applyCharacterMap'], $facets);
738
        $facets = array_map([$this->urlFacetPathService, 'encodeSingleValue'], $facets);
739
        return implode($this->urlFacetPathService->getMultiValueSeparator(), $facets);
740
    }
741
742
    /**
743
     * Builds a string out of multiple facet values
744
     *
745
     * @param array $facets
746
     * @return string
747
     */
748 2
    public function facetsToString(array $facets): string
749
    {
750 2
        $facets = $this->cleanupFacetValues($facets);
751 2
        sort($facets);
752 2
        return implode($this->getDefaultMultiValueSeparator(), $facets);
753
    }
754
755
    /**
756
     * Builds a string out of multiple facet values
757
     *
758
     * This method is used in two different situation
759
     *  1. Middleware: Here the values should not be decoded
760
     *  2. Within the event listener CachedPathVariableModifier
761
     *
762
     * @param string $facets
763
     * @param bool $decode
764
     * @return array
765
     */
766
    public function pathFacetStringToArray(string $facets, bool $decode = true): array
767
    {
768
        $facetString = $this->urlFacetPathService->applyCharacterMap($facets);
769
        $facets = explode($this->urlFacetPathService->getMultiValueSeparator(), $facetString);
770
        if (!$decode) {
771
            return $facets;
772
        }
773
        return array_map([$this->urlFacetPathService, 'decodeSingleValue'], $facets);
774
    }
775
776
    /**
777
     * Returns the multi value separator
778
     * @return string
779
     */
780 2
    public function getDefaultMultiValueSeparator(): string
781
    {
782 2
        return $this->settings['multiValueSeparator'] ?? ',';
783
    }
784
785
    /**
786
     * Find a enhancer configuration by a given page id
787
     *
788
     * @param int $pageUid
789
     * @return array
790
     */
791
    public function fetchEnhancerByPageUid(int $pageUid): array
792
    {
793
        $site = $this->findSiteByUid($pageUid);
794
        if ($site instanceof NullSite) {
795
            return [];
796
        }
797
798
        return $this->fetchEnhancerInSiteConfigurationByPageUid(
799
            $site,
800
            $pageUid
801
        );
802
    }
803
804
    /**
805
     * Returns the route enhancer configuration by given site and page uid
806
     *
807
     * @param Site $site
808
     * @param int $pageUid
809
     * @return array
810
     */
811
    public function fetchEnhancerInSiteConfigurationByPageUid(Site $site, int $pageUid): array
812
    {
813
        $configuration = $site->getConfiguration();
814
        if (empty($configuration['routeEnhancers']) || !is_array($configuration['routeEnhancers'])) {
815
            return [];
816
        }
817
        $result = [];
818
        foreach ($configuration['routeEnhancers'] as $routing => $settings) {
819
            // Not the page we are looking for
820
            if (isset($settings['limitToPages']) &&
821
                is_array($settings['limitToPages']) &&
822
                !in_array($pageUid, $settings['limitToPages'])) {
823
                continue;
824
            }
825
826
            if (empty($settings) || !isset($settings['type']) ||
827
                !$this->isRouteEnhancerForSolr((string)$settings['type'])
828
            ) {
829
                continue;
830
            }
831
            $result[] = $settings;
832
        }
833
834
        return $result;
835
    }
836
837
    /**
838
     * Add heading slash to given slug
839
     *
840
     * @param string $slug
841
     * @return string
842
     */
843
    public function cleanupHeadingSlash(string $slug): string
844
    {
845
        if (mb_substr($slug, 0, 1) !== '/') {
846
            return '/' . $slug;
847
        }
848
        if (mb_substr($slug, 0, 2) === '//') {
849
            return mb_substr($slug, 1, mb_strlen($slug) - 1);
850
        }
851
852
        return $slug;
853
    }
854
855
    /**
856
     * Add heading slash to given slug
857
     *
858
     * @param string $slug
859
     * @return string
860
     */
861
    public function addHeadingSlash(string $slug): string
862
    {
863
        if (mb_substr($slug, 0, 1) === '/') {
864
            return $slug;
865
        }
866
867
        return '/' . $slug;
868
    }
869
870
    /**
871
     * Remove heading slash from given slug
872
     *
873
     * @param string $slug
874
     * @return string
875
     */
876
    public function removeHeadingSlash(string $slug): string
877
    {
878
        if (mb_substr($slug, 0, 1) !== '/') {
879
            return $slug;
880
        }
881
882
        return mb_substr($slug, 1, mb_strlen($slug) - 1);
883
    }
884
885
    /**
886
     * Retrieve the site by given UID
887
     *
888
     * @param int $pageUid
889
     * @return SiteInterface
890
     */
891
    public function findSiteByUid(int $pageUid): SiteInterface
892
    {
893
        try {
894
            $site = $this->getSiteFinder()
895
                ->getSiteByPageId($pageUid);
896
            return $site;
897
        } catch (SiteNotFoundException $exception) {
898
            return new NullSite();
899
        }
900
    }
901
902
    /**
903
     * @param Site $site
904
     * @return PageSlugCandidateProvider
905
     */
906
    public function getSlugCandidateProvider(Site $site): PageSlugCandidateProvider
907
    {
908
        $context = GeneralUtility::makeInstance(Context::class);
909
        return GeneralUtility::makeInstance(
910
            PageSlugCandidateProvider::class,
911
            $context,
912
            $site,
913
            null
914
        );
915
    }
916
917
    /**
918
     * Convert the base string into a URI object
919
     *
920
     * @param string $base
921
     * @return UriInterface|null
922
     */
923 1
    public function convertStringIntoUri(string $base): ?UriInterface
924
    {
925
        try {
926
            /* @var Uri $uri */
927 1
            $uri = GeneralUtility::makeInstance(
928
                Uri::class,
929 1
                $base
930
            );
931
932 1
            return $uri;
933
        } catch (\InvalidArgumentException $argumentException) {
934
            return null;
935
        }
936
    }
937
938
    /**
939
     * In order to search for a path, a possible language prefix need to remove
940
     *
941
     * @param SiteLanguage $language
942
     * @param string $path
943
     * @return string
944
     */
945
    public function stripLanguagePrefixFromPath(SiteLanguage $language, string $path): string
946
    {
947
        if ($language->getBase()->getPath() === '/') {
948
            return $path;
949
        }
950
951
        $pathLength = mb_strlen($language->getBase()->getPath());
952
953
        $path = mb_substr($path, $pathLength, mb_strlen($path) - $pathLength);
954
        if (mb_substr($path, 0, 1) !== '/') {
955
            $path = '/' . $path;
956
        }
957
958
        return $path;
959
    }
960
961
    /**
962
     * Enrich the current query Params with data from path information
963
     *
964
     * @param ServerRequestInterface $request
965
     * @param array $arguments
966
     * @param array $parameters
967
     * @return ServerRequestInterface
968
     */
969 3
    public function addPathArgumentsToQuery(
970
        ServerRequestInterface $request,
971
        array $arguments,
972
        array $parameters
973
    ): ServerRequestInterface {
974 3
        $queryParams = $request->getQueryParams();
975 3
        foreach ($arguments as $fieldName => $queryPath) {
976
            // Skip if there is no parameter
977 3
            if (!isset($parameters[$fieldName])) {
978
                continue;
979
            }
980 3
            $pathElements = explode('/', $queryPath);
981
982 3
            if (!empty($this->pluginNamespace)) {
983 3
                array_unshift($pathElements, $this->pluginNamespace);
984
            }
985
986 3
            $queryParams = $this->processUriPathArgument(
987
                $queryParams,
988
                $fieldName,
989
                $parameters,
990 3
                $pathElements
991
            );
992
        }
993
994 3
        return $request->withQueryParams($queryParams);
995
    }
996
997
    /**
998
     * Check if given argument is a mapping argument
999
     *
1000
     * @param string $facetName
1001
     * @return bool
1002
     */
1003
    public function isMappingArgument(string $facetName): bool
1004
    {
1005
        $map = $this->getQueryParameterMap();
1006
        if (isset($map[$facetName]) && $this->shouldMaskQueryParameter()) {
1007
            return true;
1008
        }
1009
1010
        return false;
1011
    }
1012
1013
    /**
1014
     * Check if given facet type is an path argument
1015
     *
1016
     * @param string $facetName
1017
     * @return bool
1018
     */
1019
    public function isPathArgument(string $facetName): bool
1020
    {
1021
        return isset($this->pathArguments[$facetName]);
1022
    }
1023
1024
    /**
1025
     * @param string $variable
1026
     * @return string
1027
     */
1028
    public function reviewVariable(string $variable): string
1029
    {
1030
        if (!$this->containsFacetAndValueSeparator((string)$variable)) {
1031
            return $variable;
1032
        }
1033
1034
        $separator = $this->detectFacetAndValueSeparator((string)$variable);
1035
        [$type, $value] = explode($separator, $variable, 2);
1036
1037
        return $this->isMappingArgument($type) ? $value : $variable;
1038
    }
1039
1040
    /**
1041
     * Remove type prefix from filter
1042
     *
1043
     * @param array $variables
1044
     * @return array
1045
     */
1046
    public function reviseFilterVariables(array $variables): array
1047
    {
1048
        $newVariables = [];
1049
        foreach ($variables as $key => $value) {
1050
            $matches = [];
1051
            if (!preg_match('/###' . $this->getPluginNamespace() . ':filter:\d+:(.+?)###/', $key, $matches)) {
1052
                $newVariables[$key] = $value;
1053
                continue;
1054
            }
1055
            if (!$this->isMappingArgument($matches[1]) && !$this->isPathArgument($matches[1])) {
1056
                $newVariables[$key] = $value;
1057
                continue;
1058
            }
1059
            $separator = $this->detectFacetAndValueSeparator((string)$value);
1060
            $parts = explode($separator, $value);
1061
1062
            do {
1063
                if ($parts[0] === $matches[1]) {
1064
                    array_shift($parts);
1065
                }
1066
            } while ($parts[0] === $matches[1]);
1067
1068
            $newVariables[$key] = implode($separator, $parts);
1069
        }
1070
1071
        return $newVariables;
1072
    }
1073
1074
    /**
1075
     * Converts path segment information into query parameters
1076
     *
1077
     * Example:
1078
     * /products/household
1079
     *
1080
     * tx_solr:
1081
     *      filter:
1082
     *          - type:household
1083
     *
1084
     * @param array $queryParams
1085
     * @param string $fieldName
1086
     * @param array $parameters
1087
     * @param array $pathElements
1088
     * @return array
1089
     */
1090
    protected function processUriPathArgument(
1091
        array $queryParams,
1092
        string $fieldName,
1093
        array $parameters,
1094
        array $pathElements
1095
    ): array {
1096
        $queryKey = array_shift($pathElements);
1097
        $queryKey = (string)$queryKey;
1098
1099
        $tmpQueryKey = $queryKey;
1100
        if (strpos($queryKey, '-') !== false) {
1101
            [$tmpQueryKey, $filterName] = explode('-', $tmpQueryKey, 2);
1102
        }
1103
        if (!isset($queryParams[$tmpQueryKey]) || $queryParams[$tmpQueryKey] === null) {
1104
            $queryParams[$tmpQueryKey] = [];
1105
        }
1106
1107
        if (strpos($queryKey, '-') !== false) {
1108
            [$queryKey, $filterName] = explode('-', $queryKey, 2);
1109
            // explode multiple values
1110
            $values = $this->pathFacetStringToArray($parameters[$fieldName], false);
1111
            sort($values);
1112
1113
            // @TODO: Support URL data bag
1114
            foreach ($values as $value) {
1115
                $value = $this->urlFacetPathService->applyCharacterMap($value);
1116
                $queryParams[$queryKey][] = $filterName . ':' . $value;
1117
            }
1118
        } else {
1119
            $queryParams[$queryKey] = $this->processUriPathArgument(
1120
                $queryParams[$queryKey],
1121
                $fieldName,
1122
                $parameters,
1123
                $pathElements
1124
            );
1125
        }
1126
1127
        return $queryParams;
1128
    }
1129
1130
    /**
1131
     * Return site matcher
1132
     *
1133
     * @return SiteMatcher
1134
     */
1135
    public function getSiteMatcher(): SiteMatcher
1136
    {
1137
        return GeneralUtility::makeInstance(SiteMatcher::class, $this->getSiteFinder());
1138
    }
1139
1140
    /**
1141
     * Returns the site finder
1142
     *
1143
     * @return SiteFinder|null
1144
     */
1145
    protected function getSiteFinder(): ?SiteFinder
1146
    {
1147
        return GeneralUtility::makeInstance(SiteFinder::class);
1148
    }
1149
}
1150