Compare commits

..

No commits in common. "5119490b3de8f8a535cf08a67c5b19fac74f227c" and "a9362d02ceeb3ee5e55c2a975cb29085f35f805c" have entirely different histories.

5 changed files with 62 additions and 41 deletions

49
app-cache.go Normal file
View File

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

View File

@ -23,6 +23,7 @@ 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,12 +27,16 @@ 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"}
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())
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)
}
slowtwitchPostIdsForMigration := getPostIdsThatNeedMigration(slowtwitchPostIds, migratedPostIds)

View File

@ -2,7 +2,6 @@ package wordpress
import (
"encoding/json"
"errors"
"federated.computer/wp-sync-slowtwitch/utilities"
"net/url"
)
@ -13,18 +12,6 @@ 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)