<commit_before><?php
/**
 * Runner that executes update scripts
 */
class Kwf_Util_Update_Runner
{
    private $_updates;
    private $_debug = false;
    private $_skipClearCache = false;
    private $_verbose = false;
    /**
     * @var Zend_ProgressBar
     */
    private $_progressBar = null;

    public function __construct($updates)
    {
        $this->_updates = $updates;
    }

    public function setSkipClearCache($v)
    {
        $this->_skipClearCache = (bool)$v;
    }

    /**
     * Enabled debug mode. In debug mode when an update script causes an execption
     * the backtrace is shown and the update is stopped. This is useful for debugging
     * update scripts
     */
    public function setEnableDebug($v)
    {
        $this->_debug = (bool)$v;
    }

    /**
     * If verbose is enabled output is echoed while executing update scripts
     */
    public function setVerbose($v)
    {
        $this->_verbose = (bool)$v;
    }

    public function setProgressBar(Zend_ProgressBar $progressBar)
    {
        $this->_progressBar = $progressBar;
    }
    public function writeExecutedUpdates($doneNames)
    {
        Kwf_Registry::get('db')->query("UPDATE kwf_update SET data=?", serialize($doneNames));
        if (file_exists('update')) {
            //move away old update file to avoid confusion
            rename('update', 'update.backup');
        }
    }

    /**
     * Executes checkSettings method for all update scripts which should check if everything
     * is set up correctly to execute the update script
     */
    public function checkUpdatesSettings()
    {
        return $this->_executeUpdatesAction('checkSettings');
    }

    //TODO eventually move maintenance?
    //TODO support a progess bar, including progress steps for a single update script
    //TODO don't output anything (use output buffer for eventual output in update scripts??)
    public function executeUpdates()
    {
        $doneNames = array();

        Kwf_Util_Maintenance::writeMaintenanceBootstrap();

        $this->_executeUpdatesAction('preUpdate');
        $this->_executeUpdatesAction('update');
        $this->_executeUpdatesAction('postUpdate');
        if (!$this->_skipClearCache) {
            if ($this->_verbose) echo "\n";
            Kwf_Util_ClearCache::getInstance()->clearCache('all', !$this->_verbose);
            if ($this->_verbose) echo "\n";
        }
        $this->_executeUpdatesAction('postClearCache');
        foreach ($this->_updates as $k=>$u) {
            $doneNames[] = $u->getUniqueName();
        }

        Kwf_Util_Maintenance::restoreMaintenanceBootstrap();

        return $doneNames;
    }

    private function _executeUpdatesAction($method)
    {
        $ret = true;
        foreach ($this->_updates as $update) {
            if ($method != 'checkSettings') {
                if ($method != 'postClearCache' && !$this->_skipClearCache) {
                    Kwf_Util_ClearCache::getInstance()->clearCache('all', false, false);
                }
                Kwf_Model_Abstract::clearInstances(); //wegen eventueller meta-data-caches die sich geändert haben
                Kwf_Component_Generator_Abstract::clearInstances();
                Kwf_Component_Data_Root::reset();
            }
            if ($this->_progressBar && $method == 'update') {
                $this->_progressBar->next(1, $update->getUniqueName());
            }
            $e = false;
            try {
                if (!$this->_verbose) {
                    ob_start(); //update script should not output anything, if it still does discard it
                }
                $update->$method();
                if (!$this->_verbose) ob_end_clean();
            } catch (Exception $e) {
                if (!$this->_verbose) ob_end_clean();
                if ($this->_debug) throw $e;
                if ($method == 'checkSettings') {
                    echo get_class($update);
                }
                echo "\n\033[31mError:\033[0m\n";
                echo $e->getMessage()."\n\n";
                flush();
                $ret = false;
            }
        }
        return $ret;
    }
}
<commit_msg>simplify by not using _executeUpdatesAction for checkSettings<commit_after><?php
/**
 * Runner that executes update scripts
 */
class Kwf_Util_Update_Runner
{
    private $_updates;
    private $_debug = false;
    private $_skipClearCache = false;
    private $_verbose = false;
    /**
     * @var Zend_ProgressBar
     */
    private $_progressBar = null;

    public function __construct($updates)
    {
        $this->_updates = $updates;
    }

    public function setSkipClearCache($v)
    {
        $this->_skipClearCache = (bool)$v;
    }

    /**
     * Enabled debug mode. In debug mode when an update script causes an execption
     * the backtrace is shown and the update is stopped. This is useful for debugging
     * update scripts
     */
    public function setEnableDebug($v)
    {
        $this->_debug = (bool)$v;
    }

    /**
     * If verbose is enabled output is echoed while executing update scripts
     */
    public function setVerbose($v)
    {
        $this->_verbose = (bool)$v;
    }

    public function setProgressBar(Zend_ProgressBar $progressBar)
    {
        $this->_progressBar = $progressBar;
    }
    public function writeExecutedUpdates($doneNames)
    {
        Kwf_Registry::get('db')->query("UPDATE kwf_update SET data=?", serialize($doneNames));
        if (file_exists('update')) {
            //move away old update file to avoid confusion
            rename('update', 'update.backup');
        }
    }

    /**
     * Executes checkSettings method for all update scripts which should check if everything
     * is set up correctly to execute the update script
     */
    public function checkUpdatesSettings()
    {
        $ret = true;
        foreach ($this->_updates as $update) {
            $e = false;
            try {
                $update->checkSettings();
            } catch (Exception $e) {
                throw $e; //no special handling for now, maybe something better should be done
                $ret = false;
            }
        }
        return $ret;
    }

    //TODO eventually move maintenance?
    //TODO support a progess bar, including progress steps for a single update script
    //TODO don't output anything (use output buffer for eventual output in update scripts??)
    public function executeUpdates()
    {
        $doneNames = array();

        Kwf_Util_Maintenance::writeMaintenanceBootstrap();

        $this->_executeUpdatesAction('preUpdate');
        $this->_executeUpdatesAction('update');
        $this->_executeUpdatesAction('postUpdate');
        if (!$this->_skipClearCache) {
            if ($this->_verbose) echo "\n";
            Kwf_Util_ClearCache::getInstance()->clearCache('all', !$this->_verbose);
            if ($this->_verbose) echo "\n";
        }
        $this->_executeUpdatesAction('postClearCache');
        foreach ($this->_updates as $k=>$u) {
            $doneNames[] = $u->getUniqueName();
        }

        Kwf_Util_Maintenance::restoreMaintenanceBootstrap();

        return $doneNames;
    }

    private function _executeUpdatesAction($method)
    {
        $ret = true;
        foreach ($this->_updates as $update) {
            if ($method != 'postClearCache' && !$this->_skipClearCache) {
                Kwf_Util_ClearCache::getInstance()->clearCache('all', false, false);
            }
            Kwf_Model_Abstract::clearInstances(); //wegen eventueller meta-data-caches die sich geändert haben
            Kwf_Component_Generator_Abstract::clearInstances();
            Kwf_Component_Data_Root::reset();

            if ($this->_progressBar && $method == 'update') {
                $this->_progressBar->next(1, $update->getUniqueName());
            }
            $e = false;
            try {
                if (!$this->_verbose) {
                    ob_start(); //update script should not output anything, if it still does discard it
                }
                $update->$method();
                if (!$this->_verbose) ob_end_clean();
            } catch (Exception $e) {
                if (!$this->_verbose) ob_end_clean();
                if ($this->_debug) throw $e;
                echo "\n\033[31mError:\033[0m\n";
                echo $e->getMessage()."\n\n";
                flush();
                $ret = false;
            }
        }
        return $ret;
    }
}

{'fraction_non_alphanumeric': 0.1185682326621924, 'fraction_numerical': 0.0023548804898151417, 'mean_word_length': 2.0488155061019384, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 68, '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': '4522173', 'n_tokens_mistral': 2344, 'n_tokens_neox': 2242, 'n_words': 582}