function RepairBoards()
{
    global $txt, $scripturl, $db_connection, $context, $sourcedir;
    global $salvageCatID, $salvageBoardID, $smcFunc, $errorTests;
    isAllowedTo('admin_forum');
    // Try secure more memory.
    @ini_set('memory_limit', '128M');
    // Print out the top of the webpage.
    $context['page_title'] = $txt['admin_repair'];
    $context['sub_template'] = 'repair_boards';
    $context[$context['admin_menu_name']]['current_subsection'] = 'general';
    // Load the language file.
    loadLanguage('ManageMaintenance');
    // Make sure the tabs stay nice.
    $context[$context['admin_menu_name']]['tab_data'] = array('title' => $txt['maintain_title'], 'help' => '', 'description' => $txt['maintain_info'], 'tabs' => array());
    // Start displaying errors without fixing them.
    if (isset($_GET['fixErrors'])) {
        checkSession('get');
    }
    // Will want this.
    loadForumTests();
    // 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['error_search'] = true;
        $context['repair_errors'] = array();
        $context['to_fix'] = findForumErrors();
        if (!empty($context['to_fix'])) {
            $_SESSION['repairboards_to_fix'] = $context['to_fix'];
            $_SESSION['repairboards_to_fix2'] = null;
            if (empty($context['repair_errors'])) {
                $context['repair_errors'][] = '???';
            }
        }
    } else {
        $context['error_search'] = false;
        $context['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 = $smcFunc['db_server_info']($db_connection);
        // Actually do the fix.
        findForumErrors(true);
        // Note that we've changed everything possible ;)
        updateSettings(array('settings_updated' => time()));
        updateStats('message');
        updateStats('topic');
        updateSettings(array('calendar_updated' => time()));
        if (!empty($salvageBoardID)) {
            $context['redirect_to_recount'] = true;
        }
        $_SESSION['repairboards_to_fix'] = null;
        $_SESSION['repairboards_to_fix2'] = null;
    }
}
Example #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;
    }
}
 /**
  * Finds or repairs errors in the database to fix possible problems.
  * Requires the admin_forum permission.
  * Accessed by ?action=admin;area=repairboards.
  *
  * @uses raw_data sub-template.
  */
 public function action_repairboards()
 {
     global $txt, $context, $salvageBoardID, $db_show_debug;
     isAllowedTo('admin_forum');
     require_once SUBSDIR . '/RepairBoards.subs.php';
     // Try secure more memory.
     setMemoryLimit('128M');
     // Print out the top of the webpage.
     $context['page_title'] = $txt['admin_repair'];
     $context['sub_template'] = 'repair_boards';
     $context[$context['admin_menu_name']]['current_subsection'] = 'general';
     // Load the language file.
     loadLanguage('Maintenance');
     // Make sure the tabs stay nice.
     $context[$context['admin_menu_name']]['tab_data'] = array('title' => $txt['maintain_title'], 'help' => '', 'description' => $txt['maintain_info'], 'tabs' => array());
     // Start displaying errors without fixing them.
     if (isset($_GET['fixErrors'])) {
         checkSession('get');
     }
     // Will want this.
     loadForumTests();
     // 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['error_search'] = true;
         $context['repair_errors'] = array();
         // Logging may cause session issues with many queries
         $old_db_show_debug = $db_show_debug;
         $db_show_debug = false;
         $context['to_fix'] = findForumErrors();
         // Restore previous debug state
         $db_show_debug = $old_db_show_debug;
         if (!empty($context['to_fix'])) {
             $_SESSION['repairboards_to_fix'] = $context['to_fix'];
             $_SESSION['repairboards_to_fix2'] = null;
             if (empty($context['repair_errors'])) {
                 $context['repair_errors'][] = '???';
             }
         }
     } else {
         $context['error_search'] = false;
         $context['to_fix'] = isset($_SESSION['repairboards_to_fix']) ? $_SESSION['repairboards_to_fix'] : array();
         require_once SUBSDIR . '/Boards.subs.php';
         // Logging may cause session issues with many queries
         $old_db_show_debug = $db_show_debug;
         $db_show_debug = false;
         // Actually do the fix.
         findForumErrors(true);
         // Restore previous debug state
         $db_show_debug = $old_db_show_debug;
         // Note that we've changed everything possible ;)
         updateSettings(array('settings_updated' => time()));
         updateStats('message');
         updateStats('topic');
         updateSettings(array('calendar_updated' => time()));
         if (!empty($salvageBoardID)) {
             $context['redirect_to_recount'] = true;
         }
         $_SESSION['repairboards_to_fix'] = null;
         $_SESSION['repairboards_to_fix2'] = null;
     }
 }