<commit_before><?php
/**
 * Pimcore
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.pimcore.org/license
 *
 * @copyright  Copyright (c) 2009-2010 elements.at New Media Solutions GmbH (http://www.elements.at)
 * @license    http://www.pimcore.org/license     New BSD License
 */


include_once("simple_html_dom.php");

class Pimcore_Tool_Less {


    public static function processHtml ($body) {
        $html = str_get_html($body);

        if(!$html) {
            return $body;
        }

        $styles = $html->find("link[rel=stylesheet/less]");

        $stylesheetContents = array();
        $processedPaths = array();

        foreach ($styles as $style) {

            $media = $style->media;
            if(!$media) {
                $media = "all";
            }

            $source = $style->href;
            $path = "";
            if (is_file(PIMCORE_ASSET_DIRECTORY . $source)) {
                $path = PIMCORE_ASSET_DIRECTORY . $source;
            }
            else if (is_file(PIMCORE_DOCUMENT_ROOT . $source)) {
                $path = PIMCORE_DOCUMENT_ROOT . $source;
            }

            // add the same file only one time
            if(in_array($path, $processedPaths)) {
                continue;
            }

            if (is_file("file://".$path)) {

                $compiledContent = self::compile($path, $source);

                $stylesheetContents[$media] .= $compiledContent . "\n";
                $style->outertext = "";

                $processedPaths[] = $path;
            }
        }

        // put compiled contents into single files, grouped by their media type
        if(count($stylesheetContents) > 0) {
            $head = $html->find("head",0);
            foreach ($stylesheetContents as $media => $content) {
                $stylesheetPath = PIMCORE_TEMPORARY_DIRECTORY."/less_".md5($content).".css";

                if(!is_file($stylesheetPath)) {
                    file_put_contents($stylesheetPath, $content);
                    chmod($stylesheetPath, 0766);
                }

                $head->innertext = $head->innertext . "\n" . '<link rel="stylesheet" media="' . $media . '" type="text/css" href="' . str_replace(PIMCORE_DOCUMENT_ROOT,"",$stylesheetPath) . '" />'."\n";
            }
        }

        $body = $html->save();

        return $body;
    }

    public static function compile ($path, $source = null) {

        $conf = Pimcore_Config::getSystemConfig();
        $compiledContent = "";

        // check if the file is already compiled in the cache
        //$cacheKey = "less_file_" . md5_file($path);
        //if($contents = Pimcore_Model_Cache::load($cacheKey)) {
        //    return $contents;
        //}<commit_msg>refactored less helper, removed combinding and replaced simple_dom_html with regex, added filemtime to filename<commit_after><?php
/**
 * Pimcore
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.pimcore.org/license
 *
 * @copyright  Copyright (c) 2009-2010 elements.at New Media Solutions GmbH (http://www.elements.at)
 * @license    http://www.pimcore.org/license     New BSD License
 */


include_once("simple_html_dom.php");

class Pimcore_Tool_Less {


    public static function processHtml($body) {

        $processedPaths = array();

        preg_match_all("@\<link[^>]*(rel=\"stylesheet/less\")[^>]*\>@msUi", $body, $matches);

        if(is_array($matches)) {
            foreach ($matches[0] as $tag) {
                preg_match("/href=\"([^\"]+)*\"/", $tag, $href);
                if(array_key_exists(1, $href) && !empty($href[1])) {
                    $source = $href[1];

                    if (is_file(PIMCORE_ASSET_DIRECTORY . $source)) {
                        $path = PIMCORE_ASSET_DIRECTORY . $source;
                    }
                    else if (is_file(PIMCORE_DOCUMENT_ROOT . $source)) {
                        $path = PIMCORE_DOCUMENT_ROOT . $source;
                    }

                    // add the same file only one time
                    if(in_array($path, $processedPaths)) {
                        continue;
                    }

                    $newFile = PIMCORE_TEMPORARY_DIRECTORY . "/less___" . Pimcore_File::getValidFilename(str_replace(".less", "", $source)) . "-" . filemtime($path) . ".css";
                    if(!is_file($newFile)) {
                        $compiledContent = self::compile($path, $source);
                        file_put_contents($newFile, $compiledContent);
                    }

                    $body = str_replace($tag,
                        str_replace("stylesheet/less", "stylesheet",
                            str_replace($source,
                                str_replace(PIMCORE_DOCUMENT_ROOT, "", $newFile)
                            ,$tag))
                    , $body);
                }
            }
        }

        return $body;
    }

    /**
     * old version incl. combining and using simple_dom_html
     *
     *
     *public static function processHtmlLEGACY ($body) {
        $html = str_get_html($body);

        if(!$html) {
            return $body;
        }

        $styles = $html->find("link[rel=stylesheet/less]");

        $stylesheetContents = array();
        $processedPaths = array();

        foreach ($styles as $style) {

            $media = $style->media;
            if(!$media) {
                $media = "all";
            }

            $source = $style->href;
            $path = "";
            if (is_file(PIMCORE_ASSET_DIRECTORY . $source)) {
                $path = PIMCORE_ASSET_DIRECTORY . $source;
            }
            else if (is_file(PIMCORE_DOCUMENT_ROOT . $source)) {
                $path = PIMCORE_DOCUMENT_ROOT . $source;
            }

            // add the same file only one time
            if(in_array($path, $processedPaths)) {
                continue;
            }

            if (is_file("file:/".$path)) {

                $compiledContent = self::compile($path, $source);

                $stylesheetContents[$media] .= $compiledContent . "\n";
                $style->outertext = "";

                $processedPaths[] = $path;
            }
        }

        // put compiled contents into single files, grouped by their media type
        if(count($stylesheetContents) > 0) {
            $head = $html->find("head",0);
            foreach ($stylesheetContents as $media => $content) {
                $stylesheetPath = PIMCORE_TEMPORARY_DIRECTORY."/less_".md5($content).".css";

                if(!is_file($stylesheetPath)) {
                    file_put_contents($stylesheetPath, $content);
                    chmod($stylesheetPath, 0766);
                }

                $head->innertext = $head->innertext . "\n" . '<link rel="stylesheet" media="' . $media . '" type="text/css" href="' . str_replace(PIMCORE_DOCUMENT_ROOT,"",$stylesheetPath) . '" />'."\n";
            }
        }

        $body = $html->save();

        return $body;
    }*/

    public static function compile ($path, $source = null) {

        $conf = Pimcore_Config::getSystemConfig();
        $compiledContent = "";

        // check if the file is already compiled in the cache
        //$cacheKey = "less_file_" . md5_file($path);
        //if($contents = Pimcore_Model_Cache::load($cacheKey)) {
        //    return $contents;
        //}
{'fraction_non_alphanumeric': 0.13607843137254902, 'fraction_numerical': 0.004705882352941176, 'mean_word_length': 1.7179396092362345, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '502231', 'n_tokens_mistral': 2252, 'n_tokens_neox': 2080, 'n_words': 455}