re-factoring existing migrate posts file

This commit is contained in:
Ross Trottier 2024-05-21 10:50:05 -06:00
parent 270255b110
commit 732b238e52
4 changed files with 106 additions and 30 deletions

View File

@ -16,6 +16,7 @@ type MigrateAuthors struct {
WordpressPassword string WordpressPassword string
} }
// TODO Add first and last name
func (migration *MigrateAuthors) Execute() []EditorResult { func (migration *MigrateAuthors) Execute() []EditorResult {
editors := slowtwitch.GetUsers(migration.SlowtwitchDatabase) editors := slowtwitch.GetUsers(migration.SlowtwitchDatabase)
var output []EditorResult var output []EditorResult
@ -24,11 +25,15 @@ func (migration *MigrateAuthors) Execute() []EditorResult {
hasBeenMigrated := EditorHasBeenMigrated(editor.Username, migration.ResultsDatabase) hasBeenMigrated := EditorHasBeenMigrated(editor.Username, migration.ResultsDatabase)
if hasBeenMigrated == false { if hasBeenMigrated == false {
firstName, lastName := getFirstAndLastName(editor.Name)
createUser := wordpress.CreateUser{ createUser := wordpress.CreateUser{
Username: strings.Trim(editor.Username, " "), Username: strings.TrimSpace(editor.Username),
Email: strings.Trim(editor.Email, " "), Email: strings.TrimSpace(editor.Email),
Password: editor.Password, Password: editor.Password,
Roles: "editor", Roles: "editor",
FirstName: firstName,
LastName: lastName,
} }
result, err := createUser.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword) result, err := createUser.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword)
@ -58,3 +63,22 @@ func (migration *MigrateAuthors) Execute() []EditorResult {
return output return output
} }
func getFirstAndLastName(name string) (firstName string, lastName string) {
if len(name) == 0 {
return "", ""
}
names := strings.Split(name, " ")
firstName = strings.TrimSpace(names[0])
lastName = ""
if len(names) > 1 {
for _, name := range names[1:] {
lastName += " " + name
}
lastName = strings.TrimSpace(lastName)
}
return
}

View File

@ -18,20 +18,14 @@ type MigratePosts struct {
WordpressPassword string WordpressPassword string
} }
// TODO Get list of all ST post IDs
// TODO Get ST post by ID
// TODO Get List of all already migrated post IDs
func (migration MigratePosts) Execute() { func (migration MigratePosts) Execute() {
slowtwitchPostIds, err := slowtwitch.GetAllPostIds(migration.SlowtwitchDatabase) slowtwitchPostIds, err := slowtwitch.GetAllPostIds(migration.SlowtwitchDatabase)
if err != nil { if err != nil {
fmt.Println("Could not migrate posts:", err) panic("Could not migrate posts: " + err.Error())
return
} }
migratedPostIds, err := GetAllMigratedPostIds(migration.ResultsDatabase) migratedPostIds, err := GetAllMigratedPostIds(migration.ResultsDatabase)
if err != nil { if err != nil {
fmt.Println("Could not migrate posts:", err) panic("Could not migrate posts:" + err.Error())
return
} }
//get wordpress tag data, there are only 3 //get wordpress tag data, there are only 3
tags := []string{"swim", "bike", "run"} tags := []string{"swim", "bike", "run"}
@ -65,7 +59,7 @@ func (migration MigratePosts) Execute() {
createWordpressPost.Date = timeString createWordpressPost.Date = timeString
} else { } else {
errorMessage = errorMessage + "Invalid Date Published" errorMessage = errorMessage + "Invalid Date Published"
// TODO SEND TO RESULTS DB WITH CALL createPostFailureResult(postId, errorMessage, migration.ResultsDatabase)
continue continue
} }
@ -83,7 +77,7 @@ func (migration MigratePosts) Execute() {
if err != nil { if err != nil {
errorMessage = errorMessage + err.Error() errorMessage = errorMessage + err.Error()
// TODO SEND TO RESULTS DB WITH CALL createPostFailureResult(postId, errorMessage, migration.ResultsDatabase)
continue continue
} }
@ -93,7 +87,7 @@ func (migration MigratePosts) Execute() {
categoryResult, err := GetSlowtwitchCategoryResult(slowtwitchCategoryId, migration.ResultsDatabase) categoryResult, err := GetSlowtwitchCategoryResult(slowtwitchCategoryId, migration.ResultsDatabase)
if err != nil { if err != nil {
errorMessage = errorMessage + err.Error() errorMessage = errorMessage + err.Error()
// TODO SEND TO RESULTS DB WITH CALL createPostFailureResult(postId, errorMessage, migration.ResultsDatabase)
continue continue
} }
wordPressCategoryIds = append(wordPressCategoryIds, categoryResult.WordpressId) wordPressCategoryIds = append(wordPressCategoryIds, categoryResult.WordpressId)
@ -105,7 +99,7 @@ func (migration MigratePosts) Execute() {
if err != nil { if err != nil {
errorMessage = errorMessage + err.Error() errorMessage = errorMessage + err.Error()
// TODO SEND TO RESULTS DB WITH CALL createPostFailureResult(postId, errorMessage, migration.ResultsDatabase)
continue continue
} }
createWordpressPost.Author = editor.WordpressId createWordpressPost.Author = editor.WordpressId
@ -115,7 +109,7 @@ func (migration MigratePosts) Execute() {
if linkStatus == 404 { if linkStatus == 404 {
errorMessage = errorMessage + "Page not found on Slowtwitch" errorMessage = errorMessage + "Page not found on Slowtwitch"
// TODO SEND TO RESULTS DB WITH CALL createPostFailureResult(postId, errorMessage, migration.ResultsDatabase)
continue continue
} }
@ -125,7 +119,7 @@ func (migration MigratePosts) Execute() {
imagePaths, html, err := slowtwitch.GetImagesAndPostHtml(oldLink) imagePaths, html, err := slowtwitch.GetImagesAndPostHtml(oldLink)
if err != nil { if err != nil {
errorMessage = errorMessage + err.Error() errorMessage = errorMessage + err.Error()
// TODO SEND TO RESULTS DB WITH CALL createPostFailureResult(postId, errorMessage, migration.ResultsDatabase)
continue continue
} }
@ -141,7 +135,7 @@ func (migration MigratePosts) Execute() {
if err != nil { if err != nil {
errorMessage = errorMessage + err.Error() errorMessage = errorMessage + err.Error()
// TODO SEND TO RESULTS DB WITH CALL createImageFailureResult(imageUrl, migration.ResultsDatabase)
continue continue
} }
//first photo is the featured photo //first photo is the featured photo
@ -173,14 +167,20 @@ func (migration MigratePosts) Execute() {
createRedirect.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword) createRedirect.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword)
} }
createWordpressPost.Content = html createWordpressPost.Content = html
post, err := createWordpressPost.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword) post, err := createWordpressPost.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword)
if err != nil { if err != nil {
errorMessage = errorMessage + err.Error() errorMessage = errorMessage + err.Error()
// TODO SEND TO RESULTS DB WITH CALL createPostFailureResult(postId, errorMessage, migration.ResultsDatabase)
continue continue
} }
//set up post result here to create //set up post result here to create
//truncate error message for db
if len(errorMessage) > 1450 {
errorMessage = errorMessage[:1450]
}
postResult := PostResult{ postResult := PostResult{
SlowtwitchId: postId, SlowtwitchId: postId,
WordpressId: post.Id, WordpressId: post.Id,
@ -205,7 +205,7 @@ func (migration MigratePosts) Execute() {
oldPath := strings.ReplaceAll(oldLink, "https://www.slowtwitch.com", "") oldPath := strings.ReplaceAll(oldLink, "https://www.slowtwitch.com", "")
postRedirect := wordpress.CreateRedirect{ postRedirect := wordpress.CreateRedirect{
Title: "Article Redirect" + postBase.Title, Title: "Article Redirect" + postBase.Title,
Url: strings.ReplaceAll(oldPath, ".html", ""), Url: oldPath,
MatchType: "page", MatchType: "page",
ActionType: "url", ActionType: "url",
ActionCode: 301, ActionCode: 301,
@ -216,11 +216,24 @@ func (migration MigratePosts) Execute() {
} }
_, err = postRedirect.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword) _, err = postRedirect.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println("Error creating redirect for", postId, ":"+err.Error())
} }
fmt.Println("Successfully created post and result for", postId)
// TODO Update advanced Custom Fields with images
//Install ACF and create field
/*
"acf": {
"post_images": [...ids]
} }
//Update related posts (get from post results db) as second loop */
//Update advanced Custom Fields with images }
// TODO Update related posts (get from post results db) as second loop
//Install ACF and create field
/*
"acf": {
"related_posts": [...ids]
}
*/
} }
func getPostIdsThatNeedMigration(slowtwitchPostIds, migratedPostIds []int) []int { func getPostIdsThatNeedMigration(slowtwitchPostIds, migratedPostIds []int) []int {
@ -235,3 +248,39 @@ func getPostIdsThatNeedMigration(slowtwitchPostIds, migratedPostIds []int) []int
return output return output
} }
func createPostFailureResult(slowtwitchId int, errorMessage string, migrationDb *sql.DB) {
fmt.Println("Error creating post: ", slowtwitchId, ":", errorMessage)
postResult := PostResult{
SlowtwitchId: slowtwitchId,
WordpressId: 0,
OldUrl: "",
OldUrlStatus: 0,
NewUrl: "",
IsSuccess: false,
ErrorMessage: errorMessage,
}
_, err := CreatePostResult(postResult, migrationDb)
if err != nil {
fmt.Println("Failed to create failure result: ", err)
}
}
func createImageFailureResult(url string, resultsDatabase *sql.DB) {
fmt.Println("Error creating image failure result: ", url)
imageResult := ImageResult{
OldUrl: url,
NewUrl: "",
WordpressId: 0,
IsSuccess: false,
}
err := CreateImageResult(imageResult, resultsDatabase)
if err != nil {
fmt.Println("Failed to create failure result: ", err)
}
}

View File

@ -9,10 +9,11 @@ type SlowtwitchUser struct {
Username string Username string
Password string Password string
Email string Email string
Name string
} }
func GetUsers(db *sql.DB) []SlowtwitchUser { func GetUsers(db *sql.DB) []SlowtwitchUser {
rows, err := db.Query("select Username, Password, Email from glinks_Users;") rows, err := db.Query("select Username, Password, Email, Name from glinks_Users;")
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -24,7 +25,7 @@ func GetUsers(db *sql.DB) []SlowtwitchUser {
for rows.Next() { for rows.Next() {
user := SlowtwitchUser{} user := SlowtwitchUser{}
err := rows.Scan(&user.Username, &user.Password, &user.Email) err := rows.Scan(&user.Username, &user.Password, &user.Email, &user.Name)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)

View File

@ -10,6 +10,8 @@ type CreateUser struct {
Email string `json:"email"` Email string `json:"email"`
Roles string `json:"roles"` Roles string `json:"roles"`
Password string `json:"password"` Password string `json:"password"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
} }
type CreateUserResponse struct { type CreateUserResponse struct {