diff --git a/src/Umbraco.Infrastructure/Packaging/PackageDataInstallation.cs b/src/Umbraco.Infrastructure/Packaging/PackageDataInstallation.cs index 9b7e28f80a..70ce46164c 100644 --- a/src/Umbraco.Infrastructure/Packaging/PackageDataInstallation.cs +++ b/src/Umbraco.Infrastructure/Packaging/PackageDataInstallation.cs @@ -122,31 +122,21 @@ namespace Umbraco.Cms.Infrastructure.Packaging /// Optional id of the User performing the operation. Default is zero (admin). /// An enumerable list of generated ContentTypes public IReadOnlyList ImportMediaTypes(IEnumerable docTypeElements, int userId) - { - return ImportDocumentTypes(docTypeElements.ToList(), true, userId, _mediaTypeService); - } + => ImportDocumentTypes(docTypeElements.ToList(), true, userId, _mediaTypeService); #endregion #region Content - public IReadOnlyList ImportContentBase( + public IReadOnlyList ImportContentBase( IEnumerable docs, - IDictionary importedDocumentTypes, + IDictionary importedDocumentTypes, int userId, - IContentTypeBaseService typeService, - IContentServiceBase service) - where T : class, IContentBase - where S : IContentTypeComposition - { - return docs.SelectMany(x => ImportContentBase( - x.XmlData.Elements().Where(doc => (string)doc.Attribute("isDoc") == string.Empty), - -1, - importedDocumentTypes, - userId, - typeService, - service)).ToList(); - } + IContentTypeBaseService typeService, + IContentServiceBase service) + where TContentBase : class, IContentBase + where TContentTypeComposition : IContentTypeComposition + => docs.SelectMany(x => ImportContentBase(x.XmlData.Elements().Where(doc => (string)doc.Attribute("isDoc") == string.Empty), -1, importedDocumentTypes, userId, typeService, service)).ToList(); /// /// Imports and saves package xml as @@ -156,17 +146,16 @@ namespace Umbraco.Cms.Infrastructure.Packaging /// A dictionary of already imported document types (basically used as a cache) /// Optional Id of the user performing the import /// An enumerable list of generated content - public IEnumerable ImportContentBase( + public IEnumerable ImportContentBase( IEnumerable roots, int parentId, - IDictionary importedDocumentTypes, + IDictionary importedDocumentTypes, int userId, - IContentTypeBaseService typeService, - IContentServiceBase service) - where T : class, IContentBase - where S : IContentTypeComposition + IContentTypeBaseService typeService, + IContentServiceBase service) + where TContentBase : class, IContentBase + where TContentTypeComposition : IContentTypeComposition { - var contents = ParseContentBaseRootXml(roots, parentId, importedDocumentTypes, typeService, service).ToList(); if (contents.Any()) { @@ -213,74 +202,72 @@ namespace Umbraco.Cms.Infrastructure.Packaging { throw new InvalidOperationException("Could not find content type with alias " + contentTypeAlias); } + importedContentTypes.Add(contentTypeAlias, contentType); } - if (TryCreateContentFromXml(root, importedContentTypes[contentTypeAlias], default, parentId, service, - out TContentBase content)) + if (TryCreateContentFromXml(root, importedContentTypes[contentTypeAlias], default, parentId, service, out TContentBase content)) { contents.Add(content); } - var children = root.Elements().Where(doc => (string)doc.Attribute("isDoc") == string.Empty) - .ToList(); - + var children = root.Elements().Where(doc => (string)doc.Attribute("isDoc") == string.Empty).ToList(); if (children.Count > 0) { contents.AddRange(CreateContentFromXml(children, content, importedContentTypes, typeService, service).WhereNotNull()); } } + return contents; } - private IEnumerable CreateContentFromXml( + private IEnumerable CreateContentFromXml( IEnumerable children, - T parent, - IDictionary importedContentTypes, - IContentTypeBaseService typeService, - IContentServiceBase service) - where T : class, IContentBase - where S : IContentTypeComposition + TContentBase parent, + IDictionary importedContentTypes, + IContentTypeBaseService typeService, + IContentServiceBase service) + where TContentBase : class, IContentBase + where TContentTypeComposition : IContentTypeComposition { - var list = new List(); + var list = new List(); + foreach (var child in children) { string contentTypeAlias = child.Name.LocalName; - if (importedContentTypes.ContainsKey(contentTypeAlias) == false) { var contentType = FindContentTypeByAlias(contentTypeAlias, typeService); + importedContentTypes.Add(contentTypeAlias, contentType); } - //Create and add the child to the list + // Create and add the child to the list if (TryCreateContentFromXml(child, importedContentTypes[contentTypeAlias], parent, default, service, out var content)) { list.Add(content); } - //Recursive call - var child1 = child; - var grandChildren = (from grand in child1.Elements() - where (string)grand.Attribute("isDoc") == "" - select grand).ToList(); - + // Recursive call + var grandChildren = child.Elements().Where(x => (string)x.Attribute("isDoc") == string.Empty).ToList(); if (grandChildren.Any()) + { list.AddRange(CreateContentFromXml(grandChildren, content, importedContentTypes, typeService, service)); + } } return list; } - private bool TryCreateContentFromXml( + private bool TryCreateContentFromXml( XElement element, - S contentType, - T parent, + TContentTypeComposition contentType, + TContentBase parent, int parentId, - IContentServiceBase service, - out T output) - where T : class, IContentBase - where S : IContentTypeComposition + IContentServiceBase service, + out TContentBase output) + where TContentBase : class, IContentBase + where TContentTypeComposition : IContentTypeComposition { Guid key = element.RequiredAttributeValue("key"); @@ -317,7 +304,7 @@ namespace Umbraco.Cms.Infrastructure.Packaging } } - T content = CreateContent( + TContentBase content = CreateContent( nodeName, parent, parentId, @@ -384,34 +371,34 @@ namespace Umbraco.Cms.Infrastructure.Packaging return true; } - private T CreateContent(string name, T parent, int parentId, S contentType, Guid key, int level, int sortOrder, int? templateId) - where T : class, IContentBase - where S : IContentTypeComposition + private TContentBase CreateContent(string name, TContentBase parent, int parentId, TContentTypeComposition contentType, Guid key, int level, int sortOrder, int? templateId) + where TContentBase : class, IContentBase + where TContentTypeComposition : IContentTypeComposition { switch (contentType) { case IContentType c: if (parent is null) { - return new Content(name, parentId, c) { Key = key, Level = level, SortOrder = sortOrder, TemplateId = templateId, } as T; + return new Content(name, parentId, c) { Key = key, Level = level, SortOrder = sortOrder, TemplateId = templateId, } as TContentBase; } else { - return new Content(name, (IContent)parent, c) { Key = key, Level = level, SortOrder = sortOrder, TemplateId = templateId, } as T; + return new Content(name, (IContent)parent, c) { Key = key, Level = level, SortOrder = sortOrder, TemplateId = templateId, } as TContentBase; } case IMediaType m: if (parent is null) { - return new Core.Models.Media(name, parentId, m) { Key = key, Level = level, SortOrder = sortOrder, } as T; + return new Core.Models.Media(name, parentId, m) { Key = key, Level = level, SortOrder = sortOrder, } as TContentBase; } else { - return new Core.Models.Media(name, (IMedia)parent, m) { Key = key, Level = level, SortOrder = sortOrder, } as T; + return new Core.Models.Media(name, (IMedia)parent, m) { Key = key, Level = level, SortOrder = sortOrder, } as TContentBase; } default: - throw new NotSupportedException($"Type {typeof(S)} is not supported"); + throw new NotSupportedException($"Type {typeof(TContentTypeComposition)} is not supported"); } } @@ -420,9 +407,7 @@ namespace Umbraco.Cms.Infrastructure.Packaging #region DocumentTypes public IReadOnlyList ImportDocumentType(XElement docTypeElement, int userId) - { - return ImportDocumentTypes(new[] { docTypeElement }, userId); - } + => ImportDocumentTypes(new[] { docTypeElement }, userId); /// /// Imports and saves package xml as @@ -431,9 +416,7 @@ namespace Umbraco.Cms.Infrastructure.Packaging /// Optional id of the User performing the operation. Default is zero (admin). /// An enumerable list of generated ContentTypes public IReadOnlyList ImportDocumentTypes(IEnumerable docTypeElements, int userId) - { - return ImportDocumentTypes(docTypeElements.ToList(), true, userId, _contentTypeService); - } + => ImportDocumentTypes(docTypeElements.ToList(), true, userId, _contentTypeService); /// /// Imports and saves package xml as @@ -443,7 +426,7 @@ namespace Umbraco.Cms.Infrastructure.Packaging /// Optional id of the User performing the operation. Default is zero (admin). /// An enumerable list of generated ContentTypes public IReadOnlyList ImportDocumentTypes(IReadOnlyCollection unsortedDocumentTypes, bool importStructure, int userId, IContentTypeBaseService service) - where T : class, IContentTypeComposition + where T : class, IContentTypeComposition { var importedContentTypes = new Dictionary(); @@ -768,8 +751,7 @@ namespace Umbraco.Cms.Infrastructure.Packaging return contentType; } - private void UpdateContentTypesAllowedTemplates(IContentType contentType, - XElement allowedTemplatesElement, XElement defaultTemplateElement) + private void UpdateContentTypesAllowedTemplates(IContentType contentType, XElement allowedTemplatesElement, XElement defaultTemplateElement) { if (allowedTemplatesElement != null && allowedTemplatesElement.Elements("Template").Any()) { @@ -953,7 +935,7 @@ namespace Umbraco.Cms.Infrastructure.Packaging } private T UpdateContentTypesStructure(T contentType, XElement structureElement, IReadOnlyDictionary importedContentTypes, IContentTypeBaseService service) - where T : IContentTypeComposition + where T : IContentTypeComposition { var allowedChildren = contentType.AllowedContentTypes.ToList(); int sortOrder = allowedChildren.Any() ? allowedChildren.Last().SortOrder : 0; @@ -986,8 +968,8 @@ namespace Umbraco.Cms.Infrastructure.Packaging /// /// /// - private S FindContentTypeByAlias(string contentTypeAlias, IContentTypeBaseService typeService) - where S : IContentTypeComposition + private T FindContentTypeByAlias(string contentTypeAlias, IContentTypeBaseService typeService) + where T : IContentTypeComposition { var contentType = typeService.Get(contentTypeAlias); @@ -1207,12 +1189,7 @@ namespace Umbraco.Cms.Infrastructure.Packaging } private static bool DictionaryValueIsNew(IEnumerable translations, XElement valueElement) - { - return translations.All(t => - string.Compare(t.Language.IsoCode, valueElement.Attribute("LanguageCultureAlias").Value, - StringComparison.InvariantCultureIgnoreCase) != 0 - ); - } + => translations.All(t => string.Compare(t.Language.IsoCode, valueElement.Attribute("LanguageCultureAlias").Value, StringComparison.InvariantCultureIgnoreCase) != 0); private static void AddDictionaryTranslation(ICollection translations, XElement valueElement, IEnumerable languages) { @@ -1231,7 +1208,6 @@ namespace Umbraco.Cms.Infrastructure.Packaging #region Languages - /// /// Imports and saves the 'Languages' part of a package xml as a list of /// @@ -1289,7 +1265,6 @@ namespace Umbraco.Cms.Infrastructure.Packaging foreach (XElement macroPartialViewXml in macroPartialViewsElements) { var path = macroPartialViewXml.AttributeValue("path"); - if (path == null) { throw new InvalidOperationException("No path attribute found"); @@ -1325,24 +1300,28 @@ namespace Umbraco.Cms.Infrastructure.Packaging { useInEditor = bool.Parse(useInEditorElement.Value); } + var cacheDurationElement = macroElement.Element("refreshRate"); var cacheDuration = 0; if (cacheDurationElement != null && string.IsNullOrEmpty((string)cacheDurationElement) == false) { cacheDuration = int.Parse(cacheDurationElement.Value); } + var cacheByMemberElement = macroElement.Element("cacheByMember"); var cacheByMember = false; if (cacheByMemberElement != null && string.IsNullOrEmpty((string)cacheByMemberElement) == false) { cacheByMember = bool.Parse(cacheByMemberElement.Value); } + var cacheByPageElement = macroElement.Element("cacheByPage"); var cacheByPage = false; if (cacheByPageElement != null && string.IsNullOrEmpty((string)cacheByPageElement) == false) { cacheByPage = bool.Parse(cacheByPageElement.Value); } + var dontRenderElement = macroElement.Element("dontRender"); var dontRender = true; if (dontRenderElement != null && string.IsNullOrEmpty((string)dontRenderElement) == false) @@ -1386,6 +1365,7 @@ namespace Umbraco.Cms.Infrastructure.Packaging sortOrder++; } } + return macro; } @@ -1398,7 +1378,6 @@ namespace Umbraco.Cms.Infrastructure.Packaging foreach (XElement scriptXml in scriptElements) { var path = scriptXml.AttributeValue("path"); - if (path.IsNullOrWhiteSpace()) { continue; @@ -1462,7 +1441,6 @@ namespace Umbraco.Cms.Infrastructure.Packaging foreach (XElement n in stylesheetElements) { var stylesheetPath = n.Element("FileName")?.Value; - if (stylesheetPath.IsNullOrWhiteSpace()) { continue; @@ -1503,9 +1481,11 @@ namespace Umbraco.Cms.Infrastructure.Packaging sp = newProp; } } + sp.Alias = alias; sp.Value = prop.Element("Value")?.Value; } + _fileService.SaveStylesheet(s, userId); result.Add(s); } @@ -1518,9 +1498,7 @@ namespace Umbraco.Cms.Infrastructure.Packaging #region Templates public IEnumerable ImportTemplate(XElement templateElement, int userId) - { - return ImportTemplates(new[] { templateElement }, userId); - } + => ImportTemplates(new[] { templateElement }, userId); /// /// Imports and saves package xml as @@ -1570,6 +1548,7 @@ namespace Umbraco.Cms.Infrastructure.Packaging var existingTemplate = _fileService.GetTemplate(alias) as Template; var template = existingTemplate ?? new Template(_shortStringHelper, templateName, alias); template.Content = design; + if (masterElement != null && string.IsNullOrEmpty((string)masterElement) == false) { template.MasterTemplateAlias = masterElement.Value; @@ -1577,6 +1556,7 @@ namespace Umbraco.Cms.Infrastructure.Packaging if (masterTemplate != null) template.MasterTemplateId = new Lazy(() => masterTemplate.Id); } + templates.Add(template); }