Completed
Push — master ( 1b04e6...82e336 )
by Tomas Norre
08:07
created

Classes/Hooks/StaticFileCacheCreateUriHook.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace AOE\Crawler\Hooks;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2016 AOE GmbH <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
29
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
30
31
/**
32
 * Class StaticFileCacheCreateUriHook
33
 * @package AOE\Crawler\Hooks
34
 */
35
class StaticFileCacheCreateUriHook
36
{
37
    /**
38
     * Initializes the variables before starting the processing.
39
     *
40
     * @param array $parameters The parameters used in this hook
41
     * @param $parent The calling parent object
42
     * @return void
43
     */
44
    public function initialize(array $parameters, $parent)
0 ignored issues
show
The parameter $parent is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
45
    {
46
        list($parameters['host'], $parameters['uri']) = $this->fixNonSpeakingUri($parameters['host'], $parameters['uri'], $parameters['TSFE']);
47
    }
48
49
    /**
50
     * Fixes non speaking URLs.
51
     *
52
     * @param string $host
53
     * @param string $uri
54
     * @param TypoScriptFrontendController $frontend
55
     *
56
     * @return array
57
     *
58
     * @throws \Exception
59
     *
60
     * TODO: Write Unit test
61
     */
62
    protected function fixNonSpeakingUri($host, $uri, TypoScriptFrontendController $frontend)
63
    {
64
        $matches = [];
65
66
        if ($this->isCrawlerExtensionRunning($frontend) && preg_match('#^/index.php\?&?id=(\d+)(&.*)?$#', $uri, $matches)) {
67
            $speakingUri = $frontend->cObj->typoLink_URL(['parameter' => $matches[1], 'additionalParams' => $matches[2]]);
68
            $speakingUriParts = parse_url($speakingUri);
69
            if (false === $speakingUriParts) {
70
                throw new \Exception('Could not parse URI: ' . $speakingUri, 1289915976);
71
            }
72
            $speakingUrlPath = '/' . ltrim($speakingUriParts['path'], '/');
73
            // Don't change anything if speaking URL is part of old URI:
74
            // (it might be the case the using the speaking URL failed)
75
            if (strpos($uri, $speakingUrlPath) !== 0 || $speakingUrlPath === '/') {
76
                if (isset($speakingUriParts['host'])) {
77
                    $host = $speakingUriParts['host'];
78
                }
79
80
                $uri = $speakingUrlPath;
81
            }
82
        }
83
84
        return [$host, $uri];
85
    }
86
87
    /**
88
     * Determine whether the crawler extension is running and initiated the current request.
89
     *
90
     * @param TypoScriptFrontendController $frontend
91
     * @return boolean
92
     *
93
     * TODO: Write Unit test
94
     */
95
    protected function isCrawlerExtensionRunning(TypoScriptFrontendController $frontend)
96
    {
97
        return (
98
            ExtensionManagementUtility::isLoaded('crawler')
99
            && isset($frontend->applicationData['tx_crawler']['running'])
100
            && isset($frontend->applicationData['tx_crawler']['parameters']['procInstructions'])
101
            && $frontend->applicationData['tx_crawler']['running']
102
        );
103
    }
104
}
105