<commit_before><?php
/*
 *  Copyright (c) 2012-2015  <NAME> <<EMAIL>>,
 *                           <NAME> <<EMAIL>>,
 *                           AIP E-Science (www.aip.de)
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @class   Daiquiri_Config Config.php
 * @brief   Daiquiri config singleton storing all global configurations
 * 
 * This globally accessible singleton stores all the information about the
 * Daiquiri applications configuration. It is read from the database and also
 * incorporates the Zend configuration stuff.
 *
 */
class Daiquiri_Config extends Daiquiri_Model_Singleton {

    /**
     * Daquiri configuration object, parsed from database
     * @var Zend_Config_Ini
     */
    protected $_config;

    /**
     * Zend configuration object, parsed from application.ini
     * @var Zend_Config_Ini
     */
    protected $_application;

    /**
     * Constructor. Empty.
     */
    public function __construct() {

    }

    public function setApplication($application = null) {
        if ($application === null) {
            $this->_application = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV);
        } else {
            $this->_application = new Zend_Config($application, true);
        } 
    }

    public function setConfig($config = null) {
        if ($config === null) {
            // init the databases resource
            $resource = new Daiquiri_Model_Resource_Table();
            $resource->setTablename('Core_Config');

            $rows = $resource->fetchRows();

            if (empty($rows)) {
                return false;
            }
            
            $config = array();
            foreach ($rows as $row) {
                $keys = explode('.', $row['key']);
                $this->_buildConfig($config, $keys, $row['value']);
            }
        }

        $this->_config = new Zend_Config($config, true);

        return true;
    }

    /**
     * Recursive function to build the config array.
     * @param array $a
     * @param array $keys
     * @param string $value
     */
    private function _buildConfig(array &$config, array &$keys, $value) {
        $key = array_shift($keys);
        if (empty($keys)) {
            $config[$key] = $value;
        } else {
            if (!array_key_exists($key, $config)) {
                $config[$key] = array();
            }
            if (is_array($config[$key])) {
                $this->_buildConfig($config[$key], $keys, $value);
            } else {
                throw new Exception('Bad config array: ' . print_r($key, true));
            }
        }
    }

    /**
     * Magic method to avoid calling getConfig().
     * @param string $key
     * @return Zend_Config_Ini
     */
    public function __get($key) {
        return $this->getConfig()->$key;
    }

    public function getConfig() {
        if (empty($this->_config)) {
            throw new Exception('Empty config');
        }
        return $this->_config;
    }

    public function getApplication() {
        if (empty($this->_application)) {
            throw new Exception('Empty application config');
        }
        return $this->_application;
    }

    /**
     * Returns the site url
     * @return string 
     */
    public function getSiteUrl() {
        return $this->getHost() . Zend_Controller_Front::getInstance()->getBaseUrl();
        ;
    }

    /** Returns the host url (without base url)
     * @return string
     */
    public function getHost() {
        $front = Zend_Controller_Front::getInstance();
        $server = $front->getRequest()->getServer();

        if (isset($server['HTTPS'])) {
            $host = 'https://';
        } else {
            $host = 'http://';
        }
        $host .= $server["SERVER_NAME"];
        if ($server["SERVER_PORT"] != "80") {
            $host .= ':' . $server["SERVER_PORT"];
        }

        return $host;
    }

    /** Returns the base url
     * @return string
     */
    public function getBaseUrl() {
        $front = Zend_Controller_Front::getInstance();
        return $front->getBaseUrl();
    }

    /** Returns query parameters correctly for repeated keys with missing [],
     * multiple occurence of one key results in array of its values
     * Example: /uws/query&PHASE=a&PHASE=b will give array('PHASE'=>array('a','b')),
     * (Normally, PHP would overwrite values for repeated keys with the last value,
     * and would require square brackets to indicate an array, e.g. PHASE[]=a&PHASE[]=b.)
     * @return array
     */
    public function getMultiQuery() {
        $front = Zend_Controller_Front::getInstance();
        $requestUri = $front->getRequest()->getRequestUri();

        $queryparams = array();

        // strip path information, get query-part from Uri
        if (false !== ($pos = strpos($requestUri, '?'))) {
            $query = substr($requestUri, $pos + 1);

            // parse values and add to array, if key is repeated
            foreach (explode('&', $query) as $param) {
                $keyvalue = explode('=', $param);
                $key = urldecode($keyvalue[0]);
                $value = urldecode($keyvalue[1]);<commit_msg>Take care of missing values when parsing query parameters<commit_after><?php
/*
 *  Copyright (c) 2012-2015  <NAME> <<EMAIL>>,
 *                           <NAME> <<EMAIL>>,
 *                           AIP E-Science (www.aip.de)
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @class   Daiquiri_Config Config.php
 * @brief   Daiquiri config singleton storing all global configurations
 * 
 * This globally accessible singleton stores all the information about the
 * Daiquiri applications configuration. It is read from the database and also
 * incorporates the Zend configuration stuff.
 *
 */
class Daiquiri_Config extends Daiquiri_Model_Singleton {

    /**
     * Daquiri configuration object, parsed from database
     * @var Zend_Config_Ini
     */
    protected $_config;

    /**
     * Zend configuration object, parsed from application.ini
     * @var Zend_Config_Ini
     */
    protected $_application;

    /**
     * Constructor. Empty.
     */
    public function __construct() {

    }

    public function setApplication($application = null) {
        if ($application === null) {
            $this->_application = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV);
        } else {
            $this->_application = new Zend_Config($application, true);
        } 
    }

    public function setConfig($config = null) {
        if ($config === null) {
            // init the databases resource
            $resource = new Daiquiri_Model_Resource_Table();
            $resource->setTablename('Core_Config');

            $rows = $resource->fetchRows();

            if (empty($rows)) {
                return false;
            }
            
            $config = array();
            foreach ($rows as $row) {
                $keys = explode('.', $row['key']);
                $this->_buildConfig($config, $keys, $row['value']);
            }
        }

        $this->_config = new Zend_Config($config, true);

        return true;
    }

    /**
     * Recursive function to build the config array.
     * @param array $a
     * @param array $keys
     * @param string $value
     */
    private function _buildConfig(array &$config, array &$keys, $value) {
        $key = array_shift($keys);
        if (empty($keys)) {
            $config[$key] = $value;
        } else {
            if (!array_key_exists($key, $config)) {
                $config[$key] = array();
            }
            if (is_array($config[$key])) {
                $this->_buildConfig($config[$key], $keys, $value);
            } else {
                throw new Exception('Bad config array: ' . print_r($key, true));
            }
        }
    }

    /**
     * Magic method to avoid calling getConfig().
     * @param string $key
     * @return Zend_Config_Ini
     */
    public function __get($key) {
        return $this->getConfig()->$key;
    }

    public function getConfig() {
        if (empty($this->_config)) {
            throw new Exception('Empty config');
        }
        return $this->_config;
    }

    public function getApplication() {
        if (empty($this->_application)) {
            throw new Exception('Empty application config');
        }
        return $this->_application;
    }

    /**
     * Returns the site url
     * @return string 
     */
    public function getSiteUrl() {
        return $this->getHost() . Zend_Controller_Front::getInstance()->getBaseUrl();
        ;
    }

    /** Returns the host url (without base url)
     * @return string
     */
    public function getHost() {
        $front = Zend_Controller_Front::getInstance();
        $server = $front->getRequest()->getServer();

        if (isset($server['HTTPS'])) {
            $host = 'https://';
        } else {
            $host = 'http://';
        }
        $host .= $server["SERVER_NAME"];
        if ($server["SERVER_PORT"] != "80") {
            $host .= ':' . $server["SERVER_PORT"];
        }

        return $host;
    }

    /** Returns the base url
     * @return string
     */
    public function getBaseUrl() {
        $front = Zend_Controller_Front::getInstance();
        return $front->getBaseUrl();
    }

    /** Returns query parameters correctly for repeated keys with missing [],
     * multiple occurence of one key results in array of its values
     * Example: /uws/query&PHASE=a&PHASE=b will give array('PHASE'=>array('a','b')),
     * (Normally, PHP would overwrite values for repeated keys with the last value,
     * and would require square brackets to indicate an array, e.g. PHASE[]=a&PHASE[]=b.)
     * @return array
     */
    public function getMultiQuery() {
        $front = Zend_Controller_Front::getInstance();
        $requestUri = $front->getRequest()->getRequestUri();

        $queryparams = array();

        // strip path information, get query-part from Uri
        if (false !== ($pos = strpos($requestUri, '?'))) {
            $query = substr($requestUri, $pos + 1);

            // parse values and add to array, if key is repeated
            foreach (explode('&', $query) as $param) {
                $keyvalue = explode('=', $param);
                $key = urldecode($keyvalue[0]);

                if (isset($keyvalue[1])) {
                    $value = urldecode($keyvalue[1]);
                } else {
                    $value = "";
                }
{'fraction_non_alphanumeric': 0.12474645030425964, 'fraction_numerical': 0.0024509803921568627, 'mean_word_length': 2.079906298802707, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 59, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1654393', 'n_tokens_mistral': 3283, 'n_tokens_neox': 3130, 'n_words': 961}