2009.09.09 $script_name = 'upgrade_20090909.php'; //// //// sub-functions //// function deleteDirectory($dir) { if (!file_exists($dir)) return true; if (!is_dir($dir)) return unlink($dir); foreach (scandir($dir) as $item) { if ($item == '.' || $item == '..') continue; if (!deleteDirectory($dir.DIRECTORY_SEPARATOR.$item)) return false; } return rmdir($dir); } //// //// MAIN //// print "\n\n---------ENSEMBLE UPDATE: 9 SEPTEMBER 2009---------\n"; print "---------------------------------------------------\n\n"; // initialize variables and lists for the upgrade $ensroot = '/var/www/html/ensemble'; $svn_public_path = 'https://atonal.ucdavis.edu/repos/public'; $svn_formprocess = $svn_public_path.'/ensemble/release/v1/form_processing'; $sigen_varname = "SUBJECT_ID_GENERATOR"; $sigen_default = "increment"; // the following variables store names for the new and old form handlers, // which form handlers will be replaced and with what they will be replaced $fh_replacements = array('form_subject_id.php' => 'form_subject_register.php', 'form_subject_id_w_gender_info.php' => 'form_subject_register.php', 'form_subject_id_with_validation.php' => 'form_subject_register_wvalidation.php'); $idgen_handlers = array('subidgen_dl.php','subidgen_dl_html.php','subidgen_dl_continued_html.php', 'subidgen_increment.php','subidgen_increment_html.php','subidgen_increment_continued_html.php'); $oldformhandlers = array_unique(array_keys($fh_replacements)); $newformhandlers = array_unique(array_values($fh_replacements)); $handlers_tocopy = array_merge($newformhandlers,$idgen_handlers); // // give SVN warning // print "WARNING!!! WARNING!!! WARNING!!!\n"; print "if you are maintaining your ensemble web root as an active checked-out copy of the\n"; print "svn repository, please go to your web root and run 'svn update' before continuing!\n\n"; $continue=FALSE; while (!$continue) { print "continue (y/n)? "; $input = trim(fgets(STDIN,256)); if (ereg("^[yY]$",$input)) { $continue=TRUE; } elseif (ereg("^[nN]$",$input)) { exit; } } // // // get the root directory of the ensemble installation to upgrade // print "\n\nENSEMBLE INSTALLATION ROOT DIRECTORY\n"; print "\nWhere is your ensemble installation located?\n"; printf("[%s]: ",$ensroot); $continue=0; while (!$continue) { $input = trim(fgets(STDIN,256)); if ($input == NULL) { $continue=1; } else { if (is_dir($input)) { $ensroot = $input; $continue=1; } else { print "\ndirectory $input not found\n"; } } } // remove trailing slash from $ensroot $slash_pos = strrpos($ensroot,"/"); if ($slash_pos == (strlen($ensroot)-1)) { $ensroot = substr($ensroot,0,strlen($ensroot)-1); } // // check that the new form handlers exist on disk // print "\n\nCHECKING FOR UPDATED FORM HANDLERS\n\n"; $fpdir = $ensroot.'/form_processing/'; if (!is_dir($fpdir)) { print "$fpdir not found!!\n"; exit; } $notfound=array(); $nfstubs =array(); $nnfs = count($handlers_tocopy); print "$nnfs forms to check for\n"; for ($ii=0; $ii < $nnfs; $ii++) { print "handler $ii: " . $handlers_tocopy[$ii] . "\n"; $lpath = $fpdir . $handlers_tocopy[$ii]; if (!is_file($lpath)) { print "$lpath not found\n"; array_push($notfound,$lpath); array_push($nfstubs,$handlers_tocopy[$ii]); } else { print "$lpath found\n"; } } // if all files are not there, copy from SVN $nnf = count($notfound); if ($nnf) { print "$nnf / $nnfs forms not found on disk. Copying them from svn...\n"; $tmpdir = $ensroot.'/temp/'; print "checking out the public svn repository: " . $svn_formprocess . "\n"; if (exec("svn co $svn_formprocess $tmpdir")) { for ($ii=0;$ii<$nnf;$ii++) { $cfrom = $tmpdir.$nfstubs[$ii]; $cto = $notfound[$ii]; print "copying $cfrom to $cto\n"; if (!copy($cfrom,$cto)) { print "ERROR copying $cfrom to $cto\n\n"; exit; } // if !copy($cfrom } // for ($ii=0 } else { print "ERROR checking out svn repository\n\n"; exit; } // if exec(printf("svn co print "done copying scripts ... deleting temporary svn repository\n"; if (!deleteDirectory($tmpdir)) { print "ERROR deleting $tmpdir\n\n"; exit; } } // if (!$all // // edit the variables_config.php script // $vcpath = $ensroot.'/include/variables_config.php'; print "\n\nEDITING $vcpath: adding $sigen_varname\n\n"; if (!is_file($vcpath)) { print "ERROR finding $vcpath\n\n"; } else { // make a backup first if (!copy($vcpath,$vcpath.".20090909.bak")) { print "ERROR making a backup of $vcpath\n\n"; exit; } $contents = file_get_contents($vcpath); print "$vcpath contains " . strlen($contents) . " characters\n"; if (!strstr($contents,$sigen_varname)) { // $sigen_varname not found in $contents // set $sigen_varname // find all id generation form handlers $dh = @opendir($fpdir) or die("Unable to open $fpdir"); $idgen_scripts = array(); print "\n\nthe following scripts are available for subject id generation:\n"; while (($file = readdir($dh)) !== false) { if (preg_match('/^subidgen_([a-zA-Z0-9]+)\.php$/',$file,$matches) > 0) { if (!in_array($matches[1],$idgen_scripts)) { array_push($idgen_scripts,$matches[1]); print "\t" . $matches[1] . "\n"; } } } closedir($dh); // get the filename of the subject id generation script to use $continue=0; while (!$continue) { print "\n\nenter the script you would like to use to generate your subject ids"; print "\n\t(type 'l' to list the available scripts)\n"; printf("[%s]: ",$sigen_default); $input=trim(fgets(STDIN,256)); if (ereg("^[Ll]$",$input)) { print "\n\nthe following scripts are available for subject id generation:\n"; foreach ($idgen_scripts as $sname) { print "\t$sname\n"; } } elseif ($input == NULL) { $input = $sigen_default; $continue = 1; } else { if (in_array($input,$idgen_scripts)) { $continue = 1; } // if in_array($input } // if ereg("^[Ll } // while (!$continue print "writing ".$input." to $vcpath\n;"; $rstr = sprintf("\n$%s='%s';\n?>",$sigen_varname,$input); $contents = preg_replace('/\?>/',$rstr,$contents); print "updated $vcpath contains " . strlen($contents) . " characters\n"; if (!file_put_contents($vcpath,$contents)) { print "ERROR writing to $vcpath\n\n"; exit; } print "done editing $vcpath\n"; } else { print "\$$sigen_varname already found in $vcpath\n"; } } // // update the database // print "\n\nUPDATING THE DATABASE\n\n"; $varpath = $ensroot.'/include/variables.php'; include($varpath); // get username with ALTER TABLE privileges print "Please provide a mysql username with ALTER TABLE privileges\n"; $continue=0; while (!$continue) { print "username: "; $muser=trim(fgets(STDIN,256)); if ($muser != NULL) { $continue=1; } } // get password print "Please provide the mysql password for $muser\n"; $continue=0; while (!$continue) { print "password: "; $mpass=trim(fgets(STDIN,256)); if ($mpass != NULL) { $continue=1; } } // get username with ALTER TABLE privileges print "Please provide the hostname of your mysql server:\n"; $continue=0; while (!$continue) { sprintf("[%]: ",$MYSQL_HOSTNAME); $mhost=trim(fgets(STDIN,256)); if ($mhost != NULL) { $continue=1; } else { $mhost = $MYSQL_HOSTNAME; } } // connect to the server, use the `information_schema` table $subject = mysql_connect($mhost,$muser,$mpass) or trigger_error(mysql_error(),E_USER_ERROR); // BACKUP THE DATABASE print "BACKING UP THE " . $DATABASE_NAME . " DATABASE\n"; $logfileparts = pathinfo($LOG_FILE); print "log file directory is " . $logfileparts['dirname'] . "\n"; $backuppath = $logfileparts['dirname'] . "/backup_20090909.sql"; $backupcmd = "mysqldump -Qau $muser -p$mpass -h $mhost " . $DATABASE_NAME . " > $backuppath"; exec($backupcmd,$o,$bcstatus); if ($bcstatus) { print "could not backup database to $backuppath\n"; exit; } print "database backed up to $backuppath\n"; // check to see if there are any null columns in the current $DATABASE_NAME.experiment_x_form.form_handler print "using database " . $DATABASE_NAME . "\n"; mysql_select_db($DATABASE_NAME,$subject); $selectstr = "SELECT * FROM experiment_x_form WHERE form_handler IS NULL;"; $colquery = mysql_query($selectstr,$subject) or die(mysql_error()); if (mysql_num_rows($colquery)) { print "ERROR: " . mysql_num_rows($colquery,$subject) . " rows with NULL form handlers in experiment_x_form\n"; exit; } // iterate over old form handlers, replace with new form handlers foreach ($oldformhandlers as $ofh) { // get new form handler $nfh = $fh_replacements[$ofh]; print "replacing $ofh with $nfh\n"; // get enum values from $MYSQL_HOSTNAME.$DATABASE_NAME.experiment_x_form.form_handler mysql_select_db('information_schema',$subject); $selectstr = "SELECT column_type FROM columns WHERE table_schema='$DATABASE_NAME' "; $selectstr .= "AND table_name='experiment_x_form' AND column_name='form_handler';"; $columnquery = mysql_query($selectstr, $subject) or die(mysql_error()."\n\n"); $columnrow = mysql_fetch_assoc($columnquery); $ctyperow = $columnrow['column_type']; if (!ereg('^(enum\()(.*)(\))$',$ctyperow,$parts)) { print "ERROR: can not parse column type\n\n"; exit; } // save into array // delete each of $oldformhandlers from the array $handlers = explode(",",$parts[2]); $newhandlers = array_diff($handlers,array("'".$ofh."'")); $newhandlers = array_merge($newhandlers,array_diff(array("'".$nfh."'"),$newhandlers)); // alter table $newctypes = $parts[1] . implode(",",$newhandlers) . $parts[3]; mysql_select_db($DATABASE_NAME,$subject); $updatestr = "ALTER TABLE experiment_x_form MODIFY form_handler $newctypes;"; //print "alter table string: $updatestr\n"; mysql_query($updatestr,$subject) or die(mysql_error()."\n\n"); // replace NULL with $nfh $updatestr = "UPDATE experiment_x_form SET form_handler='$nfh' WHERE form_handler = '';"; //print "update string: $updatestr\n"; mysql_query($updatestr,$subject) or die(mysql_error()."\n\n"); } print "DONE UPDATING!\n\n"; print "Please test your installation by verifying that old experiments still run\n"; ?>