Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TASK] Make script compatible with PHP 7+ #73

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
121 changes: 65 additions & 56 deletions index.php
Expand Up @@ -20,70 +20,79 @@

/**
* Retrieves a congiguration data, and converts it to an array.
*
* @param string $strPath
*
* @param string $strPath
* @return array
*/
function getConfig($strPath)
{
$arrRetVal = [];


if(empty($strPath)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, but imho, it should be moved to line 106, just before calling the getConfig($strPath)
function.
This is because getConfig($strPath) expects $strPath to be not empty.

$strPath = __DIR__ . '/config.json';
}

if (is_file($strPath)) {
$strExtension = pathinfo($strPath, PATHINFO_EXTENSION);

switch ($strExtension) {
case 'json':
$strContents = file_get_contents($strPath);
$config = json_decode($strContents, true);
$strError = '';

switch (json_last_error()) {
case JSON_ERROR_NONE:
// No code should be put here.
break;

case JSON_ERROR_DEPTH:
$strError = 'Maximum stack depth exceeded';
break;

case JSON_ERROR_STATE_MISMATCH:
$strError = 'Underflow or the modes mismatch';
break;

case JSON_ERROR_CTRL_CHAR:
$strError = 'Unexpected control character found';
break;

case JSON_ERROR_SYNTAX:
$strError = 'Syntax error, malformed JSON';
break;

case JSON_ERROR_UTF8:
$strError = 'Malformed UTF-8 characters, possibly incorrectly encoded';
break;

default:
$strError = 'Unknown error';
break;
}

$arrRetVal = is_null($config) || !empty($strError) ? [] : $config;
break;

case 'xml':
$config = simplexml_load_file($strPath);
$arrRetVal = empty($config) ? [] : get_object_vars($config);
break;
}
}


switch ($strExtension) {


case 'xml':
$config = simplexml_load_file($strPath);
$arrRetVal = empty($config) ? [] : get_object_vars($config);
break;
case 'json':
default:
$strContents = file_get_contents($strPath);
$config = json_decode($strContents, true);
$strError = '';

switch (json_last_error()) {
case JSON_ERROR_NONE:
// No code should be put here.
break;

case JSON_ERROR_DEPTH:
$strError = 'Maximum stack depth exceeded';
break;

case JSON_ERROR_STATE_MISMATCH:
$strError = 'Underflow or the modes mismatch';
break;

case JSON_ERROR_CTRL_CHAR:
$strError = 'Unexpected control character found';
break;

case JSON_ERROR_SYNTAX:
$strError = 'Syntax error, malformed JSON';
break;

case JSON_ERROR_UTF8:
$strError = 'Malformed UTF-8 characters, possibly incorrectly encoded';
break;

default:
$strError = 'Unknown error';
break;
}

$arrRetVal = is_null($config) || !empty($strError) ? [] : $config;
break;
}


return $arrRetVal;
}

ini_set('display_errors', 'on');

// Verify the extensions are loaded before running.
if (!extension_loaded('pgsql')) {
echo "Postgresql not enabled: you need the 'pgsql' module.\n";
exit(1);
//exit(1);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pgsql extension is required, since it runs the actual COPY command.
Side note: the pgsql extension is not deprecated.

}
if (!extension_loaded('pdo_mysql')) {
echo "Postgresql not enabled: you need the 'pdo_mysql' module.\n";
Expand All @@ -99,24 +108,24 @@ function getConfig($strPath)
}
if (ini_get('register_argc_argv') == 0) {
echo "register_argc_argv is not turned on, we can't process command line arguments.\n";
exit(1);
//exit(1);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to read command line arguments without register_argc_argv extension?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use for my CLI scripts

foreach($argc as $some)

}

$strParam = isset($argv[1]) && !empty($argv[1]) ? $argv[1] : $_SERVER['argv'][1];
$strParam = isset($argv[1]) && !empty($argv[1]) ? $argv[1] : $_SERVER['argv'][1];
$arrConfig = getConfig($strParam);
unset($strParam);

if (empty($arrConfig)) {
echo PHP_EOL, '-- Cannot perform a migration due to missing "config[.xml | .json]" file.', PHP_EOL;
} else {
spl_autoload_register(function($class) {
spl_autoload_register(function ($class) {
require_once 'migration/FromMySqlToPostgreSql/' . $class . '.php';
});

$arrConfig['temp_dir_path'] = __DIR__ . '/temporary_directory';
$arrConfig['logs_dir_path'] = __DIR__ . '/logs_directory';
$migration = new \FromMySqlToPostgreSql($arrConfig);

$migration = new FromMySqlToPostgreSql($arrConfig);
$migration->migrate();
}

Expand Down
2 changes: 1 addition & 1 deletion migration/FromMySqlToPostgreSql/FromMySqlToPostgreSql.php
Expand Up @@ -1616,7 +1616,7 @@ public function migrate()
* Create a database schema.
*/
if (!$this->createSchema()) {
$this->log('-- Script is terminated.' . PHP_EOL);
$this->log('-- Script is terminated. Could not create schema' . PHP_EOL);
exit;
} else {
$this->log('-- New schema "' . $this->strSchema . '" was successfully created...' . PHP_EOL);
Expand Down