Zum Anfang der Serie Artikel-Migration von CMS nach WordPress – Teil 1
Schritt 9: Die Migration der Artikel nach WordPress
Nach soviel Vorarbeit und Analyse wird es endlich spannend. Wir haben für die Migration ein Programm in C# .NET geschrieben. Denkbar wäre auch, die Migration per PHP umzusetzen. In unserem Beispiel haben wir zwei Datenbanken („db_alt“ und „db_neu“). In der „db_alt“ ist lediglich die Tabelle „artikel“ vorhanden. In dieser Tabelle sind alle Artikel des Altsystems gespeichert. In der Datenbank „db_neu“ wurden per Export alle Tabellen der WordPress-Installation eingespielt. So haben wir die Möglichkeit, die Migration mehrfach durchzuführen, da wir die Datenbank „db_neu“ bedenkenlos neu aufsetzen können. Von Migrationen auf Echtsystemen ohne Backup ist abzuraten.
using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using System.IO;
using System.Collections;
namespace WP_import
{
class Program
{
public static string connALT = "data source=127.0.0.1;DATABASE=db_alt;UID=root;PASSWORD=xxx;";
public static string connNEU = "data source=127.0.0.1;DATABASE=db_neu;UID=root;PASSWORD=xxx;";
public static ArrayList list = new ArrayList();
public static string sRubrikid="-1";
public static string pic="";
static void Main(string[] args)
{
Console.WriteLine("START");
GetData();
Console.WriteLine("ENDE");
Console.ReadLine();
}
private static string GetmyDate(string sTimestamp)
{
string sRes = "";
try
{
sRes = sTimestamp;
sRes = sTimestamp.Substring(6, 4) + "-" + sTimestamp.Substring(3, 2) + "-" + sTimestamp.Substring(0, 2) + sTimestamp.Substring(10);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return sRes;
}
private static void GetData()
{
try
{
//Lies hier einmal alles aus der alten Artikel-Tabelle ein
string sSQL = "SELECT modelleid,timestamp,titel,perm,beschreibung, text FROM artikel where status=1";
MySqlDataReader rdr = null;
MySqlConnection cn = new MySqlConnection(connALT);
cn.Open();
MySqlCommand cmd2 = new MySqlCommand(sSQL, cn);
rdr = cmd2.ExecuteReader();
while (rdr.Read())
{
list.Clear();
string sDate = "";
try
{
//Mach ein Date aus dem Timestamp
string s = rdr["timestamp"].ToString();
sDate = GetmyDate(s);
}
catch {
sDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
//Benötigte Felder in WordPress
// post_author: ID des Autors (1)
list.Add("1");
// post_date: DateTime
list.Add(sDate);
// post_date_gmt: DateTime
list.Add(sDate);
// post_content: Text
list.Add(Umsetzung(rdr["beschreibung"].ToString()) + "<br>" + Umsetzung(rdr["text"].ToString()));
// post_title: Überschrift
list.Add(Umsetzung(rdr["titel"].ToString()));
// post_excerpt=''
list.Add("");
// post_status=publish
list.Add("publish");
// comment_status=closed
list.Add("closed");
// ping_status=closed
list.Add("closed");
// post_password=''
list.Add("");
// post_name='name-des-titels-like-seo'
string sBuffer = rdr["perm"].ToString();
sBuffer = sBuffer.Replace("_", "-");
list.Add(sBuffer);
//to_ping=''
list.Add("");
//pinged=''
list.Add("");
// post_modified: DateTime
list.Add(sDate);
// post_modified_gmt: DateTime
list.Add(sDate);
// post_content_filtered=''
list.Add("");
// post_parent=0
list.Add("0");
//Guid='http://www.domain.de/?p=' + ID (eigen)
list.Add("http://www.domain.de/?p=");
// menu_order=0
list.Add("0");
//post_type='post'
list.Add("post");
//post_mime_type=''
list.Add("");
//comment_count=''
list.Add("0");
//Rubrikid umschlüsseln
switch (rdr["modelleid"].ToString())
{
case "1": sRubrikid = "4"; break;
case "2": sRubrikid = "5"; break;
case "3": sRubrikid = "6"; break;
case "4": sRubrikid = "7"; break;
case "5": sRubrikid = "8"; break;
}
Console.WriteLine("Lege Artikel an ...");
LegeArtikelan();
}
rdr.Close();
cn.Close();
}
catch (MySqlException ex)
{
Console.WriteLine(ex.Message);
}
}
//Benötigte Felder in WordPress
// post_author: ID des Autors (1)
// post_date: DateTime
// post_date_gmt: DateTime
// post_content: Text
// post_title: Überschrift
// post_excerpt=''
// post_status=publish
// comment_status=closed
// ping_status=closed
// post_password=''
// post_name='name-des-titels-like-seo'
//to_ping=''
//pinged=''
// post_modified: DateTime
// post_modified_gmt: DateTime
// post_content_filtered=''
// post_parent=0
// Guid='http://www.domain.de/?p=' + ID (eigen)
// menu_order=0
//post_type='post'
//post_mime_type=''
//comment_count=''
private static string Umsetzung(string sText)
{
//Schlüssel die gängigsten HTML-Sonderzeichen wieder um
sText = sText.Replace("&", "&");
sText = sText.Replace("ä", "ä");
sText = sText.Replace("Ä", "Ä");
sText = sText.Replace("ö", "ö");
sText = sText.Replace("Ö", "Ö");
sText = sText.Replace("ü", "ü");
sText = sText.Replace("Ü", "Ü");
sText = sText.Replace("ß", "ß");
sText = sText.Replace("<", "<");
sText = sText.Replace(">", ">");
sText = sText.Replace(""", """);
sText = sText.Replace(" ", " ");
return sText;
}
private static void LegeArtikelan()
{
string sSQL = "";
try
{
sSQL = "INSERT into wp_posts (post_author,post_date,post_date_gmt,post_content,post_title,post_excerpt,post_status," +
"comment_status,ping_status,post_password,post_name,to_ping,pinged,post_modified,post_modified_gmt,post_content_filtered,"+
"post_parent,guid,menu_order,post_type,post_mime_type,comment_count) VALUES (";
int i = 0;
foreach (string sValue in list)
{
sSQL +="'"+sValue+"'";
i++;
if (i < list.Count) sSQL += ",";
}
sSQL+=")";
// post_author: ID des Autors (1)
// post_date: DateTime
// post_date_gmt: DateTime
// post_content: Text
// post_title: Überschrift
// post_excerpt=''
// post_status=publish
// comment_status=closed
// ping_status=closed
// post_password=''
// post_name='name-des-titels-like-seo'
//to_ping=''
//pinged=''
// post_modified: DateTime
// post_modified_gmt: DateTime
// post_content_filtered=''
// post_parent=0
//Guid='http://www.domain.de/?p=' + ID (eigen)
// menu_order=0
//post_type='post'
//post_mime_type=''
//comment_count=''
MySqlConnection cn = new MySqlConnection(connNEU);
cn.Open();
MySqlCommand cmd = new MySqlCommand(sSQL, cn);
cmd.ExecuteNonQuery();
//cn.Close();
//ID des Artikels ermitteln
sSQL = "SELECT @@IDENTITY AS 'LastID'";
MySqlCommand dbcSelect = new MySqlCommand(sSQL, cn);
MySqlDataReader dbrSelect = dbcSelect.ExecuteReader();
dbrSelect.Read();
int iLastID = Int32.Parse(dbrSelect.GetValue(0).ToString());
dbrSelect.Dispose();
//Mach jetzt ein Update auf den eben eingefügten DS (Um die guid zu aktualisieren)
sSQL = "UPDATE wp_posts SET guid = concat(`guid`,"+iLastID.ToString()+") WHERE ID=" + iLastID.ToString();
MySqlCommand cmd2 = new MySqlCommand(sSQL, cn);
cmd2.ExecuteNonQuery();
//Lege nun die Rubrik zu diesem Artikel an
sSQL = "INSERT into wp_term_relationships (object_id, term_taxonomy_id, term_order) VALUES ("+
iLastID.ToString()+"," + sRubrikid + ",0)";
MySqlCommand cmd3 = new MySqlCommand(sSQL, cn);
cmd3.ExecuteNonQuery();
//Hier könnten noch Meta-Daten gespeichert werden: wp_postmeta
sSQL = "INSERT into wp_postmeta (post_id, meta_key, meta_value) VALUES ("+
iLastID.ToString() + ",'Meta-Name','Meta_value')";
MySqlCommand cmd4 = new MySqlCommand(sSQL, cn);
cmd4.ExecuteNonQuery();
cn.Close();
}
catch (MySqlException ex)
{
Console.WriteLine(ex.Message);
}
}
}
}