<commit_before><?php
//das ist notwendig damit cli scripte was mit dem apc cache machen können
//direkt in der cli ist das leider nicht möglich, da der speicher im webserver liegt
class Kwf_Util_Apc
{
    const SILENT = 'silent';
    const VERBOSE = 'verbose';

    public static function getHttpPassword()
    {
        $file = 'cache/apcutilspass';
        if (!file_exists($file)) {
            file_put_contents($file, time().rand(100000, 1000000));
        }
        return file_get_contents($file);
    }

    public static function callClearCacheByCli($params, $verbosity)
    {
        $outputType = '';
        if (isset($param['type']) && $param['type'] == 'user') {
            $outputType = 'apc user';
        } else if (isset($param['type']) && $param['type'] == 'file') {
            $outputType = 'optcode';
        }

        $config = Kwf_Registry::get('config');
        $d = $config->server->domain;
        if (!$d && file_exists('cache/lastdomain')) {
            //this file gets written in Kwf_Setup to make it "just work"
            $d = file_get_contents('cache/lastdomain');
        }
        if (!$d) {
            if ($verbosity == self::VERBOSE) {
                echo "error:       $outputType: domain not set\n";
            }
            return;
        }
        $s = microtime(true);
        $pwd = Kwf_Util_Apc::getHttpPassword();
        $urlPart = "http".($config->server->https?'s':'')."://apcutils:".Kwf_Util_Apc::getHttpPassword()."@";
        $url = "$urlPart$d/kwf/util/apc/clear-cache";

        $client = new Zend_Http_Client();
        $client->setMethod(Zend_Http_Client::POST);
        $client->setParameterPost($params);
        $client->setConfig(array(
            'timeout' => 60,
            'keepalive' => true
        ));

        $client->setUri($url);
        $body = 'could not reach web per http';
        try {
            $response = $client->request();
            $result = !$response->isError() && substr($response->getBody(), 0, 2) == 'OK';
            $body = $response->getBody();
        } catch (Exception $e) {
            $result = false;
        }
        if (!$result && $config->server->noRedirectPattern) {
            $d = str_replace(array('^', '\\', '$'), '', $config->server->noRedirectPattern);
            $url2 = "$urlPart$d/kwf/util/apc/clear-cache";
            try {
                $client->setUri($url2);
                $client->setParameterPost($params);
                $response = $client->request();
                $result = !$response->isError() && substr($response->getBody(), 0, 2) == 'OK';
                $body = $response->getBody();
            } catch (Exception $e) {
                $result = false;
            }
        }
        if ($verbosity == self::VERBOSE) {
            $time = round((microtime(true)-$s)*1000);
            if ($result) {
                echo "cleared:     $outputType ({$time}ms) $body\n";
            } else {
                $outputUrl = $url;
                if (isset($url2)) $outputUrl .= " / $url2";
                $outputUrl = " ($outputUrl)";
                echo "error:       $outputType $outputUrl\n$body\n\n";
            }
        }
    }

    public static function dispatchUtils()
    {
<commit_msg>ClearCache: support multiple aws ec2 servers when deleting apc cache<commit_after><?php
//das ist notwendig damit cli scripte was mit dem apc cache machen können
//direkt in der cli ist das leider nicht möglich, da der speicher im webserver liegt
class Kwf_Util_Apc
{
    const SILENT = 'silent';
    const VERBOSE = 'verbose';

    public static function getHttpPassword()
    {
        $file = 'cache/apcutilspass';
        if (!file_exists($file)) {
            file_put_contents($file, time().rand(100000, 1000000));
        }
        return file_get_contents($file);
    }

    public static function callClearCacheByCli($params, $verbosity)
    {
        $outputType = '';
        if (isset($param['type']) && $param['type'] == 'user') {
            $outputType = 'apc user';
        } else if (isset($param['type']) && $param['type'] == 'file') {
            $outputType = 'optcode';
        }

        $config = Kwf_Registry::get('config');

        if (!$config->server->aws) {
            $d = $config->server->domain;
            if (!$d && file_exists('cache/lastdomain')) {
                //this file gets written in Kwf_Setup to make it "just work"
                $d = file_get_contents('cache/lastdomain');
            }
            if (!$d) {
                if ($verbosity == self::VERBOSE) {
                    echo "error:       $outputType: domain not set\n";
                }
                return;
            }

            $domains = array(
                array(
                    'domain' => $d,
                )
            );
            if ($config->server->noRedirectPattern) {
                $domains[0]['alternative'] = str_replace(array('^', '\\', '$'), '', $config->server->noRedirectPattern);
            }
        } else {
            $ec2 = new Kwf_Util_Aws_Ec2();
            $r = $ec2->describe_instances(array(
                'Filter' => array(
                    array(
                        'Name' => 'tag:application.id',
                        'Value' => $config->application->id,
                    ),
                    array(
                        'Name' => 'tag:config_section',
                        'Value' => Kwf_Setup::getConfigSection(),
                    )
                )
            ));
            if (!$r->isOK()) {
                throw new Kwf_Exception($r->body->asXml());
            }

            $domains = array();
            foreach ($r->body->reservationSet->item as $resItem) {
                foreach ($resItem->instancesSet->item as $item) {
                    $dnsName = (string)$item->dnsName;
                    if ($dnsName) {
                        $domains[] = array(
                            'domain'=>$dnsName,
                        );
                    }
                }
            }

        }

        foreach ($domains as $d) {
            $s = microtime(true);
            $pwd = Kwf_Util_Apc::getHttpPassword();
            $urlPart = "http".($config->server->https?'s':'')."://apcutils:".Kwf_Util_Apc::getHttpPassword()."@";
            $url = "$urlPart$d[domain]/kwf/util/apc/clear-cache";

            $client = new Zend_Http_Client();
            $client->setMethod(Zend_Http_Client::POST);
            $client->setParameterPost($params);
            $client->setConfig(array(
                'timeout' => 60,
                'keepalive' => true
            ));

            $client->setUri($url);
            $body = 'could not reach web per http';
            try {
                $response = $client->request();
                $result = !$response->isError() && substr($response->getBody(), 0, 2) == 'OK';
                $body = $response->getBody();
            } catch (Exception $e) {
                $result = false;
            }
            $url2 = null;
            if (!$result && isset($d['alternative'])) {
                $url2 = "$urlPart$d[alternative]/kwf/util/apc/clear-cache";
                try {
                    $client->setUri($url2);
                    $client->setParameterPost($params);
                    $response = $client->request();
                    $result = !$response->isError() && substr($response->getBody(), 0, 2) == 'OK';
                    $body = $response->getBody();
                } catch (Exception $e) {
                    $result = false;
                }
            }
            if ($verbosity == self::VERBOSE) {
                $outputUrl = $url;
                if ($url2) $outputUrl .= " / $url2";
                $outputUrl = " ($outputUrl)";
                $time = round((microtime(true)-$s)*1000);
                if ($result) {
                    echo "cleared:     $outputType $outputUrl\n    ({$time}ms) $body\n";
                } else {
                    echo "error:       $outputType $outputUrl\n    $body\n\n";
                }
            }
        }
    }

    public static function dispatchUtils()
    {

{'fraction_non_alphanumeric': 0.15466568699485672, 'fraction_numerical': 0.0073475385745775165, 'mean_word_length': 1.5253555967841683, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 72, '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': '7611342', 'n_tokens_mistral': 2415, 'n_tokens_neox': 2281, 'n_words': 464}