<commit_before># config.rb:
# Very simple config parser. Our config files are sort of cod-PHP.
#
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: <EMAIL>; WWW: http://www.mysociety.org/
#
# $Id: config.rb,v 1.2 2007-10-24 11:39:39 francis Exp $

module MySociety
    module Config

        # Parse config files (written in a sort of cod-php, using
        #     define(OPTION_VALUE_NAME, "value of option");
        # to define individual elements.
        # 
        # Example use:
        #     MySociety::Config.set_file('../conf/general')
        #     opt = MySociety::Config.get('CONFIG_VARIABLE', DEFAULT_VALUE)


        # find_php() -> php_binary
        # Try to locate the PHP binary in various sensible places.
        def Config.find_php()
            path = ENV["PATH"] or '/bin:/usr/bin'
            paths = path.split(':')
            for dir in path.split(':') +
                ['/usr/local/bin', '/usr/bin', '/software/bin', '/opt/bin', '/opt/php/bin']
                for name in ['php4', 'php', 'php4-cgi', 'php-cgi']
                    if File.exists?(File.join(dir, name))
                        return File.join(dir, name)
                    end
                end
            end
            raise "unable to locate PHP binary, needed to read config file"
        end

        # read_config(FILE) ->
        # Read configuration from FILE, which should be the name of a PHP config
        # file.  This is parsed by PHP, and any defines are extracted as config
        # values. "OPTION_" is removed from any names beginning with that.
        attr :php_path
        def Config.read_config(f)
            # We need to find the PHP binary.
            if @php_path.nil?
                @php_path = find_php()
            end

            # Delete everything from the environment other than our special variable to
            # give PHP the config file name. We don't want PHP to pick up other
            # information from our environment and turn into an FCGI server or
            # something.

            # An assignment or a .clone didn't seem to truly copy ENV
            store_environ = {}
            for k,v in ENV
                store_environ[k] = v
            end
            ENV.clear()
            ENV['MYSOCIETY_CONFIG_FILE_PATH'] = f

            buf = nil
            IO.popen(@php_path, "w+") do |child|
                child.print('''<?php
            $b = get_defined_constants();
            require(getenv("MYSOCIETY_CONFIG_FILE_PATH"));
            $a = array_diff_assoc(get_defined_constants(), $b);
            print "start_of_options\n";
            foreach ($a as $k => $v) {
                print preg_replace("/^OPTION_/", "", $k); /* strip off "OPTION_" if there */
                print "\0";
                print $v;
                print "\0";
            }
            ?>''')
                child.close_write()

                # skip any header material
                line = true
                while line
                    line = child.readline()
                    print "line:" + line
                    if line == "start_of_options\n"
                        break
                    else
                        raise "#{@php_path}: #{f}: failed to read options"
                    end
                end<commit_msg>Evil CSS speech bubble to try and make the FOI request page make more sense.<commit_after># config.rb:
# Very simple config parser. Our config files are sort of cod-PHP.
#
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: <EMAIL>; WWW: http://www.mysociety.org/
#
# $Id: config.rb,v 1.3 2007-10-24 19:13:07 francis Exp $

module MySociety
    module Config

        # Parse config files (written in a sort of cod-php, using
        #     define(OPTION_VALUE_NAME, "value of option");
        # to define individual elements.
        # 
        # Example use:
        #     MySociety::Config.set_file('../conf/general')
        #     opt = MySociety::Config.get('CONFIG_VARIABLE', DEFAULT_VALUE)


        # find_php() -> php_binary
        # Try to locate the PHP binary in various sensible places.
        def Config.find_php()
            path = ENV["PATH"] or '/bin:/usr/bin'
            paths = path.split(':')
            for dir in path.split(':') +
                ['/usr/local/bin', '/usr/bin', '/software/bin', '/opt/bin', '/opt/php/bin']
                for name in ['php4', 'php', 'php4-cgi', 'php-cgi']
                    if File.exists?(File.join(dir, name))
                        return File.join(dir, name)
                    end
                end
            end
            raise "unable to locate PHP binary, needed to read config file"
        end

        # read_config(FILE) ->
        # Read configuration from FILE, which should be the name of a PHP config
        # file.  This is parsed by PHP, and any defines are extracted as config
        # values. "OPTION_" is removed from any names beginning with that.
        attr :php_path
        def Config.read_config(f)
            # We need to find the PHP binary.
            if @php_path.nil?
                @php_path = find_php()
            end

            # Delete everything from the environment other than our special variable to
            # give PHP the config file name. We don't want PHP to pick up other
            # information from our environment and turn into an FCGI server or
            # something.

            # An assignment or a .clone didn't seem to truly copy ENV
            store_environ = {}
            for k,v in ENV
                store_environ[k] = v
            end
            ENV.clear()
            ENV['MYSOCIETY_CONFIG_FILE_PATH'] = f

            buf = nil
            IO.popen(@php_path, "w+") do |child|
                child.print('''<?php
            $b = get_defined_constants();
            require(getenv("MYSOCIETY_CONFIG_FILE_PATH"));
            $a = array_diff_assoc(get_defined_constants(), $b);
            print "start_of_options\n";
            foreach ($a as $k => $v) {
                print preg_replace("/^OPTION_/", "", $k); /* strip off "OPTION_" if there */
                print "\0";
                print $v;
                print "\0";
            }
            ?>''')
                child.close_write()

                # skip any header material
                line = true
                while line
                    line = child.readline()
                    if line == "start_of_options\n"
                        break
                    else
                        raise "#{@php_path}: #{f}: failed to read options"
                    end
                end
{'fraction_non_alphanumeric': 0.10125635656595872, 'fraction_numerical': 0.0071791803769069695, 'mean_word_length': 1.4766666666666666, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, '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': '1000544', 'n_tokens_mistral': 1878, 'n_tokens_neox': 1768, 'n_words': 596}