Merge branch 'v9/dev' into v9/task/package-refactor

# Conflicts:
#	src/Umbraco.Web.BackOffice/Controllers/PackageController.cs
#	src/Umbraco.Web.BackOffice/Controllers/PackageInstallController.cs
This commit is contained in:
Shannon
2021-07-05 15:06:26 -06:00
141 changed files with 3993 additions and 1994 deletions

View File

@@ -194,11 +194,34 @@ namespace Umbraco.Cms.Core.Services.Implement
// TODO: what about culture?
var contentType = GetContentType(contentTypeAlias);
if (contentType == null)
throw new ArgumentException("No content type with that alias.", nameof(contentTypeAlias));
return Create(name, parentId, contentType, userId);
}
/// <summary>
/// Creates an <see cref="IContent"/> object of a specified content type.
/// </summary>
/// <remarks>This method simply returns a new, non-persisted, IContent without any identity. It
/// is intended as a shortcut to creating new content objects that does not invoke a save
/// operation against the database.
/// </remarks>
/// <param name="name">The name of the content object.</param>
/// <param name="parentId">The identifier of the parent, or -1.</param>
/// <param name="contentType">The content type of the content</param>
/// <param name="userId">The optional id of the user creating the content.</param>
/// <returns>The content object.</returns>
public IContent Create(string name, int parentId, IContentType contentType,
int userId = Constants.Security.SuperUserId)
{
if (contentType is null)
{
throw new ArgumentException("Content type must be specified", nameof(contentType));
}
var parent = parentId > 0 ? GetById(parentId) : null;
if (parentId > 0 && parent == null)
if (parentId > 0 && parent is null)
{
throw new ArgumentException("No content with that id.", nameof(parentId));
}
var content = new Content(name, parentId, contentType, userId);

View File

@@ -5,19 +5,18 @@ using System.Linq;
using System.Xml.Linq;
using System.Xml.XPath;
using Microsoft.Extensions.Logging;
using Umbraco.Extensions;
namespace Umbraco.Cms.Core.Services.Implement
{
// TODO: Convert all of this over to Niels K's localization framework one day
public class LocalizedTextService : ILocalizedTextService
{
private readonly ILogger<LocalizedTextService> _logger;
private readonly Lazy<LocalizedTextServiceFileSources> _fileSources;
private readonly IDictionary<CultureInfo, IDictionary<string, IDictionary<string, string>>> _dictionarySource;
private readonly IDictionary<CultureInfo, Lazy<XDocument>> _xmlSource;
private IDictionary<CultureInfo, IDictionary<string, IDictionary<string, string>>> _dictionarySource => _dictionarySourceLazy.Value;
private IDictionary<CultureInfo, IDictionary<string, string>> _noAreaDictionarySource => _noAreaDictionarySourceLazy.Value;
private readonly Lazy<IDictionary<CultureInfo, IDictionary<string, IDictionary<string, string>>>> _dictionarySourceLazy;
private readonly Lazy<IDictionary<CultureInfo, IDictionary<string, string>>> _noAreaDictionarySourceLazy;
private readonly char[] _splitter = new[] { '/' };
/// <summary>
/// Initializes with a file sources instance
/// </summary>
@@ -25,12 +24,50 @@ namespace Umbraco.Cms.Core.Services.Implement
/// <param name="logger"></param>
public LocalizedTextService(Lazy<LocalizedTextServiceFileSources> fileSources, ILogger<LocalizedTextService> logger)
{
if (logger == null) throw new ArgumentNullException("logger");
if (logger == null) throw new ArgumentNullException(nameof(logger));
_logger = logger;
if (fileSources == null) throw new ArgumentNullException("fileSources");
if (fileSources == null) throw new ArgumentNullException(nameof(fileSources));
_dictionarySourceLazy = new Lazy<IDictionary<CultureInfo, IDictionary<string, IDictionary<string, string>>>>(() => FileSourcesToAreaDictionarySources(fileSources.Value));
_noAreaDictionarySourceLazy = new Lazy<IDictionary<CultureInfo, IDictionary<string, string>>>(() => FileSourcesToNoAreaDictionarySources(fileSources.Value));
_fileSources = fileSources;
}
private IDictionary<CultureInfo, IDictionary<string, string>> FileSourcesToNoAreaDictionarySources(LocalizedTextServiceFileSources fileSources)
{
var xmlSources = fileSources.GetXmlSources();
return XmlSourceToNoAreaDictionary(xmlSources);
}
private IDictionary<CultureInfo, IDictionary<string, string>> XmlSourceToNoAreaDictionary(IDictionary<CultureInfo, Lazy<XDocument>> xmlSources)
{
var cultureNoAreaDictionary = new Dictionary<CultureInfo, IDictionary<string, string>>();
foreach (var xmlSource in xmlSources)
{
var noAreaAliasValue = GetNoAreaStoredTranslations(xmlSources, xmlSource.Key);
cultureNoAreaDictionary.Add(xmlSource.Key, noAreaAliasValue);
}
return cultureNoAreaDictionary;
}
private IDictionary<CultureInfo, IDictionary<string, IDictionary<string, string>>> FileSourcesToAreaDictionarySources(LocalizedTextServiceFileSources fileSources)
{
var xmlSources = fileSources.GetXmlSources();
return XmlSourcesToAreaDictionary(xmlSources);
}
private IDictionary<CultureInfo, IDictionary<string, IDictionary<string, string>>> XmlSourcesToAreaDictionary(IDictionary<CultureInfo, Lazy<XDocument>> xmlSources)
{
var cultureDictionary = new Dictionary<CultureInfo, IDictionary<string, IDictionary<string, string>>>();
foreach (var xmlSource in xmlSources)
{
var areaAliaValue = GetAreaStoredTranslations(xmlSources, xmlSource.Key);
cultureDictionary.Add(xmlSource.Key, areaAliaValue);
}
return cultureDictionary;
}
/// <summary>
/// Initializes with an XML source
/// </summary>
@@ -38,12 +75,15 @@ namespace Umbraco.Cms.Core.Services.Implement
/// <param name="logger"></param>
public LocalizedTextService(IDictionary<CultureInfo, Lazy<XDocument>> source, ILogger<LocalizedTextService> logger)
{
if (source == null) throw new ArgumentNullException("source");
if (logger == null) throw new ArgumentNullException("logger");
_xmlSource = source;
_logger = logger;
if (source == null) throw new ArgumentNullException(nameof(source));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_dictionarySourceLazy = new Lazy<IDictionary<CultureInfo, IDictionary<string, IDictionary<string, string>>>>(() => XmlSourcesToAreaDictionary(source));
_noAreaDictionarySourceLazy = new Lazy<IDictionary<CultureInfo, IDictionary<string, string>>>(() => XmlSourceToNoAreaDictionary(source));
}
/// <summary>
/// Initializes with a source of a dictionary of culture -> areas -> sub dictionary of keys/values
/// </summary>
@@ -51,37 +91,54 @@ namespace Umbraco.Cms.Core.Services.Implement
/// <param name="logger"></param>
public LocalizedTextService(IDictionary<CultureInfo, IDictionary<string, IDictionary<string, string>>> source, ILogger<LocalizedTextService> logger)
{
_dictionarySource = source ?? throw new ArgumentNullException(nameof(source));
var dictionarySource = source ?? throw new ArgumentNullException(nameof(source));
_dictionarySourceLazy = new Lazy<IDictionary<CultureInfo, IDictionary<string, IDictionary<string, string>>>>(() => dictionarySource);
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
var cultureNoAreaDictionary = new Dictionary<CultureInfo, IDictionary<string, string>>();
foreach (var cultureDictionary in dictionarySource)
{
var areaAliaValue = GetAreaStoredTranslations(source, cultureDictionary.Key);
var aliasValue = new Dictionary<string, string>();
foreach (var area in areaAliaValue)
{
foreach (var alias in area.Value)
{
if (!aliasValue.ContainsKey(alias.Key))
{
aliasValue.Add(alias.Key, alias.Value);
}
}
}
cultureNoAreaDictionary.Add(cultureDictionary.Key, aliasValue);
}
_noAreaDictionarySourceLazy = new Lazy<IDictionary<CultureInfo, IDictionary<string, string>>>(() => cultureNoAreaDictionary);
}
public string Localize(string key, CultureInfo culture, IDictionary<string, string> tokens = null)
{
if (culture == null) throw new ArgumentNullException(nameof(culture));
// TODO: Hack, see notes on ConvertToSupportedCultureWithRegionCode
culture = ConvertToSupportedCultureWithRegionCode(culture);
//This is what the legacy ui service did
if (string.IsNullOrEmpty(key))
return string.Empty;
var keyParts = key.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
var keyParts = key.Split(_splitter, StringSplitOptions.RemoveEmptyEntries);
var area = keyParts.Length > 1 ? keyParts[0] : null;
var alias = keyParts.Length > 1 ? keyParts[1] : keyParts[0];
return Localize(area, alias, culture, tokens);
}
public string Localize(string area, string alias, CultureInfo culture, IDictionary<string, string> tokens = null)
{
if (culture == null) throw new ArgumentNullException(nameof(culture));
var xmlSource = _xmlSource ?? (_fileSources != null
? _fileSources.Value.GetXmlSources()
: null);
//This is what the legacy ui service did
if (string.IsNullOrEmpty(alias))
return string.Empty;
if (xmlSource != null)
{
return GetFromXmlSource(xmlSource, culture, area, alias, tokens);
}
else
{
return GetFromDictionarySource(culture, area, alias, tokens);
}
// TODO: Hack, see notes on ConvertToSupportedCultureWithRegionCode
culture = ConvertToSupportedCultureWithRegionCode(culture);
return GetFromDictionarySource(culture, area, alias, tokens);
}
/// <summary>
@@ -89,76 +146,105 @@ namespace Umbraco.Cms.Core.Services.Implement
/// </summary>
public IDictionary<string, string> GetAllStoredValues(CultureInfo culture)
{
if (culture == null) throw new ArgumentNullException("culture");
if (culture == null) throw new ArgumentNullException(nameof(culture));
// TODO: Hack, see notes on ConvertToSupportedCultureWithRegionCode
culture = ConvertToSupportedCultureWithRegionCode(culture);
var result = new Dictionary<string, string>();
var xmlSource = _xmlSource ?? (_fileSources != null
? _fileSources.Value.GetXmlSources()
: null);
if (xmlSource != null)
if (_dictionarySource.ContainsKey(culture) == false)
{
if (xmlSource.ContainsKey(culture) == false)
_logger.LogWarning("The culture specified {Culture} was not found in any configured sources for this service", culture);
return new Dictionary<string, string>(0);
}
IDictionary<string, string> result = new Dictionary<string, string>();
//convert all areas + keys to a single key with a '/'
foreach (var area in _dictionarySource[culture])
{
foreach (var key in area.Value)
{
_logger.LogWarning("The culture specified {Culture} was not found in any configured sources for this service", culture);
return result;
}
// convert all areas + keys to a single key with a '/'
result = GetStoredTranslations(xmlSource, culture);
// merge with the English file in case there's keys in there that don't exist in the local file
var englishCulture = CultureInfo.GetCultureInfo("en-US");
if (culture.Equals(englishCulture) == false)
{
var englishResults = GetStoredTranslations(xmlSource, englishCulture);
foreach (var englishResult in englishResults.Where(englishResult => result.ContainsKey(englishResult.Key) == false))
var dictionaryKey = string.Format("{0}/{1}", area.Key, key.Key);
//i don't think it's possible to have duplicates because we're dealing with a dictionary in the first place, but we'll double check here just in case.
if (result.ContainsKey(dictionaryKey) == false)
{
result.Add(englishResult.Key, englishResult.Value);
result.Add(dictionaryKey, key.Value);
}
}
}
else
{
if (_dictionarySource.ContainsKey(culture) == false)
{
_logger.LogWarning("The culture specified {Culture} was not found in any configured sources for this service", culture);
return result;
}
// convert all areas + keys to a single key with a '/'
foreach (var area in _dictionarySource[culture])
{
foreach (var key in area.Value)
{
var dictionaryKey = string.Format("{0}/{1}", area.Key, key.Key);
// i don't think it's possible to have duplicates because we're dealing with a dictionary in the first place, but we'll double check here just in case.
if (result.ContainsKey(dictionaryKey) == false)
{
result.Add(dictionaryKey, key.Value);
}
}
}
}
return result;
}
private Dictionary<string, string> GetStoredTranslations(IDictionary<CultureInfo, Lazy<XDocument>> xmlSource, CultureInfo cult)
private Dictionary<string, IDictionary<string, string>> GetAreaStoredTranslations(IDictionary<CultureInfo, Lazy<XDocument>> xmlSource, CultureInfo cult)
{
var result = new Dictionary<string, string>();
var overallResult = new Dictionary<string, IDictionary<string, string>>(StringComparer.InvariantCulture);
var areas = xmlSource[cult].Value.XPathSelectElements("//area");
foreach (var area in areas)
{
var result = new Dictionary<string, string>(StringComparer.InvariantCulture);
var keys = area.XPathSelectElements("./key");
foreach (var key in keys)
{
var dictionaryKey = string.Format("{0}/{1}", (string)area.Attribute("alias"),
(string)key.Attribute("alias"));
var dictionaryKey =
(string)key.Attribute("alias");
//there could be duplicates if the language file isn't formatted nicely - which is probably the case for quite a few lang files
if (result.ContainsKey(dictionaryKey) == false)
result.Add(dictionaryKey, key.Value);
}
overallResult.Add(area.Attribute("alias").Value, result);
}
//Merge English Dictionary
var englishCulture = new CultureInfo("en-US");
if (!cult.Equals(englishCulture))
{
var enUS = xmlSource[englishCulture].Value.XPathSelectElements("//area");
foreach (var area in enUS)
{
IDictionary<string, string> result = new Dictionary<string, string>(StringComparer.InvariantCulture);
if (overallResult.ContainsKey(area.Attribute("alias").Value))
{
result = overallResult[area.Attribute("alias").Value];
}
var keys = area.XPathSelectElements("./key");
foreach (var key in keys)
{
var dictionaryKey =
(string)key.Attribute("alias");
//there could be duplicates if the language file isn't formatted nicely - which is probably the case for quite a few lang files
if (result.ContainsKey(dictionaryKey) == false)
result.Add(dictionaryKey, key.Value);
}
if (!overallResult.ContainsKey(area.Attribute("alias").Value))
{
overallResult.Add(area.Attribute("alias").Value, result);
}
}
}
return overallResult;
}
private Dictionary<string, string> GetNoAreaStoredTranslations(IDictionary<CultureInfo, Lazy<XDocument>> xmlSource, CultureInfo cult)
{
var result = new Dictionary<string, string>(StringComparer.InvariantCulture);
var keys = xmlSource[cult].Value.XPathSelectElements("//key");
foreach (var key in keys)
{
var dictionaryKey =
(string)key.Attribute("alias");
//there could be duplicates if the language file isn't formatted nicely - which is probably the case for quite a few lang files
if (result.ContainsKey(dictionaryKey) == false)
result.Add(dictionaryKey, key.Value);
}
//Merge English Dictionary
var englishCulture = new CultureInfo("en-US");
if (!cult.Equals(englishCulture))
{
var keysEn = xmlSource[englishCulture].Value.XPathSelectElements("//key");
foreach (var key in keys)
{
var dictionaryKey =
(string)key.Attribute("alias");
//there could be duplicates if the language file isn't formatted nicely - which is probably the case for quite a few lang files
if (result.ContainsKey(dictionaryKey) == false)
result.Add(dictionaryKey, key.Value);
@@ -166,6 +252,25 @@ namespace Umbraco.Cms.Core.Services.Implement
}
return result;
}
private Dictionary<string, IDictionary<string, string>> GetAreaStoredTranslations(IDictionary<CultureInfo, IDictionary<string, IDictionary<string, string>>> dictionarySource, CultureInfo cult)
{
var overallResult = new Dictionary<string, IDictionary<string, string>>(StringComparer.InvariantCulture);
var areaDict = dictionarySource[cult];
foreach (var area in areaDict)
{
var result = new Dictionary<string, string>(StringComparer.InvariantCulture);
var keys = area.Value.Keys;
foreach (var key in keys)
{
//there could be duplicates if the language file isn't formatted nicely - which is probably the case for quite a few lang files
if (result.ContainsKey(key) == false)
result.Add(key, area.Value[key]);
}
overallResult.Add(area.Key, result);
}
return overallResult;
}
/// <summary>
/// Returns a list of all currently supported cultures
@@ -173,11 +278,7 @@ namespace Umbraco.Cms.Core.Services.Implement
/// <returns></returns>
public IEnumerable<CultureInfo> GetSupportedCultures()
{
var xmlSource = _xmlSource ?? (_fileSources != null
? _fileSources.Value.GetXmlSources()
: null);
return xmlSource != null ? xmlSource.Keys : _dictionarySource.Keys;
return _dictionarySource.Keys;
}
/// <summary>
@@ -213,27 +314,25 @@ namespace Umbraco.Cms.Core.Services.Implement
return "[" + key + "]";
}
var cultureSource = _dictionarySource[culture];
string found;
if (area.IsNullOrWhiteSpace())
string found = null;
if (string.IsNullOrWhiteSpace(area))
{
found = cultureSource
.SelectMany(x => x.Value)
.Where(keyvals => keyvals.Key.InvariantEquals(key))
.Select(x => x.Value)
.FirstOrDefault();
_noAreaDictionarySource[culture].TryGetValue(key, out found);
}
else
{
found = cultureSource
.Where(areas => areas.Key.InvariantEquals(area))
.SelectMany(a => a.Value)
.Where(keyvals => keyvals.Key.InvariantEquals(key))
.Select(x => x.Value)
.FirstOrDefault();
if (_dictionarySource[culture].TryGetValue(area, out var areaDictionary))
{
areaDictionary.TryGetValue(key, out found);
}
if (found == null)
{
_noAreaDictionarySource[culture].TryGetValue(key, out found);
}
}
if (found != null)
{
return ParseTokens(found, tokens);
@@ -242,44 +341,6 @@ namespace Umbraco.Cms.Core.Services.Implement
//NOTE: Based on how legacy works, the default text does not contain the area, just the key
return "[" + key + "]";
}
private string GetFromXmlSource(IDictionary<CultureInfo, Lazy<XDocument>> xmlSource, CultureInfo culture, string area, string key, IDictionary<string, string> tokens)
{
if (xmlSource.ContainsKey(culture) == false)
{
_logger.LogWarning("The culture specified {Culture} was not found in any configured sources for this service", culture);
return "[" + key + "]";
}
var found = FindTranslation(xmlSource, culture, area, key);
if (found != null)
{
return ParseTokens(found.Value, tokens);
}
// Fall back to English by default if we can't find the key
found = FindTranslation(xmlSource, new CultureInfo("en-US"), area, key);
if (found != null)
return ParseTokens(found.Value, tokens);
// If it can't be found in either file, fall back to the default, showing just the key in square brackets
// NOTE: Based on how legacy works, the default text does not contain the area, just the key
return "[" + key + "]";
}
private XElement FindTranslation(IDictionary<CultureInfo, Lazy<XDocument>> xmlSource, CultureInfo culture, string area, string key)
{
var cultureSource = xmlSource[culture].Value;
var xpath = area.IsNullOrWhiteSpace()
? string.Format("//key [@alias = '{0}']", key)
: string.Format("//area [@alias = '{0}']/key [@alias = '{1}']", area, key);
var found = cultureSource.XPathSelectElement(xpath);
return found;
}
/// <summary>
/// Parses the tokens in the value
/// </summary>
@@ -303,11 +364,26 @@ namespace Umbraco.Cms.Core.Services.Implement
foreach (var token in tokens)
{
value = value.Replace(string.Format("{0}{1}{0}", "%", token.Key), token.Value);
value = value.Replace(string.Concat("%", token.Key, "%"), token.Value);
}
return value;
}
public IDictionary<string, IDictionary<string, string>> GetAllStoredValuesByAreaAndAlias(CultureInfo culture)
{
if (culture == null) throw new ArgumentNullException("culture");
// TODO: Hack, see notes on ConvertToSupportedCultureWithRegionCode
culture = ConvertToSupportedCultureWithRegionCode(culture);
if (_dictionarySource.ContainsKey(culture) == false)
{
_logger.LogWarning("The culture specified {Culture} was not found in any configured sources for this service", culture);
return new Dictionary<string, IDictionary<string, string>>(0);
}
return _dictionarySource[culture];
}
}
}