Skip to content

Commit

Permalink
Fix/compare website channel name on publish event (#71)
Browse files Browse the repository at this point in the history
* fix(Lucene.Core): compare WebSiteChannelName of an indexed web page item model against Lucene index WebSiteChannelName

* feat(git): compare jpg files
  • Loading branch information
bkapustik authored Sep 2, 2024
1 parent 8f1dc3c commit 2e1ae4b
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 102 deletions.
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
* text eol=crlf
*.png binary
*.png binary
*.jpg binary
207 changes: 106 additions & 101 deletions src/Kentico.Xperience.Lucene.Core/Indexing/IndexedItemModelExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,101 +1,106 @@
using CMS.ContentEngine.Internal;
using CMS.Core;

namespace Kentico.Xperience.Lucene.Core.Indexing;

/// <summary>
/// Lucene extension methods for the <see cref="IIndexEventItemModel"/> class.
/// </summary>
internal static class IndexedItemModelExtensions
{
/// <summary>
/// Returns true if the node is included in the Lucene index based on the index's defined paths
/// </summary>
/// <remarks>Logs an error if the search model cannot be found.</remarks>
/// <param name="item">The node to check for indexing.</param>
/// <param name="log"></param>
/// <param name="indexName">The Lucene index code name.</param>
/// <param name="eventName"></param>
/// <param name="indexManager"></param>
/// <exception cref="ArgumentNullException" />
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);
});
}

/// <summary>
/// Returns true if the node is included in the Lucene index's allowed
/// </summary>
/// <remarks>Logs an error if the search model cannot be found.</remarks>
/// <param name="item">The node to check for indexing.</param>
/// <param name="log"></param>
/// <param name="indexName">The Lucene index code name.</param>
/// <param name="eventName"></param>
/// <param name="indexManager"></param>
/// <exception cref="ArgumentNullException" />
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;

/// <summary>
/// Lucene extension methods for the <see cref="IIndexEventItemModel"/> class.
/// </summary>
internal static class IndexedItemModelExtensions
{
/// <summary>
/// Returns true if the node is included in the Lucene index based on the index's defined paths
/// </summary>
/// <remarks>Logs an error if the search model cannot be found.</remarks>
/// <param name="item">The node to check for indexing.</param>
/// <param name="log"></param>
/// <param name="indexName">The Lucene index code name.</param>
/// <param name="eventName"></param>
/// <param name="indexManager"></param>
/// <exception cref="ArgumentNullException" />
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);
});
}

/// <summary>
/// Returns true if the node is included in the Lucene index's allowed
/// </summary>
/// <remarks>Logs an error if the search model cannot be found.</remarks>
/// <param name="item">The node to check for indexing.</param>
/// <param name="log"></param>
/// <param name="indexName">The Lucene index code name.</param>
/// <param name="eventName"></param>
/// <param name="indexManager"></param>
/// <exception cref="ArgumentNullException" />
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;
}
}

0 comments on commit 2e1ae4b

Please sign in to comment.