re-factoring existing migrate posts file
This commit is contained in:
parent
270255b110
commit
732b238e52
@ -16,6 +16,7 @@ type MigrateAuthors struct {
|
||||
WordpressPassword string
|
||||
}
|
||||
|
||||
// TODO Add first and last name
|
||||
func (migration *MigrateAuthors) Execute() []EditorResult {
|
||||
editors := slowtwitch.GetUsers(migration.SlowtwitchDatabase)
|
||||
var output []EditorResult
|
||||
@ -24,11 +25,15 @@ func (migration *MigrateAuthors) Execute() []EditorResult {
|
||||
hasBeenMigrated := EditorHasBeenMigrated(editor.Username, migration.ResultsDatabase)
|
||||
|
||||
if hasBeenMigrated == false {
|
||||
firstName, lastName := getFirstAndLastName(editor.Name)
|
||||
|
||||
createUser := wordpress.CreateUser{
|
||||
Username: strings.Trim(editor.Username, " "),
|
||||
Email: strings.Trim(editor.Email, " "),
|
||||
Username: strings.TrimSpace(editor.Username),
|
||||
Email: strings.TrimSpace(editor.Email),
|
||||
Password: editor.Password,
|
||||
Roles: "editor",
|
||||
FirstName: firstName,
|
||||
LastName: lastName,
|
||||
}
|
||||
|
||||
result, err := createUser.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword)
|
||||
@ -58,3 +63,22 @@ func (migration *MigrateAuthors) Execute() []EditorResult {
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
func getFirstAndLastName(name string) (firstName string, lastName string) {
|
||||
if len(name) == 0 {
|
||||
return "", ""
|
||||
}
|
||||
|
||||
names := strings.Split(name, " ")
|
||||
|
||||
firstName = strings.TrimSpace(names[0])
|
||||
|
||||
lastName = ""
|
||||
if len(names) > 1 {
|
||||
for _, name := range names[1:] {
|
||||
lastName += " " + name
|
||||
}
|
||||
lastName = strings.TrimSpace(lastName)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -18,20 +18,14 @@ type MigratePosts struct {
|
||||
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
|
||||
panic("Could not migrate posts: " + err.Error())
|
||||
}
|
||||
migratedPostIds, err := GetAllMigratedPostIds(migration.ResultsDatabase)
|
||||
if err != nil {
|
||||
fmt.Println("Could not migrate posts:", err)
|
||||
return
|
||||
panic("Could not migrate posts:" + err.Error())
|
||||
}
|
||||
//get wordpress tag data, there are only 3
|
||||
tags := []string{"swim", "bike", "run"}
|
||||
@ -65,7 +59,7 @@ func (migration MigratePosts) Execute() {
|
||||
createWordpressPost.Date = timeString
|
||||
} else {
|
||||
errorMessage = errorMessage + "Invalid Date Published"
|
||||
// TODO SEND TO RESULTS DB WITH CALL
|
||||
createPostFailureResult(postId, errorMessage, migration.ResultsDatabase)
|
||||
continue
|
||||
}
|
||||
|
||||
@ -83,7 +77,7 @@ func (migration MigratePosts) Execute() {
|
||||
|
||||
if err != nil {
|
||||
errorMessage = errorMessage + err.Error()
|
||||
// TODO SEND TO RESULTS DB WITH CALL
|
||||
createPostFailureResult(postId, errorMessage, migration.ResultsDatabase)
|
||||
continue
|
||||
}
|
||||
|
||||
@ -93,7 +87,7 @@ func (migration MigratePosts) Execute() {
|
||||
categoryResult, err := GetSlowtwitchCategoryResult(slowtwitchCategoryId, migration.ResultsDatabase)
|
||||
if err != nil {
|
||||
errorMessage = errorMessage + err.Error()
|
||||
// TODO SEND TO RESULTS DB WITH CALL
|
||||
createPostFailureResult(postId, errorMessage, migration.ResultsDatabase)
|
||||
continue
|
||||
}
|
||||
wordPressCategoryIds = append(wordPressCategoryIds, categoryResult.WordpressId)
|
||||
@ -105,7 +99,7 @@ func (migration MigratePosts) Execute() {
|
||||
|
||||
if err != nil {
|
||||
errorMessage = errorMessage + err.Error()
|
||||
// TODO SEND TO RESULTS DB WITH CALL
|
||||
createPostFailureResult(postId, errorMessage, migration.ResultsDatabase)
|
||||
continue
|
||||
}
|
||||
createWordpressPost.Author = editor.WordpressId
|
||||
@ -115,7 +109,7 @@ func (migration MigratePosts) Execute() {
|
||||
|
||||
if linkStatus == 404 {
|
||||
errorMessage = errorMessage + "Page not found on Slowtwitch"
|
||||
// TODO SEND TO RESULTS DB WITH CALL
|
||||
createPostFailureResult(postId, errorMessage, migration.ResultsDatabase)
|
||||
continue
|
||||
}
|
||||
|
||||
@ -125,7 +119,7 @@ func (migration MigratePosts) Execute() {
|
||||
imagePaths, html, err := slowtwitch.GetImagesAndPostHtml(oldLink)
|
||||
if err != nil {
|
||||
errorMessage = errorMessage + err.Error()
|
||||
// TODO SEND TO RESULTS DB WITH CALL
|
||||
createPostFailureResult(postId, errorMessage, migration.ResultsDatabase)
|
||||
continue
|
||||
}
|
||||
|
||||
@ -141,7 +135,7 @@ func (migration MigratePosts) Execute() {
|
||||
|
||||
if err != nil {
|
||||
errorMessage = errorMessage + err.Error()
|
||||
// TODO SEND TO RESULTS DB WITH CALL
|
||||
createImageFailureResult(imageUrl, migration.ResultsDatabase)
|
||||
continue
|
||||
}
|
||||
//first photo is the featured photo
|
||||
@ -173,14 +167,20 @@ func (migration MigratePosts) Execute() {
|
||||
|
||||
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
|
||||
createPostFailureResult(postId, errorMessage, migration.ResultsDatabase)
|
||||
continue
|
||||
}
|
||||
//set up post result here to create
|
||||
//truncate error message for db
|
||||
if len(errorMessage) > 1450 {
|
||||
errorMessage = errorMessage[:1450]
|
||||
}
|
||||
|
||||
postResult := PostResult{
|
||||
SlowtwitchId: postId,
|
||||
WordpressId: post.Id,
|
||||
@ -205,7 +205,7 @@ func (migration MigratePosts) Execute() {
|
||||
oldPath := strings.ReplaceAll(oldLink, "https://www.slowtwitch.com", "")
|
||||
postRedirect := wordpress.CreateRedirect{
|
||||
Title: "Article Redirect" + postBase.Title,
|
||||
Url: strings.ReplaceAll(oldPath, ".html", ""),
|
||||
Url: oldPath,
|
||||
MatchType: "page",
|
||||
ActionType: "url",
|
||||
ActionCode: 301,
|
||||
@ -216,11 +216,24 @@ func (migration MigratePosts) Execute() {
|
||||
}
|
||||
_, err = postRedirect.Execute(migration.WordpressBaseUrl, migration.WordpressUser, migration.WordpressPassword)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
fmt.Println("Error creating redirect for", postId, ":"+err.Error())
|
||||
}
|
||||
fmt.Println("Successfully created post and result for", postId)
|
||||
// TODO Update advanced Custom Fields with images
|
||||
//Install ACF and create field
|
||||
/*
|
||||
"acf": {
|
||||
"post_images": [...ids]
|
||||
}
|
||||
//Update related posts (get from post results db) as second loop
|
||||
//Update advanced Custom Fields with images
|
||||
*/
|
||||
}
|
||||
// TODO Update related posts (get from post results db) as second loop
|
||||
//Install ACF and create field
|
||||
/*
|
||||
"acf": {
|
||||
"related_posts": [...ids]
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
func getPostIdsThatNeedMigration(slowtwitchPostIds, migratedPostIds []int) []int {
|
||||
@ -235,3 +248,39 @@ func getPostIdsThatNeedMigration(slowtwitchPostIds, migratedPostIds []int) []int
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
func createPostFailureResult(slowtwitchId int, errorMessage string, migrationDb *sql.DB) {
|
||||
fmt.Println("Error creating post: ", slowtwitchId, ":", errorMessage)
|
||||
|
||||
postResult := PostResult{
|
||||
SlowtwitchId: slowtwitchId,
|
||||
WordpressId: 0,
|
||||
OldUrl: "",
|
||||
OldUrlStatus: 0,
|
||||
NewUrl: "",
|
||||
IsSuccess: false,
|
||||
ErrorMessage: errorMessage,
|
||||
}
|
||||
|
||||
_, err := CreatePostResult(postResult, migrationDb)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Failed to create failure result: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
func createImageFailureResult(url string, resultsDatabase *sql.DB) {
|
||||
fmt.Println("Error creating image failure result: ", url)
|
||||
|
||||
imageResult := ImageResult{
|
||||
OldUrl: url,
|
||||
NewUrl: "",
|
||||
WordpressId: 0,
|
||||
IsSuccess: false,
|
||||
}
|
||||
|
||||
err := CreateImageResult(imageResult, resultsDatabase)
|
||||
if err != nil {
|
||||
fmt.Println("Failed to create failure result: ", err)
|
||||
}
|
||||
}
|
||||
|
@ -9,10 +9,11 @@ type SlowtwitchUser struct {
|
||||
Username string
|
||||
Password string
|
||||
Email string
|
||||
Name string
|
||||
}
|
||||
|
||||
func GetUsers(db *sql.DB) []SlowtwitchUser {
|
||||
rows, err := db.Query("select Username, Password, Email from glinks_Users;")
|
||||
rows, err := db.Query("select Username, Password, Email, Name from glinks_Users;")
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
@ -24,7 +25,7 @@ func GetUsers(db *sql.DB) []SlowtwitchUser {
|
||||
|
||||
for rows.Next() {
|
||||
user := SlowtwitchUser{}
|
||||
err := rows.Scan(&user.Username, &user.Password, &user.Email)
|
||||
err := rows.Scan(&user.Username, &user.Password, &user.Email, &user.Name)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
|
@ -10,6 +10,8 @@ type CreateUser struct {
|
||||
Email string `json:"email"`
|
||||
Roles string `json:"roles"`
|
||||
Password string `json:"password"`
|
||||
FirstName string `json:"first_name"`
|
||||
LastName string `json:"last_name"`
|
||||
}
|
||||
|
||||
type CreateUserResponse struct {
|
||||
|
Loading…
Reference in New Issue
Block a user