Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ConvertLocalLinks migration and add a new migration in case the o… #17814

Open
wants to merge 1 commit into
base: v15/dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,6 @@ protected virtual void DefinePlan()
To<V_15_0_0.ConvertRichTextEditorProperties>("{37875E80-5CDD-42FF-A21A-7D4E3E23E0ED}");
To<V_15_0_0.ConvertLocalLinks>("{42E44F9E-7262-4269-922D-7310CB48E724}");
To<V_15_1_0.RebuildCacheMigration>("{7B51B4DE-5574-4484-993E-05D12D9ED703}");
To<V_15_1_0.FixConvertLocalLinks>("{F3D3EF46-1B1F-47DB-B437-7D573EEDEB98}");
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Collections.Concurrent;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NPoco;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Editors;
using Umbraco.Cms.Core.Scoping;
Expand All @@ -26,7 +28,9 @@
private readonly LocalLinkProcessor _localLinkProcessor;
private readonly IMediaTypeService _mediaTypeService;
private readonly ICoreScopeProvider _coreScopeProvider;
private readonly LocalLinkMigrationTracker _linkMigrationTracker;

[Obsolete("Use non obsoleted contructor instead")]
public ConvertLocalLinks(
IMigrationContext context,
IUmbracoContextFactory umbracoContextFactory,
Expand All @@ -37,18 +41,46 @@
IJsonSerializer jsonSerializer,
LocalLinkProcessor localLinkProcessor,
IMediaTypeService mediaTypeService,
ICoreScopeProvider coreScopeProvider)
ICoreScopeProvider coreScopeProvider,
LocalLinkMigrationTracker linkMigrationTracker)
: base(context)
{
_umbracoContextFactory = umbracoContextFactory;
_contentTypeService = contentTypeService;
_logger = logger;
_dataTypeService = dataTypeService;
_languageService = languageService;
_jsonSerializer = jsonSerializer;
_localLinkProcessor = localLinkProcessor;
_mediaTypeService = mediaTypeService;
_coreScopeProvider = coreScopeProvider;
_linkMigrationTracker = linkMigrationTracker;
}

Check notice on line 58 in src/Umbraco.Infrastructure/Migrations/Upgrade/V_15_0_0/ConvertLocalLinks.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v15/dev)

ℹ Getting worse: Constructor Over-Injection

ConvertLocalLinks increases from 10 to 11 arguments, threshold = 5. This constructor has too many arguments, indicating an object with low cohesion or missing function argument abstraction. Avoid adding more arguments.

public ConvertLocalLinks(
IMigrationContext context,
IUmbracoContextFactory umbracoContextFactory,
IContentTypeService contentTypeService,
ILogger<ConvertLocalLinks> logger,
IDataTypeService dataTypeService,
ILanguageService languageService,
IJsonSerializer jsonSerializer,
LocalLinkProcessor localLinkProcessor,
IMediaTypeService mediaTypeService,
ICoreScopeProvider coreScopeProvider)
: this(
context,
umbracoContextFactory,
contentTypeService,
logger,
dataTypeService,
languageService,
jsonSerializer,
localLinkProcessor,
mediaTypeService,
coreScopeProvider,
StaticServiceProvider.Instance.GetRequiredService<LocalLinkMigrationTracker>())
{

Check notice on line 83 in src/Umbraco.Infrastructure/Migrations/Upgrade/V_15_0_0/ConvertLocalLinks.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v15/dev)

ℹ New issue: Constructor Over-Injection

ConvertLocalLinks has 10 arguments, threshold = 5. This constructor has too many arguments, indicating an object with low cohesion or missing function argument abstraction. Avoid adding more arguments.
}

protected override void Migrate()
Expand Down Expand Up @@ -97,6 +129,8 @@
propertyEditorAlias);
}
}

_linkMigrationTracker.MarkFixedMigrationRan();
}

private bool ProcessPropertyTypes(IPropertyType[] propertyTypes, IDictionary<int, ILanguage> languagesById)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public void Compose(IUmbracoBuilder builder)
builder.Services.AddSingleton<ITypedLocalLinkProcessor, LocalLinkBlockGridProcessor>();
builder.Services.AddSingleton<ITypedLocalLinkProcessor, LocalLinkRteProcessor>();
builder.Services.AddSingleton<LocalLinkProcessor>();
builder.Services.AddSingleton<LocalLinkProcessorForFaultyLinks>();
builder.Services.AddSingleton<LocalLinkMigrationTracker>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_15_0_0.LocalLinks;

public class LocalLinkMigrationTracker
{
public bool HasFixedMigrationRun { get; private set; }

public void MarkFixedMigrationRan() => HasFixedMigrationRun = true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public string ProcessStringValue(string input)
string newTagHref;
if (tag.Udi is not null)
{
newTagHref = $" type=\"{tag.Udi.EntityType}\" "
+ tag.TagHref.Replace(tag.Udi.ToString(), tag.Udi.Guid.ToString());
newTagHref = tag.TagHref.Replace(tag.Udi.ToString(), tag.Udi.Guid.ToString())
+ $"\" type=\"{tag.Udi.EntityType}";
}
else if (tag.IntId is not null)
{
Expand All @@ -55,8 +55,8 @@ public string ProcessStringValue(string input)
continue;
}

newTagHref = $" type=\"{conversionResult.Value.EntityType}\" "
+ tag.TagHref.Replace(tag.IntId.Value.ToString(), conversionResult.Value.Key.ToString());
newTagHref = tag.TagHref.Replace(tag.IntId.Value.ToString(), conversionResult.Value.Key.ToString())
+ $"\" type=\"{conversionResult.Value.EntityType}";
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System.Text.RegularExpressions;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services;

namespace Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_15_0_0.LocalLinks;

[Obsolete("Will be removed in V18")]
public class LocalLinkProcessorForFaultyLinks
{
private readonly IIdKeyMap _idKeyMap;
private readonly IEnumerable<ITypedLocalLinkProcessor> _localLinkProcessors;
private const string LocalLinkLocation = "__LOCALLINKLOCATION__";
private const string TypeAttributeLocation = "__TYPEATTRIBUTELOCATION__";

internal static readonly Regex FaultyHrefPattern = new(
@"<a (?<faultyHref>href=['""] ?(?<typeAttribute> type=*?['""][^'""]*?['""] )?(?<localLink>\/{localLink:[a-fA-F0-9-]+}['""])).*?>",
RegexOptions.IgnoreCase | RegexOptions.Singleline);

public LocalLinkProcessorForFaultyLinks(
IIdKeyMap idKeyMap,
IEnumerable<ITypedLocalLinkProcessor> localLinkProcessors)
{
_idKeyMap = idKeyMap;
_localLinkProcessors = localLinkProcessors;
}

public IEnumerable<string> GetSupportedPropertyEditorAliases() =>
_localLinkProcessors.SelectMany(p => p.PropertyEditorAliases);

public bool ProcessToEditorValue(object? editorValue)
{
ITypedLocalLinkProcessor? processor =
_localLinkProcessors.FirstOrDefault(p => p.PropertyEditorValueType == editorValue?.GetType());

return processor is not null && processor.Process.Invoke(editorValue, ProcessToEditorValue, ProcessStringValue);
}

public string ProcessStringValue(string input)
{
MatchCollection faultyTags = FaultyHrefPattern.Matches(input);

foreach (Match fullTag in faultyTags)
{
var newValue =
fullTag.Value.Replace(fullTag.Groups["typeAttribute"].Value, LocalLinkLocation)
.Replace(fullTag.Groups["localLink"].Value, TypeAttributeLocation)
.Replace(LocalLinkLocation, fullTag.Groups["localLink"].Value)
.Replace(TypeAttributeLocation, fullTag.Groups["typeAttribute"].Value);
input = input.Replace(fullTag.Value, newValue);
}

return input;
}

private (Guid Key, string EntityType)? CreateIntBasedKeyType(int id)
{
// very old data, best effort replacement
Attempt<Guid> documentAttempt = _idKeyMap.GetKeyForId(id, UmbracoObjectTypes.Document);
if (documentAttempt.Success)
{
return (Key: documentAttempt.Result, EntityType: UmbracoObjectTypes.Document.ToString());
}

Attempt<Guid> mediaAttempt = _idKeyMap.GetKeyForId(id, UmbracoObjectTypes.Media);
if (mediaAttempt.Success)
{
return (Key: mediaAttempt.Result, EntityType: UmbracoObjectTypes.Media.ToString());
}

return null;
}
}
Loading
Loading