Compare commits
No commits in common. "5119490b3de8f8a535cf08a67c5b19fac74f227c" and "a9362d02ceeb3ee5e55c2a975cb29085f35f805c" have entirely different histories.
5119490b3d
...
a9362d02ce
49
app-cache.go
Normal file
49
app-cache.go
Normal 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
28
main.go
@ -3,7 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"federated.computer/wp-sync-slowtwitch/services/migration"
|
"federated.computer/wp-sync-slowtwitch/services/migration"
|
||||||
"fmt"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const baseUrl = "https://slowtwitch.cloud/"
|
const baseUrl = "https://slowtwitch.cloud/"
|
||||||
@ -17,17 +16,12 @@ const migrationDbName = "slowtwitch_transfer"
|
|||||||
const federatedDbUrl = "slowtwitch.northend.network"
|
const federatedDbUrl = "slowtwitch.northend.network"
|
||||||
const federatedDbPort = "3306"
|
const federatedDbPort = "3306"
|
||||||
|
|
||||||
|
var appCache AppCache
|
||||||
var slowtwitchDB *sql.DB
|
var slowtwitchDB *sql.DB
|
||||||
var resultsDB *sql.DB
|
var resultsDB *sql.DB
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
connectToDatabases()
|
//Connect to databases
|
||||||
migrateAuthors()
|
|
||||||
migrateCategories()
|
|
||||||
migratePosts()
|
|
||||||
}
|
|
||||||
|
|
||||||
func connectToDatabases() {
|
|
||||||
slowtwitchDatabase, slowtwitchDbErr := migration.Connect(slowtwitchAdminUser, slowtwitchAdminPass, federatedDbUrl, federatedDbPort, slowtwitchDbName+"?parseTime=true")
|
slowtwitchDatabase, slowtwitchDbErr := migration.Connect(slowtwitchAdminUser, slowtwitchAdminPass, federatedDbUrl, federatedDbPort, slowtwitchDbName+"?parseTime=true")
|
||||||
if slowtwitchDbErr != nil {
|
if slowtwitchDbErr != nil {
|
||||||
panic("Could not connect to slowtwitch database.")
|
panic("Could not connect to slowtwitch database.")
|
||||||
@ -40,10 +34,7 @@ func connectToDatabases() {
|
|||||||
} else {
|
} else {
|
||||||
resultsDB = resultsDatabase
|
resultsDB = resultsDatabase
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func migrateAuthors() {
|
|
||||||
fmt.Println("Migrating Authors and Editors")
|
|
||||||
editorMigration := migration.MigrateAuthors{
|
editorMigration := migration.MigrateAuthors{
|
||||||
SlowtwitchDatabase: slowtwitchDB,
|
SlowtwitchDatabase: slowtwitchDB,
|
||||||
ResultsDatabase: resultsDB,
|
ResultsDatabase: resultsDB,
|
||||||
@ -51,12 +42,7 @@ func migrateAuthors() {
|
|||||||
WordpressUser: wordpressKey,
|
WordpressUser: wordpressKey,
|
||||||
WordpressPassword: wordpressSecret,
|
WordpressPassword: wordpressSecret,
|
||||||
}
|
}
|
||||||
editorResults := editorMigration.Execute()
|
editorMigration.Execute()
|
||||||
fmt.Println("Migrated", len(editorResults), "Editors and Authors")
|
|
||||||
}
|
|
||||||
|
|
||||||
func migrateCategories() {
|
|
||||||
fmt.Println("Migrating Categories")
|
|
||||||
categoryMigration := migration.MigrateCategories{
|
categoryMigration := migration.MigrateCategories{
|
||||||
SlowtwitchDatabase: slowtwitchDB,
|
SlowtwitchDatabase: slowtwitchDB,
|
||||||
ResultsDatabase: resultsDB,
|
ResultsDatabase: resultsDB,
|
||||||
@ -64,12 +50,7 @@ func migrateCategories() {
|
|||||||
WordpressUser: wordpressKey,
|
WordpressUser: wordpressKey,
|
||||||
WordpressPassword: wordpressSecret,
|
WordpressPassword: wordpressSecret,
|
||||||
}
|
}
|
||||||
categoryResults := categoryMigration.Execute()
|
categoryMigration.Execute()
|
||||||
fmt.Println("Migrated", len(categoryResults), "Categories")
|
|
||||||
}
|
|
||||||
|
|
||||||
func migratePosts() {
|
|
||||||
fmt.Println("Migrating Posts")
|
|
||||||
postMigration := migration.MigratePosts{
|
postMigration := migration.MigratePosts{
|
||||||
SlowtwitchDatabase: slowtwitchDB,
|
SlowtwitchDatabase: slowtwitchDB,
|
||||||
ResultsDatabase: resultsDB,
|
ResultsDatabase: resultsDB,
|
||||||
@ -78,5 +59,4 @@ func migratePosts() {
|
|||||||
WordpressPassword: wordpressSecret,
|
WordpressPassword: wordpressSecret,
|
||||||
}
|
}
|
||||||
postMigration.Execute()
|
postMigration.Execute()
|
||||||
fmt.Println("Migration complete, please check database for results.")
|
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ func (migration *MigrateCategories) Execute() []CategoryResult {
|
|||||||
hasBeenMigrated := HasBeenMigrated(category.Id, migration.ResultsDatabase)
|
hasBeenMigrated := HasBeenMigrated(category.Id, migration.ResultsDatabase)
|
||||||
|
|
||||||
if hasBeenMigrated {
|
if hasBeenMigrated {
|
||||||
|
fmt.Println("This category has already been migrated.")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,12 +27,16 @@ func (migration MigratePosts) Execute() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic("Could not migrate posts:" + err.Error())
|
panic("Could not migrate posts:" + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
//get wordpress tag data, there are only 3
|
//get wordpress tag data, there are only 3
|
||||||
tags := []string{"swim", "bike", "run"}
|
tags := []string{"swim", "bike", "run"}
|
||||||
wpTagData, wpTagErr := wordpress.GetTags(tags, migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword)
|
var wpTagData []wordpress.TagData
|
||||||
if wpTagErr != nil {
|
|
||||||
panic("Could not migrate posts due to tags not found:" + wpTagErr.Error())
|
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)
|
slowtwitchPostIdsForMigration := getPostIdsThatNeedMigration(slowtwitchPostIds, migratedPostIds)
|
||||||
|
@ -2,7 +2,6 @@ package wordpress
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"federated.computer/wp-sync-slowtwitch/utilities"
|
"federated.computer/wp-sync-slowtwitch/utilities"
|
||||||
"net/url"
|
"net/url"
|
||||||
)
|
)
|
||||||
@ -13,18 +12,6 @@ type TagData struct {
|
|||||||
Slug string `json:"slug"`
|
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) {
|
func GetTag(tagName, baseUrl, user, pass string) (TagData, bool) {
|
||||||
endpoint := baseUrl + "wp-json/wp/v2/tags?search=" + url.QueryEscape(tagName)
|
endpoint := baseUrl + "wp-json/wp/v2/tags?search=" + url.QueryEscape(tagName)
|
||||||
body := utilities.GetHttpRequestToWordpress(endpoint, user, pass)
|
body := utilities.GetHttpRequestToWordpress(endpoint, user, pass)
|
||||||
|
Loading…
Reference in New Issue
Block a user