Skip to content

Commit b4c7ac5

Browse files
committedFeb 14, 2025·
Merge branch 'v11/dev' of https://github.com/umbraco/UmbracoExamine.PDF into v11/dev
2 parents afabb33 + 2a03c14 commit b4c7ac5

File tree

3 files changed

+97
-6
lines changed

3 files changed

+97
-6
lines changed
 

‎src/UmbracoExamine.PDF/ConfigurePdfIndexOptions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void Configure(string name, LuceneDirectoryIndexOptions options)
2626
if (name.Equals(PdfIndexConstants.PdfIndexName))
2727
{
2828
options.Analyzer = new StandardAnalyzer(LuceneVersion.LUCENE_48);
29-
options.Validator = new PdfValueSetValidator(null);
29+
options.Validator = new PdfValueSetValidator(null, true);
3030
options.FieldDefinitions = new FieldDefinitionCollection(
3131
new FieldDefinition(PdfIndexConstants.PdfContentFieldName,FieldDefinitionTypes.FullText));
3232

‎src/UmbracoExamine.PDF/PdfLuceneIndex.cs

+56-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
4+
using Examine;
35
using Examine.Lucene;
46
using Examine.Lucene.Providers;
57
using Microsoft.Extensions.Logging;
@@ -31,6 +33,59 @@ public PdfLuceneIndex(ILoggerFactory loggerFactory, string name, IOptionsMonitor
3133

3234
#endregion
3335

36+
/// <summary>
37+
/// Special check for invalid paths
38+
/// </summary>
39+
/// <param name="values"></param>
40+
/// <param name="onComplete"></param>
41+
protected override void PerformIndexItems(IEnumerable<ValueSet> values, Action<IndexOperationEventArgs> onComplete)
42+
{
43+
// We don't want to re-enumerate this list, but we need to split it into 2x enumerables: invalid and valid items.
44+
// The Invalid items will be deleted, these are items that have invalid paths (i.e. moved to the recycle bin, etc...)
45+
// Then we'll index the Value group all together.
46+
var invalidOrValid = values.GroupBy(v =>
47+
{
48+
if (!v.Values.TryGetValue("path", out IReadOnlyList<object> paths) || paths.Count <= 0 || paths[0] == null)
49+
{
50+
return ValueSetValidationStatus.Failed;
51+
}
52+
53+
ValueSetValidationResult validationResult = ValueSetValidator.Validate(v);
54+
55+
return validationResult.Status;
56+
}).ToArray();
57+
58+
var hasUpdates = false;
3459

60+
// ordering by descending so that Filtered/Failed processes first
61+
foreach (IGrouping<ValueSetValidationStatus, ValueSet> group in invalidOrValid.OrderByDescending(x => x.Key))
62+
{
63+
switch (group.Key)
64+
{
65+
case ValueSetValidationStatus.Valid:
66+
hasUpdates = true;
67+
68+
//these are the valid ones, so just index them all at once
69+
base.PerformIndexItems(group.ToArray(), onComplete);
70+
break;
71+
case ValueSetValidationStatus.Failed:
72+
// don't index anything that is invalid
73+
break;
74+
case ValueSetValidationStatus.Filtered:
75+
// these are the invalid/filtered items so we'll delete them
76+
// since the path is not valid we need to delete this item in
77+
// case it exists in the index already and has now
78+
// been moved to an invalid parent.
79+
base.PerformDeleteFromIndex(group.Select(x => x.Id), null);
80+
break;
81+
}
82+
}
83+
84+
if (!hasUpdates)
85+
{
86+
//we need to manually call the completed method
87+
onComplete(new IndexOperationEventArgs(this, 0));
88+
}
89+
}
3590
}
3691
}

‎src/UmbracoExamine.PDF/PdfValueSetValidator.cs

+40-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using Examine;
5+
using Umbraco.Cms.Core;
46
using Umbraco.Cms.Infrastructure.Examine;
57
using Umbraco.Extensions;
68

@@ -10,15 +12,31 @@ public class PdfValueSetValidator : ValueSetValidator
1012
{
1113
public int? ParentId { get; }
1214

15+
public bool PublishedValuesOnly { get; }
16+
1317
private const string PathKey = "path";
1418

15-
public PdfValueSetValidator(int? parentId,
16-
IEnumerable<string> includeItemTypes = null, IEnumerable<string> excludeItemTypes = null)
19+
[Obsolete("Please use the constructor taking all parameters. This constructor will be removed in a future version.")]
20+
public PdfValueSetValidator(
21+
int? parentId,
22+
IEnumerable<string> includeItemTypes = null,
23+
IEnumerable<string> excludeItemTypes = null)
24+
: this(parentId, false, includeItemTypes, excludeItemTypes)
25+
{
26+
}
27+
28+
public PdfValueSetValidator(
29+
int? parentId,
30+
bool publishedValuesOnly,
31+
IEnumerable<string> includeItemTypes = null,
32+
IEnumerable<string> excludeItemTypes = null)
1733
: base(includeItemTypes, excludeItemTypes, null, null)
1834
{
1935
ParentId = parentId;
36+
PublishedValuesOnly = publishedValuesOnly;
2037
}
2138

39+
// TODO (next major): make this method private
2240
public bool ValidatePath(string path)
2341
{
2442
//check if this document is a descendent of the parent
@@ -33,6 +51,24 @@ public bool ValidatePath(string path)
3351
return true;
3452
}
3553

54+
private bool ValidateRecycleBin(string path, string category)
55+
{
56+
var recycleBinId = category == IndexTypes.Content
57+
? Constants.System.RecycleBinContentString
58+
: Constants.System.RecycleBinMediaString;
59+
60+
//check for recycle bin
61+
if (PublishedValuesOnly)
62+
{
63+
if (path.Contains(string.Concat(",", recycleBinId, ",")))
64+
{
65+
return false;
66+
}
67+
}
68+
69+
return true;
70+
}
71+
3672
public override ValueSetValidationResult Validate(ValueSet valueSet)
3773
{
3874
var baseValidate = base.Validate(valueSet);
@@ -53,7 +89,7 @@ public override ValueSetValidationResult Validate(ValueSet valueSet)
5389

5490
var filteredValues = valueSet.Values.ToDictionary(x => x.Key, x => x.Value.ToList());
5591

56-
bool isFiltered = !ValidatePath(path);
92+
bool isFiltered = !ValidatePath(path) || !ValidateRecycleBin(path!, valueSet.Category);
5793

5894
var filteredValueSet = new ValueSet(valueSet.Id, valueSet.Category, valueSet.ItemType, filteredValues.ToDictionary(x => x.Key, x => (IEnumerable<object>)x.Value));
5995
return new ValueSetValidationResult(isFiltered ? ValueSetValidationStatus.Filtered : ValueSetValidationStatus.Valid, filteredValueSet);

0 commit comments

Comments
 (0)
Please sign in to comment.