<commit_before><?php

require_once __DIR__ . '/../lib/parsedown/Parsedown.php';

class Main {

    // Parameters:
    // - $baseUrl:          E.g. 'http://localhost/slim-wiki/'
    // - $basePath:         E.g. '/slim-wiki/'
    // - $requestPathArray: E.g. array('myfolder', 'mypage')
    public function dispatch($baseUrl, $basePath, $requestPathArray) {
        $articleBaseDir = realpath(__DIR__ . '/../../articles') . '/';
        $articleFilename = $articleBaseDir . implode('/', $requestPathArray);

        // Support `index.md` for directories
        if (is_dir($articleFilename)) {
            $articleFilename = rtrim($articleFilename, '/') . '/index.md';
        }

        // Make the extension `.md` optional
        if (! file_exists($articleFilename) && file_exists($articleFilename . '.md')) {
            $articleFilename .= '.md';
        }

        if (($articleFilename == realpath($articleFilename)) && file_exists($articleFilename) && is_readable($articleFilename)) {
            $wikiName = 'Slim Wiki'; // TODO: Make this configurable

            $data = array();
            $data['baseUrl']  = $baseUrl;
            $data['basePath'] = $basePath;
            $data['wikiName'] = $wikiName;

            $data['breadcrumbs'] = $this->createBreadcrumbs($articleBaseDir, $requestPathArray, $wikiName);

            $articleContent = file_get_contents($articleFilename);
            $data['articleHtml'] = Parsedown::instance()->text($articleContent);

            $this->renderPage($data);
        } else {
            // TODO: Show error page
            echo '<p style="color:#990000">File does not exist or is not readable: '.$articleFilename.'</p>';
        }

    }


    private function createBreadcrumbs($articleBaseDir, $requestPathArray, $wikiName) {
        $pathCount = count($requestPathArray);
        $breadcrumbArray = array(array('name' => $wikiName, 'path' => '', 'active' => ($pathCount == 0)));

        $currentPath = '';
        for ($i = 0; $i < $pathCount; $i++) {
            $pathPart = $requestPathArray[$i];
            $currentPath .= ($i == 0 ? '' : '/') . $pathPart;
            $isLast = ($i == $pathCount - 1);

            if ($isLast || file_exists($articleBaseDir . $currentPath . '/index.md')) {
                // This is the requested file or an directory having an index -> Add it
                $breadcrumbArray[] = array(
                    'name' => str_replace('_', ' ', $pathPart),
                    'path' => urlencode($currentPath),
                    'active' => $isLast);
            }
        }

        return $breadcrumbArray;
    }


    private function renderPage($data) {
        include(__DIR__ . '/../layout/page.php');
    }

}<commit_msg>Moved finding the article file into a helper method and corrected headers<commit_after><?php

require_once __DIR__ . '/../lib/parsedown/Parsedown.php';

class Main {

    // Parameters:
    // - $baseUrl:          E.g. 'http://localhost/slim-wiki/'
    // - $basePath:         E.g. '/slim-wiki/'
    // - $requestPathArray: E.g. array('myfolder', 'mypage')
    public function dispatch($baseUrl, $basePath, $requestPathArray) {
        $articleBaseDir = realpath(__DIR__ . '/../../articles') . '/';
        $articleFilename = $this->getArticleFilename($articleBaseDir, $requestPathArray);
        if ($articleFilename == null) {
            header('HTTP/1.0 404 Not Found');
            header('Content-Type:text/html; charset=utf-8');
            echo '<h1>File not found</h1>'; // TODO: Show error page
        } else {
            $wikiName = 'Slim Wiki'; // TODO: Make this configurable

            $data = array();
            $data['baseUrl']  = $baseUrl;
            $data['basePath'] = $basePath;
            $data['wikiName'] = $wikiName;

            $data['breadcrumbs'] = $this->createBreadcrumbs($articleBaseDir, $requestPathArray, $wikiName);

            $articleContent = file_get_contents($articleFilename);
            $data['articleHtml'] = Parsedown::instance()->text($articleContent);

            $this->renderPage($data);
        }

    }

    private function getArticleFilename($articleBaseDir, $requestPathArray) {
        $articleFilename = $articleBaseDir . implode('/', $requestPathArray);

        // Support `index.md` for directories
        if (is_dir($articleFilename)) {
            $articleFilename = rtrim($articleFilename, '/') . '/index.md';
        }

        // Make the extension `.md` optional
        if (! file_exists($articleFilename) && file_exists($articleFilename . '.md')) {
            $articleFilename .= '.md';
        }

        if ($articleFilename != realpath($articleFilename)) {
            // Attempt to break out of article base directory (e.g. `../../outside.ext`)
            return null;
        } else if (file_exists($articleFilename) && is_readable($articleFilename)) {
            return $articleFilename;
        } else {
            return null;
        }
    }

    private function createBreadcrumbs($articleBaseDir, $requestPathArray, $wikiName) {
        $pathCount = count($requestPathArray);
        $breadcrumbArray = array(array('name' => $wikiName, 'path' => '', 'active' => ($pathCount == 0)));

        $currentPath = '';
        for ($i = 0; $i < $pathCount; $i++) {
            $pathPart = $requestPathArray[$i];
            $currentPath .= ($i == 0 ? '' : '/') . $pathPart;
            $isLast = ($i == $pathCount - 1);

            if ($isLast || file_exists($articleBaseDir . $currentPath . '/index.md')) {
                // This is the requested file or an directory having an index -> Add it
                $breadcrumbArray[] = array(
                    'name' => str_replace('_', ' ', $pathPart),
                    'path' => urlencode($currentPath),
                    'active' => $isLast);
            }
        }

        return $breadcrumbArray;
    }

    private function renderPage($data) {
        header('Content-Type:text/html; charset=utf-8');

        include(__DIR__ . '/../layout/page.php');
    }

}
{'fraction_non_alphanumeric': 0.16269052352551358, 'fraction_numerical': 0.003810470510271703, 'mean_word_length': 2.1541274817136884, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 28, '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': '6507822', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1684, 'n_words': 359}