Last active
May 9, 2016 14:51
-
-
Save vmilev/9e628cb01966b5d8b44551099a778ec7 to your computer and use it in GitHub Desktop.
Migration Blog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DeserializedBlogPost | |
{ | |
private const string Wp = "http://wordpress.org/export/1.2/"; | |
private const string ContentUrl = "http://purl.org/rss/1.0/modules/content/"; | |
private const string Dc = "http://purl.org/dc/elements/1.1/"; | |
public DeserializedBlogPost(XElement blogElement) | |
{ | |
this.Title = blogElement.Element("title").Value; | |
this.URL = blogElement.Element("link").Value; | |
this.Slug = blogElement.Element($"{Wp}post_name").Value; | |
this.PublicationDate = DateTime.ParseExact( | |
blogElement.Element($"{Wp}post_date").Value, | |
"yyyy-MM-dd HH:mm:ss", | |
CultureInfo.InvariantCulture); | |
this.PostId = blogElement.Element($"{Wp}post_id").Value; | |
this.Content = blogElement.Element($"{ContentUrl}encoded").Value; | |
this.Tags = blogElement.Elements("category") | |
.Where(el => el.Attribute("domain").Value == "post_tag") | |
.Select(el => el.Value); | |
this.Categories = blogElement.Elements("category") | |
.Where(el => el.Attribute("domain").Value == "category") | |
.Select(el => el.Value); | |
this.Author = blogElement.Element($"{Dc}creator").Value; | |
this.Permalink = blogElement.Element("guid").Value; | |
} | |
//Other unimportant class members omitted for brevity | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private const string Wp = "http://wordpress.org/export/1.2/"; | |
//--Unimportant code snipped for brevity-- | |
private static IEnumerable<DeserializedBlogPost> DeserializeBlogs(XDocument importedXmlData) | |
{ | |
var importedBlogposts = importedXmlData.Descendants("item") | |
.Where(el => | |
el.Element($"{Wp}post_type").Value == "post" && el.Element($"{Wp}status").Value == "publish"); | |
return importedBlogposts.Select(el => new DeserializedBlogPost(el)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected void Application_Start(object sender, EventArgs e) | |
{ | |
SystemManager.ApplicationStart += (s, args) => | |
{ | |
var imp = new BlogsImporter(blogsProvider: "NvsBlogProvider"); | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void ImportBlogs() | |
{ | |
var nvsBlog = this.BlogsManager.GetBlog(new Guid("{dd71315a-1eae-649b-900d-ff0000b46a22}")); | |
if (nvsBlog.BlogPosts().Any()) | |
{ | |
return; | |
} | |
foreach (var deserializedBlogPost in this.DeserializedBlogs) | |
{ | |
var importedBlog = this.BlogsManager.CreateBlogPost(); | |
importedBlog.Parent = nvsBlog; | |
importedBlog.Title = deserializedBlogPost.Title; | |
importedBlog.UrlName = deserializedBlogPost.Slug; | |
importedBlog.SetValue("MigrationId", deserializedBlogPost.PostId); | |
importedBlog.Content = deserializedBlogPost.Content; | |
importedBlog.DateCreated = importedBlog.PublicationDate = deserializedBlogPost.PublicationDate; | |
BlogsImporter.MapAuthor(importedBlog, deserializedBlogPost); | |
BlogsImporter.ImportTags(importedBlog, deserializedBlogPost); | |
BlogsImporter.ImportCategories(importedBlog, deserializedBlogPost); | |
this.BlogsManager.RecompileAndValidateUrls(importedBlog); | |
this.BlogsManager.Lifecycle.PublishWithSpecificDate(importedBlog, deserializedBlogPost.PublicationDate); | |
} | |
this.BlogsManager.SaveChanges(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
XDocument.Load(HttpContext.Current.Server.MapPath("~/Import/wordpress.xml")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void ImportCategories(BlogPost importedBlog, DeserializedBlogPost deserializedBlogPost) | |
{ | |
foreach (var category in deserializedBlogPost.Categories) | |
{ | |
var taxonomy = TaxonomyService.GetHierarchicalTaxon(category); | |
if (taxonomy != null) | |
{ | |
importedBlog.Organizer.AddTaxa("Category", taxonomy.Id); | |
} | |
} | |
} | |
private static void ImportTags(BlogPost importedBlog, DeserializedBlogPost deserializedBlogPost) | |
{ | |
foreach (var tag in deserializedBlogPost.Tags) | |
{ | |
var taxonomy = TaxonomyService.GetFlatTaxon(tag); | |
if (taxonomy != null) | |
{ | |
importedBlog.Organizer.AddTaxa("Tags", taxonomy.Id); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment