138 lines
4.9 KiB
C#
138 lines
4.9 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace wp2ghost
|
|
{
|
|
// =============================================================================
|
|
// LinkDecision — gespeicherte Entscheidung für einen Link
|
|
// =============================================================================
|
|
public class LinkDecision
|
|
{
|
|
[JsonPropertyName("link_text")]
|
|
public string LinkText { get; set; } = "";
|
|
|
|
[JsonPropertyName("link_href")]
|
|
public string LinkHref { get; set; } = "";
|
|
|
|
// Entscheidung: "keep" (beibehalten), "delete" (löschen), "edit" (ersetzt)
|
|
[JsonPropertyName("decision")]
|
|
public string Decision { get; set; } = "";
|
|
|
|
// Nur bei "edit": die neue URL
|
|
[JsonPropertyName("new_url")]
|
|
public string? NewUrl { get; set; }
|
|
|
|
[JsonPropertyName("timestamp")]
|
|
public DateTime Timestamp { get; set; } = DateTime.Now;
|
|
}
|
|
|
|
// =============================================================================
|
|
// LinkDecisionManager — merkt sich User-Entscheidungen für Links (Memory)
|
|
// =============================================================================
|
|
public static class LinkDecisionManager
|
|
{
|
|
private static Dictionary<string, LinkDecision> _decisions = new(StringComparer.OrdinalIgnoreCase);
|
|
private static string _decisionsPath = "";
|
|
|
|
// Key = Normalisierter Link (Text|Href)
|
|
private static string NormalizeKey(string linkText, string linkHref)
|
|
{
|
|
return $"{linkText.Trim().ToLowerInvariant()}|{linkHref.Trim().ToLowerInvariant()}";
|
|
}
|
|
|
|
public static void Init(string baseDir)
|
|
{
|
|
_decisionsPath = Path.Combine(baseDir, "link_decisions.json");
|
|
Load();
|
|
}
|
|
|
|
public static void Save()
|
|
{
|
|
if (string.IsNullOrEmpty(_decisionsPath)) return;
|
|
try
|
|
{
|
|
var options = new JsonSerializerOptions { WriteIndented = true };
|
|
File.WriteAllText(_decisionsPath, JsonSerializer.Serialize(_decisions.Values.ToList(), options));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($" [Link-Memory] Fehler beim Speichern der Entscheidungen: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
private static void Load()
|
|
{
|
|
if (!File.Exists(_decisionsPath)) return;
|
|
try
|
|
{
|
|
var json = File.ReadAllText(_decisionsPath);
|
|
var list = JsonSerializer.Deserialize<List<LinkDecision>>(json);
|
|
if (list != null)
|
|
{
|
|
foreach (var d in list)
|
|
{
|
|
string key = NormalizeKey(d.LinkText, d.LinkHref);
|
|
_decisions[key] = d;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($" [Link-Memory] Fehler beim Laden der Entscheidungen: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Speichert eine User-Entscheidung für einen Link.
|
|
/// </summary>
|
|
public static void SaveDecision(string linkText, string linkHref, string decision, string? newUrl = null)
|
|
{
|
|
string key = NormalizeKey(linkText, linkHref);
|
|
_decisions[key] = new LinkDecision
|
|
{
|
|
LinkText = linkText,
|
|
LinkHref = linkHref,
|
|
Decision = decision,
|
|
NewUrl = newUrl
|
|
};
|
|
Save();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Versucht eine frühere Entscheidung für diesen Link zu finden.
|
|
/// </summary>
|
|
public static LinkDecision? GetPreviousDecision(string linkText, string linkHref)
|
|
{
|
|
string key = NormalizeKey(linkText, linkHref);
|
|
if (_decisions.TryGetValue(key, out var d))
|
|
return d;
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt die Anzahl gespeicherter Entscheidungen zurück (für Info-Anzeige).
|
|
/// </summary>
|
|
public static int Count => _decisions.Count;
|
|
|
|
public static void ShowInfo()
|
|
{
|
|
if (_decisions.Count > 0)
|
|
{
|
|
Console.WriteLine($"\nGespeicherte Link-Entscheidungen: {_decisions.Count}");
|
|
foreach (var kvp in _decisions.Take(20))
|
|
{
|
|
var d = kvp.Value;
|
|
string info = d.Decision switch
|
|
{
|
|
"delete" => "gelöscht",
|
|
"edit" => $"ersetzt durch '{d.NewUrl}'",
|
|
_ => "beibehalten"
|
|
};
|
|
Console.WriteLine($" \"{d.LinkText}\" -> {info}");
|
|
}
|
|
if (_decisions.Count > 20)
|
|
Console.WriteLine($" ... und {_decisions.Count - 20} weitere");
|
|
}
|
|
}
|
|
}
|
|
}
|