/// <summary> /// Runs the rewrite process as a background thread. /// </summary> /// <param name="sender">Rewrite DoWork dvent.</param> /// <param name="e">DoWorkEvent arguments.</param> /// <history> /// Sean Patterson 11/16/2010 [Created] /// </history> private void rewriteWorker_DoWork(object sender, DoWorkEventArgs e) { WorkerArgs myArgs = (WorkerArgs)e.Argument; Services myService = new Services(); Post origPost; Post updatePost; myArgs.status = "Starting rewrite process..."; rewriteWorker.ReportProgress(0, myArgs); foreach(LogData logItem in App.itemsToRewrite) { origPost = myService.GetPost (App.rewriteBlog.serviceUrl, logItem.sourceId, App.rewriteBlog.username, App.rewriteBlog.password); myArgs.status = "Rewriting post: " + origPost.title; rewriteWorker.ReportProgress(15, myArgs); updatePost = origPost; string newUrl = "<a href='" + logItem.destinationUrl + "'>" + logItem.destinationUrl + "</a>"; string newMessage = App.rewriteMessage.Replace("[URL]", newUrl); updatePost.description = newMessage; myService.UpdatePost(App.rewriteBlog.serviceUrl, App.rewriteBlog.username, App.rewriteBlog.password, updatePost); } }
/// <summary> /// Runs the get all posts process as a background thread. /// </summary> /// <param name="sender">DoWork event.</param> /// <param name="e">DoWorkEvent arguments.</param> /// <history> /// Sean Patterson 11/1/2010 [Created] /// </history> private void allPostsWorker_DoWork(object sender, DoWorkEventArgs e) { WorkerArgs myArgs = (WorkerArgs)e.Argument; Services myService = new Services(); BlogSource myBlog = App.sourceBlog; myArgs.status = "Retrieving post from source..."; allPostsWorker.ReportProgress(0, myArgs); try { // Clear out any old blog entries. App.sourceBlog.blogData = new BlogML.blogType(); App.sourceBlog.blogPosts = new List<Post>(); App.sourceBlog.postsToMigrate = new List<int>(); // Retrieve data via XML-RPC unless a source file has been // specified. if (String.IsNullOrEmpty(myBlog.blogFile)) { myArgs.status = "Connecting to source..."; allPostsWorker.ReportProgress(10, myArgs); myBlog.blogPosts = myService.GetAllPosts (myBlog.serviceUrl, myBlog.blogId, myBlog.username, myBlog.password); } else { myArgs.status = "Parsing file for posts..."; allPostsWorker.ReportProgress(10, myArgs); XmlSerializer serializer = new XmlSerializer (typeof(BlogML.blogType)); TextReader reader = new StreamReader(myBlog.blogFile); myBlog.blogData = (BlogML.blogType) serializer.Deserialize(reader); reader.Close(); myArgs.status = "Posts retrieved."; allPostsWorker.ReportProgress(100, myArgs); } } catch (Exception ex) { MessageBox.Show("An error occurred retrieving blog posts:" + Environment.NewLine + Environment.NewLine + ex.ToString() + Environment.NewLine + Environment.NewLine + "Please verify your settings and try retrieving " + "posts again.", "Error Retrieving Posts", MessageBoxButton.OK, MessageBoxImage.Error); allPostsWorker.CancelAsync(); } }
/// <summary> /// Inserts a sample post in the destination server. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnInsertSample_Click(object sender, RoutedEventArgs e) { Services myService = new Services(); string serviceUrl = ""; string status; if (cmbDestinationService.SelectedIndex > -1) { ComboBoxItem itemDest = (ComboBoxItem)cmbDestinationService.SelectedItem; try { switch (itemDest.Tag.ToString()) { case "SS": serviceUrl = "http://www.squarespace.com/process/service/PostInterceptor"; break; case "WP": serviceUrl = txtDestinationBlogUrl.Text + "/xmlrpc.php"; break; case "ASPNet": serviceUrl = "http://weblogs.asp.net/metablog.ashx"; break; case "OTHER": serviceUrl = txtDestinationServiceUrl.Text; break; } if (itemDest.Tag.ToString() != "FILE") { status = myService.InsertSamplePost (serviceUrl, txtDestinationBlogId.Text, txtDestinationUser.Text, txtDestinationPassword.Text); } else { status = "No test post. Destination is a file."; } MessageBox.Show(status, "Add Sample Post Result", MessageBoxButton.OK, MessageBoxImage.Information); } catch (Exception ex) { MessageBox.Show("Error inserting test post:" + Environment.NewLine + Environment.NewLine + ex.ToString() + Environment.NewLine + Environment.NewLine + "Please check your settings and try again.", "Error adding sample post.", MessageBoxButton.OK, MessageBoxImage.Error); } } else { MessageBox.Show("Error inserting test post. " + "No source type specified.", "Error adding sample post.", MessageBoxButton.OK, MessageBoxImage.Error); } }
/// <summary> /// Tests the destination connection by attempting to retrieve a post. /// </summary> /// <param name="sender">Test Source button click event.</param> /// <param name="e">Button click event arguments.</param> /// <history> /// Sean Patterson [11/11/2010] Created /// </history> private void btnTestDestination_Click(object sender, RoutedEventArgs e) { string status; string serviceUrl = ""; Services myService = new Services(); if (cmbDestinationService.SelectedIndex > -1) { ComboBoxItem itemDest = (ComboBoxItem)cmbDestinationService.SelectedItem; switch (itemDest.Tag.ToString()) { case "SS": serviceUrl = "http://www.squarespace.com/process/service/PostInterceptor"; break; case "WP": serviceUrl = txtDestinationBlogUrl.Text + "/xmlrpc.php"; break; case "ASPNet": serviceUrl = "http://weblogs.asp.net/metablog.ashx"; break; case "OTHER": serviceUrl = txtDestinationServiceUrl.Text; break; } if (itemDest.Tag.ToString() != "FILE") { status = myService.CheckServerStatus (serviceUrl, txtDestinationBlogId.Text, txtDestinationUser.Text, txtDestinationPassword.Text); } else { // There is no need to test if the destination file exists, only // that a location has been specified. if (!string.IsNullOrEmpty(txtDestinationFile.Text)) { status = "Connection successful."; } else { status = "Connection failed. No file specified."; } } } else { status = "Connection failed. No service type specified."; } MessageBox.Show(status, "Test Destination Connection Result", MessageBoxButton.OK, MessageBoxImage.Information); }
/// <summary> /// Saves the destination connection details into the application /// variable. /// </summary> /// <param name="sender">Save button click event.</param> /// <param name="e">Button click event arguments.</param> /// <history> /// Sean Patterson [11/11/2010] Created /// </history> private void btnSaveSource_Click(object sender, RoutedEventArgs e) { Services myService = new Services(); if (cmbDestinationService.SelectedIndex > -1) { ComboBoxItem itemDest = (ComboBoxItem)cmbDestinationService.SelectedItem; App.destBlog = new BlogSource(); App.destBlog.serviceType = itemDest.Tag.ToString(); switch (itemDest.Tag.ToString()) { case "SS": App.destBlog.serviceUrl = "http://www.squarespace.com/process/service/PostInterceptor"; break; case "WP": App.destBlog.serviceUrl = txtDestinationBlogUrl.Text + "/xmlrpc.php"; break; case "ASPNet": App.destBlog.serviceUrl = "http://weblogs.asp.net/metablog.ashx"; break; case "OTHER": App.destBlog.serviceUrl = txtDestinationServiceUrl.Text; break; case "FILE": App.destBlog.serviceUrl = txtDestinationServiceUrl.Text; break; } App.destBlog.rootUrl = txtDestinationBlogUrl.Text; App.destBlog.blogId = txtDestinationBlogId.Text; App.destBlog.username = txtDestinationUser.Text; App.destBlog.password = txtDestinationPassword.Text; App.destBlog.blogFile = txtDestinationFile.Text; if (itemDest.Tag.ToString() != "FILE") { ((MainWindow)this.Owner).btnConfigureSource.Content = "Configure Source Blog" + Environment.NewLine + Environment.NewLine + itemDest.Tag.ToString() + " - " + txtDestinationBlogUrl.Text; } else { ((MainWindow)this.Owner).btnConfigureSource.Content = "Configure Source Blog" + Environment.NewLine + Environment.NewLine + itemDest.Tag.ToString() + " - " + txtDestinationFile.Text; } MessageBox.Show("Destination Configuration Saved.", "Configuration Saved.", MessageBoxButton.OK, MessageBoxImage.Information); } else { MessageBox.Show("Please specify a service type.", "Configuration Not Saved.", MessageBoxButton.OK, MessageBoxImage.Error); } }
/// <summary> /// Tests the source connection by attempting to retrieve a post. /// </summary> /// <param name="sender">Test Source button click event.</param> /// <param name="e">Button click event arguments.</param> /// <history> /// Sean Patterson [11/11/2010] Created /// </history> private void btnTestSource_Click(object sender, RoutedEventArgs e) { string status; string serviceUrl = ""; Services myService = new Services(); if (cmbSourceService.SelectedIndex > -1) { ComboBoxItem itemSource = (ComboBoxItem)cmbSourceService.SelectedItem; switch (itemSource.Tag.ToString()) { case "SS": serviceUrl = "http://www.squarespace.com/process/service/PostInterceptor"; break; case "WP": serviceUrl = txtSourceBlogUrl.Text + "/xmlrpc.php"; break; case "ASPNet": serviceUrl = "http://weblogs.asp.net/metablog.ashx"; break; case "OTHER": serviceUrl = txtSourceServiceUrl.Text; break; } if (itemSource.Tag.ToString() != "FILE") { status = myService.CheckServerStatus (serviceUrl, txtSourceBlogId.Text, txtSourceUser.Text, txtSourcePassword.Text); } else { if (System.IO.File.Exists(txtSourceFile.Text)) { status = "Connection successful."; } else { status = "Connection failed. File not found."; } } } else { status = "Connection failed. No service type specified."; } MessageBox.Show(status, "Test Source Connection Result", MessageBoxButton.OK, MessageBoxImage.Information); }
/// <summary> /// Runs the conversion process as a background thread. /// </summary> /// <param name="sender">Conversion DoWork dvent.</param> /// <param name="e">DoWorkEvent arguments.</param> /// <history> /// Sean Patterson 11/1/2010 [Created] /// </history> private void migrationWorker_DoWork(object sender, DoWorkEventArgs e) { WorkerArgs myArgs = (WorkerArgs)e.Argument; Generator myGenerator = new Generator(); Services myService = new Services(); // Load document. myArgs.status = "Starting migration process..."; migrationWorker.ReportProgress(0, myArgs); switch (DetermineMigrationMethod()) { case "XMLtoXML": WriteXMLPosts(); break; case "FILEtoXML": ImportXMLPosts(); break; case "XMLtoWXR": myGenerator.WriteWXRDocument(App.sourceBlog.blogPosts, App.sourceBlog.postsToMigrate, App.sourceBlog.rootUrl, App.destBlog.blogFile); break; case "FILEtoWXR": myGenerator.WriteWXRDocument(App.sourceBlog.blogData, App.sourceBlog.postsToMigrate, App.sourceBlog.rootUrl, App.destBlog.blogFile); break; default: throw new Exception("No migration method found."); } myArgs.status = "Migration process complete."; migrationWorker.ReportProgress(100, myArgs); }
/// <summary> /// Publishes posts to the destination blog from a BlogML object. /// </summary> /// <history> /// Sean Patterson 11/15/2010 [Created] /// </history> public void ImportXMLPosts() { Services blogService = new Services(); Post resultPost; Post newPost; StreamWriter swLog; Generator myGenerator = new Generator(); BlogML.categoryRefType currCatRef; string categoryName; BlogML.postType currPost; List<string> categoryList; WorkerArgs args = new WorkerArgs(); string LogFile = App.sourceBlog.serviceType + "_" + App.destBlog.serviceType + "_Migration-" + DateTime.Now.ToString("yyyy_MM_dd_hhMMss") + ".csv"; // Load document. swLog = new StreamWriter(LogFile); swLog.WriteLine("Source Id, Source Link, Destination Id, " + "Destination Link"); swLog.Flush(); try { args.status = "Migrating posts from " + App.sourceBlog.serviceType + " to " + App.destBlog.serviceType; migrationWorker.ReportProgress(15, args); for (int i = 0; i <= App.sourceBlog.blogData.posts.Length - 1; i++) { currPost = App.sourceBlog.blogData.posts[i]; if (App.sourceBlog.postsToMigrate.Contains(Convert.ToInt32(currPost.id))) { args.status = "Writing Post: " + string.Join(" ", currPost.title.Text); migrationWorker.ReportProgress(20, args); newPost = new Post(); newPost.title = string.Join(" ", currPost.title.Text); newPost.dateCreated = currPost.datecreated; newPost.userid = App.destBlog.username; newPost.postid = currPost.id; newPost.description = currPost.content.Value; newPost.link = App.sourceBlog.rootUrl + currPost.posturl; // Post Tags/Categories (currently only categories are implemented with BlogML if (currPost.categories != null) { categoryList = new List<string>(); for (int j = 0; j <= currPost.categories.Length - 1; j++) { currCatRef = currPost.categories[j]; categoryName = myGenerator.GetCategoryById (App.sourceBlog.blogData, Convert.ToInt32(currCatRef.@ref)); categoryList.Add(categoryName); } newPost.categories = categoryList.ToArray(); } resultPost = blogService.InsertPost (App.destBlog.serviceUrl, App.destBlog.blogId, App.destBlog.username, App.destBlog.password, newPost); swLog.WriteLine(newPost.postid.ToString() + "," + newPost.link + "," + resultPost.postid.ToString() + "," + resultPost.link); swLog.Flush(); // Rewrite posts can still be done "live" even if a BlogML // file is being imported provided the serviceUrl details // are provided. if (App.rewritePosts) { Post updatePost = newPost; string newUrl = "<a href='" + resultPost.link + "'>" + resultPost.link + "</a>"; string newMessage = App.rewriteMessage.Replace("[URL]", newUrl); updatePost.description = newMessage; blogService.UpdatePost(App.sourceBlog.serviceUrl, App.sourceBlog.username, App.sourceBlog.password, updatePost); } } } swLog.Close(); } catch (Exception ex) { swLog.Flush(); swLog.Close(); MessageBox.Show("An error occurred migrating blog posts:" + Environment.NewLine + Environment.NewLine + ex.ToString() + Environment.NewLine + Environment.NewLine + "Please verify your settings and try migrating " + "posts again.", "Error Migrating Posts", MessageBoxButton.OK, MessageBoxImage.Error); migrationWorker.CancelAsync(); } }
/// <summary> /// Publishes posts to the destination blog. /// </summary> /// <history> /// Sean Patterson 11/15/2010 [Created] /// </history> public void WriteXMLPosts() { Services blogService = new Services(); Post resultPost; StreamWriter swLog; WorkerArgs args = new WorkerArgs(); string LogFile = App.sourceBlog.serviceType + "_" + App.destBlog.serviceType + "_Migration-" + DateTime.Now.ToString("yyyy_MM_dd_hhMMss") + ".csv"; // Load document. swLog = new StreamWriter(LogFile); swLog.WriteLine("Source Id, Source Link, Destination Id, " + "Destination Link"); swLog.Flush(); try { args.status = "Migrating posts from " + App.sourceBlog.serviceType + " to " + App.destBlog.serviceType; migrationWorker.ReportProgress(15, args); if (App.rewritePosts) { args.status = "Rewriting original posts ENABLED."; migrationWorker.ReportProgress(15, args); } else { args.status = "Rewriting original posts DISABLED."; migrationWorker.ReportProgress(15, args); } foreach (Post blogPost in App.sourceBlog.blogPosts) { if (App.sourceBlog.postsToMigrate. Contains(Convert.ToInt32(blogPost.postid))) { args.status = "Writing Post: " + blogPost.title; migrationWorker.ReportProgress(20, args); resultPost = blogService.InsertPost (App.destBlog.serviceUrl, App.destBlog.blogId, App.destBlog.username, App.destBlog.password, blogPost); swLog.WriteLine(blogPost.postid.ToString() + "," + blogPost.link + "," + resultPost.postid.ToString() + "," + resultPost.link); swLog.Flush(); if (App.rewritePosts) { Post updatePost = blogPost; string newUrl = "<a href='" + resultPost.link + "'>" + resultPost.link + "</a>"; string newMessage = App.rewriteMessage.Replace("[URL]", newUrl); updatePost.description = newMessage; blogService.UpdatePost(App.sourceBlog.serviceUrl, App.sourceBlog.username, App.sourceBlog.password, updatePost); } } } swLog.Close(); } catch (Exception ex) { swLog.Flush(); swLog.Close(); MessageBox.Show("An error occurred migrating blog posts:" + Environment.NewLine + Environment.NewLine + ex.ToString() + Environment.NewLine + Environment.NewLine + "Please verify your settings and try migrating " + "posts again.", "Error Migrating Posts", MessageBoxButton.OK, MessageBoxImage.Error); migrationWorker.CancelAsync(); } }