注册 登录
自由的生活_软路由 返回首页

心想事成的个人空间 https://bbs.routerclub.com/?681 [收藏] [复制] [分享] [RSS]

日志

把discuz 3.4 论坛从gbk转换为为utf-8工具

已有 1932 次阅读2018-3-25 10:21

因为discuz 使用了序列化存储,如果不经过转换,会有些设置丢失。
目前网上有个脚本,但不能工作在php 7.0下,我稍作修改,可以正常工作,特分享于下。

<?php
/**
* 序列化整理脚本,适合uchome2.0、discuz! X2.0、discuz! X3.1,数据库转码后在脚本运行之前请不要运行网站程序(因为网站程序会改写序列化数据),建议此脚本在数据库转换完成后立即执行。
* 此脚本放置与网站根目录,如果放置其他目录请注意修改脚本中config配置文件路径。
**/
//error_reporting(E_ALL);
$step = isset($_GET['step']) ? $_GET['step'] : 'start';
if($step == 'start') {
        $msg = "<a href='serialize.php?step=convert&type=x2'>Discuz! X2 序列化整理</a>";
        $msg .= "<br/><a href='serialize.php\?step=convert&type=x31'>Discuz! X3.1 序列化整理</a>";
        $msg .= "<br/><a href='serialize.php?step=convert&type=uch2'>UCHOME 2.0 序列化整理</a>";
        show_msg($msg);
        
} elseif($step == 'convert') {
        $type = $_GET['type'];
        
        if($type == 'x2' || $type == 'x31') {
                // require './config/config_global.php';                                // config配置文件路径请确保能连接到目标数据库
                $dbserver   = '127.0.0.1';
                $dbusername = 'root';
                $dbpassword = 'aaaa';
                $database   = 'test';
                $dbcharset  = 'utf8';
        } elseif ($type == 'uch2') {
                // require '.config.php';
                $dbserver   = '127.0.0.1';
                $dbusername = 'root';
                $dbpassword = 'aaaa';
                $database   = 'test';
                $dbcharset  = 'gbk';
        }
        
        if($dbcharset == 'gbk')
            $tocharset = 'utf8';
        else
            $tocharset = 'gbk';
        
        $limit = 100;
        $nextid = 0;
        
        
        $start = !empty($_GET['start']) ? $_GET['start'] : 0;
        $tid = !empty($_GET['tid']) ? $_GET['tid'] : 0;
        $arr = getlistarray($type);
        
        $field = $arr[intval($tid)];
        $stable = $field[0];
        $sfield = $field[1];
        $sid        = $field[2];
        $special = $field[3];
        
        // $mysql_conn = @mysql_connect("$dbserver", "$dbusername", "$dbpassword") or die("Mysql connect is error.");
    // mysql_select_db($database, $mysql_conn);
    // mysql_query('set names '.$dbcharset);
$db=mysqli_connect("localhost",$dbusername,$database,$database ); 
// $db = new mysqli(dbserver,'root',dbpassword,$database);

//判断数据库服务器是否正确连接,如果没有连接上,die掉并输出提示信息和连接错误码
if (mysqli_connect_errno($db)){
die('无法连接数据库服务器'). mysqli_connect_error();
}
$db->set_charset($dbcharset); //设置查询结果编码
    if($special) {
                $sql = "SELECT $sfield, $sid FROM $stable WHERE $sid > $start ORDER BY $sid ASC LIMIT $limit";
        } else {
                $sql = "SELECT $sfield, $sid FROM $stable";
        }
    
    // $query = mysql_query($sql);
// syslog($priority,$sql);
    $result = $db->query($sql); //得到查询结果
        // while($values = mysql_fetch_array($query)) {
   while($values = $result->fetch_array(MYSQLI_ASSOC)) {
                if($special)
                        $nextid = $values[$sid];
                else
                        $nextid = 0;
                $data = $values[$sfield];
                $id   = $values[$sid];
                $data = preg_replace_callback('/s:([0-9]+?):"([\s\S]*?)";/','_serialize',$data);
                $data1 = addslashes($data);
                // mysql_query("UPDATE `$stable` SET `$sfield` = '$data' WHERE `$sid` = '$id'", $mysql_conn);
$db->query("UPDATE `$stable` SET `$sfield` = '$data1' WHERE `$sid` = '$id'"); 
// if ($data != data1) syslog($priority,$data);
// syslog($priority,$data1);
        }
        if($nextid)
        {
                show_msg($stable." $sid > $nextid", "serialize.php?step=convert&type=$type&tid=$tid&start=$nextid");
        }
        else
        {        
                $tid++;
                if($tid < count($arr))
                        show_msg($stable." $sid > $nextid", "serialize.php?step=convert&type=$type&tid=$tid&start=0");
                else
                        show_msg('转换结束', "serialize.php?step=end");
        
        }
        //mysql_close($mysql_conn);
$db->close();
} elseif( $step == 'end') {
        show_msg('整理结束');
}
function _serialize($str) {
        $l = strlen($str[2]);
        return 's:'.$l.':"'.$str[2].'";';
}
function show_msg($message, $url_forward='', $time = 10, $noexit = 0) {
        if(!empty($url_forward)) {
                $message = "<a href=\"$url_forward\">$message (跳转中...)</a><script>setTimeout(\"window.location.href ='$url_forward';\", $time);</script>";
        }
        print<<<END
        <!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>序列化整理工具</title>
        <body>
        <table>
        <tr><td>$message</td></tr>
        </table>
        </body>
        </html>
END;
}
//以下为需要整理的数据库字段。discuz! x3.1数据库中包含ucenter数据整理,如果你使用的是独立ucenter请在ucenter里(根据自身情况修改表名)再运行一次。
function getlistarray($type) {
        if($type == 'uch2') {
                $list = array(
                        // array('uchome_data', 'datavalue', 'var', FALSE),
                        // array('uchome_blogfield', 'tag', 'blogid', TRUE),
                        // array('uchome_feed', 'body_template', 'feedid', TRUE),
                        // array('uchome_feed', 'body_data', 'feedid', TRUE),
                        // array('uchome_report', 'uids', 'rid', TRUE),
                        // array('uchome_share', 'body_data', 'sid', TRUE),
                        // array('uchome_userblock', 'blockinfo', 'bid', TRUE),
                        array('cdb_uc_pm_lists', 'lastmessage', 'plid', FALSE),
                );
        } elseif($type == 'x2') {
                $list = array(
                                array('pre_common_setting','svalue', 'skey', FALSE),
                                array('pre_forum_grouplevel','creditspolicy','levelid', TRUE),
                                array('pre_forum_grouplevel','postpolicy','levelid', TRUE),
                                array('pre_forum_grouplevel','specialswitch','levelid', TRUE),
                                array('pre_common_advertisement','parameters','advid', TRUE),
                                array('pre_common_plugin','modules','pluginid', TRUE),
                                array('pre_common_block','param','bid', TRUE),
                                array('pre_common_block_item','fields','itemid', TRUE),
                                array('pre_common_block_style','template','styleid', TRUE),
                                array('pre_common_diy_data','diycontent','targettplname', TRUE),
                                array('pre_common_member_field_forum','groups','uid', TRUE),
                                array('pre_common_member_stat_search','condition','optionid', TRUE),
                                array('pre_common_syscache','data','cname', TRUE),
                        );
        } elseif($type == 'x31') {
                $list = array(
                               array('pre_common_advertisement','parameters','advid', TRUE),
array('pre_common_block','param','bid', TRUE),
                                array('pre_common_block','blockstyle','bid', TRUE),
array('pre_common_block_item','fields','itemid', TRUE),
array('pre_common_block_style','template','styleid', TRUE),
array('pre_common_block_style','fields','styleid', TRUE),
array('pre_common_diy_data','diycontent','targettplname', TRUE),
array('pre_common_member_field_forum','groupterms','uid', TRUE),
array('pre_common_member_field_home','blockposition','uid', TRUE),
array('pre_common_member_field_home','privacy','uid', TRUE),
array('pre_common_member_field_home','acceptemail','uid', TRUE),
array('pre_common_member_field_home','magicgift','uid', TRUE),
array('pre_common_member_newprompt','data','uid', TRUE),
// array('pre_common_member_newprompt','data','uid', TRUE),
array('pre_common_member_verify_info','field','vid', TRUE),
array('pre_common_plugin','modules','pluginid', TRUE),
array('pre_common_setting','svalue', 'skey', FALSE),
// array('pre_forum_ejb_hg','content', 'id', FALSE),
array('pre_forum_forumfield','creditspolicy','fid', TRUE),
array('pre_forum_forumfield','formulaperm','fid', TRUE),
array('pre_forum_forumfield','threadsorts','fid', TRUE),
array('pre_forum_forumfield','threadtypes','fid', TRUE),
array('pre_forum_forumfield','supe_pushsetting','fid', TRUE),
array('pre_forum_forumfield','modrecommend','fid', TRUE),
array('pre_forum_forumfield','threadplugin','fid', TRUE),
array('pre_forum_forumfield','extra','fid', TRUE),
array('pre_forum_grouplevel','creditspolicy','levelid', TRUE),
array('pre_forum_grouplevel','postpolicy','levelid', TRUE),
array('pre_forum_grouplevel','specialswitch','levelid', TRUE),
array('pre_forum_medal','permission','medalid', TRUE),
// array('pre_forum_optionvalue105','stf7pic','tid', TRUE),
// array('pre_forum_optionvalue106','schematic','tid', TRUE),
// array('pre_forum_optionvalue106','source','tid', TRUE),
// array('pre_forum_optionvalue106','represent','tid', TRUE),
// array('pre_forum_plugin_eeworldtools','svalue','skey', TRUE),
array('pre_forum_postcache','comment','pid', TRUE),
array('pre_forum_postcache','rate','pid', TRUE),
// array('pre_forum_relate_news_blog_bbs','news_content','tid', TRUE),
// array('pre_forum_relate_news_blog_bbs','bbs_content','tid', TRUE),
// array('pre_forum_relate_news_blog_bbs','blog_content','tid', TRUE),
array('pre_forum_spacecache','value','uid', TRUE),
array('pre_forum_threadprofile','template','id', TRUE),
array('pre_forum_typeoption','protect','optionid', TRUE),
array('pre_forum_typeoption','rules','optionid', TRUE),
array('pre_home_feed','title_data','feedid', TRUE),
array('pre_home_feed','body_data','feedid', TRUE),
array('pre_home_share','body_data','sid', TRUE),
array('pre_mobile_wechat_resource','data','id', TRUE),
array('pre_mobile_wsq_threadlist','svalue','skey', TRUE),
                        );
        }
        return $list;
}
?>

路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

QQ|Archiver|手机版|小黑屋|软路由 ( 渝ICP备15001194号-1|渝公网安备 50011602500124号 )

GMT+8, 2024-3-28 18:13 , Processed in 0.058392 second(s), 5 queries , Gzip On, Redis On.

Powered by Discuz! X3.5 Licensed

© 2001-2023 Discuz! Team.

返回顶部