<commit_before><?php
/**
 * This file is part of the RedKite CMS Application and it is distributed
 * under the GPL LICENSE Version 2.0. To use this application you must leave
 * intact this copyright notice.
 *
 * Copyright (c) RedKite Labs <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * For extra documentation and help please visit http://www.redkite-labs.com
 *
 * @license    GPL LICENSE Version 2.0
 *
 */

namespace RedKiteCms\Tools;

use RedKiteCms\Exception\General\RuntimeException;

/**
 * Class FilesystemTools collects several methods related to the filesystem
 *
 * @author  RedKite Labs <<EMAIL>>
 * @package RedKiteCms\Tools
 */
class FilesystemTools
{
    /**
     * Looks for the current slots dir. Slots are sought in cascade in pages folders, languages repeated slots and site
     * repeated slots
     *
     * @param $sourceDir
     * @param array $options
     *
     * @return null|string
     */
    public static function slotDir($sourceDir, array $options)
    {
        $paths = array(
            sprintf(
                '%s/pages/pages/%s/%s_%s/%s',
                $sourceDir,
                $options['page'],
                $options['language'],
                $options['country'],
                $options['slot']
            ),
            sprintf('%s/slots/%s/%s_%s', $sourceDir, $options['slot'], $options['language'], $options['country']),
            sprintf('%s/slots/%s', $sourceDir, $options['slot']),
        );

        return self::cascade($paths);
    }

    /**
     * Parses given folders and returns the first existing one
     *
     * @param array $folder
     *
     * @return null|string
     */
    public static function cascade(array $folders)
    {
        $result = null;
        foreach ($folders as $folder) {
            if (is_dir($folder)) {
                $result = $folder;

                break;
            }
        }

        return $result;
    }

    /**
     * Reads and locks a file from the filesystem
     *
     * @param $file
     *
     * @return string
     */
    public static function readFile($file)
    {
        if (!file_exists($file)) {
            return null;
        }

        $handle = fopen($file, 'r');
        if (!self::lockFile($handle, LOCK_SH | LOCK_NB)) {
            $exception = array(
                "message" => 'exception_file_cannot_be_locked_for_reading',
                "parameters" => array(
                    "%file%" => basename($file),
                )
            );
            throw new RuntimeException(json_encode($exception));
        }

        $contents = file_get_contents($file);
        self::unlockFile($handle);

        return $contents;
    }

    /**
     * Locks and write a file to the filesystem
     *
     * @param string $file
     * @param string $content
     */
    public static function writeFile($file, $content)
    {
        $handle = fopen($file, 'w');
        if (!self::lockFile($handle, LOCK_EX | LOCK_NB)) {
            $exception = array(
                "message" => 'exception_file_cannot_be_locked_for_writing',
                "parameters" => array(
                    "%file%" => basename($file),
                )
            );
            throw new RuntimeException(json_encode($exception));
        }
        if (fwrite($handle, $content) === false) {
            $exception = array(
                "message" => 'exception_file_cannot_be_written',
                "parameters" => array(
                    "%file%" => basename($file),
                    "%error%" => error_get_last()['message'],
                )
            );
            throw new RuntimeException(json_encode($exception));
        }
        self::unlockFile($handle);
    }

    private static function lockFile($handle, $flags)
    {
        $locked = false;
        $tries = 5;
        while ($tries > 0) {
            $locked = flock($handle, $flags);
            if (false !== $locked) {
                break;
            }

            sleep(1);
            $tries--;
        }

        return $locked;
    }

    private static function unlockFile($handle)
    {
        flock($handle, LOCK_UN);
        fclose($handle);
    }
}<commit_msg>fixed for php 5.3<commit_after><?php
/**
 * This file is part of the RedKite CMS Application and it is distributed
 * under the GPL LICENSE Version 2.0. To use this application you must leave
 * intact this copyright notice.
 *
 * Copyright (c) RedKite Labs <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * For extra documentation and help please visit http://www.redkite-labs.com
 *
 * @license    GPL LICENSE Version 2.0
 *
 */

namespace RedKiteCms\Tools;

use RedKiteCms\Exception\General\RuntimeException;

/**
 * Class FilesystemTools collects several methods related to the filesystem
 *
 * @author  RedKite Labs <<EMAIL>>
 * @package RedKiteCms\Tools
 */
class FilesystemTools
{
    /**
     * Looks for the current slots dir. Slots are sought in cascade in pages folders, languages repeated slots and site
     * repeated slots
     *
     * @param $sourceDir
     * @param array $options
     *
     * @return null|string
     */
    public static function slotDir($sourceDir, array $options)
    {
        $paths = array(
            sprintf(
                '%s/pages/pages/%s/%s_%s/%s',
                $sourceDir,
                $options['page'],
                $options['language'],
                $options['country'],
                $options['slot']
            ),
            sprintf('%s/slots/%s/%s_%s', $sourceDir, $options['slot'], $options['language'], $options['country']),
            sprintf('%s/slots/%s', $sourceDir, $options['slot']),
        );

        return self::cascade($paths);
    }

    /**
     * Parses given folders and returns the first existing one
     *
     * @param array $folder
     *
     * @return null|string
     */
    public static function cascade(array $folders)
    {
        $result = null;
        foreach ($folders as $folder) {
            if (is_dir($folder)) {
                $result = $folder;

                break;
            }
        }

        return $result;
    }

    /**
     * Reads and locks a file from the filesystem
     *
     * @param $file
     *
     * @return string
     */
    public static function readFile($file)
    {
        if (!file_exists($file)) {
            return null;
        }

        $handle = fopen($file, 'r');
        if (!self::lockFile($handle, LOCK_SH | LOCK_NB)) {
            $exception = array(
                "message" => 'exception_file_cannot_be_locked_for_reading',
                "parameters" => array(
                    "%file%" => basename($file),
                )
            );
            throw new RuntimeException(json_encode($exception));
        }

        $contents = file_get_contents($file);
        self::unlockFile($handle);

        return $contents;
    }

    /**
     * Locks and write a file to the filesystem
     *
     * @param string $file
     * @param string $content
     */
    public static function writeFile($file, $content)
    {
        $handle = fopen($file, 'w');
        if (!self::lockFile($handle, LOCK_EX | LOCK_NB)) {
            $exception = array(
                "message" => 'exception_file_cannot_be_locked_for_writing',
                "parameters" => array(
                    "%file%" => basename($file),
                )
            );
            throw new RuntimeException(json_encode($exception));
        }
        if (fwrite($handle, $content) === false) {
            $error = error_get_last();
            $exception = array(
                "message" => 'exception_file_cannot_be_written',
                "parameters" => array(
                    "%file%" => basename($file),
                    "%error%" => $error['message'],
                )
            );
            throw new RuntimeException(json_encode($exception));
        }
        self::unlockFile($handle);
    }

    private static function lockFile($handle, $flags)
    {
        $locked = false;
        $tries = 5;
        while ($tries > 0) {
            $locked = flock($handle, $flags);
            if (false !== $locked) {
                break;
            }

            sleep(1);
            $tries--;
        }

        return $locked;
    }

    private static function unlockFile($handle)
    {
        flock($handle, LOCK_UN);
        fclose($handle);
    }
}
{'fraction_non_alphanumeric': 0.12391127627453258, 'fraction_numerical': 0.001858088491464406, 'mean_word_length': 1.7567221510883482, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7481161', 'n_tokens_mistral': 2447, 'n_tokens_neox': 2331, 'n_words': 609}