/// <summary> /// Parses the file. /// </summary> /// <param name="file">The file.</param> private bool ParseBlogEntry(FileInfo file) { if (ValidateFile(file, Parser.BlogEntry)) { var content = m_fileContent.ToString().Split('|'); var blogEntity = new PhpBlogEntity(); for (int i = 0; i < content.Length; i++) { blogEntity.FileName = file.Name.Replace(".gz", string.Empty).Replace(".txt", string.Empty); switch (content[i]) { case "VERSION": try { blogEntity.Version = content[i + 1]; } catch { blogEntity.Version = string.Empty; } break; case "SUBJECT": try { blogEntity.Subject = ParseContent(content[i + 1]); } catch { blogEntity.Subject = string.Empty; } break; case "CONTENT": try { blogEntity.Content = ParseContent(content[i + 1]); } catch { blogEntity.Content = string.Empty; } blogEntity.Content = blogEntity.Content.Replace(Environment.NewLine, "<br />"); break; case "CATEGORIES": try { blogEntity.Categories = content[i + 1]; } catch { blogEntity.Categories = string.Empty; } break; case "relatedlink": try { blogEntity.RelatedLink = content[i + 1]; } catch { blogEntity.RelatedLink = string.Empty; } break; case "IP-ADDRESS": try { blogEntity.IpAddress = content[i + 1]; } catch { blogEntity.IpAddress = string.Empty; } break; case "DATE": try { blogEntity.Date = Convert.ToInt64(content[i + 1]); } catch { blogEntity.Date = 0; } break; case "CREATEDBY": try { blogEntity.CreatedBy = content[i + 1]; } catch { blogEntity.CreatedBy = string.Empty; } break; } } // Categories if (string.IsNullOrEmpty(blogEntity.Categories)) { blogEntity.Categories = "1"; } if (m_blogEntries == null) { m_blogEntries = new List<PhpBlogEntity>(); } var phpBlogEntity = m_blogEntries.Find( item => item.Date == blogEntity.Date && item.Subject == blogEntity.Subject && item.FileName == blogEntity.FileName); if (phpBlogEntity == null) { m_blogEntries.Add(blogEntity); } return true; } return false; }
/// <summary> /// Converts the Simple PHP Blog entity to a WordPress Post entity. /// </summary> /// <param name="blogEntity">The blog entity.</param> /// <returns></returns> private WpPostEntity ConvertBlogEntity(PhpBlogEntity blogEntity) { var excerpt = string.Empty; if (blogEntity.Content.Contains("<br />")) { excerpt = blogEntity.Content.Substring(0, blogEntity.Content.IndexOf("<br />")); } var postEntity = new WpPostEntity { ID = null, PostAuthor = 1, PostDate = blogEntity.DateConverted, PostDateGmt = blogEntity.DateConverted.ToUniversalTime(), PostContent = blogEntity.Content, PostTitle = blogEntity.Subject, PostExcerpt = excerpt, PostStatus = "publish", CommentStatus = "open", PingStatus = "open", PostPassword = string.Empty, PostName = blogEntity.Subject, ToPing = string.Empty, Pinged = string.Empty, PostModified = blogEntity.DateConverted, PostModifiedGmt = blogEntity.DateConverted.ToUniversalTime(), PostContentFiltered = string.Empty, PostParent = 0, Guid = Guid.NewGuid().ToString().ToUpper(), MenuOrder = 0, PostType = "post", PostMimeType = string.Empty, RelatedLink = blogEntity.RelatedLink, CommentCount = (blogEntity.Comments ?? new List<PhpCommentEntity>()).Count }; // Comments if (blogEntity.Comments != null) { foreach (var c in blogEntity.Comments) { if (postEntity.Comments == null) { postEntity.Comments = new List<WpCommentEntity>(); } var comment = ConvertCommentEntity(c); var commentEntity = postEntity.Comments.Find( item => item.Date == comment.Date && item.Author == comment.Author && item.Content == comment.Content); if (commentEntity == null) { postEntity.Comments.Add(comment); } } } // Categories var cats = new List<WpCategoryEntity>(); if (blogEntity.CategoryList == null) { blogEntity.CategoryList = new List<PhpCategoryEntity>(); } if (postEntity.Categories == null) { postEntity.Categories = new List<WpCategoryEntity>(); } if (m_categories != null) { if (!string.IsNullOrEmpty(blogEntity.Categories)) { var categories = blogEntity.Categories.Split(','); foreach (var category in categories) { var phpCategoryEntity = m_categories.Find(item => item.ID == category); if (phpCategoryEntity != null) { var wpCategoryEntity = ConvertCategoryEntity(phpCategoryEntity); var categoryEntity = postEntity.Categories.Find(item => item.Description == wpCategoryEntity.Description); if (categoryEntity == null) { cats.Add(wpCategoryEntity); } postEntity.Categories = cats; } } } else { var phpCategoryEntity = m_categories.Find(item => item.Description == "General"); if (phpCategoryEntity != null) { var wpCategoryEntity = ConvertCategoryEntity(phpCategoryEntity); var categoryEntity = postEntity.Categories.Find(item => item.Description == wpCategoryEntity.Description); if (categoryEntity == null) { cats.Add(wpCategoryEntity); } postEntity.Categories = cats; } } } if (blogEntity.CategoryList != null && blogEntity.CategoryList.Count > 0) { postEntity.PostCategory = Convert.ToInt32(DefaultCategory); } else if (!string.IsNullOrEmpty(blogEntity.Categories)) { if ((blogEntity.Categories ?? string.Empty).Contains(",")) { var categories = (blogEntity.Categories ?? string.Empty).Split(','); postEntity.PostCategory = Convert.ToInt32(categories[0]); } else { postEntity.PostCategory = Convert.ToInt32(blogEntity.Categories); if (postEntity.PostCategory == 0) { postEntity.PostCategory = Convert.ToInt32(DefaultCategory); } } } else { postEntity.PostCategory = Convert.ToInt32(DefaultCategory); } return postEntity; }