<commit_before><?php
class utils
{
    public static function addVersion($name, $data)
    {
        $a[$name] = $data;
        $a['API version'] = RG2VERSION;
        return json_encode($a);
    }

    public static function rg2log($msg)
    {
        if (defined('RG_LOG_FILE')) {
            if (! ini_get('date.timezone')) {
                date_default_timezone_set('GMT');
            }
            error_log(date("c", time())."|".$msg.PHP_EOL, 3, RG_LOG_FILE);
            if (filesize(RG_LOG_FILE) > 500000) {
                rename(RG_LOG_FILE, RG_LOG_FILE."-".date("Y-m-d-His", time()));
            }
        }
    }

    public static function lockDatabase()
    {
        // lock directory version based on http://docstore.mik.ua/oreilly/webprog/pcook/ch18_25.htm
        // but note mkdir returns TRUE if directory already exists!
        $locked = false;

        // tidy up from possible locking errors
        if (is_dir(LOCK_DIRECTORY)) {
            // if lock directory is more than a few seconds old it wasn't deleted properly, so we'll delete it ourselves
            // not sure exactly how time changes work, but we can live with a possible twice
            // a year problem since locking is probably almost never needed
            if ((time() - filemtime(LOCK_DIRECTORY)) > 15) {
                self::unlockDatabase();
            }
        }
        if (is_dir(LOCK_DIRECTORY)) {
            // locked already by someone else
      //rg2log("Directory exists ".date("D M j G:i:s T Y", filemtime(LOCK_DIRECTORY)));
        } else {
            // try to lock it ourselves
            //rg2log("Trying to lock");
            $locked = mkdir(LOCK_DIRECTORY, 0777);
        }
        $tries = 0;
        while (!$locked && ($tries < 5)) {
            // wait 500ms up to 5 times trying to get a lock
            usleep(500000);
            if (is_dir(LOCK_DIRECTORY)) {
                // locked already by someone else
                $locked = false;
            } else {
                // try to lock it ourselves
                $locked = mkdir(LOCK_DIRECTORY, 0777);
            }
            $tries++;
            //rg2log("Lock attempt ".$tries);
        }
        //rg2log("Lock status ".$locked);
        return $locked;
    }

    public static function unlockDatabase()
    {
        // ignore any errors but try to tidy up everything
        @rmdir(LOCK_DIRECTORY);
    }

    public static function tidyNewComments($inputComments)
    {
        $comments = trim($inputComments);
        // remove HTML tags: probably not needed but do it anyway
        $comments = strip_tags($comments);
        // remove line breaks and keep compatibility with RG1
        $comments = str_replace("\r", "", $comments);
        $comments = str_replace("\n", "#cr##nl#", $comments);
        $comments = utils::encode_rg_output($comments);
        return $comments;
    }

    public static function tidyTime($in)
    {
        // takes what should be a time as mm:ss or hh:mm:ss and tidies it up
        // remove ".:" and "::" which RG1 can generate when adding results
        $t = str_replace('.:', ':', $in);
        $t = str_replace('::', ":", $t);
        // remove leading 0:
        if (substr($t, 0, 2) === '0:') {
            $t = substr($t, 2);
        }
        // correct seconds for missing leading 0 which RG1 can generate from Emit. e.g 25:9 becomes 25:09
        $secs = substr($t, -2);
        if (substr($secs, 0, 1) ===  ':') {
            $t = substr_replace($t, '0', -1, 0);
        }
        return $t;
    }

    public static function getAngle($x1, $y1, $x2, $y2)
    {
        $angle = atan2($y2 - $y1, $x2 - $x1);
        if ($angle < 0) {
            $angle = $angle + (2 * M_PI);
        }
        return $angle;
    }
<commit_msg>Tidy up time display<commit_after><?php
class utils
{
    public static function addVersion($name, $data)
    {
        $a[$name] = $data;
        $a['API version'] = RG2VERSION;
        return json_encode($a);
    }

    public static function rg2log($msg)
    {
        if (defined('RG_LOG_FILE')) {
            if (! ini_get('date.timezone')) {
                date_default_timezone_set('GMT');
            }
            error_log(date("c", time())."|".$msg.PHP_EOL, 3, RG_LOG_FILE);
            if (filesize(RG_LOG_FILE) > 500000) {
                rename(RG_LOG_FILE, RG_LOG_FILE."-".date("Y-m-d-His", time()));
            }
        }
    }

    public static function lockDatabase()
    {
        // lock directory version based on http://docstore.mik.ua/oreilly/webprog/pcook/ch18_25.htm
        // but note mkdir returns TRUE if directory already exists!
        $locked = false;

        // tidy up from possible locking errors
        if (is_dir(LOCK_DIRECTORY)) {
            // if lock directory is more than a few seconds old it wasn't deleted properly, so we'll delete it ourselves
            // not sure exactly how time changes work, but we can live with a possible twice
            // a year problem since locking is probably almost never needed
            if ((time() - filemtime(LOCK_DIRECTORY)) > 15) {
                self::unlockDatabase();
            }
        }
        if (is_dir(LOCK_DIRECTORY)) {
            // locked already by someone else
      //rg2log("Directory exists ".date("D M j G:i:s T Y", filemtime(LOCK_DIRECTORY)));
        } else {
            // try to lock it ourselves
            //rg2log("Trying to lock");
            $locked = mkdir(LOCK_DIRECTORY, 0777);
        }
        $tries = 0;
        while (!$locked && ($tries < 5)) {
            // wait 500ms up to 5 times trying to get a lock
            usleep(500000);
            if (is_dir(LOCK_DIRECTORY)) {
                // locked already by someone else
                $locked = false;
            } else {
                // try to lock it ourselves
                $locked = mkdir(LOCK_DIRECTORY, 0777);
            }
            $tries++;
            //rg2log("Lock attempt ".$tries);
        }
        //rg2log("Lock status ".$locked);
        return $locked;
    }

    public static function unlockDatabase()
    {
        // ignore any errors but try to tidy up everything
        @rmdir(LOCK_DIRECTORY);
    }

    public static function tidyNewComments($inputComments)
    {
        $comments = trim($inputComments);
        // remove HTML tags: probably not needed but do it anyway
        $comments = strip_tags($comments);
        // remove line breaks and keep compatibility with RG1
        $comments = str_replace("\r", "", $comments);
        $comments = str_replace("\n", "#cr##nl#", $comments);
        $comments = utils::encode_rg_output($comments);
        return $comments;
    }

    public static function tidyTime($in)
    {
        // takes what should be a time as mm:ss or hh:mm:ss and tidies it up
        // remove ".:" and "::" which RG1 can generate when adding results
        $t = str_replace('.:', ':', $in);
        $t = str_replace('::', ":", $t);
        // remove leading 0:
        if (substr($t, 0, 2) === '0:') {
            $t = substr($t, 2);
        }
        // correct seconds for missing leading 0 which RG1 can generate from Emit. e.g 25:9 becomes 25:09
        $secs = substr($t, -2);
        if (substr($secs, 0, 1) ===  ':') {
            $t = substr_replace($t, '0', -1, 0);
        }
        // prevent negative times which can slip through for various reasons
        if (substr($t, 0, 1) === "-") {
          $t = "0:00";
        }
        // convert hh:mm:ss to mm:ss for consistency
        $bits = explode(":", $t);
        if (count($bits) === 3) {
          $mins = (intval($bits[0]) * 60) + intval($bits[1]);
          $t = $mins.":".$bits[2]; 
        }
        return $t;
    }

    public static function getAngle($x1, $y1, $x2, $y2)
    {
        $angle = atan2($y2 - $y1, $x2 - $x1);
        if ($angle < 0) {
            $angle = $angle + (2 * M_PI);
        }
        return $angle;
    }

{'fraction_non_alphanumeric': 0.13468309859154928, 'fraction_numerical': 0.01949195171026157, 'mean_word_length': 1.768186564566655, 'pattern_counts': {'":': 6, '<': 9, '<?xml version=': 0, '>': 7, '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': '6761618', 'n_tokens_mistral': 2493, 'n_tokens_neox': 2340, 'n_words': 699}