Issues (138)

Classes/Middleware/CrawlerInitialization.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AOE\Crawler\Middleware;
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\ResponseInterface;
23
use Psr\Http\Message\ServerRequestInterface;
24
use Psr\Http\Server\MiddlewareInterface;
25
use Psr\Http\Server\RequestHandlerInterface;
26
use TYPO3\CMS\Core\Context\Context;
27
use TYPO3\CMS\Core\Http\Response;
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
29
30
/**
31
 * Evaluates HTTP headers and checks if Crawler should register itself.
32
 * Needs to be run after TSFE initialization AND Frontend User Authentication.
33
 *
34
 * Once done, the queue is fetched, and then the frontend request runs through.
35
 *
36
 * Finally, at the very end, if the crawler is still running, output the data and replace the response.
37
 */
38
class CrawlerInitialization implements MiddlewareInterface
39
{
40
    /**
41
     * @var Context
42
     */
43
    protected $context;
44
45
    public function __construct(?Context $context = null)
46
    {
47
        $this->context = $context ?? GeneralUtility::makeInstance(Context::class);
48
    }
49
50
    /**
51
     * @throws \TYPO3\CMS\Core\Context\Exception\AspectNotFoundException
52
     * @throws \TYPO3\CMS\Core\Error\Http\ServiceUnavailableException
53
     */
54
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
55
    {
56
        $queueParameters = $request->getAttribute('tx_crawler');
57
        if ($queueParameters === null) {
58
            return $handler->handle($request);
59
        }
60
61
        $GLOBALS['TSFE']->applicationData['forceIndexing'] = true;
62
        $GLOBALS['TSFE']->applicationData['tx_crawler']['running'] = true;
63
        $GLOBALS['TSFE']->applicationData['tx_crawler']['parameters'] = $queueParameters;
64
        $GLOBALS['TSFE']->applicationData['tx_crawler']['log'] = [
65
            'User Groups: ' . $queueParameters['feUserGroupList'],
66
        ];
67
68
        // Execute the frontend request as is
69
        $handler->handle($request);
70
71
        $GLOBALS['TSFE']->applicationData['tx_crawler']['vars'] = [
72
            'id' => $GLOBALS['TSFE']->id,
73
            'gr_list' => implode(',', $this->context->getAspect('frontend.user')->getGroupIds()),
0 ignored issues
show
The method getGroupIds() does not exist on TYPO3\CMS\Core\Context\AspectInterface. It seems like you code against a sub-type of TYPO3\CMS\Core\Context\AspectInterface such as TYPO3\CMS\Core\Context\UserAspect. ( Ignorable by Annotation )

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

73
            'gr_list' => implode(',', $this->context->getAspect('frontend.user')->/** @scrutinizer ignore-call */ getGroupIds()),
Loading history...
74
            'no_cache' => $GLOBALS['TSFE']->no_cache,
75
        ];
76
77
        $this->runPollSuccessHooks();
78
79
        // Output log data for crawler (serialized content):
80
        $content = serialize($GLOBALS['TSFE']->applicationData['tx_crawler']);
81
        $response = new Response();
82
        $response->getBody()->write($content);
83
84
        return $response;
85
    }
86
87
    /**
88
     * Required because some extensions (staticpub) might never be requested to run due to some Core side effects
89
     * and since this is considered as error the crawler should handle it properly
90
     */
91
    protected function runPollSuccessHooks(): void
92
    {
93
        if (! is_array($GLOBALS['TSFE']->applicationData['tx_crawler']['content']['parameters']['procInstructions'])) {
94
            return;
95
        }
96
        foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['pollSuccess'] ?? [] as $pollable) {
97
            if (in_array($pollable, $GLOBALS['TSFE']->applicationData['tx_crawler']['content']['parameters']['procInstructions'], true)) {
98
                if (empty($GLOBALS['TSFE']->applicationData['tx_crawler']['success'][$pollable])) {
99
                    $GLOBALS['TSFE']->applicationData['tx_crawler']['errorlog'][] = 'Error: Pollable extension (' . $pollable . ') did not complete successfully.';
100
                }
101
            }
102
        }
103
    }
104
}
105