migrate categories - needs cleaning

This commit is contained in:
Ross Trottier 2024-05-16 10:26:59 -06:00
parent 5737ce23d1
commit 83ef9f4192
4 changed files with 121 additions and 29 deletions

38
main.go
View File

@ -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()
}

View File

@ -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 {

View File

@ -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
}
var results []CategoryResult
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
}
}
return 0, nil
}

View File

@ -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) {