Skip to content

Commit

Permalink
+semver:breaking - refactors to support more customisation options du…
Browse files Browse the repository at this point in the history
…ring the sitemap creation process, now requires explicit introduction of application event handler for Umbraco support
  • Loading branch information
Philo committed Dec 8, 2016
1 parent 4adeb29 commit 3804e8d
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 55 deletions.
Binary file modified sample/SitemapifyUmbracoSample/App_Data/Umbraco.sdf
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using Sitemapify;
using Sitemapify.Providers;
using Sitemapify.Providers.Impl;
using Sitemapify.Config;
using Sitemapify.Umbraco;
using Umbraco.Core;
using Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSix;
using Sitemapify.Umbraco.Events;

namespace SitemapifyUmbracoSample.Code
{
public class SitemapifyApplicationEventHandler : ApplicationEventHandler
public class MySitemapifyUmbracoContentProvider : SitemapifyUmbracoContentProvider
{
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
protected override Uri GetBaseUrl(Uri baseUri)
{
Configure.With(config => config.UsingContentProvider(new SitemapifyUmbracoContentProvider()));
return new Uri("https://github.com/stormid/sitemapify", UriKind.Absolute);
}
}

protected override bool ExecuteWhenApplicationNotConfigured { get; } = false;
protected override bool ExecuteWhenDatabaseNotConfigured { get; } = false;
public class SitemapifyApplicationEventHandler : AbstractSitemapifyApplicationEventHandler
{
protected override void ConfigureWith(ISitemapifyConfigurer configure)
{
configure.UsingContentProvider(new MySitemapifyUmbracoContentProvider());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Sitemapify.Config;
using Umbraco.Core;

namespace Sitemapify.Umbraco.Events
{
public abstract class AbstractSitemapifyApplicationEventHandler : ApplicationEventHandler
{
protected sealed override void ApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
base.ApplicationInitialized(umbracoApplication, applicationContext);
}

protected sealed override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
base.ApplicationStarted(umbracoApplication, applicationContext);
}

protected sealed override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
Configure.With(ConfigureWith);
}

protected virtual void ConfigureWith(ISitemapifyConfigurer configure)
{
configure.UsingContentProvider(new SitemapifyUmbracoContentProvider());
}

protected sealed override bool ExecuteWhenApplicationNotConfigured { get; } = false;
protected sealed override bool ExecuteWhenDatabaseNotConfigured { get; } = false;
}
}
18 changes: 0 additions & 18 deletions src/Sitemapify.Umbraco/Events/SitemapifyApplicationEventHandler.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Sitemapify.Umbraco/Sitemapify.Umbraco.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Config\ConfigurationManagerUtils.cs" />
<Compile Include="Events\SitemapifyApplicationEventHandler.cs" />
<Compile Include="Events\AbstractSitemapifyApplicationEventHandler.cs" />
<Compile Include="Events\SitemapifyCacheClearApplicationEventHandler.cs" />
<Compile Include="Extensions\ContentExtensions.cs" />
<Compile Include="Config\FromConfigSitemapifyUmbracoContentProviderSettings.cs" />
Expand Down
63 changes: 52 additions & 11 deletions src/Sitemapify.Umbraco/SitemapifyUmbracoContentProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Web;
using Sitemapify.Models;
using Sitemapify.Providers;
using Sitemapify.Providers.Impl;
using Sitemapify.Umbraco.Config;
using Sitemapify.Umbraco.Extensions;
using Umbraco.Core;
Expand All @@ -14,7 +15,7 @@

namespace Sitemapify.Umbraco
{
public class SitemapifyUmbracoContentProvider : ISitemapContentProvider
public class SitemapifyUmbracoContentProvider : EmptySitemapContentProvider
{
private readonly ISitemapifyUmbracoContentProviderSettings _settings;

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

public IEnumerable<SitemapUrl> GetSitemapUrls()
public sealed override IEnumerable<SitemapUrl> GetSitemapUrls(Uri baseUrl)
{
var ctx = GetUmbracoContext();
if (ctx != null)
{
var home = FromContent(ctx);
return home
.DescendantSitemapNodes(_settings.ExcludedFromSitemapPropertyAlias, _settings.ExcludedChildrenFromSitemapPropertyAlias)
.Where(node => node.ItemType == PublishedItemType.Content)
.Select(CreateSitemapUrlForContent);
var root = FromContent(ctx);
if (root.ItemType != PublishedItemType.Content)
{
return Enumerable.Empty<SitemapUrl>();
}

var overrideBaseUrl = GetBaseUrl(baseUrl);
if (overrideBaseUrl.IsAbsoluteUri)
{
return root
.DescendantSitemapNodes(_settings.ExcludedFromSitemapPropertyAlias, _settings.ExcludedChildrenFromSitemapPropertyAlias)
.Where(node => node.ItemType == PublishedItemType.Content)
.Select(content => CreateSitemapUrlForContent(content, overrideBaseUrl));
}
}
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;
}

/// <summary>
/// The content node from which the sitemap should start
/// </summary>
/// <param name="context">The Umbraco context</param>
/// <returns>A content node, media nodes are not supported</returns>
protected virtual IPublishedContent FromContent(UmbracoContext context)
{
return context.ContentCache.GetByRoute("/");
}

public virtual bool Cacheable { get; } = true;
public override bool Cacheable { get; } = true;

public virtual DateTime CacheUntil { get; } = DateTime.UtcNow.AddHours(1);
public override DateTime CacheUntil { get; } = DateTime.UtcNow.AddHours(1);

protected virtual SitemapUrl CreateSitemapUrlForContent(IPublishedContent content)
private static SitemapUrl CreateSitemapUrlForContent(IPublishedContent content, Uri authorityUri)
{
return SitemapUrl.Create(content.UrlAbsolute(), content.UpdateDate);
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public XDocument BuildSitemapXmlDocument(IEnumerable<SitemapUrl> sitemapUrls)
};

return new XDocument(new XDeclaration("1.0", "utf-8", "yes"), headerComments, new XElement(XName.Get("urlset", SitemapUrl.SitemapNs), elements));
}
}
}
}
2 changes: 1 addition & 1 deletion src/Sitemapify/Providers/ISitemapContentProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Sitemapify.Providers
{
public interface ISitemapContentProvider
{
IEnumerable<SitemapUrl> GetSitemapUrls();
IEnumerable<SitemapUrl> GetSitemapUrls(Uri baseUrl);
bool Cacheable { get; }
DateTime CacheUntil { get; }
}
Expand Down
5 changes: 3 additions & 2 deletions src/Sitemapify/Providers/Impl/EmptySitemapContentProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ namespace Sitemapify.Providers.Impl
{
public class EmptySitemapContentProvider : ISitemapContentProvider
{
public virtual IEnumerable<SitemapUrl> GetSitemapUrls()
public virtual IEnumerable<SitemapUrl> GetSitemapUrls(Uri baseUrl)
{
yield return SitemapUrl.Create("/");
var ub = new UriBuilder(baseUrl) {Path = "/"};
yield return SitemapUrl.Create(ub.ToString());
}

public virtual bool Cacheable { get; } = true;
Expand Down
20 changes: 13 additions & 7 deletions src/Sitemapify/SitemapifyHttpHandler.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Text;
using System.Web;
using System.Web.Routing;
Expand Down Expand Up @@ -30,7 +31,7 @@ public void ProcessRequest(HttpContextBase context)
context.Response.Buffer = true;
context.Response.BufferOutput = true;
context.Response.ContentEncoding = Encoding.UTF8;
var document = GetSitemapContent();
var document = GetSitemapContent(context.Request);
document.Save(context.Response.Output, SaveOptions.OmitDuplicateNamespaces);
if (_contentProvider.Cacheable)
{
Expand All @@ -39,17 +40,22 @@ public void ProcessRequest(HttpContextBase context)
}
}

private XDocument GetSitemapContent()
private XDocument GetSitemapContent(HttpRequestBase contextRequest)
{
if (!_sitemapCacheProvider.IsCached || ResetCache)
{
var document = _documentBuilder.BuildSitemapXmlDocument(_contentProvider.GetSitemapUrls());
if (_contentProvider.Cacheable)
var baseUrl = contextRequest.Url?.GetLeftPart(UriPartial.Authority);
if (!string.IsNullOrWhiteSpace(baseUrl) && Uri.IsWellFormedUriString(baseUrl, UriKind.Absolute))
{
_sitemapCacheProvider.Add(document, _contentProvider.CacheUntil);
ResetCache = false;
var authorityUri = new Uri(baseUrl, UriKind.Absolute);
var document = _documentBuilder.BuildSitemapXmlDocument(_contentProvider.GetSitemapUrls(authorityUri));
if (_contentProvider.Cacheable)
{
_sitemapCacheProvider.Add(document, _contentProvider.CacheUntil);
ResetCache = false;
}
return document;
}
return document;
}
return _sitemapCacheProvider.Get();
}
Expand Down

0 comments on commit 3804e8d

Please sign in to comment.