<commit_before><?php
defined('COT_CODE') or die('Wrong URL');

/**
 * Cotonti Resource control class
 *
 * @package API - Resources
 * @copyright (c) Cotonti Team
 * @license https://github.com/Cotonti/Cotonti/blob/master/License.txt
 */
class Resources
{
    /**
     * @var array predefined aliases
     */
    protected static $alias = array(
        '@jQuery' => 'lib/components/jquery/jquery.min.js',

        '@ckeditor' => 'plugins/ckeditor/lib/ckeditor.js',
        '@ckeditorPreset.js' => 'plugins/ckeditor/presets/ckeditor.default.set.js',

        '@bootstrap.js' => 'lib/components/bootstrap/js/bootstrap.min.js',
        '@bootstrap.css' => 'lib/components/bootstrap/css/bootstrap.min.css',
        '@bootstrapTheme.css' => null,    // Undefined value. You can set to: lib/components/bootstrap/css/bootstrap-theme.min.css
    );

    // ==== predefined alias constants  ====
    const jQuery    = '@jQuery';
    const bootstrap = '@bootstrap.js';
    const ckeditor  = '@ckeditor';
    // ==== /predefined alias constants  ====

    /**
     * @var array Resources Registry
     */
    protected static $registry = array();

    /**
     * @var array footer Resources Registry
     */
    protected static $footerRc = array();

    /**
     * @var array header Resources Registry
     */
    protected static $headerRc = array();

    protected static $addedFiles = array();

    protected static $skip_minification = false;

    protected static $cacheOn = false;
    protected static $consolidate = false;
    protected static $isAdmin = false;
    protected static $minify = false;

    /**
     * @var bool header.php executed?
     */
    protected static $headerComplete = false;

    /**
     * @var string $dir Resources cache dir
     */
    protected static $dir = '';

    public static function __init()
    {
        if(defined('COT_HEADER_COMPLETE')) static::$headerComplete = true;

        static::$cacheOn = cot::$cfg['cache'];
        static::$consolidate = (bool)cot::$cfg['headrc_consolidate'];
        static::$dir = cot::$cfg['cache_dir'] . '/static/';
        static::$isAdmin = defined('COT_ADMIN');
        static::$minify = (bool)cot::$cfg['headrc_minify'];

        // Consolidate resources?
        static::$consolidate = static::$cacheOn && static::$consolidate && !static::$isAdmin;
        if (static::$consolidate) {
            if (!file_exists(cot::$cfg['cache_dir'])) mkdir(cot::$cfg['cache_dir'], cot::$cfg['dir_perms']);
            if (!file_exists(static::$dir)) mkdir(static::$dir, cot::$cfg['dir_perms']);
        }
    }

    /**
     * Puts a JS/CSS file into the header resource registry to be consolidated with other
     * such resources and stored in cache.
     *
     * It is recommended to use files instead of embedded code and use this method
     * instead of Resources::addEmbed(). Use this way for any sort of static JavaScript or
     * CSS linking.
     *
     * Do not put any private data in any of resource files - it is not secure. If you really need it,
     * then use direct output instead.
     *
     * @param string $path  Path to a *.js script or *.css stylesheet
     * @param string $type
     * @param int    $order Order priority number
     * @param string $scope Resource scope. Scope is a selector of domain where resource is used. Valid scopes are:
     *	   'global' - global for entire site, will be included everywhere, this is the most static and persistent scope;
     *	   'guest' - for unregistered visitors only;
     *	   'user' - for registered members only;
     *	   'group_123' - for members of a specific group (maingrp), in this example of group with id=123.
     *     It is recommended to use 'global' scope whenever possible because it delivers best caching opportunities.
     * @return bool Returns TRUE normally, FALSE is file was not found
     */
    public static function addFile($path, $type = '', $order = 50, $scope = 'global')
    {
        // header.php executed. Try add file to footer
        if(static::$headerComplete) return Resources::linkFileFooter($path, $type, $order);

        $tmp = explode('?', $path);
        $fileName = $tmp[0];

        if (in_array($fileName, static::$addedFiles)) return false; // Уже добавлено
        if (mb_strpos($fileName, '@' !== 0) && !file_exists($fileName)) return false; // Файл не найден

        if (empty($type)) $type = preg_match('#\.(min\.)?(js|css)$#', mb_strtolower($fileName), $m) ? $m[2] : 'js';

        static::$addedFiles[] = $tmp[0];

        if (static::$consolidate && static::$minify && !static::$skip_minification && mb_strpos($fileName, '.min.') === false
                        && mb_strpos($fileName, '.pack.') === false) {

            if (mb_strpos($fileName, '@') === 0) $fileName = static::$alias[$fileName];

            if($fileName != '') {
                $bname = ($type == 'css') ? str_replace('/', '._.', $fileName) : basename($fileName) . '.min';
                $code = static::minify(file_get_contents($fileName), $type);
                $path = static::$dir . $bname;
                file_put_contents($path, $code);
            }
        }

        if (static::$consolidate && static::$cacheOn && !static::$isAdmin) {
            static::$registry[$type][$scope][$order][] = $path;
        } else {
            static::$registry[$type]['files'][$scope][$order][] = $path;
        }

        foreach (static::additionalFiles($tmp[0]) as $file) {
            static::addFile($file, '', $order, $scope);
        }

        return true;
    }

    protected static function additionalFiles($file)
    {
        $ret = array();

        switch ($file) {
            case '@bootstrap.js':
                $ret[] = '@bootstrap.css';
                $ret[] = '@bootstrapTheme.css';
                break;

            case '@ckeditor':
                $ret[] = '@ckeditorPreset.js';
                break;
        }
<commit_msg>Fix to autoload jQuery migrate lib.<commit_after><?php
defined('COT_CODE') or die('Wrong URL');

/**
 * Cotonti Resource control class
 *
 * @package API - Resources
 * @copyright (c) Cotonti Team
 * @license https://github.com/Cotonti/Cotonti/blob/master/License.txt
 */
class Resources
{
    /**
     * @var array predefined aliases
     */
    protected static $alias = array(
        '@jQuery' => 'lib/components/jquery/jquery.min.js',
    	'@jQuery-migrate' => 'lib/components/jquery/jquery-migrate.min.js',

        '@ckeditor' => 'plugins/ckeditor/lib/ckeditor.js',
        '@ckeditorPreset.js' => 'plugins/ckeditor/presets/ckeditor.default.set.js',

        '@bootstrap.js' => 'lib/components/bootstrap/js/bootstrap.min.js',
        '@bootstrap.css' => 'lib/components/bootstrap/css/bootstrap.min.css',
        '@bootstrapTheme.css' => null,    // Undefined value. You can set to: lib/components/bootstrap/css/bootstrap-theme.min.css
    );

    // ==== predefined alias constants  ====
    const jQuery    = '@jQuery';
    const bootstrap = '@bootstrap.js';
    const ckeditor  = '@ckeditor';
    // ==== /predefined alias constants  ====

    /**
     * @var array Resources Registry
     */
    protected static $registry = array();

    /**
     * @var array footer Resources Registry
     */
    protected static $footerRc = array();

    /**
     * @var array header Resources Registry
     */
    protected static $headerRc = array();

    protected static $addedFiles = array();

    protected static $skip_minification = false;

    protected static $cacheOn = false;
    protected static $consolidate = false;
    protected static $isAdmin = false;
    protected static $minify = false;

    /**
     * @var bool header.php executed?
     */
    protected static $headerComplete = false;

    /**
     * @var string $dir Resources cache dir
     */
    protected static $dir = '';

    public static function __init()
    {
        if(defined('COT_HEADER_COMPLETE')) static::$headerComplete = true;

        static::$cacheOn = cot::$cfg['cache'];
        static::$consolidate = (bool)cot::$cfg['headrc_consolidate'];
        static::$dir = cot::$cfg['cache_dir'] . '/static/';
        static::$isAdmin = defined('COT_ADMIN');
        static::$minify = (bool)cot::$cfg['headrc_minify'];

        // Consolidate resources?
        static::$consolidate = static::$cacheOn && static::$consolidate && !static::$isAdmin;
        if (static::$consolidate) {
            if (!file_exists(cot::$cfg['cache_dir'])) mkdir(cot::$cfg['cache_dir'], cot::$cfg['dir_perms']);
            if (!file_exists(static::$dir)) mkdir(static::$dir, cot::$cfg['dir_perms']);
        }
    }

    /**
     * Puts a JS/CSS file into the header resource registry to be consolidated with other
     * such resources and stored in cache.
     *
     * It is recommended to use files instead of embedded code and use this method
     * instead of Resources::addEmbed(). Use this way for any sort of static JavaScript or
     * CSS linking.
     *
     * Do not put any private data in any of resource files - it is not secure. If you really need it,
     * then use direct output instead.
     *
     * @param string $path  Path to a *.js script or *.css stylesheet
     * @param string $type
     * @param int    $order Order priority number
     * @param string $scope Resource scope. Scope is a selector of domain where resource is used. Valid scopes are:
     *	   'global' - global for entire site, will be included everywhere, this is the most static and persistent scope;
     *	   'guest' - for unregistered visitors only;
     *	   'user' - for registered members only;
     *	   'group_123' - for members of a specific group (maingrp), in this example of group with id=123.
     *     It is recommended to use 'global' scope whenever possible because it delivers best caching opportunities.
     * @return bool Returns TRUE normally, FALSE is file was not found
     */
    public static function addFile($path, $type = '', $order = 50, $scope = 'global')
    {
        // header.php executed. Try add file to footer
        if(static::$headerComplete) return Resources::linkFileFooter($path, $type, $order);

        $tmp = explode('?', $path);
        $fileName = $tmp[0];

        if (in_array($fileName, static::$addedFiles)) return false; // Уже добавлено
        if (mb_strpos($fileName, '@' !== 0) && !file_exists($fileName)) return false; // Файл не найден

        if (empty($type)) $type = preg_match('#\.(min\.)?(js|css)$#', mb_strtolower($fileName), $m) ? $m[2] : 'js';

        static::$addedFiles[] = $tmp[0];

        if (static::$consolidate && static::$minify && !static::$skip_minification && mb_strpos($fileName, '.min.') === false
                        && mb_strpos($fileName, '.pack.') === false) {

            if (mb_strpos($fileName, '@') === 0) $fileName = static::$alias[$fileName];

            if($fileName != '') {
                $bname = ($type == 'css') ? str_replace('/', '._.', $fileName) : basename($fileName) . '.min';
                $code = static::minify(file_get_contents($fileName), $type);
                $path = static::$dir . $bname;
                file_put_contents($path, $code);
            }
        }

        if (static::$consolidate && static::$cacheOn && !static::$isAdmin) {
            static::$registry[$type][$scope][$order][] = $path;
        } else {
            static::$registry[$type]['files'][$scope][$order][] = $path;
        }

        foreach (static::additionalFiles($tmp[0]) as $file) {
            static::addFile($file, '', $order, $scope);
        }

        return true;
    }

    protected static function additionalFiles($file)
    {
        $ret = array();

        switch ($file) {
            case '@bootstrap.js':
                $ret[] = '@bootstrap.css';
                $ret[] = '@bootstrapTheme.css';
                break;

            case '@ckeditor':
                $ret[] = '@ckeditorPreset.js';
                break;

            case '@jQuery':
            	$ret[] = '@jQuery-migrate';
        }

{'fraction_non_alphanumeric': 0.15244359337274166, 'fraction_numerical': 0.0023311964032969776, 'mean_word_length': 2.5308641975308643, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 16, 'https://': 2, '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': '7018059', 'n_tokens_mistral': 3502, 'n_tokens_neox': 3482, 'n_words': 939}