From 83ef9f4192bc86bea7da6785f461a15662598963 Mon Sep 17 00:00:00 2001 From: Ross Trottier Date: Thu, 16 May 2024 10:26:59 -0600 Subject: [PATCH] migrate categories - needs cleaning --- main.go | 38 +++++---- services/migration/category-result.go | 12 ++- services/migration/migrate-categories.go | 98 ++++++++++++++++++++++-- services/wordpress/get-category.go | 2 + 4 files changed, 121 insertions(+), 29 deletions(-) diff --git a/main.go b/main.go index 70efddb..cf90234 100644 --- a/main.go +++ b/main.go @@ -1,13 +1,13 @@ package main import ( - "federated.computer/wp-sync-slowtwitch/services/wordpress" + "federated.computer/wp-sync-slowtwitch/services/migration" "fmt" ) -const baseUrl = "https://go-api-playground.local/wp-json/" -const wordpressKey = "admin" -const wordpressSecret = "S34E keY1 A1uX 6ncs Rx4T f21W" +const baseUrl = "https://slowtwitch.cloud/wp-json/" +const wordpressKey = "admin@slowtwitch.cloud" +const wordpressSecret = "6zY7 xsKZ dGIt l1Lp ypIK 6TWh" const slowtwitchAdminUser = "admin" const slowtwitchAdminPass = "yxnh93Ybbz2Nm8#mp28zCVv" @@ -22,23 +22,21 @@ func main() { // TODO Category migration // TODO User migration // TODO Article migration - //slowtwitchDB, err := migration.Connect(slowtwitchAdminUser, slowtwitchAdminPass, federatedDbUrl, federatedDbPort, slowtwitchDbName) - - createRedirect := wordpress.CreateRedirect{ - Url: "/gotest", - Title: "Test From Go", - MatchType: "page", - ActionType: "url", - ActionCode: 301, - GroupId: 1, - ActionData: wordpress.ActionData{ - Url: "/GoTestResult", - }, + slowtwitchDB, slowtwitchDbErr := migration.Connect(slowtwitchAdminUser, slowtwitchAdminPass, federatedDbUrl, federatedDbPort, slowtwitchDbName) + if slowtwitchDbErr != nil { + fmt.Println(slowtwitchDbErr) + } + resultsDB, resultsDBerr := migration.Connect(slowtwitchAdminUser, slowtwitchAdminPass, federatedDbUrl, federatedDbPort, migrationDbName) + if resultsDBerr != nil { + fmt.Println(resultsDBerr) } - result, err := createRedirect.Execute(baseUrl, wordpressKey, wordpressSecret) - if err != nil { - panic(err) + categoryMigration := migration.MigrateCategories{ + SlowtwitchDatabase: slowtwitchDB, + ResultsDatabase: resultsDB, + WordpressBaseUrl: baseUrl, + WordpressUser: wordpressKey, + WordpressPassword: wordpressSecret, } - fmt.Println(result) + categoryMigration.Execute() } diff --git a/services/migration/category-result.go b/services/migration/category-result.go index b969dfb..815f8c8 100644 --- a/services/migration/category-result.go +++ b/services/migration/category-result.go @@ -17,10 +17,20 @@ type CategoryResult struct { } func CreateCategoryResult(result CategoryResult, db *sql.DB) error { - _, err := db.Exec("insert into CategoryResults (SlowtwitchId, WordpressId, OldUrl, NewUrl, IsSuccess, ErrorMessage, OldUrlStatus) values (?, ?, ?, ?, ?, ?, ?);", result.SlowtwitchId, result.WordpressId, result.OldUrl, result.NewUrl, result.IsSuccess, result.ErrorMessage) + _, err := db.Exec("insert into CategoryResults (SlowtwitchId, WordpressId, OldUrl, NewUrl, IsSuccess, ErrorMessage, OldUrlStatus) values (?, ?, ?, ?, ?, ?, ?);", result.SlowtwitchId, result.WordpressId, result.OldUrl, result.NewUrl, result.IsSuccess, result.ErrorMessage, result.OldUrlStatus) return err } +func HasBeenMigrated(slowtwitchId int, db *sql.DB) bool { + _, err := GetSlowtwitchCategoryResult(slowtwitchId, db) + + if err == nil { + return true + } else { + return false + } +} + func GetSlowtwitchCategoryResult(slowtwitchId int, db *sql.DB) (CategoryResult, error) { rows, err := db.Query("select WordpressId, SlowtwitchId, OldUrl, NewUrl, (IsSuccess = b'1'), ErrorMessage, OldUrlStatus from CategoryResults where SlowtwitchId = ?", slowtwitchId) if err != nil { diff --git a/services/migration/migrate-categories.go b/services/migration/migrate-categories.go index b47185a..02652cb 100644 --- a/services/migration/migrate-categories.go +++ b/services/migration/migrate-categories.go @@ -1,18 +1,100 @@ package migration -//THIS SHOULD BE A STRUCT WITH AN EXECUTE -/*import ( +import ( "database/sql" "federated.computer/wp-sync-slowtwitch/services/slowtwitch" + "federated.computer/wp-sync-slowtwitch/services/wordpress" + "fmt" + "strings" ) -func MigrateCategories(slowtwitchDB *sql.DB, resultsDB *sql.DB, baseUrl, user, pass string) ([]CategoryResult, error) { - slowtwitchCategories := slowtwitch.GetCategories(slowtwitchDB) +type MigrateCategories struct { + SlowtwitchDatabase *sql.DB + ResultsDatabase *sql.DB + WordpressBaseUrl string + WordpressUser string + WordpressPassword string +} - for _, category := range slowtwitchCategories { +func (migration *MigrateCategories) Execute() []CategoryResult { + categories := slowtwitch.GetCategories(migration.SlowtwitchDatabase) + var output []CategoryResult + for _, category := range categories { + hasBeenMigrated := HasBeenMigrated(category.Id, migration.ResultsDatabase) + if hasBeenMigrated { + fmt.Println("This category has already been migrated.") + continue + } + + errorMessage := "" + + //Get wordpress parent if exists + wordpressParentId, err := getWordpressParentCategoryId(category, migration, errorMessage) + if err != nil { + errorMessage = errorMessage + err.Error() + err = nil + } + //convert url for redirect + slowtwitchPath := slowtwitch.ConvertUrlToCategoryFormat(category.FullName) + slowtwitchUrl := slowtwitch.GetURL(slowtwitchPath) + httpStatus := slowtwitch.GetPageStatus(slowtwitchUrl) + //create in wp + createWordpressCategory := wordpress.CreateCategory{ + Name: strings.Trim(category.Name, " "), + Description: "", + ParentId: wordpressParentId, + } + wordpressCategory := createWordpressCategory.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword) + //submit redirect + createRedirect := wordpress.CreateRedirect{ + Url: slowtwitchPath, + Title: "Category: " + wordpressCategory.Name, + MatchType: "page", + ActionType: "url", + ActionCode: 301, + GroupId: 1, + ActionData: wordpress.ActionData{ + Url: wordpressCategory.Slug, + }, + } + + _, err = createRedirect.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword) + + if err != nil { + errorMessage = errorMessage + err.Error() + err = nil + } + //submit results + overallResult := CategoryResult{ + WordpressId: wordpressCategory.Id, + SlowtwitchId: category.Id, + OldUrl: slowtwitchUrl, + OldUrlStatus: httpStatus, + NewUrl: wordpressCategory.Link, + IsSuccess: true, + ErrorMessage: errorMessage, + } + + err = CreateCategoryResult(overallResult, migration.ResultsDatabase) + if err != nil { + fmt.Println(err) + err = nil + } + output = append(output, overallResult) + } + return output +} + +func getWordpressParentCategoryId(category slowtwitch.SlowtwitchCategory, migration *MigrateCategories, errorMessage string) (int, error) { + if category.FatherId != 0 { + parentCategory, err := GetSlowtwitchCategoryResult(category.FatherId, migration.ResultsDatabase) + if err != nil { + return 0, err + } else { + return parentCategory.WordpressId, nil + } } - var results []CategoryResult - -}*/ + return 0, nil +} diff --git a/services/wordpress/get-category.go b/services/wordpress/get-category.go index 6692e81..95e5bf1 100644 --- a/services/wordpress/get-category.go +++ b/services/wordpress/get-category.go @@ -11,6 +11,8 @@ type CategoryData struct { ParentId int `json:"parent_id"` Name string `json:"name"` ParentName string `json:"parent_name"` + Link string `json:"link"` + Slug string `json:"slug"` } func GetCategory(name, baseUrl, user, pass string) (CategoryData, bool) {