- A+
所属分类:技术
WordPress转修罗XiunoBBS4.0程序的工具教程
这里我给大家分享一下wp转xiuno程序教程。包含用户表(做了一些原来QQ用户登录插件的过滤),文章表,回复表,基本上主要的内容全部转过来了。包含代码高亮也转成了xiunobbs的高亮格式。
本工具的特点在于,文章的ID和原来WP的ID是一样的,不会变化。比如原来文章www.zidiu.com/123.html变成www.zidiu/thread-123.htm,只用在转发规则到下手脚,就直接跳转新的地址上来,很好的301跳转,不会影响百度蜘蛛收录而吃闭门羹。
用前须知
转换前,请在本机环境测试和做好备份,可能你还需要自己做一些小的改动。能看懂PHP的先过一下代码,啥都不懂的,需谨慎(原wp数据不会受到任何影响,新站文章表会被清空)。
- <?php
- header("Content-Type: text/html; charset=UTF-8");
- // 本程序用来升级 wordpress 到 XiunoBlog
- if(empty($_GET['next'])) {
- echo '
- <p style="color:red;">本程序仅对XIUNO BBS4.0有效,从worpdress将用户,文章,栏目转移到BBS中!</p>
- <p style="color:red;">升级有风险,请谨慎升级!</p>
- <p style="color:red;">v1.0.2</p>
- <p style="color:#1d953f;"><a href="./wp2bbs.php?next=1">我有备份,我要升级!</a></p>
- ';
- } else {
- @set_time_limit(0);
- define('DEBUG', 0);
- define('BBS_PATH', './');
- // 加载应用的配置文件,唯一的全局变量 $conf
- if(!($conf = include BBS_PATH.'conf/conf.php')) {
- message('配置文件不存在,请先安装 Xiuno BBS。');
- }
- define('FRAMEWORK_PATH', BBS_PATH.'core/');
- define('FRAMEWORK_TMP_PATH', $conf['tmp_path']);
- define('FRAMEWORK_LOG_PATH', $conf['log_path']);
- include 'db/db.func.php';
- include 'db/db_pdo_mysql.class.php';
- // xn2 db instance
- function get_db() {
- if(isset($_SERVER['db']))
- return $_SERVER['db'];
- $conf = include BBS_PATH.'conf/conf.php';
- $db = new db_pdo_mysql($conf['db'][$conf['db']['type']]);
- $_SERVER['db'] = $db;
- return $db;
- }
- // wp db instance wp配置文件
- function get_db_wp($pwd) {
- global $wp;
- if(!empty($wp))return;
- $wp = new db_pdo_mysql(array(
- 'master' => array (
- 'host' => '127.0.0.1',
- 'user' => 'root',
- 'password' => $pwd,//你的数据库密码
- 'name' => 'wordpress',
- 'charset' => 'utf8', // 要求取出 utf-8 数据 mysql 4.1 以后支持转码
- //'charset' => $_config['db'][1]['dbcharset'],
- 'tablepre' => 'wp_',
- 'engine'=>'MyISAM',
- ),
- 'slaves' => array ()
- ));
- }
- // 升级完后 跳转
- function next_step() {
- echo "<p>升级完成,<a href='../../'>查看</a></p>";
- }
- function debug_info($str){
- echo "$str<br/>";
- }
- function array_to_object($arr) {
- if (gettype($arr) != 'array') {
- return $arr;//返回自己
- }
- foreach ($arr as $k => $v) {
- if (gettype($v) == 'array' || getType($v) == 'object') {
- $arr[$k] = (object)array_to_object($v);
- }
- }
- return (object)$arr;
- }
- function wp_query($sql){
- global $wp;
- $db = $wp;
- $rs = $db->query($sql);
- $result = array();
- foreach($rs as $val){
- $val = array_to_object($val);
- array_push($result,$val);
- }
- if(count($result)>0)
- return (object)$result;//
- return false;
- }
- // 同步用户
- function upgrade_user() {
- $cache = array();
- // wp 用户 过滤open social插件生成的QQ,CSDN,SINA用户
- $users = wp_query('SELECT * FROM wp_users WHERE user_email NOT LIKE "SINA%" AND user_email NOT LIKE "CSDN%" AND user_email NOT LIKE "QQ%" AND user_login NOT LIKE "%@%"');
- if(!$users)return;
- foreach($users as $key => $val) {
- //ca66a9ea70de87e76385ac7bfa240bf2 明文密码为123456
- if(in_array($val->user_email,$cache)) continue;
- db_insert('user',array('username'=>$val->user_login,'gid'=>0,'password'=>'ca66a9ea70de87e76385ac7bfa240bf2',
- 'salt'=>'qqxre5sgve9n7n99','create_date'=>strtotime($val->user_registered),'email'=>$val->user_email));
- array_push($cache,$val->user_email);
- }
- }
- /*
- #SELECT * FROM wp_terms WHERE term_id IN(SELECT term_id FROM wp_term_taxonomy WHERE taxonomy="category");
- #查询相对分类下面的文章
- #SELECT object_id,term_taxonomy_id FROM wp_term_relationships WHERE object_id IN(SELECT ID FROM wp_posts WHERE post_status="publish" AND post_type="post");
- */
- // 板块 分类作为板块 todo:标签做为主题分类 20条
- function upgrade_forum() {
- global $wp;
- //查询所有的分类ID语句
- $cache = array();//存放已经写入的分类
- //插入对应的分类及分类ID
- $posts = wp_query('SELECT * FROM wp_posts WHERE post_status="publish" AND post_type="post"');
- foreach($posts as $key=>$val){
- //查询所属的分类
- $ships = wp_query('SELECT * FROM wp_term_relationships WHERE object_id='.$val->ID);
- foreach($ships as $skey=>$sval){
- //查询分类标签
- $taxonomy = wp_query('SELECT * FROM wp_term_taxonomy WHERE parent!=0 AND term_taxonomy_id='.$sval->term_taxonomy_id);
- if(!$taxonomy)continue;//理论只会有一个对应的分类ID
- foreach($taxonomy as $tkey=>$tval){
- if(in_array($tval->term_id,$cache)) continue;
- $category = (array)wp_query('SELECT * FROM wp_terms WHERE term_id='.$tval->term_id);
- if($tval->taxonomy == 'category'){
- $category = array_pop($category);
- db_insert('forum',array('fid'=>$category->term_id,'name'=>$category->name,'brief'=>$category->name.'介绍'));;
- array_push($cache,$category->term_id);
- }
- }
- }
- }
- }
- // 文章导入
- function upgrade_thread() {
- //$sql = 'SELECT * FROM wp_term_relationships WHERE object_id IN(SELECT ID FROM wp_posts WHERE post_status="publish" AND post_type="post") AND term_taxonomy_id IN(SELECT term_id FROM wp_terms WHERE term_id IN(SELECT term_id FROM wp_term_taxonomy WHERE taxonomy="category"))';
- global $wp;
- //查询文章列表
- $posts = wp_query('SELECT * FROM wp_posts WHERE post_status="publish" AND post_type="post"');
- foreach($posts as $key=>$val){
- //查询所属的分类
- $ships = wp_query('SELECT * FROM wp_term_relationships WHERE object_id='.$val->ID);
- foreach($ships as $skey=>$sval){
- $fid = 1015;//默认放到网络文摘
- $post_views_count = rand(100,200);
- //查询分类标签
- $taxonomy = wp_query('SELECT * FROM wp_term_taxonomy WHERE parent!=0 AND term_taxonomy_id='.$sval->term_taxonomy_id);
- if($taxonomy){
- $taxonomy = (array)$taxonomy;
- $taxonomy = (object)$taxonomy[0];
- if($taxonomy->taxonomy == 'category'){
- $post_views_count = wp_query("SELECT meta_value FROM wp_postmeta WHERE post_id=$val->ID AND meta_key='post_views_count' LIMIT 1;");
- if($post_views_count){
- $views = (array)$post_views_count;
- $nCount = $views[0]->meta_value;
- if($nCount>$taxonomy->count)
- $post_views_count = $nCount;
- else
- $post_views_count = $taxonomy->count;
- }else{
- $post_views_count = $taxonomy->count;
- }
- $fid = $taxonomy->term_id;
- }
- }
- $thread = array(
- 'fid'=>$fid,
- 'tid'=>$val->ID,
- 'uid'=>1,//admin用户的ID
- 'subject'=>$val->post_title,
- 'create_date'=>strtotime($val->post_date),
- 'last_date'=>strtotime($val->post_date),
- 'views'=>$post_views_count,'posts'=>0,'top'=>0,
- 'firstpid'=>$val->ID,
- 'lastuid'=>1,
- 'lastpid'=>$val->ID,
- 'userip'=>2130706433,
- );
- db_insert('thread',$thread);
- $post = array (
- //'pid'=>$pid,
- 'tid'=>$val->ID,
- 'isfirst'=>1,
- 'uid'=>1,//admin用户的ID
- 'create_date'=>strtotime($val->post_date),
- 'userip'=>2130706433,
- 'message'=>'',
- 'message_fmt'=>''
- );
- //代码高亮部分的\r\n\r\n去掉
- $temp = replaceEnter($val->post_content,'cpp');
- $temp = replaceEnter($temp,'java');
- $temp = replaceEnter($temp,'php');
- $temp = replaceEnter($temp,'objc');
- $temp = replaceEnter($temp,'html');
- $temp = replaceEnter($temp,'xml');
- $temp = replaceEnter($temp,'js');
- $temp = auto_img_add_p($temp);
- $temp = str_replace("wp-content/uploads","upload/wp",$temp);//图片,附件
- $post['message'] = $temp;
- $post['message_fmt'] = $temp;
- $pid = db_insert('post',$post);//插入的ID
- db_insert('mythread',array('uid'=>1,'tid'=>$val->ID));
- db_exec("UPDATE bbs_forum SET threads=threads+1 WHERE fid=$fid LIMIT 1;");
- db_exec("UPDATE bbs_thread SET firstpid=$pid,lastpid=$pid WHERE tid=$val->ID LIMIT 1;");
- db_exec("UPDATE bbs_user SET threads=threads+1 WHERE uid=1 LIMIT 1;");
- break;
- }
- }
- }
- function upgrade_comments() {
- global $wp;
- //查询文章列表
- $comments = wp_query('SELECT * FROM wp_comments');
- $users = db_sql_find('SELECT * FROM bbs_user');
- $nMax = count($users)-1;
- $nMin = 2;
- foreach($comments as $key=>$val){
- $temp = replaceEnter($val->comment_content,'cpp');
- $temp = replaceEnter($temp,'java');
- $temp = replaceEnter($temp,'php');
- $temp = replaceEnter($temp,'objc');
- $temp = replaceEnter($temp,'html');
- $temp = replaceEnter($temp,'xml');
- $temp = replaceEnter($temp,'js');
- $temp = auto_img_add_p($temp);
- $post = array (
- 'tid'=>$val->comment_post_ID,
- 'uid'=>$users[rand($nMin,$nMax)]['uid'],//admin用户的ID
- 'create_date'=>strtotime($val->comment_date),
- 'userip'=>2130706433,
- 'message'=>$temp,
- 'message_fmt'=>$temp
- );
- db_insert('post',$post);
- db_exec("UPDATE bbs_thread SET posts=posts+1 WHERE tid=$val->comment_post_ID LIMIT 1;");
- }
- }
- //所有的图片都自动加上<p>段落功能
- function auto_img_add_p($content){
- $pregRule = "/<[img|IMG].*?src=[\'|"](.*?(?:[\.jpg|\.jpeg|\.png|\.gif|\.bmp]))[\'|"].*?[\/]?>/";
- preg_match_all($pregRule, $content,$arr);
- for($i=0,$j=count($arr[0]);$i<$j;$i++){
- $content = str_replace($arr[0][$i],'<p>'.$arr[0][$i].'</p>',$content);
- }
- return $content;
- }
- function replaceEnter($src,$tag){
- preg_match_all("/\[$tag\]([\s\S]*?)\[\/$tag\]/",$src,$mat);
- for($i=0,$j=count($mat[0]);$i<$j;$i++){
- $code = $mat[0][$i];//匹配的标签内容
- $code = str_replace("\r\n\r\n","\n",$code);
- $code = str_replace("\r\n","\n",$code);
- $code = str_replace("<br/>","\n",$code);//html转成\n
- //代码高亮标签
- $code = str_replace("[/$tag]",'</pre>',$code);
- $code = str_replace("[$tag]",'<pre class="brush:cpp;toolbar:false">',$code);
- $src = str_replace($mat[0][$i],$code,$src);
- }
- $src = str_replace("\r\n\r\n","<br/>",$src);
- //$src = str_replace("\r\n","<br/>",$src);
- return $src;
- }
- function truncate_data(){
- db_exec('TRUNCATE bbs_forum');
- db_exec('TRUNCATE bbs_mythread');
- db_exec('TRUNCATE bbs_post');
- db_exec('TRUNCATE bbs_thread');
- debug_info('bbs_forum,bbs_mythread,bbs_post,bbs_thread数据清空完成');
- }
- if(!isset($_GET['pwd'])){
- echo '没有密码';
- return;
- }
- get_db_wp($_GET['pwd']);//初始化连接WORDPRESS数据库
- get_db();//初始化数据库连接
- truncate_data();//如果已经有数据,这项可以注释
- upgrade_user();//没啥鸟用,直接用QQ插件登录
- upgrade_forum();
- upgrade_thread();
- upgrade_comments();//只能放最后,因为留言是放在文章表中ID必须要独占一个ID
- next_step();
- }
- ?>
复制代码
下面是db.php其它几个文件bbs自带的有,这里就不贴出来了。
上传到网站根目录,然后http://127.0.0.1/wp2bbs.php?next=1&pwd=你的数据库密码。
再补充一点,WP的文章分类,我把父节点没转过来。因为我不需要。如果有需要的朋友,可以把sql语句改一下。'SELECT * FROM wp_term_taxonomy WHERE parent!=0 AND 这里面的prent!=0去掉就行了。
最后后台更新缓存,大功告成!
- 我的微信
- 这是我的微信扫一扫
- 我的微信公众号
- 我的微信公众号扫一扫