From e196b4ec1353eec52057e425f68a9108660e953e Mon Sep 17 00:00:00 2001 From: hartvig Date: Thu, 7 Jul 2011 14:21:04 -0200 Subject: [PATCH] Fixes issues related to UrlResolving. Work items: 30353, 30324 and 30352 --- .../config/umbracoSettings.config | 5 ++ umbraco/businesslogic/IO/IOHelper.cs | 55 +++++++++++++------ umbraco/businesslogic/UmbracoSettings.cs | 30 +++++++++- 3 files changed, 71 insertions(+), 19 deletions(-) diff --git a/config templates/config/umbracoSettings.config b/config templates/config/umbracoSettings.config index 659355b4dd..7939ac1b70 100644 --- a/config templates/config/umbracoSettings.config +++ b/config templates/config/umbracoSettings.config @@ -68,6 +68,11 @@ 1800 + + + + + true diff --git a/umbraco/businesslogic/IO/IOHelper.cs b/umbraco/businesslogic/IO/IOHelper.cs index 45e185dbc4..1ff13ee642 100644 --- a/umbraco/businesslogic/IO/IOHelper.cs +++ b/umbraco/businesslogic/IO/IOHelper.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Text; using System.IO; @@ -14,6 +15,8 @@ namespace umbraco.IO public static class IOHelper { private static string m_rootDir = ""; + // static compiled regex for faster performance + private readonly static Regex _resolveUrlPattern = new Regex("(=[\"\']?)(\\W?\\~(?:.(?![\"\']?\\s+(?:\\S+)=|[>\"\']))+.)[\"\']?", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); public static char DirSepChar { @@ -46,31 +49,47 @@ namespace umbraco.IO return VirtualPathUtility.ToAbsolute(virtualPath, SystemDirectories.Root); } + public static string ResolveUrlsFromTextString(string text) { - // find all relative urls (ie. urls that contain ~) -// string pattern = "(\\S+)=[\"']?(\\W?\\~(?:.(?![\"']?\\s+(?:\\S+)=|[>\"']))+.)[\"']?"; - string pattern = "<.*?\\\"(\\~.*?)\\\""; - MatchCollection tags = - Regex.Matches(text, pattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); - foreach (Match tag in tags) + if (UmbracoSettings.ResolveUrlsFromTextString) { - string url = ""; - if (tag.Groups[1].Success) - url = tag.Groups[1].Value; + Stopwatch sw = new Stopwatch(); + sw.Start(); + Debug.WriteLine("Start: " + sw.ElapsedMilliseconds); - // The richtext editor inserts a slash in front of the url. That's why we need this little fix -// if (url.StartsWith("/")) -// text = text.Replace(url, ResolveUrl(url.Substring(1))); -// else - if (!String.IsNullOrEmpty(url)) + // find all relative urls (ie. urls that contain ~) + MatchCollection tags = + _resolveUrlPattern.Matches(text); + Debug.WriteLine("After regex: " + sw.ElapsedMilliseconds); + foreach (Match tag in tags) { - string resolvedUrl = (url.Substring(0,1) == "/") ? ResolveUrl(url.Substring(1)) : ResolveUrl(url); - text = text.Replace(url, resolvedUrl); + Debug.WriteLine("-- inside regex: " + sw.ElapsedMilliseconds); + string url = ""; + if (tag.Groups[1].Success) + url = tag.Groups[1].Value; + + // The richtext editor inserts a slash in front of the url. That's why we need this little fix + // if (url.StartsWith("/")) + // text = text.Replace(url, ResolveUrl(url.Substring(1))); + // else + if (!String.IsNullOrEmpty(url)) + { + Debug.WriteLine("---- before resolve: " + sw.ElapsedMilliseconds); + string resolvedUrl = (url.Substring(0, 1) == "/") ? ResolveUrl(url.Substring(1)) : ResolveUrl(url); + Debug.WriteLine("---- after resolve: " + sw.ElapsedMilliseconds); + Debug.WriteLine("---- before replace: " + sw.ElapsedMilliseconds); + text = text.Replace(url, resolvedUrl); + Debug.WriteLine("---- after replace: " + sw.ElapsedMilliseconds); + } + } - } + Debug.WriteLine("total: " + sw.ElapsedMilliseconds); + sw.Stop(); + System.Web.HttpContext.Current.Trace.Write("Resolve Urls", sw.ElapsedMilliseconds.ToString()); + } return text; } @@ -154,7 +173,7 @@ namespace umbraco.IO throw new FileSecurityException(String.Format("The extension for the current file '{0}' is not of an allowed type for this editor. This is typically controlled from either the installed MacroEngines or based on configuration in /config/umbracoSettings.config", filePath.Replace(MapPath(SystemDirectories.Root), ""))); return true; - } + } /// diff --git a/umbraco/businesslogic/UmbracoSettings.cs b/umbraco/businesslogic/UmbracoSettings.cs index 895f084d6e..d9f8d0be75 100644 --- a/umbraco/businesslogic/UmbracoSettings.cs +++ b/umbraco/businesslogic/UmbracoSettings.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.IO; using System.Web; using System.Web.Caching; @@ -13,7 +14,6 @@ namespace umbraco /// public class UmbracoSettings { - // TODO: Remove for launch public const string TEMP_FRIENDLY_XML_CHILD_CONTAINER_NODENAME = ""; // "children"; /// @@ -928,6 +928,34 @@ namespace umbraco } } + private static bool? _resolveUrlsFromTextString; + public static bool ResolveUrlsFromTextString + { + get + { + if (_resolveUrlsFromTextString == null) + { + try + { + bool enableDictionaryFallBack; + var value = GetKey("/settings/content/ResolveUrlsFromTextString"); + if (value != null) + if (bool.TryParse(value, out enableDictionaryFallBack)) + _resolveUrlsFromTextString = enableDictionaryFallBack; + } + catch (Exception ex) + { + Trace.WriteLine("Could not load /settings/content/ResolveUrlsFromTextString from umbracosettings.config:\r\n {0}", + ex.Message); + + // set url resolving to true (default (legacy) behavior) to ensure we don't keep writing to trace + _resolveUrlsFromTextString = true; + } + } + return _resolveUrlsFromTextString == true; + } + } + /// /// Configuration regarding webservices ///