1<?php
2declare(strict_types = 1);
3
4namespace TYPO3\CMS\Seo\XmlSitemap;
5
6/*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19use Psr\Http\Message\ServerRequestInterface;
20use TYPO3\CMS\Core\Utility\GeneralUtility;
21use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
22
23/**
24 * Base class for XmlSitemapProviders to extend
25 */
26abstract class AbstractXmlSitemapDataProvider implements XmlSitemapDataProviderInterface
27{
28    /**
29     * @var string
30     */
31    protected $key;
32
33    /**
34     * @var int
35     */
36    protected $lastModified;
37
38    /**
39     * @var array
40     */
41    protected $items = [];
42
43    /**
44     * @var array
45     */
46    protected $config = [];
47
48    /**
49     * @var ContentObjectRenderer
50     */
51    protected $cObj;
52
53    /**
54     * @var int
55     */
56    protected $numberOfItemsPerPage = 1000;
57
58    /**
59     * @var ServerRequestInterface
60     */
61    protected $request;
62
63    /**
64     * AbstractXmlSitemapDataProvider constructor
65     *
66     * @param \Psr\Http\Message\ServerRequestInterface $request
67     * @param string $key
68     * @param array $config
69     * @param ContentObjectRenderer $cObj
70     */
71    public function __construct(ServerRequestInterface $request, string $key, array $config = [], ContentObjectRenderer $cObj = null)
72    {
73        $this->key = $key;
74        $this->config = $config;
75        $this->request = $request;
76
77        if ($cObj === null) {
78            $cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
79        }
80        $this->cObj = $cObj;
81    }
82
83    /**
84     * @return string
85     */
86    public function getKey(): string
87    {
88        return $this->key;
89    }
90
91    /**
92     * @return int
93     */
94    public function getNumberOfPages(): int
95    {
96        return (int)ceil(count($this->items) / $this->numberOfItemsPerPage);
97    }
98
99    /**
100     * @return int
101     */
102    public function getLastModified(): int
103    {
104        $lastMod = 0;
105        foreach ($this->items as $item) {
106            if ((int)$item['lastMod'] > $lastMod) {
107                $lastMod = (int)$item['lastMod'];
108            }
109        }
110
111        return $lastMod;
112    }
113
114    /**
115     * @param array $data
116     * @return array
117     */
118    protected function defineUrl(array $data): array
119    {
120        return $data;
121    }
122
123    /**
124     * @return array
125     */
126    public function getItems(): array
127    {
128        $pageNumber = (int)($this->request->getQueryParams()['page'] ?? 0);
129        $page = $pageNumber > 0 ? $pageNumber : 0;
130        $items = array_slice(
131            $this->items,
132            $page * $this->numberOfItemsPerPage,
133            $this->numberOfItemsPerPage
134        );
135
136        return array_map([$this, 'defineUrl'], $items);
137    }
138}
139