Merge branch 'dawoe-temp_8879' into v8/contrib

This commit is contained in:
Shannon
2020-10-22 17:16:05 +11:00
5 changed files with 96 additions and 16 deletions

View File

@@ -4,6 +4,7 @@ using System.Linq;
using Examine;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Blocks;
using Umbraco.Core.Services;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.DatabaseModelDefinitions;
@@ -15,7 +16,7 @@ namespace Umbraco.Examine
/// <summary>
/// Performs the data lookups required to rebuild a content index
/// </summary>
public class ContentIndexPopulator : IndexPopulator<IUmbracoContentIndex>
public class ContentIndexPopulator : IndexPopulator<IUmbracoContentIndex2>
{
private readonly IContentService _contentService;
private readonly IValueSetBuilder<IContent> _contentValueSetBuilder;
@@ -58,6 +59,12 @@ namespace Umbraco.Examine
_parentId = parentId;
}
public override bool IsRegistered(IUmbracoContentIndex2 index)
{
// check if it should populate based on published values
return _publishedValuesOnly == index.PublishedValuesOnly;
}
protected override void PopulateIndexes(IReadOnlyList<IIndex> indexes)
{
if (indexes.Count == 0) return;
@@ -70,21 +77,24 @@ namespace Umbraco.Examine
{
contentParentId = _parentId.Value;
}
if (_publishedValuesOnly)
{
IndexPublishedContent(contentParentId, pageIndex, pageSize, indexes);
}
else
{
IndexAllContent(contentParentId, pageIndex, pageSize, indexes);
}
}
protected void IndexAllContent(int contentParentId, int pageIndex, int pageSize, IReadOnlyList<IIndex> indexes)
{
IContent[] content;
do
{
if (!_publishedValuesOnly)
{
content = _contentService.GetPagedDescendants(contentParentId, pageIndex, pageSize, out _).ToArray();
}
else
{
//add the published filter
//note: We will filter for published variants in the validator
content = _contentService.GetPagedDescendants(contentParentId, pageIndex, pageSize, out _,
_publishedQuery, Ordering.By("Path", Direction.Ascending)).ToArray();
}
content = _contentService.GetPagedDescendants(contentParentId, pageIndex, pageSize, out _).ToArray();
if (content.Length > 0)
{
@@ -96,5 +106,54 @@ namespace Umbraco.Examine
pageIndex++;
} while (content.Length == pageSize);
}
protected void IndexPublishedContent(int contentParentId, int pageIndex, int pageSize,
IReadOnlyList<IIndex> indexes)
{
IContent[] content;
var publishedPages = new HashSet<int>();
do
{
//add the published filter
//note: We will filter for published variants in the validator
content = _contentService.GetPagedDescendants(contentParentId, pageIndex, pageSize, out _, _publishedQuery,
Ordering.By("Path", Direction.Ascending)).ToArray();
if (content.Length > 0)
{
var indexableContent = new List<IContent>();
foreach (var item in content)
{
if (item.Level == 1)
{
// first level pages are always published so no need to filter them
indexableContent.Add(item);
publishedPages.Add(item.Id);
}
else
{
if (publishedPages.Contains(item.ParentId))
{
// only index when parent is published
publishedPages.Add(item.Id);
indexableContent.Add(item);
}
}
}
// ReSharper disable once PossibleMultipleEnumeration
foreach (var index in indexes)
index.IndexItems(_contentValueSetBuilder.GetValueSets(indexableContent.ToArray()));
}
pageIndex++;
} while (content.Length == pageSize);
}
}
}

View File

@@ -2,6 +2,20 @@ using Examine;
namespace Umbraco.Examine
{
/// <summary>
/// Marker interface for indexes of Umbraco content
/// </summary>
/// <remarks>
/// This is a backwards compat change, in next major version remove the need for this and just have a single interface
/// </remarks>
public interface IUmbracoContentIndex2 : IUmbracoContentIndex
{
bool PublishedValuesOnly { get; }
}
/// <summary>
/// Marker interface for indexes of Umbraco content
/// </summary>
public interface IUmbracoContentIndex : IIndex
{

View File

@@ -13,9 +13,16 @@ namespace Umbraco.Examine
{
public override bool IsRegistered(IIndex index)
{
if (base.IsRegistered(index)) return true;
return index is TIndex;
if (base.IsRegistered(index))
return true;
if (!(index is TIndex casted))
return false;
return IsRegistered(casted);
}
public virtual bool IsRegistered(TIndex index) => true;
}
public abstract class IndexPopulator : IIndexPopulator

View File

@@ -17,7 +17,7 @@ namespace Umbraco.Examine
/// <summary>
/// An indexer for Umbraco content and media
/// </summary>
public class UmbracoContentIndex : UmbracoExamineIndex, IUmbracoContentIndex
public class UmbracoContentIndex : UmbracoExamineIndex, IUmbracoContentIndex2
{
public const string VariesByCultureFieldName = SpecialFieldPrefix + "VariesByCulture";
protected ILocalizationService LanguageService { get; }

View File

@@ -45,7 +45,7 @@ namespace Umbraco.Tests.UmbracoExamine
public static ContentIndexPopulator GetContentIndexRebuilder(PropertyEditorCollection propertyEditors, IContentService contentService, IScopeProvider scopeProvider, bool publishedValuesOnly)
{
var contentValueSetBuilder = GetContentValueSetBuilder(propertyEditors, scopeProvider, publishedValuesOnly);
var contentIndexDataSource = new ContentIndexPopulator(true, null, contentService, scopeProvider.SqlContext, contentValueSetBuilder);
var contentIndexDataSource = new ContentIndexPopulator(publishedValuesOnly, null, contentService, scopeProvider.SqlContext, contentValueSetBuilder);
return contentIndexDataSource;
}