Esempio n. 1
0
function splitTopic($split1_ID_TOPIC, $splitMessages, $new_subject)
{
    global $db_prefix, $ID_MEMBER, $user_info, $topic, $board, $modSettings;
    global $func;
    // Nothing to split?
    if (empty($splitMessages)) {
        fatal_lang_error('smf271', false);
    }
    // No sense in imploding it over and over again.
    $postList = implode(',', $splitMessages);
    if ($split1_ID_TOPIC == $topic) {
        $ID_BOARD = $board;
    } else {
        $request = db_query("\n\t\t\tSELECT ID_BOARD\n\t\t\tFROM {$db_prefix}topics\n\t\t\tWHERE ID_TOPIC = {$split1_ID_TOPIC}\n\t\t\tLIMIT 1", __FILE__, __LINE__);
        list($ID_BOARD) = mysql_fetch_row($request);
        mysql_free_result($request);
    }
    // Find the new first and last not in the list. (old topic)
    $request = db_query("\n\t\tSELECT MIN(m.ID_MSG) AS myID_FIRST_MSG, MAX(m.ID_MSG) AS myID_LAST_MSG, COUNT(*) - 1 AS myNumReplies, t.isSticky\n\t\tFROM ({$db_prefix}messages AS m, {$db_prefix}topics AS t)\n\t\tWHERE m.ID_MSG NOT IN ({$postList})\n\t\t\tAND m.ID_TOPIC = {$split1_ID_TOPIC}\n\t\t\tAND t.ID_TOPIC = {$split1_ID_TOPIC}\n\t\tGROUP BY m.ID_TOPIC\n\t\tLIMIT 1", __FILE__, __LINE__);
    // You can't select ALL the messages!
    if (mysql_num_rows($request) == 0) {
        fatal_lang_error('smf271b', false);
    }
    list($split1_firstMsg, $split1_lastMsg, $split1_replies, $isSticky) = mysql_fetch_row($request);
    mysql_free_result($request);
    $split1_firstMem = getMsgMemberID($split1_firstMsg);
    $split1_lastMem = getMsgMemberID($split1_lastMsg);
    // Find the first and last in the list. (new topic)
    $result = db_query("\n\t\tSELECT MIN(ID_MSG) AS myID_FIRST_MSG, MAX(ID_MSG) AS myID_LAST_MSG, COUNT(*) - 1 AS myNumReplies\n\t\tFROM {$db_prefix}messages\n\t\tWHERE ID_MSG IN ({$postList})\n\t\t\tAND ID_TOPIC = {$split1_ID_TOPIC}\n\t\tGROUP BY ID_TOPIC\n\t\tLIMIT 1", __FILE__, __LINE__);
    list($split2_firstMsg, $split2_lastMsg, $split2_replies) = mysql_fetch_row($result);
    mysql_free_result($result);
    $split2_firstMem = getMsgMemberID($split2_firstMsg);
    $split2_lastMem = getMsgMemberID($split2_lastMsg);
    // No database changes yet, so let's double check to see if everything makes at least a little sense.
    if ($split1_firstMsg <= 0 || $split1_lastMsg <= 0 || $split2_firstMsg <= 0 || $split2_lastMsg <= 0 || $split1_replies < 0 || $split2_replies < 0) {
        fatal_lang_error('smf272');
    }
    // You cannot split of the first message of a topic.
    if ($split1_firstMsg > $split2_firstMsg) {
        fatal_lang_error('smf268', false);
    }
    // We're off to insert the new topic!  Use 0 for now to avoid UNIQUE errors.
    db_query("\n\t\tINSERT INTO {$db_prefix}topics\n\t\t\t(ID_BOARD, ID_MEMBER_STARTED, ID_MEMBER_UPDATED, ID_FIRST_MSG, ID_LAST_MSG, numReplies, isSticky)\n\t\tVALUES ({$ID_BOARD}, {$split2_firstMem}, {$split2_lastMem}, 0, 0, {$split2_replies}, {$isSticky})", __FILE__, __LINE__);
    $split2_ID_TOPIC = db_insert_id();
    if ($split2_ID_TOPIC <= 0) {
        fatal_lang_error('smf273');
    }
    // Move the messages over to the other topic.
    $new_subject = $func['htmlspecialchars']($new_subject);
    db_query("\n\t\tUPDATE {$db_prefix}messages\n\t\tSET\n\t\t\tID_TOPIC = {$split2_ID_TOPIC},\n\t\t\tsubject = '{$new_subject}'\n\t\tWHERE ID_MSG IN ({$postList})\n\t\tLIMIT " . ($split2_replies + 1), __FILE__, __LINE__);
    // Cache the new topics subject... we can do it now as all the subjects are the same!
    updateStats('subject', $split2_ID_TOPIC, $new_subject);
    // Mess with the old topic's first, last, and number of messages.
    db_query("\n\t\tUPDATE {$db_prefix}topics\n\t\tSET\n\t\t\tnumReplies = {$split1_replies},\n\t\t\tID_FIRST_MSG = {$split1_firstMsg},\n\t\t\tID_LAST_MSG = {$split1_lastMsg},\n\t\t\tID_MEMBER_STARTED = {$split1_firstMem},\n\t\t\tID_MEMBER_UPDATED = {$split1_lastMem}\n\t\tWHERE ID_TOPIC = {$split1_ID_TOPIC}\n\t\tLIMIT 1", __FILE__, __LINE__);
    // Now, put the first/last message back to what they should be.
    db_query("\n\t\tUPDATE {$db_prefix}topics\n\t\tSET\n\t\t\tID_FIRST_MSG = {$split2_firstMsg},\n\t\t\tID_LAST_MSG = {$split2_lastMsg}\n\t\tWHERE ID_TOPIC = {$split2_ID_TOPIC}\n\t\tLIMIT 1", __FILE__, __LINE__);
    // The board has more topics now.
    db_query("\n\t\tUPDATE {$db_prefix}boards\n\t\tSET numTopics = numTopics + 1\n\t\tWHERE ID_BOARD = {$ID_BOARD}\n\t\tLIMIT 1", __FILE__, __LINE__);
    // We're going to assume they bothered to read it before splitting it.
    if (!$user_info['is_guest']) {
        db_query("\n\t\t\tREPLACE INTO {$db_prefix}log_topics\n\t\t\t\t(ID_MSG, ID_MEMBER, ID_TOPIC)\n\t\t\tVALUES ({$modSettings['maxMsgID']}, {$ID_MEMBER}, {$split2_ID_TOPIC})", __FILE__, __LINE__);
    }
    // Housekeeping.
    updateStats('topic');
    updateLastMessages($ID_BOARD);
    logAction('split', array('topic' => $split1_ID_TOPIC, 'new_topic' => $split2_ID_TOPIC));
    // Notify people that this topic has been split?
    sendNotifications($split1_ID_TOPIC, 'split');
    // Return the ID of the newly created topic.
    return $split2_ID_TOPIC;
}
Esempio n. 2
0
function RepairBoards()
{
    global $db_prefix, $txt, $scripturl, $db_connection, $sc, $context, $sourcedir;
    global $salvageCatID, $salvageBoardID;
    isAllowedTo('admin_forum');
    // Set up the administrative bar thing.
    adminIndex('maintain_forum');
    // Print out the top of the webpage.
    $context['page_title'] = $txt[610];
    $context['sub_template'] = 'rawdata';
    // Start displaying errors without fixing them.
    if (isset($_GET['fixErrors'])) {
        checkSession('get');
    }
    // Giant if/else. The first displays the forum errors if a variable is not set and asks
    // if you would like to continue, the other fixes the errors.
    if (!isset($_GET['fixErrors'])) {
        $context['repair_errors'] = array();
        $to_fix = findForumErrors();
        if (!empty($to_fix)) {
            $_SESSION['repairboards_to_fix'] = $to_fix;
            $_SESSION['repairboards_to_fix2'] = null;
            if (empty($context['repair_errors'])) {
                $context['repair_errors'][] = '???';
            }
        }
        $context['raw_data'] = '
			<table width="100%" border="0" cellspacing="0" cellpadding="4" class="tborder">
				<tr class="titlebg">
					<td>' . $txt['smf73'] . '</td>
				</tr><tr>
					<td class="windowbg">';
        if (!empty($to_fix)) {
            $context['raw_data'] .= '
						' . $txt['smf74'] . ':<br />
						' . implode('
						<br />', $context['repair_errors']) . '<br />
						<br />
						' . $txt['smf85'] . '<br />
						<b><a href="' . $scripturl . '?action=repairboards;fixErrors;sesc=' . $sc . '">' . $txt[163] . '</a> - <a href="' . $scripturl . '?action=maintain">' . $txt[164] . '</a></b>';
        } else {
            $context['raw_data'] .= '
						' . $txt['maintain_no_errors'] . '<br />
						<br />
						<a href="' . $scripturl . '?action=maintain">' . $txt['maintain_return'] . '</a>';
        }
        $context['raw_data'] .= '
					</td>
				</tr>
			</table>';
    } else {
        $to_fix = isset($_SESSION['repairboards_to_fix']) ? $_SESSION['repairboards_to_fix'] : array();
        require_once $sourcedir . '/Subs-Boards.php';
        // Get the MySQL version for future reference.
        $mysql_version = mysql_get_server_info($db_connection);
        if (empty($to_fix) || in_array('zero_ids', $to_fix)) {
            // We don't allow 0's in the IDs...
            db_query("\n\t\t\t\tUPDATE {$db_prefix}topics\n\t\t\t\tSET ID_TOPIC = NULL\n\t\t\t\tWHERE ID_TOPIC = 0", __FILE__, __LINE__);
            db_query("\n\t\t\t\tUPDATE {$db_prefix}messages\n\t\t\t\tSET ID_MSG = NULL\n\t\t\t\tWHERE ID_MSG = 0", __FILE__, __LINE__);
        }
        // Remove all topics that have zero messages in the messages table.
        if (empty($to_fix) || in_array('missing_messages', $to_fix)) {
            $resultTopic = db_query("\n\t\t\t\tSELECT t.ID_TOPIC, COUNT(m.ID_MSG) AS numMsg\n\t\t\t\tFROM {$db_prefix}topics AS t\n\t\t\t\t\tLEFT JOIN {$db_prefix}messages AS m ON (m.ID_TOPIC = t.ID_TOPIC)\n\t\t\t\tGROUP BY t.ID_TOPIC\n\t\t\t\tHAVING numMsg = 0", __FILE__, __LINE__);
            if (mysql_num_rows($resultTopic) > 0) {
                $stupidTopics = array();
                while ($topicArray = mysql_fetch_assoc($resultTopic)) {
                    $stupidTopics[] = $topicArray['ID_TOPIC'];
                }
                db_query("\n\t\t\t\t\tDELETE FROM {$db_prefix}topics\n\t\t\t\t\tWHERE ID_TOPIC IN (" . implode(',', $stupidTopics) . ')
					LIMIT ' . count($stupidTopics), __FILE__, __LINE__);
                db_query("\n\t\t\t\t\tDELETE FROM {$db_prefix}log_topics\n\t\t\t\t\tWHERE ID_TOPIC IN (" . implode(',', $stupidTopics) . ')', __FILE__, __LINE__);
            }
            mysql_free_result($resultTopic);
        }
        // Fix all messages that have a topic ID that cannot be found in the topics table.
        if (empty($to_fix) || in_array('missing_topics', $to_fix)) {
            $result = db_query("\n\t\t\t\tSELECT\n\t\t\t\t\tm.ID_BOARD, m.ID_TOPIC, MIN(m.ID_MSG) AS myID_FIRST_MSG, MAX(m.ID_MSG) AS myID_LAST_MSG,\n\t\t\t\t\tCOUNT(*) - 1 AS myNumReplies\n\t\t\t\tFROM {$db_prefix}messages AS m\n\t\t\t\t\tLEFT JOIN {$db_prefix}topics AS t ON (t.ID_TOPIC = m.ID_TOPIC)\n\t\t\t\tWHERE t.ID_TOPIC IS NULL\n\t\t\t\tGROUP BY m.ID_TOPIC", __FILE__, __LINE__);
            while ($row = mysql_fetch_assoc($result)) {
                // Only if we don't have a reasonable idea of where to put it.
                if ($row['ID_BOARD'] == 0) {
                    createSalvageArea();
                    $row['ID_BOARD'] = $salvageBoardID;
                }
                $memberStartedID = getMsgMemberID($row['myID_FIRST_MSG']);
                $memberUpdatedID = getMsgMemberID($row['myID_LAST_MSG']);
                db_query("\n\t\t\t\t\tINSERT INTO {$db_prefix}topics\n\t\t\t\t\t\t(ID_BOARD, ID_MEMBER_STARTED, ID_MEMBER_UPDATED, ID_FIRST_MSG, ID_LAST_MSG, numReplies)\n\t\t\t\t\tVALUES ({$row['ID_BOARD']}, {$memberStartedID}, {$memberUpdatedID},\n\t\t\t\t\t\t{$row['myID_FIRST_MSG']}, {$row['myID_LAST_MSG']}, {$row['myNumReplies']})", __FILE__, __LINE__);
                $newTopicID = db_insert_id();
                db_query("\n\t\t\t\t\tUPDATE {$db_prefix}messages\n\t\t\t\t\tSET ID_TOPIC = {$newTopicID}, ID_BOARD = {$row['ID_BOARD']}\n\t\t\t\t\tWHERE ID_TOPIC = {$row['ID_TOPIC']}", __FILE__, __LINE__);
            }
            mysql_free_result($result);
        }
        // Fix all ID_FIRST_MSG, ID_LAST_MSG and numReplies in the topic table.
        if (empty($to_fix) || in_array('stats_topics', $to_fix)) {
            $resultTopic = db_query("\n\t\t\t\tSELECT\n\t\t\t\t\tt.ID_TOPIC, MIN(m.ID_MSG) AS myID_FIRST_MSG, t.ID_FIRST_MSG,\n\t\t\t\t\tMAX(m.ID_MSG) AS myID_LAST_MSG, t.ID_LAST_MSG, COUNT(m.ID_MSG) - 1 AS myNumReplies,\n\t\t\t\t\tt.numReplies\n\t\t\t\tFROM {$db_prefix}topics AS t\n\t\t\t\t\tLEFT JOIN {$db_prefix}messages AS m ON (m.ID_TOPIC = t.ID_TOPIC)\n\t\t\t\tGROUP BY t.ID_TOPIC\n\t\t\t\tHAVING ID_FIRST_MSG != myID_FIRST_MSG OR ID_LAST_MSG != myID_LAST_MSG OR numReplies != myNumReplies", __FILE__, __LINE__);
            while ($topicArray = mysql_fetch_assoc($resultTopic)) {
                $memberStartedID = getMsgMemberID($topicArray['myID_FIRST_MSG']);
                $memberUpdatedID = getMsgMemberID($topicArray['myID_LAST_MSG']);
                db_query("\n\t\t\t\t\tUPDATE {$db_prefix}topics\n\t\t\t\t\tSET ID_FIRST_MSG = '{$topicArray['myID_FIRST_MSG']}',\n\t\t\t\t\t\tID_MEMBER_STARTED = '{$memberStartedID}', ID_LAST_MSG = '{$topicArray['myID_LAST_MSG']}',\n\t\t\t\t\t\tID_MEMBER_UPDATED = '{$memberUpdatedID}', numReplies = '{$topicArray['myNumReplies']}'\n\t\t\t\t\tWHERE ID_TOPIC = {$topicArray['ID_TOPIC']}\n\t\t\t\t\tLIMIT 1", __FILE__, __LINE__);
            }
            mysql_free_result($resultTopic);
        }
        // Fix all topics that have a board ID that cannot be found in the boards table.
        if (empty($to_fix) || in_array('missing_boards', $to_fix)) {
            $resultTopics = db_query("\n\t\t\t\tSELECT t.ID_BOARD, COUNT(*) AS myNumTopics, COUNT(m.ID_MSG) AS myNumPosts\n\t\t\t\tFROM {$db_prefix}topics AS t\n\t\t\t\t\tLEFT JOIN {$db_prefix}boards AS b ON (b.ID_BOARD = t.ID_BOARD)\n\t\t\t\t\tLEFT JOIN {$db_prefix}messages AS m ON (m.ID_TOPIC = t.ID_TOPIC)\n\t\t\t\tWHERE b.ID_BOARD IS NULL\n\t\t\t\tGROUP BY t.ID_BOARD", __FILE__, __LINE__);
            if (mysql_num_rows($resultTopics) > 0) {
                createSalvageArea();
            }
            while ($topicArray = mysql_fetch_assoc($resultTopics)) {
                db_query("\n\t\t\t\t\tINSERT INTO {$db_prefix}boards\n\t\t\t\t\t\t(ID_CAT, name, description, numTopics, numPosts, memberGroups)\n\t\t\t\t\tVALUES ({$salvageCatID}, 'Salvaged board', '', {$topicArray['myNumTopics']}, {$topicArray['myNumPosts']}, '1')", __FILE__, __LINE__);
                $newBoardID = db_insert_id();
                db_query("\n\t\t\t\t\tUPDATE {$db_prefix}topics\n\t\t\t\t\tSET ID_BOARD = {$newBoardID}\n\t\t\t\t\tWHERE ID_BOARD = {$topicArray['ID_BOARD']}", __FILE__, __LINE__);
                db_query("\n\t\t\t\t\tUPDATE {$db_prefix}messages\n\t\t\t\t\tSET ID_BOARD = {$newBoardID}\n\t\t\t\t\tWHERE ID_BOARD = {$topicArray['ID_BOARD']}", __FILE__, __LINE__);
            }
            mysql_free_result($resultTopics);
        }
        // Fix all boards that have a cat ID that cannot be found in the cats table.
        if (empty($to_fix) || in_array('missing_categories', $to_fix)) {
            $resultBoards = db_query("\n\t\t\t\tSELECT b.ID_CAT\n\t\t\t\tFROM {$db_prefix}boards AS b\n\t\t\t\t\tLEFT JOIN {$db_prefix}categories AS c ON (c.ID_CAT = b.ID_CAT)\n\t\t\t\tWHERE c.ID_CAT IS NULL\n\t\t\t\tGROUP BY b.ID_CAT", __FILE__, __LINE__);
            if (mysql_num_rows($resultBoards) > 0) {
                createSalvageArea();
            }
            while ($boardArray = mysql_fetch_assoc($resultBoards)) {
                db_query("\n\t\t\t\t\tUPDATE {$db_prefix}boards\n\t\t\t\t\tSET ID_CAT = {$salvageCatID}\n\t\t\t\t\tWHERE ID_CAT = {$boardArray['ID_CAT']}", __FILE__, __LINE__);
            }
            mysql_free_result($resultBoards);
        }
        // Last step-make sure all non-guest posters still exist.
        if (empty($to_fix) || in_array('missing_posters', $to_fix)) {
            $result = db_query("\n\t\t\t\tSELECT m.ID_MSG\n\t\t\t\tFROM {$db_prefix}messages AS m\n\t\t\t\t\tLEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = m.ID_MEMBER)\n\t\t\t\tWHERE m.ID_MEMBER != 0\n\t\t\t\t\tAND mem.ID_MEMBER IS NULL", __FILE__, __LINE__);
            if (mysql_num_rows($result) > 0) {
                $guestMessages = array();
                while ($row = mysql_fetch_assoc($result)) {
                    $guestMessages[] = $row['ID_MSG'];
                }
                db_query("\n\t\t\t\t\tUPDATE {$db_prefix}messages\n\t\t\t\t\tSET ID_MEMBER = 0\n\t\t\t\t\tWHERE ID_MSG IN (" . implode(',', $guestMessages) . ')
					LIMIT ' . count($guestMessages), __FILE__, __LINE__);
            }
            mysql_free_result($result);
        }
        // Fix all boards that have a parent ID that cannot be found in the boards table.
        if (empty($to_fix) || in_array('missing_parents', $to_fix)) {
            $resultParents = db_query("\n\t\t\t\tSELECT b.ID_PARENT\n\t\t\t\tFROM {$db_prefix}boards AS b\n\t\t\t\t\tLEFT JOIN {$db_prefix}boards AS p ON (p.ID_BOARD = b.ID_PARENT)\n\t\t\t\tWHERE b.ID_PARENT != 0\n\t\t\t\t\tAND (p.ID_BOARD IS NULL OR p.ID_BOARD = b.ID_BOARD)\n\t\t\t\tGROUP BY b.ID_PARENT", __FILE__, __LINE__);
            if (mysql_num_rows($resultParents) > 0) {
                createSalvageArea();
            }
            while ($parentArray = mysql_fetch_assoc($resultParents)) {
                db_query("\n\t\t\t\t\tUPDATE {$db_prefix}boards\n\t\t\t\t\tSET ID_PARENT = {$salvageBoardID}, ID_CAT = {$salvageCatID}, childLevel = 1\n\t\t\t\t\tWHERE ID_PARENT = {$parentArray['ID_PARENT']}", __FILE__, __LINE__);
            }
            mysql_free_result($resultParents);
        }
        if (empty($to_fix) || in_array('missing_polls', $to_fix)) {
            if (version_compare($mysql_version, '4.0.4') >= 0) {
                db_query("\n\t\t\t\t\tUPDATE {$db_prefix}topics AS t\n\t\t\t\t\t\tLEFT JOIN {$db_prefix}polls AS p ON (p.ID_POLL = t.ID_POLL)\n\t\t\t\t\tSET t.ID_POLL = 0\n\t\t\t\t\tWHERE t.ID_POLL != 0\n\t\t\t\t\t\tAND p.ID_POLL IS NULL", __FILE__, __LINE__);
            } else {
                $resultPolls = db_query("\n\t\t\t\t\tSELECT t.ID_POLL\n\t\t\t\t\tFROM {$db_prefix}topics AS t\n\t\t\t\t\t\tLEFT JOIN {$db_prefix}polls AS p ON (p.ID_POLL = t.ID_POLL)\n\t\t\t\t\tWHERE t.ID_POLL != 0\n\t\t\t\t\t\tAND p.ID_POLL IS NULL\n\t\t\t\t\tGROUP BY t.ID_POLL", __FILE__, __LINE__);
                $polls = array();
                while ($rowPolls = mysql_fetch_assoc($resultPolls)) {
                    $polls[] = $rowPolls['ID_POLL'];
                }
                mysql_free_result($resultPolls);
                if (!empty($polls)) {
                    db_query("\n\t\t\t\t\t\tUPDATE {$db_prefix}topics\n\t\t\t\t\t\tSET ID_POLL = 0\n\t\t\t\t\t\tWHERE ID_POLL IN (" . implode(', ', $polls) . ")\n\t\t\t\t\t\tLIMIT " . count($polls), __FILE__, __LINE__);
                }
            }
        }
        if (empty($to_fix) || in_array('missing_calendar_topics', $to_fix)) {
            if (version_compare($mysql_version, '4.0.4') >= 0) {
                db_query("\n\t\t\t\t\tUPDATE {$db_prefix}calendar AS cal\n\t\t\t\t\t\tLEFT JOIN {$db_prefix}topics AS t ON (t.ID_TOPIC = cal.ID_TOPIC)\n\t\t\t\t\tSET cal.ID_BOARD = 0, cal.ID_TOPIC = 0\n\t\t\t\t\tWHERE cal.ID_TOPIC != 0\n\t\t\t\t\t\tAND t.ID_TOPIC IS NULL", __FILE__, __LINE__);
            } else {
                $resultEvents = db_query("\n\t\t\t\t\tSELECT cal.ID_TOPIC\n\t\t\t\t\tFROM {$db_prefix}calendar AS cal\n\t\t\t\t\t\tLEFT JOIN {$db_prefix}topics AS t ON (t.ID_TOPIC = cal.ID_TOPIC)\n\t\t\t\t\tWHERE cal.ID_TOPIC != 0\n\t\t\t\t\t\tAND t.ID_TOPIC IS NULL\n\t\t\t\t\tGROUP BY cal.ID_TOPIC", __FILE__, __LINE__);
                $events = array();
                while ($rowEvents = mysql_fetch_assoc($resultEvents)) {
                    $events[] = $rowEvents['ID_TOPIC'];
                }
                mysql_free_result($resultEvents);
                if (!empty($events)) {
                    db_query("\n\t\t\t\t\t\tUPDATE {$db_prefix}calendar\n\t\t\t\t\t\tSET ID_TOPIC = 0, ID_BOARD = 0\n\t\t\t\t\t\tWHERE ID_TOPIC IN (" . implode(', ', $events) . ")\n\t\t\t\t\t\tLIMIT " . count($events), __FILE__, __LINE__);
                }
            }
        }
        if (empty($to_fix) || in_array('missing_log_topics', $to_fix)) {
            $result = db_query("\n\t\t\t\tSELECT lt.ID_TOPIC\n\t\t\t\tFROM {$db_prefix}log_topics AS lt\n\t\t\t\t\tLEFT JOIN {$db_prefix}topics AS t ON (t.ID_TOPIC = lt.ID_TOPIC)\n\t\t\t\tWHERE t.ID_TOPIC IS NULL\n\t\t\t\tGROUP BY lt.ID_TOPIC", __FILE__, __LINE__);
            $topics = array();
            while ($row = mysql_fetch_assoc($result)) {
                $topics[] = $row['ID_TOPIC'];
            }
            mysql_free_result($result);
            if (!empty($topics)) {
                db_query("\n\t\t\t\t\tDELETE FROM {$db_prefix}log_topics\n\t\t\t\t\tWHERE ID_TOPIC IN (" . implode(', ', $topics) . ")", __FILE__, __LINE__);
            }
        }
        if (empty($to_fix) || in_array('missing_log_topics_members', $to_fix)) {
            $result = db_query("\n\t\t\t\tSELECT lt.ID_MEMBER\n\t\t\t\tFROM {$db_prefix}log_topics AS lt\n\t\t\t\t\tLEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = lt.ID_MEMBER)\n\t\t\t\tWHERE mem.ID_MEMBER IS NULL\n\t\t\t\tGROUP BY lt.ID_MEMBER", __FILE__, __LINE__);
            $members = array();
            while ($row = mysql_fetch_assoc($result)) {
                $members[] = $row['ID_MEMBER'];
            }
            mysql_free_result($result);
            if (!empty($members)) {
                db_query("\n\t\t\t\t\tDELETE FROM {$db_prefix}log_topics\n\t\t\t\t\tWHERE ID_MEMBER IN (" . implode(', ', $members) . ")", __FILE__, __LINE__);
            }
        }
        if (empty($to_fix) || in_array('missing_log_boards', $to_fix)) {
            $result = db_query("\n\t\t\t\tSELECT lb.ID_BOARD\n\t\t\t\tFROM {$db_prefix}log_boards AS lb\n\t\t\t\t\tLEFT JOIN {$db_prefix}boards AS b ON (b.ID_BOARD = lb.ID_BOARD)\n\t\t\t\tWHERE b.ID_BOARD IS NULL\n\t\t\t\tGROUP BY lb.ID_BOARD", __FILE__, __LINE__);
            $boards = array();
            while ($row = mysql_fetch_assoc($result)) {
                $boards[] = $row['ID_BOARD'];
            }
            mysql_free_result($result);
            if (!empty($boards)) {
                db_query("\n\t\t\t\t\tDELETE FROM {$db_prefix}log_boards\n\t\t\t\t\tWHERE ID_BOARD IN (" . implode(', ', $boards) . ")", __FILE__, __LINE__);
            }
        }
        if (empty($to_fix) || in_array('missing_log_boards_members', $to_fix)) {
            $result = db_query("\n\t\t\t\tSELECT lb.ID_MEMBER\n\t\t\t\tFROM {$db_prefix}log_boards AS lb\n\t\t\t\t\tLEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = lb.ID_MEMBER)\n\t\t\t\tWHERE mem.ID_MEMBER IS NULL\n\t\t\t\tGROUP BY lb.ID_MEMBER", __FILE__, __LINE__);
            $members = array();
            while ($row = mysql_fetch_assoc($result)) {
                $members[] = $row['ID_MEMBER'];
            }
            mysql_free_result($result);
            if (!empty($members)) {
                db_query("\n\t\t\t\t\tDELETE FROM {$db_prefix}log_boards\n\t\t\t\t\tWHERE ID_MEMBER IN (" . implode(', ', $members) . ")", __FILE__, __LINE__);
            }
        }
        if (empty($to_fix) || in_array('missing_log_mark_read', $to_fix)) {
            $result = db_query("\n\t\t\t\tSELECT lmr.ID_BOARD\n\t\t\t\tFROM {$db_prefix}log_mark_read AS lmr\n\t\t\t\t\tLEFT JOIN {$db_prefix}boards AS b ON (b.ID_BOARD = lmr.ID_BOARD)\n\t\t\t\tWHERE b.ID_BOARD IS NULL\n\t\t\t\tGROUP BY lmr.ID_BOARD", __FILE__, __LINE__);
            $boards = array();
            while ($row = mysql_fetch_assoc($result)) {
                $boards[] = $row['ID_BOARD'];
            }
            mysql_free_result($result);
            if (!empty($boards)) {
                db_query("\n\t\t\t\t\tDELETE FROM {$db_prefix}log_mark_read\n\t\t\t\t\tWHERE ID_BOARD IN (" . implode(', ', $boards) . ")", __FILE__, __LINE__);
            }
        }
        if (empty($to_fix) || in_array('missing_log_mark_read_members', $to_fix)) {
            $result = db_query("\n\t\t\t\tSELECT lmr.ID_MEMBER\n\t\t\t\tFROM {$db_prefix}log_mark_read AS lmr\n\t\t\t\t\tLEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = lmr.ID_MEMBER)\n\t\t\t\tWHERE mem.ID_MEMBER IS NULL\n\t\t\t\tGROUP BY lmr.ID_MEMBER", __FILE__, __LINE__);
            $members = array();
            while ($row = mysql_fetch_assoc($result)) {
                $members[] = $row['ID_MEMBER'];
            }
            mysql_free_result($result);
            if (!empty($members)) {
                db_query("\n\t\t\t\t\tDELETE FROM {$db_prefix}log_mark_read\n\t\t\t\t\tWHERE ID_MEMBER IN (" . implode(', ', $members) . ")", __FILE__, __LINE__);
            }
        }
        if (empty($to_fix) || in_array('missing_pms', $to_fix)) {
            $result = db_query("\n\t\t\t\tSELECT pmr.ID_PM\n\t\t\t\tFROM {$db_prefix}pm_recipients AS pmr\n\t\t\t\t\tLEFT JOIN {$db_prefix}personal_messages AS pm ON (pm.ID_PM = pmr.ID_PM)\n\t\t\t\tWHERE pm.ID_PM IS NULL\n\t\t\t\tGROUP BY pmr.ID_PM", __FILE__, __LINE__);
            $pms = array();
            while ($row = mysql_fetch_assoc($result)) {
                $pms[] = $row['ID_PM'];
            }
            mysql_free_result($result);
            if (!empty($pms)) {
                db_query("\n\t\t\t\t\tDELETE FROM {$db_prefix}pm_recipients\n\t\t\t\t\tWHERE ID_PM IN (" . implode(', ', $pms) . ")", __FILE__, __LINE__);
            }
        }
        if (empty($to_fix) || in_array('missing_recipients', $to_fix)) {
            $result = db_query("\n\t\t\t\tSELECT pmr.ID_MEMBER\n\t\t\t\tFROM {$db_prefix}pm_recipients AS pmr\n\t\t\t\t\tLEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = pmr.ID_MEMBER)\n\t\t\t\tWHERE pmr.ID_MEMBER != 0\n\t\t\t\t\tAND mem.ID_MEMBER IS NULL\n\t\t\t\tGROUP BY pmr.ID_MEMBER", __FILE__, __LINE__);
            $members = array();
            while ($row = mysql_fetch_assoc($result)) {
                $members[] = $row['ID_MEMBER'];
            }
            mysql_free_result($result);
            if (!empty($members)) {
                db_query("\n\t\t\t\t\tDELETE FROM {$db_prefix}pm_recipients\n\t\t\t\t\tWHERE ID_MEMBER IN (" . implode(', ', $members) . ")", __FILE__, __LINE__);
            }
        }
        if (empty($to_fix) || in_array('missing_senders', $to_fix)) {
            $result = db_query("\n\t\t\t\tSELECT pm.ID_PM\n\t\t\t\tFROM {$db_prefix}personal_messages AS pm\n\t\t\t\t\tLEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = pm.ID_MEMBER_FROM)\n\t\t\t\tWHERE pm.ID_MEMBER_FROM != 0\n\t\t\t\t\tAND mem.ID_MEMBER IS NULL", __FILE__, __LINE__);
            if (mysql_num_rows($result) > 0) {
                $guestMessages = array();
                while ($row = mysql_fetch_assoc($result)) {
                    $guestMessages[] = $row['ID_PM'];
                }
                db_query("\n\t\t\t\t\tUPDATE {$db_prefix}personal_messages\n\t\t\t\t\tSET ID_MEMBER_FROM = 0\n\t\t\t\t\tWHERE ID_PM IN (" . implode(',', $guestMessages) . ')
					LIMIT ' . count($guestMessages), __FILE__, __LINE__);
            }
            mysql_free_result($result);
        }
        if (empty($to_fix) || in_array('missing_notify_members', $to_fix)) {
            $result = db_query("\n\t\t\t\tSELECT ln.ID_MEMBER\n\t\t\t\tFROM {$db_prefix}log_notify AS ln\n\t\t\t\t\tLEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = ln.ID_MEMBER)\n\t\t\t\tWHERE mem.ID_MEMBER IS NULL\n\t\t\t\tGROUP BY ln.ID_MEMBER", __FILE__, __LINE__);
            $members = array();
            while ($row = mysql_fetch_assoc($result)) {
                $members[] = $row['ID_MEMBER'];
            }
            mysql_free_result($result);
            if (!empty($members)) {
                db_query("\n\t\t\t\t\tDELETE FROM {$db_prefix}log_notify\n\t\t\t\t\tWHERE ID_MEMBER IN (" . implode(', ', $members) . ")", __FILE__, __LINE__);
            }
        }
        if (empty($to_fix) || in_array('missing_cached_subject', $to_fix)) {
            $request = db_query("\n\t\t\t\tSELECT t.ID_TOPIC, m.subject\n\t\t\t\tFROM ({$db_prefix}topics AS t, {$db_prefix}messages AS m)\n\t\t\t\t\tLEFT JOIN {$db_prefix}log_search_subjects AS lss ON (lss.ID_TOPIC = t.ID_TOPIC)\n\t\t\t\tWHERE m.ID_MSG = t.ID_FIRST_MSG\n\t\t\t\t\tAND lss.ID_TOPIC IS NULL", __FILE__, __LINE__);
            $insertRows = array();
            while ($row = mysql_fetch_assoc($request)) {
                foreach (text2words($row['subject']) as $word) {
                    $insertRows[] = "'{$word}', {$row['ID_TOPIC']}";
                }
                if (count($insertRows) > 500) {
                    db_query("\n\t\t\t\t\t\tINSERT IGNORE INTO {$db_prefix}log_search_subjects\n\t\t\t\t\t\t\t(word, ID_TOPIC)\n\t\t\t\t\t\tVALUES (" . implode('),
							(', $insertRows) . ")", __FILE__, __LINE__);
                    $insertRows = array();
                }
            }
            mysql_free_result($request);
            if (!empty($insertRows)) {
                db_query("\n\t\t\t\t\tINSERT IGNORE INTO {$db_prefix}log_search_subjects\n\t\t\t\t\t\t(word, ID_TOPIC)\n\t\t\t\t\tVALUES (" . implode('),
						(', $insertRows) . ")", __FILE__, __LINE__);
            }
        }
        if (empty($to_fix) || in_array('missing_topic_for_cache', $to_fix)) {
            $request = db_query("\n\t\t\t\tSELECT lss.ID_TOPIC\n\t\t\t\tFROM {$db_prefix}log_search_subjects AS lss\n\t\t\t\t\tLEFT JOIN {$db_prefix}topics AS t ON (t.ID_TOPIC = lss.ID_TOPIC)\n\t\t\t\tWHERE t.ID_TOPIC IS NULL\n\t\t\t\tGROUP BY lss.ID_TOPIC", __FILE__, __LINE__);
            $deleteTopics = array();
            while ($row = mysql_fetch_assoc($request)) {
                $deleteTopics[] = $row['ID_TOPIC'];
            }
            mysql_free_result($request);
            if (!empty($deleteTopics)) {
                db_query("\n\t\t\t\t\tDELETE FROM {$db_prefix}log_search_subjects\n\t\t\t\t\tWHERE ID_TOPIC IN (" . implode(', ', $deleteTopics) . ')', __FILE__, __LINE__);
            }
        }
        if (empty($to_fix) || in_array('missing_member_vote', $to_fix)) {
            $result = db_query("\n\t\t\t\tSELECT lp.ID_MEMBER\n\t\t\t\tFROM {$db_prefix}log_polls AS lp\n\t\t\t\t\tLEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = lp.ID_MEMBER)\n\t\t\t\tWHERE mem.ID_MEMBER IS NULL\n\t\t\t\tGROUP BY lp.ID_MEMBER", __FILE__, __LINE__);
            $members = array();
            while ($row = mysql_fetch_assoc($result)) {
                $members[] = $row['ID_MEMBER'];
            }
            mysql_free_result($result);
            if (!empty($members)) {
                db_query("\n\t\t\t\t\tDELETE FROM {$db_prefix}log_polls\n\t\t\t\t\tWHERE ID_MEMBER IN (" . implode(', ', $members) . ")", __FILE__, __LINE__);
            }
        }
        if (empty($to_fix) || in_array('missing_log_poll_vote', $to_fix)) {
            $request = db_query("\n\t\t\t\tSELECT lp.ID_POLL\n\t\t\t\tFROM {$db_prefix}log_polls AS lp\n\t\t\t\t\tLEFT JOIN {$db_prefix}polls AS p ON (p.ID_POLL = lp.ID_POLL)\n\t\t\t\tWHERE p.ID_POLL IS NULL\n\t\t\t\tGROUP BY lp.ID_POLL", __FILE__, __LINE__);
            $polls = array();
            while ($row = mysql_fetch_assoc($request)) {
                $polls[] = $row['ID_POLL'];
            }
            mysql_free_result($request);
            if (!empty($polls)) {
                db_query("\n\t\t\t\t\tDELETE FROM {$db_prefix}log_polls\n\t\t\t\t\tWHERE ID_POLL IN (" . implode(', ', $polls) . ")", __FILE__, __LINE__);
            }
        }
        updateStats('message');
        updateStats('topic');
        updateStats('calendar');
        $context['raw_data'] = '
			<table width="100%" border="0" cellspacing="0" cellpadding="4" class="tborder">
				<tr class="titlebg">
					<td>' . $txt['smf86'] . '</td>
				</tr><tr>
					<td class="windowbg">
						' . $txt['smf92'] . '<br />
						<br />
						<a href="' . $scripturl . '?action=maintain">' . $txt['maintain_return'] . '</a>
					</td>
				</tr>
			</table>';
        $_SESSION['repairboards_to_fix'] = null;
        $_SESSION['repairboards_to_fix2'] = null;
    }
}
Esempio n. 3
0
/**
 * General function to split off a topic.
 * creates a new topic and moves the messages with the IDs in
 * array messagesToBeSplit to the new topic.
 * the subject of the newly created topic is set to 'newSubject'.
 * marks the newly created message as read for the user splitting it.
 * updates the statistics to reflect a newly created topic.
 * logs the action in the moderation log.
 * a notification is sent to all users monitoring this topic.
 *
 * @param int $split1_ID_TOPIC
 * @param int[] $splitMessages
 * @param string $new_subject
 * @return int the topic ID of the new split topic.
 */
function splitTopic($split1_ID_TOPIC, $splitMessages, $new_subject)
{
    global $txt;
    $db = database();
    // Nothing to split?
    if (empty($splitMessages)) {
        fatal_lang_error('no_posts_selected', false);
    }
    // Get some board info.
    $request = $db->query('', '
		SELECT id_board, approved
		FROM {db_prefix}topics
		WHERE id_topic = {int:id_topic}
		LIMIT 1', array('id_topic' => $split1_ID_TOPIC));
    list($id_board, $split1_approved) = $db->fetch_row($request);
    $db->free_result($request);
    // Find the new first and last not in the list. (old topic)
    $request = $db->query('', '
		SELECT
			MIN(m.id_msg) AS myid_first_msg, MAX(m.id_msg) AS myid_last_msg, COUNT(*) AS message_count, m.approved
		FROM {db_prefix}messages AS m
			INNER JOIN {db_prefix}topics AS t ON (t.id_topic = {int:id_topic})
		WHERE m.id_msg NOT IN ({array_int:no_msg_list})
			AND m.id_topic = {int:id_topic}
		GROUP BY m.approved
		ORDER BY m.approved DESC
		LIMIT 2', array('id_topic' => $split1_ID_TOPIC, 'no_msg_list' => $splitMessages));
    // You can't select ALL the messages!
    if ($db->num_rows($request) == 0) {
        fatal_lang_error('selected_all_posts', false);
    }
    $split1_first_msg = null;
    $split1_last_msg = null;
    while ($row = $db->fetch_assoc($request)) {
        // Get the right first and last message dependant on approved state...
        if (empty($split1_first_msg) || $row['myid_first_msg'] < $split1_first_msg) {
            $split1_first_msg = $row['myid_first_msg'];
        }
        if (empty($split1_last_msg) || $row['approved']) {
            $split1_last_msg = $row['myid_last_msg'];
        }
        // Get the counts correct...
        if ($row['approved']) {
            $split1_replies = $row['message_count'] - 1;
            $split1_unapprovedposts = 0;
        } else {
            if (!isset($split1_replies)) {
                $split1_replies = 0;
            } elseif (!$split1_approved) {
                $split1_replies++;
            }
            $split1_unapprovedposts = $row['message_count'];
        }
    }
    $db->free_result($request);
    $split1_firstMem = getMsgMemberID($split1_first_msg);
    $split1_lastMem = getMsgMemberID($split1_last_msg);
    // Find the first and last in the list. (new topic)
    $request = $db->query('', '
		SELECT MIN(id_msg) AS myid_first_msg, MAX(id_msg) AS myid_last_msg, COUNT(*) AS message_count, approved
		FROM {db_prefix}messages
		WHERE id_msg IN ({array_int:msg_list})
			AND id_topic = {int:id_topic}
		GROUP BY id_topic, approved
		ORDER BY approved DESC
		LIMIT 2', array('msg_list' => $splitMessages, 'id_topic' => $split1_ID_TOPIC));
    while ($row = $db->fetch_assoc($request)) {
        // As before get the right first and last message dependant on approved state...
        if (empty($split2_first_msg) || $row['myid_first_msg'] < $split2_first_msg) {
            $split2_first_msg = $row['myid_first_msg'];
        }
        if (empty($split2_last_msg) || $row['approved']) {
            $split2_last_msg = $row['myid_last_msg'];
        }
        // Then do the counts again...
        if ($row['approved']) {
            $split2_approved = true;
            $split2_replies = $row['message_count'] - 1;
            $split2_unapprovedposts = 0;
        } else {
            // Should this one be approved??
            if ($split2_first_msg == $row['myid_first_msg']) {
                $split2_approved = false;
            }
            if (!isset($split2_replies)) {
                $split2_replies = 0;
            } elseif (!$split2_approved) {
                $split2_replies++;
            }
            $split2_unapprovedposts = $row['message_count'];
        }
    }
    $db->free_result($request);
    $split2_firstMem = getMsgMemberID($split2_first_msg);
    $split2_lastMem = getMsgMemberID($split2_last_msg);
    // No database changes yet, so let's double check to see if everything makes at least a little sense.
    if ($split1_first_msg <= 0 || $split1_last_msg <= 0 || $split2_first_msg <= 0 || $split2_last_msg <= 0 || $split1_replies < 0 || $split2_replies < 0 || $split1_unapprovedposts < 0 || $split2_unapprovedposts < 0 || !isset($split1_approved) || !isset($split2_approved)) {
        fatal_lang_error('cant_find_messages');
    }
    // You cannot split off the first message of a topic.
    if ($split1_first_msg > $split2_first_msg) {
        fatal_lang_error('split_first_post', false);
    }
    // We're off to insert the new topic!  Use 0 for now to avoid UNIQUE errors.
    $db->insert('', '{db_prefix}topics', array('id_board' => 'int', 'id_member_started' => 'int', 'id_member_updated' => 'int', 'id_first_msg' => 'int', 'id_last_msg' => 'int', 'num_replies' => 'int', 'unapproved_posts' => 'int', 'approved' => 'int', 'is_sticky' => 'int'), array((int) $id_board, $split2_firstMem, $split2_lastMem, 0, 0, $split2_replies, $split2_unapprovedposts, (int) $split2_approved, 0), array('id_topic'));
    $split2_ID_TOPIC = $db->insert_id('{db_prefix}topics', 'id_topic');
    if ($split2_ID_TOPIC <= 0) {
        fatal_lang_error('cant_insert_topic');
    }
    // Move the messages over to the other topic.
    $new_subject = strtr(Util::htmltrim(Util::htmlspecialchars($new_subject)), array("\r" => '', "\n" => '', "\t" => ''));
    // Check the subject length.
    if (Util::strlen($new_subject) > 100) {
        $new_subject = Util::substr($new_subject, 0, 100);
    }
    // Valid subject?
    if ($new_subject != '') {
        $db->query('', '
			UPDATE {db_prefix}messages
			SET
				id_topic = {int:id_topic},
				subject = CASE WHEN id_msg = {int:split_first_msg} THEN {string:new_subject} ELSE {string:new_subject_replies} END
			WHERE id_msg IN ({array_int:split_msgs})', array('split_msgs' => $splitMessages, 'id_topic' => $split2_ID_TOPIC, 'new_subject' => $new_subject, 'split_first_msg' => $split2_first_msg, 'new_subject_replies' => $txt['response_prefix'] . $new_subject));
        // Cache the new topics subject... we can do it now as all the subjects are the same!
        updateStats('subject', $split2_ID_TOPIC, $new_subject);
    }
    // Any associated reported posts better follow...
    require_once SUBSDIR . '/Topic.subs.php';
    updateSplitTopics(array('splitMessages' => $splitMessages, 'split2_ID_TOPIC' => $split2_ID_TOPIC, 'split1_replies' => $split1_replies, 'split1_first_msg' => $split1_first_msg, 'split1_last_msg' => $split1_last_msg, 'split1_firstMem' => $split1_firstMem, 'split1_lastMem' => $split1_lastMem, 'split1_unapprovedposts' => $split1_unapprovedposts, 'split1_ID_TOPIC' => $split1_ID_TOPIC, 'split2_first_msg' => $split2_first_msg, 'split2_last_msg' => $split2_last_msg, 'split2_ID_TOPIC' => $split2_ID_TOPIC, 'split2_approved' => $split2_approved), $id_board);
    require_once SUBSDIR . '/FollowUps.subs.php';
    // Let's see if we can create a stronger bridge between the two topics
    // @todo not sure what message from the oldest topic I should link to the new one, so I'll go with the first
    linkMessages($split1_first_msg, $split2_ID_TOPIC);
    // Copy log topic entries.
    // @todo This should really be chunked.
    $request = $db->query('', '
		SELECT id_member, id_msg, unwatched
		FROM {db_prefix}log_topics
		WHERE id_topic = {int:id_topic}', array('id_topic' => (int) $split1_ID_TOPIC));
    if ($db->num_rows($request) > 0) {
        $replaceEntries = array();
        while ($row = $db->fetch_assoc($request)) {
            $replaceEntries[] = array($row['id_member'], $split2_ID_TOPIC, $row['id_msg'], $row['unwatched']);
        }
        require_once SUBSDIR . '/Topic.subs.php';
        markTopicsRead($replaceEntries, false);
        unset($replaceEntries);
    }
    $db->free_result($request);
    // Housekeeping.
    updateTopicStats();
    updateLastMessages($id_board);
    logAction('split', array('topic' => $split1_ID_TOPIC, 'new_topic' => $split2_ID_TOPIC, 'board' => $id_board));
    // Notify people that this topic has been split?
    require_once SUBSDIR . '/Notification.subs.php';
    sendNotifications($split1_ID_TOPIC, 'split');
    // If there's a search index that needs updating, update it...
    require_once SUBSDIR . '/Search.subs.php';
    $searchAPI = findSearchAPI();
    if (is_callable(array($searchAPI, 'topicSplit'))) {
        $searchAPI->topicSplit($split2_ID_TOPIC, $splitMessages);
    }
    // Return the ID of the newly created topic.
    return $split2_ID_TOPIC;
}
Esempio n. 4
0
/**
	int splitTopic(int topicID, array messagesToBeSplit, string newSubject)
* general function to split off a topic.
* creates a new topic and moves the messages with the IDs in
* array messagesToBeSplit to the new topic.
* the subject of the newly created topic is set to 'newSubject'.
* marks the newly created message as read for the user splitting it.
* updates the statistics to reflect a newly created topic.
* logs the action in the moderation log.
* a notification is sent to all users monitoring this topic.
* @param int $split1_ID_TOPIC
* @param array $splitMessages
* @param string $new_subject
* @return int the topic ID of the new split topic.
*/
function splitTopic($split1_ID_TOPIC, $splitMessages, $new_subject)
{
    global $user_info, $topic, $board, $modSettings, $smcFunc, $txt, $sourcedir;
    // Nothing to split?
    if (empty($splitMessages)) {
        fatal_lang_error('no_posts_selected', false);
    }
    // Get some board info.
    $request = $smcFunc['db_query']('', '
		SELECT id_board, approved
		FROM {db_prefix}topics
		WHERE id_topic = {int:id_topic}
		LIMIT 1', array('id_topic' => $split1_ID_TOPIC));
    list($id_board, $split1_approved) = $smcFunc['db_fetch_row']($request);
    $smcFunc['db_free_result']($request);
    // Find the new first and last not in the list. (old topic)
    $request = $smcFunc['db_query']('', '
		SELECT
			MIN(m.id_msg) AS myid_first_msg, MAX(m.id_msg) AS myid_last_msg, COUNT(*) AS message_count, m.approved
		FROM {db_prefix}messages AS m
			INNER JOIN {db_prefix}topics AS t ON (t.id_topic = {int:id_topic})
		WHERE m.id_msg NOT IN ({array_int:no_msg_list})
			AND m.id_topic = {int:id_topic}
		GROUP BY m.approved
		ORDER BY m.approved DESC
		LIMIT 2', array('id_topic' => $split1_ID_TOPIC, 'no_msg_list' => $splitMessages));
    // You can't select ALL the messages!
    if ($smcFunc['db_num_rows']($request) == 0) {
        fatal_lang_error('selected_all_posts', false);
    }
    $split1_first_msg = null;
    $split1_last_msg = null;
    while ($row = $smcFunc['db_fetch_assoc']($request)) {
        // Get the right first and last message dependant on approved state...
        if (empty($split1_first_msg) || $row['myid_first_msg'] < $split1_first_msg) {
            $split1_first_msg = $row['myid_first_msg'];
        }
        if (empty($split1_last_msg) || $row['approved']) {
            $split1_last_msg = $row['myid_last_msg'];
        }
        // Get the counts correct...
        if ($row['approved']) {
            $split1_replies = $row['message_count'] - 1;
            $split1_unapprovedposts = 0;
        } else {
            if (!isset($split1_replies)) {
                $split1_replies = 0;
            } elseif (!$split1_approved) {
                $split1_replies++;
            }
            $split1_unapprovedposts = $row['message_count'];
        }
    }
    $smcFunc['db_free_result']($request);
    $split1_firstMem = getMsgMemberID($split1_first_msg);
    $split1_lastMem = getMsgMemberID($split1_last_msg);
    // Find the first and last in the list. (new topic)
    $request = $smcFunc['db_query']('', '
		SELECT MIN(id_msg) AS myid_first_msg, MAX(id_msg) AS myid_last_msg, COUNT(*) AS message_count, approved
		FROM {db_prefix}messages
		WHERE id_msg IN ({array_int:msg_list})
			AND id_topic = {int:id_topic}
		GROUP BY id_topic, approved
		ORDER BY approved DESC
		LIMIT 2', array('msg_list' => $splitMessages, 'id_topic' => $split1_ID_TOPIC));
    while ($row = $smcFunc['db_fetch_assoc']($request)) {
        // As before get the right first and last message dependant on approved state...
        if (empty($split2_first_msg) || $row['myid_first_msg'] < $split2_first_msg) {
            $split2_first_msg = $row['myid_first_msg'];
        }
        if (empty($split2_last_msg) || $row['approved']) {
            $split2_last_msg = $row['myid_last_msg'];
        }
        // Then do the counts again...
        if ($row['approved']) {
            $split2_approved = true;
            $split2_replies = $row['message_count'] - 1;
            $split2_unapprovedposts = 0;
        } else {
            // Should this one be approved??
            if ($split2_first_msg == $row['myid_first_msg']) {
                $split2_approved = false;
            }
            if (!isset($split2_replies)) {
                $split2_replies = 0;
            } elseif (!$split2_approved) {
                $split2_replies++;
            }
            $split2_unapprovedposts = $row['message_count'];
        }
    }
    $smcFunc['db_free_result']($request);
    $split2_firstMem = getMsgMemberID($split2_first_msg);
    $split2_lastMem = getMsgMemberID($split2_last_msg);
    // No database changes yet, so let's double check to see if everything makes at least a little sense.
    if ($split1_first_msg <= 0 || $split1_last_msg <= 0 || $split2_first_msg <= 0 || $split2_last_msg <= 0 || $split1_replies < 0 || $split2_replies < 0 || $split1_unapprovedposts < 0 || $split2_unapprovedposts < 0 || !isset($split1_approved) || !isset($split2_approved)) {
        fatal_lang_error('cant_find_messages');
    }
    // You cannot split off the first message of a topic.
    if ($split1_first_msg > $split2_first_msg) {
        fatal_lang_error('split_first_post', false);
    }
    // We're off to insert the new topic!  Use 0 for now to avoid UNIQUE errors.
    $smcFunc['db_insert']('', '{db_prefix}topics', array('id_board' => 'int', 'id_member_started' => 'int', 'id_member_updated' => 'int', 'id_first_msg' => 'int', 'id_last_msg' => 'int', 'num_replies' => 'int', 'unapproved_posts' => 'int', 'approved' => 'int', 'is_sticky' => 'int'), array((int) $id_board, $split2_firstMem, $split2_lastMem, 0, 0, $split2_replies, $split2_unapprovedposts, (int) $split2_approved, 0), array('id_topic'));
    $split2_ID_TOPIC = $smcFunc['db_insert_id']('{db_prefix}topics', 'id_topic');
    if ($split2_ID_TOPIC <= 0) {
        fatal_lang_error('cant_insert_topic');
    }
    // Move the messages over to the other topic.
    $new_subject = strtr($smcFunc['htmltrim']($smcFunc['htmlspecialchars']($new_subject)), array("\r" => '', "\n" => '', "\t" => ''));
    // Check the subject length.
    if ($smcFunc['strlen']($new_subject) > 100) {
        $new_subject = $smcFunc['substr']($new_subject, 0, 100);
    }
    // Valid subject?
    if ($new_subject != '') {
        $smcFunc['db_query']('', '
			UPDATE {db_prefix}messages
			SET
				id_topic = {int:id_topic},
				subject = CASE WHEN id_msg = {int:split_first_msg} THEN {string:new_subject} ELSE {string:new_subject_replies} END
			WHERE id_msg IN ({array_int:split_msgs})', array('split_msgs' => $splitMessages, 'id_topic' => $split2_ID_TOPIC, 'new_subject' => $new_subject, 'split_first_msg' => $split2_first_msg, 'new_subject_replies' => $txt['response_prefix'] . $new_subject));
        // Cache the new topics subject... we can do it now as all the subjects are the same!
        updateStats('subject', $split2_ID_TOPIC, $new_subject);
    }
    // Any associated reported posts better follow...
    $smcFunc['db_query']('', '
		UPDATE {db_prefix}log_reported
		SET id_topic = {int:id_topic}
		WHERE id_msg IN ({array_int:split_msgs})', array('split_msgs' => $splitMessages, 'id_topic' => $split2_ID_TOPIC));
    // Mess with the old topic's first, last, and number of messages.
    $smcFunc['db_query']('', '
		UPDATE {db_prefix}topics
		SET
			num_replies = {int:num_replies},
			id_first_msg = {int:id_first_msg},
			id_last_msg = {int:id_last_msg},
			id_member_started = {int:id_member_started},
			id_member_updated = {int:id_member_updated},
			unapproved_posts = {int:unapproved_posts}
		WHERE id_topic = {int:id_topic}', array('num_replies' => $split1_replies, 'id_first_msg' => $split1_first_msg, 'id_last_msg' => $split1_last_msg, 'id_member_started' => $split1_firstMem, 'id_member_updated' => $split1_lastMem, 'unapproved_posts' => $split1_unapprovedposts, 'id_topic' => $split1_ID_TOPIC));
    // Now, put the first/last message back to what they should be.
    $smcFunc['db_query']('', '
		UPDATE {db_prefix}topics
		SET
			id_first_msg = {int:id_first_msg},
			id_last_msg = {int:id_last_msg}
		WHERE id_topic = {int:id_topic}', array('id_first_msg' => $split2_first_msg, 'id_last_msg' => $split2_last_msg, 'id_topic' => $split2_ID_TOPIC));
    // If the new topic isn't approved ensure the first message flags this just in case.
    if (!$split2_approved) {
        $smcFunc['db_query']('', '
			UPDATE {db_prefix}messages
			SET approved = {int:approved}
			WHERE id_msg = {int:id_msg}
				AND id_topic = {int:id_topic}', array('approved' => 0, 'id_msg' => $split2_first_msg, 'id_topic' => $split2_ID_TOPIC));
    }
    // The board has more topics now (Or more unapproved ones!).
    $smcFunc['db_query']('', '
		UPDATE {db_prefix}boards
		SET ' . ($split2_approved ? '
			num_topics = num_topics + 1' : '
			unapproved_topics = unapproved_topics + 1') . '
		WHERE id_board = {int:id_board}', array('id_board' => $id_board));
    // Copy log topic entries.
    // @todo This should really be chunked.
    $request = $smcFunc['db_query']('', '
		SELECT id_member, id_msg
		FROM {db_prefix}log_topics
		WHERE id_topic = {int:id_topic}', array('id_topic' => (int) $split1_ID_TOPIC));
    if ($smcFunc['db_num_rows']($request) > 0) {
        $replaceEntries = array();
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            $replaceEntries[] = array($row['id_member'], $split2_ID_TOPIC, $row['id_msg']);
        }
        $smcFunc['db_insert']('ignore', '{db_prefix}log_topics', array('id_member' => 'int', 'id_topic' => 'int', 'id_msg' => 'int'), $replaceEntries, array('id_member', 'id_topic'));
        unset($replaceEntries);
    }
    $smcFunc['db_free_result']($request);
    // Housekeeping.
    updateStats('topic');
    updateLastMessages($id_board);
    logAction('split', array('topic' => $split1_ID_TOPIC, 'new_topic' => $split2_ID_TOPIC, 'board' => $id_board));
    // Notify people that this topic has been split?
    sendNotifications($split1_ID_TOPIC, 'split');
    // If there's a search index that needs updating, update it...
    require_once $sourcedir . '/Search.php';
    $searchAPI = findSearchAPI();
    if (is_callable(array($searchAPI, 'topicSplit'))) {
        $searchAPI->topicSplit($split2_ID_TOPIC, $splitMessages);
    }
    // Return the ID of the newly created topic.
    return $split2_ID_TOPIC;
}
function doStep2()
{
    global $convert_data, $from_prefix, $to_prefix, $modSettings, $command_line;
    global $smcFunc, $sourcedir, $current_step;
    $current_step = 2;
    $_GET['step'] = '2';
    $debug = false;
    if (isset($_REQUEST['debug'])) {
        $debug = true;
    }
    print_line(($command_line ? ' * ' : '') . 'Recalculating forum statistics... ', false);
    if ($_GET['substep'] <= 0) {
        if ($debug) {
            print_line('Get all members with wrong number of personal messages..');
        }
        // Get all members with wrong number of personal messages.
        $request = convert_query("\n\t\t\tSELECT mem.id_member, COUNT(pmr.id_pm) AS real_num, mem.instant_messages\n\t\t\tFROM {$to_prefix}members AS mem\n\t\t\t\tLEFT JOIN {$to_prefix}pm_recipients AS pmr ON (mem.id_member = pmr.id_member AND pmr.deleted = 0)\n\t\t\tGROUP BY mem.id_member\n\t\t\tHAVING real_num != instant_messages");
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            convert_query("\n\t\t\t\tUPDATE {$to_prefix}members\n\t\t\t\tSET instant_messages = {$row['real_num']}\n\t\t\t\tWHERE id_member = {$row['id_member']}\n\t\t\t\tLIMIT 1");
            pastTime(0);
        }
        $smcFunc['db_free_result']($request);
        if ($debug) {
            print_line('Correct all unread messages..');
        }
        $request = convert_query("\n\t\t\tSELECT mem.id_member, COUNT(pmr.id_pm) AS real_num, mem.unread_messages\n\t\t\tFROM {$to_prefix}members AS mem\n\t\t\t\tLEFT JOIN {$to_prefix}pm_recipients AS pmr ON (mem.id_member = pmr.id_member AND pmr.deleted = 0 AND pmr.is_read = 0)\n\t\t\tGROUP BY mem.id_member\n\t\t\tHAVING real_num != unread_messages");
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            convert_query("\n\t\t\t\tUPDATE {$to_prefix}members\n\t\t\t\tSET unread_messages = {$row['real_num']}\n\t\t\t\tWHERE id_member = {$row['id_member']}\n\t\t\t\tLIMIT 1");
            pastTime(0);
        }
        $smcFunc['db_free_result']($request);
        pastTime(1);
    }
    if ($_GET['substep'] <= 1) {
        if ($debug) {
            print_line('Correct boards with incorrect msg ids..');
        }
        $request = convert_query("\n\t\t\tSELECT id_board, MAX(id_msg) AS id_last_msg, MAX(modified_time) AS last_edited\n\t\t\tFROM {$to_prefix}messages\n\t\t\tGROUP BY id_board");
        $modifyData = array();
        $modifyMsg = array();
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            convert_query("\n\t\t\t\tUPDATE {$to_prefix}boards\n\t\t\t\tSET id_last_msg = {$row['id_last_msg']}, id_msg_updated = {$row['id_last_msg']}\n\t\t\t\tWHERE id_board = {$row['id_board']}\n\t\t\t\tLIMIT 1");
            $modifyData[$row['id_board']] = array('last_msg' => $row['id_last_msg'], 'last_edited' => $row['last_edited']);
            $modifyMsg[] = $row['id_last_msg'];
        }
        $smcFunc['db_free_result']($request);
        // Are there any boards where the updated message is not the last?
        if (!empty($modifyMsg)) {
            if ($debug) {
                print_line('Correct any boards that do not show the correct last message..');
            }
            $request = convert_query("\n\t\t\t\tSELECT id_board, id_msg, modified_time, poster_time\n\t\t\t\tFROM {$to_prefix}messages\n\t\t\t\tWHERE id_msg IN (" . implode(',', $modifyMsg) . ")");
            while ($row = $smcFunc['db_fetch_assoc']($request)) {
                // Have we got a message modified before this was posted?
                if (max($row['modified_time'], $row['poster_time']) < $modifyData[$row['id_board']]['last_edited']) {
                    // Work out the ID of the message (This seems long but it won't happen much.
                    $request2 = convert_query("\n\t\t\t\t\t\tSELECT id_msg\n\t\t\t\t\t\tFROM {$to_prefix}messages\n\t\t\t\t\t\tWHERE modified_time = " . $modifyData[$row['id_board']]['last_edited'] . "\n\t\t\t\t\t\tLIMIT 1");
                    if ($smcFunc['db_num_rows']($request2) != 0) {
                        list($id_msg) = $smcFunc['db_fetch_row']($request2);
                        convert_query("\n\t\t\t\t\t\t\tUPDATE {$to_prefix}boards\n\t\t\t\t\t\t\tSET id_msg_updated = {$id_msg}\n\t\t\t\t\t\t\tWHERE id_board = {$row['id_board']}\n\t\t\t\t\t\t\tLIMIT 1");
                    }
                    $smcFunc['db_free_result']($request2);
                }
            }
            $smcFunc['db_free_result']($request);
        }
        pastTime(2);
    }
    if ($_GET['substep'] <= 2) {
        if ($debug) {
            print_line('Correct any incorrect groups..');
        }
        $request = convert_query("\n\t\t\tSELECT id_group\n\t\t\tFROM {$to_prefix}membergroups\n\t\t\tWHERE min_posts = -1");
        $all_groups = array();
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            $all_groups[] = $row['id_group'];
        }
        $smcFunc['db_free_result']($request);
        $request = convert_query("\n\t\t\tSELECT id_board, member_groups\n\t\t\tFROM {$to_prefix}boards\n\t\t\tWHERE FIND_IN_SET(0, member_groups)");
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            convert_query("\n\t\t\t\tUPDATE {$to_prefix}boards\n\t\t\t\tSET member_groups = '" . implode(',', array_unique(array_merge($all_groups, explode(',', $row['member_groups'])))) . "'\n\t\t\t\tWHERE id_board = {$row['id_board']}\n\t\t\t\tLIMIT 1");
        }
        $smcFunc['db_free_result']($request);
        pastTime(3);
    }
    if ($_GET['substep'] <= 3) {
        if ($debug) {
            print_line('Update our statitics..');
        }
        // Get the number of messages...
        $result = convert_query("\n\t\t\tSELECT COUNT(*) AS total_messages, MAX(id_msg) AS max_msg_id\n\t\t\tFROM {$to_prefix}messages");
        $row = $smcFunc['db_fetch_assoc']($result);
        $smcFunc['db_free_result']($result);
        // Update the latest member.  (highest id_member)
        $result = convert_query("\n\t\t\tSELECT id_member AS latest_member, real_name AS latest_real_name\n\t\t\tFROM {$to_prefix}members\n\t\t\tORDER BY id_member DESC\n\t\t\tLIMIT 1");
        if ($smcFunc['db_num_rows']($result)) {
            $row += $smcFunc['db_fetch_assoc']($result);
        }
        $smcFunc['db_free_result']($result);
        // Update the member count.
        $result = convert_query("\n\t\t\tSELECT COUNT(*) AS total_members\n\t\t\tFROM {$to_prefix}members");
        $row += $smcFunc['db_fetch_assoc']($result);
        $smcFunc['db_free_result']($result);
        // Get the number of topics.
        $result = convert_query("\n\t\t\tSELECT COUNT(*) AS total_topics\n\t\t\tFROM {$to_prefix}topics");
        $row += $smcFunc['db_fetch_assoc']($result);
        $smcFunc['db_free_result']($result);
        $smcFunc['db_insert']('replace', '{db_prefix}settings', array('variable' => 'string', 'value' => 'string'), array(array('latest_member', $row['latest_member']), array('latest_real_name', $row['latest_real_name']), array('total_members', $row['total_members']), array('total_messages', $row['total_messages']), array('max_msg_id', $row['max_msg_id']), array('total_topics', $row['total_topics']), array('disable_hash_time', time() + 7776000)), array('name'));
        pastTime(4);
    }
    if ($_GET['substep'] <= 4) {
        if ($debug) {
            print_line('Correct any posts groups..');
        }
        $request = convert_query("\n\t\t\tSELECT id_group, min_posts\n\t\t\tFROM {$to_prefix}membergroups\n\t\t\tWHERE min_posts != -1\n\t\t\tORDER BY min_posts DESC");
        $post_groups = array();
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            $post_groups[$row['min_posts']] = $row['id_group'];
        }
        $smcFunc['db_free_result']($request);
        $request = convert_query("\n\t\t\tSELECT id_member, posts\n\t\t\tFROM {$to_prefix}members");
        $mg_updates = array();
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            $group = 4;
            foreach ($post_groups as $min_posts => $group_id) {
                if ($row['posts'] >= $min_posts) {
                    $group = $group_id;
                    break;
                }
            }
            $mg_updates[$group][] = $row['id_member'];
        }
        $smcFunc['db_free_result']($request);
        foreach ($mg_updates as $group_to => $update_members) {
            convert_query("\n\t\t\t\tUPDATE {$to_prefix}members\n\t\t\t\tSET id_post_group = {$group_to}\n\t\t\t\tWHERE id_member IN (" . implode(', ', $update_members) . ")\n\t\t\t\tLIMIT " . count($update_members));
        }
        // This isn't completely related, but should be rather quick.
        convert_query("\n\t\t\tUPDATE {$to_prefix}members\n\t\t\tSET icq = ''\n\t\t\tWHERE icq = '0'");
        pastTime(5);
    }
    if ($_GET['substep'] <= 5) {
        if ($debug) {
            print_line('Correct all board topics/post counts..');
        }
        // Needs to be done separately for each board.
        $result_boards = convert_query("\n\t\t\tSELECT id_board\n\t\t\tFROM {$to_prefix}boards");
        $boards = array();
        while ($row_boards = $smcFunc['db_fetch_assoc']($result_boards)) {
            $boards[] = $row_boards['id_board'];
        }
        $smcFunc['db_free_result']($result_boards);
        foreach ($boards as $id_board) {
            // Get the number of topics, and iterate through them.
            $result_topics = convert_query("\n\t\t\t\tSELECT COUNT(*)\n\t\t\t\tFROM {$to_prefix}topics\n\t\t\t\tWHERE id_board = {$id_board}");
            list($num_topics) = $smcFunc['db_fetch_row']($result_topics);
            $smcFunc['db_free_result']($result_topics);
            // Find how many messages are in the board.
            $result_posts = convert_query("\n\t\t\t\tSELECT COUNT(*)\n\t\t\t\tFROM {$to_prefix}messages\n\t\t\t\tWHERE id_board = {$id_board}");
            list($num_posts) = $smcFunc['db_fetch_row']($result_posts);
            $smcFunc['db_free_result']($result_posts);
            // Fix the board's totals.
            convert_query("\n\t\t\t\tUPDATE {$to_prefix}boards\n\t\t\t\tSET num_topics = {$num_topics}, num_posts = {$num_posts}\n\t\t\t\tWHERE id_board = {$id_board}\n\t\t\t\tLIMIT 1");
        }
        pastTime(6);
    }
    // Remove all topics that have zero messages in the messages table.
    if ($_GET['substep'] <= 6) {
        if ($debug) {
            print_line('Removing any topics that have zero messages..');
        }
        while (true) {
            $resultTopic = convert_query("\n\t\t\t\tSELECT t.id_topic, COUNT(m.id_msg) AS num_msg\n\t\t\t\tFROM {$to_prefix}topics AS t\n\t\t\t\t\tLEFT JOIN {$to_prefix}messages AS m ON (m.id_topic = t.id_topic)\n\t\t\t\tGROUP BY t.id_topic\n\t\t\t\tHAVING num_msg = 0\n\t\t\t\tLIMIT {$_REQUEST['start']}, " . (!empty($convert_data['step2_block_size']) ? $convert_data['step2_block_size'] : 200));
            $numRows = $smcFunc['db_num_rows']($resultTopic);
            if ($numRows > 0) {
                $stupidTopics = array();
                while ($topicArray = $smcFunc['db_fetch_assoc']($resultTopic)) {
                    $stupidTopics[] = $topicArray['id_topic'];
                }
                convert_query("\n\t\t\t\t\tDELETE FROM {$to_prefix}topics\n\t\t\t\t\tWHERE id_topic IN (" . implode(',', $stupidTopics) . ')
					LIMIT ' . count($stupidTopics));
                convert_query("\n\t\t\t\t\tDELETE FROM {$to_prefix}log_topics\n\t\t\t\t\tWHERE id_topic IN (" . implode(',', $stupidTopics) . ')');
            }
            $smcFunc['db_free_result']($resultTopic);
            if ($numRows < 200) {
                break;
            }
            $_REQUEST['start'] += 200;
            pastTime(6);
        }
        $_REQUEST['start'] = 0;
        pastTime(7);
    }
    // Get the correct number of replies.
    if ($_GET['substep'] <= 7) {
        if ($debug) {
            print_line('Correct the number of replies..');
        }
        // Make sure we have the function "getMsgMemberID"
        require_once $sourcedir . '/Subs-Boards.php';
        while (true) {
            $resultTopic = convert_query("\n\t\t\t\tSELECT\n\t\t\t\t\tt.id_topic, MIN(m.id_msg) AS myid_first_msg, t.id_first_msg,\n\t\t\t\t\tMAX(m.id_msg) AS myid_last_msg, t.id_last_msg, COUNT(m.id_msg) - 1 AS my_num_replies,\n\t\t\t\t\tt.num_replies\n\t\t\t\tFROM {$to_prefix}topics AS t\n\t\t\t\t\tLEFT JOIN {$to_prefix}messages AS m ON (m.id_topic = t.id_topic)\n\t\t\t\tGROUP BY t.id_topic\n\t\t\t\tHAVING id_first_msg != myid_first_msg OR id_last_msg != myid_last_msg OR num_replies != my_num_replies\n\t\t\t\tLIMIT {$_REQUEST['start']}, " . (!empty($convert_data['step2_block_size']) ? $convert_data['step2_block_size'] : 200));
            $numRows = $smcFunc['db_num_rows']($resultTopic);
            while ($topicArray = $smcFunc['db_fetch_assoc']($resultTopic)) {
                $memberStartedID = getMsgMemberID($topicArray['myid_first_msg']);
                $memberUpdatedID = getMsgMemberID($topicArray['myid_last_msg']);
                convert_query("\n\t\t\t\t\tUPDATE {$to_prefix}topics\n\t\t\t\t\tSET id_first_msg = '{$topicArray['myid_first_msg']}',\n\t\t\t\t\t\tid_member_started = '{$memberStartedID}', id_last_msg = '{$topicArray['myid_last_msg']}',\n\t\t\t\t\t\tid_member_updated = '{$memberUpdatedID}', num_replies = '{$topicArray['my_num_replies']}'\n\t\t\t\t\tWHERE id_topic = {$topicArray['id_topic']}\n\t\t\t\t\tLIMIT 1");
            }
            $smcFunc['db_free_result']($resultTopic);
            if ($numRows < 200) {
                break;
            }
            $_REQUEST['start'] += 100;
            pastTime(7);
        }
        $_REQUEST['start'] = 0;
        pastTime(8);
    }
    // Fix id_cat, id_parent, and child_level.
    if ($_GET['substep'] <= 8) {
        if ($debug) {
            print_line('Fix the Categories and board layout..');
        }
        // First, let's get an array of boards and parents.
        $request = convert_query("\n\t\t\tSELECT id_board, id_parent, id_cat\n\t\t\tFROM {$to_prefix}boards");
        $child_map = array();
        $cat_map = array();
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            $child_map[$row['id_parent']][] = $row['id_board'];
            $cat_map[$row['id_board']] = $row['id_cat'];
        }
        $smcFunc['db_free_result']($request);
        // Let's look for any boards with obviously invalid parents...
        foreach ($child_map as $parent => $dummy) {
            if ($parent != 0 && !isset($cat_map[$parent])) {
                // Perhaps it was supposed to be their id_cat?
                foreach ($dummy as $board) {
                    if (empty($cat_map[$board])) {
                        $cat_map[$board] = $parent;
                    }
                }
                $child_map[0] = array_merge(isset($child_map[0]) ? $child_map[0] : array(), $dummy);
                unset($child_map[$parent]);
            }
        }
        // The above id_parents and id_cats may all be wrong; we know id_parent = 0 is right.
        $solid_parents = array(array(0, 0));
        $fixed_boards = array();
        while (!empty($solid_parents)) {
            list($parent, $level) = array_pop($solid_parents);
            if (!isset($child_map[$parent])) {
                continue;
            }
            // Fix all of this board's children.
            foreach ($child_map[$parent] as $board) {
                if ($parent != 0) {
                    $cat_map[$board] = $cat_map[$parent];
                }
                $fixed_boards[$board] = array($parent, $cat_map[$board], $level);
                $solid_parents[] = array($board, $level + 1);
            }
        }
        foreach ($fixed_boards as $board => $fix) {
            convert_query("\n\t\t\t\tUPDATE {$to_prefix}boards\n\t\t\t\tSET id_parent = " . (int) $fix[0] . ", id_cat = " . (int) $fix[1] . ", child_level = " . (int) $fix[2] . "\n\t\t\t\tWHERE id_board = " . (int) $board . "\n\t\t\t\tLIMIT 1");
        }
        // Leftovers should be brought to the root.  They had weird parents we couldn't find.
        if (count($fixed_boards) < count($cat_map)) {
            convert_query("\n\t\t\t\tUPDATE {$to_prefix}boards\n\t\t\t\tSET child_level = 0, id_parent = 0" . (empty($fixed_boards) ? '' : "\n\t\t\t\tWHERE id_board NOT IN (" . implode(', ', array_keys($fixed_boards)) . ")"));
        }
        // Last check: any boards not in a good category?
        $request = convert_query("\n\t\t\tSELECT id_cat\n\t\t\tFROM {$to_prefix}categories");
        $real_cats = array();
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            $real_cats[] = $row['id_cat'];
        }
        $smcFunc['db_free_result']($request);
        $fix_cats = array();
        foreach ($cat_map as $board => $cat) {
            if (!in_array($cat, $real_cats)) {
                $fix_cats[] = $cat;
            }
        }
        if (!empty($fix_cats)) {
            $smcFunc['db_insert']('insert', '{db_prefix}categories', array('name' => 'string'), array('General Category'), array('name'));
            $catch_cat = $smcFunc['db_insert_id']('{db_prefix}categories');
            convert_query("\n\t\t\t\tUPDATE {$to_prefix}boards\n\t\t\t\tSET id_cat = " . (int) $catch_cat . "\n\t\t\t\tWHERE id_cat IN (" . implode(', ', array_unique($fix_cats)) . ")");
        }
        pastTime(9);
    }
    if ($_GET['substep'] <= 9) {
        if ($debug) {
            print_line('Correct category orders..');
        }
        $request = convert_query("\n\t\t\tSELECT c.id_cat, c.cat_order, b.id_board, b.board_order\n\t\t\tFROM {$to_prefix}categories AS c\n\t\t\t\tLEFT JOIN {$to_prefix}boards AS b ON (b.id_cat = c.id_cat)\n\t\t\tORDER BY c.cat_order, b.child_level, b.board_order, b.id_board");
        $cat_order = -1;
        $board_order = -1;
        $curCat = -1;
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            if ($curCat != $row['id_cat']) {
                $curCat = $row['id_cat'];
                if (++$cat_order != $row['cat_order']) {
                    convert_query("\n\t\t\t\t\t\tUPDATE {$to_prefix}categories\n\t\t\t\t\t\tSET cat_order = {$cat_order}\n\t\t\t\t\t\tWHERE id_cat = {$row['id_cat']}\n\t\t\t\t\t\tLIMIT 1");
                }
            }
            if (!empty($row['id_board']) && ++$board_order != $row['board_order']) {
                convert_query("\n\t\t\t\t\tUPDATE {$to_prefix}boards\n\t\t\t\t\tSET board_order = {$board_order}\n\t\t\t\t\tWHERE id_board = {$row['id_board']}\n\t\t\t\t\tLIMIT 1");
            }
        }
        $smcFunc['db_free_result']($request);
        pastTime(10);
    }
    if ($_GET['substep'] <= 10) {
        if ($debug) {
            print_line('Force the board order..');
        }
        // Update our BoardOrder
        require_once $sourcedir . '/Subs-Boards.php';
        reorderBoards();
        // Update our Smileys table.
        require_once $sourcedir . '/ManageSmileys.php';
        sortSmileyTable();
        pastTime(11);
    }
    if ($_GET['substep'] <= 11) {
        if ($debug) {
            print_line('Correct any incorrect attachments..');
        }
        $request = convert_query("\n\t\t\tSELECT COUNT(*)\n\t\t\tFROM {$to_prefix}attachments");
        list($attachments) = $smcFunc['db_fetch_row']($request);
        $smcFunc['db_free_result']($request);
        while ($_REQUEST['start'] < $attachments) {
            $request = convert_query("\n\t\t\t\tSELECT id_attach, filename, attachment_type\n\t\t\t\tFROM {$to_prefix}attachments\n\t\t\t\tWHERE id_thumb = 0\n\t\t\t\t\tAND (RIGHT(filename, 4) IN ('.gif', '.jpg', '.png', '.bmp') OR RIGHT(filename, 5) = '.jpeg')\n\t\t\t\t\tAND width = 0\n\t\t\t\t\tAND height = 0\n\t\t\t\tLIMIT {$_REQUEST['start']}, " . (!empty($convert_data['step2_block_size']) ? $convert_data['step2_block_size'] : 500));
            if ($smcFunc['db_num_rows']($request) == 0) {
                break;
            }
            while ($row = $smcFunc['db_fetch_assoc']($request)) {
                if ($row['attachment_type'] == 1) {
                    $request2 = convert_query("\n\t\t\t\t\t\tSELECT value\n\t\t\t\t\t\tFROM {$to_prefix}settings\n\t\t\t\t\t\tWHERE variable = 'custom_avatar_dir'\n\t\t\t\t\t\tLIMIT 1");
                    list($custom_avatar_dir) = $smcFunc['db_fetch_row']($request2);
                    $smcFunc['db_free_result']($request2);
                    $filename = $custom_avatar_dir . '/' . $row['filename'];
                } else {
                    $filename = getAttachmentFilename($row['filename'], $row['id_attach']);
                }
                // Probably not one of the converted ones, then?
                if (!file_exists($filename)) {
                    continue;
                }
                $size = @getimagesize($filename);
                $filesize = @filesize($filename);
                if (!empty($size) && !empty($size[0]) && !empty($size[1]) && !empty($filesize)) {
                    convert_query("\n\t\t\t\t\t\tUPDATE {$to_prefix}attachments\n\t\t\t\t\t\tSET\n\t\t\t\t\t\t\tsize = " . (int) $filesize . ",\n\t\t\t\t\t\t\twidth = " . (int) $size[0] . ",\n\t\t\t\t\t\t\theight = " . (int) $size[1] . "\n\t\t\t\t\t\tWHERE id_attach = {$row['id_attach']}\n\t\t\t\t\t\tLIMIT 1");
                }
            }
            $smcFunc['db_free_result']($request);
            // More?
            // We can't keep converting the same files over and over again!
            $_REQUEST['start'] += 500;
            pastTime(11);
        }
        $_REQUEST['start'] = 0;
        pastTime(12);
    }
    // Lets rebuild the indexes.
    if ($_GET['substep'] <= 12) {
        if ($debug) {
            print_line('rebuilding indexes for topics..');
        }
        db_extend('packages');
        $indexes = $smcFunc['db_list_indexes']($to_prefix . 'topics', true, array('no_prefix' => true));
        if (!isset($indexes['PRIMARY'])) {
            $smcFunc['db_add_index']($to_prefix . 'topics', array('type' => 'PRIMARY', 'columns' => array('id_topic')), array('no_prefix' => true));
        }
        if (!isset($indexes['last_message'])) {
            $smcFunc['db_add_index']($to_prefix . 'topics', array('type' => 'UNIQUE', 'name' => 'last_message', 'columns' => array('id_last_msg', 'id_board')), array('no_prefix' => true));
        }
        if (!isset($indexes['first_message'])) {
            $smcFunc['db_add_index']($to_prefix . 'topics', array('type' => 'UNIQUE', 'name' => 'first_message', 'columns' => array('id_first_msg', 'id_board')), array('no_prefix' => true));
        }
        if (!isset($indexes['poll'])) {
            $smcFunc['db_add_index']($to_prefix . 'topics', array('type' => 'UNIQUE', 'name' => 'poll', 'columns' => array('id_poll', 'id_topic')), array('no_prefix' => true));
        }
        if (!isset($indexes['is_sticky'])) {
            $smcFunc['db_add_index']($to_prefix . 'topics', array('type' => 'INDEX', 'name' => 'is_sticky', 'columns' => array('is_sticky')), array('no_prefix' => true));
        }
        if (!isset($indexes['id_board'])) {
            $smcFunc['db_add_index']($to_prefix . 'topics', array('type' => 'INDEX', 'name' => 'id_board', 'columns' => array('id_board')), array('no_prefix' => true));
        }
        $_REQUEST['start'] = 0;
        pastTime(13);
    }
    if ($_GET['substep'] <= 13) {
        if ($debug) {
            print_line('rebuilding indexes for messages..');
        }
        db_extend('packages');
        $indexes = $smcFunc['db_list_indexes']($to_prefix . 'messages', true, array('no_prefix' => true));
        if (!isset($indexes['PRIMARY'])) {
            $smcFunc['db_add_index']($to_prefix . 'messages', array('type' => 'PRIMARY', 'columns' => array('id_msg')), array('no_prefix' => true));
        }
        if (!isset($indexes['topic'])) {
            $smcFunc['db_add_index']($to_prefix . 'messages', array('type' => 'UNIQUE', 'name' => 'topic', 'columns' => array('id_topic', 'id_msg')), array('no_prefix' => true));
        }
        if (!isset($indexes['id_board'])) {
            $smcFunc['db_add_index']($to_prefix . 'messages', array('type' => 'UNIQUE', 'name' => 'id_board', 'columns' => array('id_board', 'id_msg')), array('no_prefix' => true));
        }
        if (!isset($indexes['id_member'])) {
            $smcFunc['db_add_index']($to_prefix . 'messages', array('type' => 'UNIQUE', 'name' => 'id_member', 'columns' => array('id_member', 'id_msg')), array('no_prefix' => true));
        }
        if (!isset($indexes['ip_index'])) {
            $smcFunc['db_add_index']($to_prefix . 'messages', array('type' => 'INDEX', 'name' => 'ip_index', 'columns' => array('poster_ip(15)', 'id_topic')), array('no_prefix' => true));
        }
        if (!isset($indexes['participation'])) {
            $smcFunc['db_add_index']($to_prefix . 'messages', array('type' => 'INDEX', 'name' => 'participation', 'columns' => array('id_member', 'id_topic')), array('no_prefix' => true));
        }
        if (!isset($indexes['showPosts'])) {
            $smcFunc['db_add_index']($to_prefix . 'messages', array('type' => 'INDEX', 'name' => 'show_posts', 'columns' => array('id_member', 'id_member')), array('no_prefix' => true));
        }
        if (!isset($indexes['id_topic'])) {
            $smcFunc['db_add_index']($to_prefix . 'messages', array('type' => 'INDEX', 'name' => 'id_topic', 'columns' => array('id_topic')), array('no_prefix' => true));
        }
        $_REQUEST['start'] = 0;
        pastTime(14);
    }
    print_line(' Successful.');
    return doStep3();
}