From ad8d924740ac80a2a99dd00a8cd9572979194e33 Mon Sep 17 00:00:00 2001 From: Odd Skancke Date: Wed, 24 Jun 2026 10:14:02 +0200 Subject: [PATCH] switch from SiteDefinition to the new Application framework --- CHANGELOG.md | 13 +++- .../CommerceAndStandardSitemapXmlGenerator.cs | 10 ++-- .../CommerceSitemapXmlGenerator.cs | 7 +-- .../Pages/Index.cshtml.cs | 56 +++++++++-------- .../Models/SitemapViewModel.cs | 20 +++++-- .../Repositories/SitemapRepository.cs | 24 ++++---- .../SitemapCreateJob.cs | 3 +- .../PropertySEOSitemaps.cs | 4 +- .../Utils/ContentFilter.cs | 2 +- .../Utils/HostDefinitionExtensions.cs | 23 ------- .../Utils/IContentFilter.cs | 4 +- .../XML/MobileSitemapXmlGenerator.cs | 6 +- .../XML/SitemapXmlGenerator.cs | 60 +++++++++++-------- .../XML/StandardSitemapXmlGenerator.cs | 6 +- 14 files changed, 119 insertions(+), 119 deletions(-) delete mode 100644 src/Geta.Optimizely.Sitemaps/Utils/HostDefinitionExtensions.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a519fcf..dfe7b7c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,17 @@ # Changelog All notable changes to this project will be documented in this file. +## [5.0.0] + +- Switch from SiteDefinition to the new Application framework +- Remove remaining obsolete warnings + +### Breaking changes + +- IContentFilter: Signature changed on ShouldExcludeContent (removed SiteDefinition parameter) +- All use of ISiteDefinitionRepository replaced by IApplicationRepository +- XmlGenerator: SiteDefinition property replaced by CurrentSite +- HostDefinitionExtensions removed ## [4.0.0] @@ -28,4 +39,4 @@ All notable changes to this project will be documented in this file. ## [1.0.0] -- Initial version +- Initial version \ No newline at end of file diff --git a/src/Geta.Optimizely.Sitemaps.Commerce/CommerceAndStandardSitemapXmlGenerator.cs b/src/Geta.Optimizely.Sitemaps.Commerce/CommerceAndStandardSitemapXmlGenerator.cs index 3df4974a..f8fc509b 100644 --- a/src/Geta.Optimizely.Sitemaps.Commerce/CommerceAndStandardSitemapXmlGenerator.cs +++ b/src/Geta.Optimizely.Sitemaps.Commerce/CommerceAndStandardSitemapXmlGenerator.cs @@ -5,9 +5,9 @@ using System.Linq; using System.Xml.Linq; using EPiServer; +using EPiServer.Applications; using EPiServer.DataAbstraction; using EPiServer.Framework.Cache; -using EPiServer.Web; using EPiServer.Web.Routing; using Geta.Optimizely.Sitemaps.Repositories; using Geta.Optimizely.Sitemaps.Services; @@ -30,7 +30,7 @@ public CommerceAndStandardSitemapXmlGenerator( ISitemapRepository sitemapRepository, IContentRepository contentRepository, IUrlResolver urlResolver, - ISiteDefinitionRepository siteDefinitionRepository, + IApplicationRepository applicationRepository, ILanguageBranchRepository languageBranchRepository, ReferenceConverter referenceConverter, IContentFilter contentFilter, @@ -42,7 +42,7 @@ public CommerceAndStandardSitemapXmlGenerator( sitemapRepository, contentRepository, urlResolver, - siteDefinitionRepository, + applicationRepository, languageBranchRepository, referenceConverter, contentFilter, @@ -55,9 +55,9 @@ public CommerceAndStandardSitemapXmlGenerator( protected override IEnumerable GetSitemapXmlElements() { - var contentDescendants = ContentRepository.GetDescendents(this.SiteSettings.StartPage).ToList(); + var contentDescendants = ContentRepository.GetDescendents(this.CurrentSite.EntryPoint).ToList(); - contentDescendants.Insert(0, SiteSettings.StartPage); + contentDescendants.Insert(0, CurrentSite.EntryPoint); var contentElements = GenerateXmlElements(contentDescendants); return contentElements.Union(base.GetSitemapXmlElements()); diff --git a/src/Geta.Optimizely.Sitemaps.Commerce/CommerceSitemapXmlGenerator.cs b/src/Geta.Optimizely.Sitemaps.Commerce/CommerceSitemapXmlGenerator.cs index 2d8063bf..d08dfd9c 100644 --- a/src/Geta.Optimizely.Sitemaps.Commerce/CommerceSitemapXmlGenerator.cs +++ b/src/Geta.Optimizely.Sitemaps.Commerce/CommerceSitemapXmlGenerator.cs @@ -3,13 +3,12 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Xml.Linq; using EPiServer; +using EPiServer.Applications; using EPiServer.Core; using EPiServer.DataAbstraction; using EPiServer.Framework.Cache; -using EPiServer.Web; using EPiServer.Web.Routing; using Geta.Optimizely.Sitemaps.Repositories; using Geta.Optimizely.Sitemaps.Services; @@ -33,7 +32,7 @@ public CommerceSitemapXmlGenerator( ISitemapRepository sitemapRepository, IContentRepository contentRepository, IUrlResolver urlResolver, - ISiteDefinitionRepository siteDefinitionRepository, + IApplicationRepository applicationRepository, ILanguageBranchRepository languageBranchRepository, ReferenceConverter referenceConverter, IContentFilter contentFilter, @@ -45,7 +44,7 @@ public CommerceSitemapXmlGenerator( sitemapRepository, contentRepository, urlResolver, - siteDefinitionRepository, + applicationRepository, languageBranchRepository, contentFilter, uriAugmenterService, diff --git a/src/Geta.Optimizely.Sitemaps/Areas/GetaOptimizelySitemaps/Pages/Index.cshtml.cs b/src/Geta.Optimizely.Sitemaps/Areas/GetaOptimizelySitemaps/Pages/Index.cshtml.cs index 5a597ece..94c5bfc2 100644 --- a/src/Geta.Optimizely.Sitemaps/Areas/GetaOptimizelySitemaps/Pages/Index.cshtml.cs +++ b/src/Geta.Optimizely.Sitemaps/Areas/GetaOptimizelySitemaps/Pages/Index.cshtml.cs @@ -1,8 +1,8 @@ using System.Collections.Generic; using System.Linq; +using EPiServer.Applications; using EPiServer.Data; using EPiServer.DataAbstraction; -using EPiServer.Web; using Geta.Mapping; using Geta.Optimizely.Sitemaps.Entities; using Geta.Optimizely.Sitemaps.Models; @@ -19,20 +19,20 @@ namespace Geta.Optimizely.Sitemaps.Pages.Geta.Optimizely.Sitemaps; public class IndexModel : PageModel { private readonly ISitemapRepository _sitemapRepository; - private readonly ISiteDefinitionRepository _siteDefinitionRepository; + private readonly IApplicationRepository _applicationRepository; private readonly ILanguageBranchRepository _languageBranchRepository; private readonly IMapper _modelToEntityMapper; private readonly ICreateFrom _entityToModelCreator; public IndexModel( ISitemapRepository sitemapRepository, - ISiteDefinitionRepository siteDefinitionRepository, + IApplicationRepository applicationRepository, ILanguageBranchRepository languageBranchRepository, IMapper modelToEntityMapper, ICreateFrom entityToModelCreator) { _sitemapRepository = sitemapRepository; - _siteDefinitionRepository = siteDefinitionRepository; + _applicationRepository = applicationRepository; _languageBranchRepository = languageBranchRepository; _modelToEntityMapper = modelToEntityMapper; _entityToModelCreator = entityToModelCreator; @@ -161,36 +161,34 @@ private void BindSitemapDataList() private void LoadSiteHosts() { - var hosts = _siteDefinitionRepository.List().ToList(); + var sites = _applicationRepository + .List() + .OfType() + .ToList(); - var siteUrls = new List(hosts.Count); - - foreach (var siteInformation in hosts) + var urls = new List(); + foreach (var site in sites) { - var siteUrl = siteInformation.SiteUrl.ToString(); - siteUrls.Add(new() + if (site.Url != null) { - Text = siteUrl, - Value = siteUrl - }); - - var hostUrls = siteInformation.Hosts - .Where(host => ShouldAddToSiteHosts(host, siteInformation)) - .Select(host => host.GetUri()) - .Select(hostUri => new SelectListItem { Text = hostUri.ToString(), Value = hostUri.ToString() }); - siteUrls.AddRange(hostUrls); - } - - SiteHosts = siteUrls; - } + urls.Add(new SelectListItem { Text = site.Url.Host, Value = site.Url.Host }); + } - private static bool ShouldAddToSiteHosts(HostDefinition host, SiteDefinition siteInformation) - { - if (host.Name == "*") - { - return false; + foreach (var host in site.Hosts.Where(x => x.Url != null)) + { + if (UriComparer.SchemeAndServerEquals(site.Url, host.Url)) + { + continue; + } + + urls.Add(new SelectListItem + { + Text = host.Url!.ToString(), + Value = host.Url!.ToString() + }); + } } - return !UriComparer.SchemeAndServerEquals(host.GetUri(), siteInformation.SiteUrl); + SiteHosts = urls; } private void PopulateHostListControl(string selected = null) diff --git a/src/Geta.Optimizely.Sitemaps/Models/SitemapViewModel.cs b/src/Geta.Optimizely.Sitemaps/Models/SitemapViewModel.cs index a1a5d827..8e4e590f 100644 --- a/src/Geta.Optimizely.Sitemaps/Models/SitemapViewModel.cs +++ b/src/Geta.Optimizely.Sitemaps/Models/SitemapViewModel.cs @@ -1,7 +1,8 @@ using System; using System.Collections.Generic; +using EPiServer; +using EPiServer.Applications; using EPiServer.DataAbstraction; -using EPiServer.Web; using Geta.Mapping; using Geta.Optimizely.Sitemaps.Entities; @@ -30,10 +31,12 @@ public class SitemapViewModel public class MapperFromEntity : Mapper { private readonly ILanguageBranchRepository _languageBranchRepository; + private readonly IApplicationResolver _applicationResolver; - public MapperFromEntity(ILanguageBranchRepository languageBranchRepository) + public MapperFromEntity(ILanguageBranchRepository languageBranchRepository, IApplicationResolver applicationResolver) { _languageBranchRepository = languageBranchRepository; + _applicationResolver = applicationResolver; } public override void Map(SitemapData @from, SitemapViewModel to) @@ -57,7 +60,7 @@ public override void Map(SitemapData @from, SitemapViewModel to) private string GetLanguage(string language) { - if (string.IsNullOrWhiteSpace(language) || SiteDefinition.WildcardHostName.Equals(language)) + if (string.IsNullOrWhiteSpace(language)) { return string.Empty; } @@ -75,9 +78,16 @@ private string GetSitemapUrl(SitemapData sitemapData) return $"{sitemapData.SiteUrl}{language}{sitemapData.Host}"; } - var site = SiteDefinition.Current.SiteUrl.ToString(); + var siteUrl = new Url(sitemapData.SiteUrl); + var app = _applicationResolver.GetByHostname(siteUrl.Host, true).Application; - return $"{site}{language}{sitemapData.Host}"; + if (app is IRoutableApplication site) + { + return $"{site.Url}{language}{sitemapData.Host}"; + } + + site = _applicationResolver.GetByContext() as IRoutableApplication; + return site != null ? $"{site.Url}{language}{sitemapData.Host}" : string.Empty; } private static string GetRelativePathEditPart(string hostName) diff --git a/src/Geta.Optimizely.Sitemaps/Repositories/SitemapRepository.cs b/src/Geta.Optimizely.Sitemaps/Repositories/SitemapRepository.cs index f5a8d0d2..d644aec0 100644 --- a/src/Geta.Optimizely.Sitemaps/Repositories/SitemapRepository.cs +++ b/src/Geta.Optimizely.Sitemaps/Repositories/SitemapRepository.cs @@ -5,9 +5,9 @@ using System.Collections.Generic; using System.Linq; using EPiServer; +using EPiServer.Applications; using EPiServer.Data; using EPiServer.DataAbstraction; -using EPiServer.Web; using Geta.Optimizely.Sitemaps.Entities; namespace Geta.Optimizely.Sitemaps.Repositories @@ -15,17 +15,17 @@ namespace Geta.Optimizely.Sitemaps.Repositories public class SitemapRepository : ISitemapRepository { private readonly ILanguageBranchRepository _languageBranchRepository; - private readonly ISiteDefinitionResolver _siteDefinitionResolver; + private readonly IApplicationResolver _applicationResolver; private readonly ISitemapLoader _sitemapLoader; public SitemapRepository( ILanguageBranchRepository languageBranchRepository, - ISiteDefinitionResolver siteDefinitionResolver, + IApplicationResolver applicationResolver, ISitemapLoader sitemapLoader) { _languageBranchRepository = languageBranchRepository ?? throw new ArgumentNullException(nameof(languageBranchRepository)); - _siteDefinitionResolver = siteDefinitionResolver ?? throw new ArgumentNullException(nameof(siteDefinitionResolver)); + _applicationResolver = applicationResolver ?? throw new ArgumentNullException(nameof(applicationResolver)); _sitemapLoader = sitemapLoader ?? throw new ArgumentNullException(nameof(sitemapLoader)); } @@ -46,21 +46,19 @@ public SitemapData GetSitemapData(string requestUrl) // contains the sitemap URL, for example en/sitemap.xml var host = url.Path.TrimStart('/').ToLowerInvariant(); - //Get the site based on just the host - var siteDefinition = _siteDefinitionResolver.GetByHostname(url.Host, true, out _); - if (siteDefinition == null) - { - //If that didn't work, also include the port - siteDefinition = _siteDefinitionResolver.GetByHostname($"{url.Host}:{url.Port}", true, out _); - } - if (siteDefinition == null) + // First attempt to get the site based on just the host. + // If that fails, try to include the port and fallback to default if none is found. + var app = _applicationResolver.GetByHostname(url.Host, false).Application ?? + _applicationResolver.GetByHostname($"{url.Host}:{url.Port}", true).Application; + + if (app is not IRoutableApplication site) { return null; } var sitemapData = GetAllSitemapData()?.Where(x => GetHostWithLanguage(x) == host && - (x.SiteUrl == null || siteDefinition.Hosts.Any(h => h.Name == new Url(x.SiteUrl).Authority))).ToList(); + (x.SiteUrl == null || site.Hosts.Any(h => h.Authority == new Url(x.SiteUrl).Authority))).ToList(); if (sitemapData?.Count == 1) { diff --git a/src/Geta.Optimizely.Sitemaps/SitemapCreateJob.cs b/src/Geta.Optimizely.Sitemaps/SitemapCreateJob.cs index 1cbc0ead..cf0790d0 100644 --- a/src/Geta.Optimizely.Sitemaps/SitemapCreateJob.cs +++ b/src/Geta.Optimizely.Sitemaps/SitemapCreateJob.cs @@ -6,7 +6,6 @@ using System.Linq; using System.Text; using EPiServer.Framework.Cache; -using EPiServer.PlugIn; using EPiServer.Scheduler; using Geta.Optimizely.Sitemaps.Entities; using Geta.Optimizely.Sitemaps.Repositories; @@ -15,7 +14,7 @@ namespace Geta.Optimizely.Sitemaps { - [ScheduledPlugIn(GUID = "EC74D2A3-9D77-4265-B4FF-A1935E3C3110", DisplayName = "Generate search engine sitemaps")] + [ScheduledJob(GUID = "EC74D2A3-9D77-4265-B4FF-A1935E3C3110", DisplayName = "Generate search engine sitemaps")] public class SitemapCreateJob : ScheduledJobBase { public const string SitemapGenerationCacheKey = "SitemapGenerationKey"; diff --git a/src/Geta.Optimizely.Sitemaps/SpecializedProperties/PropertySEOSitemaps.cs b/src/Geta.Optimizely.Sitemaps/SpecializedProperties/PropertySEOSitemaps.cs index 88855ba2..6fc06139 100644 --- a/src/Geta.Optimizely.Sitemaps/SpecializedProperties/PropertySEOSitemaps.cs +++ b/src/Geta.Optimizely.Sitemaps/SpecializedProperties/PropertySEOSitemaps.cs @@ -10,11 +10,11 @@ using System.Xml; using System.Xml.Serialization; using EPiServer.Core; -using EPiServer.PlugIn; +using EPiServer.DataAnnotations; namespace Geta.Optimizely.Sitemaps.SpecializedProperties { - [PropertyDefinitionTypePlugIn(DisplayName = "SEOSitemaps")] + [PropertyDefinitionType(DisplayName = "SEOSitemaps")] public class PropertySEOSitemaps : PropertyString { public const string PropertyName = "SEOSitemaps"; diff --git a/src/Geta.Optimizely.Sitemaps/Utils/ContentFilter.cs b/src/Geta.Optimizely.Sitemaps/Utils/ContentFilter.cs index 9e756748..29b1a514 100644 --- a/src/Geta.Optimizely.Sitemaps/Utils/ContentFilter.cs +++ b/src/Geta.Optimizely.Sitemaps/Utils/ContentFilter.cs @@ -83,7 +83,7 @@ public virtual bool ShouldExcludeContent(IContent content) return false; } - public virtual bool ShouldExcludeContent(CurrentLanguageContent languageContentInfo, SiteDefinition siteSettings, SitemapData sitemapData) + public virtual bool ShouldExcludeContent(CurrentLanguageContent languageContentInfo, SitemapData sitemapData) { return ShouldExcludeContent(languageContentInfo.Content); } diff --git a/src/Geta.Optimizely.Sitemaps/Utils/HostDefinitionExtensions.cs b/src/Geta.Optimizely.Sitemaps/Utils/HostDefinitionExtensions.cs deleted file mode 100644 index a3182706..00000000 --- a/src/Geta.Optimizely.Sitemaps/Utils/HostDefinitionExtensions.cs +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright (c) Geta Digital. All rights reserved. -// Licensed under Apache-2.0. See the LICENSE file in the project root for more information - -using System; -using EPiServer.Web; - -namespace Geta.Optimizely.Sitemaps.Utils -{ - public static class HostDefinitionExtensions - { - public static Uri GetUri(this HostDefinition host) - { - var scheme = "http"; - if (host.UseSecureConnection != null && host.UseSecureConnection == true) - { - scheme = "https"; - } - - var hostUrl = $"{scheme}://{host.Name}/"; - return new Uri(hostUrl); - } - } -} \ No newline at end of file diff --git a/src/Geta.Optimizely.Sitemaps/Utils/IContentFilter.cs b/src/Geta.Optimizely.Sitemaps/Utils/IContentFilter.cs index 1d1ac668..7e0a133a 100644 --- a/src/Geta.Optimizely.Sitemaps/Utils/IContentFilter.cs +++ b/src/Geta.Optimizely.Sitemaps/Utils/IContentFilter.cs @@ -2,7 +2,6 @@ // Licensed under Apache-2.0. See the LICENSE file in the project root for more information using EPiServer.Core; -using EPiServer.Web; using Geta.Optimizely.Sitemaps.Entities; namespace Geta.Optimizely.Sitemaps.Utils @@ -10,7 +9,6 @@ namespace Geta.Optimizely.Sitemaps.Utils public interface IContentFilter { bool ShouldExcludeContent(IContent content); - bool ShouldExcludeContent( - CurrentLanguageContent languageContentInfo, SiteDefinition siteSettings, SitemapData sitemapData); + bool ShouldExcludeContent(CurrentLanguageContent languageContentInfo, SitemapData sitemapData); } } diff --git a/src/Geta.Optimizely.Sitemaps/XML/MobileSitemapXmlGenerator.cs b/src/Geta.Optimizely.Sitemaps/XML/MobileSitemapXmlGenerator.cs index 54210727..da2c1d02 100644 --- a/src/Geta.Optimizely.Sitemaps/XML/MobileSitemapXmlGenerator.cs +++ b/src/Geta.Optimizely.Sitemaps/XML/MobileSitemapXmlGenerator.cs @@ -3,10 +3,10 @@ using System.Xml.Linq; using EPiServer; +using EPiServer.Applications; using EPiServer.Core; using EPiServer.DataAbstraction; using EPiServer.Framework.Cache; -using EPiServer.Web; using EPiServer.Web.Routing; using Geta.Optimizely.Sitemaps.Repositories; using Geta.Optimizely.Sitemaps.Services; @@ -22,7 +22,7 @@ public MobileSitemapXmlGenerator( ISitemapRepository sitemapRepository, IContentRepository contentRepository, UrlResolver urlResolver, - ISiteDefinitionRepository siteDefinitionRepository, + IApplicationRepository applicationRepository, ILanguageBranchRepository languageBranchRepository, IContentFilter contentFilter, IUriAugmenterService uriAugmenterService, @@ -33,7 +33,7 @@ public MobileSitemapXmlGenerator( sitemapRepository, contentRepository, urlResolver, - siteDefinitionRepository, + applicationRepository, languageBranchRepository, contentFilter, uriAugmenterService, diff --git a/src/Geta.Optimizely.Sitemaps/XML/SitemapXmlGenerator.cs b/src/Geta.Optimizely.Sitemaps/XML/SitemapXmlGenerator.cs index 813c0d6e..d55d84fe 100644 --- a/src/Geta.Optimizely.Sitemaps/XML/SitemapXmlGenerator.cs +++ b/src/Geta.Optimizely.Sitemaps/XML/SitemapXmlGenerator.cs @@ -11,6 +11,7 @@ using System.Xml; using System.Xml.Linq; using EPiServer; +using EPiServer.Applications; using EPiServer.Core; using EPiServer.DataAbstraction; using EPiServer.Framework.Cache; @@ -39,7 +40,7 @@ public abstract class SitemapXmlGenerator : ISitemapXmlGenerator protected readonly ISitemapRepository SitemapRepository; protected readonly IContentRepository ContentRepository; protected readonly IUrlResolver UrlResolver; - protected readonly ISiteDefinitionRepository SiteDefinitionRepository; + protected readonly IApplicationRepository ApplicationRepository; protected readonly ILanguageBranchRepository LanguageBranchRepository; protected readonly IContentFilter ContentFilter; private readonly IUriAugmenterService _uriAugmenterService; @@ -48,7 +49,7 @@ public abstract class SitemapXmlGenerator : ISitemapXmlGenerator private readonly ILogger _logger; protected SitemapData SitemapData { get; set; } - protected SiteDefinition SiteSettings { get; set; } + protected IRoutableApplication CurrentSite { get; set; } protected IEnumerable EnabledLanguages { get; set; } protected IEnumerable HrefLanguageContents { get; set; } @@ -64,7 +65,7 @@ protected SitemapXmlGenerator( ISitemapRepository sitemapRepository, IContentRepository contentRepository, IUrlResolver urlResolver, - ISiteDefinitionRepository siteDefinitionRepository, + IApplicationRepository applicationRepository, ILanguageBranchRepository languageBranchRepository, IContentFilter contentFilter, IUriAugmenterService uriAugmenterService, @@ -75,7 +76,7 @@ protected SitemapXmlGenerator( SitemapRepository = sitemapRepository; ContentRepository = contentRepository; UrlResolver = urlResolver; - SiteDefinitionRepository = siteDefinitionRepository; + ApplicationRepository = applicationRepository; LanguageBranchRepository = languageBranchRepository; EnabledLanguages = LanguageBranchRepository.ListEnabled(); UrlSet = new HashSet(); @@ -111,7 +112,7 @@ public virtual bool Generate(SitemapData sitemapData, bool persistData, out int { SitemapData = sitemapData; var sitemapSiteUri = new Uri(SitemapData.SiteUrl); - SiteSettings = GetSiteDefinitionFromSiteUri(sitemapSiteUri); + CurrentSite = GetApplicationFromSiteUri(sitemapSiteUri); HostLanguageBranch = GetHostLanguageBranch(); var sitemap = CreateSitemapXmlContents(out entryCount); @@ -166,12 +167,12 @@ private XElement CreateSitemapXmlContents(out int entryCount) protected virtual IEnumerable GetSitemapXmlElements() { - if (SiteSettings == null) + if (CurrentSite == null) { return Enumerable.Empty(); } - var rootPage = SitemapData.RootPageId < 0 ? SiteSettings.StartPage : new ContentReference(SitemapData.RootPageId); + var rootPage = SitemapData.RootPageId < 0 ? CurrentSite.EntryPoint : new ContentReference(SitemapData.RootPageId); var descendants = ContentRepository.GetDescendents(rootPage).ToList(); @@ -353,7 +354,7 @@ protected virtual HrefLangData CreateHrefLangData(IContent content, CultureInfo var data = new HrefLangData(); - if (languageUrl.Equals(masterLanguageUrl) && content.ContentLink.CompareToIgnoreWorkID(SiteSettings.StartPage)) + if (languageUrl.Equals(masterLanguageUrl) && content.ContentLink.CompareToIgnoreWorkID(CurrentSite.EntryPoint)) { data.HrefLang = "x-default"; } @@ -440,7 +441,7 @@ protected virtual void AddFilteredContentElement( CurrentLanguageContent languageContentInfo, IList xmlElements) { - if (ContentFilter.ShouldExcludeContent(languageContentInfo, SiteSettings, SitemapData)) + if (ContentFilter.ShouldExcludeContent(languageContentInfo, SitemapData)) { return; } @@ -520,20 +521,20 @@ protected CultureInfo GetMasterLanguage(IContent content) return CultureInfo.InvariantCulture; } - public SiteDefinition GetSiteDefinitionFromSiteUri(Uri sitemapSiteUri) + public IRoutableApplication GetApplicationFromSiteUri(Uri sitemapSiteUri) { - var siteDefinitions = SiteDefinitionRepository.List().ToList(); - - return siteDefinitions - .FirstOrDefault(siteDef => siteDef.SiteUrl == sitemapSiteUri || siteDef.Hosts.Any( - hostDef => hostDef.Name.Equals(sitemapSiteUri.Authority, - StringComparison.InvariantCultureIgnoreCase))); + var sites = ApplicationRepository.List().OfType().ToList(); + return sites.FirstOrDefault(s => + s.Url == sitemapSiteUri || + s.Hosts.Any(i => + !string.IsNullOrWhiteSpace(i.Authority) && + i.Authority.Equals(sitemapSiteUri.Authority, StringComparison.InvariantCultureIgnoreCase))); } protected string GetHostLanguageBranch() { - var hostDefinition = GetHostDefinition(); - return hostDefinition?.Language?.Name; + var appHost = GetApplicationHost(); + return appHost?.Locale?.Name; } protected bool HostDefinitionExistsForLanguage(string languageBranch) @@ -547,24 +548,33 @@ protected bool HostDefinitionExistsForLanguage(string languageBranch) } cachedObject = - SiteSettings.Hosts.Any( + CurrentSite.Hosts.Any( x => - x.Language != null - && x.Language.ToString().Equals(languageBranch, StringComparison.InvariantCultureIgnoreCase)); + x.Locale != null + && x.Locale.Name.Equals(languageBranch, StringComparison.InvariantCultureIgnoreCase)); _memoryCache.Set(cacheKey, cachedObject, DateTime.Now.AddMinutes(10)); return (bool)cachedObject; } - protected HostDefinition GetHostDefinition() + protected ApplicationHost GetApplicationHost() { var siteUrl = new Uri(SitemapData.SiteUrl); var sitemapHost = siteUrl.Authority; - var hosts = SiteSettings?.Hosts; - return hosts?.FirstOrDefault(x => x.Name.Equals(sitemapHost, StringComparison.InvariantCultureIgnoreCase)) ?? - hosts?.FirstOrDefault(x => x.Name.Equals(SiteDefinition.WildcardHostName)); + if (CurrentSite == null) + { + return null; + } + + var host = CurrentSite.Hosts.FirstOrDefault(x => + !string.IsNullOrWhiteSpace(x.Authority) && + x.Authority.Equals(sitemapHost, StringComparison.InvariantCultureIgnoreCase)); + + return host ?? + CurrentSite.Hosts.FirstOrDefault(x => x.Type == ApplicationHostType.Primary) ?? + CurrentSite.Hosts.FirstOrDefault(x => x.Type == ApplicationHostType.Default); } protected bool ExcludeContentLanguageFromSitemap(CultureInfo language) diff --git a/src/Geta.Optimizely.Sitemaps/XML/StandardSitemapXmlGenerator.cs b/src/Geta.Optimizely.Sitemaps/XML/StandardSitemapXmlGenerator.cs index 65a2a4de..7240f14c 100644 --- a/src/Geta.Optimizely.Sitemaps/XML/StandardSitemapXmlGenerator.cs +++ b/src/Geta.Optimizely.Sitemaps/XML/StandardSitemapXmlGenerator.cs @@ -2,9 +2,9 @@ // Licensed under Apache-2.0. See the LICENSE file in the project root for more information using EPiServer; +using EPiServer.Applications; using EPiServer.DataAbstraction; using EPiServer.Framework.Cache; -using EPiServer.Web; using EPiServer.Web.Routing; using Geta.Optimizely.Sitemaps.Repositories; using Geta.Optimizely.Sitemaps.Services; @@ -20,7 +20,7 @@ public StandardSitemapXmlGenerator( ISitemapRepository sitemapRepository, IContentRepository contentRepository, IUrlResolver urlResolver, - ISiteDefinitionRepository siteDefinitionRepository, + IApplicationRepository applicationRepository, ILanguageBranchRepository languageBranchRepository, IContentFilter contentFilter, IUriAugmenterService uriAugmenterService, @@ -31,7 +31,7 @@ public StandardSitemapXmlGenerator( sitemapRepository, contentRepository, urlResolver, - siteDefinitionRepository, + applicationRepository, languageBranchRepository, contentFilter, uriAugmenterService,