Files
Umbraco-CMS/src/Umbraco.Web/PublishedCache/NuCache/PublishedContent.cs

450 lines
17 KiB
C#
Raw Normal View History

2016-05-27 14:26:28 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.Models;
2016-05-27 14:26:28 +02:00
using Umbraco.Core.Models.PublishedContent;
2017-05-30 18:13:11 +02:00
using Umbraco.Web.Composing;
2016-05-27 14:26:28 +02:00
using Umbraco.Web.Models;
using Umbraco.Web.PublishedCache.NuCache.DataSource;
namespace Umbraco.Web.PublishedCache.NuCache
{
internal class PublishedContent : PublishedContentBase
2016-05-27 14:26:28 +02:00
{
2017-10-31 12:48:24 +01:00
private readonly IPublishedSnapshotAccessor _publishedSnapshotAccessor;
2016-05-27 14:26:28 +02:00
private readonly ContentNode _contentNode;
// ReSharper disable once InconsistentNaming
internal readonly ContentData _contentData; // internal for ContentNode cloning
2018-04-28 16:34:43 +02:00
private readonly string _urlSegment;
private IReadOnlyDictionary<string, PublishedCultureName> _cultureNames;
2016-05-27 14:26:28 +02:00
#region Constructors
public PublishedContent(ContentNode contentNode, ContentData contentData, IPublishedSnapshotAccessor publishedSnapshotAccessor, IPublishedVariationContextAccessor variationContextAccessor)
2016-05-27 14:26:28 +02:00
{
_contentNode = contentNode;
_contentData = contentData;
2017-10-31 12:48:24 +01:00
_publishedSnapshotAccessor = publishedSnapshotAccessor;
2018-04-28 16:34:43 +02:00
VariationContextAccessor = variationContextAccessor; // fixme why is this a property? should be be on the base class?
2016-05-27 14:26:28 +02:00
2018-04-28 16:34:43 +02:00
_urlSegment = _contentData.Name.ToUrlSegment();
2016-05-30 19:54:36 +02:00
IsPreviewing = _contentData.Published == false;
2017-12-07 13:22:32 +01:00
var properties = new List<IPublishedProperty>();
foreach (var propertyType in _contentNode.ContentType.PropertyTypes)
{
2018-04-25 15:55:27 +02:00
// add one property per property type - this is required, for the indexing to work
// if contentData supplies pdatas, use them, else use null
contentData.Properties.TryGetValue(propertyType.Alias, out var pdatas); // else will be null
properties.Add(new Property(propertyType, this, pdatas, _publishedSnapshotAccessor));
2017-12-07 13:22:32 +01:00
}
PropertiesArray = properties.ToArray();
2016-05-27 14:26:28 +02:00
}
2016-05-30 19:54:36 +02:00
private string GetProfileNameById(int id)
2016-05-27 14:26:28 +02:00
{
2017-10-31 12:48:24 +01:00
var cache = GetCurrentSnapshotCache();
2016-05-27 14:26:28 +02:00
return cache == null
? GetProfileNameByIdNoCache(id)
: (string)cache.GetCacheItem(CacheKeys.ProfileName(id), () => GetProfileNameByIdNoCache(id));
2016-05-27 14:26:28 +02:00
}
private static string GetProfileNameByIdNoCache(int id)
{
#if DEBUG
var userService = Current.Services?.UserService;
2016-05-27 14:26:28 +02:00
if (userService == null) return "[null]"; // for tests
#else
// we don't want each published content to hold a reference to the service
// so where should they get the service from really? from the locator...
var userService = Current.Services.UserService;
2016-05-27 14:26:28 +02:00
#endif
var user = userService.GetProfileById(id);
2016-05-30 19:54:36 +02:00
return user?.Name;
2016-05-27 14:26:28 +02:00
}
// (see ContentNode.CloneParent)
public PublishedContent(ContentNode contentNode, PublishedContent origin)
2016-05-27 14:26:28 +02:00
{
_contentNode = contentNode;
2017-10-31 12:48:24 +01:00
_publishedSnapshotAccessor = origin._publishedSnapshotAccessor;
VariationContextAccessor = origin.VariationContextAccessor;
2016-05-27 14:26:28 +02:00
_contentData = origin._contentData;
2018-04-28 16:34:43 +02:00
_urlSegment = origin._urlSegment;
2016-05-30 19:54:36 +02:00
IsPreviewing = origin.IsPreviewing;
2016-05-27 14:26:28 +02:00
// here is the main benefit: we do not re-create properties so if anything
// is cached locally, we share the cache - which is fine - if anything depends
// on the tree structure, it should not be cached locally to begin with
2016-05-30 19:54:36 +02:00
PropertiesArray = origin.PropertiesArray;
2016-05-27 14:26:28 +02:00
}
// clone for previewing as draft a published content that is published and has no draft
private PublishedContent(PublishedContent origin)
2016-05-27 14:26:28 +02:00
{
2017-10-31 12:48:24 +01:00
_publishedSnapshotAccessor = origin._publishedSnapshotAccessor;
VariationContextAccessor = origin.VariationContextAccessor;
2016-05-27 14:26:28 +02:00
_contentNode = origin._contentNode;
_contentData = origin._contentData;
2018-04-28 16:34:43 +02:00
_urlSegment = origin._urlSegment;
2016-05-30 19:54:36 +02:00
IsPreviewing = true;
2016-05-27 14:26:28 +02:00
// clone properties so _isPreviewing is true
PropertiesArray = origin.PropertiesArray.Select(x => (IPublishedProperty)new Property((Property)x, this)).ToArray();
2016-05-27 14:26:28 +02:00
}
#endregion
#region Get Content/Media for Parent/Children
// this is for tests purposes
2017-10-31 12:48:24 +01:00
// args are: current published snapshot (may be null), previewing, content id - returns: content
2016-05-27 14:26:28 +02:00
2018-04-27 11:38:50 +10:00
internal static Func<IPublishedSnapshot, bool, int, IPublishedContent> GetContentByIdFunc { get; set; }
2017-10-31 12:50:30 +01:00
= (publishedShapshot, previewing, id) => publishedShapshot.Content.GetById(previewing, id);
2016-05-27 14:26:28 +02:00
2018-04-27 11:38:50 +10:00
internal static Func<IPublishedSnapshot, bool, int, IPublishedContent> GetMediaByIdFunc { get; set; }
2017-10-31 12:50:30 +01:00
= (publishedShapshot, previewing, id) => publishedShapshot.Media.GetById(previewing, id);
2016-05-27 14:26:28 +02:00
2016-05-30 19:54:36 +02:00
private IPublishedContent GetContentById(bool previewing, int id)
2016-05-27 14:26:28 +02:00
{
2017-10-31 12:48:24 +01:00
return GetContentByIdFunc(_publishedSnapshotAccessor.PublishedSnapshot, previewing, id);
2016-05-27 14:26:28 +02:00
}
2016-05-30 19:54:36 +02:00
private IEnumerable<IPublishedContent> GetContentByIds(bool previewing, IEnumerable<int> ids)
2016-05-27 14:26:28 +02:00
{
2017-10-31 12:48:24 +01:00
var publishedSnapshot = _publishedSnapshotAccessor.PublishedSnapshot;
2016-05-27 14:26:28 +02:00
// beware! the loop below CANNOT be converted to query such as:
2017-10-31 12:48:24 +01:00
//return ids.Select(x => _getContentByIdFunc(publishedSnapshot, previewing, x)).Where(x => x != null);
// because it would capture the published snapshot and cause all sorts of issues
2016-05-27 14:26:28 +02:00
//
2017-10-31 12:48:24 +01:00
// we WANT to get the actual current published snapshot each time we run
2016-05-27 14:26:28 +02:00
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (var id in ids)
{
2017-10-31 12:48:24 +01:00
var content = GetContentByIdFunc(publishedSnapshot, previewing, id);
2016-05-27 14:26:28 +02:00
if (content != null) yield return content;
}
}
2016-05-30 19:54:36 +02:00
private IPublishedContent GetMediaById(bool previewing, int id)
2016-05-27 14:26:28 +02:00
{
2017-10-31 12:48:24 +01:00
return GetMediaByIdFunc(_publishedSnapshotAccessor.PublishedSnapshot, previewing, id);
2016-05-27 14:26:28 +02:00
}
2016-05-30 19:54:36 +02:00
private IEnumerable<IPublishedContent> GetMediaByIds(bool previewing, IEnumerable<int> ids)
2016-05-27 14:26:28 +02:00
{
2017-10-31 12:48:24 +01:00
var publishedShapshot = _publishedSnapshotAccessor.PublishedSnapshot;
2016-05-27 14:26:28 +02:00
// see note above for content
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (var id in ids)
{
2017-10-31 12:48:24 +01:00
var content = GetMediaByIdFunc(publishedShapshot, previewing, id);
2016-05-27 14:26:28 +02:00
if (content != null) yield return content;
}
}
#endregion
2018-04-28 16:34:43 +02:00
#region Content Type
2016-05-27 14:26:28 +02:00
2018-04-28 16:34:43 +02:00
/// <inheritdoc />
public override PublishedContentType ContentType => _contentNode.ContentType;
#endregion
#region PublishedElement
/// <inheritdoc />
2016-05-30 19:54:36 +02:00
public override Guid Key => _contentNode.Uid;
2016-05-27 14:26:28 +02:00
2018-04-28 16:34:43 +02:00
#endregion
#region PublishedContent
/// <inheritdoc />
public override int Id => _contentNode.Id;
/// <inheritdoc />
public override string Name
{
get
{
2018-04-28 16:34:43 +02:00
if (!ContentType.Variations.Has(ContentVariation.CultureNeutral)) // fixme CultureSegment?
return _contentData.Name;
2018-04-28 16:34:43 +02:00
var culture = VariationContextAccessor.Context.Culture;
if (culture == null)
return _contentData.Name;
return Cultures.TryGetValue(culture, out var cultureInfos) ? cultureInfos.Name : null;
}
}
/// <inheritdoc />
public override string UrlName
{
get
{
if (!ContentType.Variations.Has(ContentVariation.CultureNeutral)) // fixme CultureSegment?
return _urlSegment;
var culture = VariationContextAccessor.Context.Culture;
if (culture == null)
return _urlSegment;
return Cultures.TryGetValue(culture, out var cultureInfos) ? cultureInfos.UrlSegment : null;
}
}
2018-04-28 16:34:43 +02:00
/// <inheritdoc />
public override int SortOrder => _contentNode.SortOrder;
/// <inheritdoc />
2016-05-30 19:54:36 +02:00
public override int Level => _contentNode.Level;
2018-04-28 16:34:43 +02:00
/// <inheritdoc />
2016-05-30 19:54:36 +02:00
public override string Path => _contentNode.Path;
2018-04-28 16:34:43 +02:00
/// <inheritdoc />
2016-05-30 19:54:36 +02:00
public override int TemplateId => _contentData.TemplateId;
2016-05-27 14:26:28 +02:00
2018-04-28 16:34:43 +02:00
/// <inheritdoc />
public override int CreatorId => _contentNode.CreatorId;
/// <inheritdoc />
public override string CreatorName => GetProfileNameById(_contentNode.CreatorId);
2016-05-27 14:26:28 +02:00
2018-04-28 16:34:43 +02:00
/// <inheritdoc />
2016-05-30 19:54:36 +02:00
public override DateTime CreateDate => _contentNode.CreateDate;
2016-05-27 14:26:28 +02:00
2018-04-28 16:34:43 +02:00
/// <inheritdoc />
2016-05-30 19:54:36 +02:00
public override int WriterId => _contentData.WriterId;
2018-04-28 16:34:43 +02:00
/// <inheritdoc />
2016-05-30 19:54:36 +02:00
public override string WriterName => GetProfileNameById(_contentData.WriterId);
2016-05-27 14:26:28 +02:00
2018-04-28 16:34:43 +02:00
/// <inheritdoc />
public override DateTime UpdateDate => _contentData.VersionDate;
private IReadOnlyDictionary<string, PublishedCultureInfos> _cultureInfos;
private static readonly IReadOnlyDictionary<string, PublishedCultureInfos> NoCultureInfos = new Dictionary<string, PublishedCultureInfos>();
/// <inheritdoc />
public override PublishedCultureInfos GetCulture(string culture = ".")
{
// handle context culture
if (culture == ".")
culture = VariationContextAccessor.Context.Culture;
// no invariant culture infos
if (culture == null) return null;
// get
return Cultures.TryGetValue(culture, out var cultureInfos) ? cultureInfos : null;
}
/// <inheritdoc />
public override IReadOnlyDictionary<string, PublishedCultureInfos> Cultures
{
get
{
if (!ContentType.Variations.Has(ContentVariation.CultureNeutral)) // fixme CultureSegment?
return NoCultureInfos;
if (_cultureInfos != null) return _cultureInfos;
return _cultureInfos = _contentData.CultureInfos // fixme can it be null?
.ToDictionary(x => x.Key, x => new PublishedCultureInfos(x.Key, x.Value.Name, false, DateTime.MinValue)); // fixme values!
}
}
/// <inheritdoc />
public override PublishedItemType ItemType => _contentNode.ContentType.ItemType;
/// <inheritdoc />
2016-05-30 19:54:36 +02:00
public override bool IsDraft => _contentData.Published == false;
2016-05-27 14:26:28 +02:00
2018-04-28 16:34:43 +02:00
#endregion
#region Tree
/// <inheritdoc />
2016-05-27 14:26:28 +02:00
public override IPublishedContent Parent
{
get
{
// have to use the "current" cache because a PublishedContent can be shared
// amongst many snapshots and other content depend on the snapshots
switch (_contentNode.ContentType.ItemType)
{
case PublishedItemType.Content:
2016-05-30 19:54:36 +02:00
return GetContentById(IsPreviewing, _contentNode.ParentContentId);
2016-05-27 14:26:28 +02:00
case PublishedItemType.Media:
2016-05-30 19:54:36 +02:00
return GetMediaById(IsPreviewing, _contentNode.ParentContentId);
2016-05-27 14:26:28 +02:00
default:
2018-01-26 17:55:20 +01:00
throw new Exception($"Panic: unsupported item type \"{_contentNode.ContentType.ItemType}\".");
2016-05-27 14:26:28 +02:00
}
}
}
2018-04-28 16:34:43 +02:00
/// <inheritdoc />
2016-05-27 14:26:28 +02:00
public override IEnumerable<IPublishedContent> Children
{
get
{
var cache = GetAppropriateCache();
2017-10-31 12:48:24 +01:00
if (cache == null || PublishedSnapshotService.CachePublishedContentChildren == false)
2016-05-27 14:26:28 +02:00
return GetChildren();
2016-05-27 14:26:28 +02:00
// note: ToArray is important here, we want to cache the result, not the function!
return (IEnumerable<IPublishedContent>)cache.GetCacheItem(ChildrenCacheKey, () => GetChildren().ToArray());
2016-05-27 14:26:28 +02:00
}
}
2018-04-28 16:34:43 +02:00
private string _childrenCacheKey;
private string ChildrenCacheKey => _childrenCacheKey ?? (_childrenCacheKey = CacheKeys.PublishedContentChildren(Key, IsPreviewing));
2016-05-27 14:26:28 +02:00
private IEnumerable<IPublishedContent> GetChildren()
{
IEnumerable<IPublishedContent> c;
switch (_contentNode.ContentType.ItemType)
{
case PublishedItemType.Content:
2016-05-30 19:54:36 +02:00
c = GetContentByIds(IsPreviewing, _contentNode.ChildContentIds);
2016-05-27 14:26:28 +02:00
break;
case PublishedItemType.Media:
2016-05-30 19:54:36 +02:00
c = GetMediaByIds(IsPreviewing, _contentNode.ChildContentIds);
2016-05-27 14:26:28 +02:00
break;
default:
throw new Exception("oops");
}
return c.OrderBy(x => x.SortOrder);
2016-05-27 14:26:28 +02:00
// notes:
// _contentNode.ChildContentIds is an unordered int[]
// need needs to fetch & sort - do it only once, lazyily, though
// Q: perfs-wise, is it better than having the store managed an ordered list
}
2018-04-28 16:34:43 +02:00
#endregion
#region Properties
/// <inheritdoc cref="IPublishedElement.Properties"/>
public override IEnumerable<IPublishedProperty> Properties => PropertiesArray;
2016-05-27 14:26:28 +02:00
2018-04-28 16:34:43 +02:00
/// <inheritdoc cref="IPublishedElement.GetProperty(string)"/>
2016-05-27 14:26:28 +02:00
public override IPublishedProperty GetProperty(string alias)
{
var index = _contentNode.ContentType.GetPropertyIndex(alias);
2018-04-25 15:55:27 +02:00
if (index < 0) return null; // happens when 'alias' does not match a content type property alias
if (index >= PropertiesArray.Length) // should never happen - properties array must be in sync with property type
throw new IndexOutOfRangeException("Index points outside the properties array, which means the properties array is corrupt.");
var property = PropertiesArray[index];
2016-05-27 14:26:28 +02:00
return property;
}
2018-04-28 16:34:43 +02:00
/// <inheritdoc cref="IPublishedContent.GetProperty(string, bool)"/>
2016-05-27 14:26:28 +02:00
public override IPublishedProperty GetProperty(string alias, bool recurse)
{
var property = GetProperty(alias);
if (recurse == false) return property;
var cache = GetAppropriateCache();
2016-05-27 14:26:28 +02:00
if (cache == null)
return base.GetProperty(alias, true);
var key = ((Property)property).RecurseCacheKey;
return (Property)cache.GetCacheItem(key, () => base.GetProperty(alias, true));
2016-05-27 14:26:28 +02:00
}
2016-05-30 19:54:36 +02:00
#endregion
#region Caching
// beware what you use that one for - you don't want to cache its result
private ICacheProvider GetAppropriateCache()
2016-05-30 19:54:36 +02:00
{
2018-04-27 11:38:50 +10:00
var publishedSnapshot = (PublishedSnapshot)_publishedSnapshotAccessor.PublishedSnapshot;
2017-10-31 12:48:24 +01:00
var cache = publishedSnapshot == null
2016-05-30 19:54:36 +02:00
? null
2017-10-31 12:48:24 +01:00
: ((IsPreviewing == false || PublishedSnapshotService.FullCacheWhenPreviewing) && (ItemType != PublishedItemType.Member)
? publishedSnapshot.ElementsCache
: publishedSnapshot.SnapshotCache);
2016-05-30 19:54:36 +02:00
return cache;
}
2017-10-31 12:48:24 +01:00
private ICacheProvider GetCurrentSnapshotCache()
2016-05-27 14:26:28 +02:00
{
2018-04-27 11:38:50 +10:00
var publishedSnapshot = (PublishedSnapshot)_publishedSnapshotAccessor.PublishedSnapshot;
2017-10-31 12:48:24 +01:00
return publishedSnapshot?.SnapshotCache;
2016-05-27 14:26:28 +02:00
}
#endregion
#region Internal
internal IPublishedVariationContextAccessor VariationContextAccessor { get; }
2016-05-27 14:26:28 +02:00
// used by navigable content
2016-05-30 19:54:36 +02:00
internal IPublishedProperty[] PropertiesArray { get; }
2016-05-27 14:26:28 +02:00
// used by navigable content
2016-05-30 19:54:36 +02:00
internal int ParentId => _contentNode.ParentContentId;
2016-05-27 14:26:28 +02:00
// used by navigable content
// includes all children, published or unpublished
// NavigableNavigator takes care of selecting those it wants
2016-05-30 19:54:36 +02:00
internal IList<int> ChildIds => _contentNode.ChildContentIds;
2016-05-27 14:26:28 +02:00
// used by Property
// gets a value indicating whether the content or media exists in
// a previewing context or not, ie whether its Parent, Children, and
// properties should refer to published, or draft content
2016-05-30 19:54:36 +02:00
internal bool IsPreviewing { get; }
2016-05-27 14:26:28 +02:00
private string _asPreviewingCacheKey;
2016-05-30 19:54:36 +02:00
private string AsPreviewingCacheKey => _asPreviewingCacheKey ?? (_asPreviewingCacheKey = CacheKeys.PublishedContentAsPreviewing(Key));
2016-05-27 14:26:28 +02:00
// used by ContentCache
internal IPublishedContent AsPreviewingModel()
{
2016-05-30 19:54:36 +02:00
if (IsPreviewing)
2016-05-27 14:26:28 +02:00
return this;
var cache = GetAppropriateCache();
if (cache == null) return new PublishedContent(this).CreateModel();
return (IPublishedContent)cache.GetCacheItem(AsPreviewingCacheKey, () => new PublishedContent(this).CreateModel());
2016-05-27 14:26:28 +02:00
}
// used by Navigable.Source,...
internal static PublishedContent UnwrapIPublishedContent(IPublishedContent content)
{
PublishedContentWrapped wrapped;
while ((wrapped = content as PublishedContentWrapped) != null)
content = wrapped.Unwrap();
var inner = content as PublishedContent;
if (inner == null)
throw new InvalidOperationException("Innermost content is not PublishedContent.");
return inner;
}
#endregion
}
}