Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding UriAugmenterService in order to augment Url generation. #56

Merged
merged 17 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Geta.Optimizely.Sitemaps.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Sandbox", "Sandbox", "{9003
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Geta.Optimizely.Sitemaps", "src\Geta.Optimizely.Sitemaps\Geta.Optimizely.Sitemaps.csproj", "{A56D25DD-73FB-4754-B054-C5CD9B52804F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Geta.Optimizely.Sitemaps.Commerce", "src\Geta.Optimizely.Sitemaps.Commerce\Geta.Optimizely.Sitemaps.Commerce.csproj", "{39B5430D-35AF-4413-980B-1CE51B367DC7}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Geta.Optimizely.Sitemaps.Commerce", "src\Geta.Optimizely.Sitemaps.Commerce\Geta.Optimizely.Sitemaps.Commerce.csproj", "{39B5430D-35AF-4413-980B-1CE51B367DC7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Foundation", "sandbox\Foundation\src\Foundation\Foundation.csproj", "{82A14BA5-4A85-4DC3-833E-37EBC47BB891}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Foundation", "sandbox\Foundation\src\Foundation\Foundation.csproj", "{82A14BA5-4A85-4DC3-833E-37EBC47BB891}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This tool allows you to generate xml sitemaps for search engines to better index
- ability to include pages that are in a different branch than the one of the start page
- ability to generate sitemaps for mobile pages
- it also supports multi-site and multi-language environments
- ability to augment URL generation

See the [editor guide](docs/editor-guide.md) for more information.

Expand Down Expand Up @@ -64,6 +65,19 @@ And for the Commerce support add a call to:
services.AddSitemapsCommerce();
```

In order to augment Urls for a given set of content one must prepare to build a service that identifies content to be augmented
and yields augmented Uris from IUriAugmenterService.GetAugmentUris(IContent content, CurrentLanguageContent languageContentInfo, Uri fullUri) method.

1. [Create a service that implements IUriAugmenterService yielding multiple Uris per single input content/language/Uri.](sandbox/Foundation/src/Foundation/Infrastructure/Cms/Services/SitemapUriParameterAugmenterService.cs).
2. Ensure the services is set, overring the default service, within the optionsAction of AddSitemaps. For example:

```csharp
services.AddSitemaps(options =>
{
options.SetAugmenterService<SitemapUriParameterAugmenterService>();
});
```

It is also possible to configure the application in `appsettings.json` file. A configuration from the `appsettings.json` will override configuration configured in Startup. Below is an `appsettings.json` configuration example.

```json
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using EPiServer;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using Foundation.Features.People.PersonItemPage;
using Geta.Optimizely.Sitemaps;
using Geta.Optimizely.Sitemaps.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Foundation.Infrastructure.Cms.Services
{
public class SitemapUriParameterAugmenterService : IUriAugmenterService
{
private readonly IContentTypeRepository _contentTypeRepository;
private readonly IContentModelUsage _contentModelUsage;
private readonly IContentRepository _contentRepository;

public SitemapUriParameterAugmenterService(IContentTypeRepository contentTypeRepository, IContentModelUsage contentModelUsage, IContentRepository contentRepository)
{
_contentTypeRepository = contentTypeRepository;
_contentModelUsage = contentModelUsage;
_contentRepository = contentRepository;
}

public IEnumerable<Uri> GetAugmentUris(IContent content, CurrentLanguageContent languageContentInfo, Uri fullUri)
{
if (content is PageData pageContent)
{
if (pageContent.PageTypeName == nameof(Features.People.PersonListPage))
{
var fullUriString = fullUri.ToString();

var personPageType = _contentTypeRepository.Load<PersonPage>();
var usages = _contentModelUsage.ListContentOfContentType(personPageType).Select(c => _contentRepository.Get<PersonPage>(c.ContentLink));
// Group all of the results by the querystring parameters that drive the page.
var nameSectorLocations = usages.GroupBy(k => new { k.Name, k.Sector, k.Location });

// Enumerate the total set of expected name/sectors/locations in ordr for them to be indexed.
foreach (var nameSectorLocation in nameSectorLocations)
{
var augmentedUri = new Uri($"{fullUriString}?name={nameSectorLocation.Key.Name}&sector={nameSectorLocation.Key.Sector}&location={nameSectorLocation.Key.Location}");
yield return augmentedUri;
}
}
else
{
yield return fullUri;
}
}
}
}
}
13 changes: 11 additions & 2 deletions sandbox/Foundation/src/Foundation/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using EPiServer.Authorization;
using EPiServer;
using EPiServer.Authorization;
using EPiServer.ContentApi.Cms;
using EPiServer.ContentApi.Cms.Internal;
using EPiServer.ContentDefinitionsApi;
using EPiServer.ContentManagementApi;
using EPiServer.Core;
using EPiServer.Data;
using EPiServer.DataAbstraction;
using EPiServer.Framework.Web.Resources;
using EPiServer.Labs.ContentManager;
using EPiServer.OpenIDConnect;
Expand All @@ -15,13 +18,15 @@
using Foundation.Infrastructure;
using Foundation.Infrastructure.Cms.Extensions;
using Foundation.Infrastructure.Cms.ModelBinders;
using Foundation.Infrastructure.Cms.Services;
using Foundation.Infrastructure.Cms.Users;
using Foundation.Infrastructure.Display;
using Geta.NotFoundHandler.Infrastructure.Configuration;
using Geta.NotFoundHandler.Infrastructure.Initialization;
using Geta.NotFoundHandler.Optimizely;
using Geta.Optimizely.Sitemaps;
using Geta.Optimizely.Sitemaps.Commerce;
using Geta.Optimizely.Sitemaps.Services;
using Jhoose.Security.DependencyInjection;
using Mediachase.Commerce.Anonymous;
using Mediachase.Commerce.Orders;
Expand Down Expand Up @@ -91,7 +96,11 @@ public void ConfigureServices(IServiceCollection services)
services.AddDetection();
services.AddTinyMceConfiguration();

services.AddSitemaps();
// Implement the UriAugmenterServiceImplementationFactory in order to enumerate the PersonalListPage querystring parameters.
services.AddSitemaps(options =>
{
options.SetAugmenterService<SitemapUriParameterAugmenterService>();
});
services.AddSitemapsCommerce();

//site specific
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Geta Digital. All rights reserved.
// 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.Collections.Generic;
Expand All @@ -10,6 +10,7 @@
using EPiServer.Web;
using EPiServer.Web.Routing;
using Geta.Optimizely.Sitemaps.Repositories;
using Geta.Optimizely.Sitemaps.Services;
using Geta.Optimizely.Sitemaps.Utils;
using Geta.Optimizely.Sitemaps.XML;
using Mediachase.Commerce.Catalog;
Expand All @@ -33,6 +34,7 @@ public CommerceAndStandardSitemapXmlGenerator(
ILanguageBranchRepository languageBranchRepository,
ReferenceConverter referenceConverter,
IContentFilter contentFilter,
IUriAugmenterService uriAugmenterService,
ISynchronizedObjectInstanceCache objectCache,
IMemoryCache memoryCache,
ILogger<CommerceAndStandardSitemapXmlGenerator> logger)
Expand All @@ -44,6 +46,7 @@ public CommerceAndStandardSitemapXmlGenerator(
languageBranchRepository,
referenceConverter,
contentFilter,
uriAugmenterService,
objectCache,
memoryCache,
logger)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Geta Digital. All rights reserved.
// 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;
Expand All @@ -12,6 +12,7 @@
using EPiServer.Web;
using EPiServer.Web.Routing;
using Geta.Optimizely.Sitemaps.Repositories;
using Geta.Optimizely.Sitemaps.Services;
using Geta.Optimizely.Sitemaps.Utils;
using Geta.Optimizely.Sitemaps.XML;
using Mediachase.Commerce.Catalog;
Expand All @@ -36,6 +37,7 @@ public CommerceSitemapXmlGenerator(
ILanguageBranchRepository languageBranchRepository,
ReferenceConverter referenceConverter,
IContentFilter contentFilter,
IUriAugmenterService uriAugmenterService,
ISynchronizedObjectInstanceCache objectCache,
IMemoryCache memoryCache,
ILogger<CommerceSitemapXmlGenerator> logger)
Expand All @@ -46,6 +48,7 @@ public CommerceSitemapXmlGenerator(
siteDefinitionRepository,
languageBranchRepository,
contentFilter,
uriAugmenterService,
objectCache,
memoryCache,
logger)
Expand All @@ -65,7 +68,7 @@ protected override IEnumerable<XElement> GetSitemapXmlElements()
};
}

var descendants = ContentRepository.GetDescendents(rootContentReference).ToList();
var descendants = ContentRepository.GetDescendents(rootContentReference);

return GenerateXmlElements(descendants);
}
Expand Down
14 changes: 12 additions & 2 deletions src/Geta.Optimizely.Sitemaps/Configuration/SitemapOptions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
namespace Geta.Optimizely.Sitemaps.Configuration
using System;
using Geta.Optimizely.Sitemaps.Services;

namespace Geta.Optimizely.Sitemaps.Configuration
{
public class SitemapOptions
{
public bool EnableRealtimeSitemap { get; set; } = false;
public bool EnableRealtimeCaching { get; set; } = true;
public bool EnableLanguageDropDownInAdmin { get; set; } = false;

public Type UriAugmenterService { get; set; } = typeof(DefaultUriAugmenterService);

public void SetAugmenterService<T>() where T : class, IUriAugmenterService
{
UriAugmenterService = typeof(T);
}
}
}
}
9 changes: 7 additions & 2 deletions src/Geta.Optimizely.Sitemaps/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Linq;
using EPiServer.Authorization;
using EPiServer.Shell.Modules;
Expand All @@ -7,6 +7,7 @@
using Geta.Optimizely.Sitemaps.Entities;
using Geta.Optimizely.Sitemaps.Models;
using Geta.Optimizely.Sitemaps.Repositories;
using Geta.Optimizely.Sitemaps.Services;
using Geta.Optimizely.Sitemaps.Utils;
using Geta.Optimizely.Sitemaps.XML;
using Microsoft.AspNetCore.Authorization;
Expand All @@ -23,7 +24,7 @@ public static IServiceCollection AddSitemaps(this IServiceCollection services)
{
return AddSitemaps(services, _ => { }, DefaultPolicy);
}

public static IServiceCollection AddSitemaps(
this IServiceCollection services,
Action<SitemapOptions> setupAction)
Expand Down Expand Up @@ -53,6 +54,10 @@ public static IServiceCollection AddSitemaps(
configuration.GetSection("Geta:Sitemaps").Bind(options);
});

var options = new SitemapOptions();
setupAction(options);
services.AddSingleton(typeof(IUriAugmenterService), options.UriAugmenterService);

services.AddAuthorization(options =>
{
options.AddPolicy(Constants.PolicyName, configurePolicy);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using EPiServer.Core;
using EPiServer.ServiceLocation;

namespace Geta.Optimizely.Sitemaps.Services
{
public class DefaultUriAugmenterService : IUriAugmenterService
{
public IEnumerable<Uri> GetAugmentUris(IContent content, CurrentLanguageContent languageContentInfo, Uri fullUri)
{
yield return fullUri;
}
}
}
18 changes: 18 additions & 0 deletions src/Geta.Optimizely.Sitemaps/Services/IUriAugmenterService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using EPiServer.Core;

namespace Geta.Optimizely.Sitemaps.Services
{
public interface IUriAugmenterService
{
/// <summary>
/// Allows sitemap implementer an easy facility to take a simple url and expand it in a number of ways, includig parameterizing it with QueryStrings.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Allows sitemap implementer an easy facility to take a simple url and expand it in a number of ways, includig parameterizing it with QueryStrings.
/// Allows sitemap implementers an easy facility to take a simple URL and expand it in a number of ways, including parameterizing it with QueryStrings.

/// </summary>
/// <param name="content">Original content of page URL being created</param>
/// <param name="languageContentInfo">Language for URI</param>
/// <param name="originUri">Origin URI to be included in sitemap</param>
/// <returns>Must include origin to be included in sitemap</returns>
IEnumerable<Uri> GetAugmentUris(IContent content, CurrentLanguageContent languageContentInfo, Uri originUri);
}
}
3 changes: 3 additions & 0 deletions src/Geta.Optimizely.Sitemaps/XML/MobileSitemapXmlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using EPiServer.Web;
using EPiServer.Web.Routing;
using Geta.Optimizely.Sitemaps.Repositories;
using Geta.Optimizely.Sitemaps.Services;
using Geta.Optimizely.Sitemaps.Utils;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
Expand All @@ -24,6 +25,7 @@ public MobileSitemapXmlGenerator(
ISiteDefinitionRepository siteDefinitionRepository,
ILanguageBranchRepository languageBranchRepository,
IContentFilter contentFilter,
IUriAugmenterService uriAugmenterService,
ISynchronizedObjectInstanceCache objectCache,
IMemoryCache cache,
ILogger<MobileSitemapXmlGenerator> logger)
Expand All @@ -34,6 +36,7 @@ public MobileSitemapXmlGenerator(
siteDefinitionRepository,
languageBranchRepository,
contentFilter,
uriAugmenterService,
objectCache,
cache,
logger)
Expand Down
Loading