public void Process(BlogMLPost postml) { Log.DebugFormat("Fetching comments for PostID {0}", postml.ID); using (var query = new OxiteReader("SELECT * FROM oxite_Comment A, oxite_User B WHERE A.CreatorUserID = B.UserID AND Username <> 'Anonymous' AND PostID='" + postml.ID + "'")) { var comments = query.Execute(); foreach (var comment in comments) { var commentml = new BlogMLComment { ID = comment.CommentID.ToString(), DateCreated = comment.CreatedDate, DateModified = comment.ModifiedDate, Approved = comment.State == (int)RecordStates.Normal, UserName = comment.Username, UserEMail = comment.Email, Content = new BlogMLContent { ContentType = ContentTypes.Html, Text = comment.Body } }; postml.Comments.Add(commentml); } } Log.DebugFormat("Finished adding comments."); }
public static CommentInputForImport ToImportCommentInput(this BlogMLComment blogMLComment, BlogMLBlog blog, UserAuthenticated user, Language language) { string body = blogMLComment.Content.Text; DateTime created = blogMLComment.DateCreated; DateTime modified = blogMLComment.DateModified; long creatorIP = 0; string creatorUserAgent = ""; EntityState state = blogMLComment.Approved ? EntityState.Normal : EntityState.PendingApproval; if (blogMLComment.UserEMail == user.Email || blogMLComment.UserEMail == blog.Authors[0].Email) { return(new CommentInputForImport(body, created, user, creatorIP, creatorUserAgent, language, modified, state)); } else { string creatorName; string creatorEmail; string creatorEmailHash; string creatorUrl; if (!string.IsNullOrEmpty(blogMLComment.UserName)) { creatorName = blogMLComment.UserName.Length > 50 ? blogMLComment.UserName.Substring(0, 50) : blogMLComment.UserName; } else { creatorName = ""; } if (!string.IsNullOrEmpty(blogMLComment.UserEMail)) { creatorEmail = blogMLComment.UserEMail.Length > 100 ? blogMLComment.UserEMail.Substring(0, 100) : blogMLComment.UserEMail; creatorEmailHash = creatorEmail.ComputeEmailHash(); } else { creatorEmail = creatorEmailHash = ""; } if (!string.IsNullOrEmpty(blogMLComment.UserUrl)) { creatorUrl = blogMLComment.UserUrl.Length > 300 ? blogMLComment.UserUrl.Substring(0, 300) : blogMLComment.UserUrl; } else { creatorUrl = ""; } return(new CommentInputForImport(body, created, new UserAnonymous(creatorName, creatorEmail, creatorEmailHash, creatorUrl), creatorIP, creatorUserAgent, language, modified, state)); } }
public void ConvertComment_WithInvalidUserUrl_IgnoresUrl() { // arrange var comment = new BlogMLComment { UserUrl = "not-valid-url" }; var mapper = new BlogMLImportMapper(); // act var convertComment = mapper.ConvertComment(comment, "123"); // assert Assert.AreEqual(null, convertComment.SourceUrl); }
public void ConvertComment_ReturnsFeedbackItemAsComment() { // arrange var comment = new BlogMLComment { UserUrl = "not-valid-url" }; var mapper = new BlogMLImportMapper(); // act var convertComment = mapper.ConvertComment(comment, "123"); // assert Assert.AreEqual(FeedbackType.Comment, convertComment.FeedbackType); }
public void ConvertComment_WithUnapprovedComment_SetsFeedbackToTrash() { // arrange var comment = new BlogMLComment { UserUrl = "not-valid-url", Approved = false }; var mapper = new BlogMLImportMapper(); // act var convertComment = mapper.ConvertComment(comment, "123"); // assert Assert.IsFalse(convertComment.Approved); Assert.AreEqual(FeedbackStatusFlag.NeedsModeration, convertComment.Status); }
public FeedbackItem ConvertComment(BlogMLComment comment, string parentPostId) { var feedback = new FeedbackItem(FeedbackType.Comment) { EntryId = int.Parse(parentPostId, CultureInfo.InvariantCulture), Title = comment.Title ?? string.Empty, DateCreated = comment.DateCreated, DateModified = comment.DateModified, Body = comment.Content.UncodedText ?? string.Empty, Approved = comment.Approved, Author = comment.UserName ?? string.Empty, Email = comment.UserEMail, SourceUrl = !String.IsNullOrEmpty(comment.UserUrl) ? ConvertUri(comment.UserUrl) : null }; if (!feedback.Approved) { // Have to assume it needs moderation since that's what it most likely means in other blog systems; feedback.Status = FeedbackStatusFlag.NeedsModeration; } return(feedback); }
public void Write_WithBlogContainingPostsWithComments_WritesPostCommentsToWriter() { // arrange var stringWriter = new StringWriter(); var xmlWriter = new XmlTextWriter(stringWriter) { Formatting = Formatting.Indented }; var source = new Mock <IBlogMLSource>(); var dateTime = DateTime.ParseExact("20090123", "yyyyMMdd", CultureInfo.InvariantCulture); var blog = new BlogMLBlog { Title = "Subtext Blog", RootUrl = "http://subtextproject.com/", SubTitle = "A test blog", DateCreated = dateTime }; source.Setup(s => s.GetBlog()).Returns(blog); var post = new BlogMLPost { Title = "This is a blog post" }; var posts = new List <BlogMLPost> { post }; var comment = new BlogMLComment { Title = "Test Comment Title", Content = { Text = "<p>Comment Body</p>" } }; post.Comments.Add(comment); source.Setup(s => s.GetBlogPosts(false /*embedAttachments*/)).Returns(posts); var writer = new BlogMLWriter(source.Object, false /*embedAttachments*/); // act ((IBlogMLWriter)writer).Write(xmlWriter); // assert string output = stringWriter.ToString(); Assert.Contains(output, @"<title type=""text""><![CDATA[Test Comment Title]]></title>"); Assert.Contains(output, @"<content type=""text""><![CDATA[<p>Comment Body</p>]]></content>"); }
public void Import_WithBlogPostHavingComments_CreatesCommentUsingPostId() { // arrange var blog = new BlogMLBlog(); var post = new BlogMLPost(); var comment = new BlogMLComment(); post.Comments.Add(comment); blog.Posts.Add(post); var repository = new Mock <IBlogImportRepository>(); repository.Setup(r => r.CreateBlogPost(blog, post)).Returns("98053"); bool commentCreated = false; repository.Setup(r => r.CreateComment(comment, "98053")).Callback(() => commentCreated = true); var service = new BlogImportService(repository.Object); // act service.Import(blog); // assert Assert.IsTrue(commentCreated); }
//============================================================ // CLASS SUMMARY //============================================================ /// <summary> /// Provides example code for the BlogMLPost class. /// </summary> public static void ClassExample() { #region BlogMLPost BlogMLDocument document = new BlogMLDocument(); document.RootUrl = new Uri("/blogs/default.aspx"); document.GeneratedOn = new DateTime(2006, 9, 5, 18, 22, 10); document.Title = new BlogMLTextConstruct("BlogML 2.0 Example"); document.Subtitle = new BlogMLTextConstruct("This is some sample blog content for BlogML 2.0"); BlogMLAuthor administrator = new BlogMLAuthor(); administrator.Id = "2100"; administrator.CreatedOn = new DateTime(2006, 8, 10, 8, 44, 35); administrator.LastModifiedOn = new DateTime(2006, 9, 4, 13, 46, 38); administrator.ApprovalStatus = BlogMLApprovalStatus.Approved; administrator.EmailAddress = "*****@*****.**"; administrator.Title = new BlogMLTextConstruct("admin"); document.Authors.Add(administrator); document.ExtendedProperties.Add("CommentModeration", "Anonymous"); document.ExtendedProperties.Add("SendTrackback", "yes"); BlogMLCategory category1 = new BlogMLCategory(); category1.Id = "1018"; category1.CreatedOn = new DateTime(2006, 9, 5, 17, 54, 58); category1.LastModifiedOn = new DateTime(2006, 9, 5, 17, 54, 58); category1.ApprovalStatus = BlogMLApprovalStatus.Approved; category1.Description = "Sample Category 1"; category1.ParentId = "0"; category1.Title = new BlogMLTextConstruct("Category 1"); document.Categories.Add(category1); BlogMLCategory category2 = new BlogMLCategory(); category2.Id = "1019"; category2.CreatedOn = new DateTime(2006, 9, 5, 17, 54, 59); category2.LastModifiedOn = new DateTime(2006, 9, 5, 17, 54, 59); category2.ApprovalStatus = BlogMLApprovalStatus.Approved; category2.Description = "Sample Category 2"; category2.ParentId = "0"; category2.Title = new BlogMLTextConstruct("Category 2"); document.Categories.Add(category2); BlogMLCategory category3 = new BlogMLCategory(); category3.Id = "1020"; category3.CreatedOn = new DateTime(2006, 9, 5, 17, 55, 0); category3.LastModifiedOn = new DateTime(2006, 9, 5, 17, 55, 0); category3.ApprovalStatus = BlogMLApprovalStatus.NotApproved; category3.Description = "Sample Category 3"; category3.ParentId = "0"; category3.Title = new BlogMLTextConstruct("Category 3"); document.Categories.Add(category3); // Create a blog entry BlogMLPost post = new BlogMLPost(); post.Id = "34"; post.CreatedOn = new DateTime(2006, 9, 5, 3, 19, 0); post.LastModifiedOn = new DateTime(2006, 9, 5, 3, 19, 0); post.ApprovalStatus = BlogMLApprovalStatus.Approved; post.Url = new Uri("/blogs/archive/2006/09/05/Sample-Blog-Post.aspx"); post.PostType = BlogMLPostType.Normal; post.Views = "0"; post.Title = new BlogMLTextConstruct("Sample Blog Post"); post.Content = new BlogMLTextConstruct("<p>This is <b>HTML encoded</b> content. </p>", BlogMLContentType.Html); post.Name = new BlogMLTextConstruct("Sample Blog Post"); post.Categories.Add("1018"); post.Categories.Add("1020"); post.Authors.Add("2100"); BlogMLComment comment = new BlogMLComment(); comment.Id = "35"; comment.CreatedOn = new DateTime(2006, 9, 5, 11, 36, 50); comment.LastModifiedOn = new DateTime(2006, 9, 5, 11, 36, 50); comment.Title = new BlogMLTextConstruct("re: Sample Blog Post"); comment.Content = new BlogMLTextConstruct("This is a test comment."); post.Comments.Add(comment); #endregion }
public static Comment ToComment(this BlogMLComment blogMLComment, BlogMLBlog blog, User user, Language language) { Comment comment = new Comment(); comment.Body = blogMLComment.Content.Text; comment.Created = blogMLComment.DateCreated; comment.Modified = blogMLComment.DateModified; comment.Language = language; comment.CreatorIP = 0; comment.CreatorUserAgent = ""; comment.State = blogMLComment.Approved ? EntityState.Normal : EntityState.PendingApproval; if (blogMLComment.UserEMail == user.Email || blogMLComment.UserEMail == blog.Authors[0].Email) { comment.Creator = user; } else { comment.Creator = new UserBase(); if (!string.IsNullOrEmpty(blogMLComment.UserEMail)) { comment.Creator.Email = blogMLComment.UserEMail.Length > 100 ? blogMLComment.UserEMail.Substring(0, 100) : blogMLComment.UserEMail; comment.Creator.HashedEmail = comment.Creator.Email.ComputeHash(); } else { comment.Creator.Email = comment.Creator.HashedEmail = ""; } if (!string.IsNullOrEmpty(blogMLComment.UserName)) { comment.Creator.Name = blogMLComment.UserName.Length > 50 ? blogMLComment.UserName.Substring(0, 50) : blogMLComment.UserName; } else { comment.Creator.Name = ""; } if (!string.IsNullOrEmpty(blogMLComment.UserUrl)) { comment.Creator.Url = blogMLComment.UserUrl.Length > 300 ? blogMLComment.UserUrl.Substring(0, 300) : blogMLComment.UserUrl; } else { comment.Creator.Url = ""; } } return(comment); }
public void CreateComment(BlogMLComment comment, string newPostId) { var newComment = Mapper.ConvertComment(comment, newPostId); CommentService.Create(newComment, false /*runfilters*/); }
/// <summary> /// Converts the blog export to BlogML /// </summary> public void Convert() { XDocument blogger = XDocument.Load(_bloggerExportPath); // extract basic information string blogTitle = blogger.Element(AtomNamespace + "feed").Element(AtomNamespace + "title").Value; DateTime blogUpdated = DateTime.Parse(blogger.Element(AtomNamespace + "feed").Element(AtomNamespace + "updated").Value).ToUniversalTime(); string authorName = blogger.Element(AtomNamespace + "feed").Element(AtomNamespace + "author").Element(AtomNamespace + "name").Value; string authorUri = blogger.Element(AtomNamespace + "feed").Element(AtomNamespace + "author").Element(AtomNamespace + "uri").Value; string authorEmail = blogger.Element(AtomNamespace + "feed").Element(AtomNamespace + "author").Element(AtomNamespace + "email").Value; // assume the updated date and then hunt backwards in time DateTime blogCreated = blogUpdated; LogMessage(string.Format(CultureInfo.CurrentCulture, Properties.Resources.Converter_MessageBlogTitle, blogTitle, blogUpdated)); LogMessage(string.Format(CultureInfo.CurrentCulture, Properties.Resources.Converter_MessageBlogAuthor, authorName, authorEmail, authorUri)); // parse ATOM entries var query = from entry in blogger.Descendants(AtomNamespace + "entry") select new { Id = entry.Element(AtomNamespace + "id").Value, Published = DateTime.Parse(entry.Element(AtomNamespace + "published").Value).ToUniversalTime(), Updated = DateTime.Parse(entry.Element(AtomNamespace + "updated").Value).ToUniversalTime(), Title = entry.Element(AtomNamespace + "title").Value, Content = entry.Element(AtomNamespace + "content").Value, AuthorName = entry.Element(AtomNamespace + "author").Element(AtomNamespace + "name").Value, AuthorUri = entry.Element(AtomNamespace + "author").Element(AtomNamespace + "uri") == null ? null : entry.Element(AtomNamespace + "author").Element(AtomNamespace + "uri").Value, AuthorEmail = entry.Element(AtomNamespace + "author").Element(AtomNamespace + "email") == null ? null : entry.Element(AtomNamespace + "author").Element(AtomNamespace + "email").Value, InReplyTo = entry.Element(ThrNamespace + "in-reply-to") == null ? null : entry.Element(ThrNamespace + "in-reply-to").Attribute("ref").Value, Categories = (from category in entry.Descendants(AtomNamespace + "category") select new { Scheme = category.Attribute("scheme").Value, Term = category.Attribute("term").Value, }), Links = (from link in entry.Descendants(AtomNamespace + "link") select new { Rel = link.Attribute("rel").Value, Type = link.Attribute("type").Value, Href = link.Attribute("href").Value, }), }; // separate out the different export categories from the ATOM entries Dictionary <string, BlogMLPost> posts = new Dictionary <string, BlogMLPost>(); List <CommentWithInReplyTo> comments = new List <CommentWithInReplyTo>(); List <BlogMLAuthor> authors = new List <BlogMLAuthor>(); List <BlogMLCategory> categories = new List <BlogMLCategory>(); NameValueCollection settings = new NameValueCollection(); string template = null; foreach (var q in query) { // update the blog created date as we find earlier entires if (q.Published < blogCreated) { blogCreated = q.Published; } // create a content holder BlogMLContent content = new BlogMLContent(); content.Text = q.Content; // find categories and the type of entry List <BlogMLCategoryReference> categoryRefs = new List <BlogMLCategoryReference>(); string entryKind = null; // get the type of entry and any post categories foreach (var c in q.Categories) { if (c.Scheme == "http://schemas.google.com/g/2005#kind") { entryKind = c.Term; } else if (c.Scheme == "http://www.blogger.com/atom/ns#") { BlogMLCategoryReference categoryRef = new BlogMLCategoryReference(); categoryRef.Ref = UpdateCategoriesGetRef(ref categories, c.Term, q.Published); categoryRefs.Add(categoryRef); } else { // we've found a category scheme we don't know about LogMessage(string.Format(CultureInfo.CurrentCulture, Properties.Resources.Converter_MessageUnexpectedCategoryScheme, c.Scheme)); } } // process entry based on the entry kind switch (entryKind) { case "http://schemas.google.com/blogger/2008/kind#template": template = q.Content; break; case "http://schemas.google.com/blogger/2008/kind#settings": LogMessage(string.Format(CultureInfo.CurrentCulture, Properties.Resources.Converter_ImportingSettings, q.Id, q.Content)); settings.Add(q.Id, q.Content); break; case "http://schemas.google.com/blogger/2008/kind#post": LogMessage(string.Format(CultureInfo.CurrentCulture, Properties.Resources.Converter_ImportingPost, q.Title)); // get a reference to the author of this entry BlogMLAuthorReference authorReference = new BlogMLAuthorReference(); authorReference.Ref = UpdateAuthorsGetRef(ref authors, q.AuthorName, q.AuthorEmail, q.Published); BlogMLPost post = new BlogMLPost(); post.Approved = true; post.Authors.Add(authorReference); if (categoryRefs.Count > 0) { post.Categories.AddRange(categoryRefs); } post.Content = content; post.DateCreated = q.Published; post.DateModified = q.Updated; post.HasExcerpt = false; post.ID = q.Id; post.PostType = BlogPostTypes.Normal; post.Title = q.Title; posts.Add(q.Id, post); break; case "http://schemas.google.com/blogger/2008/kind#comment": LogMessage(string.Format(CultureInfo.CurrentCulture, Properties.Resources.Converter_ImportingComment, q.Title)); BlogMLComment comment = new BlogMLComment(); comment.Approved = true; comment.Content = content; comment.DateCreated = q.Published; comment.DateModified = q.Updated; comment.ID = q.Id; comment.Title = q.Title; comment.UserEMail = q.AuthorEmail; comment.UserName = q.AuthorName; comment.UserUrl = q.AuthorUri; comments.Add(new CommentWithInReplyTo(comment, q.InReplyTo)); break; default: LogMessage(string.Format(CultureInfo.CurrentCulture, Properties.Resources.Converter_MessageUnexpectedEntryKind, entryKind)); break; } } // add comments to posts foreach (CommentWithInReplyTo comment in comments) { if (posts.ContainsKey(comment.InReplyTo)) { BlogMLPost post = posts[comment.InReplyTo]; LogMessage(string.Format(CultureInfo.CurrentCulture, Properties.Resources.Converter_AttachingComment, comment.Comment.Title, post.Title)); post.Comments.Add(comment.Comment); } else { LogMessage(string.Format(CultureInfo.CurrentCulture, Properties.Resources.Converter_OrphanedComment, comment.Comment.Title)); } } // build the blog LogMessage(Properties.Resources.Converter_BuildingBlogML); BlogMLBlog blog = new BlogMLBlog(); blog.Authors.AddRange(authors); blog.Categories.AddRange(categories); blog.DateCreated = blogCreated; blog.Posts.AddRange(posts.Values); blog.Title = blogTitle; // add blogger settings as extended properties foreach (string name in settings.Keys) { Pair <string, string> pair = new Pair <string, string>(); pair.Key = name; pair.Value = settings[name]; blog.ExtendedProperties.Add(pair); } // output BlogML LogMessage(string.Format(CultureInfo.CurrentCulture, Properties.Resources.Converter_WritingBlogML, _blogmlPath)); XmlWriterSettings writerSettings = new XmlWriterSettings(); writerSettings.CheckCharacters = true; writerSettings.CloseOutput = true; writerSettings.ConformanceLevel = ConformanceLevel.Document; writerSettings.Indent = true; using (XmlWriter writer = XmlWriter.Create(_blogmlPath, writerSettings)) { BlogMLSerializer.Serialize(writer, blog); } }
public CommentWithInReplyTo(BlogMLComment comment, string inReplyTo) { this.Comment = comment; this.InReplyTo = inReplyTo; }