<commit_before><?php

if (!class_exists('JSMin'))
    include_once(implode(DIRECTORY_SEPARATOR, array(dirname(__FILE__), 'jsmin.php')));

/*
 *   An earlier experiment contained a real framework for tag
 *   and parser registration. In the end, this turned out
 *   to be much too complex if we just need to support two tags
 *   for two types of resources.
 */
class sacy_FileExtractor{

    function extractFile($tag, $attrdata, $content){
        switch($tag){
            case 'link':
                $fn = 'extract_css_file';
                break;
            case 'script':
                $fn = 'extract_js_file';
                break;
            default: throw new sacy_Exception("Cannot handle tag: $tag");
        }
        return $this->$fn($attrdata, $content);
    }

    private function urlToFile($ref){
        $u = parse_url($ref);
        if ($u === false) return false;
        if (isset($u['host']) || isset($u['scheme']))
            return false;

        // TODO: ignore if query string is set, depending on configuration
        $ref = $u['path'];
        $path = array($_SERVER['DOCUMENT_ROOT']);
        if ($ref[0] != '/')
            $path[] = $_SERVER['PHP_SELF'];
        $path[] = $ref;
        return realpath(implode(DIRECTORY_SEPARATOR, $path));

    }


    private function extract_css_file($attrdata, $content){
        // if any of these conditions are met, this handler will decline
        // handling the tag:
        //
        //  - the tag contains content (invalid markup)
        //  - the tag uses any rel beside 'stylesheet' (valid, but not supported)
        //  - the tag uses any type besides text/css (would maybe still work, but I can't be sure)
        $attrs = sacy_extract_attrs($attrdata);
        if (empty($content) && (strtolower($attrs['rel']) == 'stylesheet') &&
            (!isset($attrs['type']) || strtolower($attrs['type']) == 'text/css')){
            if (!isset($attrs['media']))
                $attrs['media'] = "";

            $path = $this->urlToFile($attrs['href']);
            if ($path === false) return false;

            return array($attrs['media'], $path);
        }
        return false;
    }

    private function extract_js_file($attrdata, $content){
        // don't handle non-empty tags
        if (preg_match('#\S+#', $content)) return false;

        $attrs = sacy_extract_attrs($attrdata);

        if ( ($attrs['type'] == 'text/javascript' ||
                $attrs['type'] == 'application/javascript') &&
             (isset($attrs['src']) && !empty($attrs['src'])) ){

            $path = $this->urlToFile($attrs['src']);
            if ($path === false) return false;
            return array('', $path);
        }
        return false;
    }

}

class sacy_CacheRenderer {
    private $_smarty;

    function __create($smarty){
        $this->_smarty = $smarty;
    }

    function renderFiles($tag, $cat, $files){
        switch($tag){
            case 'link':
                $fn = 'render_css_files';
                break;
            case 'script':
                $fn = 'render_js_files';
                break;
            default: throw new sacy_Exception("Cannot handle tag: $tag");
        }
        return $this->$fn($files, $cat);
    }
<commit_msg>make sure the CacheRenderer actually gets its smarty<commit_after><?php

if (!class_exists('JSMin'))
    include_once(implode(DIRECTORY_SEPARATOR, array(dirname(__FILE__), 'jsmin.php')));

/*
 *   An earlier experiment contained a real framework for tag
 *   and parser registration. In the end, this turned out
 *   to be much too complex if we just need to support two tags
 *   for two types of resources.
 */
class sacy_FileExtractor{

    function extractFile($tag, $attrdata, $content){
        switch($tag){
            case 'link':
                $fn = 'extract_css_file';
                break;
            case 'script':
                $fn = 'extract_js_file';
                break;
            default: throw new sacy_Exception("Cannot handle tag: $tag");
        }
        return $this->$fn($attrdata, $content);
    }

    private function urlToFile($ref){
        $u = parse_url($ref);
        if ($u === false) return false;
        if (isset($u['host']) || isset($u['scheme']))
            return false;

        // TODO: ignore if query string is set, depending on configuration
        $ref = $u['path'];
        $path = array($_SERVER['DOCUMENT_ROOT']);
        if ($ref[0] != '/')
            $path[] = $_SERVER['PHP_SELF'];
        $path[] = $ref;
        return realpath(implode(DIRECTORY_SEPARATOR, $path));

    }


    private function extract_css_file($attrdata, $content){
        // if any of these conditions are met, this handler will decline
        // handling the tag:
        //
        //  - the tag contains content (invalid markup)
        //  - the tag uses any rel beside 'stylesheet' (valid, but not supported)
        //  - the tag uses any type besides text/css (would maybe still work, but I can't be sure)
        $attrs = sacy_extract_attrs($attrdata);
        if (empty($content) && (strtolower($attrs['rel']) == 'stylesheet') &&
            (!isset($attrs['type']) || strtolower($attrs['type']) == 'text/css')){
            if (!isset($attrs['media']))
                $attrs['media'] = "";

            $path = $this->urlToFile($attrs['href']);
            if ($path === false) return false;

            return array($attrs['media'], $path);
        }
        return false;
    }

    private function extract_js_file($attrdata, $content){
        // don't handle non-empty tags
        if (preg_match('#\S+#', $content)) return false;

        $attrs = sacy_extract_attrs($attrdata);

        if ( ($attrs['type'] == 'text/javascript' ||
                $attrs['type'] == 'application/javascript') &&
             (isset($attrs['src']) && !empty($attrs['src'])) ){

            $path = $this->urlToFile($attrs['src']);
            if ($path === false) return false;
            return array('', $path);
        }
        return false;
    }

}

class sacy_CacheRenderer {
    private $_smarty;

    function __construct($smarty){
        $this->_smarty = $smarty;
    }

    function renderFiles($tag, $cat, $files){
        switch($tag){
            case 'link':
                $fn = 'render_css_files';
                break;
            case 'script':
                $fn = 'render_js_files';
                break;
            default: throw new sacy_Exception("Cannot handle tag: $tag");
        }
        return $this->$fn($files, $cat);
    }

{'fraction_non_alphanumeric': 0.14986293024672556, 'fraction_numerical': 0.0003045994517209869, 'mean_word_length': 2.0888993414863593, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '6391793', 'n_tokens_mistral': 1876, 'n_tokens_neox': 1821, 'n_words': 505}