Skip to content

Commit

Permalink
+semver:breaking - introduces further abstractions and customisations
Browse files Browse the repository at this point in the history
  • Loading branch information
Philo committed Dec 9, 2016
1 parent 2cadeeb commit c03d043
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 51 deletions.
Binary file modified sample/SitemapifyUmbracoSample/App_Data/Umbraco.sdf
Binary file not shown.
62 changes: 17 additions & 45 deletions src/Sitemapify.Umbraco/SitemapifyUmbracoContentProvider.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,7 +13,7 @@

namespace Sitemapify.Umbraco
{
public class SitemapifyUmbracoContentProvider : EmptySitemapContentProvider
public class SitemapifyUmbracoContentProvider : AbstractSitemapContentProvider<IPublishedContent>
{
private readonly ISitemapifyUmbracoContentProviderSettings _settings;

Expand All @@ -32,37 +30,32 @@ protected UmbracoContext GetUmbracoContext()
return UmbracoContext.EnsureContext(httpContextWrapper, ApplicationContext.Current, new WebSecurity(httpContextWrapper, ApplicationContext.Current));
}

public sealed override IEnumerable<SitemapUrl> 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<IPublishedContent> GetSitemapEntries()
{
var ctx = GetUmbracoContext();
if (ctx != null)
{
var root = FromContent(ctx);
if (root.ItemType != PublishedItemType.Content)
{
return Enumerable.Empty<SitemapUrl>();
}

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<SitemapUrl>();
}

/// <summary>
/// Must return an absolute uri, otherwise the sitemap will be returned blank
/// </summary>
/// <param name="baseUri"></param>
/// <returns></returns>
protected virtual Uri GetBaseUrl(Uri baseUri)
{
return baseUri;
return Enumerable.Empty<IPublishedContent>();
}

/// <summary>
Expand All @@ -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);
}
}
}
71 changes: 71 additions & 0 deletions src/Sitemapify/Providers/Impl/AbstractSitemapContentProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Sitemapify.Models;

namespace Sitemapify.Providers.Impl
{
public abstract class AbstractSitemapContentProvider<T> : ISitemapContentProvider
{
public IEnumerable<SitemapUrl> GetSitemapUrls(Uri baseUrl)
{
var overrideBaseUrl = GetBaseUrl(baseUrl);
var entries = GetSitemapEntries();
if (entries == null)
{
return Enumerable.Empty<SitemapUrl>();
}

return entries
.Where(WhereCore)
.Select(c => CreateSitemapUrl(c, overrideBaseUrl))
.ToList();
}

/// <summary>
/// Returns a <see cref="SitemapUrl"/> composed from <see cref="entry"/>.
/// </summary>
/// <param name="entry">An object that is expected to be represented in the sitemap</param>
/// <param name="baseUri">The host of the request made to the sitemap.xml</param>
/// <returns></returns>
protected abstract SitemapUrl CreateSitemapUrl(T entry, Uri baseUri);

private bool WhereCore(T candidate)
{
if (candidate == null)
{
return false;
}
return Where(candidate);
}

/// <summary>
/// Allows custom logic to be used to determine whether a node should be included in the sitemap, this occurs after all built-in determinations
/// </summary>
/// <param name="candidate"></param>
/// <returns></returns>
protected virtual bool Where(T candidate)
{
return true;
}

/// <summary>
/// Must return an absolute uri, otherwise the sitemap will be returned blank
/// </summary>
/// <param name="baseUri"></param>
/// <returns></returns>
protected virtual Uri GetBaseUrl(Uri baseUri)
{
return baseUri;
}

/// <summary>
/// Retrieves a list of <see cref="T"/> containing entities that should be included in the sitemap
/// </summary>
/// <returns></returns>
protected abstract IEnumerable<T> GetSitemapEntries();

public virtual bool Cacheable { get; } = true;
public virtual DateTime CacheUntil { get; } = DateTime.UtcNow.AddHours(1);
}
}
19 changes: 13 additions & 6 deletions src/Sitemapify/Providers/Impl/EmptySitemapContentProvider.cs
Original file line number Diff line number Diff line change
@@ -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<object>
{
public virtual IEnumerable<SitemapUrl> 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<object> GetSitemapEntries()
{
return Enumerable.Empty<object>();
}
}
}
1 change: 1 addition & 0 deletions src/Sitemapify/Sitemapify.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<Compile Include="Config\ISitemapContainerAdapter.cs" />
<Compile Include="Config\ISitemapifyConfigurer.cs" />
<Compile Include="Config\SitemapifyPreApplicationConfigure.cs" />
<Compile Include="Providers\Impl\AbstractSitemapContentProvider.cs" />
<Compile Include="Providers\Impl\DefaultSitemapCacheProvider.cs" />
<Compile Include="Builders\Impl\DefaultSitemapDocumentBuilder.cs" />
<Compile Include="Providers\Impl\EmptySitemapContentProvider.cs" />
Expand Down

0 comments on commit c03d043

Please sign in to comment.