Esempio n. 1
0
		private static void ImportDatabase(IDocumentStore store)
		{
			Stopwatch sp = Stopwatch.StartNew();

			using (var e = new SubtextEntities())
			{
				Console.WriteLine("Starting...");

				IOrderedEnumerable<Post> theEntireDatabaseOhMygod = e.Posts
					.Include("Comments")
					.Include("Links")
					.Include("Links.Categories")
					.ToList()
					.OrderBy(x => x.DateSyndicated);

				Console.WriteLine("Loading data took {0:#,#} ms", sp.ElapsedMilliseconds);

				var usersList = new List<User>();
				using (IDocumentSession s = store.OpenSession())
				{
					var users = new[]
					{
						new {Email = "*****@*****.**", FullName = "Ayende Rahien", TwitterNick = "ayende", RelatedTwitterNick=(string)null},
						new {Email = "*****@*****.**", FullName = "Fitzchak Yitzchaki", TwitterNick = "fitzchak", RelatedTwitterNick="ayende"},
					};
					for (int i = 0; i < users.Length; i++)
					{
						var user = new User
							{
								Id = "users/" + (i + 1),
								Email = users[i].Email,
								FullName = users[i].FullName,
								TwitterNick = users[i].TwitterNick,
								RelatedTwitterNick = users[i].RelatedTwitterNick,
								Enabled = true,
							};
						user.SetPassword("123456");
						s.Store(user);
						usersList.Add(user);
					}
					s.SaveChanges();
				}

				foreach (Post post in theEntireDatabaseOhMygod)
				{
					var ravenPost = new Web.Models.Post
						{
							AuthorId = usersList
									.Where(u=> u.FullName == post.Author)
									.Select(u => u.Id)
									.FirstOrDefault() ?? 
									usersList.First().Id,
							CreatedAt = new DateTimeOffset(post.DateAdded),
							PublishAt = new DateTimeOffset(post.DateSyndicated ?? post.DateAdded),
							Body = post.Text,
							LegacySlug = post.EntryName,
							Title = HttpUtility.HtmlDecode(post.Title),
							Tags = post.Links.Select(x => x.Categories.Title)
								.Where(x => x != "Uncategorized")
								.ToArray(),
							AllowComments = true
						};

					var commentsCollection = new PostComments();
					commentsCollection.Comments = post.Comments
						.Where(comment => comment.StatusFlag == 1)
						.OrderBy(comment => comment.DateCreated)
						.Select(
							comment => new PostComments.Comment
								{
									Id = commentsCollection.GenerateNewCommentId(),
									Author = comment.Author,
									Body = ConvertCommentToMarkdown(comment.Body),
									CreatedAt = comment.DateCreated,
									Email = comment.Email,
									Url = comment.Url,
									Important = comment.IsBlogAuthor ?? false,
									UserAgent = comment.UserAgent,
									UserHostAddress = comment.IpAddress,
									IsSpam = false,
									CommenterId = null,
								}
						).ToList();
					commentsCollection.Spam = post.Comments
						.Where(comment => comment.StatusFlag != 1)
						.OrderBy(comment => comment.DateCreated)
						.Select(
							comment => new PostComments.Comment
								{
									Id = commentsCollection.GenerateNewCommentId(),
									Author = comment.Author,
									Body = ConvertCommentToMarkdown(comment.Body),
									CreatedAt = comment.DateCreated,
									Email = comment.Email,
									Url = comment.Url,
									Important = comment.IsBlogAuthor ?? false,
									UserAgent = comment.UserAgent,
									UserHostAddress = comment.IpAddress,
									IsSpam = true,
									CommenterId = null,
								}
						).ToList();

					ravenPost.CommentsCount = commentsCollection.Comments.Count;

					using (IDocumentSession s = store.OpenSession())
					{
						s.Store(commentsCollection);
						ravenPost.CommentsId = commentsCollection.Id;

						s.Store(ravenPost);
						commentsCollection.Post = new PostComments.PostReference
						{
							Id = ravenPost.Id,
							PublishAt = ravenPost.PublishAt
						};

						s.SaveChanges();
					}
				}
			}
			Console.WriteLine(sp.Elapsed);
		}
Esempio n. 2
0
        private void importBlogPosts(IDocumentStore store, BlogMLBlog blog, Dictionary<string, User> usersList)
        {
            foreach (var post in blog.Posts)
            {
                var authorId = getAuthorId(usersList, post);

                var ravenPost = new Post
                    {
                        AuthorId = authorId,
                        CreatedAt = new DateTimeOffset(post.DateCreated),
                        PublishAt = new DateTimeOffset(post.DateCreated),
                        Body = post.Content.Text,
                        LegacySlug = SlugConverter.TitleToSlug(post.PostName ?? post.Title),
                        Title = HttpUtility.HtmlDecode(post.Title),
                        Tags =
                            post.Categories.Cast<BlogMLCategoryReference>()
                                .Select(x => blog.Categories.First(c => c.ID == x.Ref).Title)
                                .ToArray(),
                        AllowComments = true
                    };

                var commentsCollection = new PostComments();
                commentsCollection.Spam = new List<PostComments.Comment>();
                commentsCollection.Comments = post.Comments.Cast<BlogMLComment>()
                                                  .Where(comment => comment.Approved)
                                                  .OrderBy(comment => comment.DateCreated)
                                                  .Select(
                                                      comment => new PostComments.Comment
                                                          {
                                                              Id = commentsCollection.GenerateNewCommentId(),
                                                              Author = comment.UserName,
                                                              Body = ConvertCommentToMarkdown(comment.Content.Text),
                                                              CreatedAt = comment.DateCreated,
                                                              Email = comment.UserEMail,
                                                              Url = comment.UserUrl,
                                                              Important =
                                                                  usersList.Any(
                                                                      u => u.Value.FullName == comment.UserName),
                                                              //UserAgent = comment.,
                                                              //UserHostAddress = comment.IpAddress,
                                                              IsSpam = false,
                                                              CommenterId = null,
                                                          }
                    ).ToList();
                commentsCollection.Spam = post.Comments.Cast<BlogMLComment>()
                                              .Where(comment => !comment.Approved)
                                              .OrderBy(comment => comment.DateCreated)
                                              .Select(
                                                  comment => new PostComments.Comment
                                                      {
                                                          Id = commentsCollection.GenerateNewCommentId(),
                                                          Author = comment.UserName,
                                                          Body = ConvertCommentToMarkdown(comment.Content.Text),
                                                          CreatedAt = comment.DateCreated,
                                                          Email = comment.UserEMail,
                                                          Url = comment.UserUrl,
                                                          Important =
                                                              usersList.Any(u => u.Value.FullName == comment.UserName),
                                                          //UserAgent = comment.UserAgent,
                                                          //UserHostAddress = comment.IpAddress,
                                                          IsSpam = true,
                                                          CommenterId = null,
                                                      }
                    ).Where(c => c.Body != null).ToList();

                ravenPost.CommentsCount = commentsCollection.Comments.Count;

                using (var s = store.OpenSession())
                {
                    s.Store(commentsCollection);
                    ravenPost.CommentsId = commentsCollection.Id;

                    s.Store(ravenPost);
                    commentsCollection.Post = new PostComments.PostReference
                        {
                            Id = ravenPost.Id,
                            PublishAt = ravenPost.PublishAt
                        };

                    s.SaveChanges();
                }
            }
        }