package migration import ( "database/sql" "federated.computer/wp-sync-slowtwitch/services/slowtwitch" "federated.computer/wp-sync-slowtwitch/services/wordpress" "fmt" "slices" "strconv" "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 } //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) } 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, Status: "publish", } if postBase.DatePublished.Valid { t := postBase.DatePublished.Time timeString := fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second()) createWordpressPost.Date = timeString } else { errorMessage = errorMessage + "Invalid Date Published" // TODO SEND TO RESULTS DB WITH CALL continue } for _, tag := range wpTagData { if postBase.Bike == true && tag.Name == "bike" { createWordpressPost.Tags = append(createWordpressPost.Tags, tag.Id) } if postBase.Swim == true && tag.Name == "swim" { createWordpressPost.Tags = append(createWordpressPost.Tags, tag.Id) } if postBase.Run == true && tag.Name == "run" { createWordpressPost.Tags = append(createWordpressPost.Tags, tag.Id) } } 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 imagePaths, html, err := slowtwitch.GetImagesAndPostHtml(oldLink) if err != nil { errorMessage = errorMessage + err.Error() // TODO SEND TO RESULTS DB WITH CALL continue } var imageResults []ImageResult for i, imagePath := range imagePaths { imageUrl := "https://www.slowtwitch.com" + imagePath createWordpressImage := wordpress.CreateImage{ Url: imageUrl, } wordpressImage, err := createWordpressImage.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword) if err != nil { errorMessage = errorMessage + err.Error() // TODO SEND TO RESULTS DB WITH CALL continue } //first photo is the featured photo if i == 0 { createWordpressPost.FeaturedMedia = wordpressImage.Id } //begin process of recording result imageResult := ImageResult{ OldUrl: imageUrl, NewUrl: wordpressImage.Link, WordpressId: wordpressImage.Id, IsSuccess: true, } imageResults = append(imageResults, imageResult) //replace old links with new in post html strings.ReplaceAll(html, imageUrl, wordpressImage.Link) //create redirect createRedirect := wordpress.CreateRedirect{ Title: postBase.Title + "image-" + string((i + 1)), Url: imagePath, MatchType: "page", ActionType: "url", ActionCode: 301, GroupId: 1, ActionData: wordpress.ActionData{ Url: "/" + wordpressImage.Slug, }, } createRedirect.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword) } createWordpressPost.Content = html post, err := createWordpressPost.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword) if err != nil { errorMessage = errorMessage + err.Error() // TODO SEND TO RESULTS DB WITH CALL continue } //set up post result here to create postResult := PostResult{ SlowtwitchId: postId, WordpressId: post.Id, OldUrl: oldLink, OldUrlStatus: linkStatus, NewUrl: post.Link, IsSuccess: true, ErrorMessage: errorMessage, } postResultId, err := CreatePostResult(postResult, migration.ResultsDatabase) if err != nil { fmt.Println("Could not record post result for Slowtwitch post:" + strconv.Itoa(postId) + err.Error()) } for _, imageResult := range imageResults { imageResult.PostId = postResultId err := CreateImageResult(imageResult, migration.ResultsDatabase) if err != nil { fmt.Println("Error recording image result") } } oldPath := strings.ReplaceAll(oldLink, "https://www.slowtwitch.com", "") postRedirect := wordpress.CreateRedirect{ Title: "Article Redirect" + postBase.Title, Url: strings.ReplaceAll(oldPath, ".html", ""), MatchType: "page", ActionType: "url", ActionCode: 301, GroupId: 1, ActionData: wordpress.ActionData{ Url: "/" + strings.ReplaceAll(postResult.NewUrl, migration.WordpressBaseUrl, ""), }, } _, err = postRedirect.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword) if err != nil { fmt.Println(err) } } //Update related posts (get from post results db) as second loop //Update advanced Custom Fields with images } 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 }