<commit_before><?php

class SiteMapPage extends Page
{
}

class SiteMapPage_Controller extends Page_Controller
{
    /**
     * Returns content with the sitemap appended to it for a SiteMap page
     * @return ViewableData
     */
    public function index()
    {
        $results = array();

        $page = $this->data();
        $sitemap = $this->getHierarchicalSitemapHTML();

		return array('Content' => str_replace("<?xml version=\"1.0\"?>\n", '', $sitemap));
		
    }

    public function getHierarchicalSitemapHTML() {
        $all = array();
        $allids = array();
        $final = array();
        
		$page = $this->data();
        $siteData = $page->siteData ? $page->siteData : singleton('SiteDataService');
		
		$items = $siteData->getItems();
		
		// get just the top level ones; ie parentID = 0;
		
		$topLevel = array();
		foreach ($items as $item) {
			if ($item->ParentID == 0 && $item->ShowInMenus) {
				$topLevel[] = $item;
			}
		}
		
		$sitemapXML = new SimpleXMLElement('<div></div>');
        $sitemapXML->addAttribute('id', 'SitemapList');
		
		foreach ($topLevel as $item) {
			$sitemapXML = $this->processPageToHierarchicalHTML($sitemapXML, $item);
		}
		
		return $sitemapXML->asXML();
    }

	protected $processed = array();

    protected function processPageToHierarchicalHTML($xml, $page) {
        if ($page->ID && !isset($this->processed[$page->ID])) {
            // add stuff
            $ul = $xml->addChild('ul');
            $li = $ul->addChild('li');
            $li->a = $page->Title;
            $li->a->addAttribute('href', $page->Link);
            // check if they have children
			$kids = $page->Children();
			
            if (count($kids)) {
                foreach ($kids as $subPage) {
                    $this->processPageToHierarchicalHTML(
						$li,
						$subPage
					);
                }
            }
            // add page id to processed
            $this->processed[$page->ID] = true;
        }
        return $xml;
    }
}
<commit_msg>Getting to level page children via Sitetree<commit_after><?php

class SiteMapPage extends Page
{
}

class SiteMapPage_Controller extends Page_Controller
{
    /**
     * Returns content with the sitemap appended to it for a SiteMap page
     * @return ViewableData
     */
    public function index()
    {
        $results = [];

        $page = $this->data();
        $sitemap = $this->getHierarchicalSitemapHTML();

        return ['Content' => str_replace("<?xml version=\"1.0\"?>\n", '', $sitemap)];

    }

    public function getHierarchicalSitemapHTML()
    {
        $all = [];
        $allids = [];
        $final = [];

        $page = $this->data();
        $siteData = $page->siteData ? $page->siteData : singleton('SiteDataService');

        $items = $siteData->getItems();

        // get just the top level ones; ie parentID = 0;

        $topLevel = [];
        foreach ($items as $item) {
            if ($item->ParentID == 0 && $item->ShowInMenus) {
                $topLevel[] = $item;
            }
        }

        $sitemapXML = new SimpleXMLElement('<div></div>');
        $sitemapXML->addAttribute('id', 'SitemapList');

        foreach ($topLevel as $item) {
            $sitemapXML = $this->processPageToHierarchicalHTML($sitemapXML, $item);
        }

        return $sitemapXML->asXML();
    }

    protected $processed = [];

    protected function processPageToHierarchicalHTML($xml, $page)
    {
        if ($page->ID && !isset($this->processed[$page->ID])) {
            // add html
            $ul = $xml->addChild('ul');
            $li = $ul->addChild('li');
            $li->a = $page->Title;
            $li->a->addAttribute('href', $page->Link);

            // check if the page has children and if so output them
            $kids = SiteTree::get()->filter('ParentID', $page->ID);
            foreach ($kids as $subPage) {
                $this->processPageToHierarchicalHTML(
                    $li,
                    $subPage
                );
            }
            // add page id to processed
            $this->processed[$page->ID] = true;
        }
        return $xml;
    }
}

{'fraction_non_alphanumeric': 0.15655955544817587, 'fraction_numerical': 0.0019328340178787146, 'mean_word_length': 1.9870129870129871, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 2, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '2980923', 'n_tokens_mistral': 1339, 'n_tokens_neox': 1233, 'n_words': 273}