Example #1
0
function findForumErrors()
{
    global $db_prefix, $context, $txt;
    // This may take some time...
    @set_time_limit(600);
    $to_fix = !empty($_SESSION['repairboards_to_fix']) ? $_SESSION['repairboards_to_fix'] : array();
    $context['repair_errors'] = isset($_SESSION['repairboards_to_fix2']) ? $_SESSION['repairboards_to_fix2'] : array();
    $_GET['step'] = empty($_GET['step']) ? 0 : (int) $_GET['step'];
    $_GET['substep'] = empty($_GET['substep']) ? 0 : (int) $_GET['substep'];
    if ($_GET['step'] <= 0) {
        // Make a last-ditch-effort check to get rid of topics with zeros..
        $result = db_query("\n\t\t\tSELECT COUNT(*)\n\t\t\tFROM {$db_prefix}topics\n\t\t\tWHERE ID_TOPIC = 0", __FILE__, __LINE__);
        list($zeroTopics) = mysql_fetch_row($result);
        mysql_free_result($result);
        // This is only going to be 1 or 0, but...
        $result = db_query("\n\t\t\tSELECT COUNT(*)\n\t\t\tFROM {$db_prefix}messages\n\t\t\tWHERE ID_MSG = 0", __FILE__, __LINE__);
        list($zeroMessages) = mysql_fetch_row($result);
        mysql_free_result($result);
        if (!empty($zeroTopics) || !empty($zeroMessages)) {
            $context['repair_errors'][] = $txt['repair_zero_ids'];
            $to_fix[] = 'zero_ids';
        }
        $_GET['step'] = 1;
        $_GET['substep'] = 0;
        pauseRepairProcess($to_fix);
    }
    if ($_GET['step'] <= 1) {
        // Find messages that don't have existing topics.
        $result = db_query("\n\t\t\tSELECT m.ID_TOPIC, m.ID_MSG\n\t\t\tFROM {$db_prefix}messages AS m\n\t\t\t\tLEFT JOIN {$db_prefix}topics AS t ON (t.ID_TOPIC = m.ID_TOPIC)\n\t\t\tWHERE t.ID_TOPIC IS NULL\n\t\t\tORDER BY m.ID_TOPIC, m.ID_MSG", __FILE__, __LINE__);
        while ($row = mysql_fetch_assoc($result)) {
            $context['repair_errors'][] = sprintf($txt['repair_missing_topics'], $row['ID_MSG'], $row['ID_TOPIC']);
        }
        if (mysql_num_rows($result) != 0) {
            $to_fix[] = 'missing_topics';
        }
        mysql_free_result($result);
        $_GET['step'] = 2;
        $_GET['substep'] = 0;
        pauseRepairProcess($to_fix);
    }
    if ($_GET['step'] <= 2) {
        // Find messages that don't have existing topics.
        $result = db_query("\n\t\t\tSELECT m.ID_TOPIC, m.ID_MSG\n\t\t\tFROM {$db_prefix}messages AS m\n\t\t\t\tLEFT JOIN {$db_prefix}topics AS t ON (t.ID_TOPIC = m.ID_TOPIC)\n\t\t\tWHERE t.ID_TOPIC IS NULL\n\t\t\tORDER BY m.ID_TOPIC, m.ID_MSG", __FILE__, __LINE__);
        while ($row = mysql_fetch_assoc($result)) {
            $context['repair_errors'][] = sprintf($txt['repair_missing_topics'], $row['ID_MSG'], $row['ID_TOPIC']);
        }
        if (mysql_num_rows($result) != 0) {
            $to_fix[] = 'missing_topics';
        }
        mysql_free_result($result);
        $_GET['step'] = 3;
        $_GET['substep'] = 0;
        pauseRepairProcess($to_fix);
    }
    if ($_GET['step'] <= 3) {
        $result = db_query("\n\t\t\tSELECT MAX(ID_TOPIC)\n\t\t\tFROM {$db_prefix}topics", __FILE__, __LINE__);
        list($topics) = mysql_fetch_row($result);
        mysql_free_result($result);
        // Find topics with no messages.
        for (; $_GET['substep'] < $topics; $_GET['substep'] += 1000) {
            pauseRepairProcess($to_fix, $topics);
            $result = 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\tWHERE t.ID_TOPIC BETWEEN {$_GET['substep']} AND {$_GET['substep']} + 999\n\t\t\t\tGROUP BY t.ID_TOPIC\n\t\t\t\tHAVING numMsg = 0", __FILE__, __LINE__);
            while ($row = mysql_fetch_assoc($result)) {
                $context['repair_errors'][] = sprintf($txt['repair_missing_messages'], $row['ID_TOPIC']);
            }
            if (mysql_num_rows($result) != 0) {
                $to_fix[] = 'missing_messages';
            }
            mysql_free_result($result);
        }
        $_GET['step'] = 4;
        $_GET['substep'] = 0;
        pauseRepairProcess($to_fix);
    }
    if ($_GET['step'] <= 4) {
        $result = db_query("\n\t\t\tSELECT MAX(ID_TOPIC)\n\t\t\tFROM {$db_prefix}topics", __FILE__, __LINE__);
        list($topics) = mysql_fetch_row($result);
        mysql_free_result($result);
        // Find topics with incorrect ID_FIRST_MSG/ID_LAST_MSG/numReplies.
        for (; $_GET['substep'] < $topics; $_GET['substep'] += 1000) {
            pauseRepairProcess($to_fix, $topics);
            $result = db_query("\n\t\t\t\tSELECT\n\t\t\t\t\tt.ID_TOPIC, t.ID_FIRST_MSG, t.ID_LAST_MSG, t.numReplies,\n\t\t\t\t\tMIN(m.ID_MSG) AS myID_FIRST_MSG, MAX(m.ID_MSG) AS myID_LAST_MSG,\n\t\t\t\t\tCOUNT(m.ID_MSG) - 1 AS myNumReplies\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\tWHERE t.ID_TOPIC BETWEEN {$_GET['substep']} AND {$_GET['substep']} + 999\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\n\t\t\t\tORDER BY t.ID_TOPIC", __FILE__, __LINE__);
            while ($row = mysql_fetch_assoc($result)) {
                if ($row['ID_FIRST_MSG'] != $row['myID_FIRST_MSG']) {
                    $context['repair_errors'][] = sprintf($txt['repair_stats_topics_1'], $row['ID_TOPIC'], $row['ID_FIRST_MSG']);
                }
                if ($row['ID_LAST_MSG'] != $row['myID_LAST_MSG']) {
                    $context['repair_errors'][] = sprintf($txt['repair_stats_topics_2'], $row['ID_TOPIC'], $row['ID_LAST_MSG']);
                }
                if ($row['numReplies'] != $row['myNumReplies']) {
                    $context['repair_errors'][] = sprintf($txt['repair_stats_topics_3'], $row['ID_TOPIC'], $row['numReplies']);
                }
            }
            if (mysql_num_rows($result) != 0) {
                $to_fix[] = 'stats_topics';
            }
            mysql_free_result($result);
        }
        $_GET['step'] = 5;
        $_GET['substep'] = 0;
        pauseRepairProcess($to_fix);
    }
    if ($_GET['step'] <= 5) {
        $result = db_query("\n\t\t\tSELECT MAX(ID_TOPIC)\n\t\t\tFROM {$db_prefix}topics", __FILE__, __LINE__);
        list($topics) = mysql_fetch_row($result);
        mysql_free_result($result);
        // Find topics with nonexistent boards.
        for (; $_GET['substep'] < $topics; $_GET['substep'] += 1000) {
            pauseRepairProcess($to_fix, $topics);
            $result = db_query("\n\t\t\t\tSELECT t.ID_TOPIC, t.ID_BOARD\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\tWHERE b.ID_BOARD IS NULL\n\t\t\t\t\tAND t.ID_TOPIC BETWEEN {$_GET['substep']} AND {$_GET['substep']} + 999\n\t\t\t\tORDER BY t.ID_BOARD, t.ID_TOPIC", __FILE__, __LINE__);
            while ($row = mysql_fetch_assoc($result)) {
                $context['repair_errors'][] = sprintf($txt['repair_missing_boards'], $row['ID_TOPIC'], $row['ID_BOARD']);
            }
            if (mysql_num_rows($result) != 0) {
                $to_fix[] = 'missing_boards';
            }
            mysql_free_result($result);
        }
        $_GET['step'] = 6;
        $_GET['substep'] = 0;
        pauseRepairProcess($to_fix);
    }
    if ($_GET['step'] <= 6) {
        // Find boards with nonexistent categories.
        $result = db_query("\n\t\t\tSELECT b.ID_BOARD, b.ID_CAT\n\t\t\tFROM {$db_prefix}boards AS b\n\t\t\t\tLEFT JOIN {$db_prefix}categories AS c ON (c.ID_CAT = b.ID_CAT)\n\t\t\tWHERE c.ID_CAT IS NULL\n\t\t\tORDER BY b.ID_CAT, b.ID_BOARD", __FILE__, __LINE__);
        while ($row = mysql_fetch_assoc($result)) {
            $context['repair_errors'][] = sprintf($txt['repair_missing_categories'], $row['ID_BOARD'], $row['ID_CAT']);
        }
        if (mysql_num_rows($result) != 0) {
            $to_fix[] = 'missing_categories';
        }
        mysql_free_result($result);
        $_GET['step'] = 7;
        $_GET['substep'] = 0;
        pauseRepairProcess($to_fix);
    }
    if ($_GET['step'] <= 7) {
        $result = db_query("\n\t\t\tSELECT MAX(ID_MSG)\n\t\t\tFROM {$db_prefix}messages", __FILE__, __LINE__);
        list($messages) = mysql_fetch_row($result);
        mysql_free_result($result);
        // Find messages with nonexistent members.
        for (; $_GET['substep'] < $messages; $_GET['substep'] += 2000) {
            pauseRepairProcess($to_fix, $messages);
            $result = db_query("\n\t\t\t\tSELECT m.ID_MSG, m.ID_MEMBER\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 mem.ID_MEMBER IS NULL\n\t\t\t\t\tAND m.ID_MEMBER != 0\n\t\t\t\t\tAND m.ID_MSG BETWEEN {$_GET['substep']} AND {$_GET['substep']} + 1999\n\t\t\t\tORDER BY m.ID_MSG", __FILE__, __LINE__);
            while ($row = mysql_fetch_assoc($result)) {
                $context['repair_errors'][] = sprintf($txt['repair_missing_posters'], $row['ID_MSG'], $row['ID_MEMBER']);
            }
            if (mysql_num_rows($result) != 0) {
                $to_fix[] = 'missing_posters';
            }
            mysql_free_result($result);
        }
        $_GET['step'] = 8;
        $_GET['substep'] = 0;
        pauseRepairProcess($to_fix);
    }
    if ($_GET['step'] <= 8) {
        // Find boards with nonexistent parents.
        $result = db_query("\n\t\t\tSELECT b.ID_BOARD, b.ID_PARENT\n\t\t\tFROM {$db_prefix}boards AS b\n\t\t\t\tLEFT JOIN {$db_prefix}boards AS p ON (p.ID_BOARD = b.ID_PARENT)\n\t\t\tWHERE b.ID_PARENT != 0\n\t\t\t\tAND (p.ID_BOARD IS NULL OR p.ID_BOARD = b.ID_BOARD)\n\t\t\tORDER BY b.ID_PARENT, b.ID_BOARD", __FILE__, __LINE__);
        while ($row = mysql_fetch_assoc($result)) {
            $context['repair_errors'][] = sprintf($txt['repair_missing_parents'], $row['ID_BOARD'], $row['ID_PARENT']);
        }
        if (mysql_num_rows($result) != 0) {
            $to_fix[] = 'missing_parents';
        }
        mysql_free_result($result);
        $_GET['step'] = 9;
        $_GET['substep'] = 0;
        pauseRepairProcess($to_fix);
    }
    if ($_GET['step'] <= 9) {
        $result = db_query("\n\t\t\tSELECT MAX(ID_POLL)\n\t\t\tFROM {$db_prefix}topics", __FILE__, __LINE__);
        list($polls) = mysql_fetch_row($result);
        mysql_free_result($result);
        for (; $_GET['substep'] < $polls; $_GET['substep'] += 500) {
            pauseRepairProcess($to_fix, $polls);
            $result = db_query("\n\t\t\t\tSELECT t.ID_POLL, t.ID_TOPIC\n\t\t\t\tFROM {$db_prefix}topics AS t\n\t\t\t\t\tLEFT JOIN {$db_prefix}polls AS p ON (p.ID_POLL = t.ID_POLL)\n\t\t\t\tWHERE t.ID_POLL != 0\n\t\t\t\t\tAND t.ID_POLL BETWEEN {$_GET['substep']} AND {$_GET['substep']} + 499\n\t\t\t\t\tAND p.ID_POLL IS NULL\n\t\t\t\tGROUP BY t.ID_POLL", __FILE__, __LINE__);
            while ($row = mysql_fetch_assoc($result)) {
                $context['repair_errors'][] = sprintf($txt['repair_missing_polls'], $row['ID_TOPIC'], $row['ID_POLL']);
            }
            if (mysql_num_rows($result) != 0) {
                $to_fix[] = 'missing_polls';
            }
            mysql_free_result($result);
        }
        $_GET['step'] = 10;
        $_GET['substep'] = 0;
        pauseRepairProcess($to_fix);
    }
    if ($_GET['step'] <= 10) {
        $result = db_query("\n\t\t\tSELECT MAX(ID_TOPIC)\n\t\t\tFROM {$db_prefix}calendar", __FILE__, __LINE__);
        list($topics) = mysql_fetch_row($result);
        mysql_free_result($result);
        for (; $_GET['substep'] < $topics; $_GET['substep'] += 1000) {
            pauseRepairProcess($to_fix, $topics);
            $result = db_query("\n\t\t\t\tSELECT cal.ID_TOPIC, cal.ID_EVENT\n\t\t\t\tFROM {$db_prefix}calendar AS cal\n\t\t\t\t\tLEFT JOIN {$db_prefix}topics AS t ON (t.ID_TOPIC = cal.ID_TOPIC)\n\t\t\t\tWHERE cal.ID_TOPIC != 0\n\t\t\t\t\tAND cal.ID_TOPIC BETWEEN {$_GET['substep']} AND {$_GET['substep']} + 999\n\t\t\t\t\tAND t.ID_TOPIC IS NULL\n\t\t\t\tORDER BY cal.ID_TOPIC", __FILE__, __LINE__);
            while ($row = mysql_fetch_assoc($result)) {
                $context['repair_errors'][] = sprintf($txt['repair_missing_calendar_topics'], $row['ID_EVENT'], $row['ID_TOPIC']);
            }
            if (mysql_num_rows($result) != 0) {
                $to_fix[] = 'missing_calendar_topics';
            }
            mysql_free_result($result);
        }
        $_GET['step'] = 11;
        $_GET['substep'] = 0;
        pauseRepairProcess($to_fix);
    }
    if ($_GET['step'] <= 11) {
        $result = db_query("\n\t\t\tSELECT MAX(ID_MEMBER)\n\t\t\tFROM {$db_prefix}members", __FILE__, __LINE__);
        list($members) = mysql_fetch_row($result);
        mysql_free_result($result);
        for (; $_GET['substep'] < $members; $_GET['substep'] += 250) {
            pauseRepairProcess($to_fix, $members);
            $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\t\tAND lt.ID_MEMBER BETWEEN {$_GET['substep']} AND {$_GET['substep']} + 249", __FILE__, __LINE__);
            while ($row = mysql_fetch_assoc($result)) {
                $context['repair_errors'][] = sprintf($txt['repair_missing_log_topics'], $row['ID_TOPIC']);
            }
            if (mysql_num_rows($result) != 0 && !in_array('missing_log_topics', $to_fix)) {
                $to_fix[] = 'missing_log_topics';
            }
            mysql_free_result($result);
        }
        $_GET['step'] = 12;
        $_GET['substep'] = 0;
        pauseRepairProcess($to_fix);
    }
    if ($_GET['step'] <= 12) {
        $result = db_query("\n\t\t\tSELECT MAX(ID_MEMBER)\n\t\t\tFROM {$db_prefix}log_topics", __FILE__, __LINE__);
        list($members) = mysql_fetch_row($result);
        mysql_free_result($result);
        for (; $_GET['substep'] < $members; $_GET['substep'] += 150) {
            pauseRepairProcess($to_fix, $members);
            $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\t\tAND lt.ID_MEMBER BETWEEN {$_GET['substep']} AND {$_GET['substep']} + 149\n\t\t\t\tGROUP BY lt.ID_MEMBER", __FILE__, __LINE__);
            while ($row = mysql_fetch_assoc($result)) {
                $context['repair_errors'][] = sprintf($txt['repair_missing_log_topics_members'], $row['ID_MEMBER']);
            }
            if (mysql_num_rows($result) != 0 && !in_array('missing_log_topics_members', $to_fix)) {
                $to_fix[] = 'missing_log_topics_members';
            }
            mysql_free_result($result);
        }
        $_GET['step'] = 13;
        $_GET['substep'] = 0;
        pauseRepairProcess($to_fix);
    }
    if ($_GET['step'] <= 13) {
        $result = db_query("\n\t\t\tSELECT MAX(ID_MEMBER)\n\t\t\tFROM {$db_prefix}log_boards", __FILE__, __LINE__);
        list($members) = mysql_fetch_row($result);
        mysql_free_result($result);
        for (; $_GET['substep'] < $members; $_GET['substep'] += 500) {
            pauseRepairProcess($to_fix, $members);
            $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\t\tAND lb.ID_MEMBER BETWEEN {$_GET['substep']} AND {$_GET['substep']} + 499\n\t\t\t\tGROUP BY lb.ID_BOARD", __FILE__, __LINE__);
            while ($row = mysql_fetch_assoc($result)) {
                $context['repair_errors'][] = sprintf($txt['repair_missing_log_boards'], $row['ID_BOARD']);
            }
            if (mysql_num_rows($result) != 0) {
                $to_fix[] = 'missing_log_boards';
            }
            mysql_free_result($result);
        }
        $_GET['step'] = 14;
        $_GET['substep'] = 0;
        pauseRepairProcess($to_fix);
    }
    if ($_GET['step'] <= 14) {
        $result = db_query("\n\t\t\tSELECT MAX(ID_MEMBER)\n\t\t\tFROM {$db_prefix}log_boards", __FILE__, __LINE__);
        list($members) = mysql_fetch_row($result);
        mysql_free_result($result);
        for (; $_GET['substep'] < $members; $_GET['substep'] += 500) {
            pauseRepairProcess($to_fix, $members);
            $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\t\tAND lb.ID_MEMBER BETWEEN {$_GET['substep']} AND {$_GET['substep']} + 499\n\t\t\t\tGROUP BY lb.ID_MEMBER", __FILE__, __LINE__);
            while ($row = mysql_fetch_assoc($result)) {
                $context['repair_errors'][] = sprintf($txt['repair_missing_log_boards_members'], $row['ID_MEMBER']);
            }
            if (mysql_num_rows($result) != 0) {
                $to_fix[] = 'missing_log_boards_members';
            }
            mysql_free_result($result);
        }
        $_GET['step'] = 15;
        $_GET['substep'] = 0;
        pauseRepairProcess($to_fix);
    }
    if ($_GET['step'] <= 15) {
        $result = db_query("\n\t\t\tSELECT MAX(ID_MEMBER)\n\t\t\tFROM {$db_prefix}log_mark_read", __FILE__, __LINE__);
        list($members) = mysql_fetch_row($result);
        mysql_free_result($result);
        for (; $_GET['substep'] < $members; $_GET['substep'] += 500) {
            pauseRepairProcess($to_fix, $members);
            $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\t\tAND lmr.ID_MEMBER BETWEEN {$_GET['substep']} AND {$_GET['substep']} + 499\n\t\t\t\tGROUP BY lmr.ID_BOARD", __FILE__, __LINE__);
            while ($row = mysql_fetch_assoc($result)) {
                $context['repair_errors'][] = sprintf($txt['repair_missing_log_mark_read'], $row['ID_BOARD']);
            }
            if (mysql_num_rows($result) != 0) {
                $to_fix[] = 'missing_log_mark_read';
            }
            mysql_free_result($result);
        }
        $_GET['step'] = 16;
        $_GET['substep'] = 0;
        pauseRepairProcess($to_fix);
    }
    if ($_GET['step'] <= 16) {
        $result = db_query("\n\t\t\tSELECT MAX(ID_MEMBER)\n\t\t\tFROM {$db_prefix}log_mark_read", __FILE__, __LINE__);
        list($members) = mysql_fetch_row($result);
        mysql_free_result($result);
        for (; $_GET['substep'] < $members; $_GET['substep'] += 500) {
            pauseRepairProcess($to_fix, $members);
            $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\t\tAND lmr.ID_MEMBER BETWEEN {$_GET['substep']} AND {$_GET['substep']} + 499\n\t\t\t\tGROUP BY lmr.ID_MEMBER", __FILE__, __LINE__);
            while ($row = mysql_fetch_assoc($result)) {
                $context['repair_errors'][] = sprintf($txt['repair_missing_log_mark_read_members'], $row['ID_MEMBER']);
            }
            if (mysql_num_rows($result) != 0) {
                $to_fix[] = 'missing_log_mark_read_members';
            }
            mysql_free_result($result);
        }
        $_GET['step'] = 17;
        $_GET['substep'] = 0;
        pauseRepairProcess($to_fix);
    }
    if ($_GET['step'] <= 17) {
        $result = db_query("\n\t\t\tSELECT MAX(ID_PM)\n\t\t\tFROM {$db_prefix}pm_recipients", __FILE__, __LINE__);
        list($pms) = mysql_fetch_row($result);
        mysql_free_result($result);
        for (; $_GET['substep'] < $pms; $_GET['substep'] += 500) {
            pauseRepairProcess($to_fix, $pms);
            $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\t\tAND pmr.ID_PM BETWEEN {$_GET['substep']} AND {$_GET['substep']} + 499\n\t\t\t\tGROUP BY pmr.ID_PM", __FILE__, __LINE__);
            while ($row = mysql_fetch_assoc($result)) {
                $context['repair_errors'][] = sprintf($txt['repair_missing_pms'], $row['ID_PM']);
            }
            if (mysql_num_rows($result) != 0) {
                $to_fix[] = 'missing_pms';
            }
            mysql_free_result($result);
        }
        $_GET['step'] = 18;
        $_GET['substep'] = 0;
        pauseRepairProcess($to_fix);
    }
    if ($_GET['step'] <= 18) {
        $result = db_query("\n\t\t\tSELECT MAX(ID_MEMBER)\n\t\t\tFROM {$db_prefix}pm_recipients", __FILE__, __LINE__);
        list($members) = mysql_fetch_row($result);
        mysql_free_result($result);
        for (; $_GET['substep'] < $members; $_GET['substep'] += 500) {
            pauseRepairProcess($to_fix, $members);
            $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 pmr.ID_MEMBER BETWEEN {$_GET['substep']} AND {$_GET['substep']} + 499\n\t\t\t\t\tAND mem.ID_MEMBER IS NULL\n\t\t\t\tGROUP BY pmr.ID_MEMBER", __FILE__, __LINE__);
            while ($row = mysql_fetch_assoc($result)) {
                $context['repair_errors'][] = sprintf($txt['repair_missing_recipients'], $row['ID_MEMBER']);
            }
            if (mysql_num_rows($result) != 0) {
                $to_fix[] = 'missing_recipients';
            }
            mysql_free_result($result);
        }
        $_GET['step'] = 19;
        $_GET['substep'] = 0;
        pauseRepairProcess($to_fix);
    }
    if ($_GET['step'] <= 19) {
        $result = db_query("\n\t\t\tSELECT MAX(ID_PM)\n\t\t\tFROM {$db_prefix}personal_messages", __FILE__, __LINE__);
        list($pms) = mysql_fetch_row($result);
        mysql_free_result($result);
        for (; $_GET['substep'] < $pms; $_GET['substep'] += 500) {
            pauseRepairProcess($to_fix, $pms);
            $result = db_query("\n\t\t\t\tSELECT pm.ID_PM, pm.ID_MEMBER_FROM\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 pm.ID_PM BETWEEN {$_GET['substep']} AND {$_GET['substep']} + 499\n\t\t\t\t\tAND mem.ID_MEMBER IS NULL", __FILE__, __LINE__);
            while ($row = mysql_fetch_assoc($result)) {
                $context['repair_errors'][] = sprintf($txt['repair_missing_senders'], $row['ID_PM'], $row['ID_MEMBER_FROM']);
            }
            if (mysql_num_rows($result) != 0) {
                $to_fix[] = 'missing_senders';
            }
            mysql_free_result($result);
        }
        $_GET['step'] = 20;
        $_GET['substep'] = 0;
        pauseRepairProcess($to_fix);
    }
    if ($_GET['step'] <= 20) {
        $result = db_query("\n\t\t\tSELECT MAX(ID_MEMBER)\n\t\t\tFROM {$db_prefix}log_notify", __FILE__, __LINE__);
        list($members) = mysql_fetch_row($result);
        mysql_free_result($result);
        for (; $_GET['substep'] < $members; $_GET['substep'] += 500) {
            pauseRepairProcess($to_fix, $members);
            $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 ln.ID_MEMBER BETWEEN {$_GET['substep']} AND {$_GET['substep']} + 499\n\t\t\t\t\tAND mem.ID_MEMBER IS NULL\n\t\t\t\tGROUP BY ln.ID_MEMBER", __FILE__, __LINE__);
            while ($row = mysql_fetch_assoc($result)) {
                $context['repair_errors'][] = sprintf($txt['repair_missing_notify_members'], $row['ID_MEMBER']);
            }
            if (mysql_num_rows($result) != 0) {
                $to_fix[] = 'missing_notify_members';
            }
            mysql_free_result($result);
        }
        $_GET['step'] = 21;
        $_GET['substep'] = 0;
        pauseRepairProcess($to_fix);
    }
    if ($_GET['step'] <= 21) {
        $request = db_query("\n\t\t\tSELECT t.ID_TOPIC, fm.subject\n\t\t\tFROM ({$db_prefix}topics AS t, {$db_prefix}messages AS fm)\n\t\t\t\tLEFT JOIN {$db_prefix}log_search_subjects AS lss ON (lss.ID_TOPIC = t.ID_TOPIC)\n\t\t\tWHERE fm.ID_MSG = t.ID_FIRST_MSG\n\t\t\t\tAND lss.ID_TOPIC IS NULL", __FILE__, __LINE__);
        $found_error = false;
        while ($row = mysql_fetch_assoc($request)) {
            if (count(text2words($row['subject'])) != 0) {
                $context['repair_errors'][] = sprintf($txt['repair_missing_cached_subject'], $row['ID_TOPIC']);
                $found_error = true;
            }
        }
        mysql_free_result($request);
        if ($found_error) {
            $to_fix[] = 'missing_cached_subject';
        }
        $_GET['step'] = 22;
        $_GET['substep'] = 0;
        pauseRepairProcess($to_fix);
    }
    if ($_GET['step'] <= 22) {
        $request = db_query("\n\t\t\tSELECT lss.word\n\t\t\tFROM {$db_prefix}log_search_subjects AS lss\n\t\t\t\tLEFT JOIN {$db_prefix}topics AS t ON (t.ID_TOPIC = lss.ID_TOPIC)\n\t\t\tWHERE t.ID_TOPIC IS NULL", __FILE__, __LINE__);
        while ($row = mysql_fetch_assoc($request)) {
            $context['repair_errors'][] = sprintf($txt['repair_missing_topic_for_cache'], htmlspecialchars($row['word']));
        }
        if (mysql_num_rows($request) != 0) {
            $to_fix[] = 'missing_topic_for_cache';
        }
        mysql_free_result($request);
        $_GET['step'] = 23;
        $_GET['substep'] = 0;
        pauseRepairProcess($to_fix);
    }
    if ($_GET['step'] <= 23) {
        $result = db_query("\n\t\t\tSELECT MAX(ID_MEMBER)\n\t\t\tFROM {$db_prefix}log_polls", __FILE__, __LINE__);
        list($members) = mysql_fetch_row($result);
        mysql_free_result($result);
        for (; $_GET['substep'] < $members; $_GET['substep'] += 500) {
            $result = db_query("\n\t\t\t\tSELECT lp.ID_POLL, 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 lp.ID_MEMBER BETWEEN {$_GET['substep']} AND {$_GET['substep']} + 499\n\t\t\t\t\tAND mem.ID_MEMBER IS NULL\n\t\t\t\tGROUP BY lp.ID_MEMBER", __FILE__, __LINE__);
            while ($row = mysql_fetch_assoc($result)) {
                $context['repair_errors'][] = sprintf($txt['repair_missing_log_poll_member'], $row['ID_POLL'], $row['ID_MEMBER']);
            }
            if (mysql_num_rows($result) != 0) {
                $to_fix[] = 'missing_member_vote';
            }
            mysql_free_result($result);
            pauseRepairProcess($to_fix, $members);
        }
        $_GET['step'] = 24;
        $_GET['substep'] = 0;
        pauseRepairProcess($to_fix);
    }
    if ($_GET['step'] <= 24) {
        $result = db_query("\n\t\t\tSELECT MAX(ID_POLL)\n\t\t\tFROM {$db_prefix}log_polls", __FILE__, __LINE__);
        list($polls) = mysql_fetch_row($result);
        mysql_free_result($result);
        for (; $_GET['substep'] < $polls; $_GET['substep'] += 500) {
            pauseRepairProcess($to_fix, $polls);
            $result = db_query("\n\t\t\t\tSELECT lp.ID_POLL, lp.ID_MEMBER\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 lp.ID_POLL BETWEEN {$_GET['substep']} AND {$_GET['substep']} + 499\n\t\t\t\t\tAND p.ID_POLL IS NULL\n\t\t\t\tGROUP BY lp.ID_POLL", __FILE__, __LINE__);
            while ($row = mysql_fetch_assoc($result)) {
                $context['repair_errors'][] = sprintf($txt['repair_missing_log_poll_vote'], $row['ID_MEMBER'], $row['ID_POLL']);
            }
            if (mysql_num_rows($result) != 0) {
                $to_fix[] = 'missing_log_poll_vote';
            }
            mysql_free_result($result);
        }
        $_GET['step'] = 25;
        $_GET['substep'] = 0;
        pauseRepairProcess($to_fix);
    }
    return $to_fix;
}
Example #2
0
function findForumErrors($do_fix = false)
{
    global $context, $txt, $smcFunc, $errorTests, $db_cache, $db_temp_cache;
    // This may take some time...
    @set_time_limit(600);
    $to_fix = !empty($_SESSION['repairboards_to_fix']) ? $_SESSION['repairboards_to_fix'] : array();
    $context['repair_errors'] = isset($_SESSION['repairboards_to_fix2']) ? $_SESSION['repairboards_to_fix2'] : array();
    $_GET['step'] = empty($_GET['step']) ? 0 : (int) $_GET['step'];
    $_GET['substep'] = empty($_GET['substep']) ? 0 : (int) $_GET['substep'];
    // Don't allow the cache to get too full.
    $db_temp_cache = $db_cache;
    $db_cache = '';
    $context['total_steps'] = count($errorTests);
    // For all the defined error types do the necessary tests.
    $current_step = -1;
    $total_queries = 0;
    foreach ($errorTests as $error_type => $test) {
        $current_step++;
        // Already done this?
        if ($_GET['step'] > $current_step) {
            continue;
        }
        // If we're fixing it but it ain't broke why try?
        if ($do_fix && !in_array($error_type, $to_fix)) {
            $_GET['step']++;
            continue;
        }
        // Has it got substeps?
        if (isset($test['substeps'])) {
            $step_size = isset($test['substeps']['step_size']) ? $test['substeps']['step_size'] : 100;
            $request = $smcFunc['db_query']('', $test['substeps']['step_max'], array());
            list($step_max) = $smcFunc['db_fetch_row']($request);
            $total_queries++;
            $smcFunc['db_free_result']($request);
        }
        // We in theory keep doing this... the substeps.
        $done = false;
        while (!$done) {
            // Make sure there's at least one ID to test.
            if (isset($test['substeps']) && empty($step_max)) {
                break;
            }
            // What is the testing query (Changes if we are testing or fixing)
            if (!$do_fix) {
                $test_query = 'check_query';
            } else {
                $test_query = isset($test['fix_query']) ? 'fix_query' : 'check_query';
            }
            // Do the test...
            $request = $smcFunc['db_query']('', isset($test['substeps']) ? strtr($test[$test_query], array('{STEP_LOW}' => $_GET['substep'], '{STEP_HIGH}' => $_GET['substep'] + $step_size - 1)) : $test[$test_query], array());
            $needs_fix = false;
            // Does it need a fix?
            if (!empty($test['check_type']) && $test['check_type'] == 'count') {
                list($needs_fix) = $smcFunc['db_fetch_row']($request);
            } else {
                $needs_fix = $smcFunc['db_num_rows']($request);
            }
            $total_queries++;
            if ($needs_fix) {
                // What about a message to the user?
                if (!$do_fix) {
                    // Assume need to fix.
                    $found_errors = true;
                    if (isset($test['message'])) {
                        $context['repair_errors'][] = $txt[$test['message']];
                    } elseif (isset($test['messages'])) {
                        while ($row = $smcFunc['db_fetch_assoc']($request)) {
                            $variables = $test['messages'];
                            foreach ($variables as $k => $v) {
                                if ($k == 0 && isset($txt[$v])) {
                                    $variables[$k] = $txt[$v];
                                } elseif ($k > 0 && isset($row[$v])) {
                                    $variables[$k] = $row[$v];
                                }
                            }
                            $context['repair_errors'][] = call_user_func_array('sprintf', $variables);
                        }
                    } elseif (isset($test['message_function'])) {
                        // Find out if there are actually errors.
                        $found_errors = false;
                        while ($row = $smcFunc['db_fetch_assoc']($request)) {
                            $found_errors |= $test['message_function']($row);
                        }
                    }
                    // Actually have something to fix?
                    if ($found_errors) {
                        $to_fix[] = $error_type;
                    }
                } else {
                    // Are we simply getting a collection of ids?
                    if (isset($test['fix_collect'])) {
                        $ids = array();
                        while ($row = $smcFunc['db_fetch_assoc']($request)) {
                            $ids[] = $row[$test['fix_collect']['index']];
                        }
                        if (!empty($ids)) {
                            // Fix it!
                            $test['fix_collect']['process']($ids);
                        }
                    } elseif (isset($test['fix_it_query'])) {
                        $smcFunc['db_query']('', $test['fix_it_query'], array());
                    } elseif (isset($test['fix_processing'])) {
                        while ($row = $smcFunc['db_fetch_assoc']($request)) {
                            $test['fix_processing']($row);
                        }
                    } elseif (isset($test['fix_full_processing'])) {
                        $test['fix_full_processing']($request);
                    }
                    // Do we have other things we need to fix as a result?
                    if (!empty($test['force_fix'])) {
                        foreach ($test['force_fix'] as $item) {
                            if (!in_array($item, $to_fix)) {
                                $to_fix[] = $item;
                            }
                        }
                    }
                }
            }
            // Free the result.
            $smcFunc['db_free_result']($request);
            // Keep memory down.
            $db_cache = '';
            // Are we done yet?
            if (isset($test['substeps'])) {
                $_GET['substep'] += $step_size;
                // Not done?
                if ($_GET['substep'] <= $step_max) {
                    pauseRepairProcess($to_fix, $error_type, $step_max);
                } else {
                    $done = true;
                }
            } else {
                $done = true;
            }
            // Don't allow more than 1000 queries at a time.
            if ($total_queries >= 1000) {
                pauseRepairProcess($to_fix, $error_type, $step_max, true);
            }
        }
        // Keep going.
        $_GET['step']++;
        $_GET['substep'] = 0;
        $to_fix = array_unique($to_fix);
        // If we're doing fixes and this needed a fix and we're all done then don't do it again.
        if ($do_fix) {
            $key = array_search($error_type, $to_fix);
            if ($key !== false && isset($to_fix[$key])) {
                unset($to_fix[$key]);
            }
        }
        // Are we done?
        pauseRepairProcess($to_fix, $error_type);
    }
    // Restore the cache.
    $db_cache = $db_temp_cache;
    return $to_fix;
}