mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-07-03 21:02:52 +01:00
Rework parental ratings (#12615)
Some checks are pending
CodeQL / Analyze (csharp) (push) Waiting to run
OpenAPI / OpenAPI - HEAD (push) Waiting to run
OpenAPI / OpenAPI - BASE (push) Waiting to run
OpenAPI / OpenAPI - Difference (push) Blocked by required conditions
OpenAPI / OpenAPI - Publish Unstable Spec (push) Blocked by required conditions
OpenAPI / OpenAPI - Publish Stable Spec (push) Blocked by required conditions
Tests / run-tests (macos-latest) (push) Waiting to run
Tests / run-tests (ubuntu-latest) (push) Waiting to run
Tests / run-tests (windows-latest) (push) Waiting to run
Project Automation / Project board (push) Waiting to run
Merge Conflict Labeler / Labeling (push) Waiting to run
Some checks are pending
CodeQL / Analyze (csharp) (push) Waiting to run
OpenAPI / OpenAPI - HEAD (push) Waiting to run
OpenAPI / OpenAPI - BASE (push) Waiting to run
OpenAPI / OpenAPI - Difference (push) Blocked by required conditions
OpenAPI / OpenAPI - Publish Unstable Spec (push) Blocked by required conditions
OpenAPI / OpenAPI - Publish Stable Spec (push) Blocked by required conditions
Tests / run-tests (macos-latest) (push) Waiting to run
Tests / run-tests (ubuntu-latest) (push) Waiting to run
Tests / run-tests (windows-latest) (push) Waiting to run
Project Automation / Project board (push) Waiting to run
Merge Conflict Labeler / Labeling (push) Waiting to run
This commit is contained in:
@@ -49,12 +49,12 @@ namespace Jellyfin.Server.Migrations
|
||||
typeof(Routines.RemoveDownloadImagesInAdvance),
|
||||
typeof(Routines.MigrateAuthenticationDb),
|
||||
typeof(Routines.FixPlaylistOwner),
|
||||
typeof(Routines.MigrateRatingLevels),
|
||||
typeof(Routines.AddDefaultCastReceivers),
|
||||
typeof(Routines.UpdateDefaultPluginRepository),
|
||||
typeof(Routines.FixAudioData),
|
||||
typeof(Routines.RemoveDuplicatePlaylistChildren),
|
||||
typeof(Routines.MigrateLibraryDb),
|
||||
typeof(Routines.MigrateRatingLevels),
|
||||
typeof(Routines.MoveTrickplayFiles),
|
||||
};
|
||||
|
||||
|
||||
@@ -1,36 +1,33 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using Emby.Server.Implementations.Data;
|
||||
using MediaBrowser.Controller;
|
||||
using System.Linq;
|
||||
using Jellyfin.Database.Implementations;
|
||||
using MediaBrowser.Model.Globalization;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Server.Migrations.Routines
|
||||
{
|
||||
/// <summary>
|
||||
/// Migrate rating levels to new rating level system.
|
||||
/// Migrate rating levels.
|
||||
/// </summary>
|
||||
internal class MigrateRatingLevels : IMigrationRoutine
|
||||
internal class MigrateRatingLevels : IDatabaseMigrationRoutine
|
||||
{
|
||||
private const string DbFilename = "library.db";
|
||||
private readonly ILogger<MigrateRatingLevels> _logger;
|
||||
private readonly IServerApplicationPaths _applicationPaths;
|
||||
private readonly IDbContextFactory<JellyfinDbContext> _provider;
|
||||
private readonly ILocalizationManager _localizationManager;
|
||||
|
||||
public MigrateRatingLevels(
|
||||
IServerApplicationPaths applicationPaths,
|
||||
IDbContextFactory<JellyfinDbContext> provider,
|
||||
ILoggerFactory loggerFactory,
|
||||
ILocalizationManager localizationManager)
|
||||
{
|
||||
_applicationPaths = applicationPaths;
|
||||
_provider = provider;
|
||||
_localizationManager = localizationManager;
|
||||
_logger = loggerFactory.CreateLogger<MigrateRatingLevels>();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public Guid Id => Guid.Parse("{73DAB92A-178B-48CD-B05B-FE18733ACDC8}");
|
||||
public Guid Id => Guid.Parse("{98724538-EB11-40E3-931A-252C55BDDE7A}");
|
||||
|
||||
/// <inheritdoc/>
|
||||
public string Name => "MigrateRatingLevels";
|
||||
@@ -41,54 +38,37 @@ namespace Jellyfin.Server.Migrations.Routines
|
||||
/// <inheritdoc/>
|
||||
public void Perform()
|
||||
{
|
||||
var dbPath = Path.Combine(_applicationPaths.DataPath, DbFilename);
|
||||
|
||||
// Back up the database before modifying any entries
|
||||
for (int i = 1; ; i++)
|
||||
{
|
||||
var bakPath = string.Format(CultureInfo.InvariantCulture, "{0}.bak{1}", dbPath, i);
|
||||
if (!File.Exists(bakPath))
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Copy(dbPath, bakPath);
|
||||
_logger.LogInformation("Library database backed up to {BackupPath}", bakPath);
|
||||
break;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Cannot make a backup of {Library} at path {BackupPath}", DbFilename, bakPath);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Migrate parental rating strings to new levels
|
||||
_logger.LogInformation("Recalculating parental rating levels based on rating string.");
|
||||
using var connection = new SqliteConnection($"Filename={dbPath}");
|
||||
connection.Open();
|
||||
using (var transaction = connection.BeginTransaction())
|
||||
using var context = _provider.CreateDbContext();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
var ratings = context.BaseItems.AsNoTracking().Select(e => e.OfficialRating).Distinct();
|
||||
foreach (var rating in ratings)
|
||||
{
|
||||
var queryResult = connection.Query("SELECT DISTINCT OfficialRating FROM TypedBaseItems");
|
||||
foreach (var entry in queryResult)
|
||||
if (string.IsNullOrEmpty(rating))
|
||||
{
|
||||
if (!entry.TryGetString(0, out var ratingString) || string.IsNullOrEmpty(ratingString))
|
||||
{
|
||||
connection.Execute("UPDATE TypedBaseItems SET InheritedParentalRatingValue = NULL WHERE OfficialRating IS NULL OR OfficialRating='';");
|
||||
}
|
||||
else
|
||||
{
|
||||
var ratingValue = _localizationManager.GetRatingLevel(ratingString)?.ToString(CultureInfo.InvariantCulture) ?? "NULL";
|
||||
|
||||
using var statement = connection.PrepareStatement("UPDATE TypedBaseItems SET InheritedParentalRatingValue = @Value WHERE OfficialRating = @Rating;");
|
||||
statement.TryBind("@Value", ratingValue);
|
||||
statement.TryBind("@Rating", ratingString);
|
||||
statement.ExecuteNonQuery();
|
||||
}
|
||||
int? value = null;
|
||||
context.BaseItems
|
||||
.Where(e => e.OfficialRating == null || e.OfficialRating == string.Empty)
|
||||
.ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingValue, value));
|
||||
context.BaseItems
|
||||
.Where(e => e.OfficialRating == null || e.OfficialRating == string.Empty)
|
||||
.ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingSubValue, value));
|
||||
}
|
||||
else
|
||||
{
|
||||
var ratingValue = _localizationManager.GetRatingScore(rating);
|
||||
var score = ratingValue?.Score;
|
||||
var subScore = ratingValue?.SubScore;
|
||||
context.BaseItems
|
||||
.Where(e => e.OfficialRating == rating)
|
||||
.ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingValue, score));
|
||||
context.BaseItems
|
||||
.Where(e => e.OfficialRating == rating)
|
||||
.ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingSubValue, subScore));
|
||||
}
|
||||
|
||||
transaction.Commit();
|
||||
}
|
||||
|
||||
transaction.Commit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,7 +112,8 @@ namespace Jellyfin.Server.Migrations.Routines
|
||||
{
|
||||
Id = entry.GetGuid(1),
|
||||
InternalId = entry.GetInt64(0),
|
||||
MaxParentalAgeRating = policy.MaxParentalRating,
|
||||
MaxParentalRatingScore = policy.MaxParentalRating,
|
||||
MaxParentalRatingSubScore = null,
|
||||
EnableUserPreferenceAccess = policy.EnableUserPreferenceAccess,
|
||||
RemoteClientBitrateLimit = policy.RemoteClientBitrateLimit,
|
||||
InvalidLoginAttemptCount = policy.InvalidLoginAttemptCount,
|
||||
|
||||
Reference in New Issue
Block a user