Created
March 15, 2016 13:50
-
-
Save k1sul1/e82891be38da7187c428 to your computer and use it in GitHub Desktop.
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
var fs = require("fs"); | |
var mysql = require("mysql"); | |
var async = require("async"); | |
var filename = process.argv[2]; | |
// NOTE: This is shaped to a very spesific format of json, you have to adjust each function to suit your format. | |
var db = mysql.createConnection({ | |
host: "localhost", | |
user: "user", | |
password: "pass", | |
database: "db" | |
}); | |
fs.readFile(filename, "utf-8", onFile); | |
function onFile(err, data){ | |
if(err){ | |
return console.error(err); | |
} | |
data = JSON.parse(data); | |
async.map(data, importThread, function(err, results){ | |
if(err){ | |
return console.error(err); | |
} | |
console.log(results); | |
}); | |
} | |
function importThread(threadObj){ | |
var topic = threadObj.posts[0]; | |
var replies = threadObj.posts.slice(1); | |
var query = "INSERT INTO wp_posts (post_author, post_date, post_content, post_title, post_name, post_parent, post_type) VALUES (?, ?, ?, ?, ?, ?, ?)"; | |
var params = [topic.regUserId, topic.created, topic.post, topic.subject, sanitize_title(topic.subject), topic.forum_id, "topic"]; | |
db.query(query, params, function(err, result){ | |
if(err){ | |
return console.error(err); | |
} | |
// inserted, proceed! | |
var topic_id = result.insertId; // https://github.com/felixge/node-mysql#getting-the-id-of-an-inserted-row | |
createReplies(topic_id, replies); | |
insertMeta(topic_id, "_bbp_author_ip", topic.ipAddress); | |
insertMeta(topic_id, "_bbp_topic_id", topic_id); | |
insertMeta(topic_id, "_bbp_forum_id", topic.forum_id); | |
}); | |
} | |
function createReplies(topic_id, replies){ | |
replies.forEach(function(reply, reply_index){ | |
var query = "INSERT INTO wp_posts (post_author, post_date, post_content, post_title, post_name, post_parent, post_type) VALUES (?, ?, ?, ?, ?, ?, ?)"; | |
var params = [reply.regUserId, reply.created, reply.post, reply.subject, sanitize_title(reply.subject) + "-" + reply_index, topic_id, "reply"]; | |
db.query(query, params, function(err, result){ | |
if(err){ | |
return console.error(err); | |
} | |
else{ | |
// notify of success? | |
var reply_id = result.insertId; // https://github.com/felixge/node-mysql#getting-the-id-of-an-inserted-row | |
insertMeta(reply_id, "_bbp_author_ip", reply.ipAddress); | |
insertMeta(reply_id, "_bbp_topic_id", topic_id); | |
insertMeta(reply_id, "_bbp_forum_id", reply.forum_id); | |
} | |
}); | |
}); | |
} | |
function insertMeta(post_id, meta_key, meta_value){ | |
var query = "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES (?,?,?);"; | |
var params = [post_id, meta_key, meta_value]; | |
query = db.query(query, params, function(err, result){ | |
if(err){ | |
return console.error(err); | |
} | |
}); | |
} | |
function sanitize_title(title){ | |
// TODO: Implement this, pretty permalinks won't work without. | |
return title; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment