将内容从随机博客迁移到DasBlog

This guy Ernie emailed me saying that he had some random, possibly home-grown, blog with content in Access that he wanted to migrate to his new DasBlog. I don't ordinarily do this (and don't plan to ever again) but it's so easy to move content into DasBlog that I decided to take this opportunity to write an example I could point other folks to.

这个人Ernie给我发了电子邮件,说他有一个随机的,可能是自己创建的博客,其中包含Access中的内容,他希望将该博客迁移到新的DasBlog中。 我通常不这样做(并且不打算再做一次),但是将内容移动到DasBlog中是如此容易,我决定借此机会编写一个可以向其他人指出的示例。

Note: this is just an example that converted Ernie's blog. It's not something that will work unchanged for you, so don't ask. :) Ernie had a "news" table and a "comments" table. Blog posts were in news and used "blogid" as a unique id.

注意:这只是转换Ernie博客的示例。 这不是对您有用的东西,所以不要问。 :) Ernie有一个“新闻”表和一个“评论”表。 博客文章在新闻中,并使用“ blogid”作为唯一ID。

Additional note: Often you can use the same unique ids in DasBlog. You don't need you use a GUID for the DasBlog EntryId. In this case, Ernie's BlogID was just an int and it was unique enough. He could implement redirects if someone visited /myoldblog.asp?id=4 to /mynewdasblog/permalink.aspx?guid=4 and all his old content would get reindexed by Google and none of the old links would be invalid.

附加说明:通常,您可以在DasBlog中使用相同的唯一ID。 您不需要为DasBlog EntryId使用GUID。 在这种情况下,Ernie的BlogID只是一个int,它足够唯一。 如果有人访问/myoldblog.asp?id=4到/mynewdasblog/permalink.aspx?guid=4并且他的所有旧内容都将由Google重新索引,并且所有旧链接都无效,则他可以实现重定向。

Just make a Console application in C# or VB.NET and add a reference to newtelligence.DasBlog.Runtime. I used a DataReader and just hard-coded the indexes of each column. It's not like this will ever be run again. It takes a little while to run because the DasBlog engine expects to run in the context of ASP.NET so it isn't able to use the ASP.NET Cache. The performance degrades O^n when run locally as your content folder grows. If that becomes a problem, run your converter in the context of IIS/ASP.NET and you'll get the high speed access.

只需在C#或VB.NET中创建一个控制台应用程序,然后添加对newtelligence.DasBlog.Runtime的引用即可。 我使用了DataReader,只是对每个列的索引进行了硬编码。 并不是说它将再次运行。 由于DasBlog引擎希望在ASP.NET上下文中运行,因此它无法使用ASP.NET缓存,因此需要花费一些时间。 当内容文件夹在本地运行时,性能会降低O ^ n。 如果这成为问题,请在IIS / ASP.NET上下文中运行转换器,您将获得高速访问。

In this example, the DasBlog dayentry.xml and dayfeedback.xml files will be left in the directory that the application was run in. I also converted newlines to <br> and when a blog post didn't include a title, I used the first 20 characters of the post as the title.

在此示例中,DasBlog dayentry.xml和dayfeedback.xml文件将保留在运行该应用程序的目录中。我还将换行符转换为<br>,并且当博客文章中不包含标题时,我使用了帖子的前20个字符作为标题。

This took about 15 mins at lunch, so you really can transfer all your existing blog's data to DasBlog quickly, usually using just the Entry class and the Comment class. Note how they relate to each other using the EntryId as the key.

这在午餐时间大约花费了15分钟,因此您实际上可以将现有博客的所有数据快速传输到DasBlog,通常只需使用Entry类和Comment类即可。 注意使用EntryId作为键如何将它们彼此关联。

static void Main(string[] args)
{
    IBlogDataService dataService = 
BlogDataServiceFactory.GetService(AppDomain.CurrentDomain.BaseDirectory,null);
    string connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;
Data Source=ernie.mdb;Mode=Share Deny None";
    using(OleDbConnection conn = new OleDbConnection(connStr))
    {
        conn.Open();
        using(OleDbCommand newsCmd = new OleDbCommand("select * from News",conn))
        {
            using (OleDbDataReader reader = newsCmd.ExecuteReader())
            {
                while(reader.Read())
                {
                    int blogId = reader.GetInt32(0);
                    DateTime date = reader.GetDateTime(1);
                    DateTime time = reader.GetDateTime(3);
                    DateTime correctDate = 
new DateTime(date.Year,date.Month,date.Day,time.Hour,time.Minute,time.Second);
                    string blogText = reader.GetString(2);
                    string blogTitle = reader.IsDBNull(4) ? String.Empty : reader.GetString(4);
 
                    Entry entry = new Entry();
                    entry.CreatedLocalTime = correctDate;
                    entry.ModifiedLocalTime = correctDate;
                    entry.Title = 
(blogTitle.Length > 0 ? blogTitle :
blogText.Substring(0,Math.Min(20,blogText.Length)));
                    entry.Content = blogText.Replace("\r\n","<br>");
                    entry.EntryId = blogId.ToString();
                    entry.Categories = "main;old site";
                    entry.Author = "Ernie";
                    dataService.SaveEntry(entry);
                }
            }
        }
 
        using(OleDbCommand newsCmd = new OleDbCommand("select * from comments",conn))
        {
            using (OleDbDataReader reader = newsCmd.ExecuteReader())
            {
                while(reader.Read())
                {
                    int blogId = reader.GetInt32(1);
                    DateTime date = reader.GetDateTime(4);
                    string commentText = reader.GetString(2);
                    string commentName = reader.GetString(3);
 
                    Comment comment = new Comment();
                    comment.CreatedLocalTime = date;
                    comment.ModifiedLocalTime = date;
                    comment.TargetEntryId = blogId.ToString();
                    comment.Author = commentName;
                    comment.Content = commentText;
                    dataService.AddComment(comment);
                }
            }
        }
    }

翻译自: https://www.hanselman.com/blog/migrating-content-from-random-blogs-to-dasblog

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值