Location: PHPKode > projects > Active Agenda > active_agenda-0.8.2-beta/s2a/s2a-apply-table-changes.php
<?php
/**
 *  Utility to apply table changes generated by the s2a generator.
 * 
 *  PHP version 4
 *
 *
 *  LICENSE NOTE:
 *
 *  Copyright  2003-2007 Active Agenda Inc., All Rights Reserved.
 *
 *  Unless explicitly acquired and licensed from Licensor under a "commercial license",
 *  the contents of this file are subject to the Reciprocal Public License ("RPL")
 *  Version 1.4, or subsequent versions as allowed by the RPL,and You may not copy
 *  or use this file in either source code or executable form, except in compliance
 *  with the terms and conditions of the RPL. You may obtain a copy of the RPL from
 *  Active Agenda Inc. at http://www.activeagenda.net/license.
 *
 *  All software distributed under the Licenses is provided strictly on an "AS IS"
 *  basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, AND ACTIVE AGENDA
 *  INC. HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY 
 *  WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT,
 *  OR NON-INFRINGEMENT. See the Licenses for specific language governing rights and
 *  limitations under the Licenses.
 *
 *
 * @author         Mattias Thorslund <hide@address.com>
 * @copyright      2003-2007 Active Agenda Inc.
 * @license        http://www.activeagenda.net/license  RPL 1.4
 * @version        SVN: $Revision: 499 $
 * @last-modified  SVN: $Date: 2007-02-16 13:43:40 -0800 (Fri, 16 Feb 2007) $
 */



if(defined('APPLY_TABLE_CHANGES_INCLUDED') && APPLY_TABLE_CHANGES_INCLUDED){
    $module_id = '*';
    $project = $Project;
    print "This is the s2a-apply-table-changes.php utility.\n";
} else {

    $module_id = $_SERVER[argv][1]; //optional 
    $project = $_SERVER[argv][2];

}

if(empty($module_id)){
   $module_id = '*';
}
if(empty($project)){
   $project = 'active_agenda';
}

//assumes we're in the 's2a' folder 
$site_folder = realpath(dirname($_SERVER['SCRIPT_FILENAME']).'');
$site_folder .= '/'.$project;

//includes
$config_file = $site_folder . '/config.php';
if(!file_exists($config_file)){
    print "Config file not found at $config_file\n";
    exit;
}
$gen_config_file = $site_folder . '/gen-config.php';
if(!file_exists($gen_config_file)){
    print "Config file not found at $gen_config_file\n";
    exit;
}

//get settings
include_once $config_file;
include_once $gen_config_file;
if(!(defined('APPLY_TABLE_CHANGES_INCLUDED') && APPLY_TABLE_CHANGES_INCLUDED)){
    set_include_path(PEAR_PATH . PATH_SEPARATOR . get_include_path());
}
require_once PEAR_PATH . '/DB.php' ;  //PEAR DB class
include_once INCLUDE_PATH . '/general_util.php';
include_once INCLUDE_PATH . '/parse_util.php';

$debug_prefix = 's2a-apply-table-changes:';

global $dbh;
$dbh = DB::connect(GEN_DB_DSN); //root user privileges
dbErrorCheck($dbh);

$sql_change_files = glob(GEN_LOG_PATH.'/'.$module_id.'_dbChanges.sql');

foreach($sql_change_files as $sql_change_file_path){
    $sql_file_content = file_get_contents($sql_change_file_path);
    $sql_change_file = basename($sql_change_file_path);
    print "\n$debug_prefix Table changes from $sql_change_file:\n\n";
    print $sql_file_content;

    if(prompt("Apply these changes?")){
        print "$debug_prefix Applying changes...\n";
        $sqls = explode("-- statement separator --", $sql_file_content);
        $success = true;
        foreach($sqls as $sql){

            $sql = trim($sql);

            //strip out comment lines
            $sql_lines = explode("\n", $sql);
            $sql = '';
            foreach($sql_lines as $sql_line){
                if('--' != substr($sql_line, 0,2)){
                    $sql_line = trim($sql_line);
                    if('' != $sql_line){
                        $sql .= $sql_line."\n";
                    }
                }
            }

            if('' != $sql){
                //since the query applies a table change, this isn't reversible: CheckSQL will apply it
                if(CheckSQL($sql, false)){
                    /*
                    $r = $dbh->query($sql);
                    dbErrorCheck($r);
                    */
                } else {
                    $success = false;
                }
            }
        }
        if($success){
            unlink($sql_change_file_path);
        }
    } else {
        print "$debug_prefix Skipping changes.\n";
    }
}

print "$debug_prefix Finished.\n\n";


?>
Return current item: Active Agenda