Fix phpcs

This commit is contained in:
Laurent Destailleur 2020-02-11 11:48:10 +01:00
parent 34cfa0c6af
commit 5533526331

View File

@ -1,7 +1,8 @@
#!/usr/bin/env php #!/usr/bin/env php
<?php <?php
/* Copyright (C) 2005-2011 James Grant <james@lightbox.org> Lightbox Technologies Inc. /*
* Copyright (C) 2020 Rodolphe Quiedeville <rodolphe@quiedeville.org> * Copyright (C) 2005-2011 James Grant <james@lightbox.org> Lightbox Technologies Inc.
* Copyright (C) 2020 Laurent Destailleur <eldy@users.sourceforge.net>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -16,7 +17,8 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
* *
* ATTENTION DE PAS EXECUTER CE SCRIPT SUR UNE INSTALLATION DE PRODUCTION * This file is base on pg2mysql provided as Open source by lightbox.org.
* It was enhanced and updated by the Dolibarr team.
*/ */
/** /**
@ -24,7 +26,6 @@
* \brief Script to migrate a postgresql dump into a mysql dump * \brief Script to migrate a postgresql dump into a mysql dump
*/ */
$sapi_type = php_sapi_name(); $sapi_type = php_sapi_name();
$script_file = basename(__FILE__); $script_file = basename(__FILE__);
$path = dirname(__FILE__) . '/'; $path = dirname(__FILE__) . '/';
@ -33,66 +34,52 @@ $path=dirname(__FILE__).'/';
$sapi_type = php_sapi_name(); $sapi_type = php_sapi_name();
if (substr($sapi_type, 0, 3) == 'cgi') { if (substr($sapi_type, 0, 3) == 'cgi') {
echo "Error: You are using PHP for CGI. To execute " . $script_file . " from command line, you must use PHP for CLI mode.\n"; echo "Error: You are using PHP for CGI. To execute " . $script_file . " from command line, you must use PHP for CLI mode.\n";
exit; exit();
} }
error_reporting(E_ALL & ~ E_DEPRECATED); error_reporting(E_ALL & ~ E_DEPRECATED);
define('PRODUCT', "pg2mysql"); define('PRODUCT', "pg2mysql");
define('VERSION', "2.0"); define('VERSION', "2.0");
// this is the default, it can be overridden here, or specified as the third parameter on the command line // this is the default, it can be overridden here, or specified as the third parameter on the command line
$config['engine'] = "InnoDB"; $config['engine'] = "InnoDB";
if (! ($argv[1] && $argv[2])) { if (! ($argv[1] && $argv[2])) {
echo "Usage: php pg2mysql_cli.php <inputfilename> <outputfilename> [engine]\n"; echo "Usage: php pg2mysql_cli.php <inputfilename> <outputfilename> [engine]\n";
exit; exit();
} } else {
else { if (isset($argv[3]))
if(isset($argv[3])) $config['engine']=$argv[3]; $config['engine'] = $argv[3];
pg2mysql_large($argv[1], $argv[2]); pg2mysql_large($argv[1], $argv[2]);
echo <<<XHTML echo <<<XHTML
Notes: Notes:
- No its not perfect - No its not perfect
- Yes it discards ALL stored procedures - Yes it discards ALL stored procedures
- Yes it discards ALL queries except for CREATE TABLE and INSERT INTO - Yes it discards ALL queries except for CREATE TABLE and INSERT INTO
- Yes you can email us suggestsions: info[AT]lightbox.org
- In emails, please include the Postgres code, and the expected MySQL code
- If you're having problems creating your postgres dump, make sure you use "--format p --inserts" - If you're having problems creating your postgres dump, make sure you use "--format p --inserts"
- Default output engine if not specified is InnoDB - Default output engine if not specified is InnoDB
XHTML; XHTML;
} }
/**
* getfieldname
*
* @param string $l String
* @return string|null Field name
*/
function getfieldname($l) function getfieldname($l)
{ {
// first check if its in nice quotes for us // first check if its in nice quotes for us
$regs = array(); $regs = array();
if(preg_match("/`(.*)`/",$l,$regs)) if (preg_match("/`(.*)`/", $l, $regs)) {
{
if ($regs[1]) if ($regs[1])
return $regs[1]; return $regs[1];
else else
return null; return null;
} } // if its not in quotes, then it should (we hope!) be the first "word" on the line, up to the first space.
//if its not in quotes, then it should (we hope!) be the first "word" on the line, up to the first space. else if (preg_match("/([^\ ]*)/", trim($l), $regs)) {
else if(preg_match("/([^\ ]*)/",trim($l),$regs))
{
if ($regs[1]) if ($regs[1])
return $regs[1]; return $regs[1];
else else
@ -100,7 +87,15 @@ function getfieldname($l)
} }
} }
function formatsize($s) {
/**
* formatsize
*
* @param string $s Size to format
* @return string Formated size
*/
function formatsize($s)
{
if ($s < pow(2, 14)) if ($s < pow(2, 14))
return "{$s}B"; return "{$s}B";
else if ($s < pow(2, 20)) else if ($s < pow(2, 20))
@ -111,9 +106,15 @@ function formatsize($s) {
return sprintf("%.1f", round($s / 1024 / 1024 / 1024, 1)) . "G"; return sprintf("%.1f", round($s / 1024 / 1024 / 1024, 1)) . "G";
} }
/**
function pg2mysql_large($infilename,$outfilename) { * pg2mysql_large
$fs=filesize($infilename); *
* @param string $infilename Input filename
* @param string $outfilename Output filename
* @return int <0 if KO, >=0 if OK
*/
function pg2mysql_large($infilename, $outfilename)
{
$infp = fopen($infilename, "rt"); $infp = fopen($infilename, "rt");
$outfp = fopen($outfilename, "wt"); $outfp = fopen($outfilename, "wt");
@ -126,6 +127,13 @@ function pg2mysql_large($infilename,$outfilename) {
$linenum = 0; $linenum = 0;
$inquotes = false; $inquotes = false;
$first = true; $first = true;
if (empty($infp)) {
print 'Failed to open file '.$infilename."\n";
return -1;
}
$fs = filesize($infilename);
echo "Filesize: " . formatsize($fs) . "\n"; echo "Filesize: " . formatsize($fs) . "\n";
while ($instr = fgets($infp)) { while ($instr = fgets($infp)) {
@ -159,33 +167,34 @@ function pg2mysql_large($infilename,$outfilename) {
printf("Processing progress: %3d%% position: %7s line: %9d sql chunk: %9d mem usage: %4dM\r", $percent, $position, $linenum, $chunkcount, $memusage); printf("Processing progress: %3d%% position: %7s line: %9d sql chunk: %9d mem usage: %4dM\r", $percent, $position, $linenum, $chunkcount, $memusage);
} }
/* /*
echo "sending chunk:\n"; * echo "sending chunk:\n";
echo "=======================\n"; * echo "=======================\n";
print_r($pgsqlchunk); * print_r($pgsqlchunk);
echo "=======================\n"; * echo "=======================\n";
*/ */
/* /*
foreach ($pgsqlchunk as $aaa) { * foreach ($pgsqlchunk as $aaa) {
if (preg_match('/MAIN_ENABLE_DEFAULT|MAIN_MAIL_SMTP_SE/', $aaa)) { * if (preg_match('/MAIN_ENABLE_DEFAULT|MAIN_MAIL_SMTP_SE/', $aaa)) {
var_dump($pgsqlchunk); * var_dump($pgsqlchunk);
} * }
}*/ * }
*/
$mysqlchunk = pg2mysql($pgsqlchunk, $arrayofprimaryalreadyintabledef, $first); $mysqlchunk = pg2mysql($pgsqlchunk, $arrayofprimaryalreadyintabledef, $first);
fputs($outfp, $mysqlchunk['output']); fputs($outfp, $mysqlchunk['output']);
/* /*
$break = false; * $break = false;
foreach ($pgsqlchunk as $aaa) { * foreach ($pgsqlchunk as $aaa) {
if (preg_match('/MAIN_ENABLE_DEFAULT|MAIN_MAIL_SMTP_SE/', $aaa)) { * if (preg_match('/MAIN_ENABLE_DEFAULT|MAIN_MAIL_SMTP_SE/', $aaa)) {
var_dump($mysqlchunk); * var_dump($mysqlchunk);
} * }
if (preg_match('/MAIN_MAIL_SMTP_SE/', $aaa)) { * if (preg_match('/MAIN_MAIL_SMTP_SE/', $aaa)) {
$break = true; * $break = true;
} * }
} * }
if ($break) break; * if ($break) break;
*/ */
$outputatend .= $mysqlchunk['outputatend']; $outputatend .= $mysqlchunk['outputatend'];
@ -207,15 +216,24 @@ function pg2mysql_large($infilename,$outfilename) {
fputs($outfp, '/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;' . "\n"); fputs($outfp, '/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;' . "\n");
fputs($outfp, '/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;' . "\n"); fputs($outfp, '/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;' . "\n");
fputs($outfp, '/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;' . "\n"); fputs($outfp, '/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;' . "\n");
fputs($outfp, '/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;'."\n");; fputs($outfp, '/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;' . "\n");
printf("Completed! %9d lines %9d sql chunks\n\n", $linenum, $chunkcount); printf("Completed! %9d lines %9d sql chunks\n\n", $linenum, $chunkcount);
fclose($infp); fclose($infp);
fclose($outfp); fclose($outfp);
return 0;
} }
/**
* pg2mysql
*
* @param array $input Array of input
* @param array $arrayofprimaryalreadyintabledef Array of table already output with a primary key set into definition
* @param boolean $header Boolean
* @return string[] Array of output
*/
function pg2mysql(&$input, &$arrayofprimaryalreadyintabledef, $header = true) function pg2mysql(&$input, &$arrayofprimaryalreadyintabledef, $header = true)
{ {
global $config; global $config;
@ -244,8 +262,7 @@ function pg2mysql(&$input, &$arrayofprimaryalreadyintabledef, $header=true)
$output .= "\n"; $output .= "\n";
$outputatend = ""; $outputatend = "";
} } else {
else {
$output = ""; $output = "";
$outputatend = ""; $outputatend = "";
} }
@ -320,10 +337,7 @@ function pg2mysql(&$input, &$arrayofprimaryalreadyintabledef, $header=true)
$line = preg_replace("/ character varying/", " varchar(255)", $line); $line = preg_replace("/ character varying/", " varchar(255)", $line);
} }
if( preg_match("/ DEFAULT \('([0-9]*)'::int/",$line,$regs) || if (preg_match("/ DEFAULT \('([0-9]*)'::int/", $line, $regs) || preg_match("/ DEFAULT \('([0-9]*)'::smallint/", $line, $regs) || preg_match("/ DEFAULT \('([0-9]*)'::bigint/", $line, $regs)) {
preg_match("/ DEFAULT \('([0-9]*)'::smallint/",$line,$regs) ||
preg_match("/ DEFAULT \('([0-9]*)'::bigint/",$line,$regs)
) {
$num = $regs[1]; $num = $regs[1];
$line = preg_replace("/ DEFAULT \('([0-9]*)'[^ ,]*/", " DEFAULT $num ", $line); $line = preg_replace("/ DEFAULT \('([0-9]*)'[^ ,]*/", " DEFAULT $num ", $line);
} }
@ -405,8 +419,7 @@ function pg2mysql(&$input, &$arrayofprimaryalreadyintabledef, $header=true)
$output .= $before . " VALUES " . $after; $output .= $before . " VALUES " . $after;
$linenumber ++; $linenumber ++;
continue; continue;
} } else {
else {
// this insert spans multiple lines, so keep dumping the lines until we reach a line // this insert spans multiple lines, so keep dumping the lines until we reach a line
// that ends with ");" // that ends with ");"
@ -427,8 +440,9 @@ function pg2mysql(&$input, &$arrayofprimaryalreadyintabledef, $header=true)
// we have an odd number of ' marks // we have an odd number of ' marks
if ($c % 2 != 0) { if ($c % 2 != 0) {
$inquotes = true; $inquotes = true;
} else {
$inquotes = false;
} }
else $inquotes=false;
$output .= $before . " VALUES " . $after; $output .= $before . " VALUES " . $after;
do { do {
@ -449,11 +463,12 @@ function pg2mysql(&$input, &$arrayofprimaryalreadyintabledef, $header=true)
$c = substr_count($line, "'"); $c = substr_count($line, "'");
// we have an odd number of ' marks // we have an odd number of ' marks
if ($c % 2 != 0) { if ($c % 2 != 0) {
if($inquotes) $inquotes=false; if ($inquotes)
else $inquotes=true; $inquotes = false;
else
$inquotes = true;
// echo "inquotes=$inquotes\n"; // echo "inquotes=$inquotes\n";
} }
} while (substr($lines[$linenumber], - 3, - 1) != ");" || $inquotes); } while (substr($lines[$linenumber], - 3, - 1) != ");" || $inquotes);
} }
} }
@ -466,8 +481,7 @@ function pg2mysql(&$input, &$arrayofprimaryalreadyintabledef, $header=true)
$linenumber ++; $linenumber ++;
if (! empty($lines[$linenumber])) { if (! empty($lines[$linenumber])) {
$line = $lines[$linenumber]; $line = $lines[$linenumber];
} } else {
else {
$line = ''; $line = '';
} }
@ -484,13 +498,11 @@ function pg2mysql(&$input, &$arrayofprimaryalreadyintabledef, $header=true)
$output .= '-- ' . str_replace("\n", "", $pkey); $output .= '-- ' . str_replace("\n", "", $pkey);
$output .= '-- ' . $line . "\n"; $output .= '-- ' . $line . "\n";
} }
} else } else {
{
$output .= '-- ' . str_replace("\n", "", $pkey); $output .= '-- ' . str_replace("\n", "", $pkey);
$output .= '-- ' . $line . "\n"; $output .= '-- ' . $line . "\n";
} }
} }
} }
// while we're here, we might as well catch CREATE INDEX as well // while we're here, we might as well catch CREATE INDEX as well
@ -543,13 +555,13 @@ function pg2mysql(&$input, &$arrayofprimaryalreadyintabledef, $header=true)
} elseif ($in_insert) { } elseif ($in_insert) {
if ($line == "\\.\n") { if ($line == "\\.\n") {
$in_insert = FALSE; $in_insert = FALSE;
if($values) $output .= "INSERT INTO $heads VALUES\n" . implode(",\n", $values) . ";\n\n"; if ($values) {
$output .= "INSERT INTO $heads VALUES\n" . implode(",\n", $values) . ";\n\n";
}
} else { } else {
$vals = explode(' ', $line); $vals = explode(' ', $line);
foreach ($vals as $i => $val) { foreach ($vals as $i => $val) {
$vals[$i] = ($val == '\\N') $vals[$i] = ($val == '\\N') ? 'NULL' : "'" . str_replace("'", "\\'", trim($val)) . "'";
? 'NULL'
: "'" . str_replace("'", "\\'", trim($val)) . "'";
} }
$values[] = '(' . implode(',', $vals) . ')'; $values[] = '(' . implode(',', $vals) . ')';
if (count($values) >= 1000) { if (count($values) >= 1000) {