Beispiel #1
0
/**
* Converts a file
*
* @param    string
* @param    bool
*/
function convertFile($file, $verbose, $quiet, $update, $backup, $warnings, $seperator = '#')
{
    $conv = new MySQLConverterTool_Converter();
    $ret = $conv->convertFile($file);
    $status = getConversionStatus($ret);
    if ($quiet) {
        printSeperator(sprintf('[ %-7s ] %s', $status, $file), $seperator);
    }
    if ($verbose) {
        printConversionHeader($ret, $status, $file);
    }
    if ($backup) {
        $ffile = $file . '.org';
        if (file_exists($ffile) && !unlink($ffile)) {
            printf("Error:\n");
            printf("Cannot unlink old backup file '%s'. Check the file permissions.\n\n", $ffile);
            return;
        }
        if (!rename($file, $ffile)) {
            printf("Error:\n");
            printf("Cannot rename '%s' to %s'. Check the file permissions.\n\n", $file, $ffile);
            return;
        }
        if ($verbose) {
            printf("Backup created.\n", $ffile);
        }
    }
    if ($update) {
        if (!($fp = fopen($file, 'w'))) {
            printf("Error:\n");
            printf("Cannot modify file '%s'. Check the file permissions.\n\n", $file);
            return;
        }
        fwrite($fp, $ret['output']);
        fclose($fp);
        if ($verbose) {
            printf("File updated/modified.\n", $file);
        }
    }
    if (($verbose || $warnings) && count($ret['errors']) > 0) {
        printConversionErrors($ret);
    }
    printConversionOutput($ret, $verbose, $quiet);
}
 $snippet_errors = array();
 if ("" == trim($_POST['file'])) {
     $snippet_errors['file'] = 'Please specify a file.';
 }
 if (!file_exists($_POST['file']) || !is_file($_POST['file']) || !is_readable($_POST['file'])) {
     $snippet_errors['file'] = 'Problems reading the file. Please verify that it exists and can be read.';
 }
 if (!empty($snippet_errors)) {
     // show the form
     MySQLConverterTool_GUI_Snippets::load(dirname(__FILE__) . '/snippets/form_file.php');
 } else {
     // let's try to convert some files...
     require_once '../Converter.php';
     $conv = new MySQLConverterTool_Converter();
     $snippet_file = $_POST['file'];
     $snippet_conv = $conv->convertFile($_POST['file']);
     if (isset($_POST['update']) && $_POST['update'] == 'yes') {
         if (isset($_POST['backup']) && $_POST['backup'] == 'on') {
             if (file_exists($snippet_file . '.org')) {
                 unlink($snippet_file . '.org');
             }
             $snippet_conv['rename'] = rename($snippet_file, $snippet_file . '.org');
         } else {
             $snippet_conv['rename'] = false;
         }
         $fp = fopen($snippet_file, 'w');
         if (!$fp) {
             $snippet_conv['update'] = false;
         } else {
             $snippet_conv['update'] = true;
             fwrite($fp, $snippet_conv['output']);
 }
 if (!empty($snippet_errors)) {
     // show the form
     MySQLConverterTool_GUI_Snippets::load(dirname(__FILE__) . '/snippets/form_directory.php');
 } else {
     // let's try to convert some files...
     $snippet_conv_found = 0;
     $snippet_conv_converted = 0;
     $snippet_conv_ok = 0;
     $snippet_conv_warnings = 0;
     $snippet_conv_errors = 0;
     $snippet_conv_count = count($files);
     $snippet_conv_length = 0;
     $outputs = array();
     foreach ($files as $k => $snippet_file) {
         $output = $conv->convertFile($snippet_file);
         $snippet_conv_found += $output['found'];
         $snippet_conv_converted += $output['converted'];
         $snippet_conv_ok += $output['found'] == $output['converted'] && count($output['errors']) == 0 ? 1 : 0;
         $snippet_conv_warnings += $output['found'] == $output['converted'] && count($output['errors']) > 0 ? 1 : 0;
         $snippet_conv_errors += $output['found'] != $output['converted'] && count($output['errors']) > 0 ? 1 : 0;
         $snippet_conv_length += strlen($output['output']);
         if (isset($_POST['update']) && $_POST['update'] == 'yes') {
             if (isset($_POST['backup']) && $_POST['backup'] == 'on') {
                 if (file_exists($snippet_file . '.org')) {
                     unlink($snippet_file . '.org');
                 }
                 $output['rename'] = rename($snippet_file, $snippet_file . '.org');
             } else {
                 $output['rename'] = false;
             }