diff --git a/src/Umbraco.Core/Models/Template.cs b/src/Umbraco.Core/Models/Template.cs
index 4659595049..a8860fdf09 100644
--- a/src/Umbraco.Core/Models/Template.cs
+++ b/src/Umbraco.Core/Models/Template.cs
@@ -67,10 +67,18 @@ namespace Umbraco.Core.Models
}
[DataMember]
- string ITemplate.Name { get; set; }
+ string ITemplate.Name
+ {
+ get { return _name; }
+ set { _name = value; }
+ }
[DataMember]
- string ITemplate.Alias { get; set; }
+ string ITemplate.Alias
+ {
+ get { return _alias; }
+ set { _alias = value; }
+ }
public override string Alias
{
diff --git a/src/Umbraco.Core/Services/FileService.cs b/src/Umbraco.Core/Services/FileService.cs
index a5685756c4..6cac0cc808 100644
--- a/src/Umbraco.Core/Services/FileService.cs
+++ b/src/Umbraco.Core/Services/FileService.cs
@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Remoting.Messaging;
-using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using Umbraco.Core.Auditing;
diff --git a/src/umbraco.cms/businesslogic/template/MasterpageHelper.cs b/src/Umbraco.Core/Services/MasterPageHelper.cs
similarity index 54%
rename from src/umbraco.cms/businesslogic/template/MasterpageHelper.cs
rename to src/Umbraco.Core/Services/MasterPageHelper.cs
index c72bba8fb1..652887d212 100644
--- a/src/umbraco.cms/businesslogic/template/MasterpageHelper.cs
+++ b/src/Umbraco.Core/Services/MasterPageHelper.cs
@@ -1,390 +1,415 @@
-using System;
-using System.Collections;
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
-using System.Text;
-using System.Text.RegularExpressions;
-using Umbraco.Core;
-using Umbraco.Core.IO;
-
-namespace umbraco.cms.businesslogic.template
-{
- internal class MasterPageHelper
- {
- internal static readonly string DefaultMasterTemplate = SystemDirectories.Umbraco + "/masterpages/default.master";
- private static readonly char[] NewLineChars = Environment.NewLine.ToCharArray();
-
- internal static bool MasterPageExists(Template t)
- {
- return File.Exists(GetFilePath(t));
- }
-
- internal static string GetFilePath(Template t)
- {
- return IOHelper.MapPath(SystemDirectories.Masterpages + "/" + t.Alias.Replace(" ", "") + ".master");
- }
-
- internal static string CreateMasterPage(Template t, bool overWrite = false)
- {
- string masterpageContent = "";
-
- if (!File.Exists(GetFilePath(t)) || overWrite)
- {
- masterpageContent = CreateDefaultMasterPageContent(t, t.Alias);
- SaveDesignToFile(t, null, masterpageContent);
- }
- else
- {
- System.IO.TextReader tr = new StreamReader(GetFilePath(t));
- masterpageContent = tr.ReadToEnd();
- tr.Close();
- }
-
- return masterpageContent;
- }
-
- internal static string GetFileContents(Template t)
- {
- string masterpageContent = "";
- if (File.Exists(GetFilePath(t)))
- {
- System.IO.TextReader tr = new StreamReader(GetFilePath(t));
- masterpageContent = tr.ReadToEnd();
- tr.Close();
- }
-
- return masterpageContent;
- }
-
- internal static string UpdateMasterPageFile(Template t, string currentAlias)
- {
- var template = UpdateMasterPageContent(t, currentAlias);
- UpdateChildTemplates(t, currentAlias);
- SaveDesignToFile(t, currentAlias, template);
-
- return template;
- }
-
- internal static string CreateDefaultMasterPageContent(Template template, string currentAlias)
- {
- StringBuilder design = new StringBuilder();
- design.Append(GetMasterPageHeader(template) + Environment.NewLine);
-
- if (template.HasMasterTemplate)
- {
- var master = new Template(template.MasterTemplate);
-
- foreach (string cpId in master.contentPlaceholderIds())
- {
- design.Append("" +
- Environment.NewLine +
- Environment.NewLine +
- "" +
- Environment.NewLine +
- Environment.NewLine);
- }
- }
- else
- {
- design.Append(GetMasterContentElement(template) + Environment.NewLine);
- design.Append(template.Design + Environment.NewLine);
- design.Append("" + Environment.NewLine);
- }
-
- return design.ToString();
- }
-
- internal static string UpdateMasterPageContent(Template template, string currentAlias)
- {
- var masterPageContent = template.Design;
-
- if (!string.IsNullOrEmpty(currentAlias) && currentAlias != template.Alias)
- {
- string masterHeader =
- masterPageContent.Substring(0, masterPageContent.IndexOf("%>") + 2).Trim(
- Environment.NewLine.ToCharArray());
-
- // find the masterpagefile attribute
- MatchCollection m = Regex.Matches(masterHeader, "(?\\S*)=\"(?[^\"]*)\"",
- RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
-
- foreach (Match attributeSet in m)
- {
- if (attributeSet.Groups["attributeName"].Value.ToLower() == "masterpagefile")
- {
- // validate the masterpagefile
- string currentMasterPageFile = attributeSet.Groups["attributeValue"].Value;
- string currentMasterTemplateFile = ParentTemplatePath(template);
-
- if (currentMasterPageFile != currentMasterTemplateFile)
- {
- masterPageContent =
- masterPageContent.Replace(
- attributeSet.Groups["attributeName"].Value + "=\"" + currentMasterPageFile + "\"",
- attributeSet.Groups["attributeName"].Value + "=\"" + currentMasterTemplateFile +
- "\"");
- }
- }
- }
- }
-
- return masterPageContent;
- }
-
- private static void UpdateChildTemplates(Template t, string currentAlias)
- {
- //if we have a Old Alias if the alias and therefor the masterpage file name has changed...
- //so before we save the new masterfile, we'll clear the old one, so we don't up with
- //Unused masterpage files
- if (!string.IsNullOrEmpty(currentAlias) && currentAlias != t.Alias)
- {
- //Ensure that child templates have the right master masterpage file name
- if (t.HasChildren)
- {
- var c = t.Children;
- foreach (CMSNode cmn in c)
- UpdateMasterPageFile(new Template(cmn.Id), null);
- }
- }
- }
-
-
- private static void SaveDesignToFile(Template t, string currentAlias, string design)
- {
- //kill the old file..
- if (!string.IsNullOrEmpty(currentAlias) && currentAlias != t.Alias)
- {
- string _oldFile =
- IOHelper.MapPath(SystemDirectories.Masterpages + "/" + currentAlias.Replace(" ", "") + ".master");
- if (System.IO.File.Exists(_oldFile))
- System.IO.File.Delete(_oldFile);
- }
-
- // save the file in UTF-8
- System.IO.File.WriteAllText(GetFilePath(t), design, System.Text.Encoding.UTF8);
- }
-
- internal static void RemoveMasterPageFile(string alias)
- {
- if (!string.IsNullOrWhiteSpace(alias))
- {
- string file = IOHelper.MapPath(SystemDirectories.Masterpages + "/" + alias.Replace(" ", "") + ".master");
- if (System.IO.File.Exists(file))
- System.IO.File.Delete(file);
- }
- }
-
- internal static string SaveTemplateToFile(Template template, string currentAlias)
- {
- var masterPageContent = template.Design;
- if (!IsMasterPageSyntax(masterPageContent))
- masterPageContent = ConvertToMasterPageSyntax(template);
-
- // Add header to master page if it doesn't exist
- if (!masterPageContent.TrimStart().StartsWith("<%@"))
- {
- masterPageContent = GetMasterPageHeader(template) + Environment.NewLine + masterPageContent;
- }
- else
- {
- // verify that the masterpage attribute is the same as the masterpage
- string masterHeader =
- masterPageContent.Substring(0, masterPageContent.IndexOf("%>") + 2).Trim(NewLineChars);
-
- // find the masterpagefile attribute
- MatchCollection m = Regex.Matches(masterHeader, "(?\\S*)=\"(?[^\"]*)\"",
- RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
-
- foreach (Match attributeSet in m)
- {
- if (attributeSet.Groups["attributeName"].Value.ToLower() == "masterpagefile")
- {
- // validate the masterpagefile
- string currentMasterPageFile = attributeSet.Groups["attributeValue"].Value;
- string currentMasterTemplateFile = ParentTemplatePath(template);
-
- if (currentMasterPageFile != currentMasterTemplateFile)
- {
- masterPageContent =
- masterPageContent.Replace(
- attributeSet.Groups["attributeName"].Value + "=\"" + currentMasterPageFile + "\"",
- attributeSet.Groups["attributeName"].Value + "=\"" + currentMasterTemplateFile +
- "\"");
-
- }
- }
- }
-
- }
-
- //we have a Old Alias if the alias and therefor the masterpage file name has changed...
- //so before we save the new masterfile, we'll clear the old one, so we don't up with
- //Unused masterpage files
- if (!string.IsNullOrEmpty(currentAlias) && currentAlias != template.Alias)
- {
-
- //Ensure that child templates have the right master masterpage file name
- if (template.HasChildren)
- {
- var c = template.Children;
- foreach (CMSNode cmn in c)
- UpdateMasterPageFile(new Template(cmn.Id), null);
- }
-
- //then kill the old file..
- string _oldFile = IOHelper.MapPath(SystemDirectories.Masterpages + "/" + currentAlias.Replace(" ", "") + ".master");
- if (System.IO.File.Exists(_oldFile))
- System.IO.File.Delete(_oldFile);
- }
-
- // save the file in UTF-8
- System.IO.File.WriteAllText(GetFilePath(template), masterPageContent, System.Text.Encoding.UTF8);
-
- return masterPageContent;
- }
-
- internal static string ConvertToMasterPageSyntax(Template template)
- {
- string masterPageContent = GetMasterContentElement(template) + Environment.NewLine;
-
- masterPageContent += template.Design;
-
- // Parse the design for getitems
- masterPageContent = EnsureMasterPageSyntax(template.Alias, masterPageContent);
-
- // append ending asp:content element
- masterPageContent += Environment.NewLine + "" + Environment.NewLine;
-
- return masterPageContent;
- }
-
- internal static bool IsMasterPageSyntax(string code)
- {
- return Regex.IsMatch(code, @"<%@\s*Master", RegexOptions.IgnoreCase) ||
- code.InvariantContains("", ParentTemplatePath(template)) + Environment.NewLine;
- }
-
- private static string ParentTemplatePath(Template template)
- {
- var masterTemplate = DefaultMasterTemplate;
- if (template.MasterTemplate != 0)
- masterTemplate = SystemDirectories.Masterpages + "/" + new Template(template.MasterTemplate).Alias.Replace(" ", "") + ".master";
-
- return masterTemplate;
- }
-
- private static string GetMasterContentElement(Template template)
- {
- if (template.MasterTemplate != 0)
- {
- string masterAlias = new Template(template.MasterTemplate).Alias.Replace(" ", "");
- return
- String.Format("",
- template.Alias.Replace(" ", ""), masterAlias);
- }
- else
- return
- String.Format("",
- template.Alias.Replace(" ", ""));
-
- }
-
- internal static string EnsureMasterPageSyntax(string templateAlias, string masterPageContent)
- {
- ReplaceElement(ref masterPageContent, "?UMBRACO_GETITEM", "umbraco:Item", true);
- ReplaceElement(ref masterPageContent, "?UMBRACO_GETITEM", "umbraco:Item", false);
-
- // Parse the design for macros
- ReplaceElement(ref masterPageContent, "?UMBRACO_MACRO", "umbraco:Macro", true);
- ReplaceElement(ref masterPageContent, "?UMBRACO_MACRO", "umbraco:Macro", false);
-
- // Parse the design for load childs
- masterPageContent = masterPageContent.Replace("", CreateDefaultPlaceHolder(templateAlias))
- .Replace("", CreateDefaultPlaceHolder(templateAlias));
- // Parse the design for aspnet forms
- GetAspNetMasterPageForm(ref masterPageContent, templateAlias);
- masterPageContent = masterPageContent.Replace("?ASPNET_FORM>", "");
- // Parse the design for aspnet heads
- masterPageContent = masterPageContent.Replace("", String.Format("", templateAlias.Replace(" ", "")));
- masterPageContent = masterPageContent.Replace("?ASPNET_HEAD>", "");
- return masterPageContent;
- }
-
-
- private static void GetAspNetMasterPageForm(ref string design, string templateAlias)
- {
- Match formElement = Regex.Match(design, GetElementRegExp("?ASPNET_FORM", false), RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
-
- if (formElement != null && formElement.Value != "")
- {
- string formReplace = String.Format("" + Environment.NewLine);
+
+ return design.ToString();
+ }
+
+ internal static IEnumerable GetContentPlaceholderIds(ITemplate template)
+ {
+ var retVal = new List();
+
+ var mp = template.Content;
+ var path = "";
+ var r = new Regex(path, RegexOptions.IgnoreCase);
+ var m = r.Match(mp);
+
+ while (m.Success)
+ {
+ var cc = m.Groups[3].Captures;
+ retVal.AddRange(cc.Cast().Where(c => c.Value != "server").Select(c => c.Value));
+
+ m = m.NextMatch();
+ }
+
+ return retVal;
+ }
+
+ internal static string UpdateMasterPageContent(ITemplate template, string currentAlias)
+ {
+ var masterPageContent = template.Content;
+
+ if (string.IsNullOrEmpty(currentAlias) == false && currentAlias != template.Alias)
+ {
+ var masterHeader =
+ masterPageContent.Substring(0, masterPageContent.IndexOf("%>", StringComparison.Ordinal) + 2).Trim(
+ Environment.NewLine.ToCharArray());
+
+ // find the masterpagefile attribute
+ var m = Regex.Matches(masterHeader, "(?\\S*)=\"(?[^\"]*)\"",
+ RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
+
+ foreach (Match attributeSet in m)
+ {
+ if (attributeSet.Groups["attributeName"].Value.ToLower() == "masterpagefile")
+ {
+ // validate the masterpagefile
+ var currentMasterPageFile = attributeSet.Groups["attributeValue"].Value;
+ var currentMasterTemplateFile = ParentTemplatePath(template);
+
+ if (currentMasterPageFile != currentMasterTemplateFile)
+ {
+ masterPageContent =
+ masterPageContent.Replace(
+ attributeSet.Groups["attributeName"].Value + "=\"" + currentMasterPageFile + "\"",
+ attributeSet.Groups["attributeName"].Value + "=\"" + currentMasterTemplateFile +
+ "\"");
+ }
+ }
+ }
+ }
+
+ return masterPageContent;
+ }
+
+ private static void UpdateChildTemplates(ITemplate template, string currentAlias, IFileService fileService)
+ {
+ //if we have a Old Alias if the alias and therefor the masterpage file name has changed...
+ //so before we save the new masterfile, we'll clear the old one, so we don't up with
+ //Unused masterpage files
+ if (string.IsNullOrEmpty(currentAlias) == false && currentAlias != template.Alias)
+ {
+ //Ensure that child templates have the right master masterpage file name
+ if (template.IsMasterTemplate)
+ {
+ var children = fileService.GetTemplates(template.Id);
+ foreach (var t in children)
+ UpdateMasterPageFile(t, null, fileService);
+ }
+ }
+ }
+
+
+ private static void SaveDesignToFile(ITemplate t, string currentAlias, string design)
+ {
+ //kill the old file..
+ if (string.IsNullOrEmpty(currentAlias) == false && currentAlias != t.Alias)
+ {
+ var oldFile =
+ IOHelper.MapPath(SystemDirectories.Masterpages + "/" + currentAlias.Replace(" ", "") + ".master");
+ if (System.IO.File.Exists(oldFile))
+ System.IO.File.Delete(oldFile);
+ }
+
+ // save the file in UTF-8
+ System.IO.File.WriteAllText(GetFilePath(t), design, Encoding.UTF8);
+ }
+
+ internal static void RemoveMasterPageFile(string alias)
+ {
+ if (string.IsNullOrWhiteSpace(alias) == false)
+ {
+ string file = IOHelper.MapPath(SystemDirectories.Masterpages + "/" + alias.Replace(" ", "") + ".master");
+ if (System.IO.File.Exists(file))
+ System.IO.File.Delete(file);
+ }
+ }
+
+ internal static string SaveTemplateToFile(ITemplate template, string currentAlias, IFileService fileService)
+ {
+ var masterPageContent = template.Content;
+ if (IsMasterPageSyntax(masterPageContent) == false)
+ masterPageContent = ConvertToMasterPageSyntax(template);
+
+ // Add header to master page if it doesn't exist
+ if (masterPageContent.TrimStart().StartsWith("<%@") == false)
+ {
+ masterPageContent = GetMasterPageHeader(template) + Environment.NewLine + masterPageContent;
+ }
+ else
+ {
+ // verify that the masterpage attribute is the same as the masterpage
+ var masterHeader =
+ masterPageContent.Substring(0, masterPageContent.IndexOf("%>", StringComparison.Ordinal) + 2).Trim(NewLineChars);
+
+ // find the masterpagefile attribute
+ var m = Regex.Matches(masterHeader, "(?\\S*)=\"(?[^\"]*)\"",
+ RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
+
+ foreach (Match attributeSet in m)
+ {
+ if (attributeSet.Groups["attributeName"].Value.ToLower() == "masterpagefile")
+ {
+ // validate the masterpagefile
+ var currentMasterPageFile = attributeSet.Groups["attributeValue"].Value;
+ var currentMasterTemplateFile = ParentTemplatePath(template);
+
+ if (currentMasterPageFile != currentMasterTemplateFile)
+ {
+ masterPageContent =
+ masterPageContent.Replace(
+ attributeSet.Groups["attributeName"].Value + "=\"" + currentMasterPageFile + "\"",
+ attributeSet.Groups["attributeName"].Value + "=\"" + currentMasterTemplateFile +
+ "\"");
+
+ }
+ }
+ }
+
+ }
+
+ //we have a Old Alias if the alias and therefor the masterpage file name has changed...
+ //so before we save the new masterfile, we'll clear the old one, so we don't up with
+ //Unused masterpage files
+ if (string.IsNullOrEmpty(currentAlias) == false && currentAlias != template.Alias)
+ {
+
+ //Ensure that child templates have the right master masterpage file name
+ if (template.IsMasterTemplate)
+ {
+ var children = fileService.GetTemplates(template.Id);
+
+ foreach (var t in children)
+ UpdateMasterPageFile(t, null, fileService);
+ }
+
+ //then kill the old file..
+ var oldFile = IOHelper.MapPath(SystemDirectories.Masterpages + "/" + currentAlias.Replace(" ", "") + ".master");
+ if (System.IO.File.Exists(oldFile))
+ System.IO.File.Delete(oldFile);
+ }
+
+ // save the file in UTF-8
+ System.IO.File.WriteAllText(GetFilePath(template), masterPageContent, Encoding.UTF8);
+
+ return masterPageContent;
+ }
+
+ internal static string ConvertToMasterPageSyntax(ITemplate template)
+ {
+ string masterPageContent = GetMasterContentElement(template) + Environment.NewLine;
+
+ masterPageContent += template.Content;
+
+ // Parse the design for getitems
+ masterPageContent = EnsureMasterPageSyntax(template.Alias, masterPageContent);
+
+ // append ending asp:content element
+ masterPageContent += Environment.NewLine + "" + Environment.NewLine;
+
+ return masterPageContent;
+ }
+
+ internal static bool IsMasterPageSyntax(string code)
+ {
+ return Regex.IsMatch(code, @"<%@\s*Master", RegexOptions.IgnoreCase) ||
+ code.InvariantContains("", ParentTemplatePath(template)) + Environment.NewLine;
+ }
+
+ private static string ParentTemplatePath(ITemplate template)
+ {
+ var masterTemplate = DefaultMasterTemplate;
+ if (template.MasterTemplateAlias.IsNullOrWhiteSpace() == false)
+ masterTemplate = SystemDirectories.Masterpages + "/" + template.MasterTemplateAlias + ".master";
+
+ return masterTemplate;
+ }
+
+ private static string GetMasterContentElement(ITemplate template)
+ {
+ if (template.MasterTemplateAlias.IsNullOrWhiteSpace() == false)
+ {
+ string masterAlias = template.MasterTemplateAlias;
+ return
+ String.Format("", masterAlias);
+ }
+ else
+ return
+ String.Format("");
+
+ }
+
+ internal static string EnsureMasterPageSyntax(string templateAlias, string masterPageContent)
+ {
+ ReplaceElement(ref masterPageContent, "?UMBRACO_GETITEM", "umbraco:Item", true);
+ ReplaceElement(ref masterPageContent, "?UMBRACO_GETITEM", "umbraco:Item", false);
+
+ // Parse the design for macros
+ ReplaceElement(ref masterPageContent, "?UMBRACO_MACRO", "umbraco:Macro", true);
+ ReplaceElement(ref masterPageContent, "?UMBRACO_MACRO", "umbraco:Macro", false);
+
+ // Parse the design for load childs
+ masterPageContent = masterPageContent.Replace("", CreateDefaultPlaceHolder(templateAlias))
+ .Replace("", CreateDefaultPlaceHolder(templateAlias));
+ // Parse the design for aspnet forms
+ GetAspNetMasterPageForm(ref masterPageContent, templateAlias);
+ masterPageContent = masterPageContent.Replace("?ASPNET_FORM>", "");
+ // Parse the design for aspnet heads
+ masterPageContent = masterPageContent.Replace("", String.Format("", templateAlias.Replace(" ", "")));
+ masterPageContent = masterPageContent.Replace("?ASPNET_HEAD>", "");
+ return masterPageContent;
+ }
+
+
+ private static void GetAspNetMasterPageForm(ref string design, string templateAlias)
+ {
+ var formElement = Regex.Match(design, GetElementRegExp("?ASPNET_FORM", false), RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
+
+ if (string.IsNullOrEmpty(formElement.Value) == false)
+ {
+ string formReplace = String.Format("