<commit_before><?php
class Kwc_Basic_LinkTag_Intern_Events extends Kwc_Abstract_Events
{
    private $_pageIds;

    public function getListeners()
    {
        $ret = parent::getListeners();
        if (Kwc_Abstract::createOwnModel($this->_class) instanceof Kwc_Basic_LinkTag_Intern_Model) {
            $ret[] = array(
                'class' => null,
                'event' => 'Kwf_Component_Event_Page_RecursiveUrlChanged',
                'callback' => 'onRecursiveUrlChanged'
            );
            $ret[] = array(
                'class' => null,
                'event' => 'Kwf_Component_Event_Component_RecursiveRemoved',
                'callback' => 'onRecursiveRemovedAdded'
            );
            $ret[] = array(
                'class' => null,
                'event' => 'Kwf_Component_Event_Component_RecursiveAdded',
                'callback' => 'onRecursiveRemovedAdded'
            );
            $ret[] = array(
                'class' => null,
                'event' => 'Kwf_Component_Event_Page_Added',
                'callback' => 'onPageRemovedAdded'
            );
            $ret[] = array(
                'class' => null,
                'event' => 'Kwf_Component_Event_Page_Removed',
                'callback' => 'onPageRemovedAdded'
            );
        }
        return $ret;
    }

    //usually child componets can be deleted using %, but not those from pages table as the ids always start with numeric
    //this method returns all child ids needed for deleting recursively
    private function _getIdsFromRecursiveEvent(Kwf_Component_Event_Component_RecursiveAbstract $event)
    {
        $c = $event->component;
        $ids = array($c->dbId);
        $c = $c->getPageOrRoot();
        foreach (Kwf_Component_Data_Root::getInstance()->getPageGenerators() as $gen) {
            $ids = array_merge($ids, $gen->getVisiblePageChildIds($c->dbId));
        }
        return $ids;
    }

    public function onRecursiveUrlChanged(Kwf_Component_Event_Page_RecursiveUrlChanged $event)
    {
        foreach ($this->_getIdsFromRecursiveEvent($event) as $childPageId) {
            foreach ($this->_getDbIds($childPageId) as $dbId) {
                foreach (Kwf_Component_Data_Root::getInstance()->getComponentsByDbId($dbId) as $c) {
                    $this->fireEvent(new Kwf_Component_Event_Component_ContentChanged($this->_class, $c));
                    if ($c->isPage) {
                        $this->fireEvent(new Kwf_Component_Event_Page_UrlChanged($this->_class, $c));
                    }
                }
            }
        }
    }

    public function onRecursiveRemovedAdded(Kwf_Component_Event_Component_RecursiveAbstract $event)
    {
        foreach ($this->_getIdsFromRecursiveEvent($event) as $childPageId) {
            foreach ($this->_getDbIds($childPageId) as $dbId) {
                foreach (Kwf_Component_Data_Root::getInstance()->getComponentsByDbId($dbId) as $c) {
                    $this->fireEvent(new Kwf_Component_Event_Component_ContentChanged($this->_class, $c));
                    if ($c->isPage) {
                        $this->fireEvent(new Kwf_Component_Event_Page_UrlChanged($this->_class, $c));
                    }
                }
            }
        }
    }

    public function onPageRemovedAdded(Kwf_Component_Event_Component_AbstractFlag $event)
    {
        foreach ($this->_getDbIds($event->component->dbId) as $dbId) {
            foreach (Kwf_Component_Data_Root::getInstance()->getComponentsByDbId($dbId) as $c) {
                $this->fireEvent(new Kwf_Component_Event_Component_ContentChanged($this->_class, $c));
                if ($c->isPage) {
                    $this->fireEvent(new Kwf_Component_Event_Page_UrlChanged($this->_class, $c));
                }
            }
        }
    }

    protected function _onOwnRowUpdate(Kwf_Component_Data $c, Kwf_Component_Event_Row_Abstract $event)
    {
        parent::_onOwnRowUpdate($c, $event);
        $this->_pageIds = null;
    }

    private function _getDbIds($targetId)
    {
        if (!$this->_pageIds) {
            $ids = array();
            $model = Kwc_Abstract::createOwnModel($this->_class);
            foreach ($model->export(Kwf_Model_Abstract::FORMAT_ARRAY) as $row) {
                $target = $row['target'];
                if (!isset($ids[$target])) $ids[$target] = array();
                $ids[$target][] = $row['component_id'];
            }
            $this->_pageIds = $ids;
        }
        $ret = array();
        foreach ($this->_pageIds as $targetPageId => $dbIds) {
            if ($targetPageId == $targetId
                || substr($targetPageId, 0, strlen($targetId)+1) == $targetId.'-'
                || substr($targetPageId, 0, strlen($targetId)+1) == $targetId.'_'
            ) {
                $ret = array_merge($ret, $dbIds);
            }
        }
        return $ret;
    }<commit_msg>add a param to check if subpages should be included for _getDbIds in KwcBasicLinkTagInternEvents<commit_after><?php
class Kwc_Basic_LinkTag_Intern_Events extends Kwc_Abstract_Events
{
    private $_pageIds;

    public function getListeners()
    {
        $ret = parent::getListeners();
        if (Kwc_Abstract::createOwnModel($this->_class) instanceof Kwc_Basic_LinkTag_Intern_Model) {
            $ret[] = array(
                'class' => null,
                'event' => 'Kwf_Component_Event_Page_RecursiveUrlChanged',
                'callback' => 'onRecursiveUrlChanged'
            );
            $ret[] = array(
                'class' => null,
                'event' => 'Kwf_Component_Event_Component_RecursiveRemoved',
                'callback' => 'onRecursiveRemovedAdded'
            );
            $ret[] = array(
                'class' => null,
                'event' => 'Kwf_Component_Event_Component_RecursiveAdded',
                'callback' => 'onRecursiveRemovedAdded'
            );
            $ret[] = array(
                'class' => null,
                'event' => 'Kwf_Component_Event_Page_Added',
                'callback' => 'onPageRemovedAdded'
            );
            $ret[] = array(
                'class' => null,
                'event' => 'Kwf_Component_Event_Page_Removed',
                'callback' => 'onPageRemovedAdded'
            );
        }
        return $ret;
    }

    //usually child componets can be deleted using %, but not those from pages table as the ids always start with numeric
    //this method returns all child ids needed for deleting recursively
    private function _getIdsFromRecursiveEvent(Kwf_Component_Event_Component_RecursiveAbstract $event)
    {
        $c = $event->component;
        $ids = array($c->dbId);
        $c = $c->getPageOrRoot();
        foreach (Kwf_Component_Data_Root::getInstance()->getPageGenerators() as $gen) {
            $ids = array_merge($ids, $gen->getVisiblePageChildIds($c->dbId));
        }
        return $ids;
    }

    public function onRecursiveUrlChanged(Kwf_Component_Event_Page_RecursiveUrlChanged $event)
    {
        foreach ($this->_getIdsFromRecursiveEvent($event) as $childPageId) {
            foreach ($this->_getDbIds($childPageId, true) as $dbId) {
                foreach (Kwf_Component_Data_Root::getInstance()->getComponentsByDbId($dbId) as $c) {
                    $this->fireEvent(new Kwf_Component_Event_Component_ContentChanged($this->_class, $c));
                    if ($c->isPage) {
                        $this->fireEvent(new Kwf_Component_Event_Page_UrlChanged($this->_class, $c));
                    }
                }
            }
        }
    }

    public function onRecursiveRemovedAdded(Kwf_Component_Event_Component_RecursiveAbstract $event)
    {
        foreach ($this->_getIdsFromRecursiveEvent($event) as $childPageId) {
            foreach ($this->_getDbIds($childPageId, true) as $dbId) {
                foreach (Kwf_Component_Data_Root::getInstance()->getComponentsByDbId($dbId) as $c) {
                    $this->fireEvent(new Kwf_Component_Event_Component_ContentChanged($this->_class, $c));
                    if ($c->isPage) {
                        $this->fireEvent(new Kwf_Component_Event_Page_UrlChanged($this->_class, $c));
                    }
                }
            }
        }
    }

    public function onPageRemovedAdded(Kwf_Component_Event_Component_AbstractFlag $event)
    {
        foreach ($this->_getDbIds($event->component->dbId, false) as $dbId) {
            foreach (Kwf_Component_Data_Root::getInstance()->getComponentsByDbId($dbId) as $c) {
                $this->fireEvent(new Kwf_Component_Event_Component_ContentChanged($this->_class, $c));
                if ($c->isPage) {
                    $this->fireEvent(new Kwf_Component_Event_Page_UrlChanged($this->_class, $c));
                }
            }
        }
    }

    protected function _onOwnRowUpdate(Kwf_Component_Data $c, Kwf_Component_Event_Row_Abstract $event)
    {
        parent::_onOwnRowUpdate($c, $event);
        $this->_pageIds = null;
    }

    private function _getDbIds($targetId, $includeSubpages = true)
    {
        if (!$this->_pageIds) {
            $ids = array();
            $model = Kwc_Abstract::createOwnModel($this->_class);
            foreach ($model->export(Kwf_Model_Abstract::FORMAT_ARRAY) as $row) {
                $target = $row['target'];
                if (!isset($ids[$target])) $ids[$target] = array();
                $ids[$target][] = $row['component_id'];
            }
            $this->_pageIds = $ids;
        }
        $ret = array();
        foreach ($this->_pageIds as $targetPageId => $dbIds) {
            if ($includeSubpages) {
                if ((string)$targetPageId == (string)$targetId
                    || substr($targetPageId, 0, strlen($targetId)+1) == $targetId.'-'
                    || substr($targetPageId, 0, strlen($targetId)+1) == $targetId.'_'
                ) {
                    $ret = array_merge($ret, $dbIds);
                }
            } else {
                if ((string)$targetPageId === (string)$targetId) {
                    $ret = array_merge($ret, $dbIds);
                }
            }
        }
        return $ret;
    }
{'fraction_non_alphanumeric': 0.12304974977921695, 'fraction_numerical': 0.0007850063781768226, 'mean_word_length': 2.0396659707724427, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 111, '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': '801999', 'n_tokens_mistral': 2999, 'n_tokens_neox': 2859, 'n_words': 489}