<commit_before><?php
namespace Concrete\Core\Tree\Type;

use Concrete\Core\Tree\Tree;
use Concrete\Core\Tree\Node\Type\Category as CategoryTreeNode;
use Database;
use Group as UserGroup;
use Concrete\Core\Permission\Access\Entity\GroupEntity as GroupPermissionAccessEntity;
use Concrete\Core\Permission\Key\CategoryTreeNodeKey as CategoryTreeNodePermissionKey;
use Concrete\Core\Permission\Access\Access as PermissionAccess;

class Topic extends Tree
{
    /** Returns the standard name for this tree
     * @return string
     */
    public function getTreeName()
    {
        return $this->topicTreeName;
    }

    /** Returns the display name for this tree (localized and escaped accordingly to $format)
     * @param  string $format = 'html' Escape the result in html format (if $format is 'html'). If $format is 'text' or any other value, the display name won't be escaped.
     *
     * @return string
     */
    public function getTreeDisplayName($format = 'html')
    {
        $value = tc('TreeName', $this->topicTreeName);
        switch ($format) {
            case 'html':
                return h($value);
            case 'text':
            default:
                return $value;
        }
    }

    public static function getDefault()
    {
        $db = Database::connection();
        $treeID = $db->GetOne('select treeID from TopicTrees order by treeID asc');

        return Tree::getByID($treeID);
    }

    protected function deleteDetails()
    {
        $db = Database::connection();
        $db->Execute('delete from TopicTrees where treeID = ?', array($this->treeID));
    }

    public static function getByName($name)
    {
        $db = Database::connection();
        $treeID = $db->GetOne('select treeID from TopicTrees where topicTreeName = ?', array($name));

        return Tree::getByID($treeID);
    }

    public static function add($name)
    {
        // copy permissions from the other node.
        $rootNode = CategoryTreeNode::add();
        $treeID = parent::create($rootNode);
        $tree = self::getByID($treeID);
        $tree->setTopicTreeName($name);

        // by default, topic trees are viewable by all
        $guestGroupEntity = GroupPermissionAccessEntity::getOrCreate(UserGroup::getByID(GUEST_GROUP_ID));
        $pk = CategoryTreeNodePermissionKey::getByHandle('view_category_tree_node');
        if (is_object($pk)) {
            $pk->setPermissionObject($rootNode);
            $pa = PermissionAccess::create($pk);
            $pa->addListItem($guestGroupEntity);
            $pt = $pk->getPermissionAssignmentObject();
            $pt->assignPermissionAccess($pa);
        }
        return $tree;
    }

    public function exportDetails(\SimpleXMLElement $sx)
    {
        $default = self::getDefault();
        if (is_object($default) && $default->getTreeID() == $this->getTreeID()) {
            $sx->addAttribute('default', 1);
        } else {
            $sx->addAttribute('name', $this->getTreeName());
        }
    }

    public static function importDetails(\SimpleXMLElement $sx)
    {
        $isDefault = (string) $sx['default'];
        if ($isDefault) {
            return static::getDefault();
        } else {
            $name = (string) $sx['name'];
            $tree = static::getByName($name);<commit_msg>always including topic tree name in export<commit_after><?php
namespace Concrete\Core\Tree\Type;

use Concrete\Core\Tree\Tree;
use Concrete\Core\Tree\Node\Type\Category as CategoryTreeNode;
use Database;
use Group as UserGroup;
use Concrete\Core\Permission\Access\Entity\GroupEntity as GroupPermissionAccessEntity;
use Concrete\Core\Permission\Key\CategoryTreeNodeKey as CategoryTreeNodePermissionKey;
use Concrete\Core\Permission\Access\Access as PermissionAccess;

class Topic extends Tree
{
    /** Returns the standard name for this tree
     * @return string
     */
    public function getTreeName()
    {
        return $this->topicTreeName;
    }

    /** Returns the display name for this tree (localized and escaped accordingly to $format)
     * @param  string $format = 'html' Escape the result in html format (if $format is 'html'). If $format is 'text' or any other value, the display name won't be escaped.
     *
     * @return string
     */
    public function getTreeDisplayName($format = 'html')
    {
        $value = tc('TreeName', $this->topicTreeName);
        switch ($format) {
            case 'html':
                return h($value);
            case 'text':
            default:
                return $value;
        }
    }

    public static function getDefault()
    {
        $db = Database::connection();
        $treeID = $db->GetOne('select treeID from TopicTrees order by treeID asc');

        return Tree::getByID($treeID);
    }

    protected function deleteDetails()
    {
        $db = Database::connection();
        $db->Execute('delete from TopicTrees where treeID = ?', array($this->treeID));
    }

    public static function getByName($name)
    {
        $db = Database::connection();
        $treeID = $db->GetOne('select treeID from TopicTrees where topicTreeName = ?', array($name));

        return Tree::getByID($treeID);
    }

    public static function add($name)
    {
        // copy permissions from the other node.
        $rootNode = CategoryTreeNode::add();
        $treeID = parent::create($rootNode);
        $tree = self::getByID($treeID);
        $tree->setTopicTreeName($name);

        // by default, topic trees are viewable by all
        $guestGroupEntity = GroupPermissionAccessEntity::getOrCreate(UserGroup::getByID(GUEST_GROUP_ID));
        $pk = CategoryTreeNodePermissionKey::getByHandle('view_category_tree_node');
        if (is_object($pk)) {
            $pk->setPermissionObject($rootNode);
            $pa = PermissionAccess::create($pk);
            $pa->addListItem($guestGroupEntity);
            $pt = $pk->getPermissionAssignmentObject();
            $pt->assignPermissionAccess($pa);
        }
        return $tree;
    }

    public function exportDetails(\SimpleXMLElement $sx)
    {
        $default = self::getDefault();
        $sx->addAttribute('name', $this->getTreeName());
        if (is_object($default) && $default->getTreeID() == $this->getTreeID()) {
            $sx->addAttribute('default', 1);
        }
    }

    public static function importDetails(\SimpleXMLElement $sx)
    {
        $isDefault = (string) $sx['default'];
        if ($isDefault) {
            return static::getDefault();
        } else {
            $name = (string) $sx['name'];
            $tree = static::getByName($name);
{'fraction_non_alphanumeric': 0.12606060606060607, 'fraction_numerical': 0.00030303030303030303, 'mean_word_length': 2.650995575221239, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, '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': '5086438', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1819, 'n_words': 450}