<commit_before><?php

namespace modmore\Gitify\Mixins;

/**
 * Class DownloadModx
 * @package modmore\Gitify\Mixins
 */
trait DownloadModx
{
    /**
     * Downloads the latest version of MODX or takes from local cache, and puts it into place in the working directory.
     *
     * @param string $version
     * @param bool $download
     * @param bool $advanced
     * @return bool
     */
    protected function getMODX($version = 'latest', $download = false, $advanced = false)
    {
        $advanced = $advanced ? '-advanced' : false;
        if ($version === 'latest') {
            $this->output->writeln('Looking up latest MODX version...');
            $link = $this->fetchUrl($version);
            $version = basename($link, '.zip');
            $this->output->writeln('<comment>Latest version: ' . $version . '</comment>');
        }

        // Force download the MODX package
        if ($download) {
            $this->output->writeln('<comment>Ignoring local cache, downloading MODX package...</comment>');
            if (!$this->download($version, $advanced)) {
                return false;
            }
        }

        // Copy the files from the local cache (and download version if necessary)
        $this->retrieveFromCache($version, $advanced);

        return true;
    }

    /**
     * Downloads specified package to local storage
     *
     * @param $version
     * @param $advanced
     * @return bool
     */
    protected function download($version, $advanced)
    {
        $link = $this->fetchUrl($version);

        $link = str_replace('.zip', $advanced ? "{$advanced}.zip" : ".zip", $link);
        if (!file_exists(GITIFY_CACHE_DIR)) {
            mkdir(GITIFY_CACHE_DIR);
        }

        $this->removeOutdatedArchive($version, $advanced); // remove old files

        $zip = GITIFY_CACHE_DIR . $version . $advanced . '.zip';
        $this->output->writeln("Downloading {$version}{$advanced} from {$link}...");
        exec("curl -Lo $zip $link -#");

        if (!file_exists($zip)) {
            $this->output->writeln('<error>Error: Could not download the MODX zip</error>');

            return false;
        }

        $this->unzip($zip, $version, $advanced);

        return true;
    }

    /**
     * Unzips the package zip to the current directory
     *
     * @param $package
     */
    protected function unzip($package, $version, $advanced)
    {
        $this->output->writeln("Extracting package...");

        $destination = dirname($package);
        exec("unzip $package -d $destination");
        exec("mv {$destination}/{$version} {$destination}/{$version}{$advanced}");
    }

    /**
     * Fetches the real download link for a MODX package version
     *
     * @param $version
     * @return mixed
     */
    protected function fetchUrl($version)
    {
        if ($this->output->isVerbose()) {
            $this->output->writeln('Fetching download URL for MODX ' . $version);
        }
        $version = str_replace('modx-', '', $version);
        $url = empty($version) || $version == 'latest'<commit_msg>Fix name collision<commit_after><?php

namespace modmore\Gitify\Mixins;

/**
 * Class DownloadModx
 * @package modmore\Gitify\Mixins
 */
trait DownloadModx
{
    /**
     * Downloads the latest version of MODX or takes from local cache, and puts it into place in the working directory.
     *
     * @param string $version
     * @param bool $download
     * @param bool $advanced
     * @return bool
     */
    protected function getMODX($version = 'latest', $download = false, $advanced = false)
    {
        $advanced = $advanced ? '-advanced' : false;
        if ($version === 'latest') {
            $this->output->writeln('Looking up latest MODX version...');
            $link = $this->fetchUrl($version);
            $version = basename($link, '.zip');
            $this->output->writeln('<comment>Latest version: ' . $version . '</comment>');
        }

        // Force download the MODX package
        if ($download) {
            $this->output->writeln('<comment>Ignoring local cache, downloading MODX package...</comment>');
            if (!$this->download($version, $advanced)) {
                return false;
            }
        }

        // Copy the files from the local cache (and download version if necessary)
        $this->retrieveFromCache($version, $advanced);

        return true;
    }

    /**
     * Downloads specified package to local storage
     *
     * @param $version
     * @param $advanced
     * @return bool
     */
    protected function download($version, $advanced)
    {
        $link = $this->fetchUrl($version);

        $link = str_replace('.zip', $advanced ? "{$advanced}.zip" : ".zip", $link);
        if (!file_exists(GITIFY_CACHE_DIR)) {
            mkdir(GITIFY_CACHE_DIR);
        }

        $this->removeOutdatedArchive($version, $advanced); // remove old files

        $zip = GITIFY_CACHE_DIR . $version . $advanced . '.zip';
        $this->output->writeln("Downloading {$version}{$advanced} from {$link}...");
        exec("curl -Lo $zip $link -#");

        if (!file_exists($zip)) {
            $this->output->writeln('<error>Error: Could not download the MODX zip</error>');

            return false;
        }

        $this->unzip($zip, $version, $advanced);

        return true;
    }

    /**
     * Unzips the package zip to the current directory
     *
     * @param $package
     */
    protected function unzip($package, $version, $advanced = '-traditional')
    {
        $this->output->writeln("Extracting package...");

        $destination = dirname($package);
        exec("unzip $package -d $destination");
        exec("mv {$destination}/{$version} {$destination}/{$version}{$advanced}");
    }

    /**
     * Fetches the real download link for a MODX package version
     *
     * @param $version
     * @return mixed
     */
    protected function fetchUrl($version)
    {
        if ($this->output->isVerbose()) {
            $this->output->writeln('Fetching download URL for MODX ' . $version);
        }
        $version = str_replace('modx-', '', $version);
        $url = empty($version) || $version == 'latest'
{'fraction_non_alphanumeric': 0.1514073115496603, 'fraction_numerical': 0.0, 'mean_word_length': 2.2507886435331232, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7458214', 'n_tokens_mistral': 1782, 'n_tokens_neox': 1711, 'n_words': 446}