public static ContentTree GetByDistinctName(string distinctName, string templateDistinctName = null, bool useCache = true) { if (string.IsNullOrEmpty(distinctName)) { return(null); } ContentTree tree = null; // look for the cache string cacheKey = string.Format("{0}_{1}", distinctName, templateDistinctName); if (useCache) { if (s_ContentTreeCache.TryGetValue(cacheKey, out tree)) { return(tree); } } tree = new ContentTree() { DistinctName = distinctName, TemplateDomainDistinctName = templateDistinctName }; string physicalPath = HostingEnvironment.MapPath(string.Format("~/Views/{0}", distinctName)).TrimEnd('\\'); ContentNode root = new ContentNode(tree, physicalPath, "/", false); tree.Root = root; bool hasTemplateTree = false; if (!string.IsNullOrEmpty(templateDistinctName)) { ContentTree templateTree = ObjectHelper.DeepClone <ContentTree>(GetByDistinctName(templateDistinctName, null, useCache)); if (templateTree != null) { hasTemplateTree = true; tree = OverrideNodes(tree, templateTree); } } if (!hasTemplateTree) { foreach (KeyValuePair <string, ContentNode> item in tree.AllNodes) { item.Value.NodeStatus = ContentNode.ContentNodeStatus.Normal; } } tree.UpdateHierarchicalStructure(); // update the cache s_ContentTreeCache[cacheKey] = tree; return(tree); }
public void Save() { string file = this.ContentNode.RealPhysicalPath + "\\.properties.xml"; cmSite domain = SiteManager.GetSiteByDistinctName(ContentNode.ContentTree.DistinctName); ContentTree.EnsureDirectoryExistsForFile(domain, file); PropertyFileHelper.Save(file, new { @Controller = this.Controller, @RouteName = this.RouteName, @IsInherited = false }); }
public void Save() { string file = this.ContentNode.RealPhysicalPath + "\\.properties.xml"; cmSite domain = SiteManager.GetSiteByDistinctName(ContentNode.ContentTree.DistinctName); ContentTree.EnsureDirectoryExistsForFile(domain, file); PropertyFileHelper.Save(file, new { @ValidFrom = this.ValidFrom, @ExpiryTime = this.ExpiryTime, @AvailableForUKLicense = this.AvailableForUKLicense, @AvailableForNonUKLicense = this.AvailableForNonUKLicense, @IsInherited = false }, true); }
/// <summary> /// Copy nodes from tree to tree /// </summary> /// <param name="fromTree">the source tree to copy from</param> /// <param name="toTree">the destination tree</param> private static ContentTree OverrideNodes(ContentTree fromTree, ContentTree toTree) { // change the ditinctname of the detination tree toTree.DistinctName = fromTree.DistinctName; // mark every node in detination tree as ContentNodeStatus.Inherited foreach (KeyValuePair <string, ContentNode> item in toTree.AllNodes) { if (item.Key != "/") { item.Value.NodeStatus = ContentNode.ContentNodeStatus.Inherited; } } // copy each node from current tree to template tree foreach (KeyValuePair <string, ContentNode> fromItem in fromTree.AllNodes) { if (fromItem.Key != "/") { // if already exists in template tree, mark as overrode if (toTree.AllNodes.ContainsKey(fromItem.Key)) { if (fromItem.Value.NodeStatus != ContentNode.ContentNodeStatus.Inherited) { fromItem.Value.NodeStatus = ContentNode.ContentNodeStatus.Overrode; } else { continue;// ignore the inheritted node } } else { fromItem.Value.NodeStatus = ContentNode.ContentNodeStatus.Normal; } // copy the node from template tree fromItem.Value.ContentTree = toTree; toTree.AllNodes[fromItem.Key] = fromItem.Value; } } return(toTree); }
} // /// <summary> /// /// </summary> /// <param name="contentTree"></param> /// <param name="physicalPath"></param> /// <param name="relativePath"></param> /// <param name="isFile"></param> public ContentNode(ContentTree contentTree, string physicalPath, string relativePath, bool isFile) { Children = new ConcurrentDictionary <string, ContentNode>(StringComparer.OrdinalIgnoreCase); this.NodeStatus = ContentNodeStatus.Normal; this.PhysicalPath = physicalPath; this.RelativePath = relativePath; this.ContentTree = contentTree; this.NodeType = ContentNodeType.None; contentTree.AllNodes[relativePath] = this; if (isFile) { switch (Path.GetExtension(relativePath)) { case ".ascx": this.DisplayName = Path.GetFileNameWithoutExtension(physicalPath); this.NodeType = ContentNodeType.PartialView; break; case ".aspx": this.DisplayName = Path.GetFileNameWithoutExtension(physicalPath); this.NodeType = ContentNodeType.View; break; case ".ashx": this.DisplayName = Path.GetFileNameWithoutExtension(physicalPath); this.NodeType = ContentNodeType.HttpHandler; break; case ".master": this.DisplayName = Path.GetFileNameWithoutExtension(physicalPath); this.NodeType = ContentNodeType.PageTemplate; break; case ".htm": this.DisplayName = Path.GetFileNameWithoutExtension(physicalPath); this.NodeType = ContentNodeType.StaticContent; break; case ".snippet": this.DisplayName = Path.GetFileNameWithoutExtension(physicalPath); this.NodeType = ContentNodeType.HtmlSnippet; break; default: this.DisplayName = Path.GetFileName(physicalPath); this.NodeType = ContentNodeType.None; break; } }// file else { this.DisplayName = Path.GetFileName(physicalPath); NodeType = ContentNodeType.Directory; string xml = string.Format("{0}\\.properties.xml", this.PhysicalPath); if (File.Exists(xml)) { XDocument doc = PropertyFileHelper.OpenReadWithoutLock(xml); XElement root = doc.Root; ContentNodeType nodeType; if (Enum.TryParse <ContentNodeType>(root.GetElementValue("Type"), out nodeType)) { NodeType = nodeType; } if (string.Compare(root.GetElementValue("IsInherited", "false"), "true", true) == 0) { this.NodeStatus = ContentNodeStatus.Inherited; } if (string.Compare(root.GetElementValue("IsDisabled", "false"), "true", true) == 0) { this.IsDisabled = true; } } LoadChildren(); } // directory } //
/// <summary> /// Write the metadata entry /// </summary> /// <param name="site"></param> /// <param name="path"></param> /// <param name="lang">If language is null, then default value is written</param> /// <param name="name"></param> /// <param name="value"></param> /// <returns></returns> public static bool Save(cmSite site, string path, string lang, string name, string value) { string postfix = string.IsNullOrWhiteSpace(lang) ? string.Empty : "." + lang; string physicalPath = HostingEnvironment.MapPath( string.Format("~/Views/{0}/{1}/.{2}{3}", site.DistinctName, path.TrimStart('/').TrimEnd('/'), name, postfix) ); ContentTree.EnsureDirectoryExistsForFile(site, physicalPath); SqlQuery <cmRevision> query = new SqlQuery <cmRevision>(); string filePath; string localFile; string metadataPath = path.TrimEnd('/') + "/." + name + postfix; if (File.Exists(physicalPath)) { // nothing changed if (WinFileIO.ReadWithoutLock(physicalPath) == value) { return(false); } // if last revision not exist, backup first { RevisionAccessor ra = DataAccessor.CreateInstance <RevisionAccessor>(); cmRevision revision = ra.GetLastRevision(site.ID, metadataPath); if (revision == null || !File.Exists(Revisions.GetLocalPath(revision.FilePath))) { localFile = Revisions.GetNewFilePath(out filePath); File.Copy(physicalPath, localFile); revision = new cmRevision(); revision.Comments = string.Format("No revision found for [{0}], language=[{1}], make a backup.", name, lang.DefaultIfNullOrEmpty("default")); revision.SiteID = site.ID; revision.FilePath = filePath; revision.Ins = DateTime.Now; revision.RelativePath = metadataPath; revision.UserID = CustomProfile.Current.UserID; query.Insert(revision); } } } else if (!string.IsNullOrWhiteSpace(site.TemplateDomainDistinctName)) { string inheritedPath = HostingEnvironment.MapPath( string.Format("~/Views/{0}/{1}/.{2}{3}", site.TemplateDomainDistinctName, path.TrimStart('/').TrimEnd('/'), name, postfix) ); if (File.Exists(inheritedPath)) { // nothing changed if (WinFileIO.ReadWithoutLock(inheritedPath) == value) { return(false); } } else if (string.IsNullOrWhiteSpace(value)) { return(false); } } else if (string.IsNullOrWhiteSpace(value)) { return(false); } using (FileStream fs = new FileStream(physicalPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Delete | FileShare.ReadWrite)) { fs.SetLength(0); using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8)) { sw.Write(value); } } // copy the file to backup localFile = Revisions.GetNewFilePath(out filePath); File.Copy(physicalPath, localFile); // save to cmRevision { cmRevision revision = new cmRevision(); revision.Comments = string.Format("update the entry [{0}], language=[{1}].", name, lang.DefaultIfNullOrEmpty("default")); revision.SiteID = site.ID; revision.FilePath = filePath; revision.Ins = DateTime.Now; revision.RelativePath = metadataPath; revision.UserID = CustomProfile.Current.UserID; query.Insert(revision); } return(true); }
public static void EnsureDirectoryExistsForFile(cmSite domain, string filename) { string dir = Path.GetDirectoryName(filename); ContentTree.EnsureDirectoryExists(domain, dir); }