<commit_before><?php
class Vpc_Chained_Abstract_Generator extends Vps_Component_Generator_Abstract
{
    private $_rows = array();

    protected function _init()
    {
        parent::_init();
        $this->_inherits = $this->_getChainedGenerator()->getInherits();
    }

    protected function _getRows($ids)
    {
        $idsToLoad = array();
        foreach ($ids as $i) {
            if (!array_key_exists($i, $this->_rows)) {
                $idsToLoad[] = $i;
            }
        }
        if ($idsToLoad) {
            //nicht $this->getModel, das ist das master model
            $m = Vpc_Abstract::createChildModel($this->_class);
            if (!$m) throw new Vps_Exception("No child model set for '$this->_class'");
            $s = $m->select();
            $s->whereEquals('component_id', $idsToLoad);
            $visible = array();
            Vps_Benchmark::count('DirectoryChainedGenerator getRows', count($idsToLoad));
            foreach ($m->getRows($s) as $r) {
                $this->_rows[$r->component_id] = $r;
            }
            foreach ($idsToLoad as $i) {
                if (!array_key_exists($i, $this->_rows)) {
                    $this->_rows[$i] = null;
                }
            }
        }
        $ret = array();
        foreach ($ids as $i) {
            $ret[$i] = $this->_rows[$i];
        }
        return $ret;
    }

    protected function _getRow($id)
    {
        $r = $this->_getRows(array($id));
        return $r[$id];
    }

    public function getPagesControllerConfig($component)
    {
        $ret = $this->_getChainedGenerator()->getPagesControllerConfig($component, $this->getClass());
        $ret['allowDrag'] = false;
        $ret['allowDrop'] = false;
        $ret['iconEffects'][] = 'chained';
        return $ret;
    }

    protected function _getChainedData($data)
    {
        // TODO: Das ist nicht wirklich korrekt, reicht aber bis jetzt aus
        /*
         * Wenn man eine MasterAsChild hat und Boxen vererbt werden,
         * haben diese Boxen kein chained gesetzt, brauchen es uU.
         * aber. Daher wird nach oben gesucht und die erste chained
         * zurückgegeben, eigentlich sollte aber wieder reingesucht
         * werden, damit es korrekt ist.
         */
        while ($data) {
            if (isset($data->chained)) return $data->chained;
            $data = $data->parent;
        }
        return null;
    }

    protected function _getChainedChildComponents($parentData, $select)
    {
        return $this->_getChainedGenerator()->getChildData(
            $this->_getChainedData($parentData), $this->_getChainedSelect($select)
        );
    }

    protected function _getChainedSelect($select)
    {
        $select = clone $select;
        if ($p = $select->getPart(Vps_Component_Select::WHERE_CHILD_OF_SAME_PAGE)) {
            $cd = $this->_getChainedData($p);
            if (!$cd) $cd = $p; // Falls Data ein parent von Komponente mit chainedType ist
            $select->whereChildOfSamePage($cd);
        }
        if ($cls = $select->getPart(Vps_Component_Select::WHERE_COMPONENT_CLASSES)) {
            foreach ($cls as &$c) {
                $c = substr($c, strpos($c, '.')+1);
            }
            $select->whereComponentClasses($cls);
        }
        if ($sr = $select->getPart(Vps_Component_Select::WHERE_SUBROOT)) {
            $newSr = array();
            foreach ($sr as $i) {
                if (isset($i->chained)) {
                    $newSr[] = $i->chained;
                } else {
                    $newSr[] = $i;
                }
            }
            $select->setPart(Vps_Component_Select::WHERE_SUBROOT, $newSr);
        }
        return $select;
    }

    public function getChildData($parentDatas, $select = array())
    {
        Vps_Benchmark::count('GenChained::getChildData');
        $ret = array();
        if (is_array($select)) $select = new Vps_Component_Select($select);
        if ($id = $select->getPart(Vps_Component_Select::WHERE_ID)) {
            if ($this->_getChainedGenerator() instanceof Vpc_Root_Category_Generator) {
                $select->whereId(substr($id, 1));
            }
        }

        $chainedType = $this->getGeneratorFlag('chainedType');

        $slaveData = $select->getPart(Vps_Component_Select::WHERE_CHILD_OF_SAME_PAGE);
        while ($slaveData) {
            if (Vpc_Abstract::getFlag($slaveData->componentClass, 'chainedType') == $chainedType) {
                break;
            }
            $slaveData = $slaveData->parent;
        }

        $parentDataSelect = new Vps_Component_Select();
        $parentDataSelect->copyParts(array('ignoreVisible'), $select);

        $parentDatas = is_array($parentDatas) ? $parentDatas : array($parentDatas);
        foreach ($parentDatas as $parentData) {
            foreach ($this->_getChainedChildComponents($parentData, $select) as $component) {
                $pData = array();
                if (!$parentData) {
                    $class = "Vpc_Chained_{$chainedType}_Component";
                    if (!$slaveData) {
                        $chainedData = $component;
                        while ($chainedData) {
                            if (Vpc_Abstract::getFlag($chainedData->componentClass, 'chainedType') == $chainedType) {
                                break;
                            }
                            $chainedData = $chainedData->parent;
                        }
                        $slaveDataClass = null;
                        foreach (Vpc_Abstract::getChildComponentClasses($chainedData->parent->componentClass) as $chainedClass) {
                            if ($chainedClass != $chainedData->componentClass &&
                                Vpc_Abstract::getFlag($chainedClass, 'chainedType') == $chainedType
                            ) {
                                $slaveDataClass = substr($chainedClass, 0, strpos($chainedClass, '.'));
                            }
                        }
                        foreach (Vps_Component_Data_Root::getInstance()->getComponentsByClass($slaveDataClass) as $d) {
                            $chainedComponent = call_user_func(array($class, 'getChainedByMaster'), $component->parent, $d, $parentDataSelect);
                            if ($chainedComponent) $pData[] = $chainedComponent;
                        }
                    } else {
                        $chainedComponent = call_user_func(array($class, 'getChainedByMaster'), $component->parent, $slaveData, $parentDataSelect);
                        if ($chainedComponent) $pData = array($chainedComponent);
                    }
                } else {
                    $pData = array($parentData);
                }
                foreach ($pData as $d) {
                    $data = $this->_createData($d, $component, $select);
                    if ($data) {
                        $ret[] = $data;
                    }
                }
            }
        }
        return $ret;
    }

    protected function _getIdFromRow($row)
    {
        if (is_numeric($row->componentId)) return $row->componentId;
        return substr($row->componentId, max(strrpos($row->componentId, '-'),strrpos($row->componentId, '_'))+1);
    }

    protected function _formatConfig($parentData, $row)
    {
        $componentClass = $this->_settings['component'][$this->_settings['masterComponentsMap'][$row->componentClass]];
        $id = $this->_getIdFromRow($row);
        $data = array(
            'componentId' => $parentData->componentId.$this->getIdSeparator().$id,
            'dbId' => $parentData->dbId.$this->getIdSeparator().$id,
            'componentClass' => $componentClass,
            'parent' => $parentData,
            'chained' => $row,
            'isPage' => $row->isPage,
            'isPseudoPage' => $row->isPseudoPage,
        );
        if (isset($row->filename)) {
            $data['filename'] = $row->filename;
        }
        if (isset($row->name)) {
            $data['name'] = $row->name;
        }
        if (isset($row->box)) {
            $data['box'] = $row->box;
        }
        if (isset($row->row)) {<commit_msg>fehlende _getComponentIdFromRow methode fuer Chained Generator implementiert<commit_after><?php
class Vpc_Chained_Abstract_Generator extends Vps_Component_Generator_Abstract
{
    private $_rows = array();

    protected function _init()
    {
        parent::_init();
        $this->_inherits = $this->_getChainedGenerator()->getInherits();
    }

    protected function _getRows($ids)
    {
        $idsToLoad = array();
        foreach ($ids as $i) {
            if (!array_key_exists($i, $this->_rows)) {
                $idsToLoad[] = $i;
            }
        }
        if ($idsToLoad) {
            //nicht $this->getModel, das ist das master model
            $m = Vpc_Abstract::createChildModel($this->_class);
            if (!$m) throw new Vps_Exception("No child model set for '$this->_class'");
            $s = $m->select();
            $s->whereEquals('component_id', $idsToLoad);
            $visible = array();
            Vps_Benchmark::count('DirectoryChainedGenerator getRows', count($idsToLoad));
            foreach ($m->getRows($s) as $r) {
                $this->_rows[$r->component_id] = $r;
            }
            foreach ($idsToLoad as $i) {
                if (!array_key_exists($i, $this->_rows)) {
                    $this->_rows[$i] = null;
                }
            }
        }
        $ret = array();
        foreach ($ids as $i) {
            $ret[$i] = $this->_rows[$i];
        }
        return $ret;
    }

    protected function _getRow($id)
    {
        $r = $this->_getRows(array($id));
        return $r[$id];
    }

    public function getPagesControllerConfig($component)
    {
        $ret = $this->_getChainedGenerator()->getPagesControllerConfig($component, $this->getClass());
        $ret['allowDrag'] = false;
        $ret['allowDrop'] = false;
        $ret['iconEffects'][] = 'chained';
        return $ret;
    }

    protected function _getChainedData($data)
    {
        // TODO: Das ist nicht wirklich korrekt, reicht aber bis jetzt aus
        /*
         * Wenn man eine MasterAsChild hat und Boxen vererbt werden,
         * haben diese Boxen kein chained gesetzt, brauchen es uU.
         * aber. Daher wird nach oben gesucht und die erste chained
         * zurückgegeben, eigentlich sollte aber wieder reingesucht
         * werden, damit es korrekt ist.
         */
        while ($data) {
            if (isset($data->chained)) return $data->chained;
            $data = $data->parent;
        }
        return null;
    }

    protected function _getChainedChildComponents($parentData, $select)
    {
        return $this->_getChainedGenerator()->getChildData(
            $this->_getChainedData($parentData), $this->_getChainedSelect($select)
        );
    }

    protected function _getChainedSelect($select)
    {
        $select = clone $select;
        if ($p = $select->getPart(Vps_Component_Select::WHERE_CHILD_OF_SAME_PAGE)) {
            $cd = $this->_getChainedData($p);
            if (!$cd) $cd = $p; // Falls Data ein parent von Komponente mit chainedType ist
            $select->whereChildOfSamePage($cd);
        }
        if ($cls = $select->getPart(Vps_Component_Select::WHERE_COMPONENT_CLASSES)) {
            foreach ($cls as &$c) {
                $c = substr($c, strpos($c, '.')+1);
            }
            $select->whereComponentClasses($cls);
        }
        if ($sr = $select->getPart(Vps_Component_Select::WHERE_SUBROOT)) {
            $newSr = array();
            foreach ($sr as $i) {
                if (isset($i->chained)) {
                    $newSr[] = $i->chained;
                } else {
                    $newSr[] = $i;
                }
            }
            $select->setPart(Vps_Component_Select::WHERE_SUBROOT, $newSr);
        }
        return $select;
    }

    public function getChildData($parentDatas, $select = array())
    {
        Vps_Benchmark::count('GenChained::getChildData');
        $ret = array();
        if (is_array($select)) $select = new Vps_Component_Select($select);
        if ($id = $select->getPart(Vps_Component_Select::WHERE_ID)) {
            if ($this->_getChainedGenerator() instanceof Vpc_Root_Category_Generator) {
                $select->whereId(substr($id, 1));
            }
        }

        $chainedType = $this->getGeneratorFlag('chainedType');

        $slaveData = $select->getPart(Vps_Component_Select::WHERE_CHILD_OF_SAME_PAGE);
        while ($slaveData) {
            if (Vpc_Abstract::getFlag($slaveData->componentClass, 'chainedType') == $chainedType) {
                break;
            }
            $slaveData = $slaveData->parent;
        }

        $parentDataSelect = new Vps_Component_Select();
        $parentDataSelect->copyParts(array('ignoreVisible'), $select);

        $parentDatas = is_array($parentDatas) ? $parentDatas : array($parentDatas);
        foreach ($parentDatas as $parentData) {
            foreach ($this->_getChainedChildComponents($parentData, $select) as $component) {
                $pData = array();
                if (!$parentData) {
                    $class = "Vpc_Chained_{$chainedType}_Component";
                    if (!$slaveData) {
                        $chainedData = $component;
                        while ($chainedData) {
                            if (Vpc_Abstract::getFlag($chainedData->componentClass, 'chainedType') == $chainedType) {
                                break;
                            }
                            $chainedData = $chainedData->parent;
                        }
                        $slaveDataClass = null;
                        foreach (Vpc_Abstract::getChildComponentClasses($chainedData->parent->componentClass) as $chainedClass) {
                            if ($chainedClass != $chainedData->componentClass &&
                                Vpc_Abstract::getFlag($chainedClass, 'chainedType') == $chainedType
                            ) {
                                $slaveDataClass = substr($chainedClass, 0, strpos($chainedClass, '.'));
                            }
                        }
                        foreach (Vps_Component_Data_Root::getInstance()->getComponentsByClass($slaveDataClass) as $d) {
                            $chainedComponent = call_user_func(array($class, 'getChainedByMaster'), $component->parent, $d, $parentDataSelect);
                            if ($chainedComponent) $pData[] = $chainedComponent;
                        }
                    } else {
                        $chainedComponent = call_user_func(array($class, 'getChainedByMaster'), $component->parent, $slaveData, $parentDataSelect);
                        if ($chainedComponent) $pData = array($chainedComponent);
                    }
                } else {
                    $pData = array($parentData);
                }
                foreach ($pData as $d) {
                    $data = $this->_createData($d, $component, $select);
                    if ($data) {
                        $ret[] = $data;
                    }
                }
            }
        }
        return $ret;
    }

    protected function _getIdFromRow($row)
    {
        if (is_numeric($row->componentId)) return $row->componentId;
        return substr($row->componentId, max(strrpos($row->componentId, '-'),strrpos($row->componentId, '_'))+1);
    }

    
    protected function _getComponentIdFromRow($parentData, $row)
    {
        return $parentData->componentId.$this->getIdSeparator().$this->_getIdFromRow($row);
    }

    protected function _formatConfig($parentData, $row)
    {
        $componentClass = $this->_settings['component'][$this->_settings['masterComponentsMap'][$row->componentClass]];
        $id = $this->_getIdFromRow($row);
        $data = array(
            'componentId' => $this->_getComponentIdFromRow($parentData, $row),
            'dbId' => $parentData->dbId.$this->getIdSeparator().$id,
            'componentClass' => $componentClass,
            'parent' => $parentData,
            'chained' => $row,
            'isPage' => $row->isPage,
            'isPseudoPage' => $row->isPseudoPage,
        );
        if (isset($row->filename)) {
            $data['filename'] = $row->filename;
        }
        if (isset($row->name)) {
            $data['name'] = $row->name;
        }
        if (isset($row->box)) {
            $data['box'] = $row->box;
        }
        if (isset($row->row)) {
{'fraction_non_alphanumeric': 0.13606794362125046, 'fraction_numerical': 0.00048186965425852307, 'mean_word_length': 1.801721228484644, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 169, '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': '1345314', 'n_tokens_mistral': 4686, 'n_tokens_neox': 4589, 'n_words': 837}