<?php
error_reporting(0);
define('SABLOG_ROOT', true);
@header("content-Type: text/html; charset=UTF-8");
ob_start();
// 允许程序在 register_globals = off 的环境下工作
$onoff = function_exists('ini_get') ? ini_get('register_globals') : get_cfg_var('register_globals');
if ($onoff != 1) {
@extract($_POST, EXTR_SKIP);
@extract($_GET, EXTR_SKIP);
}
// 去除转义字符
function stripslashes_array(&$array) {
if (is_array($array)) {
foreach ($array as $k => $v) {
$array[$k] = stripslashes_array($v);
}
} else if (is_string($array)) {
$array = stripslashes($array);
}
return $array;
}
@set_magic_quotes_runtime(0);
// 判断 magic_quotes_gpc 状态
if (@get_magic_quotes_gpc()) {
$_GET = stripslashes_array($_GET);
$_POST = stripslashes_array($_POST);
}
$step = $_GET['step'] ? $_GET['step'] : $_POST['step'];
$php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
$action = $_GET['action'] ? $_GET['action'] : $_POST['action'];
$configfile = '../config.php';
$sqlfile = 'install.sql';
if(!is_readable($sqlfile)) {
exit('数据库文件不存在或者读取失败');
}
$fp = fopen($sqlfile, 'rb');
$sql = fread($fp, 2048000);
fclose($fp);
if ($step) {
include($configfile);
include('../include/func_db_mysql.php');
$DB = new DB_MySQL;
$DB->connect($servername, $dbusername, $dbpassword, $dbname, $usepconnect);
unset($servername, $dbusername, $dbpassword, $usepconnect);
}
$wp_db_prefix = addslashes($_GET['wp_db_prefix'] ? $_GET['wp_db_prefix'] : $_POST['wp_db_prefix']);
$posturl = $wp_db_prefix ? 'wp_db_prefix='.$wp_db_prefix : '';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>WordPress to SaBlog-X 转换脚本</title>
<style type="text/css">
body {
margin: 20px;
line-height: 140%;
color: #000000;
font: 14px Georgia, Verdana, Tahoma;
background-color: #cdd6dd;
text-align: center;
}
a {
color: #333399;
text-decoration: none;
}
a:hover {
color: #CC0000;
}
td {
font: 14px Georgia, Verdana, Tahoma;
line-height: 160%;
color: #000000;
}
div {
font: 14px Georgia, Verdana, Tahoma;
line-height: 160%;
color: #000000;
}
#main {
background-color: #fff;
text-align: left;
padding: 20px;
width: 600px;
border: 1px solid #ccc;
margin-bottom: 20px;
}
.title {
font-size: 18px;
font-weight: bold;
}
form {
margin: 0px;
padding: 0px;
}
.formfield {
font: 14px Georgia, Verdana, Tahoma;
font-weight: bold;
background-color: #fff;
padding: 3px;
border: 1px solid #BFBFBF;
margin-right: 10px;
}
.formbutton {
font: 14px Georgia, Verdana, Tahoma;
font-weight: bold;
padding: 3px;
}
</style>
</head>
<body>
<div id="main">
<form method="post" action="<?php echo $php_self.'?'.$posturl;?>">
<p class="title">WordPress to SaBlog-X 转换向导</p>
<hr noshade="noshade" />
<?php
if (!$step || $step == '1') {
?>
<p class="title">第一步:转换须知</p>
<ol>
<li>本程序只用于 <b>WordPress 2.x.x</b> 转换到 <b>SaBlog-X 1.6</b></li>
<li>本程序只转换分类、文章、评论、用户、友情链接、站点统计数据。其他部分数据可以通过重建数据生成.</li>
<li>转换文章的时候,所属分类只选择WordPress的第一个分类作为SaBlog-X的所属分类.</li>
<li>转换不会对 WordPress 的数据进行任何操作!所以可以不备份 WordPress 的数据.</li>
<li>转换过程也可以看作是全新安装 SaBlog-X 1.6. 因此可以不用事先安装 SaBlog-X 1.6.</li>
<li>转换完毕后一定删除本脚本!</li>
<li>转换过程中千万不能关闭浏览器!</li>
<li>为简化步骤,不做过多的判断!因此请尽量按照程序的要求操作.</li>
<li>没有遵照步骤提示操作造成的一切后果自己承担,本程序作者不会负任何责任!</li>
</ol>
<p> </p>
<p>请输入WordPress的数据表前缀: <input type="text" name="wp_db_prefix" value="wp_" /></p>
<p> </p>
<p>点击下一步就开始升级操作!</p>
<hr noshade="noshade" />
<p align="right">
<input type="hidden" name="step" value="2" />
<input class="formbutton" type="submit" value="下一步" />
</p>
<?php
} elseif ($step == '2') {
?>
<p class="title">第二步:建立数据表</p>
<p>本步骤建立 SaBlog-X V1.6 的数据表</p>
<p>
<?php
runquery($sql);
?>
</p>
<p> </p>
<p>点击下一步就开始把 WordPress 的数据分步骤转移到刚才建立的 Sablog-X 数据表中.</p>
<hr noshade="noshade" />
<p align="right">
<input type="hidden" name="step" value="3" />
<input class="formbutton" type="submit" value="下一步" />
</p>
<?php
} elseif ($step == '3') {
?>
<p class="title">第三步:转移数据</p>
<ol>
<li>用户数据</li>
<li>分类数据</li>
<li>文章数据</li>
</ol>
<p>
<?php
///////////
$query = $DB->query("SELECT ID,user_login,user_pass,user_email,user_url,user_registered FROM {$wp_db_prefix}users");
while ($wpuser = $DB->fetch_array($query)) {
$wpuserext = $DB->fetch_one_array("SELECT meta_value FROM {$wp_db_prefix}usermeta WHERE user_id='".$wpuser['ID']."' AND meta_key='{$wp_db_prefix}user_level'");
if ($wpuserext['meta_value'] == 10) {
$SaxGroupID = 1;
} else {
$SaxGroupID = 3;
}
if ($wpuser['url']) {
$SaxUserUrl = addslashes($wpuser['user_url']);
} else {
$SaxUserUrl = addslashes($wpuser['user_email']);
}
$SaxRegDateline = strtotime($wpuser['user_registered']);
$SaxUserID = intval($wpuser['ID']);
$SaxUserName = addslashes($wpuser['user_login']);
$SaxPassWord = addslashes($wpuser['user_pass']);
$DB->query("INSERT INTO {$db_prefix}users (userid,username,password,url,regdateline,groupid) VALUES ('$SaxUserID','$SaxUserName','$SaxPassWord','$SaxUserUrl','$SaxRegDateline','$SaxGroupID')");
}
echo '用户转换完毕<br />';
//////////////
$query = $DB->query("SELECT cat_ID,cat_name,category_count FROM {$wp_db_prefix}categories");
while ($wpcate = $DB->fetch_array($query)) {
$SaxCateID = intval($wpcate['cat_ID']);
$SaxCateArticles = intval($wpcate['category_count']);
$SaxCateName = addslashes($wpcate['cat_name']);
$DB->query("INSERT INTO {$db_prefix}categories (cid,name,articles) VALUES ('$SaxCateID','$SaxCateName','$SaxCateArticles')");
}
echo '分类转换完毕<br />';
//////////////
$query = $DB->query("SELECT ID,post_author,post_date,post_content,post_title,post_category,post_excerpt,post_status,comment_status,ping_status,post_password,comment_count FROM {$wp_db_prefix}posts WHERE post_type='post'");
while ($wppost = $DB->fetch_array($query)) {
$SaxArticleID = intval($wppost['ID']);
// 评论属性
if ($wppost['comment_status'] == 'open') {
$SaxCloseComment = 0;
} else {
$SaxCloseComment = 1;
}
// 引用属性
if ($wppost['ping_status'] == 'open') {
$SaxCloseTrackback = 0;
} else {
$SaxCloseTrackback = 1;
}
// 文章状态
if ($wppost['post_status'] == 'publish') {
$SaxVisible = 1;
} else {
$SaxVisible = 0;
}
// 内容和描述
if ($wppost['post_excerpt']) {
$SaxDesc = $wppost['post_excerpt'];
} elseif (strpos($wppost['post_content'], '<!--more-->')) {
@list($SaxDesc) = @explode('<!--more-->', $wppost['post_content']);
} else {
$SaxDesc = '';
}
$SaxDesc = addslashes($SaxDesc);
$SaxTitle = addslashes($wppost['post_title']);
$SaxContent = addslashes(str_replace('<!--more-->','',$wppost['post_content']));
$SaxUID = intval($wppost['post_author']);
$SaxDateline = strtotime($wppost['post_date']);
$SaxComments = strtotime($wppost['comment_count']);
$SaxReadPassw