diff --git a/components/editorControls/macrocontainer/MacroEditor.cs b/components/editorControls/macrocontainer/MacroEditor.cs index b5c5ffb4bd..41aaabb0be 100644 --- a/components/editorControls/macrocontainer/MacroEditor.cs +++ b/components/editorControls/macrocontainer/MacroEditor.cs @@ -63,7 +63,7 @@ namespace umbraco.editorControls.macrocontainer // with _macroSelectDropdown.Items.Add(new ListItem(GetMacroNameFromAlias(item), item)); private string GetMacroNameFromAlias(string alias) { - var macro = umbraco.macro.ReturnFromAlias(alias); + var macro = umbraco.macro.GetMacro(alias); return macro == null ? string.Empty : macro.Name; } diff --git a/components/editorControls/tinyMCE3/webcontrol/TinyMCEWebControl.cs b/components/editorControls/tinyMCE3/webcontrol/TinyMCEWebControl.cs index 325cea5963..730442344e 100644 --- a/components/editorControls/tinyMCE3/webcontrol/TinyMCEWebControl.cs +++ b/components/editorControls/tinyMCE3/webcontrol/TinyMCEWebControl.cs @@ -472,7 +472,7 @@ namespace umbraco.editorControls.tinyMCE3.webcontrol // Insert macro contents here... macro m; if (helper.FindAttribute(attributes, "macroID") != "") - m = new macro(int.Parse(helper.FindAttribute(attributes, "macroID"))); + m = macro.GetMacro(int.Parse(helper.FindAttribute(attributes, "macroID"))); else { // legacy: Check if the macroAlias is typed in lowercasing @@ -485,7 +485,7 @@ namespace umbraco.editorControls.tinyMCE3.webcontrol } if (macroAlias != "") - m = new macro(Macro.GetByAlias(macroAlias).Id); + m = macro.GetMacro(macroAlias); else throw new ArgumentException("umbraco is unable to identify the macro. No id or macroalias was provided for the macro in the macro tag.", tag.Groups[1].Value); } diff --git a/umbraco/cms/businesslogic/macro/Macro.cs b/umbraco/cms/businesslogic/macro/Macro.cs index 21c5249571..8d024a32ab 100644 --- a/umbraco/cms/businesslogic/macro/Macro.cs +++ b/umbraco/cms/businesslogic/macro/Macro.cs @@ -487,23 +487,40 @@ namespace umbraco.cms.businesslogic.macro /// /// The alias of the macro /// If the macro with the given alias exists, it returns the macro, else null - - public static Macro GetByAlias(string Alias) - { - return Cache.GetCacheItem(GetCacheKey(Alias), macroCacheSyncLock, + + public static Macro GetByAlias(string alias) + { + return Cache.GetCacheItem(GetCacheKey(alias), macroCacheSyncLock, TimeSpan.FromMinutes(30), delegate { try { - return new Macro(Alias); + return new Macro(alias); } catch { return null; } }); - } + } + + public static Macro GetById(int id) + { + return Cache.GetCacheItem(GetCacheKey(string.Format("macro_via_id_{0}", id)), macroCacheSyncLock, + TimeSpan.FromMinutes(30), + delegate + { + try + { + return new Macro(id); + } + catch + { + return null; + } + }); + } public static MacroTypes FindMacroType(string xslt, string scriptFile, string scriptType, string scriptAssembly) { diff --git a/umbraco/cms/businesslogic/macro/MacroModel.cs b/umbraco/cms/businesslogic/macro/MacroModel.cs index d5ffad7a0a..0f6dbb87c2 100644 --- a/umbraco/cms/businesslogic/macro/MacroModel.cs +++ b/umbraco/cms/businesslogic/macro/MacroModel.cs @@ -10,6 +10,7 @@ namespace umbraco.cms.businesslogic.macro [Serializable] public class MacroModel { + public int Id { get; set; } public string Name { get; set; } public string Alias { get; set; } public string MacroControlIdentifier { get; set; } @@ -25,6 +26,9 @@ namespace umbraco.cms.businesslogic.macro public int CacheDuration { get; set; } public bool CacheByPage { get; set; } public bool CacheByMember { get; set; } + + public bool RenderInEditor { get; set; } + public string CacheIdentifier { get; set; } public List Properties { get; set; } @@ -34,6 +38,32 @@ namespace umbraco.cms.businesslogic.macro Properties = new List(); } + public MacroModel(Macro m) + { + Id = m.Id; + Name = m.Name; + Alias = m.Alias; + TypeAssembly = m.Assembly; + TypeName = m.Type; + Xslt = m.Xslt; + ScriptName = m.ScriptingFile; + CacheDuration = m.RefreshRate; + CacheByPage = m.CacheByPage; + CacheByMember = m.CachePersonalized; + RenderInEditor = m.RenderContent; + + Properties = new List(); + + foreach (MacroProperty mp in m.Properties) + { + Properties.Add( + new MacroPropertyModel(mp.Alias, string.Empty, mp.Type.Alias, mp.Type.BaseType)); + } + + MacroType = Macro.FindMacroType(Xslt, ScriptName, TypeName, TypeAssembly); + + } + public MacroModel(string name, string alias, string typeAssembly, string typeName, string xslt, string scriptName, int cacheDuration, bool cacheByPage, bool cacheByMember) { Name = name; @@ -57,7 +87,8 @@ namespace umbraco.cms.businesslogic.macro { public string Key { get; set; } public string Value { get; set; } - + public string Type { get; set; } + public string CLRType { get; set; } public MacroPropertyModel() { @@ -68,6 +99,14 @@ namespace umbraco.cms.businesslogic.macro Key = key; Value = value; } + + public MacroPropertyModel(string key, string value, string type, string clrType) + { + Key = key; + Value = value; + Type = type; + CLRType = clrType; + } } public enum MacroTypes diff --git a/umbraco/presentation/UmbracoContext.cs b/umbraco/presentation/UmbracoContext.cs index a3cbf00f18..1959eb9507 100644 --- a/umbraco/presentation/UmbracoContext.cs +++ b/umbraco/presentation/UmbracoContext.cs @@ -179,6 +179,14 @@ namespace umbraco.presentation } } + public virtual TraceContext Trace + { + get + { + return this.m_HttpContext.Trace; + } + } + /// /// Gets the request for the current context /// diff --git a/umbraco/presentation/UmbracoServerUtility.cs b/umbraco/presentation/UmbracoServerUtility.cs index 3ca3befcc8..2b0b57fef2 100644 --- a/umbraco/presentation/UmbracoServerUtility.cs +++ b/umbraco/presentation/UmbracoServerUtility.cs @@ -97,5 +97,6 @@ namespace umbraco.presentation return IOHelper.ResolveUrl( SystemDirectories.Data ); } } + } } diff --git a/umbraco/presentation/macro.cs b/umbraco/presentation/macro.cs index 6f5fbbe2d6..04c968ab12 100644 --- a/umbraco/presentation/macro.cs +++ b/umbraco/presentation/macro.cs @@ -1,18 +1,15 @@ using System; using System.Collections; using System.Collections.Generic; -using System.Configuration; using System.Diagnostics; using System.IO; using System.Net; using System.Net.Security; using System.Reflection; -using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; using System.Security.Permissions; using System.Text; using System.Text.RegularExpressions; -using System.Threading; using System.Web; using System.Web.Caching; using System.Web.UI; @@ -20,6 +17,7 @@ using System.Web.UI.WebControls; using System.Xml; using System.Xml.Xsl; using umbraco.BusinessLogic; +using umbraco.BusinessLogic.Utils; using umbraco.cms.businesslogic.macro; using umbraco.cms.businesslogic.member; using umbraco.DataLayer; @@ -29,7 +27,6 @@ using umbraco.NodeFactory; using umbraco.presentation; using umbraco.presentation.templateControls; using umbraco.presentation.xslt.Exslt; -using umbraco.scripting; using Content = umbraco.cms.businesslogic.Content; using Macro = umbraco.cms.businesslogic.macro.Macro; @@ -38,14 +35,10 @@ namespace umbraco /// /// Summary description for macro. /// - public class macro : Page + public class macro { #region private properties - private static Hashtable _macroAlias = new Hashtable(); - - public IList Exceptions = new List(); - private readonly Dictionary macroObjectCache = new Dictionary(); /// Cache for . private static Dictionary m_PredefinedExtensions; @@ -55,79 +48,78 @@ namespace umbraco private readonly StringBuilder mContent = new StringBuilder(); private readonly Cache macroCache = HttpRuntime.Cache; - private readonly String macroCacheIdentifier = "umbMacro"; - private readonly int macroType; + private static readonly object macroRuntimeCacheSyncLock = new object(); + private static readonly string macroRuntimeCacheKey = "UmbracoRuntimeMacroCache"; + + private readonly string macrosAddedKey = "macrosAdded"; - private readonly Hashtable propertyDefinitions = new Hashtable(); + public IList Exceptions = new List(); // Macro-elements - private int macroID; - private Hashtable properties = new Hashtable(); - private String scriptAssembly; - private String scriptFile; - private String scriptType; - private String xsltFile; + protected static ISqlHelper SqlHelper { - get { return BusinessLogic.Application.SqlHelper; } + get { return Application.SqlHelper; } } #endregion #region public properties - public int MacroID + public bool CacheByPersonalization { - set { macroID = value; } - get { return macroID; } + get { return Model.CacheByMember; } } - public bool CacheByPersonalization { set; get; } + public bool CacheByPage + { + get { return Model.CacheByPage; } + } - public bool CacheByPage { set; get; } + public bool DontRenderInEditor + { + get { return !Model.RenderInEditor; } + } - public bool DontRenderInEditor { get; set; } + public int RefreshRate + { + get { return Model.CacheDuration; } + } - public int RefreshRate { set; get; } + public String Alias + { + get { return Model.Alias; } + } - public String Alias { set; get; } - - public String Name { set; get; } + public String Name + { + get { return Model.Name; } + } public String XsltFile { - set { xsltFile = value; } - get { return xsltFile; } + get { return Model.Xslt; } } public String ScriptFile { - set { scriptFile = value; } - get { return scriptFile; } + get { return Model.ScriptName; } } public String ScriptType { - set { scriptType = value; } - get { return scriptType; } + get { return Model.TypeName; } } public String ScriptAssembly { - set { scriptAssembly = value; } - get { return scriptAssembly; } - } - - public Hashtable Properties - { - get { return properties; } - set { properties = value; } + get { return Model.TypeName; } } public int MacroType { - get { return macroType; } + get { return (int) Model.MacroType; } } public String MacroContent @@ -138,12 +130,7 @@ namespace umbraco #endregion - /// - /// Creates an empty macro object. - /// - public macro() - { - } + #region REFACTOR /// /// Creates a macro object @@ -151,184 +138,96 @@ namespace umbraco /// Specify the macro-id which should be loaded (from table macro) public macro(int id) { - macroID = id; - - if (macroObjectCache.ContainsKey(macroCacheIdentifier + id)) - { - var tempMacro = macroObjectCache[macroCacheIdentifier + id]; - Name = tempMacro.Name; - Alias = tempMacro.Alias; - ScriptType = tempMacro.ScriptType; - ScriptAssembly = tempMacro.ScriptAssembly; - XsltFile = tempMacro.XsltFile; - scriptFile = tempMacro.ScriptFile; - Properties = tempMacro.Properties; - propertyDefinitions = tempMacro.propertyDefinitions; - RefreshRate = tempMacro.RefreshRate; - CacheByPage = tempMacro.CacheByPage; - CacheByPersonalization = tempMacro.CacheByPersonalization; - DontRenderInEditor = tempMacro.DontRenderInEditor; - - HttpContext.Current.Trace.Write("umbracoMacro", - string.Format("Macro loaded from cache (ID: {0}, {1})", id, Name)); - } - else - { - using ( - IRecordsReader macroDef = - SqlHelper.ExecuteReader( - "select * from cmsMacro left join cmsMacroProperty property on property.macro = cmsMacro.id left join cmsMacroPropertyType editPropertyType on editPropertyType.id = property.macroPropertyType where cmsMacro.id = @macroID order by property.macroPropertySortOrder", - SqlHelper.CreateParameter("@macroID", id))) - { - bool hasRows = macroDef.Read(); - if (!hasRows) - HttpContext.Current.Trace.Warn("Macro", "No definition found for id " + id); - - while (hasRows) - { - string tmpStr; - bool tmpBool; - int tmpInt; - - if (TryGetColumnBool(macroDef, "macroCacheByPage", out tmpBool)) - CacheByPage = tmpBool; - if (TryGetColumnBool(macroDef, "macroCachePersonalized", out tmpBool)) - CacheByPersonalization = tmpBool; - if (TryGetColumnBool(macroDef, "macroDontRender", out tmpBool)) - DontRenderInEditor = tmpBool; - if (TryGetColumnInt32(macroDef, "macroRefreshRate", out tmpInt)) - RefreshRate = tmpInt; - if (TryGetColumnInt32(macroDef, "macroRefreshRate", out tmpInt)) - RefreshRate = tmpInt; - if (TryGetColumnString(macroDef, "macroName", out tmpStr)) - Name = tmpStr; - if (TryGetColumnString(macroDef, "macroAlias", out tmpStr)) - Alias = tmpStr; - if (TryGetColumnString(macroDef, "macroScriptType", out tmpStr)) - ScriptType = tmpStr; - if (TryGetColumnString(macroDef, "macroScriptAssembly", out tmpStr)) - ScriptAssembly = tmpStr; - if (TryGetColumnString(macroDef, "macroXSLT", out tmpStr)) - XsltFile = tmpStr; - - if (TryGetColumnString(macroDef, "macroPython", out tmpStr)) - ScriptFile = tmpStr; - - if (TryGetColumnString(macroDef, "macroPropertyAlias", out tmpStr)) - { - string typeAlias; - - if (TryGetColumnString(macroDef, "macroPropertyTypeAlias", out typeAlias) && - !properties.ContainsKey(tmpStr)) - properties.Add(tmpStr, typeAlias); - - string baseType; - if (TryGetColumnString(macroDef, "macroPropertyTypeBaseType", out baseType) && - !propertyDefinitions.ContainsKey(tmpStr)) - propertyDefinitions.Add(tmpStr, baseType); - } - hasRows = macroDef.Read(); - } - } - // add current macro-object to cache - if (!macroObjectCache.ContainsKey(macroCacheIdentifier + id)) - macroObjectCache.Add(macroCacheIdentifier + id, this); - } - - macroType = (int)Macro.FindMacroType(XsltFile, ScriptFile, ScriptType, ScriptAssembly); + Macro m = Macro.GetById(id); + Model = new MacroModel(m); } + public macro(string alias) + { + Macro m = Macro.GetByAlias(alias); + Model = new MacroModel(m); + } + + public MacroModel Model { get; set; } + + public static macro GetMacro(string alias) + { + + return cms.businesslogic.cache.Cache.GetCacheItem(GetCacheKey(alias), macroRuntimeCacheSyncLock, + TimeSpan.FromMinutes(60), + delegate + { + try + { + return new macro(alias); + } + catch + { + return null; + } + }); + } + + public static macro GetMacro(int id) + { + return cms.businesslogic.cache.Cache.GetCacheItem(GetCacheKey(string.Format("by_id_{0}", id)), macroRuntimeCacheSyncLock, + TimeSpan.FromMinutes(60), + delegate + { + try + { + return new macro(id); + } + catch + { + return null; + } + }); + } + + #endregion + + private const string _xsltExtensionsCacheKey = "UmbracoXsltExtensions"; + + private static readonly string _xsltExtensionsConfig = + IOHelper.MapPath(SystemDirectories.Config + "/xsltExtensions.config"); + + private static readonly object _xsltExtensionsSyncLock = new object(); + + private static readonly Lazy _xsltExtensionsDependency = + new Lazy(() => new CacheDependency(_xsltExtensionsConfig)); + + /// + /// Creates an empty macro object. + /// + public macro() + { + Model = new MacroModel(); + } + + public override string ToString() { - return Name; + return Model.Name; } - public static macro ReturnFromAlias(string alias) + private static string GetCacheKey(string alias) { - if (_macroAlias.ContainsKey(alias)) - return new macro((int)_macroAlias[alias]); - else - { - try - { - int macroID = Macro.GetByAlias(alias).Id; - _macroAlias.Add(alias, macroID); - return new macro(macroID); - } - catch - { - HttpContext.Current.Trace.Warn("macro", "No macro with alias '" + alias + "' found"); - return null; - } - } - } - - public static bool TryGetColumnString(IRecordsReader reader, string columnName, out string value) - { - if (reader.ContainsField(columnName) && !reader.IsNull(columnName)) - { - value = reader.GetString(columnName); - return true; - } - else - { - value = string.Empty; - return false; - } - } - - public static bool TryGetColumnInt32(IRecordsReader reader, string columnName, out int value) - { - if (reader.ContainsField(columnName) && !reader.IsNull(columnName)) - { - value = reader.GetInt(columnName); - return true; - } - else - { - value = -1; - return false; - } - } - - public static bool TryGetColumnBool(IRecordsReader reader, string columnName, out bool value) - { - if (reader.ContainsField(columnName) && !reader.IsNull(columnName)) - { - value = reader.GetBoolean(columnName); - return true; - } - else - { - value = false; - return false; - } - } - - public static void ClearAliasCache() - { - _macroAlias = new Hashtable(); + return macroRuntimeCacheKey + alias; } /// /// Deletes macro definition from cache. /// /// True if succesfull, false if nothing has been removed + //TODO: Update implementation! public bool removeFromCache() { - ClearAliasCache(); - if (macroID > 0) + if (Model.Id > 0) { - if (macroObjectCache.ContainsKey(macroCacheIdentifier + macroID)) - { - macroObjectCache.Remove(macroCacheIdentifier + macroID); - return true; - } - return false; - + cms.businesslogic.cache.Cache.ClearCacheItem(GetCacheKey(Model.Alias)); } return false; - } private string getCacheGuid(MacroModel model, Hashtable pageElements, int pageId) @@ -362,15 +261,17 @@ namespace umbraco public Control renderMacro(Hashtable attributes, Hashtable pageElements, int pageId) { - MacroModel m = ConvertToMacroModel(attributes); - return renderMacro(m, pageElements, pageId); + // TODO: Parse attributes + UpdateMacroModel(attributes); + return renderMacro(pageElements, pageId); } - public Control renderMacro(MacroModel model, Hashtable pageElements, int pageId) + public Control renderMacro(Hashtable pageElements, int pageId) { - HttpContext.Current.Trace.Write("renderMacro", - string.Format("Rendering started (macro: {0}, type: {1}, cacheRate: {2})", - Name, MacroType, model.CacheDuration)); + UmbracoContext.Current.Trace.Write("renderMacro", + string.Format( + "Rendering started (macro: {0}, type: {1}, cacheRate: {2})", + Name, MacroType, Model.CacheDuration)); StateHelper.SetContextValue(macrosAddedKey, StateHelper.GetContextValue(macrosAddedKey) + 1); @@ -378,88 +279,95 @@ namespace umbraco Control macroControl = null; // zb-00037 #29875 : parse attributes here (and before anything else) - foreach (var prop in model.Properties) + foreach (MacroPropertyModel prop in Model.Properties) prop.Value = helper.parseAttribute(pageElements, prop.Value); - model.CacheIdentifier = getCacheGuid(model, pageElements, pageId); + Model.CacheIdentifier = getCacheGuid(Model, pageElements, pageId); - if (model.CacheDuration > 0) + if (Model.CacheDuration > 0) { - if (cacheMacroAsString(model)) + if (cacheMacroAsString(Model)) { - macroHtml = macroCache["macroHtml_" + model.CacheIdentifier] as String; + macroHtml = macroCache["macroHtml_" + Model.CacheIdentifier] as String; if (!String.IsNullOrEmpty(macroHtml)) { - HttpContext.Current.Trace.Write("renderMacro", "Macro Content loaded from cache ('" + model.CacheIdentifier + "')..."); + UmbracoContext.Current.Trace.Write("renderMacro", + "Macro Content loaded from cache ('" + Model.CacheIdentifier + + "')..."); } } else { - object macroCacheContent = macroCache["macroControl_" + model.CacheIdentifier]; + object macroCacheContent = macroCache["macroControl_" + Model.CacheIdentifier]; if (macroCacheContent != null) { - MacroCacheContent cacheContent = (MacroCacheContent) macroCacheContent; + var cacheContent = (MacroCacheContent) macroCacheContent; macroControl = cacheContent.Content; macroControl.ID = cacheContent.ID; - HttpContext.Current.Trace.Write("renderMacro", "Macro Control loaded from cache ('" + model.CacheIdentifier + "')..."); + UmbracoContext.Current.Trace.Write("renderMacro", + "Macro Control loaded from cache ('" + Model.CacheIdentifier + + "')..."); } - } } if (String.IsNullOrEmpty(macroHtml) && macroControl == null) { bool renderFailed = false; - int macroType = model.MacroType != MacroTypes.Unknown ? (int)model.MacroType : MacroType; + int macroType = Model.MacroType != MacroTypes.Unknown ? (int) Model.MacroType : MacroType; switch (macroType) { - case (int)MacroTypes.UserControl: + case (int) MacroTypes.UserControl: try { - HttpContext.Current.Trace.Write("umbracoMacro", "Usercontrol added (" + scriptType + ")"); - macroControl = loadUserControl(ScriptType, model, pageElements); + UmbracoContext.Current.Trace.Write("umbracoMacro", + "Usercontrol added (" + Model.TypeName + ")"); + macroControl = loadUserControl(ScriptType, Model, pageElements); break; } catch (Exception e) { renderFailed = true; Exceptions.Add(e); - HttpContext.Current.Trace.Warn("umbracoMacro", - "Error loading userControl (" + scriptType + ")", e); - macroControl = new LiteralControl("Error loading userControl '" + scriptType + "'"); + UmbracoContext.Current.Trace.Warn("umbracoMacro", + "Error loading userControl (" + Model.TypeName + ")", e); + macroControl = new LiteralControl("Error loading userControl '" + Model.TypeName + "'"); break; } - case (int)MacroTypes.CustomControl: + case (int) MacroTypes.CustomControl: try { - HttpContext.Current.Trace.Write("umbracoMacro", "Custom control added (" + scriptType + ")"); - HttpContext.Current.Trace.Write("umbracoMacro", "ScriptAssembly (" + scriptAssembly + ")"); - macroControl = loadControl(scriptAssembly, ScriptType, model, pageElements); + UmbracoContext.Current.Trace.Write("umbracoMacro", + "Custom control added (" + Model.TypeName + ")"); + UmbracoContext.Current.Trace.Write("umbracoMacro", + "ScriptAssembly (" + Model.TypeAssembly + ")"); + macroControl = loadControl(Model.TypeAssembly, ScriptType, Model, pageElements); break; } catch (Exception e) { renderFailed = true; Exceptions.Add(e); - HttpContext.Current.Trace.Warn("umbracoMacro", - "Error loading customControl (Assembly: " + scriptAssembly + - ", Type: '" + scriptType + "'", e); + UmbracoContext.Current.Trace.Warn("umbracoMacro", + "Error loading customControl (Assembly: " + + Model.TypeAssembly + + ", Type: '" + Model.TypeName + "'", e); macroControl = - new LiteralControl("Error loading customControl (Assembly: " + scriptAssembly + + new LiteralControl("Error loading customControl (Assembly: " + Model.TypeAssembly + ", Type: '" + - scriptType + "'"); + Model.TypeName + "'"); break; } - case (int)MacroTypes.XSLT: - macroControl = loadMacroXSLT(this, model, pageElements); + case (int) MacroTypes.XSLT: + macroControl = loadMacroXSLT(this, Model, pageElements); break; - case (int)MacroTypes.Script: + case (int) MacroTypes.Script: try { - HttpContext.Current.Trace.Write("umbracoMacro", - "MacroEngine script added (" + ScriptFile + ")"); - DLRMacroResult result = loadMacroDLR(model); + UmbracoContext.Current.Trace.Write("umbracoMacro", + "MacroEngine script added (" + ScriptFile + ")"); + DLRMacroResult result = loadMacroDLR(Model); macroControl = result.Control; if (result.ResultException != null) { @@ -467,7 +375,6 @@ namespace umbraco renderFailed = true; if (HttpContext.Current != null && !HttpContext.Current.IsDebuggingEnabled) throw result.ResultException; - } break; } @@ -475,11 +382,12 @@ namespace umbraco { renderFailed = true; Exceptions.Add(e); - HttpContext.Current.Trace.Warn("umbracoMacro", - "Error loading MacroEngine script (file: " + ScriptFile + - ", Type: '" + scriptType + "'", e); + UmbracoContext.Current.Trace.Warn("umbracoMacro", + "Error loading MacroEngine script (file: " + ScriptFile + + ", Type: '" + Model.TypeName + "'", e); - var result = new LiteralControl("Error loading MacroEngine script (file: " + ScriptFile + ")"); + var result = + new LiteralControl("Error loading MacroEngine script (file: " + ScriptFile + ")"); /* string args = "
    "; @@ -507,25 +415,25 @@ namespace umbraco } // Add result to cache if successful - if (!renderFailed && model.CacheDuration > 0) + if (!renderFailed && Model.CacheDuration > 0) { // do not add to cache if there's no member and it should cache by personalization - if (!model.CacheByMember || (model.CacheByMember && Member.GetCurrentMember() != null)) + if (!Model.CacheByMember || (Model.CacheByMember && Member.GetCurrentMember() != null)) { if (macroControl != null) { // NH: Scripts and XSLT can be generated as strings, but not controls as page events wouldn't be hit (such as Page_Load, etc) - if (cacheMacroAsString(model)) + if (cacheMacroAsString(Model)) { using (var sw = new StringWriter()) { var hw = new HtmlTextWriter(sw); macroControl.RenderControl(hw); - macroCache.Insert("macroHtml_" + model.CacheIdentifier, + macroCache.Insert("macroHtml_" + Model.CacheIdentifier, sw.ToString(), null, - DateTime.Now.AddSeconds(model.CacheDuration), + DateTime.Now.AddSeconds(Model.CacheDuration), TimeSpan.Zero, CacheItemPriority.Low, null); @@ -538,10 +446,11 @@ namespace umbraco } else { - macroCache.Insert("macroControl_" + model.CacheIdentifier, new MacroCacheContent(macroControl, macroControl.ID), null, - DateTime.Now.AddSeconds(model.CacheDuration), TimeSpan.Zero, CacheItemPriority.Low, - null); - + macroCache.Insert("macroControl_" + Model.CacheIdentifier, + new MacroCacheContent(macroControl, macroControl.ID), null, + DateTime.Now.AddSeconds(Model.CacheDuration), TimeSpan.Zero, + CacheItemPriority.Low, + null); } } } @@ -564,7 +473,7 @@ namespace umbraco { if (HttpRuntime.Cache["macroXslt_" + XsltFile] != null) { - return (XslCompiledTransform)HttpRuntime.Cache["macroXslt_" + XsltFile]; + return (XslCompiledTransform) HttpRuntime.Cache["macroXslt_" + XsltFile]; } else { @@ -580,26 +489,21 @@ namespace umbraco } } - public MacroModel ConvertToMacroModel(Hashtable attributes) + public void UpdateMacroModel(Hashtable attributes) { - MacroModel model = new MacroModel( - this.Name, - this.Alias, - this.ScriptAssembly, - this.ScriptType, - this.XsltFile, - this.ScriptFile, - this.RefreshRate, - this.CacheByPage, - this.CacheByPersonalization - ); + foreach (MacroPropertyModel mp in Model.Properties) + { + if (attributes.ContainsKey(mp.Key.ToLower())) + mp.Value = attributes[mp.Key.ToLower()].ToString(); + } + } + public void GenerateMacroModelPropertiesFromAttributes(Hashtable attributes) + { foreach (string key in attributes.Keys) { - model.Properties.Add(new MacroPropertyModel(key, attributes[key].ToString())); + Model.Properties.Add(new MacroPropertyModel(key, attributes[key].ToString())); } - - return model; } @@ -656,20 +560,10 @@ namespace umbraco var macroXML = new XmlDocument(); macroXML.LoadXml(""); - foreach (DictionaryEntry macroDef in macro.properties) + foreach (MacroPropertyModel prop in macro.Model.Properties) { - var prop = model.Properties.Find(m => m.Key == (string)macroDef.Key.ToString().ToLower()); - // zb-00037 #29875 : values have already been parsed + no need to parse "" - string propValue = prop != null ? prop.Value : ""; - if (!String.IsNullOrEmpty(propValue)) - { - if (propValue != string.Empty) - addMacroXmlNode(umbracoXML, macroXML, macroDef.Key.ToString(), macroDef.Value.ToString(), - propValue); - else - addMacroXmlNode(umbracoXML, macroXML, macroDef.Key.ToString(), macroDef.Value.ToString(), - string.Empty); - } + addMacroXmlNode(umbracoXML, macroXML, prop.Key, prop.Type, + prop.Value); } if (HttpContext.Current.Request.QueryString["umbDebug"] != null && GlobalSettings.DebugMode) @@ -677,7 +571,8 @@ namespace umbraco return new LiteralControl("
    Debug from " + macro.Name + - "

    " + Page.Server.HtmlEncode(macroXML.OuterXml) + "

    "); + "

    " + UmbracoContext.Current.Server.HtmlEncode(macroXML.OuterXml) + + "

    "); } else { @@ -689,7 +584,7 @@ namespace umbraco { Control result = CreateControlsFromText(GetXsltTransformResult(macroXML, xsltFile)); - HttpContext.Current.Trace.Write("umbracoMacro", "After performing transformation"); + UmbracoContext.Current.Trace.Write("umbracoMacro", "After performing transformation"); return result; } @@ -701,7 +596,7 @@ namespace umbraco Exception ie = e; while (ie != null) { - HttpContext.Current.Trace.Warn("umbracoMacro InnerException", ie.Message, ie); + UmbracoContext.Current.Trace.Warn("umbracoMacro InnerException", ie.Message, ie); ie = ie.InnerException; } return new LiteralControl("Error parsing XSLT file: \\xslt\\" + XsltFile); @@ -710,14 +605,14 @@ namespace umbraco catch (Exception e) { Exceptions.Add(e); - HttpContext.Current.Trace.Warn("umbracoMacro", "Error loading XSLT " + xsltFile, e); + UmbracoContext.Current.Trace.Warn("umbracoMacro", "Error loading XSLT " + Model.Xslt, e); return new LiteralControl("Error reading XSLT file: \\xslt\\" + XsltFile); } } } else { - Page.Trace.Warn("macro", "Xslt is empty"); + UmbracoContext.Current.Trace.Warn("macro", "Xslt is empty"); return new LiteralControl(string.Empty); } } @@ -786,12 +681,12 @@ namespace umbraco { TextWriter tw = new StringWriter(); - HttpContext.Current.Trace.Write("umbracoMacro", "Before adding extensions"); + UmbracoContext.Current.Trace.Write("umbracoMacro", "Before adding extensions"); XsltArgumentList xslArgs; xslArgs = AddXsltExtensions(); var lib = new library(); xslArgs.AddExtensionObject("urn:umbraco.library", lib); - HttpContext.Current.Trace.Write("umbracoMacro", "After adding extensions"); + UmbracoContext.Current.Trace.Write("umbracoMacro", "After adding extensions"); // Add parameters if (parameters == null || !parameters.ContainsKey("currentPage")) @@ -805,7 +700,7 @@ namespace umbraco } // Do transformation - HttpContext.Current.Trace.Write("umbracoMacro", "Before performing transformation"); + UmbracoContext.Current.Trace.Write("umbracoMacro", "Before performing transformation"); xslt.Transform(macroXML.CreateNavigator(), xslArgs, tw); return IOHelper.ResolveUrlsFromTextString(tw.ToString()); } @@ -827,21 +722,19 @@ namespace umbraco // would not be refreshed when the .config file is modified. An application // restart would be required. Better use the cache and add a dependency. - return umbraco.cms.businesslogic.cache.Cache.GetCacheItem( + return cms.businesslogic.cache.Cache.GetCacheItem( _xsltExtensionsCacheKey, _xsltExtensionsSyncLock, CacheItemPriority.NotRemovable, // NH 4.7.1, Changing to NotRemovable null, // no refresh action - new CacheDependency(_xsltExtensionsConfig), // depends on the .config file + _xsltExtensionsDependency.Value, // depends on the .config file TimeSpan.FromDays(1), // expires in 1 day (?) () => { return GetXsltExtensionsImpl(); }); } // zb-00041 #29966 : cache the extensions - const string _xsltExtensionsCacheKey = "UmbracoXsltExtensions"; - static string _xsltExtensionsConfig = IOHelper.MapPath(SystemDirectories.Config + "/xsltExtensions.config"); - static object _xsltExtensionsSyncLock = new object(); - static Dictionary GetXsltExtensionsImpl() + + private static Dictionary GetXsltExtensionsImpl() { // fill a dictionary with the predefined extensions var extensions = new Dictionary(GetPredefinedXsltExtensions()); @@ -893,9 +786,9 @@ namespace umbraco //also get types marked with XsltExtension attribute // zb-00042 #29949 : do not hide errors, refactor - foreach (Type xsltType in BusinessLogic.Utils.TypeFinder.FindClassesMarkedWithAttribute(typeof(XsltExtensionAttribute))) + foreach (Type xsltType in TypeFinder.FindClassesMarkedWithAttribute(typeof (XsltExtensionAttribute))) { - object[] tpAttributes = xsltType.GetCustomAttributes(typeof(XsltExtensionAttribute), true); + object[] tpAttributes = xsltType.GetCustomAttributes(typeof (XsltExtensionAttribute), true); foreach (XsltExtensionAttribute tpAttribute in tpAttributes) { string ns = !string.IsNullOrEmpty(tpAttribute.Namespace) ? tpAttribute.Namespace : xsltType.FullName; @@ -945,9 +838,9 @@ namespace umbraco { string extensionNamespace = "urn:" + extension.Key; xslArgs.AddExtensionObject(extensionNamespace, extension.Value); - HttpContext.Current.Trace.Write("umbracoXsltExtension", - String.Format("Extension added: {0}, {1}", - extensionNamespace, extension.Value.GetType().Name)); + UmbracoContext.Current.Trace.Write("umbracoXsltExtension", + String.Format("Extension added: {0}, {1}", + extensionNamespace, extension.Value.GetType().Name)); } return xslArgs; @@ -963,15 +856,15 @@ namespace umbraco // If no value is passed, then use the current pageID as value if (macroPropertyValue == string.Empty) { - var umbPage = (page)HttpContext.Current.Items["umbPageObject"]; + var umbPage = (page) HttpContext.Current.Items["umbPageObject"]; if (umbPage == null) return; currentID = umbPage.PageID; } - HttpContext.Current.Trace.Write("umbracoMacro", - "Xslt node adding search start (" + macroPropertyAlias + ",'" + - macroPropertyValue + "')"); + UmbracoContext.Current.Trace.Write("umbracoMacro", + "Xslt node adding search start (" + macroPropertyAlias + ",'" + + macroPropertyValue + "')"); switch (macroPropertyType) { case "contentTree": @@ -1040,14 +933,14 @@ namespace umbraco break; } else - HttpContext.Current.Trace.Warn("umbracoMacro", - "Error adding random node - parent (" + macroPropertyValue + - ") doesn't have children!"); + UmbracoContext.Current.Trace.Warn("umbracoMacro", + "Error adding random node - parent (" + macroPropertyValue + + ") doesn't have children!"); } else - HttpContext.Current.Trace.Warn("umbracoMacro", - "Error adding random node - parent (" + macroPropertyValue + - ") doesn't exists!"); + UmbracoContext.Current.Trace.Warn("umbracoMacro", + "Error adding random node - parent (" + macroPropertyValue + + ") doesn't exists!"); break; case "mediaCurrent": var c = new Content(int.Parse(macroPropertyValue)); @@ -1078,7 +971,7 @@ namespace umbraco public DLRMacroResult loadMacroDLR(MacroModel macro) { - DLRMacroResult retVal = new DLRMacroResult(); + var retVal = new DLRMacroResult(); TraceInfo("umbracoMacro", "Loading IMacroEngine script"); var ret = new LiteralControl(); IMacroEngine engine = null; @@ -1143,7 +1036,7 @@ namespace umbraco asm = Assembly.LoadFrom(currentAss); if (HttpContext.Current != null) - HttpContext.Current.Trace.Write("umbracoMacro", "Assembly file " + currentAss + " LOADED!!"); + UmbracoContext.Current.Trace.Write("umbracoMacro", "Assembly file " + currentAss + " LOADED!!"); } catch { @@ -1153,8 +1046,8 @@ namespace umbraco } if (HttpContext.Current != null) - HttpContext.Current.Trace.Write("umbracoMacro", - string.Format("Assembly Loaded from ({0}.dll)", fileName)); + UmbracoContext.Current.Trace.Write("umbracoMacro", + string.Format("Assembly Loaded from ({0}.dll)", fileName)); type = asm.GetType(controlName); if (type == null) return new LiteralControl(string.Format("Unable to get type {0} from assembly {1}", @@ -1168,58 +1061,73 @@ namespace umbraco AddCurrentNodeToControl(control, type); // Properties - foreach (string propertyAlias in properties.Keys) + updateControlProperties(type, control, model); + return control; + } + + private void updateControlProperties(Type type, Control control, MacroModel model) + { + foreach (MacroPropertyModel mp in model.Properties) { - PropertyInfo prop = type.GetProperty(propertyAlias); + PropertyInfo prop = type.GetProperty(mp.Key); if (prop == null) { if (HttpContext.Current != null) - HttpContext.Current.Trace.Warn("macro", - string.Format("control property '{0}' doesn't exist or aren't accessible (public)", - propertyAlias)); + UmbracoContext.Current.Trace.Warn("macro", + string.Format( + "control property '{0}' doesn't exist or aren't accessible (public)", + mp.Key)); continue; } - MacroPropertyModel propModel = model.Properties.Find(m => m.Key == propertyAlias.ToLower()); - // zb-00037 #29875 : values have already been parsed + no need to parse "" - object propValue = propModel != null && prop != null ? propModel.Value : ""; + object propValue = mp.Value; // Special case for types of webControls.unit - if (prop.PropertyType == typeof(Unit)) + if (prop.PropertyType == typeof (Unit)) propValue = Unit.Parse(propValue.ToString()); else { - foreach (object s in propertyDefinitions) + try { - if (s != null) - Trace.Write("macroProp", s.ToString()); - } + if (mp.CLRType == null) + continue; + var st = (TypeCode) Enum.Parse(typeof (TypeCode), mp.CLRType, true); - Trace.Warn("macro", propertyAlias); - - object o = propertyDefinitions[propertyAlias]; - if (o == null) - continue; - var st = (TypeCode)Enum.Parse(typeof(TypeCode), o.ToString(), true); - - // Special case for booleans - if (prop.PropertyType == typeof(bool)) - { - bool parseResult; - if ( - Boolean.TryParse(propValue.ToString().Replace("1", "true").Replace("0", "false"), - out parseResult)) - propValue = parseResult; + // Special case for booleans + if (prop.PropertyType == typeof (bool)) + { + bool parseResult; + if ( + Boolean.TryParse( + propValue.ToString().Replace("1", "true").Replace("0", "false"), + out parseResult)) + propValue = parseResult; + else + propValue = false; + } else - propValue = false; + propValue = Convert.ChangeType(propValue, st); + + if (GlobalSettings.DebugMode) + UmbracoContext.Current.Trace.Write("macro.loadControlProperties", + string.Format("Property added '{0}' with value '{1}'", + mp.Key, + propValue)); + } + catch (Exception PropException) + { + Log.Instance.AddException(PropException); + if (GlobalSettings.DebugMode) + + UmbracoContext.Current.Trace.Warn("macro.loadControlProperties", + string.Format( + "Error adding property '{0}' with value '{1}'", + mp.Key, propValue), PropException); } - else - propValue = Convert.ChangeType(propValue, st); } prop.SetValue(control, Convert.ChangeType(propValue, prop.PropertyType), null); } - return control; } /// @@ -1241,7 +1149,7 @@ namespace umbraco if (!File.Exists(IOHelper.MapPath(userControlPath))) return new LiteralControl(string.Format("UserControl {0} does not exist.", fileName)); - var oControl = (UserControl)new UserControl().LoadControl(userControlPath); + var oControl = (UserControl) new UserControl().LoadControl(userControlPath); int slashIndex = fileName.LastIndexOf("/") + 1; if (slashIndex < 0) @@ -1264,80 +1172,13 @@ namespace umbraco } AddCurrentNodeToControl(oControl, type); - - foreach (string propertyAlias in properties.Keys) - { - PropertyInfo prop = type.GetProperty(propertyAlias); - if (prop == null) - { - TraceWarn(loadUserControlKey, "Unable to retrieve type from propertyAlias: " + propertyAlias); - continue; - } - - MacroPropertyModel propModel = model.Properties.Find(m => m.Key == propertyAlias.ToLower()); - // zb-00037 #29875 : values have already been parsed + no need to parse "" - object propValue = propModel != null && prop != null ? propModel.Value : null; - - if (propValue == null) - continue; - - // Special case for types of webControls.unit - try - { - if (prop.PropertyType == typeof(Unit)) - propValue = Unit.Parse(propValue.ToString()); - else - { - try - { - object o = propertyDefinitions[propertyAlias]; - if (o == null) - continue; - var st = (TypeCode)Enum.Parse(typeof(TypeCode), o.ToString(), true); - - // Special case for booleans - if (prop.PropertyType == typeof(bool)) - { - bool parseResult; - if ( - Boolean.TryParse( - propValue.ToString().Replace("1", "true").Replace("0", "false"), - out parseResult)) - propValue = parseResult; - else - propValue = false; - } - else - propValue = Convert.ChangeType(propValue, st); - - Trace.Write("macro.loadControlProperties", - string.Format("Property added '{0}' with value '{1}'", propertyAlias, - propValue)); - } - catch (Exception PropException) - { - HttpContext.Current.Trace.Warn("macro.loadControlProperties", - string.Format( - "Error adding property '{0}' with value '{1}'", - propertyAlias, propValue), PropException); - } - } - - prop.SetValue(oControl, Convert.ChangeType(propValue, prop.PropertyType), null); - } - catch (Exception propException) - { - HttpContext.Current.Trace.Warn("macro.loadControlProperties", - string.Format( - "Error adding property '{0}' with value '{1}', maybe it doesn't exists or maybe casing is wrong!", - propertyAlias, propValue), propException); - } - } + updateControlProperties(type, oControl, model); return oControl; } catch (Exception e) { - HttpContext.Current.Trace.Warn("macro", string.Format("Error creating usercontrol ({0})", fileName), e); + UmbracoContext.Current.Trace.Warn("macro", string.Format("Error creating usercontrol ({0})", fileName), + e); return new LiteralControl( string.Format( "
    Error creating control ({0}).
    Maybe file doesn't exists or the usercontrol has a cache directive, which is not allowed! See the tracestack for more information!
    ", @@ -1347,13 +1188,15 @@ namespace umbraco private static void AddCurrentNodeToControl(Control control, Type type) { - var currentNodeProperty = type.GetProperty("CurrentNode"); - if (currentNodeProperty != null && currentNodeProperty.CanWrite && currentNodeProperty.PropertyType.IsAssignableFrom(typeof(Node))) + PropertyInfo currentNodeProperty = type.GetProperty("CurrentNode"); + if (currentNodeProperty != null && currentNodeProperty.CanWrite && + currentNodeProperty.PropertyType.IsAssignableFrom(typeof (Node))) { currentNodeProperty.SetValue(control, Node.GetCurrent(), null); } currentNodeProperty = type.GetProperty("currentNode"); - if (currentNodeProperty != null && currentNodeProperty.CanWrite && currentNodeProperty.PropertyType.IsAssignableFrom(typeof(Node))) + if (currentNodeProperty != null && currentNodeProperty.CanWrite && + currentNodeProperty.PropertyType.IsAssignableFrom(typeof (Node))) { currentNodeProperty.SetValue(control, Node.GetCurrent(), null); } @@ -1362,30 +1205,13 @@ namespace umbraco private void TraceInfo(string category, string message) { if (HttpContext.Current != null) - HttpContext.Current.Trace.Write(category, message); + UmbracoContext.Current.Trace.Write(category, message); } private void TraceWarn(string category, string message) { if (HttpContext.Current != null) - HttpContext.Current.Trace.Warn(category, message); - } - - /// - /// For debug purposes only - should be deleted or made private - /// - /// The type of object (control) to show properties from - public void macroProperties(Type type) - { - PropertyInfo[] myProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); - HttpContext.Current.Response.Write("

    " + type.Name + "
    "); - foreach (PropertyInfo propertyItem in myProperties) - { - // if (propertyItem.CanWrite) - HttpContext.Current.Response.Write(propertyItem.Name + " (" + propertyItem.PropertyType + - ")
    "); - } - HttpContext.Current.Response.Write("

    "); + UmbracoContext.Current.Trace.Warn(category, message); } public static string renderMacroStartTag(Hashtable attributes, int pageId, Guid versionId) @@ -1430,7 +1256,7 @@ namespace umbraco public static string GetRenderedMacro(int MacroId, page umbPage, Hashtable attributes, int pageId) { - var m = new macro(MacroId); + macro m = GetMacro(MacroId); Control c = m.renderMacro(attributes, umbPage.Elements, pageId); TextWriter writer = new StringWriter(); var ht = new HtmlTextWriter(writer); @@ -1452,7 +1278,7 @@ namespace umbraco string tempAlias = (attributes["macroalias"] != null) ? attributes["macroalias"].ToString() : attributes["macroAlias"].ToString(); - macro currentMacro = ReturnFromAlias(tempAlias); + macro currentMacro = GetMacro(tempAlias); if (!currentMacro.DontRenderInEditor) { string querystring = "umbPageId=" + PageID + "&umbVersionId=" + PageVersion; @@ -1463,9 +1289,12 @@ namespace umbraco // Create a new 'HttpWebRequest' Object to the mentioned URL. string retVal = string.Empty; string protocol = GlobalSettings.UseSSL ? "https" : "http"; - string url = string.Format("{0}://{1}:{2}{3}/macroResultWrapper.aspx?{4}", protocol, HttpContext.Current.Request.ServerVariables["SERVER_NAME"], HttpContext.Current.Request.ServerVariables["SERVER_PORT"], IOHelper.ResolveUrl(SystemDirectories.Umbraco), querystring); + string url = string.Format("{0}://{1}:{2}{3}/macroResultWrapper.aspx?{4}", protocol, + HttpContext.Current.Request.ServerVariables["SERVER_NAME"], + HttpContext.Current.Request.ServerVariables["SERVER_PORT"], + IOHelper.ResolveUrl(SystemDirectories.Umbraco), querystring); - var myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); + var myHttpWebRequest = (HttpWebRequest) WebRequest.Create(url); // allows for validation of SSL conversations (to bypass SSL errors in debug mode!) ServicePointManager.ServerCertificateValidationCallback += ValidateRemoteCertificate; @@ -1482,7 +1311,7 @@ namespace umbraco HttpWebResponse myHttpWebResponse = null; try { - myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); + myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse(); if (myHttpWebResponse.StatusCode == HttpStatusCode.OK) { Stream streamResponse = myHttpWebResponse.GetResponseStream(); @@ -1531,15 +1360,16 @@ namespace umbraco private static string showNoMacroContent(macro currentMacro) { - return "" + currentMacro.Name + "
    No macro content available for WYSIWYG editing
    "; + return "" + currentMacro.Name + + "
    No macro content available for WYSIWYG editing
    "; } private static bool ValidateRemoteCertificate( -object sender, - X509Certificate certificate, - X509Chain chain, - SslPolicyErrors policyErrors -) + object sender, + X509Certificate certificate, + X509Chain chain, + SslPolicyErrors policyErrors + ) { if (GlobalSettings.DebugMode) { @@ -1574,29 +1404,9 @@ object sender, xslt = xslt.Replace("{1}", namespaceList.ToString()); return xslt; } - - #region Macro Init refactor - - // add some caching object here - - public macro GetMacro(string Alias) - { - - // load it via API - Macro m = Macro.GetByAlias(Alias); - - return convertAPIMacroToLegacyRuntimeFormat(m); - } - - private macro convertAPIMacroToLegacyRuntimeFormat(Macro m) - { - throw new NotImplementedException("not implemented"); - } - - #endregion - } + public class MacroCacheContent { private readonly Control _control; @@ -1643,7 +1453,6 @@ object sender, public void RefreshAll() { - macro.ClearAliasCache(); } public void Refresh(Guid Id) @@ -1653,12 +1462,12 @@ object sender, void ICacheRefresher.Refresh(int Id) { - new macro(Id).removeFromCache(); + macro.GetMacro(Id).removeFromCache(); } void ICacheRefresher.Remove(int Id) { - new macro(Id).removeFromCache(); + macro.GetMacro(Id).removeFromCache(); } #endregion @@ -1694,18 +1503,17 @@ object sender, public class DLRMacroResult { - public Control Control { get; set; } - public Exception ResultException { get; set; } - public DLRMacroResult() { - } public DLRMacroResult(Control control, Exception resultException) { - this.Control = control; - this.ResultException = resultException; + Control = control; + ResultException = resultException; } + + public Control Control { get; set; } + public Exception ResultException { get; set; } } } \ No newline at end of file diff --git a/umbraco/presentation/template.cs b/umbraco/presentation/template.cs index 43fd7ffb10..0c29825669 100644 --- a/umbraco/presentation/template.cs +++ b/umbraco/presentation/template.cs @@ -279,7 +279,7 @@ namespace umbraco if (macroID != String.Empty) tempMacro = getMacro(macroID); else - tempMacro = macro.ReturnFromAlias(helper.FindAttribute(attributes, "macroalias")); + tempMacro = macro.GetMacro(helper.FindAttribute(attributes, "macroalias")); if (tempMacro != null) { @@ -460,7 +460,7 @@ namespace umbraco private macro getMacro(String macroID) { System.Web.HttpContext.Current.Trace.Write("umbracoTemplate", "Starting macro (" + macroID.ToString() + ")"); - return new macro(Convert.ToInt16(macroID)); + return macro.GetMacro(Convert.ToInt16(macroID)); } private String FindAttribute(Hashtable attributes, String key) diff --git a/umbraco/presentation/umbraco.presentation.csproj b/umbraco/presentation/umbraco.presentation.csproj index d3d2a59a04..724e44321e 100644 --- a/umbraco/presentation/umbraco.presentation.csproj +++ b/umbraco/presentation/umbraco.presentation.csproj @@ -341,9 +341,7 @@ Code - - ASPXCodeBehind - + Code diff --git a/umbraco/presentation/umbraco/create/macroTasks.cs b/umbraco/presentation/umbraco/create/macroTasks.cs index 17e1fcc197..f5786b42cd 100644 --- a/umbraco/presentation/umbraco/create/macroTasks.cs +++ b/umbraco/presentation/umbraco/create/macroTasks.cs @@ -56,15 +56,8 @@ namespace umbraco public bool Delete() { - // Release cache - System.Web.Caching.Cache macroCache = System.Web.HttpRuntime.Cache; - if (macroCache["umbMacro" + ParentID.ToString()] != null) - { - macroCache.Remove("umbMacro" + ParentID.ToString()); - } - // Clear cache! - macro.ClearAliasCache(); + macro.GetMacro(ParentID).removeFromCache(); new cms.businesslogic.macro.Macro(ParentID).Delete(); return true; } diff --git a/umbraco/presentation/umbraco/developer/Macros/editMacro.aspx.cs b/umbraco/presentation/umbraco/developer/Macros/editMacro.aspx.cs index 41e3bd9f50..11bc90ff4d 100644 --- a/umbraco/presentation/umbraco/developer/Macros/editMacro.aspx.cs +++ b/umbraco/presentation/umbraco/developer/Macros/editMacro.aspx.cs @@ -149,7 +149,7 @@ namespace umbraco.cms.presentation.developer new Guid("7B1E683C-5F34-43dd-803D-9699EA1E98CA"), macroID); else - new macro(macroID).removeFromCache(); + macro.GetMacro(macroID).removeFromCache(); // Check for assemblyBrowser if (tempMacroType.IndexOf(".ascx") > 0) diff --git a/umbraco/presentation/umbraco/developer/Packages/installedPackage.aspx.cs b/umbraco/presentation/umbraco/developer/Packages/installedPackage.aspx.cs index e47dddc093..9b782673c3 100644 --- a/umbraco/presentation/umbraco/developer/Packages/installedPackage.aspx.cs +++ b/umbraco/presentation/umbraco/developer/Packages/installedPackage.aspx.cs @@ -436,7 +436,7 @@ namespace umbraco.presentation.developer.packages if (s != null && !String.IsNullOrEmpty(s.Name)) { // remove from cache - new runtimeMacro(s.Id).removeFromCache(); + runtimeMacro.GetMacro(s.Id).removeFromCache(); s.Delete(); } diff --git a/umbraco/presentation/umbraco/macroResultWrapper.aspx.cs b/umbraco/presentation/umbraco/macroResultWrapper.aspx.cs index 46ed47a6ea..4c9d13fb3b 100644 --- a/umbraco/presentation/umbraco/macroResultWrapper.aspx.cs +++ b/umbraco/presentation/umbraco/macroResultWrapper.aspx.cs @@ -41,7 +41,7 @@ namespace umbraco.presentation page p = new page(pageID, pageVersion); - macro m = new macro(macroID); + macro m = macro.GetMacro(macroID); Control c = m.renderMacro(attributes, p.Elements, p.PageID); PlaceHolder1.Controls.Add(c); diff --git a/umbraco/presentation/umbraco/plugins/tinymce3/insertMacro.aspx.cs b/umbraco/presentation/umbraco/plugins/tinymce3/insertMacro.aspx.cs index 85a6d33368..7d7fefc4d9 100644 --- a/umbraco/presentation/umbraco/plugins/tinymce3/insertMacro.aspx.cs +++ b/umbraco/presentation/umbraco/plugins/tinymce3/insertMacro.aspx.cs @@ -182,7 +182,7 @@ namespace umbraco.presentation.tinymce3 Hashtable attributes = new Hashtable(); attributes.Add("macroAlias", m.Alias); - macro mRender = new macro(m.Id); + macro mRender = macro.GetMacro(m.Id); foreach (Control c in _dataFields) { try diff --git a/umbraco/presentation/umbraco/templateControls/Macro.cs b/umbraco/presentation/umbraco/templateControls/Macro.cs index 584a91dc93..c3e24292b8 100644 --- a/umbraco/presentation/umbraco/templateControls/Macro.cs +++ b/umbraco/presentation/umbraco/templateControls/Macro.cs @@ -146,22 +146,22 @@ namespace umbraco.presentation.templateControls if ((!String.IsNullOrEmpty(Language) && Text != "") || !string.IsNullOrEmpty(FileLocation)) { var tempMacro = new macro(); - var model = tempMacro.ConvertToMacroModel(MacroAttributes); + tempMacro.GenerateMacroModelPropertiesFromAttributes(MacroAttributes); if (string.IsNullOrEmpty(FileLocation)) { - model.ScriptCode = Text; - model.ScriptLanguage = Language; + tempMacro.Model.ScriptCode = Text; + tempMacro.Model.ScriptLanguage = Language; } else { - model.ScriptName = FileLocation; + tempMacro.Model.ScriptName = FileLocation; } - model.MacroType = MacroTypes.Script; + tempMacro.Model.MacroType = MacroTypes.Script; if (!String.IsNullOrEmpty(Attributes["Cache"])) { var cacheDuration = 0; if (int.TryParse(Attributes["Cache"], out cacheDuration)) - model.CacheDuration = cacheDuration; + tempMacro.Model.CacheDuration = cacheDuration; else System.Web.HttpContext.Current.Trace.Warn("Template", "Cache attribute is in incorect format (should be an integer)."); } - var c = tempMacro.renderMacro(model, (Hashtable)Context.Items["pageElements"], pageId); + var c = tempMacro.renderMacro((Hashtable)Context.Items["pageElements"], pageId); if (c != null) { Exceptions = tempMacro.Exceptions; @@ -172,7 +172,7 @@ namespace umbraco.presentation.templateControls System.Web.HttpContext.Current.Trace.Warn("Template", "Result of inline macro scripting is null"); } else { - var tempMacro = macro.ReturnFromAlias(Alias); + var tempMacro = macro.GetMacro(Alias); if (tempMacro != null) { try { var c = tempMacro.renderMacro(MacroAttributes, (Hashtable)Context.Items["pageElements"], pageId);