/* JSPWiki to WordPress Migration Tool Copyright (C) 2007 Pavel Simakov http://www.softwaresecretweapons.com This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ package com.ecyrd.jspwiki.wp; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.Statement; import java.util.Collection; import java.util.Date; import java.util.HashSet; import java.util.Iterator; import java.util.Properties; import java.util.Set; import com.ecyrd.jspwiki.WikiContext; import com.ecyrd.jspwiki.WikiEngine; import com.ecyrd.jspwiki.WikiPage; import com.ecyrd.jspwiki.attachment.Attachment; import com.ecyrd.jspwiki.providers.WikiPageProvider; import com.oy.shared.util.StringNavigator; public class Export2Wp { public static void main(String [] args){ new Export2Wp().export( "D:/DEV/bin/jspwiki-2.0.52/webroot/jspwiki/WEB-INF/jspwiki.properties" ); } public void export ( String configFileName ) { try { Properties props = new Properties(); props.load(new FileInputStream(new File(configFileName))); props.put(WikiEngine.PROP_ENCODING, "windows-1252"); export(new WikiEngine(props), props); } catch (Exception e){ e.printStackTrace(); throw new RuntimeException("Export failed", e); } } private void export (WikiEngine engine, Properties props) throws Exception { // driver final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; Class.forName(JDBC_DRIVER); // connection Connection conn = DriverManager.getConnection( "jdbc:mysql://host:port/wrdp_db?user=name&password=pwd&useUnicode=true&characterEncoding=UTF-8" ); // export try { conn.setAutoCommit(false); export ("d:/DEV/jspwiki/export2wp/", engine, props, conn); conn.setAutoCommit(true); } finally { conn.close(); } // we have no time to wait for clean shutdown Runtime.getRuntime().exit(0); } private void export (String exportPath, WikiEngine engine, Properties properties, Connection conn) throws Exception { Set visited = new HashSet(); // pages Collection pages = engine.getPageManager().getAllPages(); Iterator ipages = pages.iterator(); while(ipages.hasNext()) { WikiPage page = (WikiPage) ipages.next(); if (!visited.contains(page.getName())){ exportPage(engine, properties, conn, page); visited.add(page.getName()); } } //attachments Collection files = engine.getAttachmentManager().getAllAttachments(); Iterator ifiles = files.iterator(); while(ifiles.hasNext()) { Attachment atta = (Attachment) ifiles.next(); exportAttachment(exportPath, engine, properties, conn, atta); } } private void exportPage (WikiEngine engine, Properties properties, Connection conn, WikiPage page) throws Exception { // params String name = page.getName(); String title = engine.beautifyTitle(page.getName()); Date lastmod = page.getLastModified(); String meta = engine.getPageManager().getPageText( page.getName(), WikiPageProvider.LATEST_VERSION ); // eval plugins { StringBuffer buf = new StringBuffer(); StringNavigator sn = new StringNavigator(meta); while(sn.tryNext("[{INSERT")){ buf.append(sn.prev); if (sn.tryNext("com.ecyrd.jspwiki.plugin.UnusedPagesPlugin")){ sn.next("}]"); continue; } if (sn.tryNext("com.ecyrd.jspwiki.plugin.InsertPagePlugin")){ sn.next("pageToInsert="); sn.next("}]"); buf.append("\n"); continue; } } buf.append(sn.next); meta = buf.toString(); } // eval html WikiContext context = new WikiContext( engine, page.getName() ); String html = engine.textToHTML(context, meta); // post String sql = "INSERT INTO wp_posts (" + "post_author, post_date, post_date_gmt, post_content, post_title, " + "post_category, 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(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; PreparedStatement pstmt = conn.prepareStatement(sql); // prepare pstmt.setObject(1, new Integer(1)); pstmt.setObject(2, lastmod); pstmt.setObject(3, lastmod); pstmt.setObject(4, html); pstmt.setObject(5, title); pstmt.setObject(6, new Integer(0)); pstmt.setObject(7, ""); pstmt.setObject(8, "publish"); // can also be "draft" pstmt.setObject(9, "open"); pstmt.setObject(10, "open"); pstmt.setObject(11, ""); pstmt.setObject(12, name.toLowerCase()); // WP forces slugs in lower case pstmt.setObject(13, ""); pstmt.setObject(14, ""); pstmt.setObject(15, lastmod); pstmt.setObject(16, lastmod); pstmt.setObject(17, ""); pstmt.setObject(18, new Integer(0)); pstmt.setObject(19, ""); pstmt.setObject(20, ""); pstmt.setObject(21, "page"); // can also be "post" pstmt.setObject(22, ""); pstmt.setObject(23, new Integer(0)); // execute try { pstmt.executeUpdate(); } catch (Exception e){ e.printStackTrace(); throw new RuntimeException(sql, e); } // meta sql = "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES (LAST_INSERT_ID(), 'runphp', 1);"; Statement stmt = conn.createStatement(); stmt.execute(sql); // progress System.out.println("Page:\t" + name); } private void exportAttachment (String exportPath, WikiEngine engine, Properties properties, Connection conn, Attachment atta) throws Exception { // qualified name String url = engine.getAttachmentURL(atta.getName()); StringNavigator sn = new StringNavigator(url); sn.next("http://www.softwaresecretweapons.com/jspwiki/attach?page="); url = sn.next; url = url.replaceAll("%2F", "/"); // copy file over InputStream is = engine.getAttachmentManager().getAttachmentStream(atta); File file = new File(exportPath + url); // makedirs File dir = new File(file.getParent()); dir.mkdirs(); // copy FileOutputStream fos = new FileOutputStream(file); while(true){ byte [] buf = new byte [64 * 1024]; int sz = is.read(buf); if (sz == -1){ break; } fos.write(buf, 0, sz); } fos.close(); // progress System.out.println("Attachment: " + url); } }