116 lines
3.2 KiB
Go
116 lines
3.2 KiB
Go
|
package migration
|
||
|
|
||
|
import (
|
||
|
"database/sql"
|
||
|
"federated.computer/wp-sync-slowtwitch/services/slowtwitch"
|
||
|
"federated.computer/wp-sync-slowtwitch/services/wordpress"
|
||
|
"fmt"
|
||
|
"slices"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
type MigratePosts struct {
|
||
|
SlowtwitchDatabase *sql.DB
|
||
|
ResultsDatabase *sql.DB
|
||
|
WordpressBaseUrl string
|
||
|
WordpressUser string
|
||
|
WordpressPassword string
|
||
|
}
|
||
|
|
||
|
// TODO Get list of all ST post IDs
|
||
|
// TODO Get ST post by ID
|
||
|
// TODO Get List of all already migrated post IDs
|
||
|
|
||
|
func (migration MigratePosts) Execute() {
|
||
|
slowtwitchPostIds, err := slowtwitch.GetAllPostIds(migration.SlowtwitchDatabase)
|
||
|
if err != nil {
|
||
|
fmt.Println("Could not migrate posts:", err)
|
||
|
return
|
||
|
}
|
||
|
migratedPostIds, err := GetAllMigratedPostIds(migration.ResultsDatabase)
|
||
|
if err != nil {
|
||
|
fmt.Println("Could not migrate posts:", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
slowtwitchPostIdsForMigration := getPostIdsThatNeedMigration(slowtwitchPostIds, migratedPostIds)
|
||
|
|
||
|
for _, postId := range slowtwitchPostIdsForMigration {
|
||
|
errorMessage := ""
|
||
|
//migrate
|
||
|
postBase, err := slowtwitch.GetPostBase(postId, migration.SlowtwitchDatabase)
|
||
|
createWordpressPost := wordpress.CreatePost{
|
||
|
Title: postBase.Title,
|
||
|
Excerpt: postBase.Description,
|
||
|
}
|
||
|
|
||
|
if postBase.DatePublished.Valid {
|
||
|
createWordpressPost.Date = postBase.DatePublished.Time.String()
|
||
|
} else {
|
||
|
errorMessage = errorMessage + "Invalid Date Published"
|
||
|
// TODO SEND TO RESULTS DB WITH CALL
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
if err != nil {
|
||
|
errorMessage = errorMessage + err.Error()
|
||
|
// TODO SEND TO RESULTS DB WITH CALL
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
var wordPressCategoryIds []int
|
||
|
var firstCategoryResult CategoryResult
|
||
|
for _, slowtwitchCategoryId := range postBase.CategoryIds {
|
||
|
categoryResult, err := GetSlowtwitchCategoryResult(slowtwitchCategoryId, migration.ResultsDatabase)
|
||
|
if err != nil {
|
||
|
errorMessage = errorMessage + err.Error()
|
||
|
// TODO SEND TO RESULTS DB WITH CALL
|
||
|
continue
|
||
|
}
|
||
|
wordPressCategoryIds = append(wordPressCategoryIds, categoryResult.WordpressId)
|
||
|
firstCategoryResult = categoryResult
|
||
|
}
|
||
|
createWordpressPost.Categories = wordPressCategoryIds
|
||
|
//Get Author ID
|
||
|
editor, err := GetEditor(postBase.Author, migration.ResultsDatabase)
|
||
|
|
||
|
if err != nil {
|
||
|
errorMessage = errorMessage + err.Error()
|
||
|
// TODO SEND TO RESULTS DB WITH CALL
|
||
|
continue
|
||
|
}
|
||
|
createWordpressPost.Author = editor.WordpressId
|
||
|
//Get old link
|
||
|
oldLink := strings.ReplaceAll(firstCategoryResult.OldUrl, "index.html", "") + slowtwitch.ConvertPostTitleToPath(postBase.Title, postBase.Id)
|
||
|
linkStatus := slowtwitch.GetPageStatus(oldLink)
|
||
|
|
||
|
if linkStatus == 404 {
|
||
|
errorMessage = errorMessage + "Page not found on Slowtwitch"
|
||
|
// TODO SEND TO RESULTS DB WITH CALL
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
//Get page, parse out post data and images
|
||
|
//Upload images to wordpress, swap out with new image urls
|
||
|
//Submit
|
||
|
|
||
|
//Get new link
|
||
|
//Store results
|
||
|
//Update advanced Custom Fields with images
|
||
|
}
|
||
|
//Update related posts (get from post results db) as second loop
|
||
|
}
|
||
|
|
||
|
func getPostIdsThatNeedMigration(slowtwitchPostIds, migratedPostIds []int) []int {
|
||
|
var output []int
|
||
|
|
||
|
for _, slowtwitchPostId := range slowtwitchPostIds {
|
||
|
hasId := slices.Contains(migratedPostIds, slowtwitchPostId)
|
||
|
if hasId == false {
|
||
|
output = append(output, slowtwitchPostId)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return output
|
||
|
}
|