<commit_before><?php

class SV_WarningImprovements_XenForo_Model_Warning_Patch extends XFCP_SV_WarningImprovements_XenForo_Model_Warning_Patch
{
    protected function _userWarningPointsIncreased(
        $userId,
        $newPoints,
        $oldPoints
    ) {
        $actions = $this->getWarningActions();
        if (!$actions)
        {
            return;
        }

        $warningPoints = $this->getCategoryWarningPointsByUser($userId);

        $db = $this->_getDb();
        XenForo_Db::beginTransaction($db);

        foreach ($actions as $action)
        {
            $warningCategoryId = $action['sv_warning_category_id'];

            if (isset($warningPoints[$warningCategoryId]))
            {
                $points = $warningPoints[$warningCategoryId];
            }
            else
            {
                $points = array('old' => $oldPoints, 'new' => $newPoints);
            }

            if ($action['points'] <= $points['old'])
            {
                continue; // already triggered
            }
            elseif ($action['points'] > $points['new'])
            {
                continue; // no trigger yet
            }

            $this->triggerWarningAction($userId, $action);
        }

        XenForo_Db::commit($db);
    }

    protected function _userWarningPointsDecreased(
        $userId,
        $newPoints,
        $oldPoints
    ) {
        $triggers = $this->getUserWarningActionTriggers($userId);
        if (!$triggers)
        {
            return;
        }

        $warningPoints = $this->getCategoryWarningPointsByUser($userId);
        $warningActions = $this->getWarningActions();

        $db = $this->_getDb();
        XenForo_Db::beginTransaction($db);

        foreach ($triggers as $trigger)
        {
            $warningActionId = $trigger['warning_action_id'];
            $warningAction = $warningActions[$warningActionId];
            $warningCategoryId = $warningAction['sv_warning_category_id'];

            if (isset($warningPoints[$warningCategoryId]))
            {
                $points = $warningPoints[$warningCategoryId];
            }
            else
            {
                $points = array('old' => $oldPoints, 'new' => $newPoints);
            }

            if ($trigger['trigger_points'] > $points['new'])
            {
                // points have fallen below trigger, remove it
                $this->removeWarningActionTrigger($userId, $trigger);
            }
        }

        XenForo_Db::commit($db);
    }
}<commit_msg>Fix potential error when an active warning action is deleted<commit_after><?php

class SV_WarningImprovements_XenForo_Model_Warning_Patch extends XFCP_SV_WarningImprovements_XenForo_Model_Warning_Patch
{
    protected function _userWarningPointsIncreased(
        $userId,
        $newPoints,
        $oldPoints
    ) {
        $actions = $this->getWarningActions();
        if (!$actions)
        {
            return;
        }

        $warningPoints = $this->getCategoryWarningPointsByUser($userId);

        $db = $this->_getDb();
        XenForo_Db::beginTransaction($db);

        foreach ($actions as $action)
        {
            $points = array(
                'old' => $oldPoints,
                'new' => $newPoints
            );

            $warningCategoryId = $action['sv_warning_category_id'];
            if (!empty($warningPoints[$warningCategoryId]))
            {
                $points = $warningPoints[$warningCategoryId];
            }

            if ($action['points'] <= $points['old'])
            {
                continue; // already triggered
            }
            elseif ($action['points'] > $points['new'])
            {
                continue; // no trigger yet
            }

            $this->triggerWarningAction($userId, $action);
        }

        XenForo_Db::commit($db);
    }

    protected function _userWarningPointsDecreased(
        $userId,
        $newPoints,
        $oldPoints
    ) {
        $triggers = $this->getUserWarningActionTriggers($userId);
        if (!$triggers)
        {
            return;
        }

        $warningActions = $this->getWarningActions();
        $warningPoints = $this->getCategoryWarningPointsByUser($userId);

        $db = $this->_getDb();
        XenForo_Db::beginTransaction($db);

        foreach ($triggers as $trigger)
        {
            $points = array(
                'old' => $oldPoints,
                'new' => $newPoints
            );

            $warningActionId = $trigger['warning_action_id'];
            if (!empty($warningActions[$warningActionId]))
            {
                $warningAction = $warningActions[$warningActionId];

                $warningCategoryId = $warningAction['sv_warning_category_id'];
                if (!empty($warningPoints[$warningCategoryId]))
                {
                    $points = $warningPoints[$warningCategoryId];
                }
            }

            if ($trigger['trigger_points'] > $points['new'])
            {
                // points have fallen below trigger, remove it
                $this->removeWarningActionTrigger($userId, $trigger);
            }
        }

        XenForo_Db::commit($db);
    }
}
{'fraction_non_alphanumeric': 0.11975474228779459, 'fraction_numerical': 0.0, 'mean_word_length': 1.7517132314180284, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '259415', 'n_tokens_mistral': 1389, 'n_tokens_neox': 1353, 'n_words': 219}