Update "tools" directory to be compliant with the php-cs-fixer
This commit is contained in:
@@ -1,14 +1,18 @@
|
||||
#!/usr/bin/php5
|
||||
<?php
|
||||
if (! isset ($argv[1]))
|
||||
die ("No file to decode provided\n");
|
||||
if (! isset($argv[1])) {
|
||||
die("No file to decode provided\n");
|
||||
}
|
||||
|
||||
$file = $argv[1];
|
||||
if (! file_exists ($file))
|
||||
die ("File $file not found\n");
|
||||
if (! is_readable ($file))
|
||||
die ("File $file not readable\n");
|
||||
if (! is_writeable ($file))
|
||||
die ("File $file not writeable\n");
|
||||
if (! file_exists($file)) {
|
||||
die("File $file not found\n");
|
||||
}
|
||||
if (! is_readable($file)) {
|
||||
die("File $file not readable\n");
|
||||
}
|
||||
if (! is_writeable($file)) {
|
||||
die("File $file not writeable\n");
|
||||
}
|
||||
|
||||
print_r (json_decode (file_get_contents ($file)));
|
||||
print_r(json_decode(file_get_contents($file)));
|
||||
|
||||
@@ -1,26 +1,29 @@
|
||||
#!/usr/bin/php
|
||||
<?php
|
||||
|
||||
require_once (__DIR__."/../src/markdown.php");
|
||||
require_once(__DIR__ . "/../src/markdown.php");
|
||||
|
||||
if (! isset ($argv[1]))
|
||||
{
|
||||
file_put_contents ("php://stderr",
|
||||
"No file to convert in markdown provided\n");
|
||||
exit (1);
|
||||
if (! isset($argv[1])) {
|
||||
file_put_contents(
|
||||
"php://stderr",
|
||||
"No file to convert in markdown provided\n"
|
||||
);
|
||||
exit(1);
|
||||
}
|
||||
if (! file_exists ($argv[1]))
|
||||
{
|
||||
file_put_contents ("php://stderr",
|
||||
"The file to convert in markdown doesn't exists\n");
|
||||
exit (2);
|
||||
if (! file_exists($argv[1])) {
|
||||
file_put_contents(
|
||||
"php://stderr",
|
||||
"The file to convert in markdown doesn't exists\n"
|
||||
);
|
||||
exit(2);
|
||||
}
|
||||
if (! is_readable ($argv[1]))
|
||||
{
|
||||
file_put_contents ("php://stderr",
|
||||
"The file to convert in markdown is not readable\n");
|
||||
exit (3);
|
||||
if (! is_readable($argv[1])) {
|
||||
file_put_contents(
|
||||
"php://stderr",
|
||||
"The file to convert in markdown is not readable\n"
|
||||
);
|
||||
exit(3);
|
||||
}
|
||||
|
||||
$md = new markdown ();
|
||||
echo $md->html (file_get_contents ($argv[1]));
|
||||
$md = new Domframework\Markdown();
|
||||
echo $md->html(file_get_contents($argv[1]));
|
||||
|
||||
@@ -1,187 +1,227 @@
|
||||
#!/usr/bin/php
|
||||
<?php
|
||||
/** DomFramework
|
||||
@package domframework
|
||||
@author Dominique Fournier <dominique@fournier38.fr> */
|
||||
|
||||
require_once (__DIR__."/../src/Getopts.php");
|
||||
require_once (__DIR__."/../src/Dblayeroo.php");
|
||||
/**
|
||||
* DomFramework
|
||||
* @package domframework
|
||||
* @author Dominique Fournier <dominique@fournier38.fr>
|
||||
*/
|
||||
|
||||
/** Generate the model files from an existing database
|
||||
* Provide the DSN to connect to the database, and the program will generate
|
||||
* the models/table.php files
|
||||
*/
|
||||
class modelGenerator
|
||||
require_once(__DIR__ . "/../src/Getopts.php");
|
||||
require_once(__DIR__ . "/../src/Dblayeroo.php");
|
||||
|
||||
/**
|
||||
* Generate the model files from an existing database
|
||||
* Provide the DSN to connect to the database, and the program will generate
|
||||
* the models/table.php files
|
||||
*/
|
||||
class ModelGenerator
|
||||
{
|
||||
/** The DSN provided by the user
|
||||
*/
|
||||
private $dsn;
|
||||
/**
|
||||
* The DSN provided by the user
|
||||
*/
|
||||
private $dsn;
|
||||
|
||||
/** The modelsDir provided by the user
|
||||
*/
|
||||
private $modelsDir;
|
||||
/**
|
||||
* The modelsDir provided by the user
|
||||
*/
|
||||
private $modelsDir;
|
||||
|
||||
/** The namespace name provided by the user
|
||||
*/
|
||||
private $namespaceName;
|
||||
/**
|
||||
* The namespace name provided by the user
|
||||
*/
|
||||
private $namespaceName;
|
||||
|
||||
/** The dblayer object
|
||||
*/
|
||||
private $db;
|
||||
/**
|
||||
* The dblayer object
|
||||
*/
|
||||
private $db;
|
||||
|
||||
/** The constructor
|
||||
*/
|
||||
public function __construct ()
|
||||
{
|
||||
$getopts = new Domframework\Getopts ();
|
||||
$getopts->add ("Help", "?h", "help", "Help of the software");
|
||||
$getopts->add ("DSN", "", "dsn:", "DSN - Data Source Name\n".
|
||||
" Exemple : --dsn \"mysql:dbname=DBNAME\"",
|
||||
"dsn");
|
||||
$getopts->add ("Output", "o:", "output:",
|
||||
"Output directory, ./models by default", "dir");
|
||||
$getopts->add ("Namespace", "n:", "namespace:",
|
||||
"The namespace (if '', not added), models by default",
|
||||
"namespaceName");
|
||||
$getopts->add ("Username", "u:", "username:", "Username to connect",
|
||||
"username");
|
||||
$getopts->add ("Password", "p:", "password:", "Password to connect",
|
||||
"password");
|
||||
if ($getopts->get ("Help"))
|
||||
die ($getopts->help ());
|
||||
if ($getopts->get ("DSN") === false)
|
||||
/**
|
||||
* The constructor
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
file_put_contents ("php://stderr", "ERROR: The DSN is not provided\n");
|
||||
exit (1);
|
||||
}
|
||||
$dsn = $getopts->get ("DSN");
|
||||
$modelsDir = $getopts->get ("Output");
|
||||
if ($modelsDir === false)
|
||||
$modelsDir = "./models";
|
||||
if (! file_exists ($modelsDir) || ! is_dir ($modelsDir))
|
||||
{
|
||||
file_put_contents ("php://stderr",
|
||||
"ERROR: The modelsDir '$modelsDir' doesn't exists or is not a directory\n");
|
||||
exit (1);
|
||||
$getopts = new Domframework\Getopts();
|
||||
$getopts->add("Help", "?h", "help", "Help of the software");
|
||||
$getopts->add(
|
||||
"DSN",
|
||||
"",
|
||||
"dsn:",
|
||||
"DSN - Data Source Name\n" .
|
||||
" Exemple : --dsn \"mysql:dbname=DBNAME\"",
|
||||
"dsn"
|
||||
);
|
||||
$getopts->add(
|
||||
"Output",
|
||||
"o:",
|
||||
"output:",
|
||||
"Output directory, ./models by default",
|
||||
"dir"
|
||||
);
|
||||
$getopts->add(
|
||||
"Namespace",
|
||||
"n:",
|
||||
"namespace:",
|
||||
"The namespace (if '', not added), models by default",
|
||||
"namespaceName"
|
||||
);
|
||||
$getopts->add(
|
||||
"Username",
|
||||
"u:",
|
||||
"username:",
|
||||
"Username to connect",
|
||||
"username"
|
||||
);
|
||||
$getopts->add(
|
||||
"Password",
|
||||
"p:",
|
||||
"password:",
|
||||
"Password to connect",
|
||||
"password"
|
||||
);
|
||||
if ($getopts->get("Help")) {
|
||||
die($getopts->help());
|
||||
}
|
||||
if ($getopts->get("DSN") === false) {
|
||||
file_put_contents("php://stderr", "ERROR: The DSN is not provided\n");
|
||||
exit(1);
|
||||
}
|
||||
$dsn = $getopts->get("DSN");
|
||||
$modelsDir = $getopts->get("Output");
|
||||
if ($modelsDir === false) {
|
||||
$modelsDir = "./models";
|
||||
}
|
||||
if (! file_exists($modelsDir) || ! is_dir($modelsDir)) {
|
||||
file_put_contents(
|
||||
"php://stderr",
|
||||
"ERROR: The modelsDir '$modelsDir' doesn't exists or is not a directory\n"
|
||||
);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$this->dsn = $dsn;
|
||||
$this->modelsDir = $modelsDir;
|
||||
$this->namespaceName = $getopts->get("Namespace");
|
||||
try {
|
||||
$this->db = new Domframework\Dblayeroo(
|
||||
$dsn,
|
||||
$getopts->get("Username"),
|
||||
$getopts->get("Password")
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
file_put_contents("php://stderr", $e->getMessage() . "\n");
|
||||
exit(3);
|
||||
}
|
||||
}
|
||||
|
||||
$this->dsn = $dsn;
|
||||
$this->modelsDir = $modelsDir;
|
||||
$this->namespaceName = $getopts->get ("Namespace");
|
||||
try
|
||||
/**
|
||||
* List all the existing tables in the database
|
||||
*/
|
||||
public function listTables()
|
||||
{
|
||||
$this->db = new Domframework\Dblayeroo ($dsn,
|
||||
$getopts->get ("Username"),
|
||||
$getopts->get ("Password"));
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
file_put_contents ("php://stderr", $e->getMessage ()."\n");
|
||||
exit (3);
|
||||
}
|
||||
}
|
||||
|
||||
/** List all the existing tables in the database
|
||||
*/
|
||||
public function listTables ()
|
||||
{
|
||||
return $this->db->listTables ();
|
||||
}
|
||||
|
||||
/** Create the model file from the $tableName
|
||||
* @param string $tableName The table to examine
|
||||
*/
|
||||
public function createModel ($tableName)
|
||||
{
|
||||
if (file_exists ($this->modelsDir."/$tableName".".php"))
|
||||
{
|
||||
file_put_contents ("php://stderr",
|
||||
"WARNING: File ".$this->modelsDir."/$tableName".".php already exists. ".
|
||||
"SKIP\n");
|
||||
return;
|
||||
}
|
||||
echo "Create File ".$this->modelsDir."/$tableName".".php\n";
|
||||
$schema = $this->db->getTableSchema ($tableName);
|
||||
$f = "";
|
||||
$f .= "<"."?"."php\n\n";
|
||||
if ($this->namespaceName === false)
|
||||
$f .= "namespace models;\n\n";
|
||||
elseif ($this->namespaceName !== "")
|
||||
$f .= "namespace $this->namespaceName;\n\n";
|
||||
$f .= "require_once (\"domframework/dblayeroo.php\");\n\n";
|
||||
$f .= "/** The model for $tableName table in the database\n */\n";
|
||||
$f .= "class $tableName extends \dblayeroo\n";
|
||||
$f .= "{\n";
|
||||
$f .= " /** The constructor connect to database\n */\n";
|
||||
$f .= " public function __construct ()\n {\n";
|
||||
$f .= " // The table name\n";
|
||||
$f .= " \$this->table (\"$tableName\");\n\n";
|
||||
$f .= " // The fields name and parameters\n";
|
||||
$f .= " \$this->fields (array (\n";
|
||||
foreach ($schema["fields"] as $field=>$params)
|
||||
{
|
||||
$f .= " \"$field\" => array (\"".implode ("\", \"", $params).
|
||||
"\"),\n";
|
||||
}
|
||||
$f .= " ));\n\n";
|
||||
$f .= " // The primary key\n";
|
||||
$f .= " \$this->primary (\"".$schema["primary"]."\");\n";
|
||||
if (count ($schema["unique"]))
|
||||
{
|
||||
$f .= "\n";
|
||||
$f .= " // The unique constraints\n";
|
||||
$f .= " \$this->unique (array (\n";
|
||||
foreach ($schema["unique"] as $unique)
|
||||
{
|
||||
if (is_array ($unique))
|
||||
$f .= " array (\"".implode ("\", \"", $unique)."\"),\n";
|
||||
else
|
||||
$f .= " array (\"$unique\"),\n";
|
||||
}
|
||||
$f .= " ));\n";
|
||||
}
|
||||
$f .= "\n";
|
||||
$f .= " // The titles\n";
|
||||
$f .= " \$this->titles (array (\n";
|
||||
foreach ($schema["fields"] as $field=>$params)
|
||||
{
|
||||
$f .= " \"$field\" => _(\"$field\"),\n";
|
||||
}
|
||||
$f .= " ));\n";
|
||||
if (count ($schema["foreign"]))
|
||||
{
|
||||
$f .= "\n";
|
||||
$f .= " // The foreign constraints\n";
|
||||
$f .= " \$this->foreign (array (\n";
|
||||
foreach ($schema["foreign"] as $field=>$params)
|
||||
$f .= " \"$field\" => array (\n \"".
|
||||
implode ("\", \"", $params).
|
||||
"\"),\n";
|
||||
$f .= " ));\n";
|
||||
|
||||
$f .= "\n";
|
||||
$f .= " // And the associated objects\n";
|
||||
if ($this->namespaceName === false)
|
||||
$path = "\\models\\";
|
||||
elseif ($this->namespaceName !== "")
|
||||
$path = "\\$this->namespaceName\\";
|
||||
else
|
||||
$path = "";
|
||||
foreach ($schema["foreign"] as $field=>$params)
|
||||
{
|
||||
$objName = $params[0];
|
||||
$f .= " \$this->foreign_$objName = new $path$objName ();\n";
|
||||
$f .= " \$this->setForeignObj (\$this->foreign_$objName);\n";
|
||||
}
|
||||
return $this->db->listTables();
|
||||
}
|
||||
|
||||
$f .= " }\n";
|
||||
$f .= "}\n";
|
||||
file_put_contents ($this->modelsDir."/$tableName".".php", $f);
|
||||
}
|
||||
/**
|
||||
* Create the model file from the $tableName
|
||||
* @param string $tableName The table to examine
|
||||
*/
|
||||
public function createModel($tableName)
|
||||
{
|
||||
if (file_exists($this->modelsDir . "/$tableName" . ".php")) {
|
||||
file_put_contents(
|
||||
"php://stderr",
|
||||
"WARNING: File " . $this->modelsDir . "/$tableName" . ".php already exists. " .
|
||||
"SKIP\n"
|
||||
);
|
||||
return;
|
||||
}
|
||||
echo "Create File " . $this->modelsDir . "/$tableName" . ".php\n";
|
||||
$schema = $this->db->getTableSchema($tableName);
|
||||
$f = "";
|
||||
$f .= "<" . "?" . "php\n\n";
|
||||
if ($this->namespaceName === false) {
|
||||
$f .= "namespace models;\n\n";
|
||||
} elseif ($this->namespaceName !== "") {
|
||||
$f .= "namespace $this->namespaceName;\n\n";
|
||||
}
|
||||
$f .= "require_once (\"domframework/dblayeroo.php\");\n\n";
|
||||
$f .= "/**
|
||||
* The model for $tableName table in the database\n
|
||||
*/\n";
|
||||
$f .= "class $tableName extends \dblayeroo\n";
|
||||
$f .= "{\n";
|
||||
$f .= " /**
|
||||
* The constructor connect to database\n
|
||||
*/\n";
|
||||
$f .= " public function __construct ()\n {\n";
|
||||
$f .= " // The table name\n";
|
||||
$f .= " \$this->table (\"$tableName\");\n\n";
|
||||
$f .= " // The fields name and parameters\n";
|
||||
$f .= " \$this->fields (array (\n";
|
||||
foreach ($schema["fields"] as $field => $params) {
|
||||
$f .= " \"$field\" => array (\"" . implode("\", \"", $params) .
|
||||
"\"),\n";
|
||||
}
|
||||
$f .= " ));\n\n";
|
||||
$f .= " // The primary key\n";
|
||||
$f .= " \$this->primary (\"" . $schema["primary"] . "\");\n";
|
||||
if (count($schema["unique"])) {
|
||||
$f .= "\n";
|
||||
$f .= " // The unique constraints\n";
|
||||
$f .= " \$this->unique (array (\n";
|
||||
foreach ($schema["unique"] as $unique) {
|
||||
if (is_array($unique)) {
|
||||
$f .= " array (\"" . implode("\", \"", $unique) . "\"),\n";
|
||||
} else {
|
||||
$f .= " array (\"$unique\"),\n";
|
||||
}
|
||||
}
|
||||
$f .= " ));\n";
|
||||
}
|
||||
$f .= "\n";
|
||||
$f .= " // The titles\n";
|
||||
$f .= " \$this->titles (array (\n";
|
||||
foreach ($schema["fields"] as $field => $params) {
|
||||
$f .= " \"$field\" => _(\"$field\"),\n";
|
||||
}
|
||||
$f .= " ));\n";
|
||||
if (count($schema["foreign"])) {
|
||||
$f .= "\n";
|
||||
$f .= " // The foreign constraints\n";
|
||||
$f .= " \$this->foreign (array (\n";
|
||||
foreach ($schema["foreign"] as $field => $params) {
|
||||
$f .= " \"$field\" => array (\n \"" .
|
||||
implode("\", \"", $params) .
|
||||
"\"),\n";
|
||||
}
|
||||
$f .= " ));\n";
|
||||
|
||||
$f .= "\n";
|
||||
$f .= " // And the associated objects\n";
|
||||
if ($this->namespaceName === false) {
|
||||
$path = "\\models\\";
|
||||
} elseif ($this->namespaceName !== "") {
|
||||
$path = "\\$this->namespaceName\\";
|
||||
} else {
|
||||
$path = "";
|
||||
}
|
||||
foreach ($schema["foreign"] as $field => $params) {
|
||||
$objName = $params[0];
|
||||
$f .= " \$this->foreign_$objName = new $path$objName ();\n";
|
||||
$f .= " \$this->setForeignObj (\$this->foreign_$objName);\n";
|
||||
}
|
||||
}
|
||||
|
||||
$f .= " }\n";
|
||||
$f .= "}\n";
|
||||
file_put_contents($this->modelsDir . "/$tableName" . ".php", $f);
|
||||
}
|
||||
}
|
||||
|
||||
$modelGenerator = new modelGenerator ();
|
||||
$tables = $modelGenerator->listTables ();
|
||||
foreach ($tables as $tableName)
|
||||
$modelGenerator->createModel ($tableName);
|
||||
$modelGenerator = new ModelGenerator();
|
||||
$tables = $modelGenerator->listTables();
|
||||
foreach ($tables as $tableName) {
|
||||
$modelGenerator->createModel($tableName);
|
||||
}
|
||||
|
||||
@@ -1,177 +1,182 @@
|
||||
#!/usr/bin/php
|
||||
<?php
|
||||
/** DomFramework
|
||||
* @package domframework
|
||||
* @author Dominique Fournier <dominique@fournier38.fr>
|
||||
*/
|
||||
|
||||
require_once (__DIR__."/../src/Dblayeroo.php");
|
||||
/**
|
||||
* DomFramework
|
||||
* @package domframework
|
||||
* @author Dominique Fournier <dominique@fournier38.fr>
|
||||
*/
|
||||
|
||||
/** modelGraph
|
||||
* Allow to create the relational graph of an existing database
|
||||
* Use GraphViz to generate the graph
|
||||
* Request the database DSN to operate
|
||||
*/
|
||||
class modelgraph
|
||||
require_once(__DIR__ . "/../src/Dblayeroo.php");
|
||||
|
||||
/**
|
||||
* modelGraph
|
||||
* Allow to create the relational graph of an existing database
|
||||
* Use GraphViz to generate the graph
|
||||
* Request the database DSN to operate
|
||||
*/
|
||||
class Modelgraph
|
||||
{
|
||||
// PROPERTIES
|
||||
/** The connected object dblayeroo
|
||||
*/
|
||||
private $db;
|
||||
/** The output file
|
||||
*/
|
||||
private $output;
|
||||
// PROPERTIES
|
||||
/**
|
||||
* The connected object dblayeroo
|
||||
*/
|
||||
private $db;
|
||||
/**
|
||||
* The output file
|
||||
*/
|
||||
private $output;
|
||||
|
||||
/** The constructor wait for $dsn
|
||||
* @param string $dsn The DSN to connect to database
|
||||
* @param string $username The username to connect to database
|
||||
* @param string $password The password to connect to database
|
||||
* @param string $output The output file to generate
|
||||
*/
|
||||
public function __construct ($dsn, $username, $password, $output)
|
||||
// {{{
|
||||
{
|
||||
$this->output = $output;
|
||||
$this->db = new Domframework\Dblayeroo ($dsn, $username, $password);
|
||||
$this->graphvizCommande ();
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Generate the output file
|
||||
*/
|
||||
public function generate ()
|
||||
// {{{
|
||||
{
|
||||
$tables = array ();
|
||||
foreach ($this->db->listTables () as $table)
|
||||
/**
|
||||
* The constructor wait for $dsn
|
||||
* @param string $dsn The DSN to connect to database
|
||||
* @param string $username The username to connect to database
|
||||
* @param string $password The password to connect to database
|
||||
* @param string $output The output file to generate
|
||||
*/
|
||||
public function __construct($dsn, $username, $password, $output)
|
||||
{
|
||||
$tables[$table] = $this->db->getTableSchema ($table);
|
||||
$this->output = $output;
|
||||
$this->db = new Domframework\Dblayeroo($dsn, $username, $password);
|
||||
$this->graphvizCommande();
|
||||
}
|
||||
$d = "digraph G {\n";
|
||||
$d .= " graph [\n";
|
||||
$d .= " label = <".
|
||||
"<B>Database ".$this->db->databasename ()."</B><BR/>".
|
||||
"<B>".date ("Y-m-d")."</B>".
|
||||
">\n";
|
||||
$d .= " ]\n";
|
||||
$d .= " node [\n";
|
||||
$d .= " shape=box\n";
|
||||
$d .= " fontname = \"helvetica\"\n";
|
||||
$d .= " ];\n";
|
||||
$d .= " rankdir=RL\n";
|
||||
|
||||
// Generate the tables objects with the fields
|
||||
foreach ($tables as $table => $schema)
|
||||
/**
|
||||
* Generate the output file
|
||||
*/
|
||||
public function generate()
|
||||
{
|
||||
$d .= " /** TABLE '$table' */\n";
|
||||
$d .= " \"$table\" [\n";
|
||||
$d .= " shape = \"plaintext\"\n";
|
||||
$d .= " label = <\n";
|
||||
$d .= " <TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\">\n";
|
||||
// Display the name of the table as title
|
||||
$d .= " <TR><TD COLSPAN=\"3\" BGCOLOR=\"#00B0E0\">";
|
||||
$d .= "$table</TD></TR>\n";
|
||||
// Display the fields
|
||||
$i = 0;
|
||||
foreach ($schema["fields"] as $field => $params)
|
||||
{
|
||||
$d .= " <TR>\n";
|
||||
$d .= " <TD SIDES=\"TBL\" PORT=\"OUT-$field\">";
|
||||
if (key_exists ($field, $schema["foreign"]))
|
||||
{
|
||||
// Foreign Key
|
||||
$d .= "<FONT COLOR=\"#F02020\">!</FONT>";
|
||||
$tables = [];
|
||||
foreach ($this->db->listTables() as $table) {
|
||||
$tables[$table] = $this->db->getTableSchema($table);
|
||||
}
|
||||
elseif (key_exists ("primary", $schema) &&
|
||||
$field === $schema["primary"])
|
||||
$d .= "🔑"; // Primary key found
|
||||
elseif (in_array ("not null", $params))
|
||||
$d .= "<FONT COLOR=\"#00B0E0\">◆</FONT>"; // NOT NULL
|
||||
else
|
||||
$d .= "◇";
|
||||
$d .= "</TD>\n";
|
||||
$d .= " <TD ALIGN=\"LEFT\" SIDES=\"TB\">";
|
||||
$d .= $field;
|
||||
$d .= "</TD>\n";
|
||||
$d .= " <TD ALIGN=\"LEFT\" PORT=\"IN-$field\" SIDES=\"TBR\">";
|
||||
$d .= $params[0];
|
||||
$d .= "</TD>\n";
|
||||
$d .= " </TR>\n";
|
||||
$i++;
|
||||
}
|
||||
$d .= " </TABLE>>\n";
|
||||
$d .= " ];\n";
|
||||
}
|
||||
// Generate the links between the tables
|
||||
foreach ($tables as $table => $schema)
|
||||
{
|
||||
// "node1":"f2" -> "node5":"f0" [];
|
||||
foreach ($schema["foreign"] as $field => $params)
|
||||
{
|
||||
$d .= "\"$table\":\"OUT-$field\" -> \"". $params[0].
|
||||
"\":\"IN-".$params[1]."\" ";
|
||||
$d .= "[];\n";
|
||||
}
|
||||
}
|
||||
$d .= "}\n";
|
||||
//echo $d;
|
||||
$this->createJPG ($d);
|
||||
}
|
||||
// }}}
|
||||
$d = "digraph G {\n";
|
||||
$d .= " graph [\n";
|
||||
$d .= " label = <" .
|
||||
"<B>Database " . $this->db->databasename() . "</B><BR/>" .
|
||||
"<B>" . date("Y-m-d") . "</B>" .
|
||||
">\n";
|
||||
$d .= " ]\n";
|
||||
$d .= " node [\n";
|
||||
$d .= " shape=box\n";
|
||||
$d .= " fontname = \"helvetica\"\n";
|
||||
$d .= " ];\n";
|
||||
$d .= " rankdir=RL\n";
|
||||
|
||||
/** Look for the dot command
|
||||
* @return the full path of 'dot' command
|
||||
*/
|
||||
private function graphvizCommande ()
|
||||
// {{{
|
||||
{
|
||||
foreach (explode (":", getenv ("PATH")) as $path)
|
||||
{
|
||||
if (file_exists ("$path/dot"))
|
||||
return "$path/dot";
|
||||
// Generate the tables objects with the fields
|
||||
foreach ($tables as $table => $schema) {
|
||||
$d .= " /**
|
||||
* TABLE '$table'
|
||||
*/\n";
|
||||
$d .= " \"$table\" [\n";
|
||||
$d .= " shape = \"plaintext\"\n";
|
||||
$d .= " label = <\n";
|
||||
$d .= " <TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\">\n";
|
||||
// Display the name of the table as title
|
||||
$d .= " <TR><TD COLSPAN=\"3\" BGCOLOR=\"#00B0E0\">";
|
||||
$d .= "$table</TD></TR>\n";
|
||||
// Display the fields
|
||||
$i = 0;
|
||||
foreach ($schema["fields"] as $field => $params) {
|
||||
$d .= " <TR>\n";
|
||||
$d .= " <TD SIDES=\"TBL\" PORT=\"OUT-$field\">";
|
||||
if (key_exists($field, $schema["foreign"])) {
|
||||
// Foreign Key
|
||||
$d .= "<FONT COLOR=\"#F02020\">!</FONT>";
|
||||
} elseif (
|
||||
key_exists("primary", $schema) &&
|
||||
$field === $schema["primary"]
|
||||
) {
|
||||
$d .= "🔑";
|
||||
} elseif (in_array("not null", $params)) {
|
||||
// Primary key found
|
||||
$d .= "<FONT COLOR=\"#00B0E0\">◆</FONT>";
|
||||
} else {
|
||||
// NOT NULL
|
||||
$d .= "◇";
|
||||
}
|
||||
$d .= "</TD>\n";
|
||||
$d .= " <TD ALIGN=\"LEFT\" SIDES=\"TB\">";
|
||||
$d .= $field;
|
||||
$d .= "</TD>\n";
|
||||
$d .= " <TD ALIGN=\"LEFT\" PORT=\"IN-$field\" SIDES=\"TBR\">";
|
||||
$d .= $params[0];
|
||||
$d .= "</TD>\n";
|
||||
$d .= " </TR>\n";
|
||||
$i++;
|
||||
}
|
||||
$d .= " </TABLE>>\n";
|
||||
$d .= " ];\n";
|
||||
}
|
||||
// Generate the links between the tables
|
||||
foreach ($tables as $table => $schema) {
|
||||
// "node1":"f2" -> "node5":"f0" [];
|
||||
foreach ($schema["foreign"] as $field => $params) {
|
||||
$d .= "\"$table\":\"OUT-$field\" -> \"" . $params[0] .
|
||||
"\":\"IN-" . $params[1] . "\" ";
|
||||
$d .= "[];\n";
|
||||
}
|
||||
}
|
||||
$d .= "}\n";
|
||||
//echo $d;
|
||||
$this->createJPG($d);
|
||||
}
|
||||
throw new \Exception ("Can not find 'dot' executable. Install graphviz",
|
||||
500);
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Generate the JPEG file with graphviz
|
||||
* @param string $content The content of the GV file
|
||||
*/
|
||||
private function createJPG ($content)
|
||||
// {{{
|
||||
{
|
||||
$tmp = tempnam ("/tmp", "modelgraph_");
|
||||
file_put_contents ($tmp, $content);
|
||||
$cmd = $this->graphvizCommande (). " -T jpeg $tmp ".
|
||||
"-o ".escapeshellarg ($this->output)." 2>&1";
|
||||
exec ($cmd, $output);
|
||||
unlink ($tmp);
|
||||
if (!empty ($output))
|
||||
/**
|
||||
* Look for the dot command
|
||||
* @return the full path of 'dot' command
|
||||
*/
|
||||
private function graphvizCommande()
|
||||
{
|
||||
echo "ERROR : ".implode ("\n", $output);
|
||||
exit (2);
|
||||
foreach (explode(":", getenv("PATH")) as $path) {
|
||||
if (file_exists("$path/dot")) {
|
||||
return "$path/dot";
|
||||
}
|
||||
}
|
||||
throw new \Exception(
|
||||
"Can not find 'dot' executable. Install graphviz",
|
||||
500
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the JPEG file with graphviz
|
||||
* @param string $content The content of the GV file
|
||||
*/
|
||||
private function createJPG($content)
|
||||
{
|
||||
$tmp = tempnam("/tmp", "modelgraph_");
|
||||
file_put_contents($tmp, $content);
|
||||
$cmd = $this->graphvizCommande() . " -T jpeg $tmp " .
|
||||
"-o " . escapeshellarg($this->output) . " 2>&1";
|
||||
exec($cmd, $output);
|
||||
unlink($tmp);
|
||||
if (!empty($output)) {
|
||||
echo "ERROR : " . implode("\n", $output);
|
||||
exit(2);
|
||||
}
|
||||
}
|
||||
}
|
||||
// }}}
|
||||
}
|
||||
|
||||
|
||||
// MAIN PROGRAM
|
||||
if (isset ($argv[1]) && $argv[1] === "-h")
|
||||
{
|
||||
echo "Create the ER Diagram of a SQL database\n";
|
||||
echo "Usage : \n";
|
||||
echo $argv[0]." DSN username password outputfile.jpg\n";
|
||||
echo "DSN Examples : 'mysql:dbname=databaseName'\n";
|
||||
if (isset($argv[1]) && $argv[1] === "-h") {
|
||||
echo "Create the ER Diagram of a SQL database\n";
|
||||
echo "Usage : \n";
|
||||
echo $argv[0] . " DSN username password outputfile.jpg\n";
|
||||
echo "DSN Examples : 'mysql:dbname=databaseName'\n";
|
||||
}
|
||||
if (! isset ($argv[1]))
|
||||
die ("No DSN provided\n");
|
||||
if (! isset ($argv[2]))
|
||||
die ("No username provided\n");
|
||||
if (! isset ($argv[3]))
|
||||
die ("No password provided\n");
|
||||
if (! isset ($argv[4]))
|
||||
die ("No output file provided\n");
|
||||
$modelgraph = new modelgraph ($argv[1], $argv[2], $argv[3], $argv[4]);
|
||||
$modelgraph->generate ();
|
||||
if (! isset($argv[1])) {
|
||||
die("No DSN provided\n");
|
||||
}
|
||||
if (! isset($argv[2])) {
|
||||
die("No username provided\n");
|
||||
}
|
||||
if (! isset($argv[3])) {
|
||||
die("No password provided\n");
|
||||
}
|
||||
if (! isset($argv[4])) {
|
||||
die("No output file provided\n");
|
||||
}
|
||||
$modelgraph = new modelgraph($argv[1], $argv[2], $argv[3], $argv[4]);
|
||||
$modelgraph->generate();
|
||||
|
||||
@@ -1,383 +1,407 @@
|
||||
#!/usr/bin/php
|
||||
<?php
|
||||
/** DomFramework
|
||||
* @package domframework
|
||||
* @author Dominique Fournier <dominique@fournier38.fr>
|
||||
*
|
||||
* SQL Migrate
|
||||
* This tool allow to migrate the data from one SQL database to another.
|
||||
* The DB engine can also be different.
|
||||
*
|
||||
* You must have the source DB and create a target DB with all the destination
|
||||
* tables available.
|
||||
* You must pass one parameter : the configuration file where all the
|
||||
* informations will be stored. The file will be created if it doesn't exists.
|
||||
*/
|
||||
|
||||
require_once (__DIR__."/../src/Console.php");
|
||||
require_once (__DIR__."/../src/Dblayeroo.php");
|
||||
require_once (__DIR__."/../src/Getopts.php");
|
||||
require_once (__DIR__."/../src/Config.php");
|
||||
/**
|
||||
* DomFramework
|
||||
* @package domframework
|
||||
* @author Dominique Fournier <dominique@fournier38.fr>
|
||||
*
|
||||
* SQL Migrate
|
||||
* This tool allow to migrate the data from one SQL database to another.
|
||||
* The DB engine can also be different.
|
||||
*
|
||||
* You must have the source DB and create a target DB with all the destination
|
||||
* tables available.
|
||||
* You must pass one parameter : the configuration file where all the
|
||||
* informations will be stored. The file will be created if it doesn't exists.
|
||||
*/
|
||||
|
||||
/** Manage the configuration file
|
||||
*/
|
||||
class configuration extends \config
|
||||
require_once(__DIR__ . "/../src/Console.php");
|
||||
require_once(__DIR__ . "/../src/Dblayeroo.php");
|
||||
require_once(__DIR__ . "/../src/Getopts.php");
|
||||
require_once(__DIR__ . "/../src/Config.php");
|
||||
|
||||
/**
|
||||
* Manage the configuration file
|
||||
*/
|
||||
class Configuration extends \Domframework\Config
|
||||
{
|
||||
/** The constructor of the configuration set the differents params
|
||||
*/
|
||||
function __construct ()
|
||||
{
|
||||
$this->default = array (
|
||||
"db" => array (
|
||||
"srcDSN" => "",
|
||||
"srcUser" => null,
|
||||
"srcPassword" => null,
|
||||
"dstDSN" => "",
|
||||
"dstUser" => null,
|
||||
"dstPassword" => null,
|
||||
),
|
||||
"tables" => array (
|
||||
),
|
||||
"tablesRel" => array (
|
||||
),
|
||||
"fields" => array (
|
||||
),
|
||||
"tablesOrder" => array (
|
||||
),
|
||||
"deleteContent" => null,
|
||||
);
|
||||
}
|
||||
/**
|
||||
* The constructor of the configuration set the differents params
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->default = [
|
||||
"db" => [
|
||||
"srcDSN" => "",
|
||||
"srcUser" => null,
|
||||
"srcPassword" => null,
|
||||
"dstDSN" => "",
|
||||
"dstUser" => null,
|
||||
"dstPassword" => null,
|
||||
],
|
||||
"tables" => [
|
||||
],
|
||||
"tablesRel" => [
|
||||
],
|
||||
"fields" => [
|
||||
],
|
||||
"tablesOrder" => [
|
||||
],
|
||||
"deleteContent" => null,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/** The main class
|
||||
*/
|
||||
class main
|
||||
/**
|
||||
* The main class
|
||||
*/
|
||||
class Main
|
||||
{
|
||||
/** The constructor of the class do the main job
|
||||
*/
|
||||
public function __construct ()
|
||||
{
|
||||
// Manage the Getopts
|
||||
$getopts = new Domframework\Getopts ();
|
||||
$getopts->add ("Help", "?h", array ("help"), "Help of the software");
|
||||
$getopts->add ("Step", "", array ("step:"), "Restart at provided step",
|
||||
"StepIdentifier");
|
||||
$getopts->add ("Debug", "", array ("debug"), "Display the SQL requests");
|
||||
$getopts->add ("Execute", "", array ("execute"),
|
||||
"Execute the configuration without requesting data (start at step 5)");
|
||||
if ($getopts->get ("Help"))
|
||||
die ($getopts->help ());
|
||||
if ($getopts->get ("Step"))
|
||||
/**
|
||||
* The constructor of the class do the main job
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
if (intval ($getopts->get ("Step")) < 0 ||
|
||||
intval ($getopts->get ("Step")) > 6)
|
||||
die ("Invalid Step provided (Must be between 0 and 6)\n");
|
||||
}
|
||||
if (empty ($getopts->restOfLine ()))
|
||||
throw new \Exception ("No configuration file provided");
|
||||
if (count ($getopts->restOfLine ()) > 1)
|
||||
throw new \Exception ("Too much configuration file provided");
|
||||
$configurationFile = current ($getopts->restOfLine ());
|
||||
if (! file_exists ($configurationFile))
|
||||
touch ($configurationFile);
|
||||
$config = new Domframework\Configuration ();
|
||||
$config->confFile = $configurationFile;
|
||||
|
||||
$db = $config->get ("db");
|
||||
$tables = $config->get ("tables");
|
||||
$tablesRel = $config->get ("tablesRel");
|
||||
$fields = $config->get ("fields");
|
||||
$tablesOrder = $config->get ("tablesOrder");
|
||||
$deleteContent = $config->get ("deleteContent");
|
||||
if ($db["srcDSN"] === "" || $db["dstDSN"] === "")
|
||||
$step = 0;
|
||||
elseif (empty ($tables))
|
||||
$step = 1;
|
||||
elseif (empty ($tablesRel))
|
||||
$step = 2;
|
||||
elseif (empty ($fields))
|
||||
$step = 3;
|
||||
elseif (empty ($tablesOrder))
|
||||
$step = 4;
|
||||
elseif ($deleteContent === null)
|
||||
$step = 5;
|
||||
else
|
||||
$step = 6;
|
||||
|
||||
if ($getopts->get ("Step"))
|
||||
$step = intval ($getopts->get ("Step"));
|
||||
if ($getopts->get ("Execute"))
|
||||
$step = 5;
|
||||
|
||||
if ($step === 0)
|
||||
{
|
||||
// Step 0: ask the connection parameters
|
||||
echo "#### Entering step 0 : ask the connection parameters\n";
|
||||
$srcDSN = $this->ask ("Enter SRC DSN");
|
||||
$srcUser = $this->ask ("Enter SRC User");
|
||||
$srcPassword = $this->ask ("Enter SRC Password");
|
||||
$dstDSN = $this->ask ("Enter DST DSN");
|
||||
$dstUser = $this->ask ("Enter DST User");
|
||||
$dstPassword = $this->ask ("Enter DST Password");
|
||||
if ($srcUser === "")
|
||||
{
|
||||
$srcUser = null;
|
||||
$srcPassword = null;
|
||||
}
|
||||
if ($dstUser === "")
|
||||
{
|
||||
$dstUser = null;
|
||||
$dstPassword = null;
|
||||
}
|
||||
$value = array (
|
||||
"srcDSN" => $srcDSN,
|
||||
"srcUser" => $srcUser,
|
||||
"srcPassword" => $srcPassword,
|
||||
"dstDSN" => $dstDSN,
|
||||
"dstUser" => $dstUser,
|
||||
"dstPassword" => $dstPassword,);
|
||||
$config->set ("db", $value);
|
||||
$db = $config->get ("db");
|
||||
$step++;
|
||||
}
|
||||
if ($step === 1)
|
||||
{
|
||||
echo "#### Entering step 1: read the existing databases schemes\n";
|
||||
$srcDB = new Domframework\Dblayeroo ($db["srcDSN"], $db["srcUser"],
|
||||
$db["srcPassword"]);
|
||||
$srcTables = $srcDB->listTables ();
|
||||
$dstDB = new Domframework\Dblayeroo ($db["dstDSN"], $db["dstUser"],
|
||||
$db["dstPassword"]);
|
||||
$dstTables = $dstDB->listTables ();
|
||||
$value = array (
|
||||
"srcTables" => $srcTables,
|
||||
"dstTables" => $dstTables,
|
||||
);
|
||||
$config->set ("tables", $value);
|
||||
$step++;
|
||||
echo "\n";
|
||||
}
|
||||
|
||||
echo "#### Connect to the defined databases\n";
|
||||
$srcDB = new Domframework\Dblayeroo ($db["srcDSN"], $db["srcUser"],
|
||||
$db["srcPassword"]);
|
||||
$dstDB = new Domframework\Dblayeroo ($db["dstDSN"], $db["dstUser"],
|
||||
$db["dstPassword"]);
|
||||
echo "\n";
|
||||
|
||||
if ($step == 2)
|
||||
{
|
||||
echo "#### Entering step 2: ask the relations between tables\n";
|
||||
$tables = $config->get ("tables");
|
||||
echo "# For each destination table, please provide the source table\n";
|
||||
sort ($tables["srcTables"]);
|
||||
echo "# Allowed source tables : ". implode (", ", $tables["srcTables"]).
|
||||
"\n";
|
||||
$value = array ();
|
||||
natsort ($tables["dstTables"]);
|
||||
foreach ($tables["dstTables"] as $table)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
$value[$table] = $this->ask (
|
||||
"Destination Table '$table' filled by source table");
|
||||
if (in_array ($value[$table], $tables["srcTables"]) ||
|
||||
$value[$table] === "")
|
||||
continue 2;
|
||||
$this->error ("The table '".$value[$table]." doesn't exists");
|
||||
echo "# Allowed source tables : ".
|
||||
implode (", ", $tables["srcTables"])."\n";
|
||||
// Manage the Getopts
|
||||
$getopts = new Domframework\Getopts();
|
||||
$getopts->add("Help", "?h", ["help"], "Help of the software");
|
||||
$getopts->add(
|
||||
"Step",
|
||||
"",
|
||||
["step:"],
|
||||
"Restart at provided step",
|
||||
"StepIdentifier"
|
||||
);
|
||||
$getopts->add("Debug", "", ["debug"], "Display the SQL requests");
|
||||
$getopts->add(
|
||||
"Execute",
|
||||
"",
|
||||
["execute"],
|
||||
"Execute the configuration without requesting data (start at step 5)"
|
||||
);
|
||||
if ($getopts->get("Help")) {
|
||||
die($getopts->help());
|
||||
}
|
||||
}
|
||||
$config->set ("tablesRel", $value);
|
||||
$step++;
|
||||
echo "\n";
|
||||
}
|
||||
|
||||
$tablesRel = $config->get ("tablesRel");
|
||||
if ($step === 3)
|
||||
{
|
||||
echo "#### Entering step 3: ask the relation between fields\n";
|
||||
echo "# For each destination table, if a source table is provided,\n".
|
||||
"# for each field, provide the source SQL request (can be field,\n".
|
||||
"# CONCAT(), text between simple quotes, null...)\n";
|
||||
if ($config->get("fields"))
|
||||
$value = $config->get("fields");
|
||||
else
|
||||
$value = array ();
|
||||
foreach ($tablesRel as $dstTable=>$srcTable)
|
||||
{
|
||||
echo "- Destination Table '$dstTable': ";
|
||||
if ($srcTable === "")
|
||||
{
|
||||
echo "No source table. SKIPPED\n";
|
||||
continue;
|
||||
if ($getopts->get("Step")) {
|
||||
if (
|
||||
intval($getopts->get("Step")) < 0 ||
|
||||
intval($getopts->get("Step")) > 6
|
||||
) {
|
||||
die("Invalid Step provided (Must be between 0 and 6)\n");
|
||||
}
|
||||
}
|
||||
if (empty($getopts->restOfLine())) {
|
||||
throw new \Exception("No configuration file provided");
|
||||
}
|
||||
if (count($getopts->restOfLine()) > 1) {
|
||||
throw new \Exception("Too much configuration file provided");
|
||||
}
|
||||
$configurationFile = current($getopts->restOfLine());
|
||||
if (! file_exists($configurationFile)) {
|
||||
touch($configurationFile);
|
||||
}
|
||||
$config = new Domframework\Configuration();
|
||||
$config->confFile = $configurationFile;
|
||||
|
||||
$db = $config->get("db");
|
||||
$tables = $config->get("tables");
|
||||
$tablesRel = $config->get("tablesRel");
|
||||
$fields = $config->get("fields");
|
||||
$tablesOrder = $config->get("tablesOrder");
|
||||
$deleteContent = $config->get("deleteContent");
|
||||
if ($db["srcDSN"] === "" || $db["dstDSN"] === "") {
|
||||
$step = 0;
|
||||
} elseif (empty($tables)) {
|
||||
$step = 1;
|
||||
} elseif (empty($tablesRel)) {
|
||||
$step = 2;
|
||||
} elseif (empty($fields)) {
|
||||
$step = 3;
|
||||
} elseif (empty($tablesOrder)) {
|
||||
$step = 4;
|
||||
} elseif ($deleteContent === null) {
|
||||
$step = 5;
|
||||
} else {
|
||||
$step = 6;
|
||||
}
|
||||
|
||||
if ($getopts->get("Step")) {
|
||||
$step = intval($getopts->get("Step"));
|
||||
}
|
||||
if ($getopts->get("Execute")) {
|
||||
$step = 5;
|
||||
}
|
||||
|
||||
if ($step === 0) {
|
||||
// Step 0: ask the connection parameters
|
||||
echo "#### Entering step 0 : ask the connection parameters\n";
|
||||
$srcDSN = $this->ask("Enter SRC DSN");
|
||||
$srcUser = $this->ask("Enter SRC User");
|
||||
$srcPassword = $this->ask("Enter SRC Password");
|
||||
$dstDSN = $this->ask("Enter DST DSN");
|
||||
$dstUser = $this->ask("Enter DST User");
|
||||
$dstPassword = $this->ask("Enter DST Password");
|
||||
if ($srcUser === "") {
|
||||
$srcUser = null;
|
||||
$srcPassword = null;
|
||||
}
|
||||
if ($dstUser === "") {
|
||||
$dstUser = null;
|
||||
$dstPassword = null;
|
||||
}
|
||||
$value = [
|
||||
"srcDSN" => $srcDSN,
|
||||
"srcUser" => $srcUser,
|
||||
"srcPassword" => $srcPassword,
|
||||
"dstDSN" => $dstDSN,
|
||||
"dstUser" => $dstUser,
|
||||
"dstPassword" => $dstPassword,];
|
||||
$config->set("db", $value);
|
||||
$db = $config->get("db");
|
||||
$step++;
|
||||
}
|
||||
if ($step === 1) {
|
||||
echo "#### Entering step 1: read the existing databases schemes\n";
|
||||
$srcDB = new Domframework\Dblayeroo(
|
||||
$db["srcDSN"],
|
||||
$db["srcUser"],
|
||||
$db["srcPassword"]
|
||||
);
|
||||
$srcTables = $srcDB->listTables();
|
||||
$dstDB = new Domframework\Dblayeroo(
|
||||
$db["dstDSN"],
|
||||
$db["dstUser"],
|
||||
$db["dstPassword"]
|
||||
);
|
||||
$dstTables = $dstDB->listTables();
|
||||
$value = [
|
||||
"srcTables" => $srcTables,
|
||||
"dstTables" => $dstTables,
|
||||
];
|
||||
$config->set("tables", $value);
|
||||
$step++;
|
||||
echo "\n";
|
||||
}
|
||||
|
||||
echo "#### Connect to the defined databases\n";
|
||||
$srcDB = new Domframework\Dblayeroo(
|
||||
$db["srcDSN"],
|
||||
$db["srcUser"],
|
||||
$db["srcPassword"]
|
||||
);
|
||||
$dstDB = new Domframework\Dblayeroo(
|
||||
$db["dstDSN"],
|
||||
$db["dstUser"],
|
||||
$db["dstPassword"]
|
||||
);
|
||||
echo "\n";
|
||||
$dstSchema = $dstDB->getTableSchema ($dstTable);
|
||||
$srcSchema = $srcDB->getTableSchema ($srcTable);
|
||||
echo " List of the allowed fields: ".
|
||||
implode (", ", array_keys ($srcSchema["fields"]))."\n";
|
||||
foreach (array_keys ($dstSchema["fields"]) as $field)
|
||||
{
|
||||
if (! key_exists ($dstTable, $value))
|
||||
$value[$dstTable] = array ();
|
||||
if (! key_exists ($field, $value[$dstTable]))
|
||||
$value[$dstTable][$field] = "";
|
||||
while (1)
|
||||
{
|
||||
$oldval = (isset ($config->get("fields")[$dstTable][$field])) ?
|
||||
$config->get("fields")[$dstTable][$field] : "";
|
||||
$answer = $this->ask (
|
||||
"Destination Table '$dstTable' field '$field'", $oldval);
|
||||
$value[$dstTable][$field] = $answer;
|
||||
if (trim ($value[$dstTable][$field]) !== "")
|
||||
break;
|
||||
// TODO : Check if the field is valid (think about function)
|
||||
$this->error ("The field source must be defined !");
|
||||
}
|
||||
|
||||
if ($step == 2) {
|
||||
echo "#### Entering step 2: ask the relations between tables\n";
|
||||
$tables = $config->get("tables");
|
||||
echo "# For each destination table, please provide the source table\n";
|
||||
sort($tables["srcTables"]);
|
||||
echo "# Allowed source tables : " . implode(", ", $tables["srcTables"]) .
|
||||
"\n";
|
||||
$value = [];
|
||||
natsort($tables["dstTables"]);
|
||||
foreach ($tables["dstTables"] as $table) {
|
||||
while (1) {
|
||||
$value[$table] = $this->ask(
|
||||
"Destination Table '$table' filled by source table"
|
||||
);
|
||||
if (
|
||||
in_array($value[$table], $tables["srcTables"]) ||
|
||||
$value[$table] === ""
|
||||
) {
|
||||
continue 2;
|
||||
}
|
||||
$this->error("The table '" . $value[$table] . " doesn't exists");
|
||||
echo "# Allowed source tables : " .
|
||||
implode(", ", $tables["srcTables"]) . "\n";
|
||||
}
|
||||
}
|
||||
$config->set("tablesRel", $value);
|
||||
$step++;
|
||||
echo "\n";
|
||||
}
|
||||
$config->set ("fields", $value);
|
||||
}
|
||||
$step++;
|
||||
echo "\n";
|
||||
}
|
||||
$fields = $config->get ("fields");
|
||||
if ($step === 4)
|
||||
{
|
||||
echo "#### Entering step 4: Order the tables by foreign keys\n";
|
||||
echo "# The tables with foreign keys constraint must be populated \n".
|
||||
"# after the tables depending of them\n";
|
||||
$tablesToOrder = array ();
|
||||
foreach ($tablesRel as $dstTable=>$srcTable)
|
||||
{
|
||||
if ($srcTable !== "")
|
||||
$tablesToOrder[] = $dstTable;
|
||||
}
|
||||
while (1)
|
||||
{
|
||||
echo "# Here are the tables to order: ". implode (",", $tablesToOrder).
|
||||
"\n";
|
||||
$tablesOrder = $this->ask ("Order the tables, separated by commas");
|
||||
$tablesOrder = explode (",", $tablesOrder);
|
||||
if (count ($tablesToOrder) === count ($tablesOrder))
|
||||
break;
|
||||
$this->error ("Not all the tables are ordered");
|
||||
}
|
||||
$config->set ("tablesOrder", $tablesOrder);
|
||||
$step++;
|
||||
echo "\n";
|
||||
|
||||
$tablesRel = $config->get("tablesRel");
|
||||
if ($step === 3) {
|
||||
echo "#### Entering step 3: ask the relation between fields\n";
|
||||
echo "# For each destination table, if a source table is provided,\n" .
|
||||
"# for each field, provide the source SQL request (can be field,\n" .
|
||||
"# CONCAT(), text between simple quotes, null...)\n";
|
||||
if ($config->get("fields")) {
|
||||
$value = $config->get("fields");
|
||||
} else {
|
||||
$value = [];
|
||||
}
|
||||
foreach ($tablesRel as $dstTable => $srcTable) {
|
||||
echo "- Destination Table '$dstTable': ";
|
||||
if ($srcTable === "") {
|
||||
echo "No source table. SKIPPED\n";
|
||||
continue;
|
||||
}
|
||||
echo "\n";
|
||||
$dstSchema = $dstDB->getTableSchema($dstTable);
|
||||
$srcSchema = $srcDB->getTableSchema($srcTable);
|
||||
echo " List of the allowed fields: " .
|
||||
implode(", ", array_keys($srcSchema["fields"])) . "\n";
|
||||
foreach (array_keys($dstSchema["fields"]) as $field) {
|
||||
if (! key_exists($dstTable, $value)) {
|
||||
$value[$dstTable] = [];
|
||||
}
|
||||
if (! key_exists($field, $value[$dstTable])) {
|
||||
$value[$dstTable][$field] = "";
|
||||
}
|
||||
while (1) {
|
||||
$oldval = (isset($config->get("fields")[$dstTable][$field])) ?
|
||||
$config->get("fields")[$dstTable][$field] : "";
|
||||
$answer = $this->ask(
|
||||
"Destination Table '$dstTable' field '$field'",
|
||||
$oldval
|
||||
);
|
||||
$value[$dstTable][$field] = $answer;
|
||||
if (trim($value[$dstTable][$field]) !== "") {
|
||||
break;
|
||||
}
|
||||
// TODO : Check if the field is valid (think about function)
|
||||
$this->error("The field source must be defined !");
|
||||
}
|
||||
}
|
||||
$config->set("fields", $value);
|
||||
}
|
||||
$step++;
|
||||
echo "\n";
|
||||
}
|
||||
$fields = $config->get("fields");
|
||||
if ($step === 4) {
|
||||
echo "#### Entering step 4: Order the tables by foreign keys\n";
|
||||
echo "# The tables with foreign keys constraint must be populated \n" .
|
||||
"# after the tables depending of them\n";
|
||||
$tablesToOrder = [];
|
||||
foreach ($tablesRel as $dstTable => $srcTable) {
|
||||
if ($srcTable !== "") {
|
||||
$tablesToOrder[] = $dstTable;
|
||||
}
|
||||
}
|
||||
while (1) {
|
||||
echo "# Here are the tables to order: " . implode(",", $tablesToOrder) .
|
||||
"\n";
|
||||
$tablesOrder = $this->ask("Order the tables, separated by commas");
|
||||
$tablesOrder = explode(",", $tablesOrder);
|
||||
if (count($tablesToOrder) === count($tablesOrder)) {
|
||||
break;
|
||||
}
|
||||
$this->error("Not all the tables are ordered");
|
||||
}
|
||||
$config->set("tablesOrder", $tablesOrder);
|
||||
$step++;
|
||||
echo "\n";
|
||||
}
|
||||
|
||||
$tablesOrder = $config->get("tablesOrder");
|
||||
if ($step === 5) {
|
||||
if ($deleteContent === null) {
|
||||
echo "# The content of the destination tables can be deleted before\n" .
|
||||
"# adding the source data.\n";
|
||||
echo "# Attention: the order is important if there is foreign keys!\n" .
|
||||
"# Remove the most constraint tables before deleting the less\n" .
|
||||
"# constraint ones\n";
|
||||
echo "# List of the destination tables: " .
|
||||
implode(",", array_keys(array_diff($tablesRel, [""]))) .
|
||||
"\n";
|
||||
$deleteContent = $this->ask("Get the list of the tables to be " .
|
||||
"cleaned, separated by comas");
|
||||
$config->set("deleteContent", $deleteContent);
|
||||
$deleteContent = $config->get("deleteContent");
|
||||
}
|
||||
}
|
||||
if ($deleteContent !== "") {
|
||||
echo "#### Entering step 5: delete tables content\n";
|
||||
foreach (explode(",", $deleteContent) as $table) {
|
||||
$table = trim($table);
|
||||
echo "- Delete content of table '$table'\n";
|
||||
$dstDB->table($table)->unique([])->delete()->execute();
|
||||
}
|
||||
$step = 6;
|
||||
echo "\n";
|
||||
} else {
|
||||
echo "#### Entering step5: Skipped delete tables content\n";
|
||||
$step = 6;
|
||||
}
|
||||
if ($step === 6) {
|
||||
echo "#### Entering step 6: populate the destination database with the " .
|
||||
"configured data\n";
|
||||
foreach ($tablesOrder as $dstTable) {
|
||||
echo "- Populate table $dstTable:\n";
|
||||
// Create the source SQL request. Add the alias name of the destination
|
||||
// table to allow to import directely in the destination
|
||||
$sql = "SELECT ";
|
||||
$i = 0;
|
||||
foreach ($fields[$dstTable] as $key => $val) {
|
||||
if ($i > 0) {
|
||||
$sql .= ",";
|
||||
}
|
||||
$sql .= "$val AS $key";
|
||||
$i++;
|
||||
}
|
||||
$sql .= " FROM " . $tablesRel[$dstTable];
|
||||
echo " $sql\n";
|
||||
echo " ";
|
||||
// Define all the variables needed to insert
|
||||
$dstSchema = $dstDB->getTableSchema($dstTable);
|
||||
$dstDB->table($dstTable);
|
||||
$dstDB->fields($dstSchema["fields"]);
|
||||
$dstDB->primary($dstSchema["primary"]);
|
||||
foreach ($srcDB->directQuery($sql) as $row) {
|
||||
//print_r ($row);
|
||||
$dstDB->clearRequest();
|
||||
$dstDB->insert()
|
||||
->setValues($row);
|
||||
if (! $getopts->get("Debug")) {
|
||||
echo ".";
|
||||
} else {
|
||||
echo $dstDB->getDisplayQuery();
|
||||
}
|
||||
$dstDB->execute();
|
||||
}
|
||||
echo "\n";
|
||||
}
|
||||
$step++;
|
||||
}
|
||||
// Last step
|
||||
echo "#### Entering step $step: End of process\n";
|
||||
}
|
||||
|
||||
$tablesOrder = $config->get ("tablesOrder");
|
||||
if ($step === 5)
|
||||
/**
|
||||
* Ask a question to the user and return the answer
|
||||
* @param string $question the question message
|
||||
* @param string|null $proposal The optional pre-filled proposal
|
||||
* @return string The answer
|
||||
*/
|
||||
public function ask($question, $proposal = "")
|
||||
{
|
||||
if ($deleteContent === null)
|
||||
{
|
||||
echo "# The content of the destination tables can be deleted before\n".
|
||||
"# adding the source data.\n";
|
||||
echo "# Attention: the order is important if there is foreign keys!\n".
|
||||
"# Remove the most constraint tables before deleting the less\n".
|
||||
"# constraint ones\n";
|
||||
echo "# List of the destination tables: ".
|
||||
implode (",", array_keys (array_diff ($tablesRel, array ("")))).
|
||||
"\n";
|
||||
$deleteContent = $this->ask ("Get the list of the tables to be ".
|
||||
"cleaned, separated by comas");
|
||||
$config->set ("deleteContent", $deleteContent);
|
||||
$deleteContent = $config->get ("deleteContent");
|
||||
}
|
||||
$console = new Domframework\Console();
|
||||
$console->echo("$question: ");
|
||||
return trim($console->readline($proposal));
|
||||
}
|
||||
if ($deleteContent !== "")
|
||||
{
|
||||
echo "#### Entering step 5: delete tables content\n";
|
||||
foreach (explode (",", $deleteContent) as $table)
|
||||
{
|
||||
$table = trim ($table);
|
||||
echo "- Delete content of table '$table'\n";
|
||||
$dstDB->table ($table)->unique (array ())->delete ()->execute ();
|
||||
}
|
||||
$step = 6;
|
||||
echo "\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
echo "#### Entering step5: Skipped delete tables content\n";
|
||||
$step = 6;
|
||||
}
|
||||
if ($step === 6)
|
||||
{
|
||||
echo "#### Entering step 6: populate the destination database with the ".
|
||||
"configured data\n";
|
||||
foreach ($tablesOrder as $dstTable)
|
||||
{
|
||||
echo "- Populate table $dstTable:\n";
|
||||
// Create the source SQL request. Add the alias name of the destination
|
||||
// table to allow to import directely in the destination
|
||||
$sql = "SELECT ";
|
||||
$i = 0;
|
||||
foreach ($fields[$dstTable] as $key=>$val)
|
||||
{
|
||||
if ($i > 0)
|
||||
$sql .= ",";
|
||||
$sql .= "$val AS $key";
|
||||
$i++;
|
||||
}
|
||||
$sql .= " FROM ".$tablesRel[$dstTable];
|
||||
echo " $sql\n";
|
||||
echo " ";
|
||||
// Define all the variables needed to insert
|
||||
$dstSchema = $dstDB->getTableSchema ($dstTable);
|
||||
$dstDB->table ($dstTable);
|
||||
$dstDB->fields ($dstSchema["fields"]);
|
||||
$dstDB->primary ($dstSchema["primary"]);
|
||||
foreach ($srcDB->directQuery ($sql) as $row)
|
||||
{
|
||||
//print_r ($row);
|
||||
$dstDB->clearRequest ();
|
||||
$dstDB->insert ()
|
||||
->setValues ($row);
|
||||
if (! $getopts->get ("Debug"))
|
||||
echo ".";
|
||||
else
|
||||
echo $dstDB->getDisplayQuery ();
|
||||
$dstDB->execute ();
|
||||
}
|
||||
echo "\n";
|
||||
}
|
||||
$step++;
|
||||
}
|
||||
// Last step
|
||||
echo "#### Entering step $step: End of process\n";
|
||||
}
|
||||
|
||||
/** Ask a question to the user and return the answer
|
||||
* @param string $question the question message
|
||||
* @param string|null $proposal The optional pre-filled proposal
|
||||
* @return string The answer
|
||||
*/
|
||||
public function ask ($question, $proposal = "")
|
||||
{
|
||||
$console = new Domframework\Console ();
|
||||
$console->echo ("$question: ");
|
||||
return trim ($console->readline ($proposal));
|
||||
}
|
||||
|
||||
/** Display an error to the user
|
||||
* @param string $msg The error message to display
|
||||
*/
|
||||
public function error ($msg)
|
||||
{
|
||||
file_put_contents ("php://stderr", "ERROR: ".$msg."\n");
|
||||
}
|
||||
/**
|
||||
* Display an error to the user
|
||||
* @param string $msg The error message to display
|
||||
*/
|
||||
public function error($msg)
|
||||
{
|
||||
file_put_contents("php://stderr", "ERROR: " . $msg . "\n");
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
$main = new \main ();
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
file_put_contents ("php://stderr", $e->getMessage ()."\n");
|
||||
exit (4);
|
||||
try {
|
||||
$main = new \main();
|
||||
} catch (Exception $e) {
|
||||
file_put_contents("php://stderr", $e->getMessage() . "\n");
|
||||
exit(4);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user