[FEATURE] Add possibility to configure the path to sitemap xslFile 21/62121/6
authorKevin Appelt <kevin.appelt@icloud.com>
Fri, 25 Oct 2019 21:34:52 +0000 (23:34 +0200)
committerGeorg Ringer <georg.ringer@gmail.com>
Wed, 15 Jan 2020 21:25:55 +0000 (22:25 +0100)
The xsl file to create a layout for a XML sitemap can now be configured
on three levels:

1. for all sitemaps
2. for all sitemaps of a certain sitemapType
3. for a specific sitemap

The value is inherited until it is overwritten.
If no value is specified at all, EXT:seo/Resources/Public/CSS/Sitemap.xsl
is used as default like before.

Resolves: #88147
Releases: master
Change-Id: I1c5212a1c9fe4226100de8f4bd0f84d70b89fcf5
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/62121
Tested-by: Richard Haeser <richard@maxserv.com>
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-by: Richard Haeser <richard@maxserv.com>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
typo3/sysext/core/Documentation/Changelog/master/Feature-88147-AddPossibilityToConfigureThePathToSitemapXslFile.rst [new file with mode: 0644]
typo3/sysext/seo/Classes/XmlSitemap/XmlSitemapRenderer.php
typo3/sysext/seo/Configuration/TypoScript/XmlSitemap/setup.typoscript
typo3/sysext/seo/Tests/Functional/Fixtures/sitemap-xsl1.typoscript [new file with mode: 0644]
typo3/sysext/seo/Tests/Functional/Fixtures/sitemap-xsl2.typoscript [new file with mode: 0644]
typo3/sysext/seo/Tests/Functional/Fixtures/sitemap-xsl3.typoscript [new file with mode: 0644]
typo3/sysext/seo/Tests/Functional/XmlSitemap/XmlSitemapXslTest.php [new file with mode: 0644]

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-88147-AddPossibilityToConfigureThePathToSitemapXslFile.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-88147-AddPossibilityToConfigureThePathToSitemapXslFile.rst
new file mode 100644 (file)
index 0000000..5cc756d
--- /dev/null
@@ -0,0 +1,35 @@
+.. include:: ../../Includes.txt
+
+.. highlight:: typoscript
+
+==========================================================================
+Feature: #88147 - Add possibility to configure the path to sitemap xslFile
+==========================================================================
+
+See :issue:`88147`
+
+Description
+===========
+
+The xsl file to create a layout for a XML sitemap can now be configured on three levels:
+
+1. for all sitemaps::
+
+      plugin.tx_seo.config.xslFile = EXT:myext/Resources/Public/CSS/mySite.xsl
+
+2. for all sitemaps of a certain sitemapType::
+
+      plugin.tx_seo.config.<sitemapType>.sitemaps.xslFile = EXT:myext/Resources/Public/CSS/mySite.xsl
+
+3. for a specific sitemap::
+
+      plugin.tx_seo.config.<sitemapType>.sitemaps.<sitemap>.config.xslFile = EXT:myext/Resources/Public/CSS/mySite.xsl
+
+Impact
+======
+
+The value is inherited until it is overwritten.
+
+If no value is specified at all, :file:`EXT:seo/Resources/Public/CSS/Sitemap.xsl` is used as default like before.
+
+.. index:: Frontend, TypoScript, ext:seo
index 56b9524..b89455e 100644 (file)
@@ -17,7 +17,6 @@ namespace TYPO3\CMS\Seo\XmlSitemap;
  */
 
 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;
@@ -50,12 +49,6 @@ class XmlSitemapRenderer
     {
         $this->configuration = $this->getConfiguration();
         $this->view = $this->getStandaloneView();
-        $this->view->assign(
-            'xslFile',
-            PathUtility::stripPathSitePrefix(
-                ExtensionManagementUtility::extPath('seo', 'Resources/Public/CSS/Sitemap.xsl')
-            )
-        );
     }
 
     /**
@@ -70,6 +63,7 @@ class XmlSitemapRenderer
         $request = $GLOBALS['TYPO3_REQUEST'];
         $this->view->assign('type', $GLOBALS['TSFE']->type);
         $sitemapType = $typoScriptConfiguration['sitemapType'] ?? 'xmlSitemap';
+        $this->view->assign('xslFile', $this->getXslFilePath($sitemapType));
         if (!empty($sitemap = $request->getQueryParams()['sitemap'])) {
             return $this->renderSitemap($request, $sitemap, $sitemapType);
         }
@@ -139,6 +133,7 @@ class XmlSitemapRenderer
 
                 $template = $sitemapConfig['config']['template'] ?: 'Sitemap';
                 $this->view->setTemplate($template);
+                $this->view->assign('xslFile', $this->getXslFilePath($sitemapType, $sitemap));
                 $this->view->assign('items', $items);
                 $this->view->assign('sitemapType', $sitemapType);
 
@@ -165,6 +160,19 @@ class XmlSitemapRenderer
     }
 
     /**
+     * @param string|null $sitemapType
+     * @param string|null $sitemap
+     * @return string
+     */
+    protected function getXslFilePath(string $sitemapType = null, string $sitemap = null): string
+    {
+        $path = $this->configuration['config']['xslFile'] ?? 'EXT:seo/Resources/Public/CSS/Sitemap.xsl';
+        $path = ($sitemapType !== null) ? ($this->configuration['config'][$sitemapType]['sitemaps']['xslFile'] ?? $path) : $path;
+        $path = ($sitemapType !== null && $sitemap !== null) ? ($this->configuration['config'][$sitemapType]['sitemaps'][$sitemap]['config']['xslFile'] ?? $path) : $path;
+        return PathUtility::getAbsoluteWebPath(GeneralUtility::getFileAbsFileName($path));
+    }
+
+    /**
      * Get the whole typoscript array
      * @return array
      * @throws Exception
index bde0674..238c1f5 100644 (file)
@@ -36,11 +36,17 @@ plugin.tx_seo {
   }
 
   config {
+    # Here you can override the xslFile for all sitemaps
+    # xslFile = EXT:seo/Resources/Public/CSS/Sitemap.xsl
     xmlSitemap {
       sitemaps {
+        # Here you can override the xslFile for all sitemaps of a certain sitemapType
+        # xslFile = EXT:seo/Resources/Public/CSS/Sitemap.xsl
         pages {
           provider = TYPO3\CMS\Seo\XmlSitemap\PagesXmlSitemapDataProvider
           config {
+            # Here you can override the xslFile for a single sitemap
+            # xslFile = EXT:seo/Resources/Public/CSS/Sitemap.xsl
             excludedDoktypes = {$plugin.tx_seo.settings.xmlSitemap.sitemaps.pages.excludedDoktypes}
             additionalWhere = {$plugin.tx_seo.settings.xmlSitemap.sitemaps.pages.additionalWhere}
           }
diff --git a/typo3/sysext/seo/Tests/Functional/Fixtures/sitemap-xsl1.typoscript b/typo3/sysext/seo/Tests/Functional/Fixtures/sitemap-xsl1.typoscript
new file mode 100644 (file)
index 0000000..69a4585
--- /dev/null
@@ -0,0 +1,5 @@
+plugin.tx_seo {
+  config {
+    xslFile = EXT:seo/Tests/Functional/Fixtures/XslFile1.xsl
+  }
+}
diff --git a/typo3/sysext/seo/Tests/Functional/Fixtures/sitemap-xsl2.typoscript b/typo3/sysext/seo/Tests/Functional/Fixtures/sitemap-xsl2.typoscript
new file mode 100644 (file)
index 0000000..03e21af
--- /dev/null
@@ -0,0 +1,6 @@
+plugin.tx_seo {
+  config {
+    xmlSitemap.sitemaps.xslFile = EXT:seo/Tests/Functional/Fixtures/XslFile2.xsl
+  }
+}
+
diff --git a/typo3/sysext/seo/Tests/Functional/Fixtures/sitemap-xsl3.typoscript b/typo3/sysext/seo/Tests/Functional/Fixtures/sitemap-xsl3.typoscript
new file mode 100644 (file)
index 0000000..542b588
--- /dev/null
@@ -0,0 +1,13 @@
+plugin.tx_seo {
+  config {
+    xslFile = EXT:seo/Tests/Functional/Fixtures/XslFile1.xsl
+
+    xmlSitemap {
+      sitemaps {
+        records {
+          config.xslFile = EXT:seo/Tests/Functional/Fixtures/XslFile3.xsl
+        }
+      }
+    }
+  }
+}
diff --git a/typo3/sysext/seo/Tests/Functional/XmlSitemap/XmlSitemapXslTest.php b/typo3/sysext/seo/Tests/Functional/XmlSitemap/XmlSitemapXslTest.php
new file mode 100644 (file)
index 0000000..e21af1f
--- /dev/null
@@ -0,0 +1,130 @@
+<?php
+declare(strict_types = 1);
+
+namespace TYPO3\CMS\Seo\Tests\Functional\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 TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\AbstractTestCase;
+use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
+
+class XmlSitemapXslTest extends AbstractTestCase
+{
+    /**
+     * @var string[]
+     */
+    protected $coreExtensionsToLoad = [
+        'core', 'frontend', 'seo'
+    ];
+
+    protected function setUp(): void
+    {
+        parent::setUp();
+        $this->importDataSet('EXT:seo/Tests/Functional/Fixtures/pages-sitemap.xml');
+    }
+
+    /**
+     * @test
+     * @dataProvider getXslFilePaths
+     */
+    public function checkIfDefaultSitemapReturnsDefaultXsl($typoscriptSetupFiles, $sitemap, $xslFilePath): void
+    {
+        $this->setUpFrontendRootPage(
+            1,
+            [
+                'constants' => ['EXT:seo/Configuration/TypoScript/XmlSitemap/constants.typoscript'],
+                'setup' => $typoscriptSetupFiles
+            ]
+        );
+
+        $this->writeSiteConfiguration(
+            'website-local',
+            $this->buildSiteConfiguration(1, 'http://localhost/'),
+            [
+                $this->buildDefaultLanguageConfiguration('EN', '/')
+            ]
+        );
+
+        $config = [
+            'id' => 1,
+            'type' => 1533906435
+        ];
+
+        if (!empty($sitemap)) {
+            $config['sitemap'] = $sitemap;
+        }
+
+        $response = $this->executeFrontendRequest(
+            (new InternalRequest('http://localhost/'))->withQueryParameters($config)
+        );
+
+        self::assertRegExp('/<\?xml-stylesheet type="text\/xsl" href="' . $xslFilePath . '"\?>/', (string)$response->getBody());
+    }
+
+    public function getXslFilePaths()
+    {
+        return [
+            [
+                [
+                    'EXT:seo/Configuration/TypoScript/XmlSitemap/setup.typoscript'
+                ],
+                '',
+                '\/typo3\/sysext\/seo\/Resources\/Public\/CSS\/Sitemap.xsl'
+            ],
+            [
+                [
+                    'EXT:seo/Configuration/TypoScript/XmlSitemap/setup.typoscript',
+                    'EXT:seo/Tests/Functional/Fixtures/sitemap-xsl1.typoscript'
+                ],
+                '',
+                '\/typo3\/sysext\/seo\/Tests\/Functional\/Fixtures\/XslFile1.xsl'
+            ],
+            [
+                [
+                    'EXT:seo/Configuration/TypoScript/XmlSitemap/setup.typoscript',
+                    'EXT:seo/Tests/Functional/Fixtures/sitemap-xsl2.typoscript'
+                ],
+                '',
+                '\/typo3\/sysext\/seo\/Tests\/Functional\/Fixtures\/XslFile2.xsl'
+            ],
+            [
+                [
+                    'EXT:seo/Configuration/TypoScript/XmlSitemap/setup.typoscript',
+                    'EXT:seo/Tests/Functional/Fixtures/records.typoscript',
+                    'EXT:seo/Tests/Functional/Fixtures/sitemap-xsl3.typoscript'
+                ],
+                '',
+                '\/typo3\/sysext\/seo\/Tests\/Functional\/Fixtures\/XslFile1.xsl'
+            ],
+            [
+                [
+                    'EXT:seo/Configuration/TypoScript/XmlSitemap/setup.typoscript',
+                    'EXT:seo/Tests/Functional/Fixtures/records.typoscript',
+                    'EXT:seo/Tests/Functional/Fixtures/sitemap-xsl3.typoscript'
+                ],
+                'records',
+                '\/typo3\/sysext\/seo\/Tests\/Functional\/Fixtures\/XslFile3.xsl'
+            ],
+            [
+                [
+                    'EXT:seo/Configuration/TypoScript/XmlSitemap/setup.typoscript',
+                    'EXT:seo/Tests/Functional/Fixtures/records.typoscript',
+                    'EXT:seo/Tests/Functional/Fixtures/sitemap-xsl3.typoscript'
+                ],
+                'pages',
+                '\/typo3\/sysext\/seo\/Tests\/Functional\/Fixtures\/XslFile1.xsl'
+            ],
+        ];
+    }
+}