<commit_before><?php

namespace backend\controllers;

use backend\models\Restore;
use backend\models\UploadBackup;
use common\models\Image;
use yii\web\Controller;
use yii\base\Exception;
use yii\data\ArrayDataProvider;
use yii\web\HttpException;
use yii\web\UploadedFile;

class BackupController extends Controller
{
    public $tables = [];
    public $fp ;
    public $file_name;
    public $_path = null;
    public $back_temp_file = 'db_backup_';
    public $view_table = [];
    public $count = 0;

    protected function getPath()
    {
        if ( isset ($this->module->path )) $this->_path = $this->module->path;
        else $this->_path = \Yii::$app->basePath .'/_backup/';

        if ( !file_exists($this->_path ))
        {
            mkdir($this->_path );
            chmod($this->_path, '777');
        }
        return $this->_path;
    }

    public function execSqlFile($sqlFile)
    {
        $message = "ok";

        if ( file_exists($sqlFile))
        {
            $sqlArray = file_get_contents($sqlFile);

            $cmd = \Yii::$app->db->createCommand($sqlArray);
            try	{
                $cmd->execute();
            }
            catch(Exception $e)
            {
                $message = $e->getMessage();
            }

        }
        return $message;
    }
    public function getColumns($tableName)
    {
        $sql = 'SHOW CREATE TABLE `'.$tableName . '`';
        $cmd = \Yii::$app->db->createCommand($sql);
        $table = $cmd->queryOne();

        $is_view = false;

        $create_query = '';

        if (array_key_exists('Create Table', $table)) {
            $create_query = $table['Create Table'] . ';';
        } else if(array_key_exists('Create View', $table)) {
            $is_view = true;
            $create_query .= $table['Create View'] . ';';
        }

        if (!$is_view) {
            $create_query = preg_replace('/^CREATE TABLE/', 'CREATE TABLE IF NOT EXISTS', $create_query);
            $create_query = preg_replace('/AUTO_INCREMENT\s*=\s*([0-9])+/', '', $create_query);
        } else if ($is_view) {
            $create_query = preg_replace('/^CREATE (.*) VIEW/', 'CREATE OR REPLACE VIEW', $create_query);
        }

        if ( $this->fp)
        {
            if(!$is_view) {
                $this->writeComment('TABLE `' . addslashes($tableName) . '`');
                $final = 'DROP TABLE IF EXISTS `' . addslashes($tableName) . '`;' . PHP_EOL . $create_query . PHP_EOL . PHP_EOL;
                fwrite($this->fp, $final);
            } else if ($is_view) {
                $this->view_table[$this->count]['view_name'] = 'VIEW `' . addslashes($tableName) . '`';
                $this->view_table[$this->count]['schema'] = $create_query . PHP_EOL . PHP_EOL;
                $this->count++;
            }
        }
        else
        {
            $this->tables[$tableName]['create'] = $create_query;
            return $create_query;
        }
    }

    public function getData($tableName)
    {
        $sql = 'SHOW CREATE TABLE `'.$tableName . '`';
        $cmd = \Yii::$app->db->createCommand($sql);
        $table = $cmd->queryOne();

        if (array_key_exists('Create View', $table)) {
            return;
        }

        $sql = 'SELECT * FROM `'.$tableName . '`';
        $cmd = \Yii::$app->db->createCommand($sql);
        $dataReader = $cmd->query();

        $data_string = '';

        foreach($dataReader as $data)
        {
            $itemNames = array_keys($data);
            $itemNames = array_map("addslashes", $itemNames);
            $items = join('`,`', $itemNames);
            $itemValues = array_values($data);
            $itemValues = array_map("addslashes", $itemValues);
            $valueString = '';
            foreach ($itemValues as $itemValue) {
                if (is_numeric($itemValue)) {
                    $valueString .= $itemValue . ",";
                } else if ($itemValue == '') {
                    $valueString .= "NULL";
                } else {
                    $valueString .= "'" . $itemValue . "',";
                }
            }
            $valueString = trim($valueString, ",");
            $valueString = "(" . $valueString . ")";
            $values ="\n" . $valueString;
            if ($values != "")
            {
                $data_string .= "INSERT INTO `$tableName` (`$items`) VALUES" . rtrim($values, ",") . ";" . PHP_EOL;
            }
        }

        if ( $data_string == '')
            return null;

        if ( $this->fp)
        {
            $this->writeComment('TABLE DATA '.$tableName);
            $final = $data_string.PHP_EOL.PHP_EOL.PHP_EOL;
            fwrite ( $this->fp, $final );
        }
        else
        {
            $this->tables[$tableName]['data'] = $data_string;<commit_msg>Fixed backup<commit_after><?php

namespace backend\controllers;

use backend\models\Restore;
use backend\models\UploadBackup;
use common\models\Image;
use yii\web\Controller;
use yii\base\Exception;
use yii\data\ArrayDataProvider;
use yii\web\HttpException;
use yii\web\UploadedFile;

class BackupController extends Controller
{
    public $tables = [];
    public $fp ;
    public $file_name;
    public $_path = null;
    public $back_temp_file = 'db_backup_';
    public $view_table = [];
    public $count = 0;

    protected function getPath()
    {
        if ( isset ($this->module->path )) $this->_path = $this->module->path;
        else $this->_path = \Yii::$app->basePath .'/_backup/';

        if ( !file_exists($this->_path ))
        {
            mkdir($this->_path );
            chmod($this->_path, '777');
        }
        return $this->_path;
    }

    public function execSqlFile($sqlFile)
    {
        $message = "ok";

        if ( file_exists($sqlFile))
        {
            $sqlArray = file_get_contents($sqlFile);

            $cmd = \Yii::$app->db->createCommand($sqlArray);
            try	{
                $cmd->execute();
            }
            catch(Exception $e)
            {
                $message = $e->getMessage();
            }

        }
        return $message;
    }
    public function getColumns($tableName)
    {
        $sql = 'SHOW CREATE TABLE `'.$tableName . '`';
        $cmd = \Yii::$app->db->createCommand($sql);
        $table = $cmd->queryOne();

        $is_view = false;

        $create_query = '';

        if (array_key_exists('Create Table', $table)) {
            $create_query = $table['Create Table'] . ';';
        } else if(array_key_exists('Create View', $table)) {
            $is_view = true;
            $create_query .= $table['Create View'] . ';';
        }

        if (!$is_view) {
            $create_query = preg_replace('/^CREATE TABLE/', 'CREATE TABLE IF NOT EXISTS', $create_query);
            $create_query = preg_replace('/AUTO_INCREMENT\s*=\s*([0-9])+/', '', $create_query);
        } else if ($is_view) {
            $create_query = preg_replace('/^CREATE (.*) VIEW/', 'CREATE OR REPLACE VIEW', $create_query);
        }

        if ( $this->fp)
        {
            if(!$is_view) {
                $this->writeComment('TABLE `' . addslashes($tableName) . '`');
                $final = 'DROP TABLE IF EXISTS `' . addslashes($tableName) . '`;' . PHP_EOL . $create_query . PHP_EOL . PHP_EOL;
                fwrite($this->fp, $final);
            } else if ($is_view) {
                $this->view_table[$this->count]['view_name'] = 'VIEW `' . addslashes($tableName) . '`';
                $this->view_table[$this->count]['schema'] = $create_query . PHP_EOL . PHP_EOL;
                $this->count++;
            }
        }
        else
        {
            $this->tables[$tableName]['create'] = $create_query;
            return $create_query;
        }
    }

    public function getData($tableName)
    {
        $sql = 'SHOW CREATE TABLE `'.$tableName . '`';
        $cmd = \Yii::$app->db->createCommand($sql);
        $table = $cmd->queryOne();

        if (array_key_exists('Create View', $table)) {
            return;
        }

        $sql = 'SELECT * FROM `'.$tableName . '`';
        $cmd = \Yii::$app->db->createCommand($sql);
        $dataReader = $cmd->query();

        $data_string = '';

        foreach($dataReader as $data)
        {
            $itemNames = array_keys($data);
            $itemNames = array_map("addslashes", $itemNames);
            $items = join('`,`', $itemNames);
            $itemValues = array_values($data);
            $itemValues = array_map("addslashes", $itemValues);
            $valueString = '';
            foreach ($itemValues as $itemValue) {
                if (is_numeric($itemValue)) {
                    $valueString .= $itemValue . ",";
                } else if ($itemValue == '') {
                    $valueString .= "NULL,";
                } else {
                    $valueString .= "'" . $itemValue . "',";
                }
            }
            $valueString = trim($valueString, ",");
            $valueString = "(" . $valueString . ")";
            $values ="\n" . $valueString;
            if ($values != "")
            {
                $data_string .= "INSERT INTO `$tableName` (`$items`) VALUES" . rtrim($values, ",") . ";" . PHP_EOL;
            }
        }

        if ( $data_string == '')
            return null;

        if ( $this->fp)
        {
            $this->writeComment('TABLE DATA '.$tableName);
            $final = $data_string.PHP_EOL.PHP_EOL.PHP_EOL;
            fwrite ( $this->fp, $final );
        }
        else
        {
            $this->tables[$tableName]['data'] = $data_string;
{'fraction_non_alphanumeric': 0.16596946245555322, 'fraction_numerical': 0.0012549675800041832, 'mean_word_length': 1.7369776760160274, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 77, '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': '5817958', 'n_tokens_mistral': 2943, 'n_tokens_neox': 2877, 'n_words': 520}