<commit_before><?php
class Vps_Controller_Action_Cli_ImportController extends Vps_Controller_Action_Cli_Abstract
{
    private $_useSshVps;
    private $_sshHost;
    private $_sshDir;
    public function indexAction()
    {
        $this->_useSshVps = file_exists('/usr/local/bin/sshvps');
        $localHosts = array(
            'vivid',
            'vivid-test-server'
        );

        $ownConfig = Vps_Registry::get('config');

        $server = $this->_getParam('server');
        if (!$server) {
            if ($ownConfig->server->testOf) {
                $server = $ownConfig->server->testOf;
            } else {
                $server = 'production';
            }
        }
        if (Vps_Setup::getConfigSection() == $server) {
            throw new Vps_ClientException("Von dir selbst importieren ist natuerlich nicht moeglich.");
        }
        if (Vps_Setup::getConfigSection() == 'production') {
            echo "ACHTUNG!!!!\n";
            echo "Du willst auf production importieren, dabei werden alle Daten auf production ueberschrieben.\n";
            echo "Bist du dir wirklich, wirklich sicher?\n";
            echo "[N/j]";
            $stdin = fopen('php://stdin', 'r');
            $input = trim(strtolower(fgets($stdin, 2)));
            fclose($stdin);
            if ($input != 'j' && $input != 'y') {
                exit;
            }
        }

        $config = Vps_Config_Web::getInstance($server);
        if (!$config->server || !$config->server->host) {
            throw new Vps_ClientException("kein server host konfiguriert");
        }
        if (in_array($config->server->host, $localHosts) && !in_array($ownConfig->server->host, $localHosts)) {
            if ($config->server->host != 'vivid') {
                throw new Vps_ClientException("Nur von vivid kann nach online importiert werden");
            }
            $config->server->host = 'intern.vivid-planet.com';
        }
        $this->_sshHost = $config->server->user.'@'.$config->server->host;
        $this->_sshPort = $config->server->port;
        $this->_sshDir = $config->server->dir;

        if ($ownConfig->server->host == $config->server->host) {
            $cmd = "cd {$config->server->dir} && php bootstrap.php import get-update-revision";
        } else if ($this->_useSshVps) {
            $cmd = "sudo -u vps sshvps $this->_sshHost:$this->_sshPort $this->_sshDir import get-update-revision";
        } else {
            $cmd = "ssh -p $this->_sshPort $this->_sshHost ".escapeshellarg("cd $this->_sshDir && php bootstrap.php import get-update-revision");
        }
        if ($this->_getParam('debug')) echo $cmd."\n";
        exec($cmd, $onlineRevision, $ret);
        if ($ret != 0) throw new Vps_ClientException();
        $onlineRevision = implode('', $onlineRevision);

        if (!$onlineRevision) {
            throw new Vps_ClientException("Can't get onlineRevision");
        }

        if (!$this->_getParam('skip-users') && Vps_Registry::get('config')->application->id != 'service') {
            if (Vps_Setup::getConfigSection() == 'production') {
                echo "\nAuf Production wird der user service NICHT importiert, das haette fatale Folgen.\n";
                echo "Moeglicherweise muss 'vps create-users' ausgefuehrt werden\n\n";
            } else {
                $isLocalServiceUrl = preg_match('#^http://[^/]+\\.vivid/#', $config->service->usersAll->url);
                if (!in_array($ownConfig->server->host, $localHosts) && $isLocalServiceUrl) {
                    echo "\nKann user nicht importieren da diese von einem lokalen service sind.\n";
                    echo "Moeglicherweise muss 'vps create-users' ausgefuehrt werden\n\n";
                } else {
                    $this->_copyServiceUsers($server);
                }
            }
        }

        if ($config->uploads && $ownConfig->uploads) {
            echo "kopiere uploads...\n";
            if ($ownConfig->server->host == $config->server->host) {
                if ($ownConfig->uploads == $config->uploads) {
                    throw new Vps_ClientException("Uplodas-Pfade für beide Server sind gleich!");
                }
                if (!file_exists($ownConfig->uploads)) {
                    mkdir($ownConfig->uploads);
                }
                $cmd = "rsync --progress --delete --times --exclude=cache/ --recursive {$config->uploads}/ {$ownConfig->uploads}/";
                if ($this->_getParam('debug')) echo "$cmd\n";
                $this->_systemCheckRet($cmd);
            } else if ($this->_useSshVps) {
                $this->_systemSshVps('copy-uploads '.$ownConfig->uploads.'/', $config->uploads);
            } else if ($config->server->host == 'vivid' && !in_array($ownConfig->server->host, $localHosts)) {
                $cmd = "rsync --progress --delete --times --exclude=cache/ --recursive {$this->_sshHost}:{$config->uploads}/ {$ownConfig->uploads}/";
                if ($this->_getParam('debug')) echo "$cmd\n";
                $this->_systemCheckRet($cmd);
            } else {
                $cmd = "rsync -e 'ssh -p $this->_sshPort' --progress --delete --times --exclude=cache/ --recursive {$this->_sshHost}:{$config->uploads}/ {$ownConfig->uploads}/";
                if ($this->_getParam('debug')) echo "$cmd\n";
                $this->_systemCheckRet($cmd);
            }
        }

        if ($config->server->import && $config->server->import->dirs) {
            foreach ($config->server->import->dirs as $dir) {
                echo "importing $dir...\n";
                $ig = trim(`svn propget --recursive svn:ignore $dir`);
                if (substr($ig, 0, strlen($dir))==$dir) $ig = substr($ig, strlen($dir));
                foreach (preg_split("#\n".preg_quote($dir)."#", $ig) as $p) {
                    if (preg_match("#^([^ ]*) - (.*?)$#s", $p, $m)) {
                        foreach (explode("\n", trim($m[2])) as $i) {
                            $ignores[] = $dir.$m[1].'/'.trim($i);
                        }
                    }
                }
                if (!$ignores) continue;
                $this->_importFiles($config, $ownConfig, $ignores);
            }
        }
        if ($this->_getParam('include-cache')) {
            echo "importing cache dirs...\n";<commit_msg>propget --xml wird noch nicht so lang unterstützt<commit_after><?php
class Vps_Controller_Action_Cli_ImportController extends Vps_Controller_Action_Cli_Abstract
{
    private $_useSshVps;
    private $_sshHost;
    private $_sshDir;
    public function indexAction()
    {
        $this->_useSshVps = file_exists('/usr/local/bin/sshvps');
        $localHosts = array(
            'vivid',
            'vivid-test-server'
        );

        $ownConfig = Vps_Registry::get('config');

        $server = $this->_getParam('server');
        if (!$server) {
            if ($ownConfig->server->testOf) {
                $server = $ownConfig->server->testOf;
            } else {
                $server = 'production';
            }
        }
        if (Vps_Setup::getConfigSection() == $server) {
            throw new Vps_ClientException("Von dir selbst importieren ist natuerlich nicht moeglich.");
        }
        if (Vps_Setup::getConfigSection() == 'production') {
            echo "ACHTUNG!!!!\n";
            echo "Du willst auf production importieren, dabei werden alle Daten auf production ueberschrieben.\n";
            echo "Bist du dir wirklich, wirklich sicher?\n";
            echo "[N/j]";
            $stdin = fopen('php://stdin', 'r');
            $input = trim(strtolower(fgets($stdin, 2)));
            fclose($stdin);
            if ($input != 'j' && $input != 'y') {
                exit;
            }
        }

        $config = Vps_Config_Web::getInstance($server);
        if (!$config->server || !$config->server->host) {
            throw new Vps_ClientException("kein server host konfiguriert");
        }
        if (in_array($config->server->host, $localHosts) && !in_array($ownConfig->server->host, $localHosts)) {
            if ($config->server->host != 'vivid') {
                throw new Vps_ClientException("Nur von vivid kann nach online importiert werden");
            }
            $config->server->host = 'intern.vivid-planet.com';
        }
        $this->_sshHost = $config->server->user.'@'.$config->server->host;
        $this->_sshPort = $config->server->port;
        $this->_sshDir = $config->server->dir;

        if ($ownConfig->server->host == $config->server->host) {
            $cmd = "cd {$config->server->dir} && php bootstrap.php import get-update-revision";
        } else if ($this->_useSshVps) {
            $cmd = "sudo -u vps sshvps $this->_sshHost:$this->_sshPort $this->_sshDir import get-update-revision";
        } else {
            $cmd = "ssh -p $this->_sshPort $this->_sshHost ".escapeshellarg("cd $this->_sshDir && php bootstrap.php import get-update-revision");
        }
        if ($this->_getParam('debug')) echo $cmd."\n";
        exec($cmd, $onlineRevision, $ret);
        if ($ret != 0) throw new Vps_ClientException();
        $onlineRevision = implode('', $onlineRevision);

        if (!$onlineRevision) {
            throw new Vps_ClientException("Can't get onlineRevision");
        }

        if (!$this->_getParam('skip-users') && Vps_Registry::get('config')->application->id != 'service') {
            if (Vps_Setup::getConfigSection() == 'production') {
                echo "\nAuf Production wird der user service NICHT importiert, das haette fatale Folgen.\n";
                echo "Moeglicherweise muss 'vps create-users' ausgefuehrt werden\n\n";
            } else {
                $isLocalServiceUrl = preg_match('#^http://[^/]+\\.vivid/#', $config->service->usersAll->url);
                if (!in_array($ownConfig->server->host, $localHosts) && $isLocalServiceUrl) {
                    echo "\nKann user nicht importieren da diese von einem lokalen service sind.\n";
                    echo "Moeglicherweise muss 'vps create-users' ausgefuehrt werden\n\n";
                } else {
                    $this->_copyServiceUsers($server);
                }
            }
        }

        if ($config->uploads && $ownConfig->uploads) {
            echo "kopiere uploads...\n";
            if ($ownConfig->server->host == $config->server->host) {
                if ($ownConfig->uploads == $config->uploads) {
                    throw new Vps_ClientException("Uplodas-Pfade für beide Server sind gleich!");
                }
                if (!file_exists($ownConfig->uploads)) {
                    mkdir($ownConfig->uploads);
                }
                $cmd = "rsync --progress --delete --times --exclude=cache/ --recursive {$config->uploads}/ {$ownConfig->uploads}/";
                if ($this->_getParam('debug')) echo "$cmd\n";
                $this->_systemCheckRet($cmd);
            } else if ($this->_useSshVps) {
                $this->_systemSshVps('copy-uploads '.$ownConfig->uploads.'/', $config->uploads);
            } else if ($config->server->host == 'vivid' && !in_array($ownConfig->server->host, $localHosts)) {
                $cmd = "rsync --progress --delete --times --exclude=cache/ --recursive {$this->_sshHost}:{$config->uploads}/ {$ownConfig->uploads}/";
                if ($this->_getParam('debug')) echo "$cmd\n";
                $this->_systemCheckRet($cmd);
            } else {
                $cmd = "rsync -e 'ssh -p $this->_sshPort' --progress --delete --times --exclude=cache/ --recursive {$this->_sshHost}:{$config->uploads}/ {$ownConfig->uploads}/";
                if ($this->_getParam('debug')) echo "$cmd\n";
                $this->_systemCheckRet($cmd);
            }
        }

        if ($config->server->import && $config->server->import->dirs) {
            foreach ($config->server->import->dirs as $dir) {
                echo "importing $dir...\n";
                $ignores = array();
                $ig = trim(`svn propget --recursive svn:ignore $dir`);
                if (substr($ig, 0, strlen($dir))==$dir) $ig = substr($ig, strlen($dir));
                foreach (preg_split("#\n".preg_quote($dir)."#", $ig) as $p) {
                    if (preg_match("#^([^ ]*) - (.*?)$#s", $p, $m)) {
                        foreach (explode("\n", trim($m[2])) as $i) {
                            $ignores[] = $dir.$m[1].'/'.trim($i);
                        }
                    }
                }
                if (!$ignores) continue;
                $this->_importFiles($config, $ownConfig, $ignores);
            }
        }
        if ($this->_getParam('include-cache')) {
            echo "importing cache dirs...\n";
{'fraction_non_alphanumeric': 0.16779634300126103, 'fraction_numerical': 0.0007881462799495586, 'mean_word_length': 1.9325167552576843, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 191, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, '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': '3392054', 'n_tokens_mistral': 3810, 'n_tokens_neox': 3612, 'n_words': 748}