| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Globalization; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Threading; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using Jellyfin.Data.Enums; |
| | | 8 | | using MediaBrowser.Controller.Entities; |
| | | 9 | | using MediaBrowser.Controller.Library; |
| | | 10 | | using Microsoft.Extensions.Logging; |
| | | 11 | | |
| | | 12 | | namespace Emby.Server.Implementations.Library.Validators; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Class PeopleValidator. |
| | | 16 | | /// </summary> |
| | | 17 | | public class PeopleValidator |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// The library manager. |
| | | 21 | | /// </summary> |
| | | 22 | | private readonly ILibraryManager _libraryManager; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// The logger. |
| | | 26 | | /// </summary> |
| | | 27 | | private readonly ILogger<PeopleValidator> _logger; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Initializes a new instance of the <see cref="PeopleValidator" /> class. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="libraryManager">The library manager.</param> |
| | | 33 | | /// <param name="logger">The logger.</param> |
| | | 34 | | public PeopleValidator(ILibraryManager libraryManager, ILogger<PeopleValidator> logger) |
| | | 35 | | { |
| | 0 | 36 | | _libraryManager = libraryManager; |
| | 0 | 37 | | _logger = logger; |
| | 0 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Validates the people. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="progress">The progress.</param> |
| | | 44 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 45 | | /// <returns>Task.</returns> |
| | | 46 | | public async Task Run(IProgress<double> progress, CancellationToken cancellationToken) |
| | | 47 | | { |
| | | 48 | | // Before the refresh below walks them: a credit no item maps to any more stands for nothing, |
| | | 49 | | // and while it is there the person it names cannot reach the dead-person sweep either. |
| | 0 | 50 | | var numOrphaned = _libraryManager.DeleteOrphanedCredits(); |
| | 0 | 51 | | if (numOrphaned > 0) |
| | | 52 | | { |
| | 0 | 53 | | _logger.LogInformation("Deleted {Amount} credits no item maps to", numOrphaned); |
| | | 54 | | } |
| | | 55 | | |
| | 0 | 56 | | var names = _libraryManager.GetPeopleNames(new InternalPeopleQuery()); |
| | 0 | 57 | | var existingPersonIds = _libraryManager.GetItemIds(new InternalItemsQuery |
| | 0 | 58 | | { |
| | 0 | 59 | | IncludeItemTypes = [BaseItemKind.Person] |
| | 0 | 60 | | }).ToHashSet(); |
| | | 61 | | |
| | 0 | 62 | | var (newNames, deadIds) = PartitionCreditsByPersonId(names, _libraryManager.GetPersonId, existingPersonIds); |
| | | 63 | | |
| | 0 | 64 | | var numComplete = 0; |
| | 0 | 65 | | var count = names.Count; |
| | 0 | 66 | | var refreshed = 0; |
| | | 67 | | |
| | 0 | 68 | | foreach (var name in names) |
| | | 69 | | { |
| | 0 | 70 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 71 | | |
| | | 72 | | try |
| | | 73 | | { |
| | 0 | 74 | | var item = _libraryManager.GetOrCreatePerson(name); |
| | 0 | 75 | | var isNew = !existingPersonIds.Contains(item.Id); |
| | 0 | 76 | | var neverRefreshed = item.DateLastRefreshed == default; |
| | | 77 | | |
| | 0 | 78 | | if (isNew || neverRefreshed) |
| | | 79 | | { |
| | 0 | 80 | | await item.RefreshMetadata(cancellationToken).ConfigureAwait(false); |
| | 0 | 81 | | refreshed++; |
| | | 82 | | } |
| | 0 | 83 | | } |
| | 0 | 84 | | catch (OperationCanceledException) |
| | | 85 | | { |
| | | 86 | | // Don't clutter the log |
| | 0 | 87 | | throw; |
| | | 88 | | } |
| | 0 | 89 | | catch (Exception ex) |
| | | 90 | | { |
| | 0 | 91 | | _logger.LogError(ex, "Error refreshing {PersonName}", name); |
| | 0 | 92 | | } |
| | | 93 | | |
| | 0 | 94 | | numComplete++; |
| | 0 | 95 | | double percent = numComplete; |
| | 0 | 96 | | percent /= count; |
| | 0 | 97 | | percent *= 100; |
| | | 98 | | |
| | 0 | 99 | | progress.Report(percent); |
| | 0 | 100 | | } |
| | | 101 | | |
| | 0 | 102 | | _logger.LogInformation( |
| | 0 | 103 | | "Refreshed metadata for {RefreshedCount} people out of {TotalCount} total, {NewCount} of which had no item y |
| | 0 | 104 | | refreshed, |
| | 0 | 105 | | count, |
| | 0 | 106 | | newNames.Count); |
| | | 107 | | |
| | | 108 | | // A person somebody locked is theirs, not ours, however little the library still credits them. |
| | 0 | 109 | | var deadEntities = deadIds |
| | 0 | 110 | | .Select(_libraryManager.GetItemById) |
| | 0 | 111 | | .OfType<Person>() |
| | 0 | 112 | | .Where(item => !item.IsLocked) |
| | 0 | 113 | | .ToList(); |
| | | 114 | | |
| | 0 | 115 | | foreach (var item in deadEntities) |
| | | 116 | | { |
| | 0 | 117 | | _logger.LogInformation("Deleting dead {ItemType} {ItemId} {ItemName}", item.GetType().Name, item.Id.ToString |
| | | 118 | | } |
| | | 119 | | |
| | 0 | 120 | | _libraryManager.DeleteItemsUnsafeFast(deadEntities, deleteSourceFiles: true); |
| | | 121 | | |
| | 0 | 122 | | progress.Report(100); |
| | 0 | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Splits the person items into the ones a credit still calls for and the ones nothing does. |
| | | 127 | | /// </summary> |
| | | 128 | | /// <param name="creditNames">Every name credited on an item, from the people table.</param> |
| | | 129 | | /// <param name="getPersonId">Maps a credit name to the id its person item has.</param> |
| | | 130 | | /// <param name="existingPersonIds">The ids of the person items that exist.</param> |
| | | 131 | | /// <returns>The credits needing an item, and the ids of the items nothing credits.</returns> |
| | | 132 | | internal static (List<string> NewNames, List<Guid> DeadIds) PartitionCreditsByPersonId( |
| | | 133 | | IReadOnlyList<string> creditNames, |
| | | 134 | | Func<string, Guid> getPersonId, |
| | | 135 | | IReadOnlySet<Guid> existingPersonIds) |
| | | 136 | | { |
| | 11 | 137 | | ArgumentNullException.ThrowIfNull(creditNames); |
| | 11 | 138 | | ArgumentNullException.ThrowIfNull(getPersonId); |
| | 11 | 139 | | ArgumentNullException.ThrowIfNull(existingPersonIds); |
| | | 140 | | |
| | 11 | 141 | | var newNames = new List<string>(); |
| | 11 | 142 | | var liveIds = new HashSet<Guid>(); |
| | | 143 | | |
| | 56 | 144 | | foreach (var name in creditNames) |
| | | 145 | | { |
| | 17 | 146 | | var personId = getPersonId(name); |
| | | 147 | | |
| | | 148 | | // Distinct credit names can normalize onto one id; only the first of them needs an item. |
| | 17 | 149 | | if (liveIds.Add(personId) && !existingPersonIds.Contains(personId)) |
| | | 150 | | { |
| | 5 | 151 | | newNames.Add(name); |
| | | 152 | | } |
| | | 153 | | } |
| | | 154 | | |
| | 11 | 155 | | var deadIds = existingPersonIds.Where(id => !liveIds.Contains(id)).ToList(); |
| | | 156 | | |
| | 11 | 157 | | return (newNames, deadIds); |
| | | 158 | | } |
| | | 159 | | } |