<?php
if($op == "detail") {
	if($isstop == 1) {
		$operation = $xc->language['convert_stats'];
		$show_message = $xc->language['convert_have_stoped']."<div align=\"right\">[<a href=\"index.php?mod=stats&do=".$do."&op=detail\" style=\"color:red\">".$xc->language['convert_start_again']."</a>]</div>";
		$show_message .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
	} else {
		$discuz_tablepre = $xc->config['db']['discuz_dbname'].'.'.$xc->config['db']['discuz_tablepre'];

		if($xc->config['db']['discuz_type'] == 'x') {
			$pertask = $pertask ? intval($pertask) : $xc->config['info']['pertask'];
		} else {
			$pertask = $pertask ? intval($pertask) : ($do == 'threadreplies' ? 500 : ($do == 'memberposts' ? 1000 : ($do == 'memberdigests' ? 1000 : ($do == 'forumposts' ? 15 : 50))));
		}

		if($gap == 1) {
			if(file_exists(XCONVERT_ROOT.'./cache/lock/progress_stats.txt')) {
				$contents = file_get_contents(XCONVERT_ROOT.'./cache/lock/progress_stats.txt');
				$tmparr = $parameters = array();
				preg_match_all("/&(.+?)=(\d+)?/is", $contents, $tmparr);
				foreach($tmparr[1] as $k => $v) {
					$parameters[$v] = $tmparr[2][$k];
				}
				extract($parameters);
			}
			$gapmsg = dgmdate($timestamp, 'Y-m-d H:i:s')."\t";
			$gapmsg .= 'index.php?mod=stats&do='.$do.'&op=detail&current='.$current.'&pertask='.$pertask."\n";
			runlog("jumpinterrupt", $errmsg);
			sendmail($xc->config['info']['toemail'], "XConvert Jump Interrupt Message", $message);
		}
        
		$current = $current ? intval($current) : 0;
		$next = $current + $pertask;
		$step_arr = $xc->config['db']['discuz_type'] == 'x' ? array('memberposts','memberdigests','forumposts','groupnum','groupmembernum','groupmemberpost','blogreplynum','friendnum','albumpicnum', 'threadrush') : array('memberposts', 'memberdigests', 'forumposts');
		$do = in_array($do, $step_arr) ? $do : 'threadreplies';
        
		//重新做统计
		$operation = $xc->language['stats_'.$do];
        
		if($do == 'threadreplies') {
			//统计主题回复数
			$maxtid = $xc->db['discuz']->result($xc->db['discuz']->query("SELECT max(tid) FROM {$discuz_tablepre}".($xc->config['db']['discuz_type'] == 'x' ? "forum_thread": "threads")), 0);
			if($current <= $maxtid) {
				$processed = 1;
			}
			$queryt = $xc->db['discuz']->query("SELECT tid, replies, lastpost, lastposter FROM {$discuz_tablepre}".($xc->config['db']['discuz_type'] == 'x' ? "forum_thread": "threads")." WHERE displayorder>='0' AND tid >= $current AND tid < $current + $pertask");
			while($threads = $xc->db['discuz']->fetch_array($queryt)) {
				$replynum = $xc->db['discuz']->result($xc->db['discuz']->query("SELECT COUNT(*) FROM {$discuz_tablepre}".($xc->config['db']['discuz_type'] == 'x' ? "forum_post": "posts")." WHERE tid='$threads[tid]' AND invisible='0'"), 0);
				$replynum --;
				if($xc->config['db']['discuz_type'] == 'x') {
					$lastpost = $xc->db['discuz']->fetch_array($xc->db['discuz']->query("SELECT author, dateline FROM {$discuz_tablepre}forum_post WHERE tid='$threads[tid]' AND invisible='0' ORDER BY dateline DESC LIMIT 1"));
					if($threads['replies'] != $replynum || $threads['lastpost'] != $lastpost['dateline'] || $threads['lastposter'] != $lastpost['author']) {
						$xc->db['discuz']->query("UPDATE {$discuz_tablepre}forum_thread SET replies='$replynum', lastpost='$lastpost[dateline]', lastposter='".addslashes($lastpost['author'])."' WHERE tid='$threads[tid]'");
					}
				} else {
					$xc->db['discuz']->query("UPDATE {$discuz_tablepre}threads SET replies='$replynum' WHERE tid='$threads[tid]'");
				}
				$processed = 1;
			}

			if($processed) {
				$show_url = 'index.php?mod=stats&do='.$do.'&op=detail&current='.$next.'&pertask='.$pertask;
				$show_message =$xc->language['stats_donig'].' '.$xc->language['stats_from'].$current.$xc->language['stats_to'].$next.$xc->language['stats_rowdata'];
				$show_message .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
				$show_message .= "<p><a href=\"".$show_url."\">".$xc->language['message_redirect']."</a></p><div align=\"right\">[<a href=\"index.php?mod=stats&do=".$do."&op=detail&isstop=1  style=\"color:red\">".$xc->language['message_stop']."</a>]</div><script>setTimeout(\"redirect('".$show_url."');\", 1000);</script>";
			} else {
				$show_message = $xc->language['convert_ready'].$xc->language['stats_threadreplies_finish'].$xc->language['convert_date'].'......<br>';
				$show_message .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
				$show_url = 'index.php?mod=stats&do=memberposts&op=detail';
				$show_message .= "<script>setTimeout(\"redirect('".$show_url."');\", 1000);</script>";
			}
			file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_stats.txt', $show_url);
		} elseif($do == 'memberposts') {
			//统计用户发帖量
			$maxuid = $xc->db['discuz']->result($xc->db['discuz']->query("SELECT max(uid) FROM {$discuz_tablepre}".($xc->config['db']['discuz_type'] == 'x' ? "common_member": "members")), 0);
			if($current <= $maxuid) {
				$processed = 1;
			}
			$queryt = $xc->db['discuz']->query("SELECT uid FROM {$discuz_tablepre}".($xc->config['db']['discuz_type'] == 'x' ? "common_member": "members")." WHERE uid >= $current AND uid < $current + $pertask");
			while($mem = $xc->db['discuz']->fetch_array($queryt)) {
				$postcount = $xc->db['discuz']->result($xc->db['discuz']->query("SELECT COUNT(*) FROM {$discuz_tablepre}".($xc->config['db']['discuz_type'] == 'x' ? "forum_post": "posts")." WHERE authorid='$mem[uid]' AND invisible='0'"), 0);
				if($xc->config['db']['discuz_type'] == 'x') {
					$threadcount = $xc->db['discuz']->result($xc->db['discuz']->query("SELECT COUNT(*) FROM {$discuz_tablepre}forum_thread WHERE authorid='$mem[uid]'"), 0);
					$xc->db['discuz']->query("UPDATE {$discuz_tablepre}common_member_count SET posts='".$postcount."', threads='".$threadcount."' WHERE uid='$mem[uid]'");
				} else {
					$xc->db['discuz']->query("UPDATE {$discuz_tablepre}".($xc->config['db']['discuz_type'] == 'x' ? "common_member": "members")." SET posts='".$postcount."' WHERE uid='$mem[uid]'");
				}
				$processed = 1;
			}

			if($processed) {
				$show_message =$xc->language['stats_donig'].' '.$xc->language['stats_from'].$current.$xc->language['stats_to'].$next.$xc->language['stats_rowdata'];
				$show_message .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
				$show_url = 'index.php?mod=stats&do='.$do.'&op=detail&current='.$next.'&pertask='.$pertask;
				$show_message .= "<p><a href=\"".$show_url."\">".$xc->language['message_redirect']."</a></p><div align=\"right\">[<a href=\"index.php?mod=stats&do=".$do."&op=detail&isstop=1  style=\"color:red\">".$xc->language['message_stop']."</a>]</div><script>setTimeout(\"redirect('".$show_url."');\", 1000);</script>";

			} else {
				$show_message = $xc->language['convert_ready'].$xc->language['stats_memberposts_finish'].$xc->language['convert_date'].'......<br>';
				$show_message .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
				$show_url = 'index.php?mod=stats&do=memberdigests&op=detail';
				$show_message .= "<script>setTimeout(\"redirect('".$show_url."');\", 1000);</script>";
			}
			file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_stats.txt', $show_url);
		} elseif($do == 'memberdigests') {
			//统计用户精华帖数
			$membersarray = $postsarray = array();
			if($xc->config['db']['discuz_type'] == 'x') {
				$xc->db['discuz']->query("UPDATE {$discuz_tablepre}common_member_count SET digestposts=0", 'UNBUFFERED');
			}
			
			$maxtid = $xc->db['discuz']->result($xc->db['discuz']->query("SELECT max(tid) FROM {$discuz_tablepre}".($xc->config['db']['discuz_type'] == 'x' ? "forum_thread": "threads")." WHERE digest<>'0' AND displayorder>='0'"), 0);
			if($current <= $maxtid) {
				$processed = 1;
			}

			$query = $xc->db['discuz']->query("SELECT authorid FROM {$discuz_tablepre}".($xc->config['db']['discuz_type'] == 'x' ? "forum_thread": "threads")." WHERE digest<>'0' AND displayorder>='0' AND tid >= $current AND tid < $current + $pertask");
			while($thread = $xc->db['discuz']->fetch_array($query)) {
				$thread['authorid'] && $membersarray[$thread['authorid']]++;
				$processed = 1;
			}
			
			$extra_str = '';
			foreach($membersarray as $uid => $posts) {
				$postsarray[$posts] .= $extra_str.$uid;
				$extra_str = ',';
			}
			unset($membersarray);

			foreach($postsarray as $posts => $uids) {
				$xc->db['discuz']->query("UPDATE {$discuz_tablepre}".($xc->config['db']['discuz_type'] == 'x' ? "common_member_count": "members")." SET digestposts=digestposts+'$posts' WHERE uid IN ($uids)", 'UNBUFFERED');
			}

			if($processed) {
				$show_message =$xc->language['stats_donig'].' '.$xc->language['stats_from'].$current.$xc->language['stats_to'].$next.$xc->language['stats_rowdata'];
				$show_message .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
				$show_url = 'index.php?mod=stats&do='.$do.'&op=detail&current='.$next.'&pertask='.$pertask;
				$show_message .= "<p><a href=\"".$show_url."\">".$xc->language['message_redirect']."</a></p><div align=\"right\">[<a href=\"index.php?mod=stats&do=".$do."&op=detail&isstop=1  style=\"color:red\">".$xc->language['message_stop']."</a>]</div><script>setTimeout(\"redirect('".$show_url."');\", 1000);</script>";

			} else {
				$show_message = $xc->language['convert_ready'].$xc->language['stats_memberdigests_finish'].$xc->language['convert_date'].'......<br>';
				$show_message .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
				$show_url = 'index.php?mod=stats&do=forumposts&op=detail';
				$show_message .= "<script>setTimeout(\"redirect('".$show_url."');\", 1000);</script>";
			}
			file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_stats.txt', $show_url);
		} elseif($do == 'forumposts') {
			//统计版块的主题数、回复数、最后发表主题
			$maxfid = $xc->db['discuz']->result($xc->db['discuz']->query("SELECT max(fid) FROM {$discuz_tablepre}".($xc->config['db']['discuz_type'] == 'x' ? "forum_forum": "forums")), 0);
			if($current <= $maxfid) {
				$processed = 1;
			}

			$queryf = $xc->db['discuz']->query("SELECT fid FROM {$discuz_tablepre}".($xc->config['db']['discuz_type'] == 'x' ? "forum_forum": "forums")." WHERE type<>'group' AND fid >= $current AND fid < $current + $pertask");
			while($forum = $xc->db['discuz']->fetch_array($queryf)) {
				$query = $xc->db['discuz']->query("SELECT COUNT(*) AS threads, SUM(replies)+COUNT(*) AS posts FROM {$discuz_tablepre}".($xc->config['db']['discuz_type'] == 'x' ? "forum_thread": "threads")." WHERE fid='$forum[fid]' AND displayorder>='0'");
				extract($xc->db['discuz']->fetch_array($query));

				$query = $xc->db['discuz']->query("SELECT tid, subject, lastpost, lastposter FROM {$discuz_tablepre}".($xc->config['db']['discuz_type'] == 'x' ? "forum_thread": "threads")." WHERE fid='$forum[fid]' AND displayorder>='0' ORDER BY lastpost DESC LIMIT 1");
				$thread = $xc->db['discuz']->fetch_array($query);
				$lastpost = addslashes("$thread[tid]\t$thread[subject]\t$thread[lastpost]\t$thread[lastposter]");

				$xc->db['discuz']->query("UPDATE {$discuz_tablepre}".($xc->config['db']['discuz_type'] == 'x' ? "forum_forum": "forums")." SET threads='$threads', posts='$posts', lastpost='$lastpost' WHERE fid='$forum[fid]'");

				$processed = 1;
			}

			if($processed) {
				$show_message =$xc->language['stats_donig'].' '.$xc->language['stats_from'].$current.$xc->language['stats_to'].$next.$xc->language['stats_rowdata'];
				$show_message .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
				$show_url = 'index.php?mod=stats&do='.$do.'&op=detail&current='.$next.'&pertask='.$pertask;
				$show_message .= "<p><a href=\"".$show_url."\">".$xc->language['message_redirect']."</a></p><div align=\"right\">[<a href=\"index.php?mod=stats&do=".$do."&op=detail&isstop=1  style=\"color:red\">".$xc->language['message_stop']."</a>]</div><script>setTimeout(\"redirect('".$show_url."');\", 1000);</script>";
			} else {
				$show_message = $xc->language['convert_ready'].$xc->language['stats_forumposts_finish'].$xc->language['convert_date'].'......<br>';
				$show_message .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
				$show_url = $xc->config['db']['discuz_type'] == 'x' ? 'index.php?mod=stats&do=groupnum&op=detail' : 'index.php?mod=finish';
				$show_message .= "<script>setTimeout(\"redirect('".$show_url."');\", 1000);</script>";
			}
			file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_stats.txt', $show_url);
		} elseif($do == 'groupnum') {	//重建分类群组数
			$maxfid = $xc->db['discuz']->result($xc->db['discuz']->query("SELECT max(fid) FROM {$discuz_tablepre}".($xc->config['db']['discuz_type'] == 'x' ? "forum_forum": "forums")." WHERE status='3' AND type='forum'"), 0);
			if($current <= $maxfid) {
				$processed = 1;
			}

			$query1 = $xc->db['discuz']->query("SELECT fid FROM {$discuz_tablepre}forum_forum WHERE status='3' AND type='forum' AND fid >= $current AND fid < $current + $pertask");
			while($group = $xc->db['discuz']->fetch_array($query1)) {
				//note 分类群组数
				$query2 = $xc->db['discuz']->query("SELECT COUNT(*) as num FROM {$discuz_tablepre}forum_forum WHERE fup='$group[fid]' GROUP BY fup");
				while($groupnum = $xc->db['discuz']->fetch_array($query2)) {
					$xc->db['discuz']->query("UPDATE {$discuz_tablepre}forum_forumfield SET groupnum = '$groupnum[num]' WHERE fid='$group[fid]'");
				}
				$processed = 1;
			}
			
			if($processed) {
				$show_message =$xc->language['stats_donig'].' '.$xc->language['stats_from'].$current.$xc->language['stats_to'].$next.$xc->language['stats_rowdata'];
				$show_message .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
				$show_url = 'index.php?mod=stats&do='.$do.'&op=detail&current='.$next.'&pertask='.$pertask;
				$show_message .= "<p><a href=\"".$show_url."\">".$xc->language['message_redirect']."</a></p><div align=\"right\">[<a href=\"index.php?mod=stats&do=".$do."&op=detail&isstop=1  style=\"color:red\">".$xc->language['message_stop']."</a>]</div><script>setTimeout(\"redirect('".$show_url."');\", 1000);</script>";
			} else {
				$show_message = $xc->language['convert_ready'].$xc->language['stats_groupnum_finish'].$xc->language['convert_date'].'......<br>';
				$show_message .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
				$show_url = $xc->config['db']['discuz_type'] == 'x' ? 'index.php?mod=stats&do=groupmembernum&op=detail' : 'index.php?mod=finish';
				$show_message .= "<script>setTimeout(\"redirect('".$show_url."');\", 1000);</script>";
			}
			file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_stats.txt', $show_url);
		} elseif($do == 'groupmembernum') {		//重建群组成员数
			$maxfid = $xc->db['discuz']->result($xc->db['discuz']->query("SELECT max(fid) FROM {$discuz_tablepre}".($xc->config['db']['discuz_type'] == 'x' ? "forum_forum": "forums")." WHERE status='3' AND type='sub'"), 0);
			if($current <= $maxfid) {
				$processed = 1;
			}

			$query = $xc->db['discuz']->query("SELECT fid FROM {$discuz_tablepre}forum_forum WHERE status='3' AND type='sub' AND fid >= $current AND fid < $current + $pertask");
			while($group = $xc->db['discuz']->fetch_array($query)) {
				$membernum = $xc->db['discuz']->result($xc->db['discuz']->query("SELECT COUNT(*) FROM {$discuz_tablepre}forum_groupuser WHERE fid='$group[fid]' AND level>'0'"), 0);
				$xc->db['discuz']->query("UPDATE {$discuz_tablepre}forum_forumfield SET membernum = '$membernum' WHERE fid='$group[fid]'");
				$processed = 1;
			}

			if($processed) {
				$show_message = $xc->language['stats_donig'].' '.$xc->language['stats_from'].$current.$xc->language['stats_to'].$next.$xc->language['stats_rowdata'];
				$show_message .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
				$show_url = 'index.php?mod=stats&do='.$do.'&op=detail&current='.$next.'&pertask='.$pertask;
				$show_message .= "<p><a href=\"".$show_url."\">".$xc->language['message_redirect']."</a></p><div align=\"right\">[<a href=\"index.php?mod=stats&do=".$do."&op=detail&isstop=1  style=\"color:red\">".$xc->language['message_stop']."</a>]</div><script>setTimeout(\"redirect('".$show_url."');\", 1000);</script>";
			} else {
				$show_message = $xc->language['convert_ready'].$xc->language['stats_groupmembernum_finish'].$xc->language['convert_date'].'......<br>';
				$show_message .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
				$show_url = $xc->config['db']['discuz_type'] == 'x' ? 'index.php?mod=stats&do=groupmemberpost&op=detail' : 'index.php?mod=finish';
				$show_message .= "<script>setTimeout(\"redirect('".$show_url."');\", 1000);</script>";
			}
			file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_stats.txt', $show_url);
		} elseif($do == 'groupmemberpost') {	//重建群组成员发帖数
			$maxfid = $xc->db['discuz']->result($xc->db['discuz']->query("SELECT max(fid) FROM {$discuz_tablepre}".($xc->config['db']['discuz_type'] == 'x' ? "forum_forum": "forums")." WHERE status='3' AND type='sub'"), 0);
			if($current <= $maxfid) {
				$processed = 1;
			}

			$query1 = $xc->db['discuz']->query("SELECT fid FROM {$discuz_tablepre}forum_forum WHERE status='3' AND type='sub' AND fid >= $current AND fid < $current + $pertask");
			while($group = $xc->db['discuz']->fetch_array($query1)) {
				$replies_array	= array();
				
				//note 群组用户回复数
				$query2 = $xc->db['discuz']->query("SELECT COUNT(*) as num,authorid FROM {$discuz_tablepre}forum_post WHERE fid='$group[fid]' AND first='0' GROUP BY authorid");
				while($post = $xc->db['discuz']->fetch_array($query2)) {
					$replies_array[$post['authorid']] = $post['num'];
				}
				foreach($replies_array as $replies) {
					$xc->db['discuz']->query("UPDATE {$discuz_tablepre}forum_groupuser SET replies = '$replies[num]' WHERE fid='$group[fid]' AND uid='$replies[authorid]'");
				}

				//note 群组用户主题数
				$query3 = $xc->db['discuz']->query("SELECT COUNT(*) as num, authorid FROM {$discuz_tablepre}forum_thread WHERE fid='$group[fid]' GROUP BY authorid");
				while($thread = $xc->db['discuz']->fetch_array($query3)) {
					$xc->db['discuz']->query("UPDATE {$discuz_tablepre}forum_groupuser SET threads = '$thread[num]' WHERE fid='$group[fid]' AND uid='$thread[authorid]'");
				}
				$processed = 1;
			}
			
			if($processed) {
				$show_message = $xc->language['stats_donig'].' '.$xc->language['stats_from'].$current.$xc->language['stats_to'].$next.$xc->language['stats_rowdata'];
				$show_message .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
				$show_url = 'index.php?mod=stats&do='.$do.'&op=detail&current='.$next.'&pertask='.$pertask;
				$show_message .= "<p><a href=\"".$show_url."\">".$xc->language['message_redirect']."</a></p><div align=\"right\">[<a href=\"index.php?mod=stats&do=".$do."&op=detail&isstop=1  style=\"color:red\">".$xc->language['message_stop']."</a>]</div><script>setTimeout(\"redirect('".$show_url."');\", 1000);</script>";
			} else {
				$show_message = $xc->language['convert_ready'].$xc->language['stats_groupmemberpost_finish'].$xc->language['convert_date'].'......<br>';
				$show_message .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
				$show_url = $xc->config['db']['discuz_type'] == 'x' ? 'index.php?mod=stats&do=blogreplynum&op=detail' : 'index.php?mod=finish';
				$show_message .= "<script>setTimeout(\"redirect('".$show_url."');\", 1000);</script>";
			}
			file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_stats.txt', $show_url);
		} elseif($do == 'blogreplynum') {	//重建日志回复数
			$maxblogid = $xc->db['discuz']->result($xc->db['discuz']->query("SELECT max(blogid) FROM {$discuz_tablepre}home_blog"), 0);
			if($current <= $maxblogid) {
				$processed = 1;
			}

			$query = $xc->db['discuz']->query("SELECT blogid, replynum FROM {$discuz_tablepre}home_blog WHERE blogid >= $current AND blogid < $current + $pertask");
			while ($value = $xc->db['discuz']->fetch_array($query)) {
				$count = $xc->db['discuz']->result($xc->db['discuz']->query("SELECT COUNT(*) FROM {$discuz_tablepre}home_comment WHERE id='$value[blogid]' AND idtype='blogid'"),0);
				if($count != $value['replynum']){
					$xc->db['discuz']->query("UPDATE {$discuz_tablepre}home_blog SET replynum=$count WHERE blogid = '$value[blogid]'");
				}
				$processed = 1;
			}
			
			if($processed) {
				$show_message = $xc->language['stats_donig'].' '.$xc->language['stats_from'].$current.$xc->language['stats_to'].$next.$xc->language['stats_rowdata'];
				$show_message .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
				$show_url = 'index.php?mod=stats&do='.$do.'&op=detail&current='.$next.'&pertask='.$pertask;
				$show_message .= "<p><a href=\"".$show_url."\">".$xc->language['message_redirect']."</a></p><div align=\"right\">[<a href=\"index.php?mod=stats&do=".$do."&op=detail&isstop=1  style=\"color:red\">".$xc->language['message_stop']."</a>]</div><script>setTimeout(\"redirect('".$show_url."');\", 1000);</script>";
			} else {
				$show_message = $xc->language['convert_ready'].$xc->language['stats_blogreplynum_finish'].$xc->language['convert_date'].'......<br>';
				$show_message .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
				$show_url = $xc->config['db']['discuz_type'] == 'x' ? 'index.php?mod=stats&do=friendnum&op=detail' : 'index.php?mod=finish';
				$show_message .= "<script>setTimeout(\"redirect('".$show_url."');\", 1000);</script>";
			}
			file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_stats.txt', $show_url);
		} elseif($do == 'friendnum') {			//重建空间好友数
			$maxuid = $xc->db['discuz']->result($xc->db['discuz']->query("SELECT max(uid) FROM {$discuz_tablepre}common_member_count"), 0);
			if($current <= $maxuid) {
				$processed = 1;
			}
			$query = $xc->db['discuz']->query("SELECT uid, friends FROM {$discuz_tablepre}common_member_count WHERE uid >= $current AND uid < $current + $pertask");
			while ($value = $xc->db['discuz']->fetch_array($query)) {
				$count = $xc->db['discuz']->result($xc->db['discuz']->query("SELECT COUNT(*) FROM {$discuz_tablepre}home_friend WHERE uid='$value[uid]'"),0);
				if($count != $value['friends']) {
					$xc->db['discuz']->query("UPDATE {$discuz_tablepre}common_member_count SET friends=$count WHERE uid = '$value[uid]'");
				}
				$processed = 1;
			}
			
			if($processed) {
				$show_message = $xc->language['stats_donig'].' '.$xc->language['stats_from'].$current.$xc->language['stats_to'].$next.$xc->language['stats_rowdata'];
				$show_message .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
				$show_url = 'index.php?mod=stats&do='.$do.'&op=detail&current='.$next.'&pertask='.$pertask;
				$show_message .= "<p><a href=\"".$show_url."\">".$xc->language['message_redirect']."</a></p><div align=\"right\">[<a href=\"index.php?mod=stats&do=".$do."&op=detail&isstop=1  style=\"color:red\">".$xc->language['message_stop']."</a>]</div><script>setTimeout(\"redirect('".$show_url."');\", 1000);</script>";
			} else {
				$show_message = $xc->language['convert_ready'].$xc->language['stats_friendnum_finish'].$xc->language['convert_date'].'......<br>';
				$show_message .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
				$show_url = $xc->config['db']['discuz_type'] == 'x' ? 'index.php?mod=stats&do=albumpicnum&op=detail' : 'index.php?mod=finish';
				$show_message .= "<script>setTimeout(\"redirect('".$show_url."');\", 1000);</script>";
			}
			file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_stats.txt', $show_url);
		} elseif($do == 'albumpicnum') {		//重建相册图片数
			$maxalbumid = $xc->db['discuz']->result($xc->db['discuz']->query("SELECT max(albumid) FROM {$discuz_tablepre}home_album"), 0);
			if($current <= $maxalbumid) {
				$processed = 1;
			}
			$query = $xc->db['discuz']->query("SELECT albumid, picnum FROM {$discuz_tablepre}home_album WHERE albumid >= $current AND albumid > $start + $pertask");
			while ($value = $xc->db['discuz']->fetch_array($query)) {
				$processed = 1;
				$count = $xc->db['discuz']->result($xc->db['discuz']->query("SELECT COUNT(*) FROM {$discuz_tablepre}home_pic WHERE albumid='$value[albumid]'"),0);
				if($count != $value['picnum']) {
					$xc->db['discuz']->query("UPDATE {$discuz_tablepre}home_album SET picnum='$count' WHERE albumid = '$value[albumid]'");
				}
			}
            
			if($processed) {
				$show_message = $xc->language['stats_donig'].' '.$xc->language['stats_from'].$current.$xc->language['stats_to'].$next.$xc->language['stats_rowdata'];
				$show_message .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
				$show_url = 'index.php?mod=stats&do='.$do.'&op=detail&current='.$next.'&pertask='.$pertask;
				$show_message .= "<p><a href=\"".$show_url."\">".$xc->language['message_redirect']."</a></p><div align=\"right\">[<a href=\"index.php?mod=stats&do=".$do."&op=detail&isstop=1  style=\"color:red\">".$xc->language['message_stop']."</a>]</div><script>setTimeout(\"redirect('".$show_url."');\", 1000);</script>";
				file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_stats.txt', $show_url);
			} else {
				$show_message = $xc->language['stats_albumpicnum_finish'].'<br>';
				$show_message .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
                $show_url = 'index.php?mod=finish';
                //$show_url = $xc->config['db']['discuz_type'] == 'x' ? 'index.php?mod=stats&do=threadrush&op=detail' : 'index.php?mod=finish';
				$show_message .= "<script>window.parent.location = '".$show_url."';</script>";
				unlink(XCONVERT_ROOT.'./cache/lock/progress_stats.txt');
			}
		}
        /* 
        elseif($do == 'threadrush') {     // 重建抢楼帖数据
            truncatetable('forum_postposition');
            $query = $xc->db['discuz']->query("SELECT tid FROM {$discuz_tablepre}forum_thread WHERE status='37'");  
            while($thread = $xc->db['discuz']->fetch_array($query)) {
                $position = 1;
                $sql_posts = "INSERT INTO {$discuz_tablepre}forum_postposition (tid, position, pid) VALUES ";
                $query_post = $xc->db['discuz']->query("SELECT pid FROM {$discuz_tablepre}forum_post WHERE tid='$thread[tid]' ORDER BY first DESC, pid ASC ");
                while($post = $xc->db['discuz']->fetch_array($query_post)) {
                    $insert_list[] = "('$thread[tid]', '$position', '$post[pid]')";
                    $position ++;
                }
                $sql_posts .= implode(', ', $insert_list);
                unset($insert_list);
                $xc->db['discuz']->query($sql_posts);
            }
            $show_message = $xc->language['stats_threadrush_finish'].'<br>';
			$show_message .= "<div id=\"checkflag\" name=\"checkflag\" style=\"width: 0px;height: 0px;overflow:auto;\">ok</div>";
			$show_url = 'index.php?mod=finish';
			$show_message .= "<script>window.parent.location = '".$show_url."';</script>";
			unlink(XCONVERT_ROOT.'./cache/lock/progress_stats.txt');
		}
        */
	}
	include_once template('stats_iframe');
} else {
	if(file_exists(XCONVERT_ROOT.'./cache/lock/progress_stats.txt')) {
		unlink(XCONVERT_ROOT.'./cache/lock/progress_stats.txt');
	}
	include_once template('stats');
}
?>