示例#1
0
文件: App.cs 项目: AArnott/dasblog
        static int Main(string[] args)
        {
            Console.WriteLine("BlogWorksXML Importer");
            Console.WriteLine("(import supports BlogWorks version 1.1 and above)");

            foreach (string arg in args)
            {
                if (arg.Length > 6 && arg.ToLower().StartsWith("/from:"))
                {
                    from = arg.Substring(6).Trim();
                    if (from[0] == '\"' && from[from.Length] == '\"')
                    {
                        from = from.Substring(1, from.Length - 2);
                    }
                }
                else if (arg.Length > 6 && arg.ToLower().StartsWith("/id:")) {
                    id = arg.Substring(4).Trim();
                }
                else if (arg.Length > 6 && arg.ToLower().StartsWith("/to:"))
                {
                    to = arg.Substring(4).Trim();
                    if (to[0] == '\"' && to[from.Length] == '\"')
                    {
                        to = to.Substring(1, to.Length - 2);
                    }
                }
                else
                {
                    break;
                }
            }

            if (from == null || to == null || id == null || from.Length == 0 || to.Length == 0 || id.Length == 0)
            {
                Console.WriteLine("Usage: impbwxml /from:<blogworks data directory> [/id:<blogworks blog id, e.g. 001>] /to:<output directory>");
                Console.WriteLine("");
                return -1;
            }

            IBlogDataService dataService = BlogDataServiceFactory.GetService(to,null);

            Console.WriteLine("Importing entries from...");

            ArrayList tables = new ArrayList();
            ArrayList comments = new ArrayList();
            Hashtable commentRefs = new Hashtable();

            XmlDocument masterDoc = new XmlDocument();
            StringBuilder sb = new StringBuilder();
            sb.Append("<tables>");

            foreach (FileInfo file in new DirectoryInfo(from).GetFiles("*archive"+id+".xml"))
            {
                Console.Write("  * " + file.Name);
                XmlDocument doc = new XmlDocument();
                doc.Load(file.FullName);
                foreach (XmlNode n in doc.SelectNodes("/baef/blog")) {
                    sb.Append(n.OuterXml);
                }
                Console.WriteLine(" ... done.");
            }
            sb.Append("</tables>");

            masterDoc.Load(new StringReader(sb.ToString()));

            foreach (XmlNode node in masterDoc.FirstChild)
            {
                BlogWorksTable table = new BlogWorksTable();
                table.Name = node.Attributes["id"].Value;

                foreach (XmlNode child in node)	//  author with authorname, authormail childs
                {
                    switch (child.Name) {
                        case "author":
                            break;	// ignore. dasBlog is not yet multiuser enabled
                        case "information":
                            foreach (XmlNode infoNode in child) {	//  commentthread; timestamp; language; categories
                                // how about commentthread ?
                                switch (infoNode.Name) {
                                    case "commentthread":
                                        // save the reference for later use
                                        commentRefs.Add(infoNode.InnerText, table.Name);
                                        break;
                                    case "timestamp":
                                        table.Data[infoNode.Name] = UnixToHuman(infoNode.InnerText);
                                        break;
                                    case "language":
                                        if (infoNode.InnerText != "en")
                                            table.Data[infoNode.Name] = infoNode.InnerText;
                                        break;
                                    case "categories":
                                        foreach (XmlNode catNode in infoNode) {
                                            if (catNode.InnerText.Length > 0) {
                                                if (table.Data.Contains("categories")) {
                                                    table.Data["categories"] = (string)table.Data["categories"] + ";" + catNode.InnerText;
                                                }
                                                else {
                                                    table.Data["categories"] = catNode.InnerText;
                                                }
                                            }
                                        }
                                        break;
                                }
                            }
                            if (!table.Data.Contains("categories")) {
                                table.Data["categories"] = "General";
                            }
                            break;

                        case "text":	// blogtitle (entry title); blogbody (entry body)
                            foreach (XmlNode textNode in child) {
                                switch (textNode.Name) {
                                    case "blogtitle":
                                        table.Data[textNode.Name] = textNode.InnerText;
                                        break;
                                    case "blogbody":
                                        table.Data[textNode.Name] = textNode.InnerText;
                                        break;
                                }
                            }
                            break;
                    }
                }
                tables.Add(table);
            }

            Console.WriteLine("Now writing entries....");

            foreach (BlogWorksTable table in tables)
            {
                Entry entry = new Entry();
                entry.CreatedUtc = table.When;
                entry.Title = table.Title;
                entry.Content = table.Text;
                entry.Categories = table.Categories;
                entry.EntryId = table.UniqueId;
                entry.Language = table.Language;
                dataService.SaveEntry( entry );
            }

            Console.WriteLine("Finished. Start reading comments...");

            masterDoc = new XmlDocument();
            sb = new StringBuilder();
            sb.Append("<comments>");

            foreach (FileInfo file in new DirectoryInfo(from).GetFiles("*comment"+id+".xml")) {
                Console.Write("  * " + file.Name);
                XmlDocument doc = new XmlDocument();
                doc.Load(file.FullName);
                foreach (XmlNode n in doc.SelectNodes("/comments/thread")) {
                    sb.Append(n.OuterXml);
                }
                Console.WriteLine(" ... done.");
            }
            sb.Append("</comments>");

            masterDoc.Load(new StringReader(sb.ToString()));

            foreach (XmlNode node in masterDoc.FirstChild) {

                string threadId = node.Attributes["id"].Value;

                if (!commentRefs.Contains(threadId))
                    continue;

                foreach (XmlNode cmtNode in node) {	//  comment's per thread

                    BlogWorksComment comment = new BlogWorksComment();
                    comment.Name = (string)commentRefs[threadId];	// get corresponding entry Id

                    foreach (XmlNode child in cmtNode) {	//  comment elements

                        switch (child.Name) {
                            case "name":
                                comment.Data[child.Name] = child.InnerText;	// Author
                                break;
                            case "datetime":
                                comment.Data[child.Name] = DateTime.Parse(child.InnerText);
                                break;
                            case "email":
                                comment.Data[child.Name] = child.InnerText;
                                break;
                            case "uri":
                                if (child.InnerText.Length > 7 /* "http://".Length */)
                                    comment.Data[child.Name] = child.InnerText;
                                break;
                            case "text":
                                comment.Data[child.Name] = child.InnerText;
                                break;
                            case "ip":
                                comment.Data[child.Name] = child.Clone();	// anyElement
                                break;
                        }
                    }//child
                    comments.Add(comment);
                }//cmtNode

            }

            Console.WriteLine("Now writing comment entries....");

            foreach (BlogWorksComment cmt in comments) {
                Comment comment = new Comment();
                comment.Content = cmt.Text;
                comment.Author = cmt.Author;
                comment.TargetEntryId = cmt.UniqueId;
                comment.AuthorHomepage = cmt.AuthorHomepage;
                comment.AuthorEmail = cmt.AuthorEmail;
                comment.CreatedLocalTime = cmt.When;
                comment.CreatedUtc = cmt.When.ToUniversalTime();
                comment.anyElements = new XmlElement[]{cmt.Ip};

                dataService.AddComment(comment);
            }

            Console.WriteLine("Finished. Start reading comments...");

            Console.WriteLine("Finished successfully.");
            return 0;
        }
示例#2
0
        static int Main(string[] args)
        {
            Console.WriteLine("BlogWorksXML Importer");
            Console.WriteLine("(import supports BlogWorks version 1.1 and above)");


            foreach (string arg in args)
            {
                if (arg.Length > 6 && arg.ToLower().StartsWith("/from:"))
                {
                    from = arg.Substring(6).Trim();
                    if (from[0] == '\"' && from[from.Length] == '\"')
                    {
                        from = from.Substring(1, from.Length - 2);
                    }
                }
                else if (arg.Length > 6 && arg.ToLower().StartsWith("/id:"))
                {
                    id = arg.Substring(4).Trim();
                }
                else if (arg.Length > 6 && arg.ToLower().StartsWith("/to:"))
                {
                    to = arg.Substring(4).Trim();
                    if (to[0] == '\"' && to[from.Length] == '\"')
                    {
                        to = to.Substring(1, to.Length - 2);
                    }
                }
                else
                {
                    break;
                }
            }

            if (from == null || to == null || id == null || from.Length == 0 || to.Length == 0 || id.Length == 0)
            {
                Console.WriteLine("Usage: impbwxml /from:<blogworks data directory> [/id:<blogworks blog id, e.g. 001>] /to:<output directory>");
                Console.WriteLine("");
                return(-1);
            }


            IBlogDataService dataService = BlogDataServiceFactory.GetService(to, null);


            Console.WriteLine("Importing entries from...");

            ArrayList tables      = new ArrayList();
            ArrayList comments    = new ArrayList();
            Hashtable commentRefs = new Hashtable();

            XmlDocument   masterDoc = new XmlDocument();
            StringBuilder sb        = new StringBuilder();

            sb.Append("<tables>");

            foreach (FileInfo file in new DirectoryInfo(from).GetFiles("*archive" + id + ".xml"))
            {
                Console.Write("  * " + file.Name);
                XmlDocument doc = new XmlDocument();
                doc.Load(file.FullName);
                foreach (XmlNode n in doc.SelectNodes("/baef/blog"))
                {
                    sb.Append(n.OuterXml);
                }
                Console.WriteLine(" ... done.");
            }
            sb.Append("</tables>");

            masterDoc.Load(new StringReader(sb.ToString()));

            foreach (XmlNode node in masterDoc.FirstChild)
            {
                BlogWorksTable table = new BlogWorksTable();
                table.Name = node.Attributes["id"].Value;

                foreach (XmlNode child in node) //  author with authorname, authormail childs
                {
                    switch (child.Name)
                    {
                    case "author":
                        break;                                  // ignore. dasBlog is not yet multiuser enabled

                    case "information":
                        foreach (XmlNode infoNode in child)                                     //  commentthread; timestamp; language; categories
                        // how about commentthread ?
                        {
                            switch (infoNode.Name)
                            {
                            case "commentthread":
                                // save the reference for later use
                                commentRefs.Add(infoNode.InnerText, table.Name);
                                break;

                            case "timestamp":
                                table.Data[infoNode.Name] = UnixToHuman(infoNode.InnerText);
                                break;

                            case "language":
                                if (infoNode.InnerText != "en")
                                {
                                    table.Data[infoNode.Name] = infoNode.InnerText;
                                }
                                break;

                            case "categories":
                                foreach (XmlNode catNode in infoNode)
                                {
                                    if (catNode.InnerText.Length > 0)
                                    {
                                        if (table.Data.Contains("categories"))
                                        {
                                            table.Data["categories"] = (string)table.Data["categories"] + ";" + catNode.InnerText;
                                        }
                                        else
                                        {
                                            table.Data["categories"] = catNode.InnerText;
                                        }
                                    }
                                }
                                break;
                            }
                        }
                        if (!table.Data.Contains("categories"))
                        {
                            table.Data["categories"] = "General";
                        }
                        break;

                    case "text":                                // blogtitle (entry title); blogbody (entry body)
                        foreach (XmlNode textNode in child)
                        {
                            switch (textNode.Name)
                            {
                            case "blogtitle":
                                table.Data[textNode.Name] = textNode.InnerText;
                                break;

                            case "blogbody":
                                table.Data[textNode.Name] = textNode.InnerText;
                                break;
                            }
                        }
                        break;
                    }
                }
                tables.Add(table);
            }

            Console.WriteLine("Now writing entries....");

            foreach (BlogWorksTable table in tables)
            {
                Entry entry = new Entry();
                entry.CreatedUtc = table.When;
                entry.Title      = table.Title;
                entry.Content    = table.Text;
                entry.Categories = table.Categories;
                entry.EntryId    = table.UniqueId;
                entry.Language   = table.Language;
                dataService.SaveEntry(entry);
            }


            Console.WriteLine("Finished. Start reading comments...");


            masterDoc = new XmlDocument();
            sb        = new StringBuilder();
            sb.Append("<comments>");

            foreach (FileInfo file in new DirectoryInfo(from).GetFiles("*comment" + id + ".xml"))
            {
                Console.Write("  * " + file.Name);
                XmlDocument doc = new XmlDocument();
                doc.Load(file.FullName);
                foreach (XmlNode n in doc.SelectNodes("/comments/thread"))
                {
                    sb.Append(n.OuterXml);
                }
                Console.WriteLine(" ... done.");
            }
            sb.Append("</comments>");

            masterDoc.Load(new StringReader(sb.ToString()));

            foreach (XmlNode node in masterDoc.FirstChild)
            {
                string threadId = node.Attributes["id"].Value;

                if (!commentRefs.Contains(threadId))
                {
                    continue;
                }

                foreach (XmlNode cmtNode in node)                       //  comment's per thread

                {
                    BlogWorksComment comment = new BlogWorksComment();
                    comment.Name = (string)commentRefs[threadId];               // get corresponding entry Id

                    foreach (XmlNode child in cmtNode)                          //  comment elements

                    {
                        switch (child.Name)
                        {
                        case "name":
                            comment.Data[child.Name] = child.InnerText;                                         // Author
                            break;

                        case "datetime":
                            comment.Data[child.Name] = DateTime.Parse(child.InnerText);
                            break;

                        case "email":
                            comment.Data[child.Name] = child.InnerText;
                            break;

                        case "uri":
                            if (child.InnerText.Length > 7 /* "http://".Length */)
                            {
                                comment.Data[child.Name] = child.InnerText;
                            }
                            break;

                        case "text":
                            comment.Data[child.Name] = child.InnerText;
                            break;

                        case "ip":
                            comment.Data[child.Name] = child.Clone();                                           // anyElement
                            break;
                        }
                    }            //child
                    comments.Add(comment);
                }                //cmtNode
            }

            Console.WriteLine("Now writing comment entries....");

            foreach (BlogWorksComment cmt in comments)
            {
                Comment comment = new Comment();
                comment.Content          = cmt.Text;
                comment.Author           = cmt.Author;
                comment.TargetEntryId    = cmt.UniqueId;
                comment.AuthorHomepage   = cmt.AuthorHomepage;
                comment.AuthorEmail      = cmt.AuthorEmail;
                comment.CreatedLocalTime = cmt.When;
                comment.CreatedUtc       = cmt.When.ToUniversalTime();
                comment.anyElements      = new XmlElement[] { cmt.Ip };

                dataService.AddComment(comment);
            }

            Console.WriteLine("Finished. Start reading comments...");

            Console.WriteLine("Finished successfully.");
            return(0);
        }