<commit_before><?php

declare(strict_types=1);

namespace App\Console\Command\Backup;

use App\Console\Command\AbstractDatabaseCommand;
use App\Entity\StorageLocation;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Filesystem;

use const PATHINFO_EXTENSION;

#[AsCommand(
    name: 'azuracast:restore',
    description: 'Restore a backup previously generated by AzuraCast.',
)]
final class RestoreCommand extends AbstractDatabaseCommand
{
    protected function configure(): void
    {
        $this->addArgument('path', InputArgument::OPTIONAL)
            ->addOption('restore', null, InputOption::VALUE_NONE)
            ->addOption('release', null, InputOption::VALUE_NONE);
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);

        $path = $input->getArgument('path');
        $start_time = microtime(true);

        $io->title('AzuraCast Restore');

        if (empty($path)) {
            $filesRaw = glob(StorageLocation::DEFAULT_BACKUPS_PATH . '/*', GLOB_NOSORT) ?: [];
            usort(
                $filesRaw,
                static fn($a, $b) => filemtime($b) <=> filemtime($a)
            );

            if (0 === count($filesRaw)) {
                $io->getErrorStyle()
                    ->error('Backups directory has no available files. You must explicitly specify a backup file.');
                return 1;
            }

            $files = [];
            $i = 1;
            foreach ($filesRaw as $filePath) {
                $files[$i] = basename($filePath);

                if (10 === $i) {
                    break;
                }
                $i++;
            }

            $path = $io->choice('Select backup file to restore:', $files, 1);
        }

        if ('/' !== $path[0]) {
            $path = StorageLocation::DEFAULT_BACKUPS_PATH . '/' . $path;
        }

        if (!file_exists($path)) {
            $io->getErrorStyle()->error(
                sprintf(
                    __('Backup path %s not found!'),
                    $path
                )
            );
            return 1;
        }

        $io->writeln('Please wait while the backup is restored...');

        // Extract tar.gz archive
        $io->section('Extracting backup file...');

        $file_ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));

        switch ($file_ext) {
            case 'tzst':
                $this->passThruProcess(
                    $io,
                    [
                        'tar',
                        '-I',
                        'unzstd',
                        '-xvf',
                        $path,
                    ],
                    '/'
                );
                break;

            case 'gz':
            case 'tgz':
                $this->passThruProcess(
                    $io,
                    [
                        'tar',
                        'zxvf',
                        $path,
                    ],
                    '/'
                );
                break;

            case 'zip':
            default:
                $this->passThruProcess(
                    $io,
                    [
                        'unzip',
                        $path,
                    ],
                    '/'
                );
                break;
        }

        $io->newLine();

        // Handle DB dump
        $io->section('Importing database...');

        $tmp_dir_mariadb = '/tmp/azuracast_backup_mariadb';

        try {
            $path_db_dump = $tmp_dir_mariadb . '/db.sql';
            $this->restoreDatabaseDump($io, $path_db_dump);
        } catch (\Exception $e) {
            $io->getErrorStyle()->error($e->getMessage());
            return 1;
        }

        (new Filesystem())->remove($tmp_dir_mariadb);
        $io->newLine();

        // Update from current version to latest.
        $io->section('Running standard updates...');

        $this->runCommand($output, 'azuracast:setup', ['--update' => true]);

        $end_time = microtime(true);
        $time_diff = $end_time - $start_time;

        $io->success(
            [
                'Restore complete in ' . round($time_diff, 3) . ' seconds.',
            ]
        );
        return 0;
    }
}
<commit_msg>#5835 -- Force overwrite on unzip.<commit_after><?php

declare(strict_types=1);

namespace App\Console\Command\Backup;

use App\Console\Command\AbstractDatabaseCommand;
use App\Entity\StorageLocation;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Filesystem;

use const PATHINFO_EXTENSION;

#[AsCommand(
    name: 'azuracast:restore',
    description: 'Restore a backup previously generated by AzuraCast.',
)]
final class RestoreCommand extends AbstractDatabaseCommand
{
    protected function configure(): void
    {
        $this->addArgument('path', InputArgument::OPTIONAL)
            ->addOption('restore', null, InputOption::VALUE_NONE)
            ->addOption('release', null, InputOption::VALUE_NONE);
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);

        $path = $input->getArgument('path');
        $start_time = microtime(true);

        $io->title('AzuraCast Restore');

        if (empty($path)) {
            $filesRaw = glob(StorageLocation::DEFAULT_BACKUPS_PATH . '/*', GLOB_NOSORT) ?: [];
            usort(
                $filesRaw,
                static fn($a, $b) => filemtime($b) <=> filemtime($a)
            );

            if (0 === count($filesRaw)) {
                $io->getErrorStyle()
                    ->error('Backups directory has no available files. You must explicitly specify a backup file.');
                return 1;
            }

            $files = [];
            $i = 1;
            foreach ($filesRaw as $filePath) {
                $files[$i] = basename($filePath);

                if (10 === $i) {
                    break;
                }
                $i++;
            }

            $path = $io->choice('Select backup file to restore:', $files, 1);
        }

        if ('/' !== $path[0]) {
            $path = StorageLocation::DEFAULT_BACKUPS_PATH . '/' . $path;
        }

        if (!file_exists($path)) {
            $io->getErrorStyle()->error(
                sprintf(
                    __('Backup path %s not found!'),
                    $path
                )
            );
            return 1;
        }

        $io->writeln('Please wait while the backup is restored...');

        // Extract tar.gz archive
        $io->section('Extracting backup file...');

        $file_ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));

        switch ($file_ext) {
            case 'tzst':
                $this->passThruProcess(
                    $io,
                    [
                        'tar',
                        '-I',
                        'unzstd',
                        '-xvf',
                        $path,
                    ],
                    '/'
                );
                break;

            case 'gz':
            case 'tgz':
                $this->passThruProcess(
                    $io,
                    [
                        'tar',
                        'zxvf',
                        $path,
                    ],
                    '/'
                );
                break;

            case 'zip':
            default:
                $this->passThruProcess(
                    $io,
                    [
                        'unzip',
                        '-o',
                        $path,
                    ],
                    '/'
                );
                break;
        }

        $io->newLine();

        // Handle DB dump
        $io->section('Importing database...');

        $tmp_dir_mariadb = '/tmp/azuracast_backup_mariadb';

        try {
            $path_db_dump = $tmp_dir_mariadb . '/db.sql';
            $this->restoreDatabaseDump($io, $path_db_dump);
        } catch (\Exception $e) {
            $io->getErrorStyle()->error($e->getMessage());
            return 1;
        }

        (new Filesystem())->remove($tmp_dir_mariadb);
        $io->newLine();

        // Update from current version to latest.
        $io->section('Running standard updates...');

        $this->runCommand($output, 'azuracast:setup', ['--update' => true]);

        $end_time = microtime(true);
        $time_diff = $end_time - $start_time;

        $io->success(
            [
                'Restore complete in ' . round($time_diff, 3) . ' seconds.',
            ]
        );
        return 0;
    }
}

{'fraction_non_alphanumeric': 0.12932428063368898, 'fraction_numerical': 0.0030175665481194094, 'mean_word_length': 1.344026269259914, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 61, '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': '1075930', 'n_tokens_mistral': 2576, 'n_tokens_neox': 2419, 'n_words': 477}