Skip to content

Instantly share code, notes, and snippets.

@MikeCoder MikeCoder/em2wp.php
Created Mar 6, 2018

Embed
What would you like to do?
Emlog to Wordpress
<?php
/**
* Emlog 5.3.1 to Wordpress 4.9.4
* created by i@wyt.me
* modified by mike@mikecoder.cn
* run on php7+ and mysql5.6+
*/
if (!function_exists('d')) {
function d($value)
{
if (is_array($value)) {
header("Content-type: application/json");
echo json_encode($value, JSON_UNESCAPED_UNICODE);
} else {
echo '<pre>';
var_dump($value);
echo '</pre>';
}
}
}
if (!function_exists('dd')) {
function dd($value)
{
d($value);
die(0);
}
}
if (!function_exists('json_return')) {
function json_return($value)
{
dd($value);
}
}
set_time_limit(0);
header("Content-type:text/html;charset=utf-8");
if (isset($_POST["host"]) and $_GET["act"]=="doDB") {
$i_arr=array();
$tag_num=0;
$host = trim($_POST['host']);
$username = trim($_POST['username']);
$password = trim($_POST['password']);
$em_db = trim($_POST['em_db']);
$wp_db = trim($_POST['wp_db']);
$em_prefix = trim($_POST['em_prefix']);
$wp_prefix = trim($_POST['wp_prefix']);
// 连接WP数据库连接
$wpdb = new MYSQL($host, $username, $password, $wp_db);
// 清空WP数据表
$wpdb->executeCommand("TRUNCATE {$wp_prefix}posts");
$wpdb->executeCommand("TRUNCATE {$wp_prefix}comments");
$wpdb->executeCommand("TRUNCATE {$wp_prefix}links");
$wpdb->executeCommand("TRUNCATE {$wp_prefix}terms");
$wpdb->executeCommand("TRUNCATE {$wp_prefix}term_relationships");
$wpdb->executeCommand("TRUNCATE {$wp_prefix}term_taxonomy");
// 连接EM数据库
$em_conn = mysqli_connect($host, $username, $password);
mysqli_select_db($em_conn, $em_db);
mysqli_query($em_conn, "SET NAMES utf8");
/* 文章 */
$post_sql = "SELECT * FROM {$em_prefix}blog";
$post_result = mysqli_query($em_conn, $post_sql);
while ($row = mysqli_fetch_array($post_result)) {
$doPost=$wpdb->insert(
$wp_prefix."posts",
array(
'ID' => $row['gid'],
'post_author' => $row['author'],
'post_date' => date("Y-m-d H:i:s", $row['date']),
'post_date_gmt' => date("Y-m-d H:i:s", $row['date']),
'post_content' => addslashes($row['content']),
'post_excerpt' => addslashes($row['excerpt']),
'post_title' => $row['title'],
'post_status' => $row['hide'] === 'n' ? 'publish' : 'draft',
'comment_status' => $row['allow_remark'] === 'y' ? 'open' : 'closed',
'ping_status' => "open",
'post_password' => $row['password'],
'post_name' => $row['alias'],
'post_modified' => date("Y-m-d H:i:s", $row['date']),
'post_modified_gmt' => date("Y-m-d H:i:s", $row['date']),
'post_type' => $row['type'] === 'blog' ? 'post' : 'page',
'comment_count' => $row['comnum'],
'to_ping' => '',
'pinged' => '',
'post_content_filtered' => '',
)
);
if (is_int($doPost)) {
$i_arr[0]++;
}
$i_arr[1]++;
}
/* 评论 */
$comment_sql = "SELECT * FROM {$em_prefix}comment";
$comment_result = mysqli_query($em_conn, $comment_sql);
while ($row = mysqli_fetch_array($comment_result)) {
$doComment=$wpdb->insert(
$wp_prefix."comments",
array(
'comment_ID' => $row['cid'],
'comment_post_id' => $row['gid'],
'comment_author' => $row['poster'],
'comment_author_email' => $row['mail'],
'comment_author_url' => $row['url'],
'comment_author_IP' => $row['ip'],
'comment_date' => date("Y-m-d H:i:s", $row['date']),
'comment_date_gmt' => date("Y-m-d H:i:s", $row['date']),
'comment_content' => addslashes($row['comment']),
'comment_approved' => $row['hide'] === 'n' ? '1' : '0',
'comment_agent' => "",
'comment_type' => "",
'comment_parent' => $row['pid'],
'user_id' => 0,
)
);
if (is_int($doComment)) {
$i_arr[2]++;
} else {
//echo $do;
}
$i_arr[3]++;
}
/* 标签 */
$tag_sql = "SELECT * FROM {$em_prefix}tag";
$tag_result = mysqli_query($em_conn, $tag_sql);
while ($row = mysqli_fetch_array($tag_result)) {
//写标签到terms
$doTag1=$wpdb->insert(
$wp_prefix."terms",
array(
'term_id' => $row['tid'],
'name' => $row['tagname'],
'slug' => urlencode($row['tagname']),
'term_group' => 0,
)
);
if (is_int($doTag1)) {
$i_arr[4]++;
} else {
//echo $do;
}
$i_arr[5]++;
//写标签到term_taxonomy
$doTag2=$wpdb->insert(
$wp_prefix."term_taxonomy",
array(
'term_id' => $row['tid'],
'taxonomy' => "post_tag",
)
);
//写标签到term_relationships
$gid=explode(",", $row["gid"]);
array_pop($gid);
array_shift($gid);
for ($x=0;$x<count($gid);$x++) {
$doTag3=$wpdb->insert(
$wp_prefix."term_relationships",
array(
'object_id' => $gid[$x],
'term_taxonomy_id' => $wpdb->selectOne("SELECT term_taxonomy_id FROM `{$wp_prefix}term_taxonomy` WHERE term_id='{$row['tid']}'")
)
);
}
}
/* 分类 */
$sort_sql = "SELECT * FROM {$em_prefix}sort";
$sort_result = mysqli_query($em_conn, $sort_sql);
$sort_num=array();
while ($row = mysqli_fetch_array($sort_result)) {
//写分类到terms,返回insert_id
$doSort1=$wpdb->insert(
$wp_prefix."terms",
array(
'name' => addslashes($row['sortname']),
'slug' => urlencode($row['alias']),
'term_group' => 0,
)
);
if (is_int($doSort1)) {
$i_arr[6]++;
} else {
//echo $do;
}
$i_arr[7]++;
//写分类到term_taxonomy
$doSort2=$wpdb->insert(
$wp_prefix."term_taxonomy",
array(
'term_id' => $doSort1,
'taxonomy' => "category",
'description' => addslashes($row['description']),
'parent' => $row['pid'],
)
);
//暂存分类term_id及对应term_taxonomy_id
array_push($sort_num, $doSort2);
//写分类关系到term_relationships
$sort_relationship_sql = "SELECT gid,sortid FROM `{$em_prefix}blog` WHERE sortid='{$row['sid']}'";
$sort_relationship_result = mysqli_query($em_conn, $sort_relationship_sql);
while ($row = mysqli_fetch_array($sort_relationship_result)) {
$doSort3=$wpdb->insert(
$wp_prefix."term_relationships",
array(
'object_id' => $row["gid"],
'term_taxonomy_id' => $wpdb->selectOne("SELECT term_taxonomy_id FROM `{$wp_prefix}term_taxonomy` WHERE term_id='{$doSort1}'"),
)
);
}
}
/* 分类计数 */
for ($y=0;$y<count($sort_num);$y++) {
$tmpNum=$wpdb->selectOne("SELECT count(object_id) FROM `{$wp_prefix}term_relationships` WHERE term_taxonomy_id='{$sort_num[$y]}'");
$wpdb->executeCommand("UPDATE `{$wp_prefix}term_taxonomy` SET `count`={$tmpNum} WHERE term_taxonomy_id='{$sort_num[$y]}'");
}
/* 链接 */
$link_sql = "SELECT * FROM {$em_prefix}link";
$link_result = mysqli_query($em_conn, $link_sql);
while ($row = mysqli_fetch_array($link_result)) {
$doLink=$wpdb->insert(
$wp_prefix."links",
array(
'link_url' => $row['siteurl'],
'link_name' => addslashes($row['sitename']),
'link_target' => "_blank",
'link_visible' => $row['hide'] === 'n' ? 'Y' : 'N',
)
);
if (is_int($doLink)) {
$i_arr[8]++;
} else {
//echo $doLink;
}
$i_arr[9]++;
}
$info = "<p>文章原数据<span>".$i_arr[1]."</span>条,转换成功<span>".$i_arr[0]."</span>条</p>";
$info .= "<p>评论原数据<span>".$i_arr[3]."</span>条,转换成功<span>".$i_arr[2]."</span>条</p>";
$info .= "<p>标签原数据<span>".$i_arr[5]."</span>条,转换成功<span>".$i_arr[4]."</span>条</p>";
$info .= "<p>分类原数据<span>".$i_arr[7]."</span>条,转换成功<span>".$i_arr[6]."</span>条</p>";
$info .= "<p>链接原数据<span>".$i_arr[9]."</span>条,转换成功<span>".$i_arr[8]."</span>条</p>";
die($info);
} elseif (isset($_POST["host"]) and $_GET["act"]=="doFILE") {
$host = trim($_POST['host']);
$username = trim($_POST['username']);
$password = trim($_POST['password']);
$em_db = trim($_POST['em_db']);
$wp_db = trim($_POST['wp_db']);
$em_prefix = trim($_POST['em_prefix']);
$wp_prefix = trim($_POST['wp_prefix']);
// 连接WP数据库连接
$wpdb = new MYSQL($host, $username, $password, $wp_db);
$dir_wp="./wp-content/uploads";
$dir_em="./content/uploadfile";
$ok=0;
$fail=0;
$status_dir_em = is_dir($dir_em)==true ? 1 : 0;
$status_dir_wp1 = is_dir($dir_wp)==true ? 1 : 0;
$status_dir_wp2 = is_writable($dir_wp)==true ? 1 : 0;
if ($status_dir_em.$status_dir_wp1.$status_dir_wp2 != 111) {
die("<p><span>".$dir_wp."</span>或<span>".$dir_em."</span>目录不可写或不存在,请检查后再试</p>");
}
$filesnames = scandir($dir_em);
foreach ($filesnames as $name) {
if ((is_dir($dir_em."/".$name)) and ($name!=".") and ($name!="..")) {
$year=substr($name, 0, 4);
$month=substr($name, 4, 2);
mkdir2($dir_wp."/".$year."/".$month);
foreach (glob($dir_em."/".$name."/*") as $filepath) {
if (copy($filepath, $dir_wp."/".$year."/".$month."/".basename($filepath))) {
$ok++;
} else {
$fail++;
}
}
$wpdb->executeCommand("UPDATE wp_posts SET post_content = replace(post_content,'".substr($dir_em."/".$name, 1)."','".substr($dir_wp."/".$year."/".$month, 1)."')");
$wpdb->executeCommand("UPDATE wp_posts SET post_excerpt = replace(post_excerpt,'".substr($dir_em."/".$name, 1)."','".substr($dir_wp."/".$year."/".$month, 1)."')");
}
}
$info = "<p>复制文件成功:<span>".$ok."</span>个,失败<span>".$fail."</span>个</p>";
die($info);
} else {
?>
<!DOCTYPE html><html lang="zh-cn"><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Emlog to Wordpress</title><link href="//cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"><style>body{color:#666;font-size:14px;font-family:Microsoft YaHei,arial,Arial Narrow,HELVETICA;padding:20px}.wrap{width:600px;margin:auto}label{color:#74B200}.form-control{color:#000}.form-control:focus{border-color:#74B200;outline:0;box-shadow:inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(116,178,0,.5)}#info_box{color:#949494;font-size:9pt}#info_box p{margin:2px 0}#info_box span{color:#74B200;margin:0 5px}.footer{text-align:center}</style><!--[if lt IE 9]><script src="//cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script><script src="//cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script><![endif]--></head><body></body><script src="//cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script><script>var strVar="",strVar=strVar+'<div class="wrap">',strVar=strVar+'<div class="page-header">',strVar=strVar+"<h1>Emlog To Wordpress</h1>",strVar=strVar+"</div>",strVar=strVar+'<div class="alert alert-info alert-dismissible fade in" role="alert" id="alertFrame">',strVar=strVar+'<h3 style="margin-bottom: 20px;">\u8bf7\u4ed4\u7ec6\u9605\u8bfb\u4ee5\u4e0b\u8bf4\u660e\uff1a</h3>',strVar=strVar+"<p>1.\u672c\u7a0b\u5e8f\u4ec5\u5728Emlog 5.3.1 \u2192 Wordpress 4.7 \u4e4b\u95f4\u8fdb\u884c\u6d4b\u8bd5</p>",strVar=strVar+"<p>2.\u672c\u7a0b\u5e8f\u53ef\u65e0\u635f\u8f6c\u6362\uff1a\u65e5\u5fd7\u3001\u8bc4\u8bba\u3001\u5206\u7c7b\u3001\u6807\u7b7e\u3001\u94fe\u63a5</p>",strVar=strVar+'<p>3.\u672c\u7a0b\u5e8f<span style="margin:0 5px;font-weight:bold;">\u4e0d\u4f1a</span>\u7834\u574f\u539f\u6709Emlog\u6570\u636e\u5e93\u7ed3\u6784\u548c\u6570\u636e</p>',strVar=strVar+"<p>4.\u672c\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u9996\u5148\u4f1a\u6e05\u9664Wordpress\u6570\u636e\uff0c\u5305\u62ec\u6587\u7ae0\u3001\u5206\u7c7b\u7b49\uff0c\u8bf7\u6ce8\u610f\uff01</p>",strVar=strVar+"<p>5.\u672c\u7a0b\u5e8f\u9644\u4ef6\u8f6c\u6362\u53ea\u9488\u5bf9\u7b26\u5408Emlog\u50a8\u5b58\u8def\u5f84\u6807\u51c6\u7684\u9644\u4ef6</p>",strVar=strVar+"<p>6.\u672c\u7a0b\u5e8f\u4e0d\u63d0\u4f9b\u6280\u672f\u652f\u6301\uff0c\u6709\u80fd\u529b\u7684\u670b\u53cb\u8bf7\u81ea\u884c\u89e3\u51b3</p>",strVar=strVar+'<div class="form-group">',strVar=strVar+'<button type="button" class="btn btn-default" style="margin-top: 15px;" onclick="$(\'#alertFrame\').hide();$(\'#convertForm\').fadeIn();">\u6211\u5df2\u9605\u8bfb</button>',strVar=strVar+"</div>",strVar=strVar+"</div>",strVar=strVar+'<form role="form" id="convertForm" style="display:none;">',strVar=strVar+'<div class="form-group">',strVar=strVar+"<label>MySQL\u6570\u636e\u5e93\u5730\u5740</label>",strVar=strVar+'<input type="text" class="form-control" id="host" value="localhost">',strVar=strVar+"</div>",strVar=strVar+'<div class="form-group">',strVar=strVar+"<label>MySQL\u6570\u636e\u5e93\u7528\u6237\u540d</label>",strVar=strVar+'<input type="text" class="form-control" id="username" value="">',strVar=strVar+"</div>",strVar=strVar+'<div class="form-group">',strVar=strVar+"<label>MySQL\u6570\u636e\u5e93\u5bc6\u7801</label>",strVar=strVar+'<input type="text" class="form-control" id="password" value="">',strVar=strVar+"</div>",strVar=strVar+'<div class="form-group">',strVar=strVar+"<label>Emlog\u6570\u636e\u5e93\u540d\u79f0</label>",strVar=strVar+'<input type="text" class="form-control" id="em_db" value="">',strVar=strVar+"</div>",strVar=strVar+'<div class="form-group">',strVar=strVar+"<label>Wordpress\u6570\u636e\u5e93\u540d\u79f0</label>",strVar=strVar+'<input type="text" class="form-control" id="wp_db" value="">',strVar=strVar+"</div>",strVar=strVar+'<div class="form-group">',strVar=strVar+"<label>Emlog\u8868\u5355\u524d\u7f00</label>",strVar=strVar+'<input type="text" class="form-control" id="em_prefix" value="emlog_">',strVar=strVar+"</div>",strVar=strVar+'<div class="form-group">',strVar=strVar+"<label>Wordpress\u8868\u5355\u524d\u7f00</label>",strVar=strVar+'<input type="text" class="form-control" id="wp_prefix" value="wp_">',strVar=strVar+"</div>",strVar=strVar+'<div class="form-group">',strVar=strVar+"<label>\u6570\u636e\u5e93 or \u6587\u4ef6</label><br>",strVar=strVar+'<input type="radio" name="fileORdb" value="<?php echo substr($_SERVER['PHP_SELF'], strrpos($_SERVER['PHP_SELF'], '/')+1); ?>?act=doDB" checked> \u6570\u636e\u5e93',strVar=strVar+'<input type="radio" onclick="alertFileTips()" name="fileORdb" value="<?php echo substr($_SERVER['PHP_SELF'], strrpos($_SERVER['PHP_SELF'], '/')+1); ?>?act=doFILE"> \u6587\u4ef6',strVar=strVar+"</div>",strVar=strVar+'<div class="form-group">',strVar=strVar+'<div id="info_box">',strVar=strVar+"</div>",strVar=strVar+"</div>",strVar=strVar+'<div id="info_box">',strVar=strVar+"</div>",strVar=strVar+'<input type="button" class="btn btn-default" id="submit" onclick="convert()" value="Let\'s \u8f6c\uff01">',strVar=strVar+"</form>",strVar=strVar+"<hr>",strVar=strVar+'<div class="footer">',strVar=strVar+"<p>\u7248\u6743\u6ca1\u6709\uff0c\u76d7\u7248\u4e0d\u7ea0</p>",strVar=strVar+'<p style="font-size: 12px;color: #B0B0B0;">i@wyt.me</p>',strVar=strVar+"</div>",strVar=strVar+"</div>";$("body").html(strVar);function convert(){$("#info_box").html("");var a=$('input[name="fileORdb"]:checked').val(),b=$("#host").val(),c=$("#username").val(),d=$("#password").val(),e=$("#em_db").val(),f=$("#wp_db").val(),g=$("#em_prefix").val(),h=$("#wp_prefix").val();if(""==b||""==c||""==d||""==e||""==f||""==g||""==h)return $("#info_box").html("<span>\u4fe1\u606f\u8981\u586b\u5b8c\u6574\u54e6~</span>"),!1;$("#submit").val("\u6b63\u5728\u8f6c\u6362");$("#info_box").html("<p>\u8f6c\u6362\u65f6\u957f\u6839\u636e\u4e3b\u673a\u6027\u80fd\u3001\u6570\u636e\u91cf\u5927\u5c0f\u800c\u5b9a\uff0c\u8bf7\u8010\u5fc3\u7b49\u5f85\u3002\u6b63\u5e38\u60c5\u51b5\u4e0b 2s ~ 3min\uff0c\u8bf7\u52ff\u5173\u95ed\u6b64\u9875\u9762</p>");timeStart=0;timer=setInterval("timePlus()","1000");$.post(a,{host:b,username:c,password:d,em_db:e,wp_db:f,em_prefix:g,wp_prefix:h},convert_callback)}function timePlus(){$("#submit").val("\u6b63\u5728\u8f6c\u6362\uff08\u5df2\u7528\u65f6\uff1a"+timeStart+"\u79d2\uff09");if(60==timeStart){var a="<p>\u5df2\u7ecf\u4e00\u5206\u949f\u4e86\uff1f\u522b\u6025\uff0c\u518d\u7b49\u4e00\u5206\u949f~</p>";$("#info_box").html(a)}else 120==timeStart?(a="<p>\u6211\u64e6\uff0c\u6709\u70b9\u6162\u554a\u2026\u2026\u6570\u636e\u91cf\u592a\u5927\uff1f\u8fd8\u662f\u60a8\u7684\u865a\u62df\u4e3b\u673a\u4e0d\u7ed9\u529b\uff1f</p>",$("#info_box").html(a)):240==timeStart?(a="<p>\u8fd9\u4e2a\u65f6\u5019\u53ef\u80fd\u60a8\u9700\u8981\u624b\u52a8\u68c0\u67e5\u4e00\u4e0b\u8f6c\u6362\u662f\u5426\u6210\u529f\u2026\u2026</p>",$("#info_box").html(a)):300==timeStart&&(a="<p>\u53ef\u80fd\u7a0b\u5e8f\u5361\u4f4f\u4e86\uff0c\u8bf7\u5237\u65b0\u9875\u9762\uff0c\u518d\u6765\u4e00\u6b21\uff0c\u653e\u5fc3\uff0cEmlog\u6570\u636e\u6ca1\u635f\u574f</p>",$("#info_box").html(a));timeStart++}function convert_callback(a){clearInterval(timer);$("#info_box").html(a);$("#submit").val("Let's \u8f6c\uff01");$("#submit").attr("disabled",!1)}function alertFileTips(){alert("\u9644\u4ef6\u8f6c\u6362\u529f\u80fd\u53ea\u662f\u6d4b\u8bd5\uff0c\u4e0d\u4fdd\u8bc1\u5b8c\u7f8e\u8f6c\u6362\uff0c\u5177\u4f53\u8bf7\u81ea\u6d4b")}console.log("%c\u4f60\u7785\u5565\uff01"," text-shadow: 0 1px 0 #ccc,0 2px 0 #c9c9c9,0 3px 0 #bbb,0 4px 0 #b9b9b9,0 5px 0 #aaa,0 6px 1px rgba(0,0,0,.1),0 0 5px rgba(0,0,0,.1),0 1px 3px rgba(0,0,0,.3),0 3px 5px rgba(0,0,0,.2),0 5px 10px rgba(0,0,0,.25),0 10px 10px rgba(0,0,0,.2),0 20px 20px rgba(0,0,0,.15);font-size:5em;font-family: Microsoft YaHei;"); </script></html>
<?php
}
function mkdir2($dir)
{
if (is_dir($dir) || @mkdir($dir, 0777)) {
return true;
} else {
$dirArr=explode('/', $dir);
array_pop($dirArr);
$newDir=implode('/', $dirArr);
mkdir2($newDir);
if (@mkdir($dir, 0777)) {
return true;
} else {
return false;
}
}
}
class MYSQL
{
private $conn;
public function __construct($host, $username, $password, $dbname, $charset='')
{
$conn = mysqli_connect($host, $username, $password);
if (!$conn) {
echo '连接失败,请检查用户名密码';
exit;
}
$this->conn = $conn;
$res = mysqli_select_db($conn, $dbname);
if (!$res) {
echo '连接失败,请检查数据库是否正确';
exit;
}
mysqli_set_charset($conn, $charset);
}
public function __destruct()
{
mysqli_close($this->conn);
}
public function insert($table, $data)
{
$str = '';
$str .="INSERT INTO `$table` ";
$str .="(`".implode("`,`", array_keys($data))."`) ";
$str .=" VALUES ";
$str .= "('".implode("','", $data)."')";
$res = mysqli_query($this->conn, $str);
if ($res) {
return mysqli_insert_id($this->conn);
} else {
echo("错误描述: " . mysqli_error($this->conn));
return false;
}
}
public function selectOne($sql)
{
$result = mysqli_query($this->conn, $sql);
$data = array();
if ($result && mysqli_num_rows($result) > 0) {
$data = mysqli_fetch_array($result);
}
return $data[0];
}
public function executeCommand($command)
{
$sql = $command;
$res = mysqli_query($this->conn, $sql);
return $res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.