2024-05-15 23:37:42 +00:00
|
|
|
package migration
|
|
|
|
|
2024-05-16 16:26:59 +00:00
|
|
|
import (
|
2024-05-15 23:37:42 +00:00
|
|
|
"database/sql"
|
|
|
|
"federated.computer/wp-sync-slowtwitch/services/slowtwitch"
|
2024-05-16 16:26:59 +00:00
|
|
|
"federated.computer/wp-sync-slowtwitch/services/wordpress"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2024-05-15 23:37:42 +00:00
|
|
|
)
|
|
|
|
|
2024-05-16 16:26:59 +00:00
|
|
|
type MigrateCategories struct {
|
|
|
|
SlowtwitchDatabase *sql.DB
|
|
|
|
ResultsDatabase *sql.DB
|
|
|
|
WordpressBaseUrl string
|
|
|
|
WordpressUser string
|
|
|
|
WordpressPassword string
|
|
|
|
}
|
2024-05-15 23:37:42 +00:00
|
|
|
|
2024-05-16 16:26:59 +00:00
|
|
|
func (migration *MigrateCategories) Execute() []CategoryResult {
|
|
|
|
categories := slowtwitch.GetCategories(migration.SlowtwitchDatabase)
|
|
|
|
var output []CategoryResult
|
|
|
|
for _, category := range categories {
|
|
|
|
hasBeenMigrated := HasBeenMigrated(category.Id, migration.ResultsDatabase)
|
2024-05-15 23:37:42 +00:00
|
|
|
|
2024-05-16 16:26:59 +00:00
|
|
|
if hasBeenMigrated {
|
|
|
|
fmt.Println("This category has already been migrated.")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
errorMessage := ""
|
|
|
|
|
|
|
|
wordpressParentId, err := getWordpressParentCategoryId(category, migration, errorMessage)
|
|
|
|
if err != nil {
|
|
|
|
errorMessage = errorMessage + err.Error()
|
|
|
|
err = nil
|
|
|
|
}
|
2024-05-16 18:50:53 +00:00
|
|
|
slowtwitchPath, slowtwitchUrl, httpStatus := getSlowtwitchUrlsAndVerify(category)
|
|
|
|
wordpressCategory := createInWordpress(category, wordpressParentId, migration)
|
|
|
|
err, errorMessage = createRedirect(wordpressCategory, migration, slowtwitchPath, err, errorMessage)
|
|
|
|
overallResult := submitResults(wordpressCategory, category, slowtwitchUrl, httpStatus, errorMessage, err, migration)
|
|
|
|
fmt.Println("Successfully Created Category:", wordpressCategory.Name)
|
|
|
|
output = append(output, overallResult)
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
2024-05-16 16:26:59 +00:00
|
|
|
|
2024-05-16 18:50:53 +00:00
|
|
|
func submitResults(wordpressCategory wordpress.CategoryData, category slowtwitch.SlowtwitchCategory, slowtwitchUrl string, httpStatus int, errorMessage string, err error, migration *MigrateCategories) CategoryResult {
|
|
|
|
overallResult := CategoryResult{
|
|
|
|
WordpressId: wordpressCategory.Id,
|
|
|
|
SlowtwitchId: category.Id,
|
|
|
|
OldUrl: slowtwitchUrl,
|
|
|
|
OldUrlStatus: httpStatus,
|
|
|
|
NewUrl: wordpressCategory.Link,
|
|
|
|
IsSuccess: true,
|
|
|
|
ErrorMessage: errorMessage,
|
|
|
|
}
|
2024-05-16 16:26:59 +00:00
|
|
|
|
2024-05-16 18:50:53 +00:00
|
|
|
err = CreateCategoryResult(overallResult, migration.ResultsDatabase)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return overallResult
|
|
|
|
}
|
2024-05-16 16:26:59 +00:00
|
|
|
|
2024-05-16 18:50:53 +00:00
|
|
|
func createRedirect(wordpressCategory wordpress.CategoryData, migration *MigrateCategories, slowtwitchPath string, err error, errorMessage string) (error, string) {
|
|
|
|
redirectUrl := "/" + strings.ReplaceAll(wordpressCategory.Link, migration.WordpressBaseUrl, "")
|
|
|
|
createRedirect := wordpress.CreateRedirect{
|
|
|
|
Url: slowtwitchPath,
|
|
|
|
Title: "Category: " + wordpressCategory.Name,
|
|
|
|
MatchType: "page",
|
|
|
|
ActionType: "url",
|
|
|
|
ActionCode: 301,
|
|
|
|
GroupId: 1,
|
|
|
|
ActionData: wordpress.ActionData{
|
|
|
|
Url: redirectUrl,
|
|
|
|
},
|
2024-05-15 23:37:42 +00:00
|
|
|
}
|
2024-05-16 18:50:53 +00:00
|
|
|
_, err = createRedirect.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword)
|
|
|
|
if err != nil {
|
|
|
|
errorMessage = errorMessage + err.Error()
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return err, errorMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
func createInWordpress(category slowtwitch.SlowtwitchCategory, wordpressParentId int, migration *MigrateCategories) wordpress.CategoryData {
|
|
|
|
createWordpressCategory := wordpress.CreateCategory{
|
|
|
|
Name: strings.Trim(category.Name, " "),
|
|
|
|
Description: "",
|
|
|
|
ParentId: wordpressParentId,
|
|
|
|
}
|
|
|
|
wordpressCategory := createWordpressCategory.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword)
|
|
|
|
return wordpressCategory
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSlowtwitchUrlsAndVerify(category slowtwitch.SlowtwitchCategory) (string, string, int) {
|
|
|
|
slowtwitchPath := slowtwitch.ConvertUrlToCategoryFormat(category.FullName)
|
|
|
|
slowtwitchUrl := slowtwitch.GetURL(slowtwitchPath)
|
|
|
|
httpStatus := slowtwitch.GetPageStatus(slowtwitchUrl)
|
|
|
|
return slowtwitchPath, slowtwitchUrl, httpStatus
|
2024-05-16 16:26:59 +00:00
|
|
|
}
|
2024-05-15 23:37:42 +00:00
|
|
|
|
2024-05-16 16:26:59 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2024-05-15 23:37:42 +00:00
|
|
|
|
2024-05-16 16:26:59 +00:00
|
|
|
return 0, nil
|
|
|
|
}
|