Merge pull request #1388 from umbraco/temp-u4-8720

temp-u4-8720
This commit is contained in:
Warren Buckley
2016-07-21 15:48:47 +01:00
committed by GitHub
6 changed files with 365 additions and 110 deletions
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Xml.XPath;
using Umbraco.Core.Xml;
@@ -10,15 +11,22 @@ namespace Umbraco.Web
public interface IDynamicPublishedContentQuery
{
dynamic Content(int id);
dynamic Content(Guid id);
dynamic ContentSingleAtXPath(string xpath, params XPathVariable[] vars);
dynamic ContentSingleAtXPath(XPathExpression xpath, params XPathVariable[] vars);
dynamic Content(IEnumerable<int> ids);
dynamic Content(IEnumerable<Guid> ids);
dynamic ContentAtXPath(string xpath, params XPathVariable[] vars);
dynamic ContentAtXPath(XPathExpression xpath, params XPathVariable[] vars);
dynamic ContentAtRoot();
// note: we CANNOT implement Media by Guid in v7 without break-changing IPublishedCache,
// since we don't support XPath navigation of the media tree.
dynamic Media(int id);
//dynamic Media(Guid id);
dynamic Media(IEnumerable<int> ids);
//dynamic Media(IEnumerable<Guid> ids);
dynamic MediaAtRoot();
/// <summary>
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Xml.XPath;
using Umbraco.Core.Models;
@@ -11,14 +12,21 @@ namespace Umbraco.Web
public interface ITypedPublishedContentQuery
{
IPublishedContent TypedContent(int id);
IPublishedContent TypedContent(Guid id);
IPublishedContent TypedContentSingleAtXPath(string xpath, params XPathVariable[] vars);
IEnumerable<IPublishedContent> TypedContent(IEnumerable<int> ids);
IEnumerable<IPublishedContent> TypedContent(IEnumerable<Guid> ids);
IEnumerable<IPublishedContent> TypedContentAtXPath(string xpath, params XPathVariable[] vars);
IEnumerable<IPublishedContent> TypedContentAtXPath(XPathExpression xpath, params XPathVariable[] vars);
IEnumerable<IPublishedContent> TypedContentAtRoot();
// note: we CANNOT implement TypedMedia by Guid in v7 without break-changing IPublishedCache,
// since we don't support XPath navigation of the media tree.
IPublishedContent TypedMedia(int id);
//IPublishedContent TypedMedia(Guid id);
IEnumerable<IPublishedContent> TypedMedia(IEnumerable<int> ids);
//IEnumerable<IPublishedContent> TypedMedia(IEnumerable<Guid> ids);
IEnumerable<IPublishedContent> TypedMediaAtRoot();
/// <summary>
@@ -1101,11 +1101,9 @@ namespace Umbraco.Web.Models
}
/// <summary>
/// A shortcut method for AncestorOrSelf(1)
/// Gets the 'site' content for this content.
/// </summary>
/// <returns>
/// The site homepage
/// </returns>
/// <returns>The 'site' content ie AncestorOrSelf(1).</returns>
public DynamicPublishedContent Site()
{
return AncestorOrSelf(1);
@@ -1746,6 +1746,17 @@ namespace Umbraco.Web
return content.Children().Where(predicate);
}
/// <summary>
/// Gets the children of the content, of any of the specified types.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="alias">One or more content type alias.</param>
/// <returns>The children of the content, of any of the specified types.</returns>
public static IEnumerable<IPublishedContent> Children(this IPublishedContent content, params string[] alias)
{
return content.Children(x => alias.InvariantContains(x.DocumentTypeAlias));
}
/// <summary>
/// Gets the children of the content, of a given content type.
/// </summary>
@@ -1853,6 +1864,21 @@ namespace Umbraco.Web
#endregion
#region Axes: custom
// todo: in v8, rename this 'Root'
/// <summary>
/// Gets the 'site' content for this content.
/// </summary>
/// <param name="content">The content.</param>
/// <returns>The 'site' content ie AncestorOrSelf(1).</returns>
public static IPublishedContent Site(this IPublishedContent content)
{
return content.AncestorOrSelf(1);
}
#endregion
#region OfTypes
// the .OfType<T>() filter is nice when there's only one type
+64 -2
View File
@@ -2,7 +2,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Xml.XPath;
using umbraco;
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.Dynamics;
using Umbraco.Core.Models;
using Umbraco.Core.Xml;
@@ -56,6 +58,13 @@ namespace Umbraco.Web
: _typedContentQuery.TypedContent(id);
}
public IPublishedContent TypedContent(Guid id)
{
return _typedContentQuery == null
? TypedDocumentById(id, _contentCache)
: _typedContentQuery.TypedContent(id);
}
public IPublishedContent TypedContentSingleAtXPath(string xpath, params XPathVariable[] vars)
{
return _typedContentQuery == null
@@ -70,6 +79,13 @@ namespace Umbraco.Web
: _typedContentQuery.TypedContent(ids);
}
public IEnumerable<IPublishedContent> TypedContent(IEnumerable<Guid> ids)
{
return _typedContentQuery == null
? TypedDocumentsByIds(_contentCache, ids)
: _typedContentQuery.TypedContent(ids);
}
public IEnumerable<IPublishedContent> TypedContentAtXPath(string xpath, params XPathVariable[] vars)
{
return _typedContentQuery == null
@@ -97,7 +113,14 @@ namespace Umbraco.Web
? DocumentById(id, _contentCache, DynamicNull.Null)
: _dynamicContentQuery.Content(id);
}
public dynamic Content(Guid id)
{
return _dynamicContentQuery == null
? DocumentById(id, _contentCache, DynamicNull.Null)
: _dynamicContentQuery.Content(id);
}
public dynamic ContentSingleAtXPath(string xpath, params XPathVariable[] vars)
{
return _dynamicContentQuery == null
@@ -119,6 +142,13 @@ namespace Umbraco.Web
: _dynamicContentQuery.Content(ids);
}
public dynamic Content(IEnumerable<Guid> ids)
{
return _dynamicContentQuery == null
? DocumentByIds(_contentCache, ids.ToArray())
: _dynamicContentQuery.Content(ids);
}
public dynamic ContentAtXPath(string xpath, params XPathVariable[] vars)
{
return _dynamicContentQuery == null
@@ -150,7 +180,7 @@ namespace Umbraco.Web
? TypedDocumentById(id, _mediaCache)
: _typedContentQuery.TypedMedia(id);
}
public IEnumerable<IPublishedContent> TypedMedia(IEnumerable<int> ids)
{
return _typedContentQuery == null
@@ -196,6 +226,15 @@ namespace Umbraco.Web
return doc;
}
private IPublishedContent TypedDocumentById(Guid id, ContextualPublishedCache cache)
{
// todo: in v8, implement in a more efficient way
var legacyXml = UmbracoConfig.For.UmbracoSettings().Content.UseLegacyXmlSchema;
var xpath = legacyXml ? "//node [@key=$guid]" : "//* [@isDoc and @key=$guid]";
var doc = cache.GetSingleByXPath(xpath, new XPathVariable("guid", id.ToString()));
return doc;
}
private IPublishedContent TypedDocumentByXPath(string xpath, XPathVariable[] vars, ContextualPublishedContentCache cache)
{
var doc = cache.GetSingleByXPath(xpath, vars);
@@ -214,6 +253,12 @@ namespace Umbraco.Web
return ids.Select(eachId => TypedDocumentById(eachId, cache)).WhereNotNull();
}
private IEnumerable<IPublishedContent> TypedDocumentsByIds(ContextualPublishedCache cache, IEnumerable<Guid> ids)
{
// todo: in v8, implement in a more efficient way
return ids.Select(eachId => TypedDocumentById(eachId, cache)).WhereNotNull();
}
private IEnumerable<IPublishedContent> TypedDocumentsByXPath(string xpath, XPathVariable[] vars, ContextualPublishedContentCache cache)
{
var doc = cache.GetByXPath(xpath, vars);
@@ -239,6 +284,14 @@ namespace Umbraco.Web
: new DynamicPublishedContent(doc).AsDynamic();
}
private dynamic DocumentById(Guid id, ContextualPublishedCache cache, object ifNotFound)
{
var doc = TypedDocumentById(id, cache);
return doc == null
? ifNotFound
: new DynamicPublishedContent(doc).AsDynamic();
}
private dynamic DocumentByXPath(string xpath, XPathVariable[] vars, ContextualPublishedCache cache, object ifNotFound)
{
var doc = cache.GetSingleByXPath(xpath, vars);
@@ -264,6 +317,15 @@ namespace Umbraco.Web
return new DynamicPublishedContentList(nodes);
}
private dynamic DocumentByIds(ContextualPublishedCache cache, IEnumerable<Guid> ids)
{
var dNull = DynamicNull.Null;
var nodes = ids.Select(eachId => DocumentById(eachId, cache, dNull))
.Where(x => TypeHelper.IsTypeAssignableFrom<DynamicNull>(x) == false)
.Cast<DynamicPublishedContent>();
return new DynamicPublishedContentList(nodes);
}
private dynamic DocumentsByXPath(string xpath, XPathVariable[] vars, ContextualPublishedCache cache)
{
return new DynamicPublishedContentList(
+256 -103
View File
@@ -15,6 +15,7 @@ using Umbraco.Web.Routing;
using Umbraco.Web.Security;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using Umbraco.Core.Cache;
@@ -29,10 +30,10 @@ namespace Umbraco.Web
private readonly UmbracoContext _umbracoContext;
private readonly IPublishedContent _currentPage;
private readonly ITypedPublishedContentQuery _typedQuery;
private readonly IDynamicPublishedContentQuery _dynamicQuery;
private readonly IDynamicPublishedContentQuery _dynamicQuery;
private readonly HtmlStringUtilities _stringUtilities = new HtmlStringUtilities();
private IUmbracoComponentRenderer _componentRenderer;
private IUmbracoComponentRenderer _componentRenderer;
private PublishedContentQuery _query;
private MembershipHelper _membershipHelper;
private TagQuery _tag;
@@ -223,7 +224,7 @@ namespace Umbraco.Web
{
if (query == null) throw new ArgumentNullException("query");
_query = query;
}
}
#endregion
/// <summary>
@@ -232,7 +233,7 @@ namespace Umbraco.Web
/// <remarks>
/// Note that this is the assigned IPublishedContent item to the UmbracoHelper, this is not necessarily the Current IPublishedContent item
/// being rendered. This IPublishedContent object is contextual to the current UmbracoHelper instance.
///
///
/// In some cases accessing this property will throw an exception if there is not IPublishedContent assigned to the Helper
/// this will only ever happen if the Helper is constructed with an UmbracoContext and it is not a front-end request
/// </remarks>
@@ -243,7 +244,7 @@ namespace Umbraco.Web
{
if (_currentPage == null)
throw new InvalidOperationException("Cannot return the " + typeof(IPublishedContent).Name + " because the " + typeof(UmbracoHelper).Name + " was constructed with an " + typeof(UmbracoContext).Name + " and the current request is not a front-end request.");
return _currentPage;
}
}
@@ -315,15 +316,15 @@ namespace Umbraco.Web
/// <param name="formatAsDateWithTimeSeparator"></param>
//// <param name="formatString"></param>
/// <returns></returns>
public IHtmlString Field(string fieldAlias,
public IHtmlString Field(string fieldAlias,
string altFieldAlias = "", string altText = "", string insertBefore = "", string insertAfter = "",
bool recursive = false, bool convertLineBreaks = false, bool removeParagraphTags = false,
RenderFieldCaseType casing = RenderFieldCaseType.Unchanged,
RenderFieldEncodingType encoding = RenderFieldEncodingType.Unchanged,
bool formatAsDate = false,
RenderFieldEncodingType encoding = RenderFieldEncodingType.Unchanged,
bool formatAsDate = false,
bool formatAsDateWithTime = false,
string formatAsDateWithTimeSeparator = "")
{
{
return UmbracoComponentRenderer.Field(AssignedContentItem, fieldAlias, altFieldAlias,
altText, insertBefore, insertAfter, recursive, convertLineBreaks, removeParagraphTags,
casing, encoding, formatAsDate, formatAsDateWithTime, formatAsDateWithTimeSeparator);
@@ -348,11 +349,11 @@ namespace Umbraco.Web
/// <param name="formatAsDateWithTimeSeparator"></param>
//// <param name="formatString"></param>
/// <returns></returns>
public IHtmlString Field(IPublishedContent currentPage, string fieldAlias,
public IHtmlString Field(IPublishedContent currentPage, string fieldAlias,
string altFieldAlias = "", string altText = "", string insertBefore = "", string insertAfter = "",
bool recursive = false, bool convertLineBreaks = false, bool removeParagraphTags = false,
RenderFieldCaseType casing = RenderFieldCaseType.Unchanged,
RenderFieldEncodingType encoding = RenderFieldEncodingType.Unchanged,
RenderFieldEncodingType encoding = RenderFieldEncodingType.Unchanged,
bool formatAsDate = false,
bool formatAsDateWithTime = false,
string formatAsDateWithTimeSeparator = "")
@@ -424,7 +425,7 @@ namespace Umbraco.Web
/// Check if the current user has access to a document
/// </summary>
/// <param name="path">The full path of the document object to check</param>
/// <returns>True if the current user has access or if the current document isn't protected</returns>
/// <returns>True if the current user has access or if the current document isn't protected</returns>
public bool MemberHasAccess(string path)
{
if (IsProtected(path))
@@ -455,7 +456,7 @@ namespace Umbraco.Web
public bool MemberIsLoggedOn()
{
return MembershipHelper.IsLoggedIn();
}
}
#endregion
@@ -495,7 +496,7 @@ namespace Umbraco.Web
}
/// <summary>
/// This method will always add the domain to the path if the hostnames are set up correctly.
/// This method will always add the domain to the path if the hostnames are set up correctly.
/// </summary>
/// <param name="nodeId">Identifier for the node that should be returned</param>
/// <returns>String with a friendly url with full domain from a node</returns>
@@ -560,53 +561,114 @@ namespace Umbraco.Web
#region Content
/// <summary>
/// Gets a content item from the cache.
/// </summary>
/// <param name="id">The unique identifier, or the key, of the content item.</param>
/// <returns>The content, or null of the content item is not in the cache.</returns>
public IPublishedContent TypedContent(object id)
{
int intId;
return ConvertIdObjectToInt(id, out intId) ? ContentQuery.TypedContent(intId) : null;
{
return TypedContentForObject(id);
}
public IPublishedContent TypedContent(int id)
private IPublishedContent TypedContentForObject(object id)
{
int intId;
if (ConvertIdObjectToInt(id, out intId))
return ContentQuery.TypedContent(intId);
Guid guidId;
if (ConvertIdObjectToGuid(id, out guidId))
return ContentQuery.TypedContent(guidId);
return null;
}
/// <summary>
/// Gets a content item from the cache.
/// </summary>
/// <param name="id">The unique identifier of the content item.</param>
/// <returns>The content, or null of the content item is not in the cache.</returns>
public IPublishedContent TypedContent(int id)
{
return ContentQuery.TypedContent(id);
}
/// <summary>
/// Gets a content item from the cache.
/// </summary>
/// <param name="id">The key of the content item.</param>
/// <returns>The content, or null of the content item is not in the cache.</returns>
public IPublishedContent TypedContent(Guid id)
{
return ContentQuery.TypedContent(id);
}
/// <summary>
/// Gets a content item from the cache.
/// </summary>
/// <param name="id">The unique identifier, or the key, of the content item.</param>
/// <returns>The content, or null of the content item is not in the cache.</returns>
public IPublishedContent TypedContent(string id)
{
int intId;
return ConvertIdObjectToInt(id, out intId) ? ContentQuery.TypedContent(intId) : null;
}
return TypedContentForObject(id);
}
public IPublishedContent TypedContentSingleAtXPath(string xpath, params XPathVariable[] vars)
{
return ContentQuery.TypedContentSingleAtXPath(xpath, vars);
}
/// <summary>
/// Gets content items from the cache.
/// </summary>
/// <param name="ids">The unique identifiers, or the keys, of the content items.</param>
/// <returns>The content items that were found in the cache.</returns>
/// <remarks>Does not support mixing identifiers and keys.</remarks>
public IEnumerable<IPublishedContent> TypedContent(params object[] ids)
{
return ContentQuery.TypedContent(ConvertIdsObjectToInts(ids));
return TypedContentForObjects(ids);
}
private IEnumerable<IPublishedContent> TypedContentForObjects(IEnumerable<object> ids)
{
var idsA = ids.ToArray();
IEnumerable<int> intIds;
if (ConvertIdsObjectToInts(idsA, out intIds))
return ContentQuery.TypedContent(intIds);
IEnumerable<Guid> guidIds;
if (ConvertIdsObjectToGuids(idsA, out guidIds))
return ContentQuery.TypedContent(guidIds);
return Enumerable.Empty<IPublishedContent>();
}
/// <summary>
/// Gets the contents corresponding to the identifiers.
/// Gets content items from the cache.
/// </summary>
/// <param name="ids">The content identifiers.</param>
/// <returns>The existing contents corresponding to the identifiers.</returns>
/// <remarks>If an identifier does not match an existing content, it will be missing in the returned value.</remarks>
/// <param name="ids">The unique identifiers of the content items.</param>
/// <returns>The content items that were found in the cache.</returns>
public IEnumerable<IPublishedContent> TypedContent(params int[] ids)
{
return ContentQuery.TypedContent(ids);
}
/// <summary>
/// Gets the contents corresponding to the identifiers.
/// Gets content items from the cache.
/// </summary>
/// <param name="ids">The content identifiers.</param>
/// <returns>The existing contents corresponding to the identifiers.</returns>
/// <remarks>If an identifier does not match an existing content, it will be missing in the returned value.</remarks>
/// <param name="ids">The keys of the content items.</param>
/// <returns>The content items that were found in the cache.</returns>
public IEnumerable<IPublishedContent> TypedContent(params Guid[] ids)
{
return ContentQuery.TypedContent(ids);
}
/// <summary>
/// Gets content items from the cache.
/// </summary>
/// <param name="ids">The unique identifiers, or the keys, of the content items.</param>
/// <returns>The content items that were found in the cache.</returns>
/// <remarks>Does not support mixing identifiers and keys.</remarks>
public IEnumerable<IPublishedContent> TypedContent(params string[] ids)
{
return ContentQuery.TypedContent(ConvertIdsObjectToInts(ids));
{
return TypedContentForObjects(ids);
}
/// <summary>
@@ -616,8 +678,8 @@ namespace Umbraco.Web
/// <returns>The existing contents corresponding to the identifiers.</returns>
/// <remarks>If an identifier does not match an existing content, it will be missing in the returned value.</remarks>
public IEnumerable<IPublishedContent> TypedContent(IEnumerable<object> ids)
{
return ContentQuery.TypedContent(ConvertIdsObjectToInts(ids));
{
return TypedContentForObjects(ids);
}
/// <summary>
@@ -628,7 +690,7 @@ namespace Umbraco.Web
/// <remarks>If an identifier does not match an existing content, it will be missing in the returned value.</remarks>
public IEnumerable<IPublishedContent> TypedContent(IEnumerable<string> ids)
{
return ContentQuery.TypedContent(ConvertIdsObjectToInts(ids));
return TypedContentForObjects(ids);
}
/// <summary>
@@ -657,22 +719,46 @@ namespace Umbraco.Web
return ContentQuery.TypedContentAtRoot();
}
/// <summary>
/// Gets a content item from the cache.
/// </summary>
/// <param name="id">The unique identifier, or the key, of the content item.</param>
/// <returns>The content, or DynamicNull of the content item is not in the cache.</returns>
public dynamic Content(object id)
{
int intId;
return ConvertIdObjectToInt(id, out intId) ? ContentQuery.Content(intId) : DynamicNull.Null;
return ContentForObject(id);
}
public dynamic Content(int id)
private dynamic ContentForObject(object id)
{
int intId;
if (ConvertIdObjectToInt(id, out intId))
return ContentQuery.Content(intId);
Guid guidId;
if (ConvertIdObjectToGuid(id, out guidId))
return ContentQuery.Content(guidId);
return DynamicNull.Null;
}
/// <summary>
/// Gets a content item from the cache.
/// </summary>
/// <param name="id">The unique identifier of the content item.</param>
/// <returns>The content, or DynamicNull of the content item is not in the cache.</returns>
public dynamic Content(int id)
{
return ContentQuery.Content(id);
}
/// <summary>
/// Gets a content item from the cache.
/// </summary>
/// <param name="id">The unique identifier, or the key, of the content item.</param>
/// <returns>The content, or DynamicNull of the content item is not in the cache.</returns>
public dynamic Content(string id)
{
int intId;
return ConvertIdObjectToInt(id, out intId) ? ContentQuery.Content(intId) : DynamicNull.Null;
}
return ContentForObject(id);
}
public dynamic ContentSingleAtXPath(string xpath, params XPathVariable[] vars)
{
@@ -685,70 +771,80 @@ namespace Umbraco.Web
}
/// <summary>
/// Gets the contents corresponding to the identifiers.
/// Gets content items from the cache.
/// </summary>
/// <param name="ids">The content identifiers.</param>
/// <returns>The existing contents corresponding to the identifiers.</returns>
/// <remarks>If an identifier does not match an existing content, it will be missing in the returned value.</remarks>
/// <param name="ids">The unique identifiers, or the keys, of the content items.</param>
/// <returns>The content items that were found in the cache.</returns>
/// <remarks>Does not support mixing identifiers and keys.</remarks>
public dynamic Content(params object[] ids)
{
return ContentQuery.Content(ConvertIdsObjectToInts(ids));
}
{
return ContentForObjects(ids);
}
private dynamic ContentForObjects(IEnumerable<object> ids)
{
var idsA = ids.ToArray();
IEnumerable<int> intIds;
if (ConvertIdsObjectToInts(idsA, out intIds))
return ContentQuery.Content(intIds);
IEnumerable<Guid> guidIds;
if (ConvertIdsObjectToGuids(idsA, out guidIds))
return ContentQuery.Content(guidIds);
return Enumerable.Empty<IPublishedContent>();
}
/// <summary>
/// Gets the contents corresponding to the identifiers.
/// Gets content items from the cache.
/// </summary>
/// <param name="ids">The content identifiers.</param>
/// <returns>The existing contents corresponding to the identifiers.</returns>
/// <remarks>If an identifier does not match an existing content, it will be missing in the returned value.</remarks>
/// <param name="ids">The unique identifiers of the content items.</param>
/// <returns>The content items that were found in the cache.</returns>
public dynamic Content(params int[] ids)
{
return ContentQuery.Content(ids);
}
/// <summary>
/// Gets the contents corresponding to the identifiers.
/// Gets content items from the cache.
/// </summary>
/// <param name="ids">The content identifiers.</param>
/// <returns>The existing contents corresponding to the identifiers.</returns>
/// <remarks>If an identifier does not match an existing content, it will be missing in the returned value.</remarks>
/// <param name="ids">The unique identifiers, or the keys, of the content items.</param>
/// <returns>The content items that were found in the cache.</returns>
/// <remarks>Does not support mixing identifiers and keys.</remarks>
public dynamic Content(params string[] ids)
{
return ContentQuery.Content(ConvertIdsObjectToInts(ids));
}
return ContentForObjects(ids);
}
/// <summary>
/// Gets the contents corresponding to the identifiers.
/// Gets content items from the cache.
/// </summary>
/// <param name="ids">The content identifiers.</param>
/// <returns>The existing contents corresponding to the identifiers.</returns>
/// <remarks>If an identifier does not match an existing content, it will be missing in the returned value.</remarks>
/// <param name="ids">The unique identifiers, or the keys, of the content items.</param>
/// <returns>The content items that were found in the cache.</returns>
/// <remarks>Does not support mixing identifiers and keys.</remarks>
public dynamic Content(IEnumerable<object> ids)
{
return ContentQuery.Content(ConvertIdsObjectToInts(ids));
}
return ContentForObjects(ids);
}
/// <summary>
/// Gets the contents corresponding to the identifiers.
/// Gets content items from the cache.
/// </summary>
/// <param name="ids">The content identifiers.</param>
/// <returns>The existing contents corresponding to the identifiers.</returns>
/// <remarks>If an identifier does not match an existing content, it will be missing in the returned value.</remarks>
/// <param name="ids">The unique identifiers of the content items.</param>
/// <returns>The content items that were found in the cache.</returns>
public dynamic Content(IEnumerable<int> ids)
{
return ContentQuery.Content(ids);
}
/// <summary>
/// Gets the contents corresponding to the identifiers.
/// Gets content items from the cache.
/// </summary>
/// <param name="ids">The content identifiers.</param>
/// <returns>The existing contents corresponding to the identifiers.</returns>
/// <remarks>If an identifier does not match an existing content, it will be missing in the returned value.</remarks>
/// <param name="ids">The unique identifiers, or the keys, of the content items.</param>
/// <returns>The content items that were found in the cache.</returns>
/// <remarks>Does not support mixing identifiers and keys.</remarks>
public dynamic Content(IEnumerable<string> ids)
{
return ContentQuery.Content(ConvertIdsObjectToInts(ids));
}
return ContentForObjects(ids);
}
public dynamic ContentAtXPath(string xpath, params XPathVariable[] vars)
{
@@ -765,35 +861,68 @@ namespace Umbraco.Web
return ContentQuery.ContentAtRoot();
}
private bool ConvertIdObjectToInt(object id, out int intId)
private static bool ConvertIdObjectToInt(object id, out int intId)
{
var s = id as string;
if (s != null)
{
return int.TryParse(s, out intId);
}
if (id is int)
{
intId = (int) id;
return true;
}
throw new InvalidOperationException("The value of parameter 'id' must be either a string or an integer");
intId = default(int);
return false;
}
private IEnumerable<int> ConvertIdsObjectToInts(IEnumerable<object> ids)
private static bool ConvertIdObjectToGuid(object id, out Guid guidId)
{
var s = id as string;
if (s != null)
{
return Guid.TryParse(s, out guidId);
}
if (id is Guid)
{
guidId = (Guid) id;
return true;
}
guidId = default(Guid);
return false;
}
private static bool ConvertIdsObjectToInts(IEnumerable<object> ids, out IEnumerable<int> intIds)
{
var list = new List<int>();
intIds = null;
foreach (var id in ids)
{
int intId;
if (ConvertIdObjectToInt(id, out intId))
{
list.Add(intId);
}
else
return false; // if one of them is not an int, fail
}
return list;
intIds = list;
return true;
}
private static bool ConvertIdsObjectToGuids(IEnumerable<object> ids, out IEnumerable<Guid> guidIds)
{
var list = new List<Guid>();
guidIds = null;
foreach (var id in ids)
{
Guid guidId;
if (ConvertIdObjectToGuid(id, out guidId))
list.Add(guidId);
else
return false; // if one of them is not a guid, fail
}
guidIds = list;
return true;
}
#endregion
@@ -806,7 +935,7 @@ namespace Umbraco.Web
/// <param name="id"></param>
/// <returns></returns>
/// <remarks>
/// We accept an object type because GetPropertyValue now returns an 'object', we still want to allow people to pass
/// We accept an object type because GetPropertyValue now returns an 'object', we still want to allow people to pass
/// this result in to this method.
/// This method will throw an exception if the value is not of type int or string.
/// </remarks>
@@ -834,9 +963,21 @@ namespace Umbraco.Web
/// <returns>The existing medias corresponding to the identifiers.</returns>
/// <remarks>If an identifier does not match an existing media, it will be missing in the returned value.</remarks>
public IEnumerable<IPublishedContent> TypedMedia(params object[] ids)
{
return ContentQuery.TypedMedia(ConvertIdsObjectToInts(ids));
}
{
return TypedMediaForObjects(ids);
}
private IEnumerable<IPublishedContent> TypedMediaForObjects(IEnumerable<object> ids)
{
var idsA = ids.ToArray();
IEnumerable<int> intIds;
if (ConvertIdsObjectToInts(idsA, out intIds))
return ContentQuery.TypedMedia(intIds);
//IEnumerable<Guid> guidIds;
//if (ConvertIdsObjectToGuids(idsA, out guidIds))
// return ContentQuery.TypedMedia(guidIds);
return Enumerable.Empty<IPublishedContent>();
}
/// <summary>
/// Gets the medias corresponding to the identifiers.
@@ -857,8 +998,8 @@ namespace Umbraco.Web
/// <remarks>If an identifier does not match an existing media, it will be missing in the returned value.</remarks>
public IEnumerable<IPublishedContent> TypedMedia(params string[] ids)
{
return ContentQuery.TypedMedia(ConvertIdsObjectToInts(ids));
}
return TypedMediaForObjects(ids);
}
/// <summary>
/// Gets the medias corresponding to the identifiers.
@@ -868,8 +1009,8 @@ namespace Umbraco.Web
/// <remarks>If an identifier does not match an existing media, it will be missing in the returned value.</remarks>
public IEnumerable<IPublishedContent> TypedMedia(IEnumerable<object> ids)
{
return ContentQuery.TypedMedia(ConvertIdsObjectToInts(ids));
}
return TypedMediaForObjects(ids);
}
/// <summary>
/// Gets the medias corresponding to the identifiers.
@@ -890,8 +1031,8 @@ namespace Umbraco.Web
/// <remarks>If an identifier does not match an existing media, it will be missing in the returned value.</remarks>
public IEnumerable<IPublishedContent> TypedMedia(IEnumerable<string> ids)
{
return ContentQuery.TypedMedia(ConvertIdsObjectToInts(ids));
}
return TypedMediaForObjects(ids);
}
public IEnumerable<IPublishedContent> TypedMediaAtRoot()
{
@@ -922,9 +1063,21 @@ namespace Umbraco.Web
/// <returns>The existing medias corresponding to the identifiers.</returns>
/// <remarks>If an identifier does not match an existing media, it will be missing in the returned value.</remarks>
public dynamic Media(params object[] ids)
{
return ContentQuery.Media(ConvertIdsObjectToInts(ids));
}
{
return MediaForObjects(ids);
}
private dynamic MediaForObjects(IEnumerable<object> ids)
{
var idsA = ids.ToArray();
IEnumerable<int> intIds;
if (ConvertIdsObjectToInts(idsA, out intIds))
return ContentQuery.Media(intIds);
//IEnumerable<Guid> guidIds;
//if (ConvertIdsObjectToGuids(idsA, out guidIds))
// return ContentQuery.Media(guidIds);
return Enumerable.Empty<IPublishedContent>();
}
/// <summary>
/// Gets the medias corresponding to the identifiers.
@@ -944,8 +1097,8 @@ namespace Umbraco.Web
/// <returns>The existing medias corresponding to the identifiers.</returns>
/// <remarks>If an identifier does not match an existing media, it will be missing in the returned value.</remarks>
public dynamic Media(params string[] ids)
{
return ContentQuery.Media(ConvertIdsObjectToInts(ids));
{
return MediaForObjects(ids);
}
/// <summary>
@@ -956,8 +1109,8 @@ namespace Umbraco.Web
/// <remarks>If an identifier does not match an existing media, it will be missing in the returned value.</remarks>
public dynamic Media(IEnumerable<object> ids)
{
return ContentQuery.Media(ConvertIdsObjectToInts(ids));
}
return MediaForObjects(ids);
}
/// <summary>
/// Gets the medias corresponding to the identifiers.
@@ -978,8 +1131,8 @@ namespace Umbraco.Web
/// <remarks>If an identifier does not match an existing media, it will be missing in the returned value.</remarks>
public dynamic Media(IEnumerable<string> ids)
{
return ContentQuery.Media(ConvertIdsObjectToInts(ids));
}
return MediaForObjects(ids);
}
public dynamic MediaAtRoot()
{
@@ -987,7 +1140,7 @@ namespace Umbraco.Web
}
#endregion
#region Search
/// <summary>
@@ -1234,7 +1387,7 @@ namespace Umbraco.Web
#endregion
#region canvasdesigner
[Obsolete("Use EnableCanvasDesigner on the HtmlHelper extensions instead")]
public IHtmlString EnableCanvasDesigner()
{