2012-07-20 01:04:35 +06:00
|
|
|
using System;
|
2012-10-04 01:31:08 +05:00
|
|
|
using System.Collections.Generic;
|
2012-07-20 01:04:35 +06:00
|
|
|
using System.Text;
|
|
|
|
|
using System.Xml;
|
2012-08-10 13:08:47 +06:00
|
|
|
using Umbraco.Core.Models;
|
2013-02-14 16:23:56 -01:00
|
|
|
using Umbraco.Core.Xml;
|
2012-07-20 01:04:35 +06:00
|
|
|
using umbraco;
|
2012-09-13 12:19:56 +07:00
|
|
|
using System.Linq;
|
2012-07-20 01:04:35 +06:00
|
|
|
|
2013-02-05 06:31:13 -01:00
|
|
|
namespace Umbraco.Web.PublishedCache.LegacyXmlCache
|
2012-07-20 01:04:35 +06:00
|
|
|
{
|
2013-02-05 06:31:13 -01:00
|
|
|
internal class PublishedContentCache : IPublishedContentCache
|
2012-10-05 08:00:06 -02:00
|
|
|
{
|
|
|
|
|
#region XPath Strings
|
2012-07-20 01:04:35 +06:00
|
|
|
|
2012-10-05 08:00:06 -02:00
|
|
|
class XPathStringsDefinition
|
|
|
|
|
{
|
2012-10-05 08:23:43 -02:00
|
|
|
public int Version { get; private set; }
|
|
|
|
|
|
2013-02-05 06:31:13 -01:00
|
|
|
public static string Root { get { return "/root"; } }
|
2012-10-05 08:00:06 -02:00
|
|
|
public string RootDocuments { get; private set; }
|
|
|
|
|
public string DescendantDocumentById { get; private set; }
|
|
|
|
|
public string DescendantDocumentByAlias { get; private set; }
|
|
|
|
|
public string ChildDocumentByUrlName { get; private set; }
|
2013-02-19 13:58:16 -01:00
|
|
|
public string ChildDocumentByUrlNameVar { get; private set; }
|
|
|
|
|
public string RootDocumentWithLowestSortOrder { get; private set; }
|
2012-10-05 08:00:06 -02:00
|
|
|
|
|
|
|
|
public XPathStringsDefinition(int version)
|
|
|
|
|
{
|
2012-10-05 08:23:43 -02:00
|
|
|
Version = version;
|
|
|
|
|
|
2012-10-05 08:00:06 -02:00
|
|
|
switch (version)
|
|
|
|
|
{
|
|
|
|
|
// legacy XML schema
|
|
|
|
|
case 0:
|
|
|
|
|
RootDocuments = "/root/node";
|
|
|
|
|
DescendantDocumentById = "//node [@id={0}]";
|
|
|
|
|
DescendantDocumentByAlias = "//node[("
|
|
|
|
|
+ "contains(concat(',',translate(data [@alias='umbracoUrlAlias'], ' ', ''),','),',{0},')"
|
2012-11-30 13:33:24 -01:00
|
|
|
+ " or contains(concat(',',translate(data [@alias='umbracoUrlAlias'], ' ', ''),','),',/{0},')"
|
2012-10-05 08:00:06 -02:00
|
|
|
+ ")]";
|
|
|
|
|
ChildDocumentByUrlName = "/node [@urlName='{0}']";
|
2013-02-19 13:58:16 -01:00
|
|
|
ChildDocumentByUrlNameVar = "/node [@urlName=${0}]";
|
2012-10-05 08:00:06 -02:00
|
|
|
RootDocumentWithLowestSortOrder = "/root/node [not(@sortOrder > ../node/@sortOrder)][1]";
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// default XML schema as of 4.10
|
|
|
|
|
case 1:
|
|
|
|
|
RootDocuments = "/root/* [@isDoc]";
|
|
|
|
|
DescendantDocumentById = "//* [@isDoc and @id={0}]";
|
|
|
|
|
DescendantDocumentByAlias = "//* [@isDoc and ("
|
|
|
|
|
+ "contains(concat(',',translate(umbracoUrlAlias, ' ', ''),','),',{0},')"
|
|
|
|
|
+ " or contains(concat(',',translate(umbracoUrlAlias, ' ', ''),','),',/{0},')"
|
|
|
|
|
+ ")]";
|
|
|
|
|
ChildDocumentByUrlName = "/* [@isDoc and @urlName='{0}']";
|
2013-02-19 13:58:16 -01:00
|
|
|
ChildDocumentByUrlNameVar = "/* [@isDoc and @urlName=${0}]";
|
2012-10-05 08:00:06 -02:00
|
|
|
RootDocumentWithLowestSortOrder = "/root/* [@isDoc and not(@sortOrder > ../* [@isDoc]/@sortOrder)][1]";
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw new Exception(string.Format("Unsupported Xml schema version '{0}').", version));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-05 06:31:13 -01:00
|
|
|
static XPathStringsDefinition _xPathStringsValue;
|
2012-10-05 08:23:43 -02:00
|
|
|
static XPathStringsDefinition XPathStrings
|
2012-10-05 08:00:06 -02:00
|
|
|
{
|
2012-10-05 08:23:43 -02:00
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
// in theory XPathStrings should be a static variable that
|
|
|
|
|
// we should initialize in a static ctor - but then test cases
|
|
|
|
|
// that switch schemas fail - so cache and refresh when needed,
|
|
|
|
|
// ie never when running the actual site
|
|
|
|
|
|
2013-02-05 06:31:13 -01:00
|
|
|
var version = UmbracoSettings.UseLegacyXmlSchema ? 0 : 1;
|
|
|
|
|
if (_xPathStringsValue == null || _xPathStringsValue.Version != version)
|
|
|
|
|
_xPathStringsValue = new XPathStringsDefinition(version);
|
|
|
|
|
return _xPathStringsValue;
|
2012-10-05 08:23:43 -02:00
|
|
|
}
|
2012-10-05 08:00:06 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2013-02-05 06:31:13 -01:00
|
|
|
private static IPublishedContent ConvertToDocument(XmlNode xmlNode)
|
2012-08-10 13:08:47 +06:00
|
|
|
{
|
2013-02-05 06:31:13 -01:00
|
|
|
return xmlNode == null ? null : new Models.XmlPublishedContent(xmlNode);
|
|
|
|
|
}
|
2012-08-10 13:08:47 +06:00
|
|
|
|
2013-02-05 06:31:13 -01:00
|
|
|
private static IEnumerable<IPublishedContent> ConvertToDocuments(XmlNodeList xmlNodes)
|
|
|
|
|
{
|
|
|
|
|
return xmlNodes.Cast<XmlNode>().Select(xmlNode => new Models.XmlPublishedContent(xmlNode));
|
|
|
|
|
}
|
2012-08-10 13:38:02 +06:00
|
|
|
|
2013-02-05 06:31:13 -01:00
|
|
|
public virtual IPublishedContent GetById(UmbracoContext umbracoContext, int nodeId)
|
2012-09-08 11:59:01 +07:00
|
|
|
{
|
|
|
|
|
if (umbracoContext == null) throw new ArgumentNullException("umbracoContext");
|
|
|
|
|
|
|
|
|
|
return ConvertToDocument(GetXml(umbracoContext).GetElementById(nodeId.ToString()));
|
|
|
|
|
}
|
2012-07-20 01:04:35 +06:00
|
|
|
|
2013-02-05 06:31:13 -01:00
|
|
|
public IEnumerable<IPublishedContent> GetAtRoot(UmbracoContext umbracoContext)
|
2012-10-04 01:31:08 +05:00
|
|
|
{
|
2012-10-05 08:00:06 -02:00
|
|
|
return (from XmlNode x in GetXml(umbracoContext).SelectNodes(XPathStrings.RootDocuments) select ConvertToDocument(x)).ToList();
|
2012-10-04 01:31:08 +05:00
|
|
|
}
|
|
|
|
|
|
2013-02-05 06:31:13 -01:00
|
|
|
public IPublishedContent GetSingleByXPath(UmbracoContext umbracoContext, string xpath, params XPathVariable[] vars)
|
|
|
|
|
{
|
|
|
|
|
if (umbracoContext == null) throw new ArgumentNullException("umbracoContext");
|
|
|
|
|
if (xpath == null) throw new ArgumentNullException("xpath");
|
|
|
|
|
if (string.IsNullOrWhiteSpace(xpath)) return null;
|
|
|
|
|
|
|
|
|
|
var xml = GetXml(umbracoContext);
|
|
|
|
|
var node = vars == null
|
|
|
|
|
? xml.SelectSingleNode(xpath)
|
|
|
|
|
: xml.SelectSingleNode(xpath, vars);
|
|
|
|
|
return ConvertToDocument(node);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<IPublishedContent> GetByXPath(UmbracoContext umbracoContext, string xpath, params XPathVariable[] vars)
|
|
|
|
|
{
|
|
|
|
|
if (umbracoContext == null) throw new ArgumentNullException("umbracoContext");
|
|
|
|
|
if (xpath == null) throw new ArgumentNullException("xpath");
|
|
|
|
|
if (string.IsNullOrWhiteSpace(xpath)) return Enumerable.Empty<IPublishedContent>();
|
|
|
|
|
|
|
|
|
|
var xml = GetXml(umbracoContext);
|
|
|
|
|
var nodes = vars == null
|
|
|
|
|
? xml.SelectNodes(xpath)
|
|
|
|
|
: xml.SelectNodes(xpath, vars);
|
|
|
|
|
return ConvertToDocuments(nodes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//FIXME keep here or remove?
|
|
|
|
|
public IPublishedContent GetByRoute(UmbracoContext umbracoContext, string route, bool? hideTopLevelNode = null)
|
2012-07-20 01:04:35 +06:00
|
|
|
{
|
2012-09-08 11:59:01 +07:00
|
|
|
if (umbracoContext == null) throw new ArgumentNullException("umbracoContext");
|
|
|
|
|
if (route == null) throw new ArgumentNullException("route");
|
|
|
|
|
|
2012-08-09 07:41:13 +06:00
|
|
|
//set the default to be what is in the settings
|
2012-09-24 11:36:25 -02:00
|
|
|
hideTopLevelNode = hideTopLevelNode ?? GlobalSettings.HideTopLevelNodeFromPath;
|
2012-08-09 07:41:13 +06:00
|
|
|
|
|
|
|
|
//the route always needs to be lower case because we only store the urlName attribute in lower case
|
|
|
|
|
route = route.ToLowerInvariant();
|
|
|
|
|
|
2013-02-05 06:31:13 -01:00
|
|
|
var pos = route.IndexOf('/');
|
|
|
|
|
var path = pos == 0 ? route : route.Substring(pos);
|
|
|
|
|
var startNodeId = pos == 0 ? 0 : int.Parse(route.Substring(0, pos));
|
2013-02-14 16:23:56 -01:00
|
|
|
IEnumerable<XPathVariable> vars;
|
2012-07-20 01:04:35 +06:00
|
|
|
|
2013-02-14 16:23:56 -01:00
|
|
|
var xpath = CreateXpathQuery(startNodeId, path, hideTopLevelNode.Value, out vars);
|
2012-08-10 13:08:47 +06:00
|
|
|
|
2012-09-13 12:19:56 +07:00
|
|
|
//check if we can find the node in our xml cache
|
2013-02-05 06:31:13 -01:00
|
|
|
var content = GetSingleByXPath(umbracoContext, xpath, vars == null ? null : vars.ToArray());
|
2012-09-13 12:19:56 +07:00
|
|
|
|
2012-09-24 11:36:25 -02:00
|
|
|
// if hideTopLevelNodePath is true then for url /foo we looked for /*/foo
|
|
|
|
|
// but maybe that was the url of a non-default top-level node, so we also
|
|
|
|
|
// have to look for /foo (see note in NiceUrlProvider).
|
2013-02-05 06:31:13 -01:00
|
|
|
if (content == null && hideTopLevelNode.Value && path.Length > 1 && path.IndexOf('/', 1) < 0)
|
2012-09-13 12:19:56 +07:00
|
|
|
{
|
2013-02-14 16:23:56 -01:00
|
|
|
xpath = CreateXpathQuery(startNodeId, path, false, out vars);
|
2013-02-05 06:31:13 -01:00
|
|
|
content = GetSingleByXPath(umbracoContext, xpath, vars == null ? null : vars.ToArray());
|
2012-09-13 12:19:56 +07:00
|
|
|
}
|
|
|
|
|
|
2013-02-05 06:31:13 -01:00
|
|
|
return content;
|
2012-07-20 01:04:35 +06:00
|
|
|
}
|
|
|
|
|
|
2013-02-05 06:31:13 -01:00
|
|
|
// FIXME MOVE THAT ONE OUT OF HERE?
|
|
|
|
|
public IPublishedContent GetByUrlAlias(UmbracoContext umbracoContext, int rootNodeId, string alias)
|
2012-07-20 01:04:35 +06:00
|
|
|
{
|
2012-09-08 11:59:01 +07:00
|
|
|
if (umbracoContext == null) throw new ArgumentNullException("umbracoContext");
|
|
|
|
|
if (alias == null) throw new ArgumentNullException("alias");
|
2012-07-20 01:04:35 +06:00
|
|
|
|
2012-09-08 11:59:01 +07:00
|
|
|
// the alias may be "foo/bar" or "/foo/bar"
|
2012-07-20 01:04:35 +06:00
|
|
|
// there may be spaces as in "/foo/bar, /foo/nil"
|
|
|
|
|
// these should probably be taken care of earlier on
|
|
|
|
|
|
|
|
|
|
alias = alias.TrimStart('/');
|
|
|
|
|
var xpathBuilder = new StringBuilder();
|
2013-02-05 06:31:13 -01:00
|
|
|
xpathBuilder.Append(XPathStringsDefinition.Root);
|
2012-10-04 21:37:11 +05:00
|
|
|
|
2012-10-05 08:00:06 -02:00
|
|
|
if (rootNodeId > 0)
|
|
|
|
|
xpathBuilder.AppendFormat(XPathStrings.DescendantDocumentById, rootNodeId);
|
2013-02-14 16:23:56 -01:00
|
|
|
|
|
|
|
|
XPathVariable var = null;
|
|
|
|
|
if (alias.Contains('\'') || alias.Contains('"'))
|
|
|
|
|
{
|
|
|
|
|
// use a var, escaping gets ugly pretty quickly
|
|
|
|
|
var = new XPathVariable("alias", alias);
|
|
|
|
|
alias = "$alias";
|
|
|
|
|
}
|
2012-10-05 08:00:06 -02:00
|
|
|
xpathBuilder.AppendFormat(XPathStrings.DescendantDocumentByAlias, alias);
|
2012-07-20 01:04:35 +06:00
|
|
|
|
2012-10-05 08:00:06 -02:00
|
|
|
var xpath = xpathBuilder.ToString();
|
2012-07-20 01:04:35 +06:00
|
|
|
|
2013-02-05 06:31:13 -01:00
|
|
|
return GetSingleByXPath(umbracoContext, xpath, var);
|
2012-07-20 01:04:35 +06:00
|
|
|
}
|
|
|
|
|
|
2012-09-28 07:04:33 -02:00
|
|
|
public bool HasContent(UmbracoContext umbracoContext)
|
|
|
|
|
{
|
2012-10-11 02:30:48 +05:00
|
|
|
var xml = GetXml(umbracoContext);
|
|
|
|
|
if (xml == null)
|
|
|
|
|
return false;
|
|
|
|
|
var node = xml.SelectSingleNode(XPathStrings.RootDocuments);
|
2012-10-05 08:00:06 -02:00
|
|
|
return node != null;
|
2012-09-28 07:04:33 -02:00
|
|
|
}
|
|
|
|
|
|
2013-02-05 06:31:13 -01:00
|
|
|
static XmlDocument GetXml(UmbracoContext umbracoContext)
|
2012-09-08 11:59:01 +07:00
|
|
|
{
|
|
|
|
|
if (umbracoContext == null) throw new ArgumentNullException("umbracoContext");
|
2012-07-20 01:04:35 +06:00
|
|
|
|
2012-08-10 13:38:02 +06:00
|
|
|
return umbracoContext.GetXml();
|
2012-09-08 11:59:01 +07:00
|
|
|
}
|
2012-07-20 01:04:35 +06:00
|
|
|
|
2013-02-05 06:31:13 -01:00
|
|
|
static readonly char[] SlashChar = new[] { '/' };
|
2012-07-20 01:04:35 +06:00
|
|
|
|
2013-02-14 16:23:56 -01:00
|
|
|
protected string CreateXpathQuery(int startNodeId, string path, bool hideTopLevelNodeFromPath, out IEnumerable<XPathVariable> vars)
|
2012-07-20 01:04:35 +06:00
|
|
|
{
|
|
|
|
|
string xpath;
|
2013-02-14 16:23:56 -01:00
|
|
|
vars = null;
|
2012-07-20 01:04:35 +06:00
|
|
|
|
|
|
|
|
if (path == string.Empty || path == "/")
|
|
|
|
|
{
|
|
|
|
|
// if url is empty
|
|
|
|
|
if (startNodeId > 0)
|
|
|
|
|
{
|
2012-10-05 08:00:06 -02:00
|
|
|
// if in a domain then use the root node of the domain
|
2013-02-05 06:31:13 -01:00
|
|
|
xpath = string.Format(XPathStringsDefinition.Root + XPathStrings.DescendantDocumentById, startNodeId);
|
2012-07-20 01:04:35 +06:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// if not in a domain - what is the default page?
|
2012-09-30 12:25:47 -02:00
|
|
|
// let's say it is the first one in the tree, if any -- order by sortOrder
|
2012-10-01 06:55:44 -02:00
|
|
|
|
|
|
|
|
// but!
|
|
|
|
|
// umbraco does not consistently guarantee that sortOrder starts with 0
|
|
|
|
|
// so the one that we want is the one with the smallest sortOrder
|
|
|
|
|
// read http://stackoverflow.com/questions/1128745/how-can-i-use-xpath-to-find-the-minimum-value-of-an-attribute-in-a-set-of-elemen
|
|
|
|
|
|
|
|
|
|
// so that one does not work, because min(@sortOrder) maybe 1
|
|
|
|
|
// xpath = "/root/*[@isDoc and @sortOrder='0']";
|
|
|
|
|
|
|
|
|
|
// and we can't use min() because that's XPath 2.0
|
|
|
|
|
// that one works
|
2012-10-05 08:00:06 -02:00
|
|
|
xpath = XPathStrings.RootDocumentWithLowestSortOrder;
|
2012-07-20 01:04:35 +06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// if url is not empty, then use it to try lookup a matching page
|
|
|
|
|
var urlParts = path.Split(SlashChar, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
var xpathBuilder = new StringBuilder();
|
|
|
|
|
int partsIndex = 0;
|
2013-02-14 16:23:56 -01:00
|
|
|
List<XPathVariable> varsList = null;
|
2012-07-20 01:04:35 +06:00
|
|
|
|
|
|
|
|
if (startNodeId == 0)
|
|
|
|
|
{
|
2012-10-05 08:00:06 -02:00
|
|
|
if (hideTopLevelNodeFromPath)
|
|
|
|
|
xpathBuilder.Append(XPathStrings.RootDocuments); // first node is not in the url
|
2012-10-04 21:37:11 +05:00
|
|
|
else
|
2013-02-05 06:31:13 -01:00
|
|
|
xpathBuilder.Append(XPathStringsDefinition.Root);
|
2012-07-20 01:04:35 +06:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-02-05 06:31:13 -01:00
|
|
|
xpathBuilder.AppendFormat(XPathStringsDefinition.Root + XPathStrings.DescendantDocumentById, startNodeId);
|
2012-10-01 12:20:51 -02:00
|
|
|
// always "hide top level" when there's a domain
|
2012-07-20 01:04:35 +06:00
|
|
|
}
|
2012-10-05 08:00:06 -02:00
|
|
|
|
2012-07-20 01:04:35 +06:00
|
|
|
while (partsIndex < urlParts.Length)
|
2012-10-04 21:37:11 +05:00
|
|
|
{
|
2013-02-14 16:23:56 -01:00
|
|
|
var part = urlParts[partsIndex++];
|
|
|
|
|
if (part.Contains('\'') || part.Contains('"'))
|
|
|
|
|
{
|
|
|
|
|
// use vars, escaping gets ugly pretty quickly
|
|
|
|
|
varsList = varsList ?? new List<XPathVariable>();
|
|
|
|
|
var varName = string.Format("var{0}", partsIndex);
|
|
|
|
|
varsList.Add(new XPathVariable(varName, part));
|
2013-02-19 13:58:16 -01:00
|
|
|
xpathBuilder.AppendFormat(XPathStrings.ChildDocumentByUrlNameVar, varName);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
xpathBuilder.AppendFormat(XPathStrings.ChildDocumentByUrlName, part);
|
|
|
|
|
|
2013-02-14 16:23:56 -01:00
|
|
|
}
|
2012-10-04 21:37:11 +05:00
|
|
|
}
|
2012-07-20 01:04:35 +06:00
|
|
|
|
|
|
|
|
xpath = xpathBuilder.ToString();
|
2013-02-19 13:58:16 -01:00
|
|
|
if (varsList != null)
|
|
|
|
|
vars = varsList.ToArray();
|
2012-07-20 01:04:35 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return xpath;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|