📄 tools.php
字号:
$db->select_db($dbname);
if(!get_cfg_var('register_globals')) {
@extract($_GET, EXTR_SKIP);
}
$rpp = "1000"; //每次处理多少条数据
$totalrows = isset($totalrows) ? $totalrows : 0;
$convertedrows = isset($convertedrows) ? $convertedrows : 0;
$start = isset($start) && $start > 0 ? $start : 0;
$sqlstart = isset($start) && $start > $convertedrows ? $start - $convertedrows : 0;
$end = $start + $rpp - 1;
$stay = isset($stay) ? $stay : 0;
$converted = 0;
$step = isset($step) ? $step : 0;
$info = isset($info) ? $info : '';
$action = array(
'1'=>'冗余回复数据整理',
'2'=>'冗余附件数据整理',
'3'=>'冗余会员数据整理',
'4'=>'冗余版块数据整理',
'5'=>'冗余短信数据整理',
'6'=>'主题信息整理',
'7'=>'完成数据冗余整理'
);
$steps = count($action);
$actionnow = isset($action[$step]) ? $action[$step] : '结束';
$maxid = isset($maxid) ? $maxid : 0;
$tableid = isset($tableid) ? $tableid : 1;
htmlheader();
if($step==0){
?>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr><td>
<p class="subtitle">数据库冗余数据整理 <ul>
<center><div style="margin-top: 4px; border: 1px solid #7AC4EA; width: 80%;float:center;">
<table width="100%" border="0" cellpadding="6" cellspacing="0">
<tr class="header"><td colspan="9">数据库冗余数据整理项目详细信息</td></tr>
<tr align="center" style="background: #FFFFD9;">
<td>Posts表的整理</td><td>Attachments表的整理</td>
<td>Members表的整理</td><td>Forums表的整理</td>
<td>Pms表的整理</td><td>Threads表的整理</td><td>所有表的整理</td></tr><tr align="center">
<td class="altbg2">[<a href="?action=mysqlclear&step=1&stay=1">单步整理</a>]</td>
<td class="altbg1">[<a href="?action=mysqlclear&step=2&stay=1">单步整理</a>]</td>
<td class="altbg2">[<a href="?action=mysqlclear&step=3&stay=1">单步整理</a>]</td>
<td class="altbg1">[<a href="?action=mysqlclear&step=4&stay=1">单步整理</a>]</td>
<td class="altbg2">[<a href="?action=mysqlclear&step=5&stay=1">单步整理</a>]</td>
<td class="altbg2">[<a href="?action=mysqlclear&step=6&stay=1">单步整理</a>]</td>
<td class="altbg1">[<a href="?action=mysqlclear&step=1&stay=0">全部整理</a>]</td>
</tr>
</center></table></div>
<p><font color="red">注意:
<br><p style="text-indent: 3em; margin: 0;">对数据库操作可能会出现意外现象的发生及破坏,所以请先备份好数据库再进行上述操作!另外请您选择服务器压力比较小的时候进行一些优化操作。
<br><p style="text-indent: 3em; margin: 0;">当您使用完毕Discuz! 系统维护工具箱后,请点击锁定工具箱以确保系统的安全!下次使用前只需要在/forumdata目录下删除tool.lock文件即可开始使用。</p></font>
</td></tr></table>
<?php
} elseif ($step=='1'){
$query = "SELECT pid,tid FROM {$tablepre}posts LIMIT ".$sqlstart.", $rpp";
$posts=$db->query($query);
while ($post = $db->fetch_array($posts)){
$query = $db->query("SELECT tid FROM {$tablepre}threads WHERE tid='".$post['tid']."'");
if ($db->result($query, 0)) {
} else {
$convertedrows ++;
$db->query("DELETE FROM {$tablepre}posts WHERE pid='".$post['pid']."'");
}
$converted = 1;
$totalrows ++;
}
if($converted || $end < $maxid) {
continue_redirect();
} else {
stay_redirect();
}
} elseif ($step=='2'){
$query = "SELECT aid,pid,attachment FROM {$tablepre}attachments LIMIT ".$sqlstart.", $rpp";
$posts=$db->query($query);
while ($post = $db->fetch_array($posts)){
$query = $db->query("SELECT pid FROM {$tablepre}posts WHERE pid='".$post['pid']."'");
if ($db->result($query, 0)) {
} else {
$convertedrows ++;
$db->query("DELETE FROM {$tablepre}attachments WHERE aid='".$post['aid']."'");
$attachmentdir = DISCUZ_ROOT.'./attachments/';
@unlink($attachmentdir.$post['attachment']);
}
$converted = 1;
$totalrows ++;
}
if($converted || $end < $maxid) {
continue_redirect();
} else {
stay_redirect();
}
} elseif ($step=='3'){
$query = "SELECT uid FROM {$tablepre}memberfields LIMIT ".$sqlstart.", $rpp";
$posts=$db->query($query);
while ($post = $db->fetch_array($posts)){
$query = $db->query("SELECT uid FROM {$tablepre}members WHERE uid='".$post['uid']."'");
if ($db->result($query, 0)) {
} else {
$convertedrows ++;
$db->query("DELETE FROM {$tablepre}memberfields WHERE uid='".$post['uid']."'");
}
$converted = 1;
$totalrows ++;
}
if($converted || $end < $maxid) {
continue_redirect();
} else {
stay_redirect();
}
} elseif ($step=='4'){
$query = "SELECT fid FROM {$tablepre}forumfields LIMIT ".$sqlstart.", $rpp";
$posts=$db->query($query);
while ($post = $db->fetch_array($posts)){
$query = $db->query("SELECT fid FROM {$tablepre}forums WHERE fid='".$post['fid']."'");
if ($db->result($query, 0)) {
} else {
$convertedrows ++;
$db->query("DELETE FROM {$tablepre}forumfields WHERE fid='".$post['fid']."'");
}
$converted = 1;
$totalrows ++;
}
if($converted || $end < $maxid) {
continue_redirect();
} else {
stay_redirect();
}
} elseif ($step=='5'){
$query = "SELECT msgfromid,msgtoid FROM {$tablepre}pms LIMIT ".$sqlstart.", $rpp";
$posts=$db->query($query);
while ($post = $db->fetch_array($posts)){
$query = $db->query("SELECT uid FROM {$tablepre}members WHERE uid='".$post['msgtoid']."'");
if ($db->result($query, 0)) {
} else {
$convertedrows ++;
$db->query("DELETE FROM {$tablepre}pms WHERE msgtoid='".$post['msgtoid']."'");
}
$converted = 1;
$totalrows ++;
}
if($converted || $end < $maxid) {
continue_redirect();
} else {
stay_redirect();
}
} elseif ($step=='6'){
$query = "SELECT tid FROM {$tablepre}threads LIMIT ".$sqlstart.", $rpp";
$posts=$db->query($query);
while ($threads = $db->fetch_array($posts)){
$query = $db->query("SELECT COUNT(*) FROM {$tablepre}posts WHERE tid='".$threads['tid']."' AND invisible='0'");
$replynum = $db->result($query, 0) - 1;
if ($replynum < 0) {
$db->query("DELETE FROM {$tablepre}threads WHERE tid='".$threads['tid']."'");
} else {
$query = $db->query("SELECT a.aid FROM {$tablepre}posts p, {$tablepre}attachments a WHERE a.tid='".$threads['tid']."' AND a.pid=p.pid AND p.invisible='0' LIMIT 1");
$attachment = $db->num_rows($query) ? 1 : 0;//修复附件
$query = $db->query("SELECT pid, subject, rate FROM {$tablepre}posts WHERE tid='".$threads['tid']."' AND invisible='0' ORDER BY dateline LIMIT 1");
$firstpost = $db->fetch_array($query);
$firstpost['subject'] = addslashes($firstpost['subject']);
@$firstpost['rate'] = $firstpost['rate'] / abs($firstpost['rate']);//修复发帖
$query = $db->query("SELECT author, dateline FROM {$tablepre}posts WHERE tid='".$threads['tid']."' AND invisible='0' ORDER BY dateline DESC LIMIT 1");
$lastpost = $db->fetch_array($query);//修复最后发帖
$db->query("UPDATE {$tablepre}threads SET subject='".$firstpost['subject']."', replies='$replynum', lastpost='".$lastpost['dateline']."', lastposter='".addslashes($lastpost['author'])."', rate='".$firstpost['rate']."', attachment='$attachment' WHERE tid='".$threads['tid']."'", 'UNBUFFERED');
$db->query("UPDATE {$tablepre}posts SET first='1', subject='".$firstpost['subject']."' WHERE pid='".$firstpost['pid']."'", 'UNBUFFERED');
$db->query("UPDATE {$tablepre}posts SET first='0' WHERE tid='".$threads['tid']."' AND pid<>'".$firstpost['pid']."'", 'UNBUFFERED');
}
$converted = 1;
$totalrows ++;
}
if($converted || $end < $maxid) {
continue_redirect();
} else {
stay_redirect();
}
} elseif ($step=='7'){
echo '<div style="margin-top: 4px; border: 1px solid #7AC4EA; width: 80%;float:center;">
<table width="100%" border="0" cellpadding="6" cellspacing="0">
<tr class="header"><td colspan="9">完成冗余数据整理</td></tr><tr align="center" class="category">
<td>所有数据整理操作完毕.<br><br><font color="red">在您不使用本程序的时候,请注意锁定或删除本文件!</font><br></td></tr></table></div>';
}
htmlfooter();
} elseif ($action == 'repair_auto') {
if(@!include("./config.inc.php")) {
if(@!include("./config.php")) {
exit("请先上传config文件以保证您的数据库能正常链接!");
}
}
mysql_connect($dbhost, $dbuser, $dbpw);
mysql_select_db($dbname);
@set_time_limit(0);
$querysql = array(
'activityapplies' => 'applyid',
'adminnotes' => 'id',
'advertisements' => 'advid',
'announcements' => 'id',
'attachments' => 'aid',
'attachtypes' => 'id',
'banned' => 'id',
'bbcodes' => 'id',
'crons' => 'cronid',
'faqs' => 'id',
'forumlinks' => 'id',
'forums' => 'fid',
'itempool' => 'id',
'magicmarket' => 'mid',
'magics' => 'magicid',
'medals' => 'medalid',
'members' => 'uid',
'pluginhooks' => 'pluginhookid',
'plugins' => 'pluginid',
'pluginvars' => 'pluginvarid',
'pms' => 'pmid',
'pmsearchindex' => 'searchid',
'polloptions' => 'polloptionid',
'posts' => 'pid',
'profilefields' => 'fieldid',
'projects' => 'id',
'ranks' => 'rankid',
'searchindex' => 'searchid',
'smilies' => 'id',
'styles' => 'styleid',
'stylevars' => 'stylevarid',
'templates' => 'templateid',
'threads' => 'tid',
'threadtypes' => 'typeid',
'tradecomments' => 'id',
'typeoptions' => 'optionid',
'words' => 'id'
);
htmlheader();
echo '<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr><td>
<p class="subtitle">Discuz! 自增长字段修复 <ul>
<center><p class="subtitle">检查结果
<div style="margin-top: 4px; border: 1px solid #7AC4EA; width: 80%;">
<table width="100%" cellpadding="6" cellspacing="0" border="0">
<tr align="center" class="header"><td width="25%">数据表名</td><td width="25%">字段名</td><td width="25%">是否正常</td><td width="25%">自增长状态</td></tr>';
foreach($querysql as $key => $keyfield) {
echo '<tr align="center"><td width="25%" class="altbg2" align="left">'.$tablepre.$key.'</td><td width="25%" class="altbg1">'.$keyfield.'</td>';
if($query = @mysql_query("Describe $tablepre$key $keyfield")) {
$istableexist = '存在';
$field = @mysql_fetch_array($query);
if(empty($field[5]) && $field[0] == $keyfield) {
mysql_query("ALTER TABLE $tablepre$key CHANGE $keyfield $keyfield $field[1] NOT NULL AUTO_INCREMENT");
$tablestate = '<font color="red">已经修复</font>';
} else {
$tablestate = '正常';
}
} else {
$istableexist = '不存在';
$tablestate = '----';
}
echo '<td width="25%" class="altbg2">'.$istableexist.'</td><td width="25%" class="altbg1">'.$tablestate.'</td></tr>';
}
echo '</table>
</div><br></center>
<br><br>
<p><font color="red">注意:
<br><p style="text-indent: 3em; margin: 0;">对数据库操作可能会出现意外现象的发生及破坏,所以请先备份好数据库再进行上述操作!另外请您选择服务器压力比较小的时候进行一些优化操作。
<br><p style="text-indent: 3em; margin: 0;">当您使用完毕Discuz! 系统维护工具箱后,请点击锁定工具箱以确保系统的安全!下次使用前只需要在/forumdata目录下删除tool.lock文件即可开始使用。</p></font>
</td></tr></table>';
htmlfooter();
} elseif ($action == 'restore') {
ob_implicit_flush();
define('IN_DISCUZ', TRUE);
if(@(!include("./config.inc.php")) || @(!include('./include/db_'.$database.'.class.php'))) {
if(@(!include("./config.php")) || @(!include('./include/db_'.$database.'.class.php'))) {
exit("请先上传所有新版本的程序文件后再运行本升级程序!");
}
}
$db = new dbstuff;
$db->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
$db->select_db($dbname);
if(!get_cfg_var('register_globals')) {
@extract($HTTP_GET_VARS);
}
$sqldump = '';
htmlheader();
?>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr><td>
<p class="subtitle">数据库恢复实用工具 <ul>
<?php
echo "本程序用于恢复用 Discuz! 备份的数据文件,当 Discuz! 出现问题无法运行和恢复数据,<br>".
"而 phpMyAdmin 又不能恢复大文件时,可尝试使用此工具.<br><br>".
"注意:<ul>".
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -