Files
Umbraco-CMS/src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedContentCache.cs

552 lines
21 KiB
C#
Raw Normal View History

2017-07-20 11:21:28 +02:00
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Xml;
using System.Xml.XPath;
using Umbraco.Core.Configuration;
2013-06-20 15:57:23 +10:00
using Umbraco.Core;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.Xml;
using Umbraco.Web.Routing;
using System.Linq;
using Umbraco.Core.Cache;
namespace Umbraco.Web.PublishedCache.XmlPublishedCache
{
internal class PublishedContentCache : PublishedCacheBase, IPublishedContentCache
{
private readonly ICacheProvider _cacheProvider;
private readonly IGlobalSettings _globalSettings;
private readonly RoutesCache _routesCache;
private readonly IDomainCache _domainCache;
private readonly DomainHelper _domainHelper;
private readonly PublishedContentTypeCache _contentTypeCache;
// initialize a PublishedContentCache instance with
// an XmlStore containing the master xml
// an ICacheProvider that should be at request-level
// a RoutesCache - need to cleanup that one
// a preview token string (or null if not previewing)
public PublishedContentCache(
XmlStore xmlStore, // an XmlStore containing the master xml
IDomainCache domainCache, // an IDomainCache implementation
ICacheProvider cacheProvider, // an ICacheProvider that should be at request-level
IGlobalSettings globalSettings,
ISiteDomainHelper siteDomainHelper,
PublishedContentTypeCache contentTypeCache, // a PublishedContentType cache
RoutesCache routesCache, // a RoutesCache
string previewToken) // a preview token string (or null if not previewing)
: base(previewToken.IsNullOrWhiteSpace() == false)
2015-12-21 17:09:31 +01:00
{
_cacheProvider = cacheProvider;
_globalSettings = globalSettings;
_routesCache = routesCache; // may be null for unit-testing
_contentTypeCache = contentTypeCache;
_domainCache = domainCache;
_domainHelper = new DomainHelper(_domainCache, siteDomainHelper);
_xmlStore = xmlStore;
_xml = _xmlStore.Xml; // capture - because the cache has to remain consistent
if (previewToken.IsNullOrWhiteSpace() == false)
_previewContent = new PreviewContent(_xmlStore, previewToken);
}
#region Unit Tests
// for INTERNAL, UNIT TESTS use ONLY
internal RoutesCache RoutesCache => _routesCache;
// for INTERNAL, UNIT TESTS use ONLY
internal XmlStore XmlStore => _xmlStore;
#endregion
#region Routes
public virtual IPublishedContent GetByRoute(bool preview, string route, bool? hideTopLevelNode = null, string culture = null)
{
if (route == null) throw new ArgumentNullException(nameof(route));
// try to get from cache if not previewing
2017-05-12 14:49:44 +02:00
var contentId = preview || _routesCache == null ? 0 : _routesCache.GetNodeId(route);
// if found id in cache then get corresponding content
// and clear cache if not found - for whatever reason
IPublishedContent content = null;
if (contentId > 0)
{
content = GetById(preview, contentId);
if (content == null)
_routesCache?.ClearNode(contentId);
}
// still have nothing? actually determine the id
hideTopLevelNode = hideTopLevelNode ?? _globalSettings.HideTopLevelNodeFromPath; // default = settings
content = content ?? DetermineIdByRoute(preview, route, hideTopLevelNode.Value);
// cache if we have a content and not previewing
if (content != null && preview == false && _routesCache != null)
2016-06-02 10:03:14 +02:00
AddToCacheIfDeepestRoute(content, route);
return content;
}
2016-06-02 10:03:14 +02:00
private void AddToCacheIfDeepestRoute(IPublishedContent content, string route)
{
var domainRootNodeId = route.StartsWith("/") ? -1 : int.Parse(route.Substring(0, route.IndexOf('/')));
// so we have a route that maps to a content... say "1234/path/to/content" - however, there could be a
// domain set on "to" and route "4567/content" would also map to the same content - and due to how
// urls computing work (by walking the tree up to the first domain we find) it is that second route
// that would be returned - the "deepest" route - and that is the route we want to cache, *not* the
// longer one - so make sure we don't cache the wrong route
2016-06-02 10:03:14 +02:00
var deepest = DomainHelper.ExistsDomainInPath(_domainCache.GetAll(false), content.Path, domainRootNodeId) == false;
if (deepest)
2017-05-12 14:49:44 +02:00
_routesCache.Store(content.Id, route, true); // trusted route
}
public IPublishedContent GetByRoute(string route, bool? hideTopLevelNode = null, string culture = null)
{
return GetByRoute(PreviewDefault, route, hideTopLevelNode);
}
public virtual string GetRouteById(bool preview, int contentId, string culture = null)
{
// try to get from cache if not previewing
2017-05-12 14:49:44 +02:00
var route = preview || _routesCache == null ? null : _routesCache.GetRoute(contentId);
// if found in cache then return
if (route != null)
return route;
// else actually determine the route
route = DetermineRouteById(preview, contentId);
// node not found
if (route == null)
return null;
2017-05-12 14:49:44 +02:00
// cache the route BUT do NOT trust it as it can be a colliding route
// meaning if we GetRouteById again, we'll get it from cache, but it
// won't be used for inbound routing
if (preview == false)
_routesCache.Store(contentId, route, false);
2017-05-12 14:49:44 +02:00
return route;
}
public string GetRouteById(int contentId, string culture = null)
{
return GetRouteById(PreviewDefault, contentId, culture);
}
IPublishedContent DetermineIdByRoute(bool preview, string route, bool hideTopLevelNode)
{
//the route always needs to be lower case because we only store the urlName attribute in lower case
2017-05-12 14:49:44 +02:00
route = route?.ToLowerInvariant() ?? throw new ArgumentNullException(nameof(route));
var pos = route.IndexOf('/');
var path = pos == 0 ? route : route.Substring(pos);
var startNodeId = pos == 0 ? 0 : int.Parse(route.Substring(0, pos));
//check if we can find the node in our xml cache
var id = NavigateRoute(preview, startNodeId, path, hideTopLevelNode);
2017-05-12 14:49:44 +02:00
return id > 0 ? GetById(preview, id) : null;
}
2017-05-12 14:49:44 +02:00
private static XmlElement GetXmlElementChildWithLowestSortOrder(XmlNode element)
{
XmlElement elt = null;
var min = int.MaxValue;
foreach (var n in element.ChildNodes)
{
2017-05-12 14:49:44 +02:00
var e = n as XmlElement;
if (e == null) continue;
2017-05-12 14:49:44 +02:00
var sortOrder = int.Parse(e.GetAttribute("sortOrder"));
if (sortOrder >= min) continue;
min = sortOrder;
elt = e;
}
return elt;
2016-10-28 14:33:44 +02:00
}
private int NavigateRoute(bool preview, int startNodeId, string path, bool hideTopLevelNode)
2016-10-28 14:33:44 +02:00
{
var xml = GetXml(preview);
2016-10-28 14:33:44 +02:00
XmlElement elt;
// empty path
if (path == string.Empty || path == "/")
{
if (startNodeId > 0)
{
elt = xml.GetElementById(startNodeId.ToString(CultureInfo.InvariantCulture));
return elt == null ? -1 : startNodeId;
}
2017-05-12 14:49:44 +02:00
elt = GetXmlElementChildWithLowestSortOrder(xml.DocumentElement);
2016-10-28 14:33:44 +02:00
return elt == null ? -1 : int.Parse(elt.GetAttribute("id"));
}
// non-empty path
elt = startNodeId <= 0
? xml.DocumentElement
: xml.GetElementById(startNodeId.ToString(CultureInfo.InvariantCulture));
if (elt == null) return -1;
var urlParts = path.Split(SlashChar, StringSplitOptions.RemoveEmptyEntries);
if (hideTopLevelNode && startNodeId <= 0)
{
2017-05-12 14:49:44 +02:00
//Don't use OfType<T> or Cast<T>, this is critical code, all ChildNodes are XmlElement so explicitly cast
// https://gist.github.com/Shazwazza/04e2e5642a316f4a87e52dada2901198
foreach (var n in elt.ChildNodes)
2016-10-28 14:33:44 +02:00
{
2017-05-12 14:49:44 +02:00
var e = n as XmlElement;
if (e == null) continue;
2016-10-28 14:33:44 +02:00
var id = NavigateElementRoute(e, urlParts);
if (id > 0) return id;
}
2017-05-12 14:49:44 +02:00
if (urlParts.Length > 1)
return -1;
2016-10-28 14:33:44 +02:00
}
return NavigateElementRoute(elt, urlParts);
}
private static int NavigateElementRoute(XmlElement elt, string[] urlParts)
2016-10-28 14:33:44 +02:00
{
var found = true;
var i = 0;
while (found && i < urlParts.Length)
{
found = false;
2017-05-12 14:49:44 +02:00
//Don't use OfType<T> or Cast<T>, this is critical code, all ChildNodes are XmlElement so explicitly cast
// https://gist.github.com/Shazwazza/04e2e5642a316f4a87e52dada2901198
var sortOrder = -1;
foreach (var o in elt.ChildNodes)
2016-10-28 14:33:44 +02:00
{
2017-05-12 14:49:44 +02:00
var child = o as XmlElement;
if (child == null) continue;
var noNode = child.GetAttributeNode("isDoc") == null;
2016-10-28 14:33:44 +02:00
if (noNode) continue;
if (child.GetAttribute("urlName") != urlParts[i]) continue;
found = true;
2017-05-12 14:49:44 +02:00
var so = int.Parse(child.GetAttribute("sortOrder"));
if (sortOrder >= 0 && so >= sortOrder) continue;
sortOrder = so;
2016-10-28 14:33:44 +02:00
elt = child;
}
i++;
}
return found ? int.Parse(elt.GetAttribute("id")) : -1;
}
string DetermineRouteById(bool preview, int contentId)
{
var node = GetById(preview, contentId);
if (node == null) return null;
// walk up from that node until we hit a node with a domain,
// or we reach the content root, collecting urls in the way
var pathParts = new List<string>();
var n = node;
var hasDomains = _domainHelper.NodeHasDomains(n.Id);
while (hasDomains == false && n != null) // n is null at root
{
// get the url
2018-04-28 16:35:33 +02:00
var urlName = n.UrlSegment;
pathParts.Add(urlName);
// move to parent node
n = n.Parent;
hasDomains = n != null && _domainHelper.NodeHasDomains(n.Id);
}
// no domain, respect HideTopLevelNodeFromPath for legacy purposes
if (hasDomains == false && _globalSettings.HideTopLevelNodeFromPath)
2017-05-12 14:49:44 +02:00
{
if (node.Parent == null)
{
var rootNode = GetByRoute(preview, "/", true);
if (rootNode == null)
throw new Exception("Failed to get node at /.");
if (rootNode.Id == node.Id) // remove only if we're the default node
pathParts.RemoveAt(pathParts.Count - 1);
}
else
{
pathParts.RemoveAt(pathParts.Count - 1);
}
}
// assemble the route
pathParts.Reverse();
var path = "/" + string.Join("/", pathParts); // will be "/" or "/foo" or "/foo/bar" etc
var route = (n?.Id.ToString(CultureInfo.InvariantCulture) ?? "") + path;
return route;
}
#endregion
#region XPath Strings
static class XPathStrings
{
public const string Root = "/root";
public const string RootDocuments = "/root/* [@isDoc]";
}
#endregion
#region Converters
2016-06-29 14:46:53 +02:00
private IPublishedContent ConvertToDocument(XmlNode xmlNode, bool isPreviewing)
2017-07-20 11:21:28 +02:00
{
2016-07-21 11:07:25 +02:00
return xmlNode == null ? null : XmlPublishedContent.Get(xmlNode, isPreviewing, _cacheProvider, _contentTypeCache);
2017-07-20 11:21:28 +02:00
}
2016-06-29 14:46:53 +02:00
private IEnumerable<IPublishedContent> ConvertToDocuments(XmlNodeList xmlNodes, bool isPreviewing)
2013-02-05 06:31:13 -01:00
{
return xmlNodes.Cast<XmlNode>()
2016-07-21 11:07:25 +02:00
.Select(xmlNode => XmlPublishedContent.Get(xmlNode, isPreviewing, _cacheProvider, _contentTypeCache));
2013-02-05 06:31:13 -01:00
}
#endregion
#region Getters
public override IPublishedContent GetById(bool preview, int nodeId)
2017-07-20 11:21:28 +02:00
{
return ConvertToDocument(GetXml(preview).GetElementById(nodeId.ToString(CultureInfo.InvariantCulture)), preview);
}
public override IPublishedContent GetById(bool preview, Guid nodeId)
{
// implement this, but in a more efficient way
//const string xpath = "//* [@isDoc and @key=$guid]";
//return GetSingleByXPath(preview, xpath, new[] { new XPathVariable("guid", nodeId.ToString()) });
var keyMatch = nodeId.ToString();
var nav = GetXml(preview).CreateNavigator();
if (nav.MoveToFirstChild() == false) return null; // from / to /root
if (nav.MoveToFirstChild() == false) return null; // from /root to /root/*
while (true)
{
var isDoc = false;
string key = null;
if (nav.HasAttributes)
{
nav.MoveToFirstAttribute();
do
{
if (nav.Name == "isDoc") isDoc = true;
if (nav.Name == "key") key = nav.Value;
if (isDoc && key != null) break;
} while (nav.MoveToNextAttribute());
nav.MoveToParent();
}
if (isDoc == false || key != keyMatch)
{
if (isDoc && nav.MoveToFirstChild())
continue;
while (nav.MoveToNext(XPathNodeType.Element) == false)
if (nav.MoveToParent() == false || nav.NodeType == XPathNodeType.Root) return null;
continue;
}
var elt = nav.UnderlyingObject as XmlNode;
return ConvertToDocument(elt, preview);
}
}
public override bool HasById(bool preview, int contentId)
{
return GetXml(preview).CreateNavigator().MoveToId(contentId.ToString(CultureInfo.InvariantCulture));
}
public override IEnumerable<IPublishedContent> GetAtRoot(bool preview)
{
2016-06-29 14:46:53 +02:00
return ConvertToDocuments(GetXml(preview).SelectNodes(XPathStrings.RootDocuments), preview);
2017-07-20 11:21:28 +02:00
}
public override IPublishedContent GetSingleByXPath(bool preview, string xpath, XPathVariable[] vars)
2013-02-05 06:31:13 -01:00
{
if (xpath == null) throw new ArgumentNullException(nameof(xpath));
2013-02-05 06:31:13 -01:00
if (string.IsNullOrWhiteSpace(xpath)) return null;
var xml = GetXml(preview);
2013-02-05 06:31:13 -01:00
var node = vars == null
? xml.SelectSingleNode(xpath)
: xml.SelectSingleNode(xpath, vars);
2016-06-29 14:46:53 +02:00
return ConvertToDocument(node, preview);
2013-02-05 06:31:13 -01:00
}
public override IPublishedContent GetSingleByXPath(bool preview, XPathExpression xpath, XPathVariable[] vars)
{
if (xpath == null) throw new ArgumentNullException(nameof(xpath));
var xml = GetXml(preview);
var node = vars == null
? xml.SelectSingleNode(xpath)
: xml.SelectSingleNode(xpath, vars);
2016-06-29 14:46:53 +02:00
return ConvertToDocument(node, preview);
}
public override IEnumerable<IPublishedContent> GetByXPath(bool preview, string xpath, XPathVariable[] vars)
2013-02-05 06:31:13 -01:00
{
if (xpath == null) throw new ArgumentNullException(nameof(xpath));
2013-02-05 06:31:13 -01:00
if (string.IsNullOrWhiteSpace(xpath)) return Enumerable.Empty<IPublishedContent>();
var xml = GetXml(preview);
var nodes = vars == null
? xml.SelectNodes(xpath)
: xml.SelectNodes(xpath, vars);
2016-06-29 14:46:53 +02:00
return ConvertToDocuments(nodes, preview);
}
public override IEnumerable<IPublishedContent> GetByXPath(bool preview, XPathExpression xpath, XPathVariable[] vars)
{
if (xpath == null) throw new ArgumentNullException(nameof(xpath));
var xml = GetXml(preview);
2013-02-05 06:31:13 -01:00
var nodes = vars == null
? xml.SelectNodes(xpath)
: xml.SelectNodes(xpath, vars);
2016-06-29 14:46:53 +02:00
return ConvertToDocuments(nodes, preview);
2013-02-05 06:31:13 -01:00
}
public override bool HasContent(bool preview)
{
2017-07-20 11:21:28 +02:00
var xml = GetXml(preview);
var node = xml?.SelectSingleNode(XPathStrings.RootDocuments);
2017-07-20 11:21:28 +02:00
return node != null;
}
public override XPathNavigator CreateNavigator(bool preview)
{
var xml = GetXml(preview);
return xml.CreateNavigator();
}
public override XPathNavigator CreateNodeNavigator(int id, bool preview)
{
// hackish - backward compatibility ;-(
XPathNavigator navigator = null;
if (preview)
{
var node = _xmlStore.GetPreviewXmlNode(id);
if (node != null)
{
navigator = node.CreateNavigator();
}
}
else
{
var node = GetXml(false).GetElementById(id.ToInvariantString());
if (node != null)
{
var doc = new XmlDocument();
var clone = doc.ImportNode(node, false);
var props = node.SelectNodes("./* [not(@id)]");
if (props == null) throw new Exception("oops");
foreach (var n in props.Cast<XmlNode>())
clone.AppendChild(doc.ImportNode(n, true));
navigator = node.CreateNavigator();
}
}
return navigator;
}
#endregion
#region Legacy Xml
private readonly XmlStore _xmlStore;
private XmlDocument _xml;
private readonly PreviewContent _previewContent;
internal XmlDocument GetXml(bool preview)
{
// not trying to be thread-safe here, that's not the point
if (preview == false)
2018-03-27 10:51:41 +02:00
{
// if there's a current enlisted reader/writer, use its xml
var tempXml = _xmlStore.TempXml;
if (tempXml != null) return tempXml;
return _xml;
2018-03-27 10:51:41 +02:00
}
// Xml cache does not support retrieving preview content when not previewing
if (_previewContent == null)
throw new InvalidOperationException("Cannot retrieve preview content when not previewing.");
// PreviewContent tries to load the Xml once and if it fails,
// it invalidates itself and always return null for XmlContent.
var previewXml = _previewContent.XmlContent;
return previewXml ?? _xml;
}
2017-06-27 16:09:33 +02:00
internal void Resync(XmlDocument xml)
{
2017-06-27 16:09:33 +02:00
_xml = xml; // re-capture
// note: we're not resyncing "preview" because that would mean re-building the whole
// preview set which is costly, so basically when previewing, there will be no resync.
// clear recursive properties cached by XmlPublishedContent.GetProperty
// assume that nothing else is going to cache IPublishedProperty items (else would need to do ByKeySearch)
// NOTE also clears all the media cache properties, which is OK (see media cache)
_cacheProvider.ClearCacheObjectTypes<IPublishedProperty>();
//_cacheProvider.ClearCacheByKeySearch("XmlPublishedCache.PublishedContentCache:RecursiveProperty-");
}
#endregion
#region XPathQuery
static readonly char[] SlashChar = { '/' };
#endregion
#region Content types
public override PublishedContentType GetContentType(int id)
{
return _contentTypeCache.Get(PublishedItemType.Content, id);
}
public override PublishedContentType GetContentType(string alias)
{
return _contentTypeCache.Get(PublishedItemType.Content, alias);
}
public override IEnumerable<IPublishedContent> GetByContentType(PublishedContentType contentType)
{
throw new NotImplementedException();
}
#endregion
}
2017-07-20 11:21:28 +02:00
}