diff --git a/src/Orchard.Web/Modules/Orchard.ImportExport/Commands/ImportExportCommands.cs b/src/Orchard.Web/Modules/Orchard.ImportExport/Commands/ImportExportCommands.cs index f4d21e5ca..f772b47fc 100644 --- a/src/Orchard.Web/Modules/Orchard.ImportExport/Commands/ImportExportCommands.cs +++ b/src/Orchard.Web/Modules/Orchard.ImportExport/Commands/ImportExportCommands.cs @@ -1,29 +1,49 @@ using System; using System.IO; using System.Linq; -using System.Xml.Linq; using Orchard.Commands; -using Orchard.ContentManagement; -using Orchard.ContentManagement.Aspects; -using Orchard.Core.Common.Models; -using Orchard.Core.Navigation.Models; +using Orchard.ContentManagement.MetaData; +using Orchard.ImportExport.Models; using Orchard.ImportExport.Services; using Orchard.Security; -using Orchard.Core.Navigation.Services; using Orchard.Settings; -using Orchard.Core.Title.Models; namespace Orchard.ImportExport.Commands { public class ImportExportCommands : DefaultOrchardCommandHandler { private readonly IImportExportService _importExportService; + private readonly IContentDefinitionManager _contentDefinitionManager; + private readonly ISiteService _siteService; + private readonly IMembershipService _membershipService; + private readonly IAuthenticationService _authenticationService; - public ImportExportCommands(IImportExportService importExportService) { + public ImportExportCommands( + IImportExportService importExportService, + IContentDefinitionManager contentDefinitionManager, + ISiteService siteService, + IMembershipService membershipService, + IAuthenticationService authenticationService) { _importExportService = importExportService; + _contentDefinitionManager = contentDefinitionManager; + _siteService = siteService; + _membershipService = membershipService; + _authenticationService = authenticationService; } [OrchardSwitch] public string Filename { get; set; } - + [OrchardSwitch] + public string Types { get; set; } + [OrchardSwitch] + public bool Metadata { get; set; } + [OrchardSwitch] + public bool Data { get; set; } + [OrchardSwitch] + public string Steps { get; set; } + [OrchardSwitch] + public string Version { get; set; } + [OrchardSwitch] + public bool SiteSettings { get; set; } + [CommandName("import file")] [CommandHelp("import file /Filename: \r\n\t" + "Imports the content of a file.")] [OrchardSwitches("Filename")] @@ -44,5 +64,43 @@ namespace Orchard.ImportExport.Commands { Context.Output.WriteLine(T("Import running...")); } + [CommandName("export file")] + [CommandHelp("export file [/Types:, ... ,] [/Metadata:true|false] [/Data:true|false] [/Version:Published|Draft] [/SiteSettings:true|false] [/Steps:, ... ,]\r\n\t" + "Create an export file according to the specified options.")] + [OrchardSwitches("Types,Metadata,Data,Version,SiteSettings,Steps")] + public void ExportFile() { + // impersonate the Site owner + var superUser = _siteService.GetSiteSettings().SuperUser; + var owner = _membershipService.GetUser(superUser); + _authenticationService.SetAuthenticatedUserForRequest(owner); + + var versionOption = VersionHistoryOptions.Published; + + if (!String.IsNullOrEmpty(Version) && !Enum.TryParse(Version, out versionOption)) { + Context.Output.WriteLine(T("Invalid version option")); + return; + } + + var enteredTypes = (Types ?? String.Empty).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); + + var exportTypes = _contentDefinitionManager.ListTypeDefinitions() + .Where(contentType => enteredTypes.Contains(contentType.Name)) + .Select(contentType => contentType.Name); + + var enteredSteps = (Steps ?? String.Empty).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); + + var exportOptions = new ExportOptions { + ExportMetadata = Metadata, + ExportData = Data, + VersionHistoryOptions = versionOption, + ExportSiteSettings = SiteSettings, + CustomSteps = enteredSteps + }; + + Context.Output.WriteLine(T("Export starting...")); + + var exportFilePath = _importExportService.Export(exportTypes, exportOptions); + + Context.Output.WriteLine(T("Export completed at {0}", exportFilePath)); + } } } \ No newline at end of file