Skip to content

Commit b09619f

Browse files
authored
Merge pull request #11561 from umbraco/v8/feature/version-history-cleanup-ui
V8: Added UI to configure history cleanup per doc type
2 parents d0a0a0a + 8e49b91 commit b09619f

File tree

29 files changed

+437
-353
lines changed

29 files changed

+437
-353
lines changed

src/Umbraco.Core/Composing/CompositionExtensions/Services.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static Composition ComposeServices(this Composition composition)
3434

3535
composition.RegisterUnique<ContentService>();
3636
composition.RegisterUnique<IContentService>(factory => factory.GetInstance<ContentService>());
37-
composition.RegisterUnique<IContentVersionCleanupService>(factory => factory.GetInstance<ContentService>());
37+
composition.RegisterUnique<IContentVersionService>(factory => factory.GetInstance<ContentService>());
3838
composition.RegisterUnique<IContentVersionCleanupPolicy, DefaultContentVersionCleanupPolicy>();
3939

4040
composition.RegisterUnique<IUserService, UserService>();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Runtime.Serialization;
2+
3+
namespace Umbraco.Core.Models.ContentEditing
4+
{
5+
[DataContract(Name = "historyCleanup", Namespace = "")]
6+
public class HistoryCleanup
7+
{
8+
[DataMember(Name = "preventCleanup")]
9+
public bool PreventCleanup { get; set; }
10+
11+
[DataMember(Name = "keepAllVersionsNewerThanDays")]
12+
public int? KeepAllVersionsNewerThanDays { get;set; }
13+
14+
[DataMember(Name = "keepLatestVersionPerDayForDays")]
15+
public int? KeepLatestVersionPerDayForDays { get;set; }
16+
}
17+
}
18+

src/Umbraco.Core/Models/ContentType.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Reflection;
55
using System.Runtime.Serialization;
6+
using Umbraco.Core.Models.ContentEditing;
67

78
namespace Umbraco.Core.Models
89
{
@@ -26,6 +27,7 @@ public class ContentType : ContentTypeCompositionBase, IContentType
2627
public ContentType(int parentId) : base(parentId)
2728
{
2829
_allowedTemplates = new List<ITemplate>();
30+
HistoryCleanup = new HistoryCleanup();
2931
}
3032

3133

@@ -39,6 +41,7 @@ public ContentType(IContentType parent, string alias)
3941
: base(parent, alias)
4042
{
4143
_allowedTemplates = new List<ITemplate>();
44+
HistoryCleanup = new HistoryCleanup();
4245
}
4346

4447
/// <inheritdoc />
@@ -47,6 +50,8 @@ public ContentType(IContentType parent, string alias)
4750
/// <inheritdoc />
4851
public override bool SupportsPublishing => SupportsPublishingConst;
4952

53+
54+
5055
//Custom comparer for enumerable
5156
private static readonly DelegateEqualityComparer<IEnumerable<ITemplate>> TemplateComparer = new DelegateEqualityComparer<IEnumerable<ITemplate>>(
5257
(templates, enumerable) => templates.UnsortedSequenceEqual(enumerable),
@@ -93,6 +98,8 @@ public IEnumerable<ITemplate> AllowedTemplates
9398
}
9499
}
95100

101+
public HistoryCleanup HistoryCleanup { get; set; }
102+
96103
/// <summary>
97104
/// Determines if AllowedTemplates contains templateId
98105
/// </summary>

src/Umbraco.Core/Models/IContentType.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using Umbraco.Core.Models.ContentEditing;
23

34
namespace Umbraco.Core.Models
45
{
@@ -17,6 +18,10 @@ public interface IContentType : IContentTypeComposition
1718
/// </summary>
1819
IEnumerable<ITemplate> AllowedTemplates { get; set; }
1920

21+
/// <summary>
22+
/// Gets or Sets the history cleanup configuration
23+
/// </summary>
24+
HistoryCleanup HistoryCleanup { get; set; }
2025
/// <summary>
2126
/// Determines if AllowedTemplates contains templateId
2227
/// </summary>

src/Umbraco.Core/Persistence/Repositories/Implement/ContentTypeCommonRepository.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Umbraco.Core.Cache;
66
using Umbraco.Core.Exceptions;
77
using Umbraco.Core.Models;
8+
using Umbraco.Core.Models.ContentEditing;
89
using Umbraco.Core.Persistence.Dtos;
910
using Umbraco.Core.Persistence.Factories;
1011
using Umbraco.Core.Scoping;
@@ -109,6 +110,7 @@ private IEnumerable<IContentTypeComposition> GetAllTypesInternal()
109110
MapTemplates(contentTypes);
110111
MapComposition(contentTypes);
111112
MapGroupsAndProperties(contentTypes);
113+
MapHistoryCleanup(contentTypes);
112114

113115
// finalize
114116
foreach (var contentType in contentTypes.Values)
@@ -119,6 +121,35 @@ private IEnumerable<IContentTypeComposition> GetAllTypesInternal()
119121
return contentTypes.Values;
120122
}
121123

124+
private void MapHistoryCleanup(Dictionary<int, IContentTypeComposition> contentTypes)
125+
{
126+
// get templates
127+
var sql1 = Sql()
128+
.Select<ContentVersionCleanupPolicyDto>()
129+
.From<ContentVersionCleanupPolicyDto>()
130+
.OrderBy<ContentVersionCleanupPolicyDto>(x => x.ContentTypeId);
131+
132+
var contentVersionCleanupPolicyDtos = Database.Fetch<ContentVersionCleanupPolicyDto>(sql1);
133+
134+
var contentVersionCleanupPolicyDictionary =
135+
contentVersionCleanupPolicyDtos.ToDictionary(x => x.ContentTypeId);
136+
foreach (var c in contentTypes.Values)
137+
{
138+
if (!(c is ContentType contentType)) continue;
139+
140+
var historyCleanup = new HistoryCleanup();
141+
142+
if (contentVersionCleanupPolicyDictionary.TryGetValue(contentType.Id, out var versionCleanup))
143+
{
144+
historyCleanup.PreventCleanup = versionCleanup.PreventCleanup;
145+
historyCleanup.KeepAllVersionsNewerThanDays = versionCleanup.KeepAllVersionsNewerThanDays;
146+
historyCleanup.KeepLatestVersionPerDayForDays = versionCleanup.KeepLatestVersionPerDayForDays;
147+
}
148+
149+
contentType.HistoryCleanup = historyCleanup;
150+
}
151+
}
152+
122153
private void MapTemplates(Dictionary<int, IContentTypeComposition> contentTypes)
123154
{
124155
// get templates
@@ -145,7 +176,7 @@ private void MapTemplates(Dictionary<int, IContentTypeComposition> contentTypes)
145176
templateDtoIx++;
146177
if (!templates.TryGetValue(allowedDto.TemplateNodeId, out var template)) continue;
147178
allowedTemplates.Add(template);
148-
179+
149180
if (allowedDto.IsDefault)
150181
defaultTemplateId = template.Id;
151182
}

src/Umbraco.Core/Persistence/Repositories/Implement/ContentTypeRepository.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ protected override void PersistNewItem(IContentType entity)
236236

237237
PersistNewBaseContentType(entity);
238238
PersistTemplates(entity, false);
239+
PersistHistoryCleanup(entity);
239240

240241
entity.ResetDirtyProperties();
241242
}
@@ -289,8 +290,22 @@ protected override void PersistUpdatedItem(IContentType entity)
289290

290291
PersistUpdatedBaseContentType(entity);
291292
PersistTemplates(entity, true);
293+
PersistHistoryCleanup(entity);
292294

293295
entity.ResetDirtyProperties();
294296
}
297+
298+
private void PersistHistoryCleanup(IContentType entity)
299+
{
300+
ContentVersionCleanupPolicyDto dto = new ContentVersionCleanupPolicyDto()
301+
{
302+
ContentTypeId = entity.Id,
303+
Updated = DateTime.Now,
304+
PreventCleanup = entity.HistoryCleanup.PreventCleanup,
305+
KeepAllVersionsNewerThanDays = entity.HistoryCleanup.KeepAllVersionsNewerThanDays,
306+
KeepLatestVersionPerDayForDays = entity.HistoryCleanup.KeepLatestVersionPerDayForDays,
307+
};
308+
Database.InsertOrUpdate(dto);
309+
}
295310
}
296311
}

src/Umbraco.Core/Persistence/Repositories/Implement/DocumentRepository.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,11 @@ public override IEnumerable<IContent> GetAllVersionsSlim(int nodeId, int skip, i
266266
.OrderByDescending<ContentVersionDto>(x => x.Current)
267267
.AndByDescending<ContentVersionDto>(x => x.VersionDate);
268268

269-
return MapDtosToContent(Database.Fetch<DocumentDto>(sql), true,
269+
var pageIndex = skip / take;
270+
271+
return MapDtosToContent(Database.Page<DocumentDto>(pageIndex+1, take, sql).Items, true,
270272
// load bare minimum, need variants though since this is used to rollback with variants
271-
false, false, false, true).Skip(skip).Take(take);
273+
false, false, false, true);
272274
}
273275

274276
public override IContent GetVersion(int versionId)

src/Umbraco.Core/Services/IContentVersionCleanupService.cs renamed to src/Umbraco.Core/Services/IContentVersionService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Umbraco.Core.Services
66
{
7-
public interface IContentVersionCleanupService
7+
public interface IContentVersionService
88
{
99
/// <summary>
1010
/// Removes historic content versions according to a policy.

src/Umbraco.Core/Services/Implement/ContentService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace Umbraco.Core.Services.Implement
1919
/// <summary>
2020
/// Implements the content service.
2121
/// </summary>
22-
public class ContentService : RepositoryService, IContentService, IContentVersionCleanupService
22+
public class ContentService : RepositoryService, IContentService, IContentVersionService
2323
{
2424
private readonly IDocumentRepository _documentRepository;
2525
private readonly IEntityRepository _entityRepository;

src/Umbraco.Core/Umbraco.Core.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@
163163
<Compile Include="Models\ContentDataIntegrityReport.cs" />
164164
<Compile Include="Models\ContentDataIntegrityReportEntry.cs" />
165165
<Compile Include="Models\ContentDataIntegrityReportOptions.cs" />
166+
<Compile Include="Models\ContentEditing\HistoryCleanup.cs" />
166167
<Compile Include="Models\ContentVersionCleanupPolicySettings.cs" />
167168
<Compile Include="Models\Identity\ExternalLogin.cs" />
168169
<Compile Include="Models\Identity\IExternalLogin.cs" />
@@ -198,7 +199,7 @@
198199
<Compile Include="PropertyEditors\EyeDropperColorPickerConfiguration.cs" />
199200
<Compile Include="PropertyEditors\ComplexPropertyEditorContentEventHandler.cs" />
200201
<Compile Include="Services\IContentVersionCleanupPolicy.cs" />
201-
<Compile Include="Services\IContentVersionCleanupService.cs" />
202+
<Compile Include="Services\IContentVersionService.cs" />
202203
<Compile Include="Services\IIconService.cs" />
203204
<Compile Include="Services\Implement\DefaultContentVersionCleanupPolicy.cs" />
204205
<Compile Include="Services\Implement\InstallationService.cs" />

0 commit comments

Comments
 (0)