/// <summary> /// Validates the file. /// </summary> /// <param name="file">The file.</param> /// <param name="parser">The parser.</param> /// <returns></returns> private bool ValidateFile(FileInfo file, Parser parser) { var fileSystemHelper = new FileSystemHelper(); m_fileContent = new StringBuilder(); var sr = new StreamReader(fileSystemHelper.GetFileNameFromFileInfo(file), Encoding.Default); m_fileContent.Append(sr.ReadToEnd()); return parser == Parser.Comment ? m_fileContent.ToString().StartsWith("VERSION|") && m_fileContent.ToString().Contains("|NAME|") && m_fileContent.ToString().Contains("|CONTENT|") : m_fileContent.ToString().StartsWith("VERSION|") && m_fileContent.ToString().Contains("|SUBJECT|") && m_fileContent.ToString().Contains("|CONTENT|"); }
/// <summary> /// Converts the blog to SQL. /// </summary> /// <param name="sourcePath">The source path.</param> /// <param name="targetPath">The target path.</param> public void ConvertBlogToSql(string sourcePath, string targetPath) { // Create the categories m_categories = ParseCategories(CategoryList); // Go through the folder structure, looking for txt and GZ files // There are three levels under content: // Year (usually YY) // Month (usually MM) var fileSystemHelper = new FileSystemHelper(); WpPostTable = "wp_posts"; WpCommentTable = "wp_comments"; WpMetaTable = "wp_postmeta"; WpTermTable = "wp_terms"; WpTermRelationshipTable = "wp_term_relationships"; WpTermTaxonomyTable = "wp_term_taxonomy"; var di = new DirectoryInfo(sourcePath); // the content folder foreach (var yearFolder in di.GetDirectories()) { if (yearFolder.ToString().Trim().ToLower() == "counter" || yearFolder.ToString().Trim().ToLower() == "static") { // Static Pages if (yearFolder.ToString().Trim().ToLower() == "static") { foreach (var file in yearFolder.GetFiles()) { PrepareFiles(fileSystemHelper, file, Parser.Static); } } } else { // Blog entries foreach (var monthFolder in yearFolder.GetDirectories()) { foreach (var file in monthFolder.GetFiles()) { PrepareFiles(fileSystemHelper, file, Parser.BlogEntry); var commentFolderName = string.Format("{0}\\{1}\\comments", file.DirectoryName, file.Name.Replace(".gz", string.Empty).Replace(".txt", string. Empty)); if (Directory.Exists(commentFolderName) && Directory.GetFiles(commentFolderName).Length > 0) { var directoryInfo = new DirectoryInfo(commentFolderName); foreach (var fileInfo in directoryInfo.GetFiles()) { m_comments = new List<string>(); m_commentEntries = new List<PhpCommentEntity>(); PrepareFiles(fileSystemHelper, fileInfo, Parser.Comment); // Add comment to appropriate blog entry foreach (var b in m_blogEntries) { if (b.FileName == file.Name.Replace(".gz", string.Empty).Replace(".txt", string.Empty)) { foreach (var c in m_commentEntries) { if (b.Comments == null) { b.Comments = new List<PhpCommentEntity>(); } var phpCommentEntity = b.Comments.Find( item => item.Date == c.Date && item.Content == c.Content && item.Name == c.Name); if (phpCommentEntity == null) { b.Comments.Add(c); } } break; } } } } } } } } // Empty lists for GC if (m_statics != null) { m_statics.Clear(); } if (m_blogs != null) { m_blogs.Clear(); } if (m_comments != null) { m_comments.Clear(); } // Now go through the list of BlogEntities and convert them m_postEntities = new List<WpPostEntity>(); if (m_blogEntries == null) { m_blogEntries = new List<PhpBlogEntity>(); } foreach (var b in m_blogEntries) { m_postEntities.Add(ConvertBlogEntity(b)); } if (m_staticEntries == null) { m_staticEntries = new List<PhpStaticEntity>(); } foreach (var s in m_staticEntries) { m_postEntities.Add(ConvertStaticEntity(s)); } // Write to the SQL StringBuilder var sb = GenerateSql(m_postEntities); // Write to the SQL file fileSystemHelper.FileWrite(sb, targetPath); }
/// <summary> /// Prepares the files. /// </summary> /// <param name="fileSystemHelper">The file system helper.</param> /// <param name="file">The file.</param> /// <param name="parser">The parser.</param> private void PrepareFiles(FileSystemHelper fileSystemHelper, FileInfo file, Parser parser) { List<string> files = new List<string>(); switch (parser) { case Parser.BlogEntry: files = m_blogs; break; case Parser.Comment: files = m_comments; break; case Parser.Static: files = m_statics; break; } if (files == null) { files = new List<string>(); } if (!files.Contains(fileSystemHelper.GetFileNameFromFileInfo(file))) { FileInfo unzippedFile; if (!file.Extension.Contains(".gz")) unzippedFile = file; else { var fileName = fileSystemHelper.GetFileNameFromFileInfo(file).Replace(".gz", string.Empty); unzippedFile = !File.Exists(fileName) ? fileSystemHelper.UnzipFile(file) : file; } bool result; switch (parser) { case Parser.Comment: result = ParseComment(unzippedFile); break; case Parser.Static: result = ParseStaticPage(unzippedFile); break; //case Parser.File: default: result = ParseBlogEntry(unzippedFile); break; } if (result) { if (!files.Contains(fileSystemHelper.GetFileNameFromFileInfo(unzippedFile))) { files.Add(fileSystemHelper.GetFileNameFromFileInfo(unzippedFile)); } } } switch (parser) { case Parser.Static: m_statics = files; break; case Parser.Comment: m_comments = files; break; default: m_blogs = files; break; } }