-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimizing Sitemap Creation with Batched Content Items (#16636)
--------- Co-authored-by: Sébastien Ros <sebastienros@gmail.com>
- Loading branch information
1 parent
fe89c06
commit ed8d018
Showing
5 changed files
with
341 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 43 additions & 44 deletions
87
...es/OrchardCore.ContentLocalization/Sitemaps/SitemapUrlHrefLangExtendedMetadataProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,67 @@ | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using System.Xml.Linq; | ||
using OrchardCore.ContentLocalization.Models; | ||
using OrchardCore.ContentManagement; | ||
using OrchardCore.Sitemaps.Aspects; | ||
using OrchardCore.Sitemaps.Builders; | ||
using OrchardCore.Sitemaps.Services; | ||
|
||
namespace OrchardCore.ContentLocalization.Sitemaps | ||
namespace OrchardCore.ContentLocalization.Sitemaps; | ||
|
||
public class SitemapUrlHrefLangExtendedMetadataProvider : ISitemapContentItemExtendedMetadataProvider | ||
{ | ||
public class SitemapUrlHrefLangExtendedMetadataProvider : ISitemapContentItemExtendedMetadataProvider | ||
private static readonly XNamespace _extendedNamespace = "http://www.w3.org/1999/xhtml"; | ||
private static readonly XAttribute _extendedAttribute = new(XNamespace.Xmlns + "xhtml", _extendedNamespace); | ||
|
||
private readonly IContentManager _contentManager; | ||
private readonly IRouteableContentTypeCoordinator _routeableContentTypeCoordinator; | ||
|
||
public SitemapUrlHrefLangExtendedMetadataProvider( | ||
IContentManager contentManager, | ||
IRouteableContentTypeCoordinator routeableContentTypeCoordinator | ||
) | ||
{ | ||
private static readonly XNamespace _extendedNamespace = "http://www.w3.org/1999/xhtml"; | ||
private static readonly XAttribute _extendedAttribute = new(XNamespace.Xmlns + "xhtml", _extendedNamespace); | ||
_contentManager = contentManager; | ||
_routeableContentTypeCoordinator = routeableContentTypeCoordinator; | ||
} | ||
|
||
private readonly IContentManager _contentManager; | ||
private readonly IRouteableContentTypeCoordinator _routeableContentTypeCoordinator; | ||
public XAttribute GetExtendedAttribute => _extendedAttribute; | ||
|
||
public SitemapUrlHrefLangExtendedMetadataProvider( | ||
IContentManager contentManager, | ||
IRouteableContentTypeCoordinator routeableContentTypeCoordinator | ||
) | ||
public async Task<bool> ApplyExtendedMetadataAsync( | ||
SitemapBuilderContext context, | ||
ContentItemsQueryContext queryContext, | ||
ContentItem contentItem, | ||
XElement url) | ||
{ | ||
var part = contentItem.As<LocalizationPart>(); | ||
if (part == null || | ||
queryContext.ReferenceContentItems == null || | ||
!queryContext.ReferenceContentItems.Any()) | ||
{ | ||
_contentManager = contentManager; | ||
_routeableContentTypeCoordinator = routeableContentTypeCoordinator; | ||
return true; | ||
} | ||
|
||
public XAttribute GetExtendedAttribute => _extendedAttribute; | ||
var localizedContentParts = queryContext.ReferenceContentItems | ||
.Select(ci => ci.As<LocalizationPart>()) | ||
.Where(cp => cp.LocalizationSet == part.LocalizationSet); | ||
|
||
public async Task<bool> ApplyExtendedMetadataAsync( | ||
SitemapBuilderContext context, | ||
ContentItemsQueryContext queryContext, | ||
ContentItem contentItem, | ||
XElement url) | ||
foreach (var localizedPart in localizedContentParts) | ||
{ | ||
var part = contentItem.As<LocalizationPart>(); | ||
if (part == null) | ||
var sitemapMetadataAspect = await _contentManager.PopulateAspectAsync<SitemapMetadataAspect>(localizedPart.ContentItem); | ||
if (sitemapMetadataAspect.Exclude) | ||
{ | ||
return true; | ||
continue; | ||
} | ||
|
||
var localizedContentParts = queryContext.ReferenceContentItems | ||
.Select(ci => ci.As<LocalizationPart>()) | ||
.Where(cp => cp.LocalizationSet == part.LocalizationSet); | ||
var hrefValue = await _routeableContentTypeCoordinator.GetRouteAsync(context, localizedPart.ContentItem); | ||
|
||
foreach (var localizedPart in localizedContentParts) | ||
{ | ||
var sitemapMetadataAspect = await _contentManager.PopulateAspectAsync<SitemapMetadataAspect>(localizedPart.ContentItem); | ||
if (sitemapMetadataAspect.Exclude) | ||
{ | ||
continue; | ||
} | ||
var linkNode = new XElement(_extendedNamespace + "link", | ||
new XAttribute("rel", "alternate"), | ||
new XAttribute("hreflang", localizedPart.Culture), | ||
new XAttribute("href", hrefValue)); | ||
|
||
var hrefValue = await _routeableContentTypeCoordinator.GetRouteAsync(context, localizedPart.ContentItem); | ||
|
||
var linkNode = new XElement(_extendedNamespace + "link", | ||
new XAttribute("rel", "alternate"), | ||
new XAttribute("hreflang", localizedPart.Culture), | ||
new XAttribute("href", hrefValue)); | ||
|
||
url.Add(linkNode); | ||
} | ||
|
||
return true; | ||
url.Add(linkNode); | ||
} | ||
|
||
return true; | ||
} | ||
} |
Oops, something went wrong.