<commit_before><?php
abstract class Kwf_Component_Renderer_Abstract
{
    protected $_enableCache = null;
    protected $_renderComponent;

    protected $_includedComponents = array();
    protected $_minLifetime;

    public function includedComponent($targetComponentId, $targetType)
    {
        $this->_includedComponents[] = $targetComponentId;
    }

    public function __construct()
    {
        $this->_enableCache = !Kwf_Config::getValue('debug.componentCache.disable');
        if (Kwf_Component_Data_Root::getShowInvisible()) {
            $this->_enableCache = false;
        }
    }

    public function setEnableCache($enableCache)
    {
        if ($enableCache === null) throw new Kwf_Exception('expected boolean parameter');
        $this->_enableCache = $enableCache;
    }

    public function renderComponent($component)
    {
        $this->_renderComponent = $component;

        $helper = new Kwf_Component_View_Helper_Component();
        $helper->setRenderer($this);
        $content = $helper->component($component);

        $ret = $this->_render(2, $content);
        Kwf_Component_Cache::getInstance()->writeBuffer();
        return $ret;
    }

    protected abstract function _getRendererName();

    public function getTemplate(Kwf_Component_Data $component, $type)
    {
        $template = Kwc_Abstract::getTemplateFile($component->componentClass, $type);
        if (!$template) throw new Kwf_Exception("No $type-Template found for '{$component->componentClass}'");
        return $template;
    }

    public function render($ret)
    {
        $ret = $this->_render(2, $ret);
        return $ret;
    }

    private function _executePlugins($ret, $pluginType)
    {
        static $benchmarkEnabled;
        if (!isset($benchmarkEnabled)) $benchmarkEnabled = Kwf_Benchmark::isEnabled();

        while (($start = strpos($ret, '<plugin'.$pluginType.' ')) !== false) {
            $startEnd = strpos($ret, '>', $start);
            $args = explode(' ', substr($ret, $start+9, $startEnd-$start-9));
            $end = strpos($ret, '</plugin'.$pluginType.' '.$args[0].'>');
            $content = substr($ret, $startEnd+1, $end-$startEnd-1);
            if ($benchmarkEnabled) $startTime = microtime(true);
            $plugin = Kwf_Component_Plugin_Abstract::getInstance($args[1], $args[2]);
            if ($pluginType == 'C') {
                if (!$plugin->useViewCache($this)) {
                    $c = Kwf_Component_Data_Root::getInstance()->getComponentById($args[2], array('ignoreVisible'=>true));
                    $content = $this->renderComponent($c);
                }
            } else if ($pluginType == 'A' || $pluginType == 'B') {
                $content = $plugin->processOutput($content, $this);
            } else if ($pluginType == 'R') {
                $c = $plugin->replaceOutput($this);
                if ($c !== false) {
                    $content = $c;
                }
            }

            if ($benchmarkEnabled) Kwf_Benchmark::subCheckpoint('plugin '.$args[1], microtime(true)-$startTime);
            $ret = substr($ret, 0, $start).$content.substr($ret, $end+11+strlen($args[0]));
        }
        return $ret;
    }

    /**
     * Render components (ie. expand <cc ...>)
     *
     * @param int render pass; 1 or 2: 1 for content that can be stored in fullPage cache,
                                       2 for everything else. 2 includes 1, so calling just with 2 also works
     * @param string render content
     */
    protected function _render($pass, $ret)
    {
        static $benchmarkEnabled;
        if (!isset($benchmarkEnabled)) $benchmarkEnabled = Kwf_Benchmark::isEnabled();

        $pluginNr = 0;
        $helpers = array();

        if ($pass == 2) {
            //in second pass execute all EXECUTE_BEFORE plugins
            $ret = $this->_executePlugins($ret, 'B');
        }
        $offset = 0;
        while (($start = strpos($ret, '<kwc', $offset)) !== false) {
            $p = substr($ret, $start+4, 1);
            if ($pass == 1 && $p == 2) {
                $offset = $start+1;
                continue; //matches $pass? (first char after {cc)
            }
            if ($benchmarkEnabled) $startTime = microtime(true);
            $end = strpos($ret, '>', $start);

            $args = explode(' ', substr($ret, $start+6, $end-$start-6));
            $type = $args[0];
            $componentId = $args[1];<commit_msg>update comment to match actual output<commit_after><?php
abstract class Kwf_Component_Renderer_Abstract
{
    protected $_enableCache = null;
    protected $_renderComponent;

    protected $_includedComponents = array();
    protected $_minLifetime;

    public function includedComponent($targetComponentId, $targetType)
    {
        $this->_includedComponents[] = $targetComponentId;
    }

    public function __construct()
    {
        $this->_enableCache = !Kwf_Config::getValue('debug.componentCache.disable');
        if (Kwf_Component_Data_Root::getShowInvisible()) {
            $this->_enableCache = false;
        }
    }

    public function setEnableCache($enableCache)
    {
        if ($enableCache === null) throw new Kwf_Exception('expected boolean parameter');
        $this->_enableCache = $enableCache;
    }

    public function renderComponent($component)
    {
        $this->_renderComponent = $component;

        $helper = new Kwf_Component_View_Helper_Component();
        $helper->setRenderer($this);
        $content = $helper->component($component);

        $ret = $this->_render(2, $content);
        Kwf_Component_Cache::getInstance()->writeBuffer();
        return $ret;
    }

    protected abstract function _getRendererName();

    public function getTemplate(Kwf_Component_Data $component, $type)
    {
        $template = Kwc_Abstract::getTemplateFile($component->componentClass, $type);
        if (!$template) throw new Kwf_Exception("No $type-Template found for '{$component->componentClass}'");
        return $template;
    }

    public function render($ret)
    {
        $ret = $this->_render(2, $ret);
        return $ret;
    }

    private function _executePlugins($ret, $pluginType)
    {
        static $benchmarkEnabled;
        if (!isset($benchmarkEnabled)) $benchmarkEnabled = Kwf_Benchmark::isEnabled();

        while (($start = strpos($ret, '<plugin'.$pluginType.' ')) !== false) {
            $startEnd = strpos($ret, '>', $start);
            $args = explode(' ', substr($ret, $start+9, $startEnd-$start-9));
            $end = strpos($ret, '</plugin'.$pluginType.' '.$args[0].'>');
            $content = substr($ret, $startEnd+1, $end-$startEnd-1);
            if ($benchmarkEnabled) $startTime = microtime(true);
            $plugin = Kwf_Component_Plugin_Abstract::getInstance($args[1], $args[2]);
            if ($pluginType == 'C') {
                if (!$plugin->useViewCache($this)) {
                    $c = Kwf_Component_Data_Root::getInstance()->getComponentById($args[2], array('ignoreVisible'=>true));
                    $content = $this->renderComponent($c);
                }
            } else if ($pluginType == 'A' || $pluginType == 'B') {
                $content = $plugin->processOutput($content, $this);
            } else if ($pluginType == 'R') {
                $c = $plugin->replaceOutput($this);
                if ($c !== false) {
                    $content = $c;
                }
            }

            if ($benchmarkEnabled) Kwf_Benchmark::subCheckpoint('plugin '.$args[1], microtime(true)-$startTime);
            $ret = substr($ret, 0, $start).$content.substr($ret, $end+11+strlen($args[0]));
        }
        return $ret;
    }

    /**
     * Render components (ie. expand <kwc ...>)
     *
     * @param int render pass; 1 or 2: 1 for content that can be stored in fullPage cache,
                                       2 for everything else. 2 includes 1, so calling just with 2 also works
     * @param string render content
     */
    protected function _render($pass, $ret)
    {
        static $benchmarkEnabled;
        if (!isset($benchmarkEnabled)) $benchmarkEnabled = Kwf_Benchmark::isEnabled();

        $pluginNr = 0;
        $helpers = array();

        if ($pass == 2) {
            //in second pass execute all EXECUTE_BEFORE plugins
            $ret = $this->_executePlugins($ret, 'B');
        }
        $offset = 0;
        while (($start = strpos($ret, '<kwc', $offset)) !== false) {
            $p = substr($ret, $start+4, 1);
            if ($pass == 1 && $p == 2) {
                $offset = $start+1;
                continue; //matches $pass? (first char after <kwc)
            }
            if ($benchmarkEnabled) $startTime = microtime(true);
            $end = strpos($ret, '>', $start);

            $args = explode(' ', substr($ret, $start+6, $end-$start-6));
            $type = $args[0];
            $componentId = $args[1];
{'fraction_non_alphanumeric': 0.1500449236298293, 'fraction_numerical': 0.0076370170709793355, 'mean_word_length': 2.327727952167414, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4919827', 'n_tokens_mistral': 2568, 'n_tokens_neox': 2469, 'n_words': 562}