first commit

This commit is contained in:
salam committed 2026-10-19 08:18:39 +02:00
commit 1218dd1517
12716 files changed
+989233

No files matched your search

@@ -0,0 +1,168 @@
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Seo\Canonical;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use TYPO3\CMS\Frontend\Page\PageRepository;
use TYPO3\CMS\Frontend\Utility\CanonicalizationUtility;
/**
* Class to add the canonical tag to the page
*
* @internal this class is not part of TYPO3's Core API.
*/
class CanonicalGenerator
{
/**
* @var TypoScriptFrontendController
*/
protected $typoScriptFrontendController;
/**
* @var PageRepository
*/
protected $pageRepository;
/**
* @var Dispatcher
*/
protected $signalSlotDispatcher;
/**
* CanonicalGenerator constructor
*
* @param TypoScriptFrontendController $typoScriptFrontendController
* @param Dispatcher $signalSlotDispatcher
*/
public function __construct(TypoScriptFrontendController $typoScriptFrontendController = null, Dispatcher $signalSlotDispatcher = null)
{
if ($typoScriptFrontendController === null) {
$typoScriptFrontendController = $this->getTypoScriptFrontendController();
}
if ($signalSlotDispatcher === null) {
$signalSlotDispatcher = GeneralUtility::makeInstance(Dispatcher::class);
}
$this->typoScriptFrontendController = $typoScriptFrontendController;
$this->signalSlotDispatcher = $signalSlotDispatcher;
$this->pageRepository = GeneralUtility::makeInstance(PageRepository::class);
}
/**
* @return string
* @throws \TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotException
* @throws \TYPO3\CMS\Extbase\SignalSlot\Exception\InvalidSlotReturnException
*/
public function generate(): string
{
$href = '';
$this->signalSlotDispatcher->dispatch(self::class, 'beforeGeneratingCanonical', [&$href]);
if (empty($href) && (int)$this->typoScriptFrontendController->page['no_index'] === 1) {
return '';
}
if (empty($href)) {
// 1) Check if page show content from other page
$href = $this->checkContentFromPid();
}
if (empty($href)) {
// 2) Check if page has canonical URL set
$href = $this->checkForCanonicalLink();
}
if (empty($href)) {
// 3) Fallback, create canonical URL
$href = $this->checkDefaultCanonical();
}
if (!empty($href)) {
$canonical = '<link ' . GeneralUtility::implodeAttributes([
'rel' => 'canonical',
'href' => $href
], true) . '/>' . LF;
$this->typoScriptFrontendController->additionalHeaderData[] = $canonical;
return $canonical;
}
return '';
}
/**
* @return string
*/
protected function checkForCanonicalLink(): string
{
if (!empty($this->typoScriptFrontendController->page['canonical_link'])) {
return $this->typoScriptFrontendController->cObj->typoLink_URL([
'parameter' => $this->typoScriptFrontendController->page['canonical_link'],
'forceAbsoluteUrl' => true,
]);
}
return '';
}
/**
* @return string
*/
protected function checkContentFromPid(): string
{
if (!empty($this->typoScriptFrontendController->page['content_from_pid'])) {
$parameter = (int)$this->typoScriptFrontendController->page['content_from_pid'];
if ($parameter > 0) {
$targetPage = $this->pageRepository->getPage($parameter, true);
if (!empty($targetPage['canonical_link'])) {
$parameter = $targetPage['canonical_link'];
}
return $this->typoScriptFrontendController->cObj->typoLink_URL([
'parameter' => $parameter,
'forceAbsoluteUrl' => true,
]);
}
}
return '';
}
/**
* @return string
*/
protected function checkDefaultCanonical(): string
{
return $this->typoScriptFrontendController->cObj->typoLink_URL([
'parameter' => $this->typoScriptFrontendController->id . ',' . $this->typoScriptFrontendController->type,
'forceAbsoluteUrl' => true,
'addQueryString' => true,
'addQueryString.' => [
'method' => 'GET',
'exclude' => implode(
',',
CanonicalizationUtility::getParamsToExcludeForCanonicalizedUrl(
(int)$this->typoScriptFrontendController->id,
(array)$GLOBALS['TYPO3_CONF_VARS']['FE']['additionalCanonicalizedUrlParameters']
)
)
]
]);
}
/**
* @return TypoScriptFrontendController
*/
protected function getTypoScriptFrontendController(): TypoScriptFrontendController
{
return $GLOBALS['TSFE'];
}
}
@@ -0,0 +1,146 @@
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Seo\HrefLang;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Http\Uri;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use TYPO3\CMS\Frontend\DataProcessing\LanguageMenuProcessor;
/**
* Class to add the hreflang tags to the page
*
* @internal
*/
class HrefLangGenerator
{
/**
* The content object renderer
*
* @var ContentObjectRenderer
*/
public $cObj;
/**
* @var TypoScriptFrontendController
*/
protected $typoScriptFrontendController;
/**
* @var ServerRequestInterface
*/
protected $request;
/**
* HreflangGenerator constructor
*
* @param ContentObjectRenderer|null $cObj
* @param TypoScriptFrontendController|null $typoScriptFrontendController
*/
public function __construct(ContentObjectRenderer $cObj = null, TypoScriptFrontendController $typoScriptFrontendController = null, ServerRequestInterface $request = null)
{
if ($cObj === null) {
$cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
}
if ($typoScriptFrontendController === null) {
$typoScriptFrontendController = $this->getTypoScriptFrontendController();
}
if ($request === null) {
$request = $this->getRequest();
}
$this->cObj = $cObj;
$this->typoScriptFrontendController = $typoScriptFrontendController;
$this->request = $request;
}
public function generate(): string
{
$hreflangs = [];
if ((int)$this->typoScriptFrontendController->page['no_index'] === 1) {
return '';
}
if ($this->request->getAttribute('site') instanceof Site) {
$languageMenu = GeneralUtility::makeInstance(LanguageMenuProcessor::class);
$languages = $languageMenu->process($this->cObj, [], [], []);
foreach ($languages['languagemenu'] as $language) {
if ($language['available'] === 1 && !empty($language['link'])) {
$href = $this->getAbsoluteUrl($language['link']);
$hreflangs[] =
'<link rel="alternate" hreflang="' . htmlspecialchars($language['hreflang']) . '" href="' . htmlspecialchars($href) . '"/>';
}
}
if (count($hreflangs) > 1) {
$href = $this->getAbsoluteUrl($languages['languagemenu'][0]['link']);
$hreflangs[] =
'<link rel="alternate" hreflang="x-default" href="' . htmlspecialchars($href) . '"/>' . LF;
$this->getTypoScriptFrontendController()->additionalHeaderData[] = implode(LF, $hreflangs);
}
}
return implode(LF, $hreflangs);
}
/**
* @param string $url
* @return string
*/
protected function getAbsoluteUrl(string $url): string
{
$uri = new Uri($url);
if (empty($uri->getHost())) {
$url = $this->getSiteLanguage()->getBase()->withPath($uri->getPath());
if ($uri->getQuery()) {
$url = $url->withQuery($uri->getQuery());
}
}
return (string)$url;
}
/**
* @return SiteLanguage
*/
protected function getSiteLanguage(): SiteLanguage
{
return $this->request->getAttribute('language');
}
/**
* @return TypoScriptFrontendController
*/
protected function getTypoScriptFrontendController(): TypoScriptFrontendController
{
return $GLOBALS['TSFE'];
}
/**
* @return ServerRequestInterface
*/
protected function getRequest(): ServerRequestInterface
{
return $GLOBALS['TYPO3_REQUEST'];
}
}
@@ -0,0 +1,170 @@
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Seo\MetaTag;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection;
use TYPO3\CMS\Core\MetaTag\MetaTagManagerRegistry;
use TYPO3\CMS\Core\Resource\FileReference;
use TYPO3\CMS\Core\Resource\ProcessedFile;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Service\ImageService;
use TYPO3\CMS\Frontend\Resource\FileCollector;
/**
* Class to add the metatags for the SEO fields in core
*
* @internal
*/
class MetaTagGenerator
{
/**
* Generate the meta tags that can be set in backend and add them to frontend by using the MetaTag API
*
* @param array $params
*/
public function generate(array $params)
{
$metaTagManagerRegistry = GeneralUtility::makeInstance(MetaTagManagerRegistry::class);
if (!empty($params['page']['description'])) {
$manager = $metaTagManagerRegistry->getManagerForProperty('description');
$manager->addProperty('description', $params['page']['description']);
}
if (!empty($params['page']['og_title'])) {
$manager = $metaTagManagerRegistry->getManagerForProperty('og:title');
$manager->addProperty('og:title', $params['page']['og_title']);
}
if (!empty($params['page']['og_description'])) {
$manager = $metaTagManagerRegistry->getManagerForProperty('og:description');
$manager->addProperty('og:description', $params['page']['og_description']);
}
if (!empty($params['page']['og_image'])) {
$fileCollector = GeneralUtility::makeInstance(FileCollector::class);
$fileCollector->addFilesFromRelation('pages', 'og_image', $params['page']);
$manager = $metaTagManagerRegistry->getManagerForProperty('og:image');
$ogImages = $this->generateSocialImages($fileCollector->getFiles());
foreach ($ogImages as $ogImage) {
$subProperties = [];
$subProperties['url'] = $ogImage['url'];
$subProperties['width'] = $ogImage['width'];
$subProperties['height'] = $ogImage['height'];
if (!empty($ogImage['alternative'])) {
$subProperties['alt'] = $ogImage['alternative'];
}
$manager->addProperty(
'og:image',
$ogImage['url'],
$subProperties
);
}
}
/*
* Set type of twitter card to summary. This value can be overridden by TypoScript or the MetaTag API by
* using the replace option. In v10 this will be a page property
*/
$manager = $metaTagManagerRegistry->getManagerForProperty('twitter:card');
$manager->addProperty('twitter:card', 'summary');
if (!empty($params['page']['twitter_title'])) {
$manager = $metaTagManagerRegistry->getManagerForProperty('twitter:title');
$manager->addProperty('twitter:title', $params['page']['twitter_title']);
}
if (!empty($params['page']['twitter_description'])) {
$manager = $metaTagManagerRegistry->getManagerForProperty('twitter:description');
$manager->addProperty('twitter:description', $params['page']['twitter_description']);
}
if (!empty($params['page']['twitter_image'])) {
$fileCollector = GeneralUtility::makeInstance(FileCollector::class);
$fileCollector->addFilesFromRelation('pages', 'twitter_image', $params['page']);
$manager = $metaTagManagerRegistry->getManagerForProperty('twitter:image');
$twitterImages = $this->generateSocialImages($fileCollector->getFiles());
foreach ($twitterImages as $twitterImage) {
$subProperties = [];
if (!empty($twitterImage['alternative'])) {
$subProperties['alt'] = $twitterImage['alternative'];
}
$manager->addProperty(
'twitter:image',
$twitterImage['url'],
$subProperties
);
}
}
$noIndex = ((bool)$params['page']['no_index']) ? 'noindex' : 'index';
$noFollow = ((bool)$params['page']['no_follow']) ? 'nofollow' : 'follow';
if ($noIndex === 'noindex' || $noFollow === 'nofollow') {
$manager = $metaTagManagerRegistry->getManagerForProperty('robots');
$manager->addProperty('robots', implode(',', [$noIndex, $noFollow]));
}
}
/**
* @param array $fileReferences
* @return array
*/
protected function generateSocialImages(array $fileReferences): array
{
$imageService = GeneralUtility::makeInstance(ImageService::class);
$socialImages = [];
/** @var FileReference $file */
foreach ($fileReferences as $file) {
$arguments = $file->getProperties();
$cropVariantCollection = CropVariantCollection::create((string)$arguments['crop']);
$cropVariant = $arguments['cropVariant'] ?: 'social';
$cropArea = $cropVariantCollection->getCropArea($cropVariant);
$crop = $cropArea->makeAbsoluteBasedOnFile($file);
$cropInformation = $crop->asArray();
$processingConfiguration = [
'crop' => $crop
];
$processedImage = $file->getOriginalFile()->process(
ProcessedFile::CONTEXT_IMAGECROPSCALEMASK,
$processingConfiguration
);
$imageUri = $imageService->getImageUri($processedImage, true);
$socialImages[] = [
'url' => $imageUri,
'width' => floor($cropInformation['width']),
'height' => floor($cropInformation['height']),
'alternative' => $arguments['alternative'],
];
}
return $socialImages;
}
}
@@ -0,0 +1,68 @@
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Seo\MetaTag;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager;
/**
* @internal this class is not part of TYPO3's Core API.
*/
class OpenGraphMetaTagManager extends AbstractMetaTagManager
{
/**
* The default attribute that defines the name of the property
*
* This creates tags like <meta property="" /> by default
*
* @var string
*/
protected $defaultNameAttribute = 'property';
/**
* Array of properties that can be handled by this manager
*
* @var array
*/
protected $handledProperties = [
'og:type' => [],
'og:title' => [],
'og:description' => [],
'og:site_name' => [],
'og:url' => [],
'og:audio' => [],
'og:video' => [],
'og:determiner' => [],
'og:locale' => [
'allowedSubProperties' => [
'alternate' => [
'allowMultipleOccurrences' => true
],
]
],
'og:image' => [
'allowMultipleOccurrences' => true,
'allowedSubProperties' => [
'url' => [],
'secure_url' => [],
'type' => [],
'width' => [],
'height' => [],
'alt' => [],
]
]
];
}
@@ -0,0 +1,71 @@
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Seo\MetaTag;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\MetaTag\AbstractMetaTagManager;
/**
* @internal this class is not part of TYPO3's Core API.
*/
class TwitterCardMetaTagManager extends AbstractMetaTagManager
{
/**
* Array of properties that can be handled by this manager
*
* @var array
*/
protected $handledProperties = [
'twitter:card' => [],
'twitter:site' => [
'allowedSubProperties' => [
'id' => [],
]
],
'twitter:creator' => [
'allowedSubProperties' => [
'id' => [],
]
],
'twitter:description' => [],
'twitter:title' => [],
'twitter:image' => [
'allowedSubProperties' => [
'alt' => [],
]
],
'twitter:player' => [
'allowedSubProperties' => [
'width' => [],
'height' => [],
'stream' => [],
]
],
'twitter:app' => [
'allowedSubProperties' => [
'name:iphone' => [],
'id:iphone' => [],
'url:iphone' => [],
'name:ipad' => [],
'id:ipad' => [],
'url:ipad' => [],
'name:googleplay' => [],
'id:googleplay' => [],
'url:googleplay' => [],
]
],
];
}
@@ -0,0 +1,31 @@
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Seo\PageTitle;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\PageTitle\AbstractPageTitleProvider;
/**
* This class will take care of the seo title that can be set in the backend
* @internal this class is not part of TYPO3's Core API.
*/
class SeoTitlePageTitleProvider extends AbstractPageTitleProvider
{
public function __construct()
{
$this->title = (string)$GLOBALS['TSFE']->page['seo_title'];
}
}
@@ -0,0 +1,138 @@
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Seo\XmlSitemap;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
/**
* Base class for XmlSitemapProviders to extend
*/
abstract class AbstractXmlSitemapDataProvider implements XmlSitemapDataProviderInterface
{
/**
* @var string
*/
protected $key;
/**
* @var int
*/
protected $lastModified;
/**
* @var array
*/
protected $items = [];
/**
* @var array
*/
protected $config = [];
/**
* @var ContentObjectRenderer
*/
protected $cObj;
/**
* @var int
*/
protected $numberOfItemsPerPage = 1000;
/**
* @var ServerRequestInterface
*/
protected $request;
/**
* AbstractXmlSitemapDataProvider constructor
*
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param string $key
* @param array $config
* @param ContentObjectRenderer $cObj
*/
public function __construct(ServerRequestInterface $request, string $key, array $config = [], ContentObjectRenderer $cObj = null)
{
$this->key = $key;
$this->config = $config;
$this->request = $request;
if ($cObj === null) {
$cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
}
$this->cObj = $cObj;
}
/**
* @return string
*/
public function getKey(): string
{
return $this->key;
}
/**
* @return int
*/
public function getNumberOfPages(): int
{
return (int)ceil(count($this->items) / $this->numberOfItemsPerPage);
}
/**
* @return int
*/
public function getLastModified(): int
{
$lastMod = 0;
foreach ($this->items as $item) {
if ((int)$item['lastMod'] > $lastMod) {
$lastMod = (int)$item['lastMod'];
}
}
return $lastMod;
}
/**
* @param array $data
* @return array
*/
protected function defineUrl(array $data): array
{
return $data;
}
/**
* @return array
*/
public function getItems(): array
{
$pageNumber = (int)($this->request->getQueryParams()['page'] ?? 0);
$page = $pageNumber > 0 ? $pageNumber : 0;
$items = array_slice(
$this->items,
$page * $this->numberOfItemsPerPage,
$this->numberOfItemsPerPage
);
return array_map([$this, 'defineUrl'], $items);
}
}
@@ -0,0 +1,20 @@
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Seo\XmlSitemap\Exception;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
class InvalidConfigurationException extends \TYPO3\CMS\Core\Resource\Exception
{
}
@@ -0,0 +1,20 @@
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Seo\XmlSitemap\Exception;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
class MissingConfigurationException extends \TYPO3\CMS\Core\Resource\Exception
{
}
@@ -0,0 +1,124 @@
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Seo\XmlSitemap;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\LanguageAspect;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryHelper;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\Page\PageRepository;
/**
* Class to generate a XML sitemap for pages
* @internal this class is not part of TYPO3's Core API.
*/
class PagesXmlSitemapDataProvider extends AbstractXmlSitemapDataProvider
{
public function __construct(ServerRequestInterface $request, string $key, array $config = [], ContentObjectRenderer $cObj = null)
{
parent::__construct($request, $key, $config, $cObj);
$this->generateItems();
}
protected function generateItems(): void
{
$pageRepository = GeneralUtility::makeInstance(PageRepository::class);
$pages = $pageRepository->getPagesOverlay($this->getPages());
$languageAspect = $this->getCurrentLanguageAspect();
foreach ($pages as $page) {
if (!$pageRepository->isPageSuitableForLanguage($page, $languageAspect)) {
continue;
}
$this->items[] = [
'uid' => $page['uid'],
'lastMod' => (int)($page['SYS_LASTCHANGED'] ?: $page['tstamp'])
];
}
}
/**
* @return array
*/
protected function getPages(): array
{
if (!empty($this->config['rootPage'])) {
$rootPageId = (int)$this->config['rootPage'];
} else {
$site = $this->request->getAttribute('site');
$rootPageId = $site->getRootPageId();
}
$cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
$treeList = $cObj->getTreeList(-$rootPageId, 99);
$treeListArray = GeneralUtility::intExplode(',', $treeList);
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('pages');
$constraints = [
$queryBuilder->expr()->in('uid', $treeListArray)
];
if (!empty($this->config['additionalWhere'])) {
$constraints[] = QueryHelper::stripLogicalOperatorPrefix($this->config['additionalWhere']);
}
if (!empty($this->config['excludedDoktypes'])) {
$excludedDoktypes = GeneralUtility::intExplode(',', $this->config['excludedDoktypes']);
if (!empty($excludedDoktypes)) {
$constraints[] = $queryBuilder->expr()->notIn('doktype', implode(',', $excludedDoktypes));
}
}
$pages = $queryBuilder->select('*')
->from('pages')
->where(...$constraints)
->orderBy('uid', 'ASC')
->execute()
->fetchAll();
return $pages;
}
/**
* @return LanguageAspect
*/
protected function getCurrentLanguageAspect(): LanguageAspect
{
return GeneralUtility::makeInstance(Context::class)->getAspect('language');
}
/**
* @param array $data
* @return array
*/
protected function defineUrl(array $data): array
{
$typoLinkConfig = [
'parameter' => $data['uid'],
'forceAbsoluteUrl' => 1,
];
$data['loc'] = $this->cObj->typoLink_URL($typoLinkConfig);
return $data;
}
}
@@ -0,0 +1,195 @@
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Seo\XmlSitemap;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryHelper;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Seo\XmlSitemap\Exception\MissingConfigurationException;
/**
* XmlSiteDataProvider will provide information for the XML sitemap for a specific database table
* @internal this class is not part of TYPO3's Core API.
*/
class RecordsXmlSitemapDataProvider extends AbstractXmlSitemapDataProvider
{
/**
* @param ServerRequestInterface $request
* @param string $key
* @param array $config
* @param ContentObjectRenderer|null $cObj
* @throws MissingConfigurationException
*/
public function __construct(ServerRequestInterface $request, string $key, array $config = [], ContentObjectRenderer $cObj = null)
{
parent::__construct($request, $key, $config, $cObj);
$this->generateItems();
}
/**
* @throws MissingConfigurationException
*/
public function generateItems(): void
{
$table = $this->config['table'];
if (empty($table)) {
throw new MissingConfigurationException(
'No configuration found for sitemap ' . $this->getKey(),
1535576053
);
}
$pids = !empty($this->config['pid']) ? GeneralUtility::intExplode(',', $this->config['pid']) : [];
$lastModifiedField = $this->config['lastModifiedField'] ?? 'tstamp';
$sortField = $this->config['sortField'] ?? 'sorting';
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable($table);
$constraints = [];
if (!empty($GLOBALS['TCA'][$table]['ctrl']['languageField'])) {
$constraints[] = $queryBuilder->expr()->in(
$GLOBALS['TCA'][$table]['ctrl']['languageField'],
[
-1, // All languages
$this->getLanguageId() // Current language
]
);
}
if (!empty($pids)) {
$recursiveLevel = isset($this->config['recursive']) ? (int)$this->config['recursive'] : 0;
if ($recursiveLevel) {
$newList = [];
foreach ($pids as $pid) {
$list = $this->cObj->getTreeList($pid, $recursiveLevel);
if ($list) {
$newList = array_merge($newList, explode(',', $list));
}
}
$pids = array_merge($pids, $newList);
}
$constraints[] = $queryBuilder->expr()->in('pid', $pids);
}
if (!empty($this->config['additionalWhere'])) {
$constraints[] = QueryHelper::stripLogicalOperatorPrefix($this->config['additionalWhere']);
}
$queryBuilder->select('*')
->from($table);
if (!empty($constraints)) {
$queryBuilder->where(
...$constraints
);
}
$rows = $queryBuilder->orderBy($sortField)
->execute()
->fetchAll();
foreach ($rows as $row) {
$this->items[] = [
'data' => $row,
'lastMod' => (int)$row[$lastModifiedField]
];
}
}
/**
* @param array $data
* @return array
*/
protected function defineUrl(array $data): array
{
$pageId = $this->config['url']['pageId'] ?? $GLOBALS['TSFE']->id;
$additionalParams = [];
$additionalParams = $this->getUrlFieldParameterMap($additionalParams, $data['data']);
$additionalParams = $this->getUrlAdditionalParams($additionalParams);
$additionalParamsString = http_build_query(
$additionalParams,
'',
'&',
PHP_QUERY_RFC3986
);
$typoLinkConfig = [
'parameter' => $pageId,
'additionalParams' => $additionalParamsString ? '&' . $additionalParamsString : '',
'forceAbsoluteUrl' => 1,
'useCacheHash' => $this->config['url']['useCacheHash'] ?? 0
];
$data['loc'] = $this->cObj->typoLink_URL($typoLinkConfig);
return $data;
}
/**
* @param array $additionalParams
* @param array $data
* @return array
*/
protected function getUrlFieldParameterMap(array $additionalParams, array $data): array
{
if (!empty($this->config['url']['fieldToParameterMap']) &&
\is_array($this->config['url']['fieldToParameterMap'])) {
foreach ($this->config['url']['fieldToParameterMap'] as $field => $urlPart) {
$additionalParams[$urlPart] = $data[$field];
}
}
return $additionalParams;
}
/**
* @param array $additionalParams
* @return array
*/
protected function getUrlAdditionalParams(array $additionalParams): array
{
if (!empty($this->config['url']['additionalGetParameters']) &&
is_array($this->config['url']['additionalGetParameters'])) {
foreach ($this->config['url']['additionalGetParameters'] as $extension => $extensionConfig) {
foreach ($extensionConfig as $key => $value) {
$additionalParams[$extension . '[' . $key . ']'] = $value;
}
}
}
return $additionalParams;
}
/**
* @return int
* @throws \TYPO3\CMS\Core\Context\Exception\AspectNotFoundException
*/
protected function getLanguageId(): int
{
$context = GeneralUtility::makeInstance(Context::class);
return (int)$context->getPropertyFromAspect('language', 'id');
}
}
@@ -0,0 +1,32 @@
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Seo\XmlSitemap;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
/**
* Interface for XmlSitemapDataProviders containing the methods that are called by the XmlSitemapRenderer
*/
interface XmlSitemapDataProviderInterface
{
public function __construct(ServerRequestInterface $request, string $name, array $config = [], ContentObjectRenderer $cObj = null);
public function getKey(): string;
public function getItems(): array;
public function getLastModified(): int;
public function getNumberOfPages(): int;
}
@@ -0,0 +1,169 @@
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Seo\XmlSitemap;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\PathUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Fluid\View\StandaloneView;
use TYPO3\CMS\Seo\XmlSitemap\Exception\InvalidConfigurationException;
/**
* Class to render the XML Sitemap to be used as a UserFunction
* @internal this class is not part of TYPO3's Core API.
*/
class XmlSitemapRenderer
{
/**
* @var array
*/
protected $configuration;
/**
* @var \TYPO3\CMS\Fluid\View\StandaloneView
*/
protected $view;
public function __construct()
{
$this->configuration = $this->getConfiguration();
$this->view = $this->getStandaloneView();
$this->view->assign(
'xslFile',
PathUtility::stripPathSitePrefix(
ExtensionManagementUtility::extPath('seo', 'Resources/Public/CSS/Sitemap.xsl')
)
);
}
/**
* @return string
* @throws InvalidConfigurationException
*/
public function render(): string
{
// Inject request from globals until request will be available to cObj
$request = $GLOBALS['TYPO3_REQUEST'];
$this->view->assign('type', $GLOBALS['TSFE']->type);
if (!empty($sitemap = $request->getQueryParams()['sitemap'])) {
return $this->renderSitemap($request, $sitemap);
}
return $this->renderIndex($request);
}
/**
* @param \Psr\Http\Message\ServerRequestInterface $request
* @return string
*/
protected function renderIndex(ServerRequestInterface $request): string
{
$sitemaps = [];
foreach ($this->configuration['config']['xmlSitemap']['sitemaps'] ?? [] as $sitemap => $config) {
if (class_exists($config['provider']) &&
is_subclass_of($config['provider'], XmlSitemapDataProviderInterface::class)) {
/** @var XmlSitemapDataProviderInterface $provider */
$provider = GeneralUtility::makeInstance(
$config['provider'],
$request,
$sitemap,
(array)$config['config']
);
$pages = $provider->getNumberOfPages();
for ($page = 0; $page < $pages; $page++) {
$sitemaps[] = [
'key' => $sitemap,
'page' => $page,
'lastMod' => $provider->getLastModified()
];
}
}
}
$this->view->assign('sitemaps', $sitemaps);
$this->view->setTemplate('Index');
return $this->view->render();
}
/**
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param string $sitemap
* @return string
* @throws \TYPO3\CMS\Seo\XmlSitemap\Exception\InvalidConfigurationException
*/
protected function renderSitemap(ServerRequestInterface $request, string $sitemap): string
{
if (!empty($sitemapConfig = $this->configuration['config']['xmlSitemap']['sitemaps'][$sitemap])) {
if (class_exists($sitemapConfig['provider']) &&
is_subclass_of($sitemapConfig['provider'], XmlSitemapDataProviderInterface::class)) {
/** @var XmlSitemapDataProviderInterface $provider */
$provider = GeneralUtility::makeInstance(
$sitemapConfig['provider'],
$request,
$sitemap,
(array)$sitemapConfig['config']
);
$items = $provider->getItems();
$template = $sitemapConfig['config']['template'] ?: 'Sitemap';
$this->view->setTemplate($template);
$this->view->assign('items', $items);
return $this->view->render();
}
throw new InvalidConfigurationException('No valid provider set for ' . $sitemap, 1535578522);
}
throw new InvalidConfigurationException('No valid configuration found for sitemap ' . $sitemap, 1535578569);
}
/**
* @return \TYPO3\CMS\Fluid\View\StandaloneView
*/
protected function getStandaloneView(): StandaloneView
{
$view = GeneralUtility::makeInstance(StandaloneView::class);
$view->setTemplateRootPaths($this->configuration['view']['templateRootPaths']);
$view->setLayoutRootPaths($this->configuration['view']['layoutRootPaths']);
$view->setPartialRootPaths($this->configuration['view']['partialRootPaths']);
$view->setFormat('xml');
return $view;
}
/**
* Get the whole typoscript array
* @return array
*/
private function getConfiguration(): array
{
$configurationManager = GeneralUtility::makeInstance(ObjectManager::class)
->get(ConfigurationManagerInterface::class);
return $configurationManager->getConfiguration(
ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK,
'seo'
);
}
}