Added UDI support for GetById and fixed bug not using specified preview mode

This commit is contained in:
Ronald Barendse
2019-04-05 20:55:38 +02:00
parent 2ae639a3ae
commit d2e04614c6

View File

@@ -1,8 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.XPath;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Xml;
@@ -13,14 +12,10 @@ namespace Umbraco.Web.PublishedCache
/// </summary>
public abstract class ContextualPublishedCache
{
//TODO: We need to add:
//* GetById(Guid contentId)
//* GetById(UDI contentId)
protected readonly UmbracoContext UmbracoContext;
/// <summary>
/// Initializes a new instance of the <see cref="ContextualPublishedCache"/> with a context.
/// Initializes a new instance of the <see cref="ContextualPublishedCache" /> with a context.
/// </summary>
/// <param name="umbracoContext">The context.</param>
protected ContextualPublishedCache(UmbracoContext umbracoContext)
@@ -32,8 +27,12 @@ namespace Umbraco.Web.PublishedCache
/// Gets a content identified by its unique identifier.
/// </summary>
/// <param name="contentId">The content unique identifier.</param>
/// <returns>The content, or null.</returns>
/// <remarks>Considers published or unpublished content depending on context.</remarks>
/// <returns>
/// The content, or null.
/// </returns>
/// <remarks>
/// Considers published or unpublished content depending on context.
/// </remarks>
public IPublishedContent GetById(int contentId)
{
return GetById(UmbracoContext.InPreviewMode, contentId);
@@ -43,35 +42,84 @@ namespace Umbraco.Web.PublishedCache
/// Gets a content identified by its unique identifier.
/// </summary>
/// <param name="contentId">The content unique identifier.</param>
/// <returns>The content, or null.</returns>
/// <remarks>Considers published or unpublished content depending on context.</remarks>
/// <returns>
/// The content, or null.
/// </returns>
/// <remarks>
/// Considers published or unpublished content depending on context.
/// </remarks>
public IPublishedContent GetById(Guid contentId)
{
return GetById(UmbracoContext.InPreviewMode, contentId);
}
/// <summary>
/// Gets a content identified by its unique identifier.
/// </summary>
/// <param name="contentId">The content unique identifier.</param>
/// <returns>
/// The content, or null.
/// </returns>
/// <remarks>
/// Considers published or unpublished content depending on context.
/// </remarks>
public IPublishedContent GetById(Udi contentId)
{
return GetById(UmbracoContext.InPreviewMode, contentId);
}
/// <summary>
/// Gets a content identified by its unique identifier.
/// </summary>
/// <param name="preview">A value indicating whether to consider unpublished content.</param>
/// <param name="contentId">The content unique identifier.</param>
/// <returns>The content, or null.</returns>
/// <returns>
/// The content, or null.
/// </returns>
public abstract IPublishedContent GetById(bool preview, int contentId);
// same with Guid
// cannot make this public nor abstract without breaking backward compatibility
/// <summary>
/// Gets a content identified by its unique identifier.
/// </summary>
/// <param name="preview">A value indicating whether to consider unpublished content.</param>
/// <param name="contentKey">The content key.</param>
/// <returns>
/// The content, or null.
/// </returns>
public virtual IPublishedContent GetById(bool preview, Guid contentKey)
{
// original implementation - override in concrete classes
var intId = UmbracoContext.Application.Services.EntityService.GetIdForKey(contentKey, UmbracoObjectTypes.Document);
return GetById(intId.Success ? intId.Result : -1);
var contentIdAttempt = UmbracoContext.Application.Services.EntityService.GetIdForKey(contentKey, UmbracoObjectTypes.Document);
return GetById(preview, contentIdAttempt.Success ? contentIdAttempt.Result : -1);
}
/// <summary>
/// Gets a content identified by its unique identifier.
/// </summary>
/// <param name="preview">A value indicating whether to consider unpublished content.</param>
/// <param name="contentId">The content identifier.</param>
/// <returns>
/// The content, or null.
/// </returns>
/// <exception cref="InvalidOperationException">UDIs for content items must be <see cref="GuidUdi" /></exception>
public virtual IPublishedContent GetById(bool preview, Udi contentId)
{
var guidUdi = contentId as GuidUdi;
if (guidUdi == null)
throw new InvalidOperationException("UDIs for content items must be " + typeof(GuidUdi));
return GetById(preview, guidUdi.Guid);
}
/// <summary>
/// Gets content at root.
/// </summary>
/// <returns>The contents.</returns>
/// <remarks>Considers published or unpublished content depending on context.</remarks>
/// <returns>
/// The contents.
/// </returns>
/// <remarks>
/// Considers published or unpublished content depending on context.
/// </remarks>
public IEnumerable<IPublishedContent> GetAtRoot()
{
return GetAtRoot(UmbracoContext.InPreviewMode);
@@ -81,7 +129,9 @@ namespace Umbraco.Web.PublishedCache
/// Gets contents at root.
/// </summary>
/// <param name="preview">A value indicating whether to consider unpublished content.</param>
/// <returns>The contents.</returns>
/// <returns>
/// The contents.
/// </returns>
public abstract IEnumerable<IPublishedContent> GetAtRoot(bool preview);
/// <summary>
@@ -89,10 +139,11 @@ namespace Umbraco.Web.PublishedCache
/// </summary>
/// <param name="xpath">The XPath query.</param>
/// <param name="vars">Optional XPath variables.</param>
/// <returns>The content, or null.</returns>
/// <returns>
/// The content, or null.
/// </returns>
/// <remarks>
/// <para>If <param name="vars" /> is <c>null</c>, or is empty, or contains only one single
/// value which itself is <c>null</c>, then variables are ignored.</para>
/// <para>If <param name="vars" /> is <c>null</c>, or is empty, or contains only one single value which itself is <c>null</c>, then variables are ignored.</para>
/// <para>The XPath expression should reference variables as <c>$var</c>.</para>
/// <para>Considers published or unpublished content depending on context.</para>
/// </remarks>
@@ -106,10 +157,11 @@ namespace Umbraco.Web.PublishedCache
/// </summary>
/// <param name="xpath">The XPath query.</param>
/// <param name="vars">Optional XPath variables.</param>
/// <returns>The content, or null.</returns>
/// <returns>
/// The content, or null.
/// </returns>
/// <remarks>
/// <para>If <param name="vars" /> is <c>null</c>, or is empty, or contains only one single
/// value which itself is <c>null</c>, then variables are ignored.</para>
/// <para>If <param name="vars" /> is <c>null</c>, or is empty, or contains only one single value which itself is <c>null</c>, then variables are ignored.</para>
/// <para>The XPath expression should reference variables as <c>$var</c>.</para>
/// <para>Considers published or unpublished content depending on context.</para>
/// </remarks>
@@ -124,10 +176,11 @@ namespace Umbraco.Web.PublishedCache
/// <param name="preview">A value indicating whether to consider unpublished content.</param>
/// <param name="xpath">The XPath query.</param>
/// <param name="vars">Optional XPath variables.</param>
/// <returns>The content, or null.</returns>
/// <returns>
/// The content, or null.
/// </returns>
/// <remarks>
/// <para>If <param name="vars" /> is <c>null</c>, or is empty, or contains only one single
/// value which itself is <c>null</c>, then variables are ignored.</para>
/// <para>If <param name="vars" /> is <c>null</c>, or is empty, or contains only one single value which itself is <c>null</c>, then variables are ignored.</para>
/// <para>The XPath expression should reference variables as <c>$var</c>.</para>
/// </remarks>
public abstract IPublishedContent GetSingleByXPath(bool preview, string xpath, params XPathVariable[] vars);
@@ -138,10 +191,11 @@ namespace Umbraco.Web.PublishedCache
/// <param name="preview">A value indicating whether to consider unpublished content.</param>
/// <param name="xpath">The XPath query.</param>
/// <param name="vars">Optional XPath variables.</param>
/// <returns>The content, or null.</returns>
/// <returns>
/// The content, or null.
/// </returns>
/// <remarks>
/// <para>If <param name="vars" /> is <c>null</c>, or is empty, or contains only one single
/// value which itself is <c>null</c>, then variables are ignored.</para>
/// <para>If <param name="vars" /> is <c>null</c>, or is empty, or contains only one single value which itself is <c>null</c>, then variables are ignored.</para>
/// <para>The XPath expression should reference variables as <c>$var</c>.</para>
/// </remarks>
public abstract IPublishedContent GetSingleByXPath(bool preview, XPathExpression xpath, params XPathVariable[] vars);
@@ -151,7 +205,9 @@ namespace Umbraco.Web.PublishedCache
/// </summary>
/// <param name="xpath">The XPath query.</param>
/// <param name="vars">Optional XPath variables.</param>
/// <returns>The contents.</returns>
/// <returns>
/// The contents.
/// </returns>
/// <remarks>
/// <para>If <param name="vars" /> is <c>null</c>, or is empty, or contains only one single
/// value which itself is <c>null</c>, then variables are ignored.</para>
@@ -186,10 +242,11 @@ namespace Umbraco.Web.PublishedCache
/// <param name="preview">A value indicating whether to consider unpublished content.</param>
/// <param name="xpath">The XPath query.</param>
/// <param name="vars">Optional XPath variables.</param>
/// <returns>The contents.</returns>
/// <returns>
/// The contents.
/// </returns>
/// <remarks>
/// <para>If <param name="vars" /> is <c>null</c>, or is empty, or contains only one single
/// value which itself is <c>null</c>, then variables are ignored.</para>
/// <para>If <param name="vars" /> is <c>null</c>, or is empty, or contains only one single value which itself is <c>null</c>, then variables are ignored.</para>
/// <para>The XPath expression should reference variables as <c>$var</c>.</para>
/// </remarks>
public abstract IEnumerable<IPublishedContent> GetByXPath(bool preview, string xpath, params XPathVariable[] vars);
@@ -200,10 +257,11 @@ namespace Umbraco.Web.PublishedCache
/// <param name="preview">A value indicating whether to consider unpublished content.</param>
/// <param name="xpath">The XPath query.</param>
/// <param name="vars">Optional XPath variables.</param>
/// <returns>The contents.</returns>
/// <returns>
/// The contents.
/// </returns>
/// <remarks>
/// <para>If <param name="vars" /> is <c>null</c>, or is empty, or contains only one single
/// value which itself is <c>null</c>, then variables are ignored.</para>
/// <para>If <param name="vars" /> is <c>null</c>, or is empty, or contains only one single value which itself is <c>null</c>, then variables are ignored.</para>
/// <para>The XPath expression should reference variables as <c>$var</c>.</para>
/// </remarks>
public abstract IEnumerable<IPublishedContent> GetByXPath(bool preview, XPathExpression xpath, params XPathVariable[] vars);
@@ -211,8 +269,12 @@ namespace Umbraco.Web.PublishedCache
/// <summary>
/// Gets an XPath navigator that can be used to navigate content.
/// </summary>
/// <returns>The XPath navigator.</returns>
/// <remarks>Considers published or unpublished content depending on context.</remarks>
/// <returns>
/// The XPath navigator.
/// </returns>
/// <remarks>
/// Considers published or unpublished content depending on context.
/// </remarks>
public XPathNavigator GetXPathNavigator()
{
return GetXPathNavigator(UmbracoContext.InPreviewMode);
@@ -222,20 +284,28 @@ namespace Umbraco.Web.PublishedCache
/// Gets an XPath navigator that can be used to navigate content.
/// </summary>
/// <param name="preview">A value indicating whether to consider unpublished content.</param>
/// <returns>The XPath navigator.</returns>
/// <returns>
/// The XPath navigator.
/// </returns>
public abstract XPathNavigator GetXPathNavigator(bool preview);
/// <summary>
/// Gets a value indicating whether <c>GetXPathNavigator</c> returns an <c>XPathNavigator</c>
/// and that navigator is a <c>NavigableNavigator</c>.
/// Gets a value indicating whether <c>GetXPathNavigator</c> returns an <c>XPathNavigator</c> and that navigator is a <c>NavigableNavigator</c>.
/// </summary>
/// <value>
/// <c>true</c> if the <c>XPathNavigator</c> is navigable; otherwise, <c>false</c>.
/// </value>
public abstract bool XPathNavigatorIsNavigable { get; }
/// <summary>
/// Gets a value indicating whether the underlying non-contextual cache contains content.
/// </summary>
/// <returns>A value indicating whether the underlying non-contextual cache contains content.</returns>
/// <remarks>Considers published or unpublished content depending on context.</remarks>
/// <returns>
/// A value indicating whether the underlying non-contextual cache contains content.
/// </returns>
/// <remarks>
/// Considers published or unpublished content depending on context.
/// </remarks>
public bool HasContent()
{
return HasContent(UmbracoContext.InPreviewMode);
@@ -245,7 +315,9 @@ namespace Umbraco.Web.PublishedCache
/// Gets a value indicating whether the underlying non-contextual cache contains content.
/// </summary>
/// <param name="preview">A value indicating whether to consider unpublished content.</param>
/// <returns>A value indicating whether the underlying non-contextual cache contains content.</returns>
/// <returns>
/// A value indicating whether the underlying non-contextual cache contains content.
/// </returns>
public abstract bool HasContent(bool preview);
}
}