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.Reflection;
|
2018-12-17 13:11:51 +11:00
|
|
|
|
using System.Text.RegularExpressions;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
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.Services;
|
|
|
|
|
|
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
|
|
|
|
|
|
{
|
|
|
|
|
|
using Examine = global::Examine;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// A class used to query for published content, media items
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class PublishedContentQuery : IPublishedContentQuery
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IPublishedContentCache _contentCache;
|
|
|
|
|
|
private readonly IPublishedMediaCache _mediaCache;
|
2019-01-07 23:49:29 +11:00
|
|
|
|
private readonly IVariationContextAccessor _variationContextAccessor;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Constructor used to return results from the caches
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="contentCache"></param>
|
|
|
|
|
|
/// <param name="mediaCache"></param>
|
2019-01-07 23:49:29 +11:00
|
|
|
|
/// <param name="variationContextAccessor"></param>
|
|
|
|
|
|
public PublishedContentQuery(IPublishedContentCache contentCache, IPublishedMediaCache mediaCache, IVariationContextAccessor variationContextAccessor)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
_contentCache = contentCache ?? throw new ArgumentNullException(nameof(contentCache));
|
|
|
|
|
|
_mediaCache = mediaCache ?? throw new ArgumentNullException(nameof(mediaCache));
|
2019-01-07 23:49:29 +11:00
|
|
|
|
_variationContextAccessor = variationContextAccessor ?? throw new ArgumentNullException(nameof(variationContextAccessor));
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#region Content
|
|
|
|
|
|
|
|
|
|
|
|
public IPublishedContent Content(int id)
|
|
|
|
|
|
{
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemById(id, _contentCache);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IPublishedContent Content(Guid id)
|
|
|
|
|
|
{
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemById(id, _contentCache);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IPublishedContent Content(Udi id)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!(id is GuidUdi udi)) return null;
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemById(udi.Guid, _contentCache);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IPublishedContent ContentSingleAtXPath(string xpath, params XPathVariable[] vars)
|
|
|
|
|
|
{
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemByXPath(xpath, vars, _contentCache);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<IPublishedContent> Content(IEnumerable<int> ids)
|
|
|
|
|
|
{
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemsByIds(_contentCache, ids);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<IPublishedContent> Content(IEnumerable<Guid> ids)
|
|
|
|
|
|
{
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemsByIds(_contentCache, ids);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<IPublishedContent> ContentAtXPath(string xpath, params XPathVariable[] vars)
|
|
|
|
|
|
{
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemsByXPath(xpath, vars, _contentCache);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<IPublishedContent> ContentAtXPath(XPathExpression xpath, params XPathVariable[] vars)
|
|
|
|
|
|
{
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemsByXPath(xpath, vars, _contentCache);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<IPublishedContent> ContentAtRoot()
|
|
|
|
|
|
{
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemsAtRoot(_contentCache);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Media
|
|
|
|
|
|
|
|
|
|
|
|
public IPublishedContent Media(int id)
|
|
|
|
|
|
{
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemById(id, _mediaCache);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IPublishedContent Media(Guid id)
|
|
|
|
|
|
{
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemById(id, _mediaCache);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IPublishedContent Media(Udi id)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!(id is GuidUdi udi)) return null;
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemById(udi.Guid, _mediaCache);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<IPublishedContent> Media(IEnumerable<int> ids)
|
|
|
|
|
|
{
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemsByIds(_mediaCache, ids);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<IPublishedContent> Media(IEnumerable<Guid> ids)
|
|
|
|
|
|
{
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemsByIds(_mediaCache, ids);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<IPublishedContent> MediaAtRoot()
|
|
|
|
|
|
{
|
2018-12-06 12:41:38 +11:00
|
|
|
|
return ItemsAtRoot(_mediaCache);
|
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();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<IPublishedContent> ItemsByXPath(string xpath, XPathVariable[] vars, IPublishedCache cache)
|
|
|
|
|
|
{
|
|
|
|
|
|
var doc = cache.GetByXPath(xpath, vars);
|
|
|
|
|
|
return doc;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<IPublishedContent> ItemsByXPath(XPathExpression xpath, XPathVariable[] vars, IPublishedCache cache)
|
|
|
|
|
|
{
|
|
|
|
|
|
var doc = cache.GetByXPath(xpath, vars);
|
|
|
|
|
|
return doc;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<IPublishedContent> ItemsAtRoot(IPublishedCache cache)
|
|
|
|
|
|
{
|
|
|
|
|
|
return cache.GetAtRoot();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Search
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2018-12-17 13:11:51 +11:00
|
|
|
|
public IEnumerable<PublishedSearchResult> Search(string term, string culture = null, string indexName = null)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2018-12-17 13:11:51 +11:00
|
|
|
|
return Search(term, 0, 0, out _, culture, indexName);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2018-12-17 13:11:51 +11:00
|
|
|
|
public IEnumerable<PublishedSearchResult> Search(string term, int skip, int take, out long totalRecords, string culture = null, string indexName = null)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2018-12-03 22:10:56 +11:00
|
|
|
|
indexName = string.IsNullOrEmpty(indexName)
|
2018-12-11 15:42:32 +11:00
|
|
|
|
? Constants.UmbracoIndexes.ExternalIndexName
|
2018-12-03 22:10:56 +11:00
|
|
|
|
: indexName;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2018-12-17 13:11:51 +11:00
|
|
|
|
if (!ExamineManager.Instance.TryGetIndex(indexName, out var index) || !(index is IUmbracoIndex umbIndex))
|
|
|
|
|
|
throw new InvalidOperationException($"No index found by name {indexName} or is not of type {typeof(IUmbracoIndex)}");
|
|
|
|
|
|
|
|
|
|
|
|
var searcher = umbIndex.GetSearcher();
|
|
|
|
|
|
|
2018-12-20 13:29:57 +01:00
|
|
|
|
// default to max 500 results
|
|
|
|
|
|
var count = skip == 0 && take == 0 ? 500 : skip + take;
|
|
|
|
|
|
|
2019-01-07 23:49:29 +11:00
|
|
|
|
//set this to the specific culture or to the culture in the request
|
|
|
|
|
|
culture = culture ?? _variationContextAccessor.VariationContext.Culture;
|
|
|
|
|
|
|
2018-12-17 13:11:51 +11:00
|
|
|
|
ISearchResults results;
|
2018-12-17 13:44:11 +11:00
|
|
|
|
if (culture.IsNullOrWhiteSpace())
|
2018-12-17 13:11:51 +11:00
|
|
|
|
{
|
2018-12-20 13:29:57 +01:00
|
|
|
|
results = searcher.Search(term, count);
|
2018-12-17 13:11:51 +11:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
//get all index fields suffixed with the culture name supplied
|
2019-01-08 13:25:07 +11:00
|
|
|
|
var cultureFields = umbIndex.GetCultureFields(culture);
|
2019-01-07 23:49:29 +11:00
|
|
|
|
var qry = searcher.CreateQuery().Field(UmbracoContentIndex.VariesByCultureFieldName, "y"); //must vary by culture
|
2018-12-20 13:29:57 +01:00
|
|
|
|
qry = qry.And().ManagedQuery(term, cultureFields.ToArray());
|
|
|
|
|
|
results = qry.Execute(count);
|
2018-12-17 13:11:51 +11:00
|
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2018-12-05 16:53:25 +11:00
|
|
|
|
totalRecords = results.TotalItemCount;
|
2019-01-07 23:49:29 +11:00
|
|
|
|
|
|
|
|
|
|
return new CultureContextualSearchResults(results.ToPublishedSearchResults(_contentCache), _variationContextAccessor, culture);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2018-12-17 12:17:03 +11:00
|
|
|
|
public IEnumerable<PublishedSearchResult> Search(IQueryExecutor query)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2018-12-17 12:17:03 +11:00
|
|
|
|
return Search(query, 0, 0, out _);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2018-12-17 12:17:03 +11:00
|
|
|
|
public IEnumerable<PublishedSearchResult> Search(IQueryExecutor query, int skip, int take, out long totalRecords)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
var results = skip == 0 && take == 0
|
2018-12-17 12:17:03 +11:00
|
|
|
|
? query.Execute()
|
|
|
|
|
|
: query.Execute(maxResults: skip + take);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
|
|
totalRecords = results.TotalItemCount;
|
|
|
|
|
|
return results.ToPublishedSearchResults(_contentCache);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-07 23:49:29 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// This is used to contextualize the values in the search results when enumerating over them so that the correct culture values are used
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private class CultureContextualSearchResults : IEnumerable<PublishedSearchResult>
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IEnumerable<PublishedSearchResult> _wrapped;
|
|
|
|
|
|
private readonly IVariationContextAccessor _variationContextAccessor;
|
|
|
|
|
|
private readonly string _culture;
|
|
|
|
|
|
|
|
|
|
|
|
public CultureContextualSearchResults(IEnumerable<PublishedSearchResult> wrapped, IVariationContextAccessor variationContextAccessor, string culture)
|
|
|
|
|
|
{
|
|
|
|
|
|
_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
|
|
|
|
|
|
return new CultureContextualSearchResultsEnumerator(_wrapped.GetEnumerator(), _variationContextAccessor, originalContext);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
|
|
|
|
{
|
|
|
|
|
|
return GetEnumerator();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Resets the variation context when this is disposed
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private class CultureContextualSearchResultsEnumerator : IEnumerator<PublishedSearchResult>
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IEnumerator<PublishedSearchResult> _wrapped;
|
|
|
|
|
|
private readonly IVariationContextAccessor _variationContextAccessor;
|
|
|
|
|
|
private readonly VariationContext _originalContext;
|
|
|
|
|
|
|
|
|
|
|
|
public CultureContextualSearchResultsEnumerator(IEnumerator<PublishedSearchResult> wrapped, IVariationContextAccessor variationContextAccessor, VariationContext originalContext)
|
|
|
|
|
|
{
|
|
|
|
|
|
_wrapped = wrapped;
|
|
|
|
|
|
_variationContextAccessor = variationContextAccessor;
|
|
|
|
|
|
_originalContext = originalContext;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
|
|
|
|
|
_wrapped.Dispose();
|
|
|
|
|
|
//reset
|
|
|
|
|
|
_variationContextAccessor.VariationContext = _originalContext;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool MoveNext()
|
|
|
|
|
|
{
|
|
|
|
|
|
return _wrapped.MoveNext();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Reset()
|
|
|
|
|
|
{
|
|
|
|
|
|
_wrapped.Reset();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public PublishedSearchResult Current => _wrapped.Current;
|
|
|
|
|
|
object IEnumerator.Current => Current;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-08 13:25:07 +11:00
|
|
|
|
|
2018-12-17 13:11:51 +11:00
|
|
|
|
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|