<commit_before><?php
// (c) Copyright 2002-2016 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$

namespace Tiki\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class BackupDBCommand extends Command
{
	protected function configure()
	{
		$this
			->setName('database:backup')
			->setDescription('Create a database backup (with mysqldump)')
			->addArgument(
				'path',
				InputArgument::REQUIRED,	
				'Path to save backup (relative to console.php, or absolute)' 
			)
			->addArgument(
				'dateFormat',
				InputArgument::OPTIONAL,
				'Format to use for the date part of the backup file. Defaults to "Y-m-d_H-i-s" and uses the PHP date function format'
			)
		;
	}

	protected function execute(InputInterface $input, OutputInterface $output)
	{
		$path = $input->getArgument('path');
		if (substr($path, -1) == '/') {
			$path = substr($path, 0, strlen($path) - 1);
		}

		if (!is_dir($path)) {
			$output->writeln('<error>Error: Provided path not found</error>');
			return;
		}

		$local = \TikiInit::getCredentialsFile();
		if (! is_readable($local)) {
			$output->writeln('<error>Error: "' . $local . '" not readable.</error>');
			return;
		}

		$dateFormat = $input->getArgument('dateFormat');
		if (! $dateFormat) {
			$dateFormat = 'Y-m-d_H-i-s';
		}

		$user_tiki = $pass_tiki = $host_tiki = $dbs_tiki = '';

		require $local;

		$args = array();
		if( $user_tiki ) {
			$args[] = "-u" . escapeshellarg( $user_tiki );
		}
		if( $pass_tiki ) {
			$args[] = "-p" . escapeshellarg( $pass_tiki );
		}
		if( $host_tiki ) {
			$args[] = "-h" . escapeshellarg( $host_tiki );
		}

		// Check out many non-InnoDB tables exist in the table
		$db = \TikiDb::get();
		$query = "SELECT count(TABLE_NAME) FROM information_schema.TABLES WHERE TABLE_SCHEMA = '$dbs_tiki' AND engine <> 'InnoDB'";
		$numTables = $db->getOne($query);

		if ($numTables === '0') {
			$args[] = "--single-transaction";
		} else {
			$args[] = "--lock-tables";
		}

		$args[] = $dbs_tiki;
	
		$args = implode( ' ', $args );
		$outputFile = $path . '/' . $dbs_tiki . '_' . date($dateFormat) . '.sql.gz';
		$command = "mysqldump --quick --create-options --extended-insert $args | gzip -5 > " . escapeshellarg( $outputFile );<commit_msg>[REF] execute(): Fix comment<commit_after><?php
// (c) Copyright 2002-2016 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$

namespace Tiki\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class BackupDBCommand extends Command
{
	protected function configure()
	{
		$this
			->setName('database:backup')
			->setDescription('Create a database backup (with mysqldump)')
			->addArgument(
				'path',
				InputArgument::REQUIRED,	
				'Path to save backup (relative to console.php, or absolute)' 
			)
			->addArgument(
				'dateFormat',
				InputArgument::OPTIONAL,
				'Format to use for the date part of the backup file. Defaults to "Y-m-d_H-i-s" and uses the PHP date function format'
			)
		;
	}

	protected function execute(InputInterface $input, OutputInterface $output)
	{
		$path = $input->getArgument('path');
		if (substr($path, -1) == '/') {
			$path = substr($path, 0, strlen($path) - 1);
		}

		if (!is_dir($path)) {
			$output->writeln('<error>Error: Provided path not found</error>');
			return;
		}

		$local = \TikiInit::getCredentialsFile();
		if (! is_readable($local)) {
			$output->writeln('<error>Error: "' . $local . '" not readable.</error>');
			return;
		}

		$dateFormat = $input->getArgument('dateFormat');
		if (! $dateFormat) {
			$dateFormat = 'Y-m-d_H-i-s';
		}

		$user_tiki = $pass_tiki = $host_tiki = $dbs_tiki = '';

		require $local;

		$args = array();
		if( $user_tiki ) {
			$args[] = "-u" . escapeshellarg( $user_tiki );
		}
		if( $pass_tiki ) {
			$args[] = "-p" . escapeshellarg( $pass_tiki );
		}
		if( $host_tiki ) {
			$args[] = "-h" . escapeshellarg( $host_tiki );
		}

		// Find out how many non-InnoDB tables exist in the schema
		$db = \TikiDb::get();
		$query = "SELECT count(TABLE_NAME) FROM information_schema.TABLES WHERE TABLE_SCHEMA = '$dbs_tiki' AND engine <> 'InnoDB'";
		$numTables = $db->getOne($query);

		if ($numTables === '0') {
			$args[] = "--single-transaction";
		} else {
			$args[] = "--lock-tables";
		}

		$args[] = $dbs_tiki;
	
		$args = implode( ' ', $args );
		$outputFile = $path . '/' . $dbs_tiki . '_' . date($dateFormat) . '.sql.gz';
		$command = "mysqldump --quick --create-options --extended-insert $args | gzip -5 > " . escapeshellarg( $outputFile );
{'fraction_non_alphanumeric': 0.16657126502575845, 'fraction_numerical': 0.004960885327227629, 'mean_word_length': 3.5622280243690168, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 33, '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': '4140470', 'n_tokens_mistral': 2026, 'n_tokens_neox': 1794, 'n_words': 442}