|
| 1 | +using Microsoft.Extensions.Logging; |
| 2 | +using NPoco; |
| 3 | +using Umbraco.Cms.Core; |
| 4 | +using Umbraco.Cms.Core.Models; |
| 5 | +using Umbraco.Cms.Core.Models.Editors; |
| 6 | +using Umbraco.Cms.Core.Serialization; |
| 7 | +using Umbraco.Cms.Core.Services; |
| 8 | +using Umbraco.Cms.Core.Web; |
| 9 | +using Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_15_0_0.LocalLinks; |
| 10 | +using Umbraco.Cms.Infrastructure.Persistence; |
| 11 | +using Umbraco.Cms.Infrastructure.Persistence.Dtos; |
| 12 | +using Umbraco.Extensions; |
| 13 | + |
| 14 | +namespace Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_15_0_0; |
| 15 | + |
| 16 | +public class ConvertLocalLinks : MigrationBase |
| 17 | +{ |
| 18 | + private readonly IUmbracoContextFactory _umbracoContextFactory; |
| 19 | + private readonly IContentTypeService _contentTypeService; |
| 20 | + private readonly ILogger<ConvertLocalLinks> _logger; |
| 21 | + private readonly IDataTypeService _dataTypeService; |
| 22 | + private readonly ILanguageService _languageService; |
| 23 | + private readonly IJsonSerializer _jsonSerializer; |
| 24 | + private readonly LocalLinkProcessor _localLinkProcessor; |
| 25 | + private readonly IMediaTypeService _mediaTypeService; |
| 26 | + |
| 27 | + public ConvertLocalLinks( |
| 28 | + IMigrationContext context, |
| 29 | + IUmbracoContextFactory umbracoContextFactory, |
| 30 | + IContentTypeService contentTypeService, |
| 31 | + ILogger<ConvertLocalLinks> logger, |
| 32 | + IDataTypeService dataTypeService, |
| 33 | + ILanguageService languageService, |
| 34 | + IJsonSerializer jsonSerializer, |
| 35 | + LocalLinkProcessor localLinkProcessor, |
| 36 | + IMediaTypeService mediaTypeService) |
| 37 | + : base(context) |
| 38 | + { |
| 39 | + _umbracoContextFactory = umbracoContextFactory; |
| 40 | + _contentTypeService = contentTypeService; |
| 41 | + _logger = logger; |
| 42 | + _dataTypeService = dataTypeService; |
| 43 | + _languageService = languageService; |
| 44 | + _jsonSerializer = jsonSerializer; |
| 45 | + _localLinkProcessor = localLinkProcessor; |
| 46 | + _mediaTypeService = mediaTypeService; |
| 47 | + } |
| 48 | + |
| 49 | + protected override void Migrate() |
| 50 | + { |
| 51 | + IEnumerable<string> propertyEditorAliases = _localLinkProcessor.GetSupportedPropertyEditorAliases(); |
| 52 | + |
| 53 | + using UmbracoContextReference umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext(); |
| 54 | + var languagesById = _languageService.GetAllAsync().GetAwaiter().GetResult() |
| 55 | + .ToDictionary(language => language.Id); |
| 56 | + |
| 57 | + IEnumerable<IContentType> allContentTypes = _contentTypeService.GetAll(); |
| 58 | + IEnumerable<IPropertyType> contentPropertyTypes = allContentTypes |
| 59 | + .SelectMany(ct => ct.PropertyTypes); |
| 60 | + |
| 61 | + IMediaType[] allMediaTypes = _mediaTypeService.GetAll().ToArray(); |
| 62 | + IEnumerable<IPropertyType> mediaPropertyTypes = allMediaTypes |
| 63 | + .SelectMany(ct => ct.PropertyTypes); |
| 64 | + |
| 65 | + var relevantPropertyEditors = |
| 66 | + contentPropertyTypes.Concat(mediaPropertyTypes).DistinctBy(pt => pt.Id) |
| 67 | + .Where(pt => propertyEditorAliases.Contains(pt.PropertyEditorAlias)) |
| 68 | + .GroupBy(pt => pt.PropertyEditorAlias) |
| 69 | + .ToDictionary(group => group.Key, group => group.ToArray()); |
| 70 | + |
| 71 | + |
| 72 | + foreach (var propertyEditorAlias in propertyEditorAliases) |
| 73 | + { |
| 74 | + if (relevantPropertyEditors.TryGetValue(propertyEditorAlias, out IPropertyType[]? propertyTypes) is false) |
| 75 | + { |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + _logger.LogInformation( |
| 80 | + "Migration starting for all properties of type: {propertyEditorAlias}", |
| 81 | + propertyEditorAlias); |
| 82 | + if (ProcessPropertyTypes(propertyTypes, languagesById)) |
| 83 | + { |
| 84 | + _logger.LogInformation( |
| 85 | + "Migration succeeded for all properties of type: {propertyEditorAlias}", |
| 86 | + propertyEditorAlias); |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + _logger.LogError( |
| 91 | + "Migration failed for one or more properties of type: {propertyEditorAlias}", |
| 92 | + propertyEditorAlias); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private bool ProcessPropertyTypes(IPropertyType[] propertyTypes, IDictionary<int, ILanguage> languagesById) |
| 98 | + { |
| 99 | + foreach (IPropertyType propertyType in propertyTypes) |
| 100 | + { |
| 101 | + IDataType dataType = _dataTypeService.GetAsync(propertyType.DataTypeKey).GetAwaiter().GetResult() |
| 102 | + ?? throw new InvalidOperationException("The data type could not be fetched."); |
| 103 | + |
| 104 | + IDataValueEditor valueEditor = dataType.Editor?.GetValueEditor() |
| 105 | + ?? throw new InvalidOperationException( |
| 106 | + "The data type value editor could not be fetched."); |
| 107 | + |
| 108 | + Sql<ISqlContext> sql = Sql() |
| 109 | + .Select<PropertyDataDto>() |
| 110 | + .From<PropertyDataDto>() |
| 111 | + .InnerJoin<ContentVersionDto>() |
| 112 | + .On<PropertyDataDto, ContentVersionDto>((propertyData, contentVersion) => |
| 113 | + propertyData.VersionId == contentVersion.Id) |
| 114 | + .LeftJoin<DocumentVersionDto>() |
| 115 | + .On<ContentVersionDto, DocumentVersionDto>((contentVersion, documentVersion) => |
| 116 | + contentVersion.Id == documentVersion.Id) |
| 117 | + .Where<PropertyDataDto, ContentVersionDto, DocumentVersionDto>( |
| 118 | + (propertyData, contentVersion, documentVersion) => |
| 119 | + (contentVersion.Current == true || documentVersion.Published == true) |
| 120 | + && propertyData.PropertyTypeId == propertyType.Id); |
| 121 | + |
| 122 | + List<PropertyDataDto> propertyDataDtos = Database.Fetch<PropertyDataDto>(sql); |
| 123 | + if (propertyDataDtos.Any() is false) |
| 124 | + { |
| 125 | + continue; |
| 126 | + } |
| 127 | + |
| 128 | + foreach (PropertyDataDto propertyDataDto in propertyDataDtos) |
| 129 | + { |
| 130 | + if (ProcessPropertyDataDto(propertyDataDto, propertyType, languagesById, valueEditor)) |
| 131 | + { |
| 132 | + Database.Update(propertyDataDto); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return true; |
| 138 | + } |
| 139 | + |
| 140 | + private bool ProcessPropertyDataDto(PropertyDataDto propertyDataDto, IPropertyType propertyType, |
| 141 | + IDictionary<int, ILanguage> languagesById, IDataValueEditor valueEditor) |
| 142 | + { |
| 143 | + // NOTE: some old property data DTOs can have variance defined, even if the property type no longer varies |
| 144 | + var culture = propertyType.VariesByCulture() |
| 145 | + && propertyDataDto.LanguageId.HasValue |
| 146 | + && languagesById.TryGetValue(propertyDataDto.LanguageId.Value, out ILanguage? language) |
| 147 | + ? language.IsoCode |
| 148 | + : null; |
| 149 | + |
| 150 | + if (culture is null && propertyType.VariesByCulture()) |
| 151 | + { |
| 152 | + // if we end up here, the property DTO is bound to a language that no longer exists. this is an error scenario, |
| 153 | + // and we can't really handle it in any other way than logging; in all likelihood this is an old property version, |
| 154 | + // and it won't cause any runtime issues |
| 155 | + _logger.LogWarning( |
| 156 | + " - property data with id: {propertyDataId} references a language that does not exist - language id: {languageId} (property type: {propertyTypeName}, id: {propertyTypeId}, alias: {propertyTypeAlias})", |
| 157 | + propertyDataDto.Id, |
| 158 | + propertyDataDto.LanguageId, |
| 159 | + propertyType.Name, |
| 160 | + propertyType.Id, |
| 161 | + propertyType.Alias); |
| 162 | + return false; |
| 163 | + } |
| 164 | + |
| 165 | + var segment = propertyType.VariesBySegment() ? propertyDataDto.Segment : null; |
| 166 | + var property = new Property(propertyType); |
| 167 | + property.SetValue(propertyDataDto.Value, culture, segment); |
| 168 | + var toEditorValue = valueEditor.ToEditor(property, culture, segment); |
| 169 | + |
| 170 | + _localLinkProcessor.ProcessToEditorValue(toEditorValue); |
| 171 | + |
| 172 | + var editorValue = _jsonSerializer.Serialize(toEditorValue); |
| 173 | + var dbValue = valueEditor.FromEditor(new ContentPropertyData(editorValue, null), null); |
| 174 | + if (dbValue is not string stringValue || stringValue.DetectIsJson() is false) |
| 175 | + { |
| 176 | + _logger.LogError( |
| 177 | + " - value editor did not yield a valid JSON string as FromEditor value property data with id: {propertyDataId} (property type: {propertyTypeName}, id: {propertyTypeId}, alias: {propertyTypeAlias})", |
| 178 | + propertyDataDto.Id, |
| 179 | + propertyType.Name, |
| 180 | + propertyType.Id, |
| 181 | + propertyType.Alias); |
| 182 | + return false; |
| 183 | + } |
| 184 | + |
| 185 | + propertyDataDto.TextValue = stringValue; |
| 186 | + return true; |
| 187 | + } |
| 188 | +} |
0 commit comments