2022-06-23 00:35:12 +00:00
|
|
|
module MyWebLog.Maintenance
|
|
|
|
|
|
|
|
open System
|
|
|
|
open System.IO
|
|
|
|
open Microsoft.Extensions.DependencyInjection
|
|
|
|
open MyWebLog.Data
|
2022-08-21 22:56:18 +00:00
|
|
|
open NodaTime
|
2022-06-23 00:35:12 +00:00
|
|
|
|
|
|
|
/// Create the web log information
|
|
|
|
let private doCreateWebLog (args : string[]) (sp : IServiceProvider) = task {
|
|
|
|
|
|
|
|
let data = sp.GetRequiredService<IData> ()
|
|
|
|
|
|
|
|
let timeZone =
|
|
|
|
let local = TimeZoneInfo.Local.Id
|
|
|
|
match TimeZoneInfo.Local.HasIanaId with
|
|
|
|
| true -> local
|
|
|
|
| false ->
|
|
|
|
match TimeZoneInfo.TryConvertWindowsIdToIanaId local with
|
|
|
|
| true, ianaId -> ianaId
|
|
|
|
| false, _ -> raise <| TimeZoneNotFoundException $"Cannot find IANA timezone for {local}"
|
|
|
|
|
|
|
|
// Create the web log
|
2023-12-16 17:24:45 +00:00
|
|
|
let webLogId = WebLogId.Create()
|
|
|
|
let userId = WebLogUserId.Create()
|
2023-12-16 03:46:12 +00:00
|
|
|
let homePageId = PageId.Create()
|
2022-07-02 00:59:21 +00:00
|
|
|
let slug = Handlers.Upload.makeSlug args[2]
|
2022-06-23 00:35:12 +00:00
|
|
|
|
2022-07-17 02:17:57 +00:00
|
|
|
// If this is the first web log being created, the user will be an installation admin; otherwise, they will be an
|
|
|
|
// admin just over their web log
|
2023-12-16 03:46:12 +00:00
|
|
|
let! webLogs = data.WebLog.All()
|
2022-07-17 02:17:57 +00:00
|
|
|
let accessLevel = if List.isEmpty webLogs then Administrator else WebLogAdmin
|
|
|
|
|
2022-07-18 03:10:30 +00:00
|
|
|
do! data.WebLog.Add
|
2023-12-16 19:05:45 +00:00
|
|
|
{ WebLog.Empty with
|
2022-07-19 00:05:10 +00:00
|
|
|
Id = webLogId
|
|
|
|
Name = args[2]
|
|
|
|
Slug = slug
|
|
|
|
UrlBase = args[1]
|
2023-12-16 17:24:45 +00:00
|
|
|
DefaultPage = string homePageId
|
2022-07-19 00:05:10 +00:00
|
|
|
TimeZone = timeZone
|
2022-06-23 00:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the admin user
|
2022-08-21 22:56:18 +00:00
|
|
|
let now = Noda.now ()
|
|
|
|
let user =
|
2023-12-16 19:05:45 +00:00
|
|
|
{ WebLogUser.Empty with
|
2022-08-21 22:56:18 +00:00
|
|
|
Id = userId
|
|
|
|
WebLogId = webLogId
|
|
|
|
Email = args[3]
|
|
|
|
FirstName = "Admin"
|
|
|
|
LastName = "User"
|
|
|
|
PreferredName = "Admin"
|
|
|
|
AccessLevel = accessLevel
|
|
|
|
CreatedOn = now
|
|
|
|
}
|
|
|
|
do! data.WebLogUser.Add { user with PasswordHash = Handlers.User.createPasswordHash user args[4] }
|
2022-06-23 00:35:12 +00:00
|
|
|
|
|
|
|
// Create the default home page
|
2022-07-18 03:10:30 +00:00
|
|
|
do! data.Page.Add
|
2023-12-16 19:05:45 +00:00
|
|
|
{ Page.Empty with
|
2022-07-19 00:05:10 +00:00
|
|
|
Id = homePageId
|
|
|
|
WebLogId = webLogId
|
|
|
|
AuthorId = userId
|
|
|
|
Title = "Welcome to myWebLog!"
|
|
|
|
Permalink = Permalink "welcome-to-myweblog.html"
|
|
|
|
PublishedOn = now
|
|
|
|
UpdatedOn = now
|
|
|
|
Text = "<p>This is your default home page.</p>"
|
|
|
|
Revisions = [
|
2022-08-21 22:56:18 +00:00
|
|
|
{ AsOf = now
|
|
|
|
Text = Html "<p>This is your default home page.</p>"
|
2022-06-23 00:35:12 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
printfn $"Successfully initialized database for {args[2]} with URL base {args[1]}"
|
2022-07-17 02:17:57 +00:00
|
|
|
match accessLevel with
|
|
|
|
| Administrator -> printfn $" ({args[3]} is an installation administrator)"
|
|
|
|
| WebLogAdmin ->
|
|
|
|
printfn $" ({args[3]} is a web log administrator;"
|
|
|
|
printfn """ use "upgrade-user" to promote to installation administrator)"""
|
|
|
|
| _ -> ()
|
2022-06-23 00:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new web log
|
|
|
|
let createWebLog args sp = task {
|
|
|
|
match args |> Array.length with
|
|
|
|
| 5 -> do! doCreateWebLog args sp
|
2022-08-01 11:46:53 +00:00
|
|
|
| _ -> eprintfn "Usage: myWebLog init [url] [name] [admin-email] [admin-pw]"
|
2022-06-23 00:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Import prior permalinks from a text files with lines in the format "[old] [new]"
|
2022-07-17 19:50:33 +00:00
|
|
|
let private importPriorPermalinks urlBase file (sp : IServiceProvider) = task {
|
2022-06-23 00:35:12 +00:00
|
|
|
let data = sp.GetRequiredService<IData> ()
|
|
|
|
|
2022-07-18 03:10:30 +00:00
|
|
|
match! data.WebLog.FindByHost urlBase with
|
2022-06-23 00:35:12 +00:00
|
|
|
| Some webLog ->
|
|
|
|
|
|
|
|
let mapping =
|
|
|
|
File.ReadAllLines file
|
|
|
|
|> Seq.ofArray
|
|
|
|
|> Seq.map (fun it ->
|
|
|
|
let parts = it.Split " "
|
|
|
|
Permalink parts[0], Permalink parts[1])
|
|
|
|
|
|
|
|
for old, current in mapping do
|
2022-07-19 00:05:10 +00:00
|
|
|
match! data.Post.FindByPermalink current webLog.Id with
|
2022-06-23 00:35:12 +00:00
|
|
|
| Some post ->
|
2022-07-19 00:05:10 +00:00
|
|
|
let! withLinks = data.Post.FindFullById post.Id post.WebLogId
|
|
|
|
let! _ = data.Post.UpdatePriorPermalinks post.Id post.WebLogId
|
|
|
|
(old :: withLinks.Value.PriorPermalinks)
|
2023-12-16 17:24:45 +00:00
|
|
|
printfn $"{old} -> {current}"
|
|
|
|
| None -> eprintfn $"Cannot find current post for {current}"
|
2022-06-23 00:35:12 +00:00
|
|
|
printfn "Done!"
|
|
|
|
| None -> eprintfn $"No web log found at {urlBase}"
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Import permalinks if all is well
|
|
|
|
let importLinks args sp = task {
|
|
|
|
match args |> Array.length with
|
|
|
|
| 3 -> do! importPriorPermalinks args[1] args[2] sp
|
2022-08-01 11:46:53 +00:00
|
|
|
| _ -> eprintfn "Usage: myWebLog import-links [url] [file-name]"
|
2022-06-23 00:35:12 +00:00
|
|
|
}
|
|
|
|
|
2022-06-23 18:56:18 +00:00
|
|
|
// Loading a theme and restoring a backup are not statically compilable; this is OK
|
|
|
|
#nowarn "3511"
|
|
|
|
|
2022-07-22 14:33:11 +00:00
|
|
|
open Microsoft.Extensions.Logging
|
|
|
|
|
2022-06-23 00:35:12 +00:00
|
|
|
/// Load a theme from the given ZIP file
|
|
|
|
let loadTheme (args : string[]) (sp : IServiceProvider) = task {
|
2022-07-24 20:32:37 +00:00
|
|
|
if args.Length = 2 then
|
2022-06-23 00:35:12 +00:00
|
|
|
let fileName =
|
|
|
|
match args[1].LastIndexOf Path.DirectorySeparatorChar with
|
|
|
|
| -1 -> args[1]
|
|
|
|
| it -> args[1][(it + 1)..]
|
2022-07-29 00:36:02 +00:00
|
|
|
match Handlers.Admin.Theme.deriveIdFromFileName fileName with
|
2022-07-24 20:32:37 +00:00
|
|
|
| Ok themeId ->
|
2022-06-23 00:35:12 +00:00
|
|
|
let data = sp.GetRequiredService<IData> ()
|
|
|
|
use stream = File.Open (args[1], FileMode.Open)
|
|
|
|
use copy = new MemoryStream ()
|
|
|
|
do! stream.CopyToAsync copy
|
2022-07-29 00:36:02 +00:00
|
|
|
let! theme = Handlers.Admin.Theme.loadFromZip themeId copy data
|
2022-07-22 14:33:11 +00:00
|
|
|
let fac = sp.GetRequiredService<ILoggerFactory> ()
|
|
|
|
let log = fac.CreateLogger "MyWebLog.Themes"
|
2023-12-16 17:24:45 +00:00
|
|
|
log.LogInformation $"{theme.Name} v{theme.Version} ({theme.Id}) loaded"
|
2022-06-23 00:35:12 +00:00
|
|
|
| Error message -> eprintfn $"{message}"
|
|
|
|
else
|
2022-08-01 11:46:53 +00:00
|
|
|
eprintfn "Usage: myWebLog load-theme [theme-zip-file-name]"
|
2022-06-23 00:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Back up a web log's data
|
|
|
|
module Backup =
|
|
|
|
|
|
|
|
open MyWebLog.Converters
|
|
|
|
open Newtonsoft.Json
|
|
|
|
|
|
|
|
/// A theme asset, with the data base-64 encoded
|
|
|
|
type EncodedAsset =
|
|
|
|
{ /// The ID of the theme asset
|
2022-07-19 00:05:10 +00:00
|
|
|
Id : ThemeAssetId
|
2022-06-23 00:35:12 +00:00
|
|
|
|
|
|
|
/// The updated date for this asset
|
2022-08-21 22:56:18 +00:00
|
|
|
UpdatedOn : Instant
|
2022-06-23 00:35:12 +00:00
|
|
|
|
|
|
|
/// The data for this asset, base-64 encoded
|
2022-07-19 00:05:10 +00:00
|
|
|
Data : string
|
2022-06-23 00:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create an encoded theme asset from the original theme asset
|
|
|
|
static member fromAsset (asset : ThemeAsset) =
|
2022-07-21 03:13:16 +00:00
|
|
|
{ Id = asset.Id
|
|
|
|
UpdatedOn = asset.UpdatedOn
|
|
|
|
Data = Convert.ToBase64String asset.Data
|
2022-06-23 00:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a theme asset from an encoded theme asset
|
2022-07-19 00:05:10 +00:00
|
|
|
static member toAsset (encoded : EncodedAsset) : ThemeAsset =
|
2022-07-21 03:13:16 +00:00
|
|
|
{ Id = encoded.Id
|
|
|
|
UpdatedOn = encoded.UpdatedOn
|
|
|
|
Data = Convert.FromBase64String encoded.Data
|
2022-06-23 00:35:12 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 21:34:18 +00:00
|
|
|
/// An uploaded file, with the data base-64 encoded
|
|
|
|
type EncodedUpload =
|
|
|
|
{ /// The ID of the upload
|
2022-07-19 00:05:10 +00:00
|
|
|
Id : UploadId
|
2022-06-28 21:34:18 +00:00
|
|
|
|
|
|
|
/// The ID of the web log to which the upload belongs
|
2022-07-19 00:05:10 +00:00
|
|
|
WebLogId : WebLogId
|
2022-06-28 21:34:18 +00:00
|
|
|
|
|
|
|
/// The path at which this upload is served
|
2022-07-19 00:05:10 +00:00
|
|
|
Path : Permalink
|
2022-06-28 21:34:18 +00:00
|
|
|
|
|
|
|
/// The date/time this upload was last updated (file time)
|
2022-08-21 22:56:18 +00:00
|
|
|
UpdatedOn : Instant
|
2022-06-28 21:34:18 +00:00
|
|
|
|
|
|
|
/// The data for the upload, base-64 encoded
|
2022-07-19 00:05:10 +00:00
|
|
|
Data : string
|
2022-06-28 21:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create an encoded uploaded file from the original uploaded file
|
|
|
|
static member fromUpload (upload : Upload) : EncodedUpload =
|
2022-07-21 03:13:16 +00:00
|
|
|
{ Id = upload.Id
|
|
|
|
WebLogId = upload.WebLogId
|
|
|
|
Path = upload.Path
|
|
|
|
UpdatedOn = upload.UpdatedOn
|
|
|
|
Data = Convert.ToBase64String upload.Data
|
2022-06-28 21:34:18 +00:00
|
|
|
}
|
2022-06-23 00:35:12 +00:00
|
|
|
|
2022-06-28 21:34:18 +00:00
|
|
|
/// Create an uploaded file from an encoded uploaded file
|
2022-07-19 00:05:10 +00:00
|
|
|
static member toUpload (encoded : EncodedUpload) : Upload =
|
2022-07-21 03:13:16 +00:00
|
|
|
{ Id = encoded.Id
|
|
|
|
WebLogId = encoded.WebLogId
|
|
|
|
Path = encoded.Path
|
|
|
|
UpdatedOn = encoded.UpdatedOn
|
|
|
|
Data = Convert.FromBase64String encoded.Data
|
2022-06-28 21:34:18 +00:00
|
|
|
}
|
|
|
|
|
2022-06-23 00:35:12 +00:00
|
|
|
/// A unified archive for a web log
|
|
|
|
type Archive =
|
|
|
|
{ /// The web log to which this archive belongs
|
2022-07-19 00:05:10 +00:00
|
|
|
WebLog : WebLog
|
2022-06-23 00:35:12 +00:00
|
|
|
|
|
|
|
/// The users for this web log
|
2022-07-19 00:05:10 +00:00
|
|
|
Users : WebLogUser list
|
2022-06-23 00:35:12 +00:00
|
|
|
|
|
|
|
/// The theme used by this web log at the time the archive was made
|
2022-07-19 00:05:10 +00:00
|
|
|
Theme : Theme
|
2022-06-23 00:35:12 +00:00
|
|
|
|
|
|
|
/// Assets for the theme used by this web log at the time the archive was made
|
2022-07-19 00:05:10 +00:00
|
|
|
Assets : EncodedAsset list
|
2022-06-23 00:35:12 +00:00
|
|
|
|
|
|
|
/// The categories for this web log
|
2022-07-19 00:05:10 +00:00
|
|
|
Categories : Category list
|
2022-06-23 00:35:12 +00:00
|
|
|
|
|
|
|
/// The tag mappings for this web log
|
2022-07-19 00:05:10 +00:00
|
|
|
TagMappings : TagMap list
|
2022-06-23 00:35:12 +00:00
|
|
|
|
|
|
|
/// The pages for this web log (containing only the most recent revision)
|
2022-07-19 00:05:10 +00:00
|
|
|
Pages : Page list
|
2022-06-23 00:35:12 +00:00
|
|
|
|
|
|
|
/// The posts for this web log (containing only the most recent revision)
|
2022-07-19 00:05:10 +00:00
|
|
|
Posts : Post list
|
2022-06-28 21:34:18 +00:00
|
|
|
|
|
|
|
/// The uploaded files for this web log
|
2022-07-19 00:05:10 +00:00
|
|
|
Uploads : EncodedUpload list
|
2022-06-23 00:35:12 +00:00
|
|
|
}
|
|
|
|
|
2022-08-21 22:56:18 +00:00
|
|
|
/// Create a JSON serializer
|
2022-06-23 00:35:12 +00:00
|
|
|
let private getSerializer prettyOutput =
|
2022-08-21 22:56:18 +00:00
|
|
|
let serializer = Json.configure (JsonSerializer.CreateDefault ())
|
2022-06-23 00:35:12 +00:00
|
|
|
if prettyOutput then serializer.Formatting <- Formatting.Indented
|
|
|
|
serializer
|
|
|
|
|
|
|
|
/// Display statistics for a backup archive
|
|
|
|
let private displayStats (msg : string) (webLog : WebLog) archive =
|
|
|
|
|
2022-07-19 00:05:10 +00:00
|
|
|
let userCount = List.length archive.Users
|
|
|
|
let assetCount = List.length archive.Assets
|
|
|
|
let categoryCount = List.length archive.Categories
|
|
|
|
let tagMapCount = List.length archive.TagMappings
|
|
|
|
let pageCount = List.length archive.Pages
|
|
|
|
let postCount = List.length archive.Posts
|
|
|
|
let uploadCount = List.length archive.Uploads
|
2022-06-23 00:35:12 +00:00
|
|
|
|
|
|
|
// Create a pluralized output based on the count
|
|
|
|
let plural count ifOne ifMany =
|
|
|
|
if count = 1 then ifOne else ifMany
|
|
|
|
|
|
|
|
printfn ""
|
2022-07-19 00:05:10 +00:00
|
|
|
printfn $"""{msg.Replace ("<>NAME<>", webLog.Name)}"""
|
|
|
|
printfn $""" - The theme "{archive.Theme.Name}" with {assetCount} asset{plural assetCount "" "s"}"""
|
2022-06-23 00:35:12 +00:00
|
|
|
printfn $""" - {userCount} user{plural userCount "" "s"}"""
|
|
|
|
printfn $""" - {categoryCount} categor{plural categoryCount "y" "ies"}"""
|
|
|
|
printfn $""" - {tagMapCount} tag mapping{plural tagMapCount "" "s"}"""
|
|
|
|
printfn $""" - {pageCount} page{plural pageCount "" "s"}"""
|
|
|
|
printfn $""" - {postCount} post{plural postCount "" "s"}"""
|
2022-06-28 21:34:18 +00:00
|
|
|
printfn $""" - {uploadCount} uploaded file{plural uploadCount "" "s"}"""
|
2022-06-23 00:35:12 +00:00
|
|
|
|
|
|
|
/// Create a backup archive
|
|
|
|
let private createBackup webLog (fileName : string) prettyOutput (data : IData) = task {
|
|
|
|
// Create the data structure
|
|
|
|
printfn "- Exporting theme..."
|
2022-07-19 00:05:10 +00:00
|
|
|
let! theme = data.Theme.FindById webLog.ThemeId
|
|
|
|
let! assets = data.ThemeAsset.FindByThemeWithData webLog.ThemeId
|
2022-06-23 00:35:12 +00:00
|
|
|
|
|
|
|
printfn "- Exporting users..."
|
2022-07-19 00:05:10 +00:00
|
|
|
let! users = data.WebLogUser.FindByWebLog webLog.Id
|
2022-06-23 00:35:12 +00:00
|
|
|
|
|
|
|
printfn "- Exporting categories and tag mappings..."
|
2022-07-19 00:05:10 +00:00
|
|
|
let! categories = data.Category.FindByWebLog webLog.Id
|
|
|
|
let! tagMaps = data.TagMap.FindByWebLog webLog.Id
|
2022-06-23 00:35:12 +00:00
|
|
|
|
|
|
|
printfn "- Exporting pages..."
|
2022-07-19 00:05:10 +00:00
|
|
|
let! pages = data.Page.FindFullByWebLog webLog.Id
|
2022-06-23 00:35:12 +00:00
|
|
|
|
|
|
|
printfn "- Exporting posts..."
|
2022-07-19 00:05:10 +00:00
|
|
|
let! posts = data.Post.FindFullByWebLog webLog.Id
|
2022-06-23 00:35:12 +00:00
|
|
|
|
2022-06-28 21:34:18 +00:00
|
|
|
printfn "- Exporting uploads..."
|
2022-07-19 00:05:10 +00:00
|
|
|
let! uploads = data.Upload.FindByWebLogWithData webLog.Id
|
2022-06-28 21:34:18 +00:00
|
|
|
|
2022-06-23 00:35:12 +00:00
|
|
|
printfn "- Writing archive..."
|
2022-07-21 03:13:16 +00:00
|
|
|
let archive =
|
|
|
|
{ WebLog = webLog
|
|
|
|
Users = users
|
|
|
|
Theme = Option.get theme
|
|
|
|
Assets = assets |> List.map EncodedAsset.fromAsset
|
|
|
|
Categories = categories
|
|
|
|
TagMappings = tagMaps
|
|
|
|
Pages = pages |> List.map (fun p -> { p with Revisions = List.truncate 1 p.Revisions })
|
|
|
|
Posts = posts |> List.map (fun p -> { p with Revisions = List.truncate 1 p.Revisions })
|
|
|
|
Uploads = uploads |> List.map EncodedUpload.fromUpload
|
|
|
|
}
|
2022-06-23 00:35:12 +00:00
|
|
|
|
|
|
|
// Write the structure to the backup file
|
|
|
|
if File.Exists fileName then File.Delete fileName
|
|
|
|
let serializer = getSerializer prettyOutput
|
|
|
|
use writer = new StreamWriter (fileName)
|
|
|
|
serializer.Serialize (writer, archive)
|
|
|
|
writer.Close ()
|
|
|
|
|
2022-07-04 23:06:32 +00:00
|
|
|
displayStats $"{fileName} (for <>NAME<>) contains:" webLog archive
|
2022-06-23 00:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let private doRestore archive newUrlBase (data : IData) = task {
|
|
|
|
let! restore = task {
|
2022-07-19 00:05:10 +00:00
|
|
|
match! data.WebLog.FindById archive.WebLog.Id with
|
|
|
|
| Some webLog when defaultArg newUrlBase webLog.UrlBase = webLog.UrlBase ->
|
|
|
|
do! data.WebLog.Delete webLog.Id
|
|
|
|
return { archive with WebLog = { archive.WebLog with UrlBase = defaultArg newUrlBase webLog.UrlBase } }
|
2022-06-23 00:35:12 +00:00
|
|
|
| Some _ ->
|
|
|
|
// Err'body gets new IDs...
|
2023-12-16 17:24:45 +00:00
|
|
|
let newWebLogId = WebLogId.Create()
|
|
|
|
let newCatIds = archive.Categories |> List.map (fun cat -> cat.Id, CategoryId.Create() ) |> dict
|
|
|
|
let newMapIds = archive.TagMappings |> List.map (fun tm -> tm.Id, TagMapId.Create() ) |> dict
|
|
|
|
let newPageIds = archive.Pages |> List.map (fun page -> page.Id, PageId.Create() ) |> dict
|
|
|
|
let newPostIds = archive.Posts |> List.map (fun post -> post.Id, PostId.Create() ) |> dict
|
|
|
|
let newUserIds = archive.Users |> List.map (fun user -> user.Id, WebLogUserId.Create()) |> dict
|
|
|
|
let newUpIds = archive.Uploads |> List.map (fun up -> up.Id, UploadId.Create() ) |> dict
|
2022-06-23 00:35:12 +00:00
|
|
|
return
|
|
|
|
{ archive with
|
2022-07-19 00:05:10 +00:00
|
|
|
WebLog = { archive.WebLog with Id = newWebLogId; UrlBase = Option.get newUrlBase }
|
|
|
|
Users = archive.Users
|
|
|
|
|> List.map (fun u -> { u with Id = newUserIds[u.Id]; WebLogId = newWebLogId })
|
|
|
|
Categories = archive.Categories
|
|
|
|
|> List.map (fun c -> { c with Id = newCatIds[c.Id]; WebLogId = newWebLogId })
|
|
|
|
TagMappings = archive.TagMappings
|
|
|
|
|> List.map (fun tm -> { tm with Id = newMapIds[tm.Id]; WebLogId = newWebLogId })
|
|
|
|
Pages = archive.Pages
|
2022-06-23 00:35:12 +00:00
|
|
|
|> List.map (fun page ->
|
|
|
|
{ page with
|
2022-07-19 00:05:10 +00:00
|
|
|
Id = newPageIds[page.Id]
|
|
|
|
WebLogId = newWebLogId
|
|
|
|
AuthorId = newUserIds[page.AuthorId]
|
2022-06-23 00:35:12 +00:00
|
|
|
})
|
2022-07-19 00:05:10 +00:00
|
|
|
Posts = archive.Posts
|
2022-06-23 00:35:12 +00:00
|
|
|
|> List.map (fun post ->
|
|
|
|
{ post with
|
2022-07-19 00:05:10 +00:00
|
|
|
Id = newPostIds[post.Id]
|
|
|
|
WebLogId = newWebLogId
|
|
|
|
AuthorId = newUserIds[post.AuthorId]
|
|
|
|
CategoryIds = post.CategoryIds |> List.map (fun c -> newCatIds[c])
|
2022-06-23 00:35:12 +00:00
|
|
|
})
|
2022-07-19 00:05:10 +00:00
|
|
|
Uploads = archive.Uploads
|
|
|
|
|> List.map (fun u -> { u with Id = newUpIds[u.Id]; WebLogId = newWebLogId })
|
2022-06-23 00:35:12 +00:00
|
|
|
}
|
|
|
|
| None ->
|
|
|
|
return
|
|
|
|
{ archive with
|
2022-07-19 00:05:10 +00:00
|
|
|
WebLog = { archive.WebLog with UrlBase = defaultArg newUrlBase archive.WebLog.UrlBase }
|
2022-06-23 00:35:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore theme and assets (one at a time, as assets can be large)
|
|
|
|
printfn ""
|
|
|
|
printfn "- Importing theme..."
|
2022-07-19 00:05:10 +00:00
|
|
|
do! data.Theme.Save restore.Theme
|
2022-08-21 22:56:18 +00:00
|
|
|
restore.Assets
|
|
|
|
|> List.iter (EncodedAsset.toAsset >> data.ThemeAsset.Save >> Async.AwaitTask >> Async.RunSynchronously)
|
2022-06-23 00:35:12 +00:00
|
|
|
|
|
|
|
// Restore web log data
|
|
|
|
|
|
|
|
printfn "- Restoring web log..."
|
2023-08-01 01:28:02 +00:00
|
|
|
// v2.0 backups will not have redirect rules; fix that if restoring to v2.1 or later
|
|
|
|
let webLog =
|
|
|
|
if isNull (box restore.WebLog.RedirectRules) then { restore.WebLog with RedirectRules = [] }
|
|
|
|
else restore.WebLog
|
|
|
|
do! data.WebLog.Add webLog
|
2022-06-23 00:35:12 +00:00
|
|
|
|
|
|
|
printfn "- Restoring users..."
|
2022-07-19 00:05:10 +00:00
|
|
|
do! data.WebLogUser.Restore restore.Users
|
2022-06-23 00:35:12 +00:00
|
|
|
|
|
|
|
printfn "- Restoring categories and tag mappings..."
|
2022-08-21 22:56:18 +00:00
|
|
|
if not (List.isEmpty restore.TagMappings) then do! data.TagMap.Restore restore.TagMappings
|
|
|
|
if not (List.isEmpty restore.Categories) then do! data.Category.Restore restore.Categories
|
2022-06-23 00:35:12 +00:00
|
|
|
|
|
|
|
printfn "- Restoring pages..."
|
2022-08-21 22:56:18 +00:00
|
|
|
if not (List.isEmpty restore.Pages) then do! data.Page.Restore restore.Pages
|
2022-06-23 00:35:12 +00:00
|
|
|
|
|
|
|
printfn "- Restoring posts..."
|
2022-08-21 22:56:18 +00:00
|
|
|
if not (List.isEmpty restore.Posts) then do! data.Post.Restore restore.Posts
|
2022-06-23 00:35:12 +00:00
|
|
|
|
|
|
|
// TODO: comments not yet implemented
|
|
|
|
|
2022-06-28 21:34:18 +00:00
|
|
|
printfn "- Restoring uploads..."
|
2022-08-21 22:56:18 +00:00
|
|
|
if not (List.isEmpty restore.Uploads) then
|
|
|
|
do! data.Upload.Restore (restore.Uploads |> List.map EncodedUpload.toUpload)
|
2022-06-28 21:34:18 +00:00
|
|
|
|
2022-07-19 00:05:10 +00:00
|
|
|
displayStats "Restored for <>NAME<>:" restore.WebLog restore
|
2022-06-23 00:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Decide whether to restore a backup
|
|
|
|
let private restoreBackup (fileName : string) newUrlBase promptForOverwrite data = task {
|
|
|
|
|
|
|
|
let serializer = getSerializer false
|
|
|
|
use stream = new FileStream (fileName, FileMode.Open)
|
|
|
|
use reader = new StreamReader (stream)
|
|
|
|
use jsonReader = new JsonTextReader (reader)
|
|
|
|
let archive = serializer.Deserialize<Archive> jsonReader
|
|
|
|
|
|
|
|
let mutable doOverwrite = not promptForOverwrite
|
|
|
|
if promptForOverwrite then
|
|
|
|
printfn "** WARNING: Restoring a web log will delete existing data for that web log"
|
|
|
|
printfn " (unless restoring to a different URL base), and will overwrite the"
|
|
|
|
printfn " theme in either case."
|
|
|
|
printfn ""
|
|
|
|
printf "Continue? [Y/n] "
|
|
|
|
doOverwrite <- not ((Console.ReadKey ()).Key = ConsoleKey.N)
|
|
|
|
|
|
|
|
if doOverwrite then
|
|
|
|
do! doRestore archive newUrlBase data
|
|
|
|
else
|
2022-07-19 00:05:10 +00:00
|
|
|
printfn $"{archive.WebLog.Name} backup restoration canceled"
|
2022-06-23 00:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate a backup archive
|
|
|
|
let generateBackup (args : string[]) (sp : IServiceProvider) = task {
|
2022-07-04 23:06:32 +00:00
|
|
|
if args.Length > 1 && args.Length < 5 then
|
2022-06-23 00:35:12 +00:00
|
|
|
let data = sp.GetRequiredService<IData> ()
|
2022-07-18 03:10:30 +00:00
|
|
|
match! data.WebLog.FindByHost args[1] with
|
2022-06-23 00:35:12 +00:00
|
|
|
| Some webLog ->
|
2022-07-04 23:06:32 +00:00
|
|
|
let fileName =
|
|
|
|
if args.Length = 2 || (args.Length = 3 && args[2] = "pretty") then
|
2022-07-19 00:05:10 +00:00
|
|
|
$"{webLog.Slug}.json"
|
2022-07-04 23:06:32 +00:00
|
|
|
elif args[2].EndsWith ".json" then
|
|
|
|
args[2]
|
|
|
|
else
|
|
|
|
$"{args[2]}.json"
|
|
|
|
let prettyOutput = (args.Length = 3 && args[2] = "pretty") || (args.Length = 4 && args[3] = "pretty")
|
2022-06-23 00:35:12 +00:00
|
|
|
do! createBackup webLog fileName prettyOutput data
|
2022-07-17 19:50:33 +00:00
|
|
|
| None -> eprintfn $"Error: no web log found for {args[1]}"
|
2022-06-23 00:35:12 +00:00
|
|
|
else
|
2022-08-01 11:46:53 +00:00
|
|
|
eprintfn """Usage: myWebLog backup [url-base] [*backup-file-name] [**"pretty"]"""
|
2022-07-17 19:50:33 +00:00
|
|
|
eprintfn """ * optional - default is [web-log-slug].json"""
|
|
|
|
eprintfn """ ** optional - default is non-pretty JSON output"""
|
2022-06-23 00:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Restore a backup archive
|
|
|
|
let restoreFromBackup (args : string[]) (sp : IServiceProvider) = task {
|
|
|
|
if args.Length = 2 || args.Length = 3 then
|
|
|
|
let data = sp.GetRequiredService<IData> ()
|
|
|
|
let newUrlBase = if args.Length = 3 then Some args[2] else None
|
|
|
|
do! restoreBackup args[1] newUrlBase (args[0] <> "do-restore") data
|
|
|
|
else
|
2022-08-01 11:46:53 +00:00
|
|
|
eprintfn "Usage: myWebLog restore [backup-file-name] [*url-base]"
|
2022-07-17 19:50:33 +00:00
|
|
|
eprintfn " * optional - will restore to original URL base if omitted"
|
|
|
|
eprintfn " (use do-restore to skip confirmation prompt)"
|
2022-06-23 00:35:12 +00:00
|
|
|
}
|
2022-07-17 19:50:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// Upgrade a WebLogAdmin user to an Administrator user
|
|
|
|
let private doUserUpgrade urlBase email (data : IData) = task {
|
2022-07-18 03:10:30 +00:00
|
|
|
match! data.WebLog.FindByHost urlBase with
|
2022-07-17 19:50:33 +00:00
|
|
|
| Some webLog ->
|
2022-07-19 00:05:10 +00:00
|
|
|
match! data.WebLogUser.FindByEmail email webLog.Id with
|
2022-07-17 19:50:33 +00:00
|
|
|
| Some user ->
|
2022-07-19 00:05:10 +00:00
|
|
|
match user.AccessLevel with
|
2022-07-17 19:50:33 +00:00
|
|
|
| WebLogAdmin ->
|
2022-07-19 00:05:10 +00:00
|
|
|
do! data.WebLogUser.Update { user with AccessLevel = Administrator }
|
2022-07-17 19:50:33 +00:00
|
|
|
printfn $"{email} is now an Administrator user"
|
2023-12-16 17:24:45 +00:00
|
|
|
| other -> eprintfn $"ERROR: {email} is an {other}, not a WebLogAdmin"
|
2022-07-17 19:50:33 +00:00
|
|
|
| None -> eprintfn $"ERROR: no user {email} found at {urlBase}"
|
|
|
|
| None -> eprintfn $"ERROR: no web log found for {urlBase}"
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Upgrade a WebLogAdmin user to an Administrator user if the command-line arguments are good
|
|
|
|
let upgradeUser (args : string[]) (sp : IServiceProvider) = task {
|
|
|
|
match args.Length with
|
|
|
|
| 3 -> do! doUserUpgrade args[1] args[2] (sp.GetRequiredService<IData> ())
|
2022-08-01 11:46:53 +00:00
|
|
|
| _ -> eprintfn "Usage: myWebLog upgrade-user [web-log-url-base] [email-address]"
|
2022-07-17 19:50:33 +00:00
|
|
|
}
|
2022-08-21 22:56:18 +00:00
|
|
|
|
|
|
|
/// Set a user's password
|
|
|
|
let doSetPassword urlBase email password (data : IData) = task {
|
|
|
|
match! data.WebLog.FindByHost urlBase with
|
|
|
|
| Some webLog ->
|
|
|
|
match! data.WebLogUser.FindByEmail email webLog.Id with
|
|
|
|
| Some user ->
|
|
|
|
do! data.WebLogUser.Update { user with PasswordHash = Handlers.User.createPasswordHash user password }
|
|
|
|
printfn $"Password for user {email} at {webLog.Name} set successfully"
|
|
|
|
| None -> eprintfn $"ERROR: no user {email} found at {urlBase}"
|
|
|
|
| None -> eprintfn $"ERROR: no web log found for {urlBase}"
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set a user's password if the command-line arguments are good
|
|
|
|
let setPassword (args : string[]) (sp : IServiceProvider) = task {
|
|
|
|
match args.Length with
|
|
|
|
| 4 -> do! doSetPassword args[1] args[2] args[3] (sp.GetRequiredService<IData> ())
|
|
|
|
| _ -> eprintfn "Usage: myWebLog set-password [web-log-url-base] [email-address] [password]"
|
|
|
|
}
|