From 106d733d08957e74ebeee5b59a7c5e7c0ddfe15a Mon Sep 17 00:00:00 2001 From: Stephan Date: Wed, 10 Apr 2013 12:49:45 -0200 Subject: [PATCH] Web.PublishedCache - support XPathExpression --- .../ContextualPublishedCache.cs | 62 +++++++++++++++++++ .../ContextualPublishedCacheOfT.cs | 34 ++++++++++ .../PublishedCache/IPublishedCache.cs | 22 +++++++ .../PublishedContentCache.cs | 22 +++++++ .../XmlPublishedCache/PublishedMediaCache.cs | 12 +++- src/Umbraco.Web/UmbracoHelper.cs | 51 +++++++++++++-- 6 files changed, 198 insertions(+), 5 deletions(-) diff --git a/src/Umbraco.Web/PublishedCache/ContextualPublishedCache.cs b/src/Umbraco.Web/PublishedCache/ContextualPublishedCache.cs index 6436384ffa..188e5712fb 100644 --- a/src/Umbraco.Web/PublishedCache/ContextualPublishedCache.cs +++ b/src/Umbraco.Web/PublishedCache/ContextualPublishedCache.cs @@ -77,6 +77,23 @@ namespace Umbraco.Web.PublishedCache return GetSingleByXPath(UmbracoContext.InPreviewMode, xpath, vars); } + /// + /// Gets a content resulting from an XPath query. + /// + /// The XPath query. + /// Optional XPath variables. + /// The content, or null. + /// + /// If is null, or is empty, or contains only one single + /// value which itself is null, then variables are ignored. + /// The XPath expression should reference variables as $var. + /// Considers published or unpublished content depending on context. + /// + public IPublishedContent GetSingleByXPath(XPathExpression xpath, params XPathVariable[] vars) + { + return GetSingleByXPath(UmbracoContext.InPreviewMode, xpath, vars); + } + /// /// Gets a content resulting from an XPath query. /// @@ -91,6 +108,20 @@ namespace Umbraco.Web.PublishedCache /// public abstract IPublishedContent GetSingleByXPath(bool preview, string xpath, params XPathVariable[] vars); + /// + /// Gets a content resulting from an XPath query. + /// + /// A value indicating whether to consider unpublished content. + /// The XPath query. + /// Optional XPath variables. + /// The content, or null. + /// + /// If is null, or is empty, or contains only one single + /// value which itself is null, then variables are ignored. + /// The XPath expression should reference variables as $var. + /// + public abstract IPublishedContent GetSingleByXPath(bool preview, XPathExpression xpath, params XPathVariable[] vars); + /// /// Gets content resulting from an XPath query. /// @@ -108,6 +139,23 @@ namespace Umbraco.Web.PublishedCache return GetByXPath(UmbracoContext.InPreviewMode, xpath, vars); } + /// + /// Gets content resulting from an XPath query. + /// + /// The XPath query. + /// Optional XPath variables. + /// The contents. + /// + /// If is null, or is empty, or contains only one single + /// value which itself is null, then variables are ignored. + /// The XPath expression should reference variables as $var. + /// Considers published or unpublished content depending on context. + /// + public IEnumerable GetByXPath(XPathExpression xpath, params XPathVariable[] vars) + { + return GetByXPath(UmbracoContext.InPreviewMode, xpath, vars); + } + /// /// Gets content resulting from an XPath query. /// @@ -122,6 +170,20 @@ namespace Umbraco.Web.PublishedCache /// public abstract IEnumerable GetByXPath(bool preview, string xpath, params XPathVariable[] vars); + /// + /// Gets content resulting from an XPath query. + /// + /// A value indicating whether to consider unpublished content. + /// The XPath query. + /// Optional XPath variables. + /// The contents. + /// + /// If is null, or is empty, or contains only one single + /// value which itself is null, then variables are ignored. + /// The XPath expression should reference variables as $var. + /// + public abstract IEnumerable GetByXPath(bool preview, XPathExpression xpath, params XPathVariable[] vars); + /// /// Gets an XPath navigator that can be used to navigate content. /// diff --git a/src/Umbraco.Web/PublishedCache/ContextualPublishedCacheOfT.cs b/src/Umbraco.Web/PublishedCache/ContextualPublishedCacheOfT.cs index a279026e86..67030fa50d 100644 --- a/src/Umbraco.Web/PublishedCache/ContextualPublishedCacheOfT.cs +++ b/src/Umbraco.Web/PublishedCache/ContextualPublishedCacheOfT.cs @@ -71,6 +71,23 @@ namespace Umbraco.Web.PublishedCache return _cache.GetSingleByXPath(UmbracoContext, preview, xpath, vars); } + /// + /// Gets a content resulting from an XPath query. + /// + /// A value indicating whether to consider unpublished content. + /// The XPath query. + /// Optional XPath variables. + /// The content, or null. + /// + /// If is null, or is empty, or contains only one single + /// value which itself is null, then variables are ignored. + /// The XPath expression should reference variables as $var. + /// + public override IPublishedContent GetSingleByXPath(bool preview, XPathExpression xpath, params XPathVariable[] vars) + { + return _cache.GetSingleByXPath(UmbracoContext, preview, xpath, vars); + } + /// /// Gets content resulting from an XPath query. /// @@ -88,6 +105,23 @@ namespace Umbraco.Web.PublishedCache return _cache.GetByXPath(UmbracoContext, preview, xpath, vars); } + /// + /// Gets content resulting from an XPath query. + /// + /// A value indicating whether to consider unpublished content. + /// The XPath query. + /// Optional XPath variables. + /// The contents. + /// + /// If is null, or is empty, or contains only one single + /// value which itself is null, then variables are ignored. + /// The XPath expression should reference variables as $var. + /// + public override IEnumerable GetByXPath(bool preview, XPathExpression xpath, params XPathVariable[] vars) + { + return _cache.GetByXPath(UmbracoContext, preview, xpath, vars); + } + /// /// Gets an XPath navigator that can be used to navigate content. /// diff --git a/src/Umbraco.Web/PublishedCache/IPublishedCache.cs b/src/Umbraco.Web/PublishedCache/IPublishedCache.cs index fb9d7e71b9..50a161422e 100644 --- a/src/Umbraco.Web/PublishedCache/IPublishedCache.cs +++ b/src/Umbraco.Web/PublishedCache/IPublishedCache.cs @@ -46,6 +46,17 @@ namespace Umbraco.Web.PublishedCache /// The value of overrides the context. IPublishedContent GetSingleByXPath(UmbracoContext umbracoContext, bool preview, string xpath, XPathVariable[] vars); + /// + /// Gets a content resulting from an XPath query. + /// + /// The context. + /// A value indicating whether to consider unpublished content. + /// The XPath query. + /// Optional XPath variables. + /// The content, or null. + /// The value of overrides the context. + IPublishedContent GetSingleByXPath(UmbracoContext umbracoContext, bool preview, XPathExpression xpath, XPathVariable[] vars); + /// /// Gets contents resulting from an XPath query. /// @@ -57,6 +68,17 @@ namespace Umbraco.Web.PublishedCache /// The value of overrides the context. IEnumerable GetByXPath(UmbracoContext umbracoContext, bool preview, string xpath, XPathVariable[] vars); + /// + /// Gets contents resulting from an XPath query. + /// + /// The context. + /// A value indicating whether to consider unpublished content. + /// The XPath query. + /// Optional XPath variables. + /// The contents. + /// The value of overrides the context. + IEnumerable GetByXPath(UmbracoContext umbracoContext, bool preview, XPathExpression xpath, XPathVariable[] vars); + /// /// Gets an XPath navigator that can be used to navigate contents. /// diff --git a/src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedContentCache.cs b/src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedContentCache.cs index 10bedb99cb..15372a1758 100644 --- a/src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedContentCache.cs +++ b/src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedContentCache.cs @@ -269,6 +269,17 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache return ConvertToDocument(node); } + public virtual IPublishedContent GetSingleByXPath(UmbracoContext umbracoContext, bool preview, XPathExpression xpath, params XPathVariable[] vars) + { + if (xpath == null) throw new ArgumentNullException("xpath"); + + var xml = GetXml(umbracoContext, preview); + var node = vars == null + ? xml.SelectSingleNode(xpath) + : xml.SelectSingleNode(xpath, vars); + return ConvertToDocument(node); + } + public virtual IEnumerable GetByXPath(UmbracoContext umbracoContext, bool preview, string xpath, params XPathVariable[] vars) { if (xpath == null) throw new ArgumentNullException("xpath"); @@ -281,6 +292,17 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache return ConvertToDocuments(nodes); } + public virtual IEnumerable GetByXPath(UmbracoContext umbracoContext, bool preview, XPathExpression xpath, params XPathVariable[] vars) + { + if (xpath == null) throw new ArgumentNullException("xpath"); + + var xml = GetXml(umbracoContext, preview); + var nodes = vars == null + ? xml.SelectNodes(xpath) + : xml.SelectNodes(xpath, vars); + return ConvertToDocuments(nodes); + } + public virtual bool HasContent(UmbracoContext umbracoContext, bool preview) { var xml = GetXml(umbracoContext, preview); diff --git a/src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedMediaCache.cs b/src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedMediaCache.cs index debb1f8a22..b42aae843b 100644 --- a/src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedMediaCache.cs +++ b/src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedMediaCache.cs @@ -72,7 +72,17 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache throw new NotImplementedException("PublishedMediaCache does not support XPath."); } - public virtual IEnumerable GetByXPath(UmbracoContext umbracoContext, bool preview, string xpath, XPathVariable[] vars) + public virtual IPublishedContent GetSingleByXPath(UmbracoContext umbracoContext, bool preview, XPathExpression xpath, XPathVariable[] vars) + { + throw new NotImplementedException("PublishedMediaCache does not support XPath."); + } + + public virtual IEnumerable GetByXPath(UmbracoContext umbracoContext, bool preview, string xpath, XPathVariable[] vars) + { + throw new NotImplementedException("PublishedMediaCache does not support XPath."); + } + + public virtual IEnumerable GetByXPath(UmbracoContext umbracoContext, bool preview, XPathExpression xpath, XPathVariable[] vars) { throw new NotImplementedException("PublishedMediaCache does not support XPath."); } diff --git a/src/Umbraco.Web/UmbracoHelper.cs b/src/Umbraco.Web/UmbracoHelper.cs index 17fb53ddfa..2e8f4808d6 100644 --- a/src/Umbraco.Web/UmbracoHelper.cs +++ b/src/Umbraco.Web/UmbracoHelper.cs @@ -492,6 +492,11 @@ namespace Umbraco.Web return TypedDocumentsByXPath(xpath, vars, _umbracoContext.ContentCache); } + public IEnumerable TypedContentAtXPath(XPathExpression xpath, params XPathVariable[] vars) + { + return TypedDocumentsByXPath(xpath, vars, _umbracoContext.ContentCache); + } + public IEnumerable TypedContentAtRoot() { return TypedDocumentsAtRoot(_umbracoContext.ContentCache); @@ -517,7 +522,12 @@ namespace Umbraco.Web return DocumentByXPath(xpath, vars, _umbracoContext.ContentCache, new DynamicNull()); } - public dynamic Content(params object[] ids) + public dynamic ContentSingleAtXPath(XPathExpression xpath, params XPathVariable[] vars) + { + return DocumentByXPath(xpath, vars, _umbracoContext.ContentCache, new DynamicNull()); + } + + public dynamic Content(params object[] ids) { return DocumentByIds(_umbracoContext.ContentCache, ids); } @@ -552,6 +562,11 @@ namespace Umbraco.Web return DocumentsByXPath(xpath, vars, _umbracoContext.ContentCache); } + public dynamic ContentAtXPath(XPathExpression xpath, params XPathVariable[] vars) + { + return DocumentsByXPath(xpath, vars, _umbracoContext.ContentCache); + } + public dynamic ContentAtRoot() { return DocumentsAtRoot(_umbracoContext.ContentCache); @@ -715,7 +730,13 @@ namespace Umbraco.Web return doc; } - /// + private IPublishedContent TypedDocumentByXPath(XPathExpression xpath, XPathVariable[] vars, ContextualPublishedContentCache cache) + { + var doc = cache.GetSingleByXPath(xpath, vars); + return doc; + } + + /// /// Overloaded method accepting an 'object' type /// /// @@ -747,6 +768,12 @@ namespace Umbraco.Web return doc; } + private IEnumerable TypedDocumentsByXPath(XPathExpression xpath, XPathVariable[] vars, ContextualPublishedContentCache cache) + { + var doc = cache.GetByXPath(xpath, vars); + return doc; + } + private IEnumerable TypedDocumentsAtRoot(ContextualPublishedCache cache) { return cache.GetAtRoot(); @@ -796,8 +823,16 @@ namespace Umbraco.Web ? ifNotFound : new DynamicPublishedContent(doc).AsDynamic(); } - - /// + + private dynamic DocumentByXPath(XPathExpression xpath, XPathVariable[] vars, ContextualPublishedCache cache, object ifNotFound) + { + var doc = cache.GetSingleByXPath(xpath, vars); + return doc == null + ? ifNotFound + : new DynamicPublishedContent(doc).AsDynamic(); + } + + /// /// Overloaded method accepting an 'object' type /// /// @@ -843,6 +878,14 @@ namespace Umbraco.Web ); } + private dynamic DocumentsByXPath(XPathExpression xpath, XPathVariable[] vars, ContextualPublishedCache cache) + { + return new DynamicPublishedContentList( + cache.GetByXPath(xpath, vars) + .Select(publishedContent => new DynamicPublishedContent(publishedContent)) + ); + } + private dynamic DocumentsAtRoot(ContextualPublishedCache cache) { return new DynamicPublishedContentList(