2018-06-29 19:52:40 +02:00
|
|
|
|
using System;
|
2019-01-07 23:49:29 +11:00
|
|
|
|
using System.Collections;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Xml.XPath;
|
|
|
|
|
|
using Examine;
|
2018-12-17 12:17:03 +11:00
|
|
|
|
using Examine.Search;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
using Umbraco.Core;
|
|
|
|
|
|
using Umbraco.Core.Models.PublishedContent;
|
|
|
|
|
|
using Umbraco.Core.Xml;
|
2018-12-17 13:11:51 +11:00
|
|
|
|
using Umbraco.Examine;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
using Umbraco.Web.PublishedCache;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2020-03-03 11:59:17 +01:00
|
|
|
|
/// A class used to query for published content, media items
|
2018-06-29 19:52:40 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class PublishedContentQuery : IPublishedContentQuery
|
|
|
|
|
|
{
|
2020-03-03 11:59:17 +01:00
|
|
|
|
private readonly IExamineManager _examineManager;
|
2019-02-07 09:59:16 +01:00
|
|
|
|
private readonly IPublishedSnapshot _publishedSnapshot;
|
2019-01-07 23:49:29 +11:00
|
|
|
|
private readonly IVariationContextAccessor _variationContextAccessor;
|
2019-07-24 18:46:14 +10:00
|
|
|
|
|
2018-06-29 19:52:40 +02:00
|
|
|
|
/// <summary>
|
2020-03-03 11:59:17 +01:00
|
|
|
|
/// Initializes a new instance of the <see cref="PublishedContentQuery" /> class.
|
2018-06-29 19:52:40 +02:00
|
|
|
|
/// </summary>
|
2020-03-03 11:59:17 +01:00
|
|
|
|
public PublishedContentQuery(IPublishedSnapshot publishedSnapshot,
|
|
|
|
|
|
IVariationContextAccessor variationContextAccessor, IExamineManager examineManager)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2019-02-07 09:59:16 +01:00
|
|
|
|
_publishedSnapshot = publishedSnapshot ?? throw new ArgumentNullException(nameof(publishedSnapshot));
|
2020-03-03 11:59:17 +01:00
|
|
|
|
_variationContextAccessor = variationContextAccessor ??
|
|
|
|
|
|
throw new ArgumentNullException(nameof(variationContextAccessor));
|
2019-07-24 18:46:14 +10:00
|
|
|
|
_examineManager = examineManager ?? throw new ArgumentNullException(nameof(examineManager));
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-03 11:59:17 +01:00
|
|
|
|
#region Convert Helpers
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2020-03-03 11:59:17 +01:00
|
|
|
|
private static bool ConvertIdObjectToInt(object id, out int intId)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2020-03-03 11:59:17 +01:00
|
|
|
|
switch (id)
|
|
|
|
|
|
{
|
|
|
|
|
|
case string s:
|
|
|
|
|
|
return int.TryParse(s, out intId);
|
|
|
|
|
|
|
|
|
|
|
|
case int i:
|
|
|
|
|
|
intId = i;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
intId = default;
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-03 11:59:17 +01:00
|
|
|
|
private static bool ConvertIdObjectToGuid(object id, out Guid guidId)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2020-03-03 11:59:17 +01:00
|
|
|
|
switch (id)
|
|
|
|
|
|
{
|
|
|
|
|
|
case string s:
|
|
|
|
|
|
return Guid.TryParse(s, out guidId);
|
|
|
|
|
|
|
|
|
|
|
|
case Guid g:
|
|
|
|
|
|
guidId = g;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
guidId = default;
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
private static bool ConvertIdObjectToUdi(object id, out Udi guidId)
|
|
|
|
|
|
{
|
|
|
|
|
|
switch (id)
|
|
|
|
|
|
{
|
|
|
|
|
|
case string s:
|
|
|
|
|
|
return UdiParser.TryParse(s, out guidId);
|
|
|
|
|
|
|
|
|
|
|
|
case Udi u:
|
|
|
|
|
|
guidId = u;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
guidId = default;
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-03 11:59:17 +01:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Content
|
|
|
|
|
|
|
|
|
|
|
|
public IPublishedContent Content(int id) => ItemById(id, _publishedSnapshot.Content);
|
|
|
|
|
|
|
|
|
|
|
|
public IPublishedContent Content(Guid id) => ItemById(id, _publishedSnapshot.Content);
|
|
|
|
|
|
|
2018-06-29 19:52:40 +02:00
|
|
|
|
public IPublishedContent Content(Udi id)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!(id is GuidUdi udi)) return null;
|
2019-02-07 09:59:16 +01:00
|
|
|
|
return ItemById(udi.Guid, _publishedSnapshot.Content);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-03 11:59:17 +01:00
|
|
|
|
public IPublishedContent Content(object id)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2020-03-03 11:59:17 +01:00
|
|
|
|
if (ConvertIdObjectToInt(id, out var intId))
|
|
|
|
|
|
return Content(intId);
|
|
|
|
|
|
if (ConvertIdObjectToGuid(id, out var guidId))
|
|
|
|
|
|
return Content(guidId);
|
|
|
|
|
|
if (ConvertIdObjectToUdi(id, out var udiId))
|
|
|
|
|
|
return Content(udiId);
|
|
|
|
|
|
return null;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-03 11:59:17 +01:00
|
|
|
|
public IPublishedContent ContentSingleAtXPath(string xpath, params XPathVariable[] vars) =>
|
|
|
|
|
|
ItemByXPath(xpath, vars, _publishedSnapshot.Content);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2020-03-03 11:59:17 +01:00
|
|
|
|
public IEnumerable<IPublishedContent> Content(IEnumerable<int> ids) =>
|
|
|
|
|
|
ItemsByIds(_publishedSnapshot.Content, ids);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2020-03-03 11:59:17 +01:00
|
|
|
|
public IEnumerable<IPublishedContent> Content(IEnumerable<Guid> ids) =>
|
|
|
|
|
|
ItemsByIds(_publishedSnapshot.Content, ids);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2020-03-03 11:59:17 +01:00
|
|
|
|
public IEnumerable<IPublishedContent> Content(IEnumerable<object> ids)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2020-03-03 11:59:17 +01:00
|
|
|
|
return ids.Select(Content).WhereNotNull();
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
2020-03-03 11:59:17 +01:00
|
|
|
|
public IEnumerable<IPublishedContent> ContentAtXPath(string xpath, params XPathVariable[] vars) =>
|
|
|
|
|
|
ItemsByXPath(xpath, vars, _publishedSnapshot.Content);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2020-03-03 11:59:17 +01:00
|
|
|
|
public IEnumerable<IPublishedContent> ContentAtXPath(XPathExpression xpath, params XPathVariable[] vars) =>
|
|
|
|
|
|
ItemsByXPath(xpath, vars, _publishedSnapshot.Content);
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<IPublishedContent> ContentAtRoot() => ItemsAtRoot(_publishedSnapshot.Content);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Media
|
|
|
|
|
|
|
2020-03-03 11:59:17 +01:00
|
|
|
|
public IPublishedContent Media(int id) => ItemById(id, _publishedSnapshot.Media);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2020-03-03 11:59:17 +01:00
|
|
|
|
public IPublishedContent Media(Guid id) => ItemById(id, _publishedSnapshot.Media);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
|
|
public IPublishedContent Media(Udi id)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!(id is GuidUdi udi)) return null;
|
2019-02-07 09:59:16 +01:00
|
|
|
|
return ItemById(udi.Guid, _publishedSnapshot.Media);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-03 11:59:17 +01:00
|
|
|
|
public IPublishedContent Media(object id)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2020-03-03 11:59:17 +01:00
|
|
|
|
if (ConvertIdObjectToInt(id, out var intId))
|
|
|
|
|
|
return Media(intId);
|
|
|
|
|
|
if (ConvertIdObjectToGuid(id, out var guidId))
|
|
|
|
|
|
return Media(guidId);
|
|
|
|
|
|
if (ConvertIdObjectToUdi(id, out var udiId))
|
|
|
|
|
|
return Media(udiId);
|
|
|
|
|
|
return null;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-03 11:59:17 +01:00
|
|
|
|
public IEnumerable<IPublishedContent> Media(IEnumerable<int> ids) => ItemsByIds(_publishedSnapshot.Media, ids);
|
|
|
|
|
|
public IEnumerable<IPublishedContent> Media(IEnumerable<object> ids)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2020-03-03 11:59:17 +01:00
|
|
|
|
return ids.Select(Media).WhereNotNull();
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-03 11:59:17 +01:00
|
|
|
|
public IEnumerable<IPublishedContent> Media(IEnumerable<Guid> ids) => ItemsByIds(_publishedSnapshot.Media, ids);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2020-03-03 11:59:17 +01:00
|
|
|
|
public IEnumerable<IPublishedContent> MediaAtRoot() => ItemsAtRoot(_publishedSnapshot.Media);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Used by Content/Media
|
|
|
|
|
|
|
|
|
|
|
|
private static IPublishedContent ItemById(int id, IPublishedCache cache)
|
|
|
|
|
|
{
|
|
|
|
|
|
var doc = cache.GetById(id);
|
|
|
|
|
|
return doc;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static IPublishedContent ItemById(Guid id, IPublishedCache cache)
|
|
|
|
|
|
{
|
|
|
|
|
|
var doc = cache.GetById(id);
|
|
|
|
|
|
return doc;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static IPublishedContent ItemByXPath(string xpath, XPathVariable[] vars, IPublishedCache cache)
|
|
|
|
|
|
{
|
|
|
|
|
|
var doc = cache.GetSingleByXPath(xpath, vars);
|
|
|
|
|
|
return doc;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//NOTE: Not used?
|
|
|
|
|
|
//private IPublishedContent ItemByXPath(XPathExpression xpath, XPathVariable[] vars, IPublishedCache cache)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// var doc = cache.GetSingleByXPath(xpath, vars);
|
|
|
|
|
|
// return doc;
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<IPublishedContent> ItemsByIds(IPublishedCache cache, IEnumerable<int> ids)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ids.Select(eachId => ItemById(eachId, cache)).WhereNotNull();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private IEnumerable<IPublishedContent> ItemsByIds(IPublishedCache cache, IEnumerable<Guid> ids)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ids.Select(eachId => ItemById(eachId, cache)).WhereNotNull();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-03 11:59:17 +01:00
|
|
|
|
private static IEnumerable<IPublishedContent> ItemsByXPath(string xpath, XPathVariable[] vars,
|
|
|
|
|
|
IPublishedCache cache)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
var doc = cache.GetByXPath(xpath, vars);
|
|
|
|
|
|
return doc;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-03 11:59:17 +01:00
|
|
|
|
private static IEnumerable<IPublishedContent> ItemsByXPath(XPathExpression xpath, XPathVariable[] vars,
|
|
|
|
|
|
IPublishedCache cache)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
var doc = cache.GetByXPath(xpath, vars);
|
|
|
|
|
|
return doc;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-03 11:59:17 +01:00
|
|
|
|
private static IEnumerable<IPublishedContent> ItemsAtRoot(IPublishedCache cache) => cache.GetAtRoot();
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Search
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-03-03 11:59:17 +01:00
|
|
|
|
public IEnumerable<PublishedSearchResult> Search(string term, string culture = "*",
|
|
|
|
|
|
string indexName = Constants.UmbracoIndexes.ExternalIndexName) =>
|
|
|
|
|
|
Search(term, 0, 0, out _, culture, indexName);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-03-03 11:59:17 +01:00
|
|
|
|
public IEnumerable<PublishedSearchResult> Search(string term, int skip, int take, out long totalRecords,
|
|
|
|
|
|
string culture = "*", string indexName = Constants.UmbracoIndexes.ExternalIndexName)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2020-01-05 22:55:55 +01:00
|
|
|
|
if (skip < 0)
|
|
|
|
|
|
{
|
2020-03-03 11:59:17 +01:00
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(skip), skip,
|
|
|
|
|
|
"The value must be greater than or equal to zero.");
|
2020-01-05 22:55:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (take < 0)
|
|
|
|
|
|
{
|
2020-03-03 11:59:17 +01:00
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(take), take,
|
|
|
|
|
|
"The value must be greater than or equal to zero.");
|
2020-01-05 22:55:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-11-01 10:49:45 +01:00
|
|
|
|
if (string.IsNullOrEmpty(indexName))
|
|
|
|
|
|
{
|
|
|
|
|
|
indexName = Constants.UmbracoIndexes.ExternalIndexName;
|
|
|
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2019-07-24 18:46:14 +10:00
|
|
|
|
if (!_examineManager.TryGetIndex(indexName, out var index) || !(index is IUmbracoIndex umbIndex))
|
2019-11-01 10:49:45 +01:00
|
|
|
|
{
|
2020-03-03 11:59:17 +01:00
|
|
|
|
throw new InvalidOperationException(
|
|
|
|
|
|
$"No index found by name {indexName} or is not of type {typeof(IUmbracoIndex)}");
|
2019-11-01 10:49:45 +01:00
|
|
|
|
}
|
2018-12-17 13:11:51 +11:00
|
|
|
|
|
2019-11-13 14:39:11 +01:00
|
|
|
|
var query = umbIndex.GetSearcher().CreateQuery(IndexTypes.Content);
|
2018-12-20 13:29:57 +01:00
|
|
|
|
|
2019-11-13 14:39:11 +01:00
|
|
|
|
IQueryExecutor queryExecutor;
|
2019-07-12 13:15:41 +10:00
|
|
|
|
if (culture == "*")
|
2018-12-17 13:11:51 +11:00
|
|
|
|
{
|
2019-11-01 10:49:45 +01:00
|
|
|
|
// Search everything
|
2019-11-13 14:39:11 +01:00
|
|
|
|
queryExecutor = query.ManagedQuery(term);
|
2018-12-17 13:11:51 +11:00
|
|
|
|
}
|
2019-11-13 14:39:11 +01:00
|
|
|
|
else if (string.IsNullOrWhiteSpace(culture))
|
2019-07-12 13:15:41 +10:00
|
|
|
|
{
|
2019-11-13 14:39:11 +01:00
|
|
|
|
// Only search invariant
|
2020-03-03 11:59:17 +01:00
|
|
|
|
queryExecutor = query
|
|
|
|
|
|
.Field(UmbracoExamineFieldNames.VariesByCultureFieldName, "n") // Must not vary by culture
|
2019-11-13 14:39:11 +01:00
|
|
|
|
.And().ManagedQuery(term);
|
2019-07-12 13:15:41 +10:00
|
|
|
|
}
|
2018-12-17 13:11:51 +11:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2019-11-13 14:39:11 +01:00
|
|
|
|
// Only search the specified culture
|
2020-03-03 11:59:17 +01:00
|
|
|
|
var fields =
|
|
|
|
|
|
umbIndex.GetCultureAndInvariantFields(culture)
|
|
|
|
|
|
.ToArray(); // Get all index fields suffixed with the culture name supplied
|
2019-11-13 14:39:11 +01:00
|
|
|
|
queryExecutor = query.ManagedQuery(term, fields);
|
2018-12-17 13:11:51 +11:00
|
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2019-11-13 14:39:11 +01:00
|
|
|
|
var results = skip == 0 && take == 0
|
|
|
|
|
|
? queryExecutor.Execute()
|
|
|
|
|
|
: queryExecutor.Execute(skip + take);
|
|
|
|
|
|
|
2018-12-05 16:53:25 +11:00
|
|
|
|
totalRecords = results.TotalItemCount;
|
2019-01-07 23:49:29 +11:00
|
|
|
|
|
2020-03-03 11:59:17 +01:00
|
|
|
|
return new CultureContextualSearchResults(
|
|
|
|
|
|
results.Skip(skip).ToPublishedSearchResults(_publishedSnapshot.Content), _variationContextAccessor,
|
|
|
|
|
|
culture);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-03-03 11:59:17 +01:00
|
|
|
|
public IEnumerable<PublishedSearchResult> Search(IQueryExecutor query) => Search(query, 0, 0, out _);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-03-03 11:59:17 +01:00
|
|
|
|
public IEnumerable<PublishedSearchResult> Search(IQueryExecutor query, int skip, int take,
|
|
|
|
|
|
out long totalRecords)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2020-01-05 22:55:55 +01:00
|
|
|
|
if (skip < 0)
|
|
|
|
|
|
{
|
2020-03-03 11:59:17 +01:00
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(skip), skip,
|
|
|
|
|
|
"The value must be greater than or equal to zero.");
|
2020-01-05 22:55:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (take < 0)
|
|
|
|
|
|
{
|
2020-03-03 11:59:17 +01:00
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(take), take,
|
|
|
|
|
|
"The value must be greater than or equal to zero.");
|
2020-01-05 22:55:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-06-29 19:52:40 +02:00
|
|
|
|
var results = skip == 0 && take == 0
|
2018-12-17 12:17:03 +11:00
|
|
|
|
? query.Execute()
|
2019-11-01 10:49:45 +01:00
|
|
|
|
: query.Execute(skip + take);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
|
|
totalRecords = results.TotalItemCount;
|
2019-11-01 10:49:45 +01:00
|
|
|
|
|
2019-11-14 09:23:18 +01:00
|
|
|
|
return results.Skip(skip).ToPublishedSearchResults(_publishedSnapshot);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-07 23:49:29 +11:00
|
|
|
|
/// <summary>
|
2020-03-03 11:59:17 +01:00
|
|
|
|
/// This is used to contextualize the values in the search results when enumerating over them so that the correct
|
|
|
|
|
|
/// culture values are used
|
2019-01-07 23:49:29 +11:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
private class CultureContextualSearchResults : IEnumerable<PublishedSearchResult>
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly string _culture;
|
2020-03-03 11:59:17 +01:00
|
|
|
|
private readonly IVariationContextAccessor _variationContextAccessor;
|
|
|
|
|
|
private readonly IEnumerable<PublishedSearchResult> _wrapped;
|
2019-01-07 23:49:29 +11:00
|
|
|
|
|
2020-03-03 11:59:17 +01:00
|
|
|
|
public CultureContextualSearchResults(IEnumerable<PublishedSearchResult> wrapped,
|
|
|
|
|
|
IVariationContextAccessor variationContextAccessor, string culture)
|
2019-01-07 23:49:29 +11:00
|
|
|
|
{
|
|
|
|
|
|
_wrapped = wrapped;
|
|
|
|
|
|
_variationContextAccessor = variationContextAccessor;
|
|
|
|
|
|
_culture = culture;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerator<PublishedSearchResult> GetEnumerator()
|
|
|
|
|
|
{
|
|
|
|
|
|
//We need to change the current culture to what is requested and then change it back
|
|
|
|
|
|
var originalContext = _variationContextAccessor.VariationContext;
|
|
|
|
|
|
if (!_culture.IsNullOrWhiteSpace() && !_culture.InvariantEquals(originalContext.Culture))
|
|
|
|
|
|
_variationContextAccessor.VariationContext = new VariationContext(_culture);
|
|
|
|
|
|
|
|
|
|
|
|
//now the IPublishedContent returned will be contextualized to the culture specified and will be reset when the enumerator is disposed
|
2020-03-03 11:59:17 +01:00
|
|
|
|
return new CultureContextualSearchResultsEnumerator(_wrapped.GetEnumerator(), _variationContextAccessor,
|
|
|
|
|
|
originalContext);
|
2019-01-07 23:49:29 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-03 11:59:17 +01:00
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
2019-01-07 23:49:29 +11:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-03-03 11:59:17 +01:00
|
|
|
|
/// Resets the variation context when this is disposed
|
2019-01-07 23:49:29 +11:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
private class CultureContextualSearchResultsEnumerator : IEnumerator<PublishedSearchResult>
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly VariationContext _originalContext;
|
2020-03-03 11:59:17 +01:00
|
|
|
|
private readonly IVariationContextAccessor _variationContextAccessor;
|
|
|
|
|
|
private readonly IEnumerator<PublishedSearchResult> _wrapped;
|
2019-01-07 23:49:29 +11:00
|
|
|
|
|
2020-03-03 11:59:17 +01:00
|
|
|
|
public CultureContextualSearchResultsEnumerator(IEnumerator<PublishedSearchResult> wrapped,
|
|
|
|
|
|
IVariationContextAccessor variationContextAccessor, VariationContext originalContext)
|
2019-01-07 23:49:29 +11:00
|
|
|
|
{
|
|
|
|
|
|
_wrapped = wrapped;
|
|
|
|
|
|
_variationContextAccessor = variationContextAccessor;
|
|
|
|
|
|
_originalContext = originalContext;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
|
|
|
|
|
_wrapped.Dispose();
|
|
|
|
|
|
//reset
|
|
|
|
|
|
_variationContextAccessor.VariationContext = _originalContext;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-03 11:59:17 +01:00
|
|
|
|
public bool MoveNext() => _wrapped.MoveNext();
|
2019-01-07 23:49:29 +11:00
|
|
|
|
|
|
|
|
|
|
public void Reset()
|
|
|
|
|
|
{
|
|
|
|
|
|
_wrapped.Reset();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public PublishedSearchResult Current => _wrapped.Current;
|
|
|
|
|
|
object IEnumerator.Current => Current;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-06-29 19:52:40 +02:00
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|