Last active
December 18, 2015 20:39
-
-
Save jonbullock/5841502 to your computer and use it in GitHub Desktop.
WordPress import script for JBake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.jbake.launcher; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.OutputStreamWriter; | |
import java.io.Writer; | |
import java.sql.Connection; | |
import java.sql.Driver; | |
import java.sql.DriverManager; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import java.sql.Timestamp; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.GregorianCalendar; | |
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* WordPress import script for JBake | |
* | |
* @author Jon Bullock <[email protected]> | |
* | |
*/ | |
public class WordPress { | |
private static final String USAGE = "Usage: wordpress <host> <database> <user> <pass> <destination_path>"; | |
private static String HOST; | |
private static String DATABASE; | |
private static String USER; | |
private static String PASS; | |
private static File DESTINATION; | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
if (args.length == 0) { | |
System.out.println(USAGE); | |
} else { | |
if (args.length != 5) { | |
System.out.println(USAGE); | |
} else { | |
HOST = args[0]; | |
DATABASE = args[1]; | |
USER = args[2]; | |
PASS = args[3]; | |
DESTINATION = new File(args[4]); | |
if (!DESTINATION.exists()) { | |
System.out.println(USAGE); | |
} else { | |
// deal with pages | |
processPages(); | |
// deal with posts | |
processPosts(); | |
} | |
} | |
} | |
} | |
private static void processPages() { | |
Connection conn = null; | |
Statement stmt = null; | |
ResultSet rs = null; | |
try { | |
Class.forName("com.mysql.jdbc.Driver"); | |
conn = DriverManager.getConnection("jdbc:mysql://"+HOST+"/"+DATABASE+"?user="+USER+"&password="+PASS+"&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull"); | |
stmt = conn.createStatement(); | |
rs = stmt.executeQuery("select * from wp_posts where post_type = 'page'"); | |
while (rs.next()) { | |
Map<String, Object> content = new HashMap<String, Object>(); | |
content.put("title", rs.getString("post_title")); | |
Timestamp date = rs.getTimestamp("post_date_gmt"); | |
if (date == null) { | |
date = rs.getTimestamp("post_date"); | |
} | |
content.put("date", date); | |
String status = rs.getString("post_status"); | |
if (status.equalsIgnoreCase("publish")) { | |
content.put("status", "published"); | |
} | |
if (status.equalsIgnoreCase("draft")) { | |
content.put("status", "draft"); | |
} | |
content.put("type", "page"); | |
content.put("body", rs.getString("post_content")); | |
// System.out.println("Post ID = [" + rs.getString("ID") + "]"); | |
// System.out.println("Page title = ["+ rs.getString("post_title") + "]"); | |
// System.out.println("Page status = ["+ rs.getString("post_status") + "]"); | |
String slug = rs.getString("post_name"); | |
if (slug.equals("")) { | |
slug = rs.getString("post_title").toLowerCase().replaceAll("[^a-z0-9-]", ""); | |
} | |
File outputFile = new File(DESTINATION+File.separator+slug+".html"); | |
writeFile(outputFile, content); | |
} | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} finally { | |
try { | |
if (rs != null) | |
rs.close(); | |
if (stmt != null) | |
stmt.close(); | |
if (conn != null) | |
conn.close(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
private static void processPosts() { | |
Connection conn = null; | |
Statement stmt = null; | |
ResultSet rs = null; | |
try { | |
Class.forName("com.mysql.jdbc.Driver"); | |
conn = DriverManager.getConnection("jdbc:mysql://"+HOST+"/"+DATABASE+"?user="+USER+"&password="+PASS+"&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull"); | |
stmt = conn.createStatement(); | |
rs = stmt.executeQuery("select * from wp_posts where post_type = 'post' and (post_status = 'draft' or post_status = 'publish') order by post_date"); | |
while (rs.next()) { | |
Map<String, Object> content = new HashMap<String, Object>(); | |
content.put("title", rs.getString("post_title")); | |
Timestamp date = rs.getTimestamp("post_date_gmt"); | |
if (date == null) { | |
date = rs.getTimestamp("post_date"); | |
} | |
content.put("date", date); | |
String status = rs.getString("post_status"); | |
if (status.equalsIgnoreCase("publish")) { | |
content.put("status", "published"); | |
} | |
if (status.equalsIgnoreCase("draft")) { | |
content.put("status", "draft"); | |
} | |
content.put("type", "post"); | |
content.put("body", rs.getString("post_content").replace("\n", "<br />")); | |
content.put("tags", getTags(rs.getLong("ID"))); | |
String slug = rs.getString("post_name"); | |
if (slug.equals("")) { | |
slug = rs.getString("post_title").trim().toLowerCase().replaceAll("\\s+", "-").replaceAll("[^a-z0-9-]", "").replaceAll("-+", "-"); | |
} | |
SimpleDateFormat year = new SimpleDateFormat("yyyy"); | |
SimpleDateFormat month = new SimpleDateFormat("MM"); | |
SimpleDateFormat day = new SimpleDateFormat("dd"); | |
String outputFilename = ""; | |
if (content.get("status").toString().equals("published")) { | |
outputFilename = DESTINATION+File.separator+(year.format(date))+File.separator+(month.format(date))+File.separator+slug+".html"; | |
} else { | |
outputFilename = DESTINATION+File.separator+"drafts"+File.separator+slug+".html"; | |
} | |
File outputFile = new File(outputFilename); | |
writeFile(outputFile, content); | |
} | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} finally { | |
try { | |
if (rs != null) | |
rs.close(); | |
if (stmt != null) | |
stmt.close(); | |
if (conn != null) | |
conn.close(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
private static String getTags(long id) { | |
StringBuffer tags = new StringBuffer(); | |
Connection conn = null; | |
Statement stmt = null; | |
ResultSet rs = null; | |
try { | |
Class.forName("com.mysql.jdbc.Driver"); | |
conn = DriverManager.getConnection("jdbc:mysql://"+HOST+"/"+DATABASE+"?user="+USER+"&password="+PASS); | |
// deal with pages | |
stmt = conn.createStatement(); | |
rs = stmt.executeQuery("select wp_terms.term_id, wp_terms.slug from wp_term_taxonomy inner join wp_term_relationships on wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id inner join wp_terms on wp_term_taxonomy.term_id = wp_terms.term_id where wp_term_taxonomy.taxonomy = 'post_tag' and wp_term_relationships.object_id = " + id); | |
while (rs.next()) { | |
tags.append(rs.getString("slug")+","); | |
// System.out.println("Tag = " + rs.getString("slug")); | |
} | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} finally { | |
try { | |
if (rs != null) | |
rs.close(); | |
if (stmt != null) | |
stmt.close(); | |
if (conn != null) | |
conn.close(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
if (tags.length() > 0) { | |
return tags.substring(0, tags.length()-1); | |
} else { | |
return tags.toString(); | |
} | |
} | |
private static void writeFile(File file, Map<String, Object> content) { | |
System.out.print("Writing file [" + file.getPath() + "]..."); | |
try { | |
if (!file.exists()) { | |
file.getParentFile().mkdirs(); | |
file.createNewFile(); | |
} | |
Writer out = new OutputStreamWriter(new FileOutputStream(file)); | |
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); | |
out.write("title=" + content.get("title") + "\n"); | |
if (content.get("date") != null) { | |
out.write("date=" + df.format(content.get("date")) + "\n"); | |
} | |
out.write("type=" + content.get("type").toString().toLowerCase() + "\n"); | |
if (content.get("tags") != null && content.get("tags").toString().length() > 0) { | |
out.write("tags=" + content.get("tags") + "\n"); | |
} | |
out.write("status=" + content.get("status").toString().toLowerCase() + "\n"); | |
out.write("~~~~~~\n"); | |
out.write("\n"); | |
out.write(content.get("body").toString()); | |
out.close(); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
System.out.println("done!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment