migrate categories - needs cleaning
This commit is contained in:
parent
5737ce23d1
commit
83ef9f4192
38
main.go
38
main.go
@ -1,13 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"federated.computer/wp-sync-slowtwitch/services/wordpress"
|
"federated.computer/wp-sync-slowtwitch/services/migration"
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
const baseUrl = "https://go-api-playground.local/wp-json/"
|
const baseUrl = "https://slowtwitch.cloud/wp-json/"
|
||||||
const wordpressKey = "admin"
|
const wordpressKey = "admin@slowtwitch.cloud"
|
||||||
const wordpressSecret = "S34E keY1 A1uX 6ncs Rx4T f21W"
|
const wordpressSecret = "6zY7 xsKZ dGIt l1Lp ypIK 6TWh"
|
||||||
|
|
||||||
const slowtwitchAdminUser = "admin"
|
const slowtwitchAdminUser = "admin"
|
||||||
const slowtwitchAdminPass = "yxnh93Ybbz2Nm8#mp28zCVv"
|
const slowtwitchAdminPass = "yxnh93Ybbz2Nm8#mp28zCVv"
|
||||||
@ -22,23 +22,21 @@ func main() {
|
|||||||
// TODO Category migration
|
// TODO Category migration
|
||||||
// TODO User migration
|
// TODO User migration
|
||||||
// TODO Article migration
|
// TODO Article migration
|
||||||
//slowtwitchDB, err := migration.Connect(slowtwitchAdminUser, slowtwitchAdminPass, federatedDbUrl, federatedDbPort, slowtwitchDbName)
|
slowtwitchDB, slowtwitchDbErr := migration.Connect(slowtwitchAdminUser, slowtwitchAdminPass, federatedDbUrl, federatedDbPort, slowtwitchDbName)
|
||||||
|
if slowtwitchDbErr != nil {
|
||||||
createRedirect := wordpress.CreateRedirect{
|
fmt.Println(slowtwitchDbErr)
|
||||||
Url: "/gotest",
|
}
|
||||||
Title: "Test From Go",
|
resultsDB, resultsDBerr := migration.Connect(slowtwitchAdminUser, slowtwitchAdminPass, federatedDbUrl, federatedDbPort, migrationDbName)
|
||||||
MatchType: "page",
|
if resultsDBerr != nil {
|
||||||
ActionType: "url",
|
fmt.Println(resultsDBerr)
|
||||||
ActionCode: 301,
|
|
||||||
GroupId: 1,
|
|
||||||
ActionData: wordpress.ActionData{
|
|
||||||
Url: "/GoTestResult",
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := createRedirect.Execute(baseUrl, wordpressKey, wordpressSecret)
|
categoryMigration := migration.MigrateCategories{
|
||||||
if err != nil {
|
SlowtwitchDatabase: slowtwitchDB,
|
||||||
panic(err)
|
ResultsDatabase: resultsDB,
|
||||||
|
WordpressBaseUrl: baseUrl,
|
||||||
|
WordpressUser: wordpressKey,
|
||||||
|
WordpressPassword: wordpressSecret,
|
||||||
}
|
}
|
||||||
fmt.Println(result)
|
categoryMigration.Execute()
|
||||||
}
|
}
|
||||||
|
@ -17,10 +17,20 @@ type CategoryResult struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func CreateCategoryResult(result CategoryResult, db *sql.DB) error {
|
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
|
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) {
|
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)
|
rows, err := db.Query("select WordpressId, SlowtwitchId, OldUrl, NewUrl, (IsSuccess = b'1'), ErrorMessage, OldUrlStatus from CategoryResults where SlowtwitchId = ?", slowtwitchId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1,18 +1,100 @@
|
|||||||
package migration
|
package migration
|
||||||
|
|
||||||
//THIS SHOULD BE A STRUCT WITH AN EXECUTE
|
import (
|
||||||
/*import (
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"federated.computer/wp-sync-slowtwitch/services/slowtwitch"
|
"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) {
|
type MigrateCategories struct {
|
||||||
slowtwitchCategories := slowtwitch.GetCategories(slowtwitchDB)
|
SlowtwitchDatabase *sql.DB
|
||||||
|
ResultsDatabase *sql.DB
|
||||||
for _, category := range slowtwitchCategories {
|
WordpressBaseUrl string
|
||||||
|
WordpressUser string
|
||||||
|
WordpressPassword string
|
||||||
}
|
}
|
||||||
|
|
||||||
var results []CategoryResult
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
@ -11,6 +11,8 @@ type CategoryData struct {
|
|||||||
ParentId int `json:"parent_id"`
|
ParentId int `json:"parent_id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
ParentName string `json:"parent_name"`
|
ParentName string `json:"parent_name"`
|
||||||
|
Link string `json:"link"`
|
||||||
|
Slug string `json:"slug"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetCategory(name, baseUrl, user, pass string) (CategoryData, bool) {
|
func GetCategory(name, baseUrl, user, pass string) (CategoryData, bool) {
|
||||||
|
Loading…
Reference in New Issue
Block a user