Compare commits

..

2 Commits

Author SHA1 Message Date
5119490b3d refactor main 2024-05-21 11:37:55 -06:00
cb6dd813f5 cleaned up upost migration for usability 2024-05-21 11:31:59 -06:00
5 changed files with 41 additions and 62 deletions

View File

@ -1,49 +0,0 @@
package main
import "federated.computer/wp-sync-slowtwitch/services/wordpress"
type AppCache struct {
UsersCache map[string]wordpress.UserData
TagCache map[string]wordpress.TagData
CategoryCache map[string]wordpress.CategoryData
}
func (appCache *AppCache) GetUser(username string) (wordpress.UserData, bool) {
user, ok := appCache.UsersCache[username]
return user, ok
}
func (appCache *AppCache) SetUser(user wordpress.UserData) {
if appCache.UsersCache == nil {
appCache.UsersCache = make(map[string]wordpress.UserData)
}
appCache.UsersCache[user.Name] = user
}
func (appCache *AppCache) GetTag(tagName string) (wordpress.TagData, bool) {
tag, ok := appCache.TagCache[tagName]
return tag, ok
}
func (appCache *AppCache) SetTag(tag wordpress.TagData) {
if appCache.TagCache == nil {
appCache.TagCache = make(map[string]wordpress.TagData)
}
appCache.TagCache[tag.Name] = tag
}
func (appCache *AppCache) GetCategory(categoryName string) (wordpress.CategoryData, bool) {
if appCache.CategoryCache != nil {
category, ok := appCache.CategoryCache[categoryName]
return category, ok
}
return wordpress.CategoryData{}, false
}
func (appCache *AppCache) SetCategory(category wordpress.CategoryData) {
if appCache.CategoryCache == nil {
appCache.CategoryCache = make(map[string]wordpress.CategoryData)
}
appCache.CategoryCache[category.Name] = category
}

28
main.go
View File

@ -3,6 +3,7 @@ package main
import (
"database/sql"
"federated.computer/wp-sync-slowtwitch/services/migration"
"fmt"
)
const baseUrl = "https://slowtwitch.cloud/"
@ -16,12 +17,17 @@ const migrationDbName = "slowtwitch_transfer"
const federatedDbUrl = "slowtwitch.northend.network"
const federatedDbPort = "3306"
var appCache AppCache
var slowtwitchDB *sql.DB
var resultsDB *sql.DB
func main() {
//Connect to databases
connectToDatabases()
migrateAuthors()
migrateCategories()
migratePosts()
}
func connectToDatabases() {
slowtwitchDatabase, slowtwitchDbErr := migration.Connect(slowtwitchAdminUser, slowtwitchAdminPass, federatedDbUrl, federatedDbPort, slowtwitchDbName+"?parseTime=true")
if slowtwitchDbErr != nil {
panic("Could not connect to slowtwitch database.")
@ -34,7 +40,10 @@ func main() {
} else {
resultsDB = resultsDatabase
}
}
func migrateAuthors() {
fmt.Println("Migrating Authors and Editors")
editorMigration := migration.MigrateAuthors{
SlowtwitchDatabase: slowtwitchDB,
ResultsDatabase: resultsDB,
@ -42,7 +51,12 @@ func main() {
WordpressUser: wordpressKey,
WordpressPassword: wordpressSecret,
}
editorMigration.Execute()
editorResults := editorMigration.Execute()
fmt.Println("Migrated", len(editorResults), "Editors and Authors")
}
func migrateCategories() {
fmt.Println("Migrating Categories")
categoryMigration := migration.MigrateCategories{
SlowtwitchDatabase: slowtwitchDB,
ResultsDatabase: resultsDB,
@ -50,7 +64,12 @@ func main() {
WordpressUser: wordpressKey,
WordpressPassword: wordpressSecret,
}
categoryMigration.Execute()
categoryResults := categoryMigration.Execute()
fmt.Println("Migrated", len(categoryResults), "Categories")
}
func migratePosts() {
fmt.Println("Migrating Posts")
postMigration := migration.MigratePosts{
SlowtwitchDatabase: slowtwitchDB,
ResultsDatabase: resultsDB,
@ -59,4 +78,5 @@ func main() {
WordpressPassword: wordpressSecret,
}
postMigration.Execute()
fmt.Println("Migration complete, please check database for results.")
}

View File

@ -23,7 +23,6 @@ func (migration *MigrateCategories) Execute() []CategoryResult {
hasBeenMigrated := HasBeenMigrated(category.Id, migration.ResultsDatabase)
if hasBeenMigrated {
fmt.Println("This category has already been migrated.")
continue
}

View File

@ -27,16 +27,12 @@ func (migration MigratePosts) Execute() {
if err != nil {
panic("Could not migrate posts:" + err.Error())
}
//get wordpress tag data, there are only 3
tags := []string{"swim", "bike", "run"}
var wpTagData []wordpress.TagData
for _, tag := range tags {
tagData, ok := wordpress.GetTag(tag, migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword)
if ok == false {
panic("could not get tag data from wp")
}
wpTagData = append(wpTagData, tagData)
wpTagData, wpTagErr := wordpress.GetTags(tags, migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword)
if wpTagErr != nil {
panic("Could not migrate posts due to tags not found:" + wpTagErr.Error())
}
slowtwitchPostIdsForMigration := getPostIdsThatNeedMigration(slowtwitchPostIds, migratedPostIds)

View File

@ -2,6 +2,7 @@ package wordpress
import (
"encoding/json"
"errors"
"federated.computer/wp-sync-slowtwitch/utilities"
"net/url"
)
@ -12,6 +13,18 @@ type TagData struct {
Slug string `json:"slug"`
}
func GetTags(tags []string, baseUrl, user, pass string) ([]TagData, error) {
var wpTagData []TagData
for _, tag := range tags {
tagData, ok := GetTag(tag, baseUrl, user, pass)
if ok == false {
return wpTagData, errors.New("Tag Not Found")
}
wpTagData = append(wpTagData, tagData)
}
return wpTagData, nil
}
func GetTag(tagName, baseUrl, user, pass string) (TagData, bool) {
endpoint := baseUrl + "wp-json/wp/v2/tags?search=" + url.QueryEscape(tagName)
body := utilities.GetHttpRequestToWordpress(endpoint, user, pass)