<commit_before><?php
/*
* phpCan - http://idc.anavallasuiza.com/
*
* phpCan is released under the GNU Affero GPL version 3
*
* More information at license.txt
*/

namespace ANS\PHPCan\I18n;

defined('ANS') or die();

class Gettext_builder
{
    private $Debug;

    /**
     * public function __construct ([string $autoglobal])
     *
     * return none
     */
    public function __construct ($autoglobal = '')
    {
        global $Debug;

        $this->Debug = $Debug;

        if ($autoglobal) {
            global $Config;

            $Config->config['autoglobal'][] = $autoglobal;
        }
    }

    public function getEntries ($folders, $po_files)
    {
        $return = array(
            'headers' => array(
                'Project-Id-Version' => '',
                'Report-Msgid-Bugs-To' => '',
                'POT-Creation-Date' => '',
                'PO-Revision-Date' => '',
                'Last-Translator' => '',
                'Language-Team' => '',
                'Content-Type' => '',
                'Content-Transfer-Encoding' => '',
                'Plural-Forms' => '',
            )
        );

        $return['entries'] = $this->scan($folders);

        if ($po_files) {
            foreach ($po_files as $po_file) {
                $po_file = filePath($po_file);

                if (is_file($po_file)) {
                    $po_entries = $this->parsePo($po_file);

                    if ($po_entries) {
                        foreach ($return['entries'] as $key => &$entry) {
                            if ($po_entries['entries'][$key]) {
                                foreach ($po_entries['entries'][$key] as $po_key => $po_entry) {
                                    if (!$entry[$po_key]) {
                                        $entry[$po_key] = $po_entry;
                                    }
                                }
                            }
                        }

                        $return['headers'] = $po_entries['headers'];
                    }
                }
            }
        }

        return $return;
    }

    private function scan ($folders)
    {
        if (!is_array($folders)) {
            $folders = array($folders);
        }

        $File = new \ANS\PHPCan\Files\File;

        $entries = array();

        foreach ($folders as $folder) {
            $folder = filePath($folder);

            if (is_file($folder) && in_array(strtolower(pathinfo($folder, PATHINFO_EXTENSION)), array('php', 'js'))) {
                $entries = arrayMergeReplaceRecursive($entries, $this->extractStrings($folder));
            } else if (is_dir($folder)) {
                $entries = arrayMergeReplaceRecursive($entries, $this->scan($File->listFolder($folder, '*', -1)));
            }
        }

        return $entries;
    }

    public function extractStrings ($filename)
    {<commit_msg>Improved gettext parser<commit_after><?php
/*
* phpCan - http://idc.anavallasuiza.com/
*
* phpCan is released under the GNU Affero GPL version 3
*
* More information at license.txt
*/

namespace ANS\PHPCan\I18n;

defined('ANS') or die();

class Gettext_builder
{
    private $Debug;

    /**
     * public function __construct ([string $autoglobal])
     *
     * return none
     */
    public function __construct ($autoglobal = '')
    {
        global $Debug;

        $this->Debug = $Debug;

        if ($autoglobal) {
            global $Config;

            $Config->config['autoglobal'][] = $autoglobal;
        }
    }

    public function getEntries ($folders, $po_files)
    {
        $return = array(
            'headers' => array(
                'Project-Id-Version' => '',
                'Report-Msgid-Bugs-To' => '',
                'POT-Creation-Date' => '',
                'PO-Revision-Date' => '',
                'Last-Translator' => '',
                'Language-Team' => '',
                'Content-Type' => '',
                'Content-Transfer-Encoding' => '',
                'Plural-Forms' => '',
            )
        );

        $return['entries'] = $this->scan($folders);

        if (!$po_files) {
            return $return;
        }

        foreach ($po_files as $po_file) {
            $po_file = filePath($po_file);

            if (!is_file($po_file)) {
                continue;
            }

            $po_entries = $this->parsePo($po_file);

            if (!$po_entries) {
                continue;
            }

            foreach ($return['entries'] as $key => &$entry) {
                if (!$po_entries['entries'][$key]) {
                    continue;
                }

                foreach ($po_entries['entries'][$key] as $po_key => $po_entry) {
                    if (!$entry[$po_key]) {
                        $entry[$po_key] = $po_entry;
                    }
                }

                $return['headers'] = $po_entries['headers'];
            }
        }
        
        return $return;
    }

    private function scan ($folders)
    {
        if (!is_array($folders)) {
            $folders = array($folders);
        }

        $File = new \ANS\PHPCan\Files\File;

        $finfo = finfo_open(FILEINFO_MIME);
        $entries = array();

        foreach ($folders as $folder) {
            $folder = filePath($folder);

            if (is_file($folder) && (substr(finfo_file($finfo, $folder), 0, 4) === 'text')) {
                $entries = arrayMergeReplaceRecursive($entries, $this->extractStrings($folder));
            } else if (is_dir($folder)) {
                $entries = arrayMergeReplaceRecursive($entries, $this->scan($File->listFolder($folder, '*', -1)));
            }
        }

        return $entries;
    }

    public function extractStrings ($filename)
    {
{'fraction_non_alphanumeric': 0.14885828830399164, 'fraction_numerical': 0.001743071291615827, 'mean_word_length': 1.4385890352741182, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1208157', 'n_tokens_mistral': 1698, 'n_tokens_neox': 1583, 'n_words': 294}