<commit_before><?php

class UpgradeController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $airtime_upgrade_version = '2.5.3';
        
        $this->view->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        
        if (!$this->verifyAuth()) {
            return;
        }
        
        if (!$this->verifyAirtimeVersion()) {
            return;
        }

        $con = Propel::getConnection();
        $con->beginTransaction();
        try {
            //Disable Airtime UI
            //create a temporary maintenance notification file
            //when this file is on the server, zend framework redirects all
            //requests to the maintenance page and sets a 503 response code
            $maintenanceFile = isset($_SERVER['AIRTIME_BASE']) ? $_SERVER['AIRTIME_BASE']."maintenance.txt" : "/tmp/maintenance.txt";
            $file = fopen($maintenanceFile, 'w');
            fclose($file);

            //Begin upgrade
            
            //Update disk_usage value in cc_pref
            $musicDir = CcMusicDirsQuery::create()
                ->filterByType('stor')
                ->filterByExists(true)
                ->findOne();
            $storPath = $musicDir->getDirectory();
            
            $freeSpace = disk_free_space($storPath);
            $totalSpace = disk_total_space($storPath);
            
            Application_Model_Preference::setDiskUsage($totalSpace - $freeSpace);

            //update application.ini
            $iniFile = isset($_SERVER['AIRTIME_BASE']) ? $_SERVER['AIRTIME_BASE']."application.ini" : "/usr/share/airtime/application/configs/application.ini";
            
            $newLines = "resources.frontController.moduleDirectory = APPLICATION_PATH \"/modules\"\n".
                        "resources.frontController.plugins.putHandler = \"Zend_Controller_Plugin_PutHandler\"\n".
                        ";load everything in the modules directory including models\n".
                        "resources.modules[] = \"\"\n";
    
            $currentIniFile = file_get_contents($iniFile);
    
            /* We want to add the new lines after a specific line. So we must find read the file 
             * into an array, find the key to which our desired line belongs, and use the key
             * to split the file in two halves, $beginning and $end.
             * The $newLines will go inbetween $beginning and $end
             */
            $lines = explode("\n", $currentIniFile);

            $key = array_search("resources.layout.layoutPath = APPLICATION_PATH \"/layouts/scripts/\"", $lines);
            if (!$key) {
                throw new Exception('Upgrade to Airtime 2.5.3 FAILED. Could not upgrade application.ini');
            }

            $beginning = implode("\n", array_slice($lines, 0, $key));
            $end = implode("\n", array_slice($lines, $key));
            
            if (!is_writeable($iniFile)) {
                throw new Exception('Upgrade to Airtime 2.5.3 FAILED. Could not upgrade application.ini - Permission denied.');
            }
            $file = new SplFileObject($iniFile, "w");
            $file->fwrite($beginning."\n".$newLines.$end);

            //TODO: clear out the cache

            $con->commit();

            //update system_version in cc_pref and change some columns in cc_files
            $airtimeConf = isset($_SERVER['AIRTIME_CONF']) ? $_SERVER['AIRTIME_CONF'] : "/etc/airtime/airtime.conf";
            $values = parse_ini_file($airtimeConf, true);
            
            $username = $values['database']['dbuser'];
            $password = $values['database']['dbpass'];
            $host = $values['database']['host'];
            $database = $values['database']['dbname'];
            $dir = __DIR__;
            
            passthru("export PGPASSWORD=$password && psql -h $host -U $username -q -f $dir/upgrade_sql/airtime_$airtime_upgrade_version/upgrade.sql $database 2>&1 | grep -v \"will create implicit index\"");
            
            //delete maintenance.txt to give users access back to Airtime
            unlink($maintenanceFile);
<commit_msg>CC-5802: Upgrade application.ini file<commit_after><?php

class UpgradeController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $airtime_upgrade_version = '2.5.3';
        
        $this->view->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        
        if (!$this->verifyAuth()) {
            return;
        }
        
        if (!$this->verifyAirtimeVersion()) {
            return;
        }

        $con = Propel::getConnection();
        $con->beginTransaction();
        try {
            //Disable Airtime UI
            //create a temporary maintenance notification file
            //when this file is on the server, zend framework redirects all
            //requests to the maintenance page and sets a 503 response code
            $maintenanceFile = isset($_SERVER['AIRTIME_BASE']) ? $_SERVER['AIRTIME_BASE']."maintenance.txt" : "/tmp/maintenance.txt";
            $file = fopen($maintenanceFile, 'w');
            fclose($file);

            //Begin upgrade
            
            //Update disk_usage value in cc_pref
            $musicDir = CcMusicDirsQuery::create()
                ->filterByType('stor')
                ->filterByExists(true)
                ->findOne();
            $storPath = $musicDir->getDirectory();
            
            $freeSpace = disk_free_space($storPath);
            $totalSpace = disk_total_space($storPath);
            
            Application_Model_Preference::setDiskUsage($totalSpace - $freeSpace);

            //TODO: clear out the cache

            $con->commit();

            //update system_version in cc_pref and change some columns in cc_files
            $airtimeConf = isset($_SERVER['AIRTIME_CONF']) ? $_SERVER['AIRTIME_CONF'] : "/etc/airtime/airtime.conf";
            $values = parse_ini_file($airtimeConf, true);
            
            $username = $values['database']['dbuser'];
            $password = $values['database']['dbpass'];
            $host = $values['database']['host'];
            $database = $values['database']['dbname'];
            $dir = __DIR__;
            
            passthru("export PGPASSWORD=$password && psql -h $host -U $username -q -f $dir/upgrade_sql/airtime_$airtime_upgrade_version/upgrade.sql $database 2>&1 | grep -v \"will create implicit index\"");
            
            //delete maintenance.txt to give users access back to Airtime
            unlink($maintenanceFile);

{'fraction_non_alphanumeric': 0.12422920739960897, 'fraction_numerical': 0.00406076101669424, 'mean_word_length': 1.9727313366115333, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 34, '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': '5904518', 'n_tokens_mistral': 1781, 'n_tokens_neox': 1734, 'n_words': 413}