1717
1818namespace ApacheSolrForTypo3 \Solr \Domain \Site ;
1919
20+ use ApacheSolrForTypo3 \Solr \Event \Site \AfterSiteHashHasBeenDeterminedForSiteEvent ;
21+ use ApacheSolrForTypo3 \Solr \System \Configuration \ExtensionConfiguration ;
2022use ApacheSolrForTypo3 \Solr \System \Util \SiteUtility ;
23+ use Psr \EventDispatcher \EventDispatcherInterface ;
24+ use TYPO3 \CMS \Core \Core \Environment ;
2125use TYPO3 \CMS \Core \Exception \SiteNotFoundException ;
26+ use TYPO3 \CMS \Core \Site \Entity \Site as TYPO3Site ;
2227use TYPO3 \CMS \Core \Site \SiteFinder ;
28+ use TYPO3 \CMS \Core \Utility \GeneralUtility ;
2329
2430/**
2531 * SiteHashService
2632 *
27- * Responsible to provide sitehash related service methods.
33+ * Responsible to provide site-hash related service methods.
2834 */
2935class SiteHashService
3036{
31- protected SiteFinder $ siteFinder ;
37+ protected EventDispatcherInterface $ eventDispatcher ;
3238
33- public function __construct (SiteFinder $ siteFinder )
34- {
35- $ this ->siteFinder = $ siteFinder ;
39+ public function __construct (
40+ protected SiteFinder $ siteFinder ,
41+ protected ExtensionConfiguration $ extensionConfiguration ,
42+ ?EventDispatcherInterface $ eventDispatcherInterface = null ,
43+ ) {
44+ $ this ->eventDispatcher = $ eventDispatcherInterface ?? GeneralUtility::makeInstance (EventDispatcherInterface::class);
3645 }
3746
3847 /**
@@ -52,32 +61,98 @@ public function getAllowedSitesForPageIdAndAllowedSitesConfiguration(
5261 ?string $ allowedSitesConfiguration = '' ,
5362 ): string {
5463 if ($ allowedSitesConfiguration === '__all ' ) {
55- return $ this ->getDomainListOfAllSites ();
64+ return $ this ->extensionConfiguration ->getSiteHashStrategy () === 1
65+ ? $ this ->getIdentifiersOfAllSites ()
66+ : $ this ->getDomainListOfAllSites ();
5667 }
5768 if ($ allowedSitesConfiguration === '* ' ) {
5869 return '* ' ;
5970 }
6071 // we thread empty allowed site configurations as __solr_current_site since this is the default behaviour
6172 $ allowedSitesConfiguration = empty ($ allowedSitesConfiguration ) ? '__solr_current_site ' : $ allowedSitesConfiguration ;
62- return $ this ->getDomainByPageIdAndReplaceMarkers ($ pageId , $ allowedSitesConfiguration );
73+
74+ if ($ this ->extensionConfiguration ->getSiteHashStrategy () === 0 ) {
75+ return $ this ->getDomainByPageIdAndReplaceMarkers ($ pageId , $ allowedSitesConfiguration );
76+ }
77+ return $ this ->getSiteIdentifierByPageIdAndReplaceMarkers ($ pageId , $ allowedSitesConfiguration );
78+ }
79+
80+ public function getSiteHash (TYPO3Site $ site ): string
81+ {
82+ static $ siteHashes = [];
83+ if (isset ($ siteHashes [$ site ->getIdentifier ()])) {
84+ return $ siteHashes [$ site ->getIdentifier ()];
85+ }
86+
87+ $ siteHash = $ this ->getSiteHashForSiteIdentifier ($ site ->getIdentifier ());
88+
89+ $ event = $ this ->eventDispatcher ->dispatch (
90+ new AfterSiteHashHasBeenDeterminedForSiteEvent (
91+ $ siteHash ,
92+ $ site ,
93+ $ this ->extensionConfiguration ,
94+ ),
95+ );
96+ $ siteHashes [$ site ->getIdentifier ()] = $ event ->getSiteHash ();
97+ return $ siteHashes [$ site ->getIdentifier ()];
6398 }
6499
65100 /**
66101 * Gets the site hash for a given domain
102+ *
103+ * @deprecated The method SiteHashService::getSiteHashForDomain() is deprecated and will be removed in version 13.1.x+.
104+ * Use SiteHashService::getSiteHashForSiteIdentifier() or SiteHashService::getSiteHash() instead.
67105 */
68106 public function getSiteHashForDomain (string $ domain ): string
107+ {
108+ trigger_error (
109+ 'The method SiteHashService::getSiteHashForDomain() is deprecated and will be removed in version 13.1.x+. ' .
110+ 'Use SiteHashService::getSiteHashForSiteIdentifier() or SiteHashService::getSiteHash() instead. ' ,
111+ E_USER_DEPRECATED ,
112+ );
113+ return $ this ->getSiteHashForSiteIdentifier ($ domain );
114+ }
115+
116+ /**
117+ * Gets the site hash for a given site-identifier
118+ */
119+ public function getSiteHashForSiteIdentifier (string $ siteIdentifier ): string
69120 {
70121 static $ siteHashes = [];
71- if (isset ($ siteHashes [$ domain ])) {
72- return $ siteHashes [$ domain ];
122+ if (isset ($ siteHashes [$ siteIdentifier ])) {
123+ return $ siteHashes [$ siteIdentifier ];
124+ }
125+
126+ $ applicationContext = (string )Environment::getContext ();
127+ if ($ this ->extensionConfiguration ->getSiteHashStrategy () === 0 ) {
128+ $ applicationContext = '' ;
129+ }
130+ $ siteHashes [$ siteIdentifier ] = sha1 ($ applicationContext . $ siteIdentifier . $ GLOBALS ['TYPO3_CONF_VARS ' ]['SYS ' ]['encryptionKey ' ] . 'tx_solr ' );
131+ return $ siteHashes [$ siteIdentifier ];
132+ }
133+
134+ /**
135+ * Returns a comma separated list of all site-identifiers.
136+ */
137+ protected function getIdentifiersOfAllSites (): string
138+ {
139+ $ sites = $ this ->siteFinder ->getAllSites ();
140+ $ siteIdentifiers = [];
141+ foreach ($ sites as $ typo3Site ) {
142+ $ connections = SiteUtility::getAllSolrConnectionConfigurations ($ typo3Site );
143+ if (!empty ($ connections )) {
144+ $ siteIdentifiers [] = $ typo3Site ->getIdentifier ();
145+ }
73146 }
74147
75- $ siteHashes [$ domain ] = sha1 ($ domain . $ GLOBALS ['TYPO3_CONF_VARS ' ]['SYS ' ]['encryptionKey ' ] . 'tx_solr ' );
76- return $ siteHashes [$ domain ];
148+ return implode (', ' , $ siteIdentifiers );
77149 }
78150
79151 /**
80152 * Returns a comma separated list of all domains from all sites.
153+ *
154+ * @deprecated SiteHashService::getDomainListOfAllSites() is deprecated and will be removed in v14.
155+ * Use SiteHashService::getIdentifiersOfAllSites() instead.
81156 */
82157 protected function getDomainListOfAllSites (): string
83158 {
@@ -93,9 +168,29 @@ protected function getDomainListOfAllSites(): string
93168 return implode (', ' , $ domains );
94169 }
95170
171+ /**
172+ * Retrieves the site identifier of the site that belongs to the passed pageId and replaces their markers __solr_current_site
173+ * and __current_site.
174+ */
175+ protected function getSiteIdentifierByPageIdAndReplaceMarkers (int $ pageId , string $ allowedSitesConfiguration ): string
176+ {
177+ try {
178+ $ typo3Site = $ this ->siteFinder ->getSiteByPageId ($ pageId );
179+ $ siteIdentifierOfPage = $ typo3Site ->getIdentifier ();
180+ } catch (SiteNotFoundException ) {
181+ return '' ;
182+ }
183+
184+ $ allowedSites = str_replace (['__solr_current_site ' , '__current_site ' ], $ siteIdentifierOfPage , $ allowedSitesConfiguration );
185+ return (string )$ allowedSites ;
186+ }
187+
96188 /**
97189 * Retrieves the domain of the site that belongs to the passed pageId and replaces their markers __solr_current_site
98190 * and __current_site.
191+ *
192+ * @deprecated SiteHashService::getDomainByPageIdAndReplaceMarkers() is deprecated and will be removed in v14.
193+ * Use SiteHashService::getSiteIdentifierByPageIdAndReplaceMarkers() instead.
99194 */
100195 protected function getDomainByPageIdAndReplaceMarkers (int $ pageId , string $ allowedSitesConfiguration ): string
101196 {
0 commit comments