Skip to content

Commit

Permalink
my little script to convert from b2evolution to wordpress
Browse files Browse the repository at this point in the history
  • Loading branch information
kohsuke committed Nov 16, 2011
0 parents commit 1a18c48
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
35 changes: 35 additions & 0 deletions pom.xml
@@ -0,0 +1,35 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>test</groupId>
<artifactId>b2evo-import</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>b2evo-import</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
</dependencies>
</project>
74 changes: 74 additions & 0 deletions src/main/java/test/App.java
@@ -0,0 +1,74 @@
package test;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Hello world!
*
*/
public class App
{
// tweaked atom feed and have it return all comments

public static void main( String[] args ) throws Exception {
Map<String,Integer> ids = buildIdMap();

SAXReader r = new SAXReader();
Document dom = r.read(new File("shiori.comment.rss"));
List<Element> list = dom.getRootElement().elements("entry");
for (Element e : list) {
String title = e.elementText("title");
title = title.substring(title.indexOf(':')+2);

String name = e.element("author").elementText("name");
name = name.substring(0,name.lastIndexOf('[')-1);
String date = e.elementText("issued");
date = date.replace('T',' ').replaceAll("Z","");
String content = e.elementText("content");

int id = ids.get(title);

System.out.printf("INSERT INTO wp_comments (comment_post_ID,comment_author,comment_date,comment_date_gmt,comment_content) "
+"VALUES(%d,\"%s\",\"%s\",\"%s\",\"%s\");\n", id,
name, date, date, escape(content));
}
}

private static String escape(String content) {
StringBuilder b = new StringBuilder(content.length());
for (int i=0; i<content.length(); i++) {
char ch = content.charAt(i);
int idx = "'\"\n\r\t\\".indexOf(ch);
if (idx <0)
b.append(ch);
else
b.append('\\').append("'\"nrt\\".charAt(idx));
}
return b.toString();
}

private static Map<String,Integer> buildIdMap() throws IOException {
Map<String,Integer> postIds = new HashMap<String, Integer>();
BufferedReader r = new BufferedReader(new FileReader("wp-posts.csv"));
r.readLine(); // caption
String s;
while ((s=r.readLine())!=null) {
int idx = s.indexOf(',');
int id = Integer.parseInt(s.substring(0,idx));
String post = s.substring(idx+1);
post = post.substring(1,post.length()-1);
postIds.put(post,id);
}
return postIds;
}
}
38 changes: 38 additions & 0 deletions src/test/java/test/AppTest.java
@@ -0,0 +1,38 @@
package test;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}

/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}

/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}

0 comments on commit 1a18c48

Please sign in to comment.