2015-02-18 17:14:55 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2013-11-07 17:16:22 +01:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Xml.XPath;
|
|
|
|
|
|
using Umbraco.Core;
|
2016-06-10 16:37:28 +02:00
|
|
|
|
using Umbraco.Core.Models.PublishedContent;
|
2013-11-07 17:16:22 +01:00
|
|
|
|
using Umbraco.Core.Xml;
|
|
|
|
|
|
using Umbraco.Web.PublishedCache;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web
|
|
|
|
|
|
{
|
2017-07-27 12:01:38 +02:00
|
|
|
|
using Examine = global::Examine;
|
|
|
|
|
|
|
2013-11-07 17:16:22 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// A class used to query for published content, media items
|
|
|
|
|
|
/// </summary>
|
2016-06-30 18:35:43 +02:00
|
|
|
|
public class PublishedContentQuery : IPublishedContentQuery
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2016-06-30 18:35:43 +02:00
|
|
|
|
private readonly IPublishedContentQuery _query;
|
2016-05-26 17:12:04 +02:00
|
|
|
|
private readonly IPublishedContentCache _contentCache;
|
|
|
|
|
|
private readonly IPublishedMediaCache _mediaCache;
|
2013-11-07 17:16:22 +01:00
|
|
|
|
|
2015-02-18 17:14:55 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Constructor used to return results from the caches
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="contentCache"></param>
|
|
|
|
|
|
/// <param name="mediaCache"></param>
|
2016-05-26 17:12:04 +02:00
|
|
|
|
public PublishedContentQuery(IPublishedContentCache contentCache, IPublishedMediaCache mediaCache)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2016-05-26 17:12:04 +02:00
|
|
|
|
if (contentCache == null) throw new ArgumentNullException(nameof(contentCache));
|
|
|
|
|
|
if (mediaCache == null) throw new ArgumentNullException(nameof(mediaCache));
|
2013-11-07 17:16:22 +01:00
|
|
|
|
_contentCache = contentCache;
|
|
|
|
|
|
_mediaCache = mediaCache;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-02-18 17:14:55 +01:00
|
|
|
|
/// <summary>
|
2016-06-30 16:39:05 +02:00
|
|
|
|
/// Constructor used to wrap the ITypedPublishedContentQuery object passed in
|
2015-02-18 17:14:55 +01:00
|
|
|
|
/// </summary>
|
2016-06-30 18:35:43 +02:00
|
|
|
|
/// <param name="query"></param>
|
|
|
|
|
|
public PublishedContentQuery(IPublishedContentQuery query)
|
2015-02-18 17:14:55 +01:00
|
|
|
|
{
|
2016-06-30 18:35:43 +02:00
|
|
|
|
if (query == null) throw new ArgumentNullException(nameof(query));
|
|
|
|
|
|
_query = query;
|
2015-02-18 17:14:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-11-07 17:16:22 +01:00
|
|
|
|
#region Content
|
|
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
public IPublishedContent Content(int id)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2016-06-30 18:35:43 +02:00
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemById(id, _contentCache)
|
|
|
|
|
|
: _query.Content(id);
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-03 10:31:44 +01:00
|
|
|
|
public IPublishedContent Content(Guid id)
|
2016-07-19 13:00:43 +02:00
|
|
|
|
{
|
2016-11-03 10:31:44 +01:00
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemById(id, _contentCache)
|
|
|
|
|
|
: _query.Content(id);
|
2016-07-19 13:00:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-09-08 19:39:13 +02:00
|
|
|
|
public IPublishedContent Content(Udi id)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!(id is GuidUdi udi)) return null;
|
|
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemById(udi.Guid, _contentCache)
|
|
|
|
|
|
: _query.Content(udi.Guid);
|
|
|
|
|
|
}
|
2017-09-23 10:08:18 +02:00
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
public IPublishedContent ContentSingleAtXPath(string xpath, params XPathVariable[] vars)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2016-06-30 18:35:43 +02:00
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemByXPath(xpath, vars, _contentCache)
|
|
|
|
|
|
: _query.ContentSingleAtXPath(xpath, vars);
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
public IEnumerable<IPublishedContent> Content(IEnumerable<int> ids)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2016-06-30 18:35:43 +02:00
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemsByIds(_contentCache, ids)
|
|
|
|
|
|
: _query.Content(ids);
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-03 10:31:44 +01:00
|
|
|
|
public IEnumerable<IPublishedContent> Content(IEnumerable<Guid> ids)
|
2016-07-19 13:00:43 +02:00
|
|
|
|
{
|
2016-11-03 10:31:44 +01:00
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemsByIds(_contentCache, ids)
|
|
|
|
|
|
: _query.Content(ids);
|
2016-07-19 13:00:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
public IEnumerable<IPublishedContent> ContentAtXPath(string xpath, params XPathVariable[] vars)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2016-06-30 18:35:43 +02:00
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemsByXPath(xpath, vars, _contentCache)
|
|
|
|
|
|
: _query.ContentAtXPath(xpath, vars);
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
public IEnumerable<IPublishedContent> ContentAtXPath(XPathExpression xpath, params XPathVariable[] vars)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2016-06-30 18:35:43 +02:00
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemsByXPath(xpath, vars, _contentCache)
|
|
|
|
|
|
: _query.ContentAtXPath(xpath, vars);
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
public IEnumerable<IPublishedContent> ContentAtRoot()
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2016-06-30 18:35:43 +02:00
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemsAtRoot(_contentCache)
|
|
|
|
|
|
: _query.ContentAtRoot();
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Media
|
2017-07-20 11:21:28 +02:00
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
public IPublishedContent Media(int id)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2016-06-30 18:35:43 +02:00
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemById(id, _mediaCache)
|
|
|
|
|
|
: _query.Media(id);
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
2016-07-19 13:00:43 +02:00
|
|
|
|
|
2016-11-05 12:22:57 +01:00
|
|
|
|
public IPublishedContent Media(Guid id)
|
|
|
|
|
|
{
|
|
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemById(id, _mediaCache)
|
|
|
|
|
|
: _query.Media(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-09-08 19:39:13 +02:00
|
|
|
|
public IPublishedContent Media(Udi id)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!(id is GuidUdi udi)) return null;
|
|
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemById(udi.Guid, _mediaCache)
|
|
|
|
|
|
: _query.Media(udi.Guid);
|
|
|
|
|
|
}
|
2017-09-23 10:08:18 +02:00
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
public IEnumerable<IPublishedContent> Media(IEnumerable<int> ids)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2016-06-30 18:35:43 +02:00
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemsByIds(_mediaCache, ids)
|
|
|
|
|
|
: _query.Media(ids);
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-05 12:22:57 +01:00
|
|
|
|
public IEnumerable<IPublishedContent> Media(IEnumerable<Guid> ids)
|
|
|
|
|
|
{
|
|
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemsByIds(_mediaCache, ids)
|
|
|
|
|
|
: _query.Media(ids);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
public IEnumerable<IPublishedContent> MediaAtRoot()
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2016-06-30 18:35:43 +02:00
|
|
|
|
return _query == null
|
|
|
|
|
|
? ItemsAtRoot(_mediaCache)
|
|
|
|
|
|
: _query.MediaAtRoot();
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Used by Content/Media
|
|
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
private static IPublishedContent ItemById(int id, IPublishedCache cache)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
|
|
|
|
|
var doc = cache.GetById(id);
|
|
|
|
|
|
return doc;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-03 10:31:44 +01:00
|
|
|
|
private static IPublishedContent ItemById(Guid id, IPublishedCache cache)
|
2016-07-19 13:00:43 +02:00
|
|
|
|
{
|
2016-11-03 10:31:44 +01:00
|
|
|
|
var doc = cache.GetById(id);
|
2016-07-19 13:00:43 +02:00
|
|
|
|
return doc;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
private static IPublishedContent ItemByXPath(string xpath, XPathVariable[] vars, IPublishedCache cache)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
|
|
|
|
|
var doc = cache.GetSingleByXPath(xpath, vars);
|
|
|
|
|
|
return doc;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//NOTE: Not used?
|
2016-06-30 18:35:43 +02:00
|
|
|
|
//private IPublishedContent ItemByXPath(XPathExpression xpath, XPathVariable[] vars, IPublishedCache cache)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
//{
|
|
|
|
|
|
// var doc = cache.GetSingleByXPath(xpath, vars);
|
|
|
|
|
|
// return doc;
|
2017-07-20 11:21:28 +02:00
|
|
|
|
//}
|
2013-11-07 17:16:22 +01:00
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
private static IEnumerable<IPublishedContent> ItemsByIds(IPublishedCache cache, IEnumerable<int> ids)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2016-06-30 18:35:43 +02:00
|
|
|
|
return ids.Select(eachId => ItemById(eachId, cache)).WhereNotNull();
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-03 10:31:44 +01:00
|
|
|
|
private IEnumerable<IPublishedContent> ItemsByIds(IPublishedCache cache, IEnumerable<Guid> ids)
|
2016-07-19 13:00:43 +02:00
|
|
|
|
{
|
2016-11-03 10:31:44 +01:00
|
|
|
|
return ids.Select(eachId => ItemById(eachId, cache)).WhereNotNull();
|
2016-07-19 13:00:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
private static IEnumerable<IPublishedContent> ItemsByXPath(string xpath, XPathVariable[] vars, IPublishedCache cache)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
|
|
|
|
|
var doc = cache.GetByXPath(xpath, vars);
|
|
|
|
|
|
return doc;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
private static IEnumerable<IPublishedContent> ItemsByXPath(XPathExpression xpath, XPathVariable[] vars, IPublishedCache cache)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
|
|
|
|
|
var doc = cache.GetByXPath(xpath, vars);
|
|
|
|
|
|
return doc;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-30 18:35:43 +02:00
|
|
|
|
private static IEnumerable<IPublishedContent> ItemsAtRoot(IPublishedCache cache)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
|
|
|
|
|
return cache.GetAtRoot();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Search
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Searches content
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="term"></param>
|
|
|
|
|
|
/// <param name="useWildCards"></param>
|
2018-03-27 18:14:21 +11:00
|
|
|
|
/// <param name="indexName">The index to search</param>
|
2013-11-07 17:16:22 +01:00
|
|
|
|
/// <returns></returns>
|
2018-03-27 18:14:21 +11:00
|
|
|
|
public IEnumerable<PublishedSearchResult> Search(string term, bool useWildCards = true, string indexName = null)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2018-03-27 18:14:21 +11:00
|
|
|
|
if (_query != null) return _query.Search(term, useWildCards, indexName);
|
2015-02-18 17:14:55 +01:00
|
|
|
|
|
2018-03-27 18:14:21 +11:00
|
|
|
|
var searcher = string.IsNullOrEmpty(indexName)
|
|
|
|
|
|
? Examine.ExamineManager.Instance.GetIndexSearcher(Constants.Examine.ExternalIndexer)
|
|
|
|
|
|
: Examine.ExamineManager.Instance.GetIndexSearcher(indexName);
|
2013-11-07 17:16:22 +01:00
|
|
|
|
|
2018-03-27 18:14:21 +11:00
|
|
|
|
if (searcher == null)
|
|
|
|
|
|
throw new InvalidOperationException("No searcher found for index " + indexName);
|
|
|
|
|
|
|
2013-11-07 17:16:22 +01:00
|
|
|
|
var results = searcher.Search(term, useWildCards);
|
2017-07-21 17:04:14 +02:00
|
|
|
|
return results.ToPublishedSearchResults(_contentCache);
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Searhes content
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="criteria"></param>
|
|
|
|
|
|
/// <param name="searchProvider"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2018-03-27 18:14:21 +11:00
|
|
|
|
public IEnumerable<PublishedSearchResult> Search(Examine.SearchCriteria.ISearchCriteria criteria, Examine.ISearcher searchProvider = null)
|
2013-11-07 17:16:22 +01:00
|
|
|
|
{
|
2016-06-30 18:35:43 +02:00
|
|
|
|
if (_query != null) return _query.Search(criteria, searchProvider);
|
2015-02-18 17:14:55 +01:00
|
|
|
|
|
2018-03-27 18:14:21 +11:00
|
|
|
|
var s = searchProvider ?? Examine.ExamineManager.Instance.GetIndexSearcher(Constants.Examine.ExternalIndexer);
|
2013-11-07 17:16:22 +01:00
|
|
|
|
|
|
|
|
|
|
var results = s.Search(criteria);
|
2017-07-21 17:04:14 +02:00
|
|
|
|
return results.ToPublishedSearchResults(_contentCache);
|
2013-11-07 17:16:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|