mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-04-05 21:01:35 +08:00
Adding Build Archive command for Blogs
--HG-- branch : 1.x
This commit is contained in:
parent
f6a174a7d9
commit
61ea8eaa72
@ -13,7 +13,6 @@ using Orchard.Core.Navigation.Services;
|
|||||||
using Orchard.Settings;
|
using Orchard.Settings;
|
||||||
using Orchard.Core.Title.Models;
|
using Orchard.Core.Title.Models;
|
||||||
using Orchard.UI.Navigation;
|
using Orchard.UI.Navigation;
|
||||||
using Orchard.Utility;
|
|
||||||
|
|
||||||
namespace Orchard.Blogs.Commands {
|
namespace Orchard.Blogs.Commands {
|
||||||
public class BlogCommands : DefaultOrchardCommandHandler {
|
public class BlogCommands : DefaultOrchardCommandHandler {
|
||||||
@ -23,6 +22,7 @@ namespace Orchard.Blogs.Commands {
|
|||||||
private readonly IMenuService _menuService;
|
private readonly IMenuService _menuService;
|
||||||
private readonly ISiteService _siteService;
|
private readonly ISiteService _siteService;
|
||||||
private readonly INavigationManager _navigationManager;
|
private readonly INavigationManager _navigationManager;
|
||||||
|
private readonly IArchiveService _archiveService;
|
||||||
|
|
||||||
public BlogCommands(
|
public BlogCommands(
|
||||||
IContentManager contentManager,
|
IContentManager contentManager,
|
||||||
@ -30,13 +30,15 @@ namespace Orchard.Blogs.Commands {
|
|||||||
IBlogService blogService,
|
IBlogService blogService,
|
||||||
IMenuService menuService,
|
IMenuService menuService,
|
||||||
ISiteService siteService,
|
ISiteService siteService,
|
||||||
INavigationManager navigationManager) {
|
INavigationManager navigationManager,
|
||||||
|
IArchiveService archiveService) {
|
||||||
_contentManager = contentManager;
|
_contentManager = contentManager;
|
||||||
_membershipService = membershipService;
|
_membershipService = membershipService;
|
||||||
_blogService = blogService;
|
_blogService = blogService;
|
||||||
_menuService = menuService;
|
_menuService = menuService;
|
||||||
_siteService = siteService;
|
_siteService = siteService;
|
||||||
_navigationManager = navigationManager;
|
_navigationManager = navigationManager;
|
||||||
|
_archiveService = archiveService;
|
||||||
}
|
}
|
||||||
|
|
||||||
[OrchardSwitch]
|
[OrchardSwitch]
|
||||||
@ -158,5 +160,19 @@ namespace Orchard.Blogs.Commands {
|
|||||||
Context.Output.WriteLine(T("Import feed completed."));
|
Context.Output.WriteLine(T("Import feed completed."));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[CommandName("blog build archive")]
|
||||||
|
[CommandHelp("blog build archive /BlogId:<id> \r\n\t" + "Rebuild the archive information for the blog specified by <id>")]
|
||||||
|
[OrchardSwitches("BlogId")]
|
||||||
|
public void BuilddArchive() {
|
||||||
|
|
||||||
|
var blog = _blogService.Get(BlogId, VersionOptions.Latest);
|
||||||
|
|
||||||
|
if (blog == null) {
|
||||||
|
Context.Output.WriteLine(T("Blog not found with specified Id: {0}", BlogId));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_archiveService.RebuildArchive(blog.As<BlogPart>());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -21,6 +21,10 @@
|
|||||||
</UpgradeBackupLocation>
|
</UpgradeBackupLocation>
|
||||||
<TargetFrameworkProfile />
|
<TargetFrameworkProfile />
|
||||||
<UseIISExpress>false</UseIISExpress>
|
<UseIISExpress>false</UseIISExpress>
|
||||||
|
<IISExpressSSLPort />
|
||||||
|
<IISExpressAnonymousAuthentication />
|
||||||
|
<IISExpressWindowsAuthentication />
|
||||||
|
<IISExpressUseClassicPipelineMode />
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
<DebugSymbols>true</DebugSymbols>
|
<DebugSymbols>true</DebugSymbols>
|
||||||
@ -89,6 +93,7 @@
|
|||||||
<Compile Include="Routing\IRsdConstraint.cs" />
|
<Compile Include="Routing\IRsdConstraint.cs" />
|
||||||
<Compile Include="Routing\IBlogPathConstraint.cs" />
|
<Compile Include="Routing\IBlogPathConstraint.cs" />
|
||||||
<Compile Include="Security\BlogAuthorizationEventHandler.cs" />
|
<Compile Include="Security\BlogAuthorizationEventHandler.cs" />
|
||||||
|
<Compile Include="Services\ArchiveService.cs" />
|
||||||
<Compile Include="Services\BlogService.cs" />
|
<Compile Include="Services\BlogService.cs" />
|
||||||
<Compile Include="Controllers\BlogController.cs" />
|
<Compile Include="Controllers\BlogController.cs" />
|
||||||
<Compile Include="Models\BlogPart.cs" />
|
<Compile Include="Models\BlogPart.cs" />
|
||||||
@ -99,6 +104,7 @@
|
|||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="Routes.cs" />
|
<Compile Include="Routes.cs" />
|
||||||
<Compile Include="Services\BlogPostService.cs" />
|
<Compile Include="Services\BlogPostService.cs" />
|
||||||
|
<Compile Include="Services\IArchiveService.cs" />
|
||||||
<Compile Include="Services\IBlogPostService.cs" />
|
<Compile Include="Services\IBlogPostService.cs" />
|
||||||
<Compile Include="Services\IBlogService.cs" />
|
<Compile Include="Services\IBlogService.cs" />
|
||||||
<Compile Include="Services\XmlRpcHandler.cs" />
|
<Compile Include="Services\XmlRpcHandler.cs" />
|
||||||
|
@ -0,0 +1,69 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using Orchard.Blogs.Models;
|
||||||
|
using Orchard.ContentManagement;
|
||||||
|
using Orchard.Core.Common.Models;
|
||||||
|
using Orchard.Data;
|
||||||
|
|
||||||
|
namespace Orchard.Blogs.Services {
|
||||||
|
public class ArchiveService : IArchiveService {
|
||||||
|
private readonly IRepository<BlogPartArchiveRecord> _blogArchiveRepository;
|
||||||
|
private readonly IContentManager _contentManager;
|
||||||
|
private readonly IWorkContextAccessor _workContextAccessor;
|
||||||
|
|
||||||
|
public ArchiveService(
|
||||||
|
IRepository<BlogPartArchiveRecord> blogArchiveRepository,
|
||||||
|
IContentManager contentManager,
|
||||||
|
IWorkContextAccessor workContextAccessor) {
|
||||||
|
_blogArchiveRepository = blogArchiveRepository;
|
||||||
|
_contentManager = contentManager;
|
||||||
|
_workContextAccessor = workContextAccessor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RebuildArchive(BlogPart blogPart) {
|
||||||
|
|
||||||
|
var first = _contentManager.Query<BlogPostPart>().OrderBy<CommonPartRecord>(x => x.CreatedUtc).Slice(0, 1).FirstOrDefault();
|
||||||
|
|
||||||
|
if (first == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var last = _contentManager.Query<BlogPostPart>().OrderByDescending<CommonPartRecord>(x => x.CreatedUtc).Slice(0, 1).FirstOrDefault();
|
||||||
|
|
||||||
|
DateTime? start = DateTime.MaxValue;
|
||||||
|
if (first.As<CommonPart>() != null) {
|
||||||
|
start = first.As<CommonPart>().CreatedUtc;
|
||||||
|
}
|
||||||
|
|
||||||
|
DateTime? end = DateTime.MinValue;
|
||||||
|
if (last.As<CommonPart>() != null) {
|
||||||
|
end = first.As<CommonPart>().CreatedUtc;
|
||||||
|
}
|
||||||
|
|
||||||
|
// delete previous archive records
|
||||||
|
foreach (var record in _blogArchiveRepository.Table.Where(x => x.BlogPart == blogPart.Record)) {
|
||||||
|
_blogArchiveRepository.Delete(record);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!start.HasValue || !end.HasValue) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// get the time zone for the current request
|
||||||
|
var timeZone = _workContextAccessor.GetContext().CurrentTimeZone;
|
||||||
|
|
||||||
|
for (int year = start.Value.Year - 1; year <= end.Value.Year + 1; year++) {
|
||||||
|
for (int month = 1; month <= 12; month++) {
|
||||||
|
int yearCopy = year;
|
||||||
|
int monthCopy = month;
|
||||||
|
var from = TimeZoneInfo.ConvertTimeFromUtc(new DateTime(yearCopy, monthCopy, 1), timeZone);
|
||||||
|
var to = from.AddMonths(1);
|
||||||
|
var count = _contentManager.Query<BlogPostPart>().Where<CommonPartRecord>(x => x.CreatedUtc.Value >= from && x.CreatedUtc.Value < to).Count();
|
||||||
|
|
||||||
|
var newArchiveRecord = new BlogPartArchiveRecord { BlogPart = blogPart.Record, Year = from.Year, Month = from.Month, PostCount = count };
|
||||||
|
_blogArchiveRepository.Create(newArchiveRecord);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -23,7 +23,8 @@ namespace Orchard.Blogs.Services {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ContentItem Get(int id, VersionOptions versionOptions) {
|
public ContentItem Get(int id, VersionOptions versionOptions) {
|
||||||
return _contentManager.Get(id, versionOptions);
|
var blogPart = _contentManager.Get<BlogPart>(id, versionOptions);
|
||||||
|
return blogPart == null ? null : blogPart.ContentItem;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<BlogPart> Get() {
|
public IEnumerable<BlogPart> Get() {
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
using Orchard.Blogs.Models;
|
||||||
|
|
||||||
|
namespace Orchard.Blogs.Services {
|
||||||
|
public interface IArchiveService : IDependency {
|
||||||
|
void RebuildArchive(BlogPart blog);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user