From 2e1ae4bc050ab4223e43c2b4160e383bfb6ebb84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Boris=20Kapust=C3=ADk?= <82807109+bkapustik@users.noreply.github.com> Date: Mon, 2 Sep 2024 11:14:20 +0200 Subject: [PATCH] Fix/compare website channel name on publish event (#71) * fix(Lucene.Core): compare WebSiteChannelName of an indexed web page item model against Lucene index WebSiteChannelName * feat(git): compare jpg files --- .gitattributes | 3 +- .../Indexing/IndexedItemModelExtensions.cs | 207 +++++++++--------- 2 files changed, 108 insertions(+), 102 deletions(-) diff --git a/.gitattributes b/.gitattributes index 0899b91..974c308 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,3 @@ * text eol=crlf -*.png binary \ No newline at end of file +*.png binary +*.jpg binary \ No newline at end of file diff --git a/src/Kentico.Xperience.Lucene.Core/Indexing/IndexedItemModelExtensions.cs b/src/Kentico.Xperience.Lucene.Core/Indexing/IndexedItemModelExtensions.cs index 73b787a..3fe017c 100644 --- a/src/Kentico.Xperience.Lucene.Core/Indexing/IndexedItemModelExtensions.cs +++ b/src/Kentico.Xperience.Lucene.Core/Indexing/IndexedItemModelExtensions.cs @@ -1,101 +1,106 @@ -using CMS.ContentEngine.Internal; -using CMS.Core; - -namespace Kentico.Xperience.Lucene.Core.Indexing; - -/// -/// Lucene extension methods for the class. -/// -internal static class IndexedItemModelExtensions -{ - /// - /// Returns true if the node is included in the Lucene index based on the index's defined paths - /// - /// Logs an error if the search model cannot be found. - /// The node to check for indexing. - /// - /// The Lucene index code name. - /// - /// - /// - public static bool IsIndexedByIndex(this IndexEventWebPageItemModel item, IEventLogService log, ILuceneIndexManager indexManager, string indexName, string eventName) - { - if (string.IsNullOrWhiteSpace(indexName)) - { - throw new ArgumentNullException(nameof(indexName)); - } - if (item is null) - { - throw new ArgumentNullException(nameof(item)); - } - - var luceneIndex = indexManager.GetIndex(indexName); - if (luceneIndex is null) - { - log.LogError(nameof(IndexedItemModelExtensions), nameof(IsIndexedByIndex), $"Error loading registered Lucene index '{indexName}' for event [{eventName}]."); - return false; - } - - if (!luceneIndex.LanguageNames.Exists(x => x == item.LanguageName)) - { - return false; - } - - return luceneIndex.IncludedPaths.Any(path => - { - bool matchesContentType = path.ContentTypes.Exists(x => string.Equals(x.ContentTypeName, item.ContentTypeName)); - - if (!matchesContentType) - { - return false; - } - - // Supports wildcard matching - if (path.AliasPath.EndsWith("/%", StringComparison.OrdinalIgnoreCase)) - { - string pathToMatch = path.AliasPath[..^2]; - var pathsOnPath = TreePathUtils.GetTreePathsOnPath(item.WebPageItemTreePath, true, false).ToHashSet(); - - return pathsOnPath.Any(p => p.StartsWith(pathToMatch, StringComparison.OrdinalIgnoreCase)); - } - - return item.WebPageItemTreePath.Equals(path.AliasPath, StringComparison.OrdinalIgnoreCase); - }); - } - - /// - /// Returns true if the node is included in the Lucene index's allowed - /// - /// Logs an error if the search model cannot be found. - /// The node to check for indexing. - /// - /// The Lucene index code name. - /// - /// - /// - public static bool IsIndexedByIndex(this IndexEventReusableItemModel item, IEventLogService log, ILuceneIndexManager indexManager, string indexName, string eventName) - { - if (string.IsNullOrEmpty(indexName)) - { - throw new ArgumentNullException(nameof(indexName)); - } - if (item is null) - { - throw new ArgumentNullException(nameof(item)); - } - - var luceneIndex = indexManager.GetIndex(indexName); - if (luceneIndex is null) - { - log.LogError(nameof(IndexedItemModelExtensions), nameof(IsIndexedByIndex), $"Error loading registered Lucene index '{indexName}' for event [{eventName}]."); - return false; - } - - if (luceneIndex.LanguageNames.Exists(x => x == item.LanguageName)) - { - return true; - } - - return false; - } -} +using CMS.ContentEngine.Internal; +using CMS.Core; + +namespace Kentico.Xperience.Lucene.Core.Indexing; + +/// +/// Lucene extension methods for the class. +/// +internal static class IndexedItemModelExtensions +{ + /// + /// Returns true if the node is included in the Lucene index based on the index's defined paths + /// + /// Logs an error if the search model cannot be found. + /// The node to check for indexing. + /// + /// The Lucene index code name. + /// + /// + /// + public static bool IsIndexedByIndex(this IndexEventWebPageItemModel item, IEventLogService log, ILuceneIndexManager indexManager, string indexName, string eventName) + { + if (string.IsNullOrWhiteSpace(indexName)) + { + throw new ArgumentNullException(nameof(indexName)); + } + if (item is null) + { + throw new ArgumentNullException(nameof(item)); + } + + var luceneIndex = indexManager.GetIndex(indexName); + if (luceneIndex is null) + { + log.LogError(nameof(IndexedItemModelExtensions), nameof(IsIndexedByIndex), $"Error loading registered Lucene index '{indexName}' for event [{eventName}]."); + return false; + } + + if (!string.Equals(item.WebsiteChannelName, luceneIndex.WebSiteChannelName)) + { + return false; + } + + if (!luceneIndex.LanguageNames.Exists(x => x == item.LanguageName)) + { + return false; + } + + return luceneIndex.IncludedPaths.Any(path => + { + bool matchesContentType = path.ContentTypes.Exists(x => string.Equals(x.ContentTypeName, item.ContentTypeName)); + + if (!matchesContentType) + { + return false; + } + + // Supports wildcard matching + if (path.AliasPath.EndsWith("/%", StringComparison.OrdinalIgnoreCase)) + { + string pathToMatch = path.AliasPath[..^2]; + var pathsOnPath = TreePathUtils.GetTreePathsOnPath(item.WebPageItemTreePath, true, false).ToHashSet(); + + return pathsOnPath.Any(p => p.StartsWith(pathToMatch, StringComparison.OrdinalIgnoreCase)); + } + + return item.WebPageItemTreePath.Equals(path.AliasPath, StringComparison.OrdinalIgnoreCase); + }); + } + + /// + /// Returns true if the node is included in the Lucene index's allowed + /// + /// Logs an error if the search model cannot be found. + /// The node to check for indexing. + /// + /// The Lucene index code name. + /// + /// + /// + public static bool IsIndexedByIndex(this IndexEventReusableItemModel item, IEventLogService log, ILuceneIndexManager indexManager, string indexName, string eventName) + { + if (string.IsNullOrEmpty(indexName)) + { + throw new ArgumentNullException(nameof(indexName)); + } + if (item is null) + { + throw new ArgumentNullException(nameof(item)); + } + + var luceneIndex = indexManager.GetIndex(indexName); + if (luceneIndex is null) + { + log.LogError(nameof(IndexedItemModelExtensions), nameof(IsIndexedByIndex), $"Error loading registered Lucene index '{indexName}' for event [{eventName}]."); + return false; + } + + if (luceneIndex.LanguageNames.Exists(x => x == item.LanguageName)) + { + return true; + } + + return false; + } +}