Created
April 11, 2023 04:58
-
-
Save vtml/e77c72cd709b3356301eeebd725f6c88 to your computer and use it in GitHub Desktop.
Fix Sitecore Multi Site Context URL before Coveo Indexing
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
using System; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
using Coveo.Framework.Processor; | |
using Coveo.SearchProvider.Pipelines; | |
using Sitecore.Data; | |
using Sitecore.Sites; | |
using System.Collections.Generic; | |
using Sitecore.Data.Items; | |
using Sitecore.Data.Managers; | |
using Sitecore.DependencyInjection; | |
using Sitecore.Layouts; | |
using Sitecore.Links; | |
using Sitecore.Links.UrlBuilders; | |
using Sitecore.Resources.Media; | |
using Sitecore.Web; | |
using Sitecore.XA.Foundation.Multisite; | |
namespace CPA.Foundation.Coveo.Processors.coveoPostItemProcessingPipeline | |
{ | |
/// <summary> | |
/// Ensures Coveo indexes the correct URL Host name for Master and Web Indexes | |
/// </summary> | |
public class FixPageLinkUrisPostItemProcessing : IProcessor<CoveoPostItemProcessingPipelineArgs> | |
{ | |
private const string HostnameRegexPattern = "(http|https)://[^/]+"; | |
// ReSharper disable once InconsistentNaming | |
/// <summary> | |
/// The Full CM URL eg. https://authoring.tempuri.org | |
/// </summary> | |
public string CMUrl { get; set; } | |
// ReSharper disable once InconsistentNaming | |
/// <summary> | |
/// The Full CD URL eg. https://contentdelivery.tempuri.org | |
/// </summary> | |
public string CDUrl { get; set; } | |
public void Process(CoveoPostItemProcessingPipelineArgs p_Args) | |
{ | |
if (p_Args == null || p_Args.CoveoItem == null || p_Args.CoveoItem.Metadata == null) | |
{ | |
return; | |
} | |
// GEt CoveoItem-> GUId's site-> get target hostname | |
var sitecoreItemID = p_Args.CoveoItem.Id; | |
string databaseName = (string)p_Args.CoveoItem.Metadata["_database"]; | |
Regex hostnameRegex = new Regex(HostnameRegexPattern, RegexOptions.IgnoreCase | RegexOptions.Compiled); | |
/// if media -> cm or cd? - intheblack -> hostname--> sxa way for finding sitecontext for media library | |
if (!string.IsNullOrEmpty(databaseName) && databaseName.Equals("web", StringComparison.OrdinalIgnoreCase)) | |
{ | |
CDUrl = GetURL(sitecoreItemID, databaseName); | |
if (!string.IsNullOrEmpty(CDUrl)) | |
{ | |
p_Args.CoveoItem.ClickableUri = hostnameRegex.Replace(p_Args.CoveoItem.ClickableUri, CDUrl); | |
p_Args.CoveoItem.PrintablePath = hostnameRegex.Replace(p_Args.CoveoItem.PrintablePath, CDUrl); | |
p_Args.CoveoItem.Uri = hostnameRegex.Replace(p_Args.CoveoItem.Uri, CDUrl); | |
} | |
} | |
else if (!string.IsNullOrEmpty(databaseName) && databaseName.Equals("master", StringComparison.OrdinalIgnoreCase)) | |
{ | |
CMUrl = GetURL(sitecoreItemID, databaseName); | |
if (!string.IsNullOrEmpty(CMUrl)) | |
{ | |
p_Args.CoveoItem.ClickableUri = hostnameRegex.Replace(p_Args.CoveoItem.ClickableUri, CMUrl); | |
p_Args.CoveoItem.PrintablePath = hostnameRegex.Replace(p_Args.CoveoItem.PrintablePath, CMUrl); | |
p_Args.CoveoItem.Uri = hostnameRegex.Replace(p_Args.CoveoItem.Uri, CMUrl); | |
} | |
} | |
} | |
private string GetURL(string sitecoreItemID, string databaseName) | |
{ | |
Database sitecoreDB = Sitecore.Configuration.Factory.GetDatabase(databaseName); | |
var item = sitecoreDB?.GetItem(new ID(sitecoreItemID)); | |
var siteinfo = GetSiteInfo(item); | |
if (item != null && item.Paths.IsMediaItem) | |
{ | |
var itemPath = item.Paths.FullPath; | |
var siteForItem = SiteContextFactory.Sites | |
.OrderByDescending(x => x.RootPath.Length) | |
.FirstOrDefault(x => itemPath.StartsWith(x.Properties["MediaPath"]?.Trim(), StringComparison.OrdinalIgnoreCase)); | |
siteinfo = siteForItem; | |
} | |
if (databaseName.Equals("master", StringComparison.OrdinalIgnoreCase)) | |
return siteinfo?.Properties["coveoIngestCMURL"]?.Trim(); | |
else if (databaseName.Equals("web", StringComparison.OrdinalIgnoreCase)) | |
return siteinfo?.Properties["coveoIngestCDURL"]?.Trim(); | |
return null; | |
} | |
private SiteInfo GetSiteInfo(Item item) | |
{ | |
if (item.Paths.IsMediaItem) | |
{ | |
return SiteContextFactory.GetSiteInfo(Sitecore.Constants.ShellSiteName); | |
} | |
var itemPath = item.Paths.FullPath; | |
var siteForItem = SiteContextFactory.Sites | |
.OrderByDescending(x => x.RootPath.Length) | |
.FirstOrDefault(x => itemPath.StartsWith(x.RootPath, StringComparison.OrdinalIgnoreCase)); | |
return siteForItem; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment