From c03d0436fd502918e1e7ab8aebaa1a90ecf4d21a Mon Sep 17 00:00:00 2001 From: philo Date: Fri, 9 Dec 2016 10:14:42 +0000 Subject: [PATCH] +semver:breaking - introduces further abstractions and customisations --- .../App_Data/Umbraco.sdf | Bin 1114112 -> 1114112 bytes .../SitemapifyUmbracoContentProvider.cs | 62 +++++---------- .../Impl/AbstractSitemapContentProvider.cs | 71 ++++++++++++++++++ .../Impl/EmptySitemapContentProvider.cs | 19 +++-- src/Sitemapify/Sitemapify.csproj | 1 + 5 files changed, 102 insertions(+), 51 deletions(-) create mode 100644 src/Sitemapify/Providers/Impl/AbstractSitemapContentProvider.cs diff --git a/sample/SitemapifyUmbracoSample/App_Data/Umbraco.sdf b/sample/SitemapifyUmbracoSample/App_Data/Umbraco.sdf index 9dec3409c8e90990eb3a92f4b69cfa899c702b54..256ad395eda325fd7a569ec2009748f212985bea 100644 GIT binary patch delta 121 zcmZo@aA|08VR3%QQL&Nb52ISQ2m^yW3j@OhMh1om1_lPEd{&0HVhjwT5v)KS1DMUt zFcmk9`b004x;8!G?+ delta 121 zcmW;9u?>Pi00q#0a0h~--{1r$(y|0Am{?e7>Oukmd^rYd*96a#kyoB*5;4xt2YTlX9O4(X$c_DL8Fj;_BBUC~lBVkE|*6`h!f Uskjg`F&CHON-V^+xOtNE7em<{l>h($ diff --git a/src/Sitemapify.Umbraco/SitemapifyUmbracoContentProvider.cs b/src/Sitemapify.Umbraco/SitemapifyUmbracoContentProvider.cs index 3145923..792b803 100644 --- a/src/Sitemapify.Umbraco/SitemapifyUmbracoContentProvider.cs +++ b/src/Sitemapify.Umbraco/SitemapifyUmbracoContentProvider.cs @@ -1,10 +1,8 @@ using System; using System.Collections.Generic; -using System.Configuration; using System.Linq; using System.Web; using Sitemapify.Models; -using Sitemapify.Providers; using Sitemapify.Providers.Impl; using Sitemapify.Umbraco.Config; using Sitemapify.Umbraco.Extensions; @@ -15,7 +13,7 @@ namespace Sitemapify.Umbraco { - public class SitemapifyUmbracoContentProvider : EmptySitemapContentProvider + public class SitemapifyUmbracoContentProvider : AbstractSitemapContentProvider { private readonly ISitemapifyUmbracoContentProviderSettings _settings; @@ -32,37 +30,32 @@ protected UmbracoContext GetUmbracoContext() return UmbracoContext.EnsureContext(httpContextWrapper, ApplicationContext.Current, new WebSecurity(httpContextWrapper, ApplicationContext.Current)); } - public sealed override IEnumerable GetSitemapUrls(Uri baseUrl) + protected override SitemapUrl CreateSitemapUrl(IPublishedContent entry, Uri baseUri) + { + var uri = entry.UrlAbsolute(); + if(!Uri.IsWellFormedUriString(uri, UriKind.Absolute)) + { + uri = $"{baseUri}{entry.Url()}"; + } + + var absoluteUri = uri.TrimEnd("/"); + return SitemapUrl.Create(absoluteUri, entry.UpdateDate); + } + + protected sealed override IEnumerable GetSitemapEntries() { var ctx = GetUmbracoContext(); if (ctx != null) { var root = FromContent(ctx); - if (root.ItemType != PublishedItemType.Content) - { - return Enumerable.Empty(); - } - - var overrideBaseUrl = GetBaseUrl(baseUrl); - if (overrideBaseUrl.IsAbsoluteUri) + if (root.ItemType == PublishedItemType.Content) { return root .DescendantSitemapNodes(_settings.ExcludedFromSitemapPropertyAlias, _settings.ExcludedChildrenFromSitemapPropertyAlias) - .Where(node => node.ItemType == PublishedItemType.Content) - .Select(content => CreateSitemapUrlForContent(content, overrideBaseUrl)); + .Where(node => node.ItemType == PublishedItemType.Content); } } - return Enumerable.Empty(); - } - - /// - /// Must return an absolute uri, otherwise the sitemap will be returned blank - /// - /// - /// - protected virtual Uri GetBaseUrl(Uri baseUri) - { - return baseUri; + return Enumerable.Empty(); } /// @@ -78,26 +71,5 @@ protected virtual IPublishedContent FromContent(UmbracoContext context) public override bool Cacheable { get; } = true; public override DateTime CacheUntil { get; } = DateTime.UtcNow.AddHours(1); - - private static SitemapUrl CreateSitemapUrlForContent(IPublishedContent content, Uri authorityUri) - { - var ub = new UriBuilder(authorityUri) - { - Path = content.Url() - }; - var absoluteUri = ub.Uri.ToString().TrimEnd("/"); - return SitemapUrl.Create(absoluteUri, content.UpdateDate); - - //var absoluteUri = content.UrlAbsolute(); - //if (!Uri.IsWellFormedUriString(absoluteUri, UriKind.Absolute)) - //{ - // var ub = new UriBuilder(authorityUri) - // { - // Path = content.Url() - // }; - // absoluteUri = ub.ToString().TrimEnd("/"); - //} - //return SitemapUrl.Create(absoluteUri, content.UpdateDate); - } } } \ No newline at end of file diff --git a/src/Sitemapify/Providers/Impl/AbstractSitemapContentProvider.cs b/src/Sitemapify/Providers/Impl/AbstractSitemapContentProvider.cs new file mode 100644 index 0000000..daf286a --- /dev/null +++ b/src/Sitemapify/Providers/Impl/AbstractSitemapContentProvider.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Sitemapify.Models; + +namespace Sitemapify.Providers.Impl +{ + public abstract class AbstractSitemapContentProvider : ISitemapContentProvider + { + public IEnumerable GetSitemapUrls(Uri baseUrl) + { + var overrideBaseUrl = GetBaseUrl(baseUrl); + var entries = GetSitemapEntries(); + if (entries == null) + { + return Enumerable.Empty(); + } + + return entries + .Where(WhereCore) + .Select(c => CreateSitemapUrl(c, overrideBaseUrl)) + .ToList(); + } + + /// + /// Returns a composed from . + /// + /// An object that is expected to be represented in the sitemap + /// The host of the request made to the sitemap.xml + /// + protected abstract SitemapUrl CreateSitemapUrl(T entry, Uri baseUri); + + private bool WhereCore(T candidate) + { + if (candidate == null) + { + return false; + } + return Where(candidate); + } + + /// + /// Allows custom logic to be used to determine whether a node should be included in the sitemap, this occurs after all built-in determinations + /// + /// + /// + protected virtual bool Where(T candidate) + { + return true; + } + + /// + /// Must return an absolute uri, otherwise the sitemap will be returned blank + /// + /// + /// + protected virtual Uri GetBaseUrl(Uri baseUri) + { + return baseUri; + } + + /// + /// Retrieves a list of containing entities that should be included in the sitemap + /// + /// + protected abstract IEnumerable GetSitemapEntries(); + + public virtual bool Cacheable { get; } = true; + public virtual DateTime CacheUntil { get; } = DateTime.UtcNow.AddHours(1); + } +} \ No newline at end of file diff --git a/src/Sitemapify/Providers/Impl/EmptySitemapContentProvider.cs b/src/Sitemapify/Providers/Impl/EmptySitemapContentProvider.cs index 06b39c4..5b0e14e 100644 --- a/src/Sitemapify/Providers/Impl/EmptySitemapContentProvider.cs +++ b/src/Sitemapify/Providers/Impl/EmptySitemapContentProvider.cs @@ -1,18 +1,25 @@ using System; using System.Collections.Generic; +using System.Linq; using Sitemapify.Models; namespace Sitemapify.Providers.Impl { - public class EmptySitemapContentProvider : ISitemapContentProvider + public class EmptySitemapContentProvider : AbstractSitemapContentProvider { - public virtual IEnumerable GetSitemapUrls(Uri baseUrl) + protected override SitemapUrl CreateSitemapUrl(object entry, Uri baseUri) { - var ub = new UriBuilder(baseUrl) {Path = "/"}; - yield return SitemapUrl.Create(ub.ToString()); + var ub = new UriBuilder(baseUri) + { + Path = "/" + + }; + return SitemapUrl.Create(ub.Uri.ToString(), changeFreq: SitemapChangeFrequency.Never); } - public virtual bool Cacheable { get; } = true; - public virtual DateTime CacheUntil { get; } = DateTime.UtcNow.AddHours(1); + protected override IEnumerable GetSitemapEntries() + { + return Enumerable.Empty(); + } } } \ No newline at end of file diff --git a/src/Sitemapify/Sitemapify.csproj b/src/Sitemapify/Sitemapify.csproj index 43011fb..c4013f6 100644 --- a/src/Sitemapify/Sitemapify.csproj +++ b/src/Sitemapify/Sitemapify.csproj @@ -51,6 +51,7 @@ +