<<<<<<< HEAD --- layout: default ---

Importing Drupal Entries to DasBlog

I certainly see the benefit of posting the code that I used to import my old Drupal entries into DasBlog, this code was heavily inspired by Scott Hanselmann's example

using System; using newtelligence.DasBlog.Web.Services; using newtelligence.DasBlog.Runtime; using MySql.Data.MySqlClient; using System.Configuration; namespace ImportDrupal
        {     ///
        <summary>     ///
        Summary description for Class1.     ///
        </summary>     class Class1
            {         ///
        <summary>         ///
        The main entry point for the application.         ///
        </summary>         [STAThread]
                static void Main(string[]
        args)         {             IBlogDataService
        dataService =                 BlogDataServiceFactory.GetService(AppDomain.CurrentDomain.BaseDirectory,null);
                    string connStr = ConfigurationSettings.AppSettings["DrupalConnectionString"];
                    string author = ConfigurationSettings.AppSettings["author"];
                    using(MySqlConnection
        conn = new MySqlConnection(connStr))
                    {                 conn.Open();
                        using(MySqlCommand
        newsCmd = new MySqlCommand("SELECT
        DISTINCT * FROM `node_revisions`",conn))                 {
                            using (MySqlDataReader
        reader = newsCmd.ExecuteReader())
                            {
                                while(reader.Read())
                                {
                                    int blogId = reader.GetInt32(0);
                                    int timeStamp = reader.GetInt32(6);
                                    DateTime
        correctDate = ToDateTime(timeStamp);
                                    
                                    
                                    string blogText = reader.GetString(4);
                                    string blogTitle = reader.IsDBNull(3)
        ? String.Empty : reader.GetString(3);                             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 = GetNodeCategories(
        blogId );                             entry.Author = author;
                                    dataService.SaveEntry(entry);
                                }
                            }
                        }
                        using(MySqlCommand
        newsCmd = new MySqlCommand("select
        * from comments",conn))                 {
                            using (MySqlDataReader
        reader = newsCmd.ExecuteReader())
                            {
                                while(reader.Read())
                                {
                                    int blogId = reader.GetInt32(2);
                                    int timeStamp = reader.GetInt32(7);
                                    DateTime
        date = ToDateTime(timeStamp);
                                    string commentText = reader.GetString(5);
                                    string commentName = reader.GetString(13);
                                    Comment
        comment = new Comment();
                                    comment.CreatedLocalTime = date;
                                    comment.ModifiedLocalTime = date;
                                    comment.TargetEntryId = blogId.ToString();
                                    comment.Author = commentName;
                                    comment.Content = commentText;
                                    dataService.AddComment(comment);
                                }
                            }
                        }
                    }         }
                public static string GetNodeCategories(int nodeId
        )         {             string connStr = ConfigurationSettings.AppSettings["DrupalConnectionString"];
                    string output = "";
                    using(MySqlConnection
        conn = new MySqlConnection(connStr))
                    {                 conn.Open();
                        using(MySqlCommand
        termCmd = new MySqlCommand(@"SELECT
        name                                                                     FROM
        term_data                                                                     INNER
        JOIN term_node ON term_node.tid = term_data.tid                                                                     WHERE
        term_node.nid = ?nid"                             ,conn))
                        {
                            termCmd.Parameters.Add("?nid",
        nodeId );                     using (MySqlDataReader
        reader = termCmd.ExecuteReader())
                            {
                                while(
        reader.Read() )                         {
                                    if(
        output != "" )
                                    {
                                        output
        += ",";
                                    }
                                    output
        += reader.GetString(0);                         }
                            }
                        }
                    }             return output;
                }         private static DateTime
        ToDateTime( int timeStamp
        )         {             //
        First make a System.DateTime equivalent to the UNIX Epoch.             System.DateTime
        dateTime = new System.DateTime(1970,
        1, 1, 0, 0, 0, 0);             //
        Add the number of seconds in UNIX timestamp to be converted.             dateTime = dateTime.AddSeconds(timeStamp);
                    return dateTime;
                }     } } 

Related Posts