/// <summary> /// Add new blog post to system /// </summary> /// <returns> /// string containing unique post identifier /// </returns> public string AddPost(BlogMlExtendedPost extPost) { if (!Security.IsAdministrator) { throw new InvalidOperationException("BlogImporter.AddPost: Wrong credentials"); } using (var p = new Post()) { p.Title = extPost.BlogPost.Title; p.DateCreated = extPost.BlogPost.DateCreated; p.DateModified = extPost.BlogPost.DateModified; p.Content = extPost.BlogPost.Content.UncodedText; p.Description = extPost.BlogPost.Excerpt.UncodedText; p.IsPublished = extPost.BlogPost.Approved; if (!string.IsNullOrEmpty(extPost.PostUrl)) { // looking for a Slug with patterns such as: // /some-slug.aspx // /some-slug.html // /some-slug // Match slugMatch = Regex.Match(extPost.PostUrl, @"/([^/\.]+)(?:$|\.[\w]{1,10}$)", RegexOptions.IgnoreCase); if (slugMatch.Success) p.Slug = slugMatch.Groups[1].Value.Trim(); } if(extPost.BlogPost.Authors != null && extPost.BlogPost.Authors.Count > 0) p.Author = extPost.BlogPost.Authors[0].Ref; if (extPost.Categories != null && extPost.Categories.Count > 0) p.Categories.AddRange(extPost.Categories); if(extPost.Tags != null && extPost.Tags.Count > 0) p.Tags.AddRange(extPost.Tags); // skip if post with this url already exists var s = PostUrl(p.Slug, p.DateCreated); var list = Post.Posts.FindAll(ps => ps.RelativeLink == s); if (list.Count > 0) { return string.Empty; } if(extPost.Comments != null && extPost.Comments.Count > 0) { foreach (var comment in extPost.Comments) { p.ImportComment(comment); } } p.Import(); return p.Id.ToString(); } }
/// <summary> /// BlogML does not support tags - load directly fro XML /// </summary> private void LoadFromXmlDocument() { var doc = new XmlDocument(); doc.Load(XmlReader); var posts = doc.GetElementsByTagName("post"); foreach (XmlNode post in posts) { var blogX = new BlogMlExtendedPost(); if (post.Attributes != null) { blogX.PostUrl = post.Attributes["post-url"].Value; } if (post.ChildNodes.Count <= 0) { blogsExtended.Add(blogX); continue; } foreach (XmlNode child in post.ChildNodes) { if (child.Name == "tags") { foreach (XmlNode tag in child.ChildNodes) { if (tag.Attributes != null) { if (blogX.Tags == null) { blogX.Tags = new StateList <string>(); } blogX.Tags.Add(tag.Attributes["ref"].Value); } } } if (child.Name == "comments") { LoadBlogComments(blogX, child); } if (child.Name == "trackbacks") { LoadBlogTrackbacks(blogX, child); } } blogsExtended.Add(blogX); } }
/// <summary> /// Loads the blog posts. /// </summary> private void LoadBlogPosts() { var bi = new BlogImporter(); Utils.Log("BlogReader.LoadBlogPosts: Start importing posts"); foreach (BlogMlExtendedPost extPost in blogsExtended) { try { BlogMlExtendedPost post = extPost; if (extPost.BlogPost.Categories.Count > 0) { for (var i = 0; i < extPost.BlogPost.Categories.Count; i++) { int i2 = i; var cId = new Guid(post.BlogPost.Categories[i2].Ref); foreach (var category in categoryLookup) { if (category.Id == cId) { if (extPost.Categories == null) { extPost.Categories = new StateList <Category>(); } extPost.Categories.Add(category); } } } } if (!string.IsNullOrEmpty(bi.AddPost(extPost))) { PostCount++; } else { Utils.Log("Post '{0}' has been skipped" + extPost.BlogPost.Title); } } catch (Exception ex) { Utils.Log("BlogReader.LoadBlogPosts: " + ex.Message); } } bi.ForceReload(); Utils.Log(string.Format("BlogReader.LoadBlogPosts: Completed importing {0} posts", PostCount)); }
/// <summary> /// Add new blog post to system /// </summary> /// <returns> /// string containing unique post identifier /// </returns> public string AddPost(BlogMlExtendedPost extPost) { if (!Security.IsAdministrator) { throw new InvalidOperationException("BlogImporter.AddPost: Wrong credentials"); } using (var p = new Post()) { p.Title = extPost.BlogPost.Title; p.DateCreated = extPost.BlogPost.DateCreated; p.DateModified = extPost.BlogPost.DateModified; p.Content = extPost.BlogPost.Content.UncodedText; p.Description = extPost.BlogPost.Excerpt.UncodedText; p.IsPublished = extPost.BlogPost.Approved; if (extPost.BlogPost.Authors != null && extPost.BlogPost.Authors.Count > 0) { p.Author = extPost.BlogPost.Authors[0].Ref; } if (extPost.Categories != null && extPost.Categories.Count > 0) { p.Categories.AddRange(extPost.Categories); } if (extPost.Tags != null && extPost.Tags.Count > 0) { p.Tags.AddRange(extPost.Tags); } // skip if post with this url already exists var s = PostUrl(p.Slug, p.DateCreated); var list = Post.Posts.FindAll(ps => ps.RelativeLink == s); if (list.Count > 0) { return(string.Empty); } if (extPost.Comments != null && extPost.Comments.Count > 0) { foreach (var comment in extPost.Comments) { p.ImportComment(comment); } } p.Import(); return(p.Id.ToString()); } }
/// <summary> /// Add new blog post to system /// </summary> /// <returns> /// string containing unique post identifier /// </returns> public string AddPost(BlogMlExtendedPost extPost) { if (!Security.IsAdministrator) { throw new InvalidOperationException("BlogImporter.AddPost: Wrong credentials"); } using (var p = new Post()) { p.Title = extPost.BlogPost.Title; p.DateCreated = extPost.BlogPost.DateCreated; p.DateModified = extPost.BlogPost.DateModified; p.Content = extPost.BlogPost.Content.UncodedText; p.Description = extPost.BlogPost.Excerpt.UncodedText; p.IsPublished = extPost.BlogPost.Approved; if(extPost.BlogPost.Authors != null && extPost.BlogPost.Authors.Count > 0) p.Author = extPost.BlogPost.Authors[0].Ref; if (extPost.Categories != null && extPost.Categories.Count > 0) p.Categories.AddRange(extPost.Categories); if(extPost.Tags != null && extPost.Tags.Count > 0) p.Tags.AddRange(extPost.Tags); // skip if post with this url already exists var s = PostUrl(p.Slug, p.DateCreated); var list = Post.Posts.FindAll(ps => ps.RelativeLink == s); if (list.Count > 0) { return string.Empty; } if(extPost.Comments != null && extPost.Comments.Count > 0) { foreach (var comment in extPost.Comments) { p.ImportComment(comment); } } p.Import(); return p.Id.ToString(); } }
/// <summary> /// Lost post trackbacks and pingbacks from xml file /// </summary> /// <param name="blogX">extended blog</param> /// <param name="child">comments xml node</param> private static void LoadBlogTrackbacks(BlogMlExtendedPost blogX, XmlNode child) { foreach (XmlNode com in child.ChildNodes) { if (com.Attributes != null) { var c = new Comment { Id = new Guid(com.Attributes["id"].Value), IP = "127.0.0.1", IsApproved = bool.Parse(com.Attributes["approved"].Value), DateCreated = DateTime.ParseExact(com.Attributes["date-created"].Value, "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture) }; if (!string.IsNullOrEmpty(com.Attributes["url"].Value)) { c.Website = new Uri(com.Attributes["url"].Value); } foreach (XmlNode comNode in com.ChildNodes) { if (comNode.Name == "title") { c.Content = comNode.InnerText; } } c.Email = c.Content.ToLowerInvariant().Contains("pingback") ? "pingback" : "trackback"; c.Author = c.Email; if (blogX.Comments == null) { blogX.Comments = new List <Comment>(); } blogX.Comments.Add(c); } } }
/// <summary> /// Lost post comments from xml file /// </summary> /// <param name="blogX">extended blog</param> /// <param name="child">comments xml node</param> private static void LoadBlogComments(BlogMlExtendedPost blogX, XmlNode child) { foreach (XmlNode com in child.ChildNodes) { if (com.Attributes != null) { var c = new Comment { Id = new Guid(com.Attributes["id"].Value), Author = com.Attributes["user-name"].Value, Email = com.Attributes["user-email"].Value, ParentId = new Guid(com.Attributes["parentid"].Value), IP = com.Attributes["user-ip"].Value, DateCreated = DateTime.ParseExact(com.Attributes["date-created"].Value, "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture) }; if (!string.IsNullOrEmpty(com.Attributes["user-url"].Value)) { c.Website = new Uri(com.Attributes["user-url"].Value); } c.IsApproved = bool.Parse(com.Attributes["approved"].Value); foreach (XmlNode comNode in com.ChildNodes) { if (comNode.Name == "content") { c.Content = comNode.InnerText; } } if (blogX.Comments == null) { blogX.Comments = new List <Comment>(); } blogX.Comments.Add(c); } } }
/// <summary> /// Lost post trackbacks and pingbacks from xml file /// </summary> /// <param name="blogX">extended blog</param> /// <param name="child">comments xml node</param> private void LoadBlogTrackbacks(BlogMlExtendedPost blogX, XmlNode child) { foreach (XmlNode com in child.ChildNodes) { if (com.Attributes != null) { var c = new Comment { Id = GetGuid("comment", GetAttributeValue <string>(com.Attributes["id"])), IP = "127.0.0.1", IsApproved = GetAttributeValue <bool>(com.Attributes["approved"]), DateCreated = GetDate(com.Attributes["date-created"]) }; if (!string.IsNullOrEmpty(GetAttributeValue <string>(com.Attributes["url"]))) { c.Website = GetUri(GetAttributeValue <string>(com.Attributes["url"])); } foreach (XmlNode comNode in com.ChildNodes) { if (comNode.Name == "title") { c.Content = comNode.InnerText; } } c.Email = c.Content.ToLowerInvariant().Contains("pingback") ? "pingback" : "trackback"; c.Author = c.Email; if (blogX.Comments == null) { blogX.Comments = new List <Comment>(); } blogX.Comments.Add(c); } } }
/// <summary> /// Lost post comments from xml file /// </summary> /// <param name="blogX">extended blog</param> /// <param name="child">comments xml node</param> private void LoadBlogComments(BlogMlExtendedPost blogX, XmlNode child) { foreach (XmlNode com in child.ChildNodes) { if (com.Attributes != null) { var c = new Comment { Id = GetGuid("comment", GetAttributeValue <string>(com.Attributes["id"])), Author = GetAttributeValue <string>(com.Attributes["user-name"]), Email = GetAttributeValue <string>(com.Attributes["user-email"]), ParentId = GetGuid("comment", GetAttributeValue <string>(com.Attributes["parentid"])), IP = GetAttributeValue <string>(com.Attributes["user-ip"]), DateCreated = GetDate(com.Attributes["date-created"]) }; if (!string.IsNullOrEmpty(GetAttributeValue <string>(com.Attributes["user-url"]))) { c.Website = GetUri(GetAttributeValue <string>(com.Attributes["user-url"])); } c.IsApproved = GetAttributeValue <bool>(com.Attributes["approved"]); foreach (XmlNode comNode in com.ChildNodes) { if (comNode.Name == "content") { c.Content = comNode.InnerText; } } if (blogX.Comments == null) { blogX.Comments = new List <Comment>(); } blogX.Comments.Add(c); } } }
/// <summary> /// Add new blog post to system /// </summary> /// <returns> /// string containing unique post identifier /// </returns> public string AddPost(BlogMlExtendedPost extPost) { if (!Security.IsAdministrator) { throw new InvalidOperationException("BlogImporter.AddPost: Wrong credentials"); } using (var p = new Post()) { p.Title = extPost.BlogPost.Title; p.DateCreated = extPost.BlogPost.DateCreated; p.DateModified = extPost.BlogPost.DateModified; p.Content = extPost.BlogPost.Content.UncodedText; p.Description = extPost.BlogPost.Excerpt.UncodedText; p.IsPublished = extPost.BlogPost.Approved; if (!string.IsNullOrEmpty(extPost.PostUrl)) { // looking for a Slug with patterns such as: // /some-slug.aspx // /some-slug.html // /some-slug // Match slugMatch = Regex.Match(extPost.PostUrl, @"/([^/\.]+)(?:$|\.[\w]{1,10}$)", RegexOptions.IgnoreCase); if (slugMatch.Success) { p.Slug = slugMatch.Groups[1].Value.Trim(); } } if (extPost.BlogPost.Authors != null && extPost.BlogPost.Authors.Count > 0) { p.Author = extPost.BlogPost.Authors[0].Ref; } if (extPost.Categories != null && extPost.Categories.Count > 0) { p.Categories.AddRange(extPost.Categories); } if (extPost.Tags != null && extPost.Tags.Count > 0) { p.Tags.AddRange(extPost.Tags); } // skip if post with this url already exists var s = PostUrl(p.Slug, p.DateCreated); var list = Post.Posts.FindAll(ps => ps.RelativeLink == s); if (list.Count > 0) { return(string.Empty); } if (extPost.Comments != null && extPost.Comments.Count > 0) { foreach (var comment in extPost.Comments) { p.ImportComment(comment); } } p.Import(); return(p.Id.ToString()); } }
/// <summary> /// BlogML does not support tags - load directly fro XML /// </summary> private void LoadFromXmlDocument() { var doc = new XmlDocument(); doc.Load(XmlReader); var posts = doc.GetElementsByTagName("post"); foreach (XmlNode post in posts) { var blogX = new BlogMlExtendedPost(); if(post.Attributes != null) blogX.PostUrl = post.Attributes["post-url"].Value; if (post.ChildNodes.Count <= 0) { blogsExtended.Add(blogX); continue; } foreach (XmlNode child in post.ChildNodes) { if (child.Name == "tags") { foreach (XmlNode tag in child.ChildNodes) { if (tag.Attributes != null) { if (blogX.Tags == null) blogX.Tags = new StateList<string>(); blogX.Tags.Add(tag.Attributes["ref"].Value); } } } if(child.Name == "comments") LoadBlogComments(blogX, child); if (child.Name == "trackbacks") LoadBlogTrackbacks(blogX, child); } blogsExtended.Add(blogX); } }
/// <summary> /// Lost post trackbacks and pingbacks from xml file /// </summary> /// <param name="blogX">extended blog</param> /// <param name="child">comments xml node</param> private static void LoadBlogTrackbacks(BlogMlExtendedPost blogX, XmlNode child) { foreach (XmlNode com in child.ChildNodes) { if (com.Attributes != null) { var c = new Comment { Id = new Guid(com.Attributes["id"].Value), IP = "127.0.0.1", IsApproved = bool.Parse(com.Attributes["approved"].Value), DateCreated = DateTime.ParseExact(com.Attributes["date-created"].Value, "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture) }; if (!string.IsNullOrEmpty(com.Attributes["url"].Value)) c.Website = new Uri(com.Attributes["url"].Value); foreach (XmlNode comNode in com.ChildNodes) { if (comNode.Name == "title") { c.Content = comNode.InnerText; } } c.Email = c.Content.ToLowerInvariant().Contains("pingback") ? "pingback" : "trackback"; c.Author = c.Email; if (blogX.Comments == null) blogX.Comments = new List<Comment>(); blogX.Comments.Add(c); } } }
/// <summary> /// Lost post comments from xml file /// </summary> /// <param name="blogX">extended blog</param> /// <param name="child">comments xml node</param> private static void LoadBlogComments(BlogMlExtendedPost blogX, XmlNode child) { foreach (XmlNode com in child.ChildNodes) { if(com.Attributes != null) { var c = new Comment { Id = new Guid(com.Attributes["id"].Value), Author = com.Attributes["user-name"].Value, Email = com.Attributes["user-email"].Value, ParentId = new Guid(com.Attributes["parentid"].Value), IP = com.Attributes["user-ip"].Value, DateCreated = DateTime.ParseExact(com.Attributes["date-created"].Value, "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture) }; if(!string.IsNullOrEmpty(com.Attributes["user-url"].Value)) c.Website = new Uri(com.Attributes["user-url"].Value); c.IsApproved = bool.Parse(com.Attributes["approved"].Value); foreach (XmlNode comNode in com.ChildNodes) { if(comNode.Name == "content") { c.Content = comNode.InnerText; } } if(blogX.Comments == null) blogX.Comments = new List<Comment>(); blogX.Comments.Add(c); } } }