<commit_before><?php

namespace Contentacle\Resources;

/**
 * @uri /users/:username/repos/:repo/branches/:branch/documents
 * @uri /users/:username/repos/:repo/branches/:branch/documents/?(.*)$
 */
class Document extends WithinDocument
{
    protected function configureResponseWithDocument($response, $repo, $branchName, $document)
    {
        $response->addLink('self', $this->buildUrl($repo->username, strtolower($repo->name), $branchName, 'documents', $document['path']));
        $response->addLink('cont:doc', '/rels/document');

        parent::configureResponseWithDocument($response, $repo, $branchName, $document);
    }

    /**
     * Get the contents of a document.
     *
     * @method get
     * @response 200 OK
     * @provides application/hal+yaml
     * @provides application/hal+json
     * @provides text/html
     * @field filename The filename of the document.
     * @field path The path of the document.
     * @field type Directory (dir) or file.
     * @field sha Hash of this documents content blob
     * @field username Username of committer
     * @field email Email of committer
     * @field author Name of committer
     * @field date Date of the commit (as unix timestamp)
     * @field branch The branch the document is committed to
     * @field commit Hash of the commit
     * @field content The content of the document
     * @links self Link to itself
     * @links cont:doc Link to this documentation.
     * @links cont:user Link to the owner of the repository.
     * @links cont:repo Link to the repository.
     * @links cont:branch Link to the branch within the repository.
     * @links cont:documents Link to the documents within the branch.
     * @links cont:commits Link to the commits within the branch.
     * @links cont:history Link to the history of the document.
     * @links cont:raw Link to the raw content of the document.
     * @links cont:document Link to itself.
     * @links cont:edit Link to the form to edit itself.
     * @links cont:commit Link to the commit of this version of this document.
     * @links cont:author Link to the author of this version of this document.
     * @embeds cont:document Documents within this document (if it is a directory).
     */
    function get($username, $repoName, $branchName, $path = null, $fixPath = true)
    {
        if ($fixPath) {
            $path = $this->fixPath($path, $username, $repoName, $branchName, 'documents');
        }
        $repo = $this->repoRepository->getRepo($username, $repoName);
        try {
            $response = $this->response(200, 'directory');

            $this->configureResponse($response, $repo, $branchName);

            $response->addData(array(
                'path' => $path,
                'filename' => basename($path),
                'dir' => true
            ));

            $breadcrumb = array();
            $breadcrumbUrl = '/users/'.$username.'/repos/'.$repoName.'/branches/'.$branchName.'/documents';
            foreach (explode('/', $path) as $part) {
                if ($part) {
                    $breadcrumbUrl .= '/'.$part;
                    $breadcrumb[$breadcrumbUrl] = $part;
                }
            }
            $response->addVar('breadcrumb', $breadcrumb);

            $response->addLink('self', $this->buildUrl($username, $repoName, $branchName, 'documents', $path));
            $response->addLink('cont:doc', '/rels/document');

            $documents = $repo->documents($branchName, $path);
            $commits = $repo->commits($branchName, null, 1);

            foreach ($documents as $filename) {
                if ($this->embed) {
                    $response->embed('cont:document', $this->getChildResource('\Contentacle\Resources\Document', array($username, $repoName, $branchName, $filename, false)));
                } else {
                    $response->addLink('cont:document', '/users/'.$username.'/repos/'.$repoName.'/branches/'.$branchName.'/documents/'.$filename);
                }
            }

            foreach ($commits as $commit) {
                if ($this->embed) {
                    $response->embed('cont:commit', $this->getChildResource('\Contentacle\Resources\Commit', array($username, $repoName, $branchName, $commit['sha'])));
                }
                $response->addLink('cont:commit', $this->buildUrl($username, $repoName, $branchName, 'commits', $commit['sha']));
            }

            return $response;

        } catch (\Contentacle\Exceptions\RepoException $e) {
            try {
                $document = $repo->document($branchName, $path);
                $response = $this->response(200, 'document');

                $this->configureResponseWithDocument($response, $repo, $branchName, $document);
                
                return $response;

            } catch (\Contentacle\Exceptions\RepoException $e) {}
        }
        throw new \Tonic\NotFoundException;
    }<commit_msg>Only fetch commit, build breadcrumb, and embed documents if we're not embedded within another resource.<commit_after><?php

namespace Contentacle\Resources;

/**
 * @uri /users/:username/repos/:repo/branches/:branch/documents
 * @uri /users/:username/repos/:repo/branches/:branch/documents/?(.*)$
 */
class Document extends WithinDocument
{
    protected function configureResponseWithDocument($response, $repo, $branchName, $document)
    {
        $response->addLink('self', $this->buildUrl($repo->username, strtolower($repo->name), $branchName, 'documents', $document['path']));
        $response->addLink('cont:doc', '/rels/document');

        parent::configureResponseWithDocument($response, $repo, $branchName, $document);
    }

    /**
     * Get the contents of a document.
     *
     * @method get
     * @response 200 OK
     * @provides application/hal+yaml
     * @provides application/hal+json
     * @provides text/html
     * @field filename The filename of the document.
     * @field path The path of the document.
     * @field type Directory (dir) or file.
     * @field sha Hash of this documents content blob
     * @field username Username of committer
     * @field email Email of committer
     * @field author Name of committer
     * @field date Date of the commit (as unix timestamp)
     * @field branch The branch the document is committed to
     * @field commit Hash of the commit
     * @field content The content of the document
     * @links self Link to itself
     * @links cont:doc Link to this documentation.
     * @links cont:user Link to the owner of the repository.
     * @links cont:repo Link to the repository.
     * @links cont:branch Link to the branch within the repository.
     * @links cont:documents Link to the documents within the branch.
     * @links cont:commits Link to the commits within the branch.
     * @links cont:history Link to the history of the document.
     * @links cont:raw Link to the raw content of the document.
     * @links cont:document Link to itself.
     * @links cont:edit Link to the form to edit itself.
     * @links cont:commit Link to the commit of this version of this document.
     * @links cont:author Link to the author of this version of this document.
     * @embeds cont:document Documents within this document (if it is a directory).
     */
    function get($username, $repoName, $branchName, $path = null, $fixPath = true)
    {
        if ($fixPath) {
            $path = $this->fixPath($path, $username, $repoName, $branchName, 'documents');
        }
        $repo = $this->repoRepository->getRepo($username, $repoName);
        try {
            $response = $this->response(200, 'directory');

            $this->configureResponse($response, $repo, $branchName);

            $response->addData(array(
                'path' => $path,
                'filename' => basename($path),
                'dir' => true
            ));

            $response->addLink('self', $this->buildUrl($username, $repoName, $branchName, 'documents', $path));
            $response->addLink('cont:doc', '/rels/document');

            $documents = $repo->documents($branchName, $path);

            if ($this->embed) {
                $commits = $repo->commits($branchName, null, 1);

                $breadcrumb = array();
                $breadcrumbUrl = '/users/'.$username.'/repos/'.$repoName.'/branches/'.$branchName.'/documents';
                foreach (explode('/', $path) as $part) {
                    if ($part) {
                        $breadcrumbUrl .= '/'.$part;
                        $breadcrumb[$breadcrumbUrl] = $part;
                    }
                }

                $response->addVar('breadcrumb', $breadcrumb);
                $response->addVar('description', $repo->description);

                foreach ($documents as $filename) {
                    if ($this->embed) {
                        $response->embed('cont:document', $this->getChildResource('\Contentacle\Resources\Document', array($username, $repoName, $branchName, $filename, false)));
                    } else {
                        $response->addLink('cont:document', '/users/'.$username.'/repos/'.$repoName.'/branches/'.$branchName.'/documents/'.$filename);
                    }
                }

                foreach ($commits as $commit) {
                    if ($this->embed) {
                        $response->embed('cont:commit', $this->getChildResource('\Contentacle\Resources\Commit', array($username, $repoName, $branchName, $commit['sha'])));
                    }
                    $response->addLink('cont:commit', $this->buildUrl($username, $repoName, $branchName, 'commits', $commit['sha']));
                }
            }

            return $response;

        } catch (\Contentacle\Exceptions\RepoException $e) {
            try {
                $document = $repo->document($branchName, $path);
                $response = $this->response(200, 'document');

                $this->configureResponseWithDocument($response, $repo, $branchName, $document);
                
                return $response;

            } catch (\Contentacle\Exceptions\RepoException $e) {}
        } catch (\Git\Exception $e) {
            return $response;
        }
        throw new \Tonic\NotFoundException;
    }
{'fraction_non_alphanumeric': 0.13507201245620865, 'fraction_numerical': 0.001946282600233554, 'mean_word_length': 2.2939102564102565, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 70, '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': '1067806', 'n_tokens_mistral': 2786, 'n_tokens_neox': 2689, 'n_words': 751}