-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrescue.php
More file actions
143 lines (120 loc) · 4.12 KB
/
Copy pathrescue.php
File metadata and controls
143 lines (120 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/php
<?php
/**
* Created by PhpStorm.
* User: Stefano "Yoghi" Tamagnini
* Date: 05/04/14
* Time: 18:45.
*/
require 'vendor/autoload.php';
require 'config.php';
use BitPrepared\Asa\Driver\OdsDriver;
use BitPrepared\Asa\Driver\SqliteDriver;
use Monolog\Logger;
use RedBean_Facade as R;
$strict = in_array('--strict', $_SERVER['argv']);
$arguments = new \cli\Arguments(compact('strict'));
$arguments->addFlag(['verbose', 'v'], 'Turn on verbose output');
$arguments->addFlag('version', 'Display the version');
$arguments->addFlag(['quiet', 'q'], 'Disable all output');
$arguments->addFlag(['help', 'h'], 'Show this help screen');
$arguments->addOption(['import-asa', 'i'], [
'default' => getcwd().'/resources/uploads/',
'description' => 'Setta la directory dove importare i soci da formato ods/sqlite', ]);
$arguments->addOption(['export-csv', 'e'], [
'default' => getcwd(),
'description' => 'Setta la directory dove esportare i soci come csv', ]);
$arguments->addFlag(['update-db', 'u'], 'Abilita la sovra-scrittura su db');
$arguments->parse();
if ($arguments['help']) {
echo $arguments->getHelpScreen();
echo "\n\n";
}
$arguments_parsed = $arguments->getArguments();
if (isset($arguments_parsed['verbose'])) {
define('VERBOSE', true);
} else {
define('VERBOSE', false);
}
if (isset($arguments_parsed['quiet'])) {
define('QUIET', true);
} else {
define('QUIET', false);
}
// create a log channel
$log = new Logger('rescue-script');
if (VERBOSE) {
\cli\out($arguments->asJSON()."\n");
if (!QUIET) {
$handler = new \Monolog\Handler\StreamHandler('php://stdout', Logger::DEBUG);
$log->pushHandler($handler);
}
$handler = new \Monolog\Handler\StreamHandler($config['log']['filename'], Logger::DEBUG);
$log->pushHandler($handler);
} else {
if (!QUIET) {
$handler = new \Monolog\Handler\StreamHandler($config['log']['filename'], Logger::INFO);
$log->pushHandler($handler);
} else {
$handler = new \Monolog\Handler\StreamHandler($config['log']['filename'], Logger::WARNING);
$log->pushHandler($handler);
}
}
$dsn = 'mysql:host='.$config['db']['host'].';dbname='.$config['db']['database'];
$username = $config['db']['user'];
$password = $config['db']['password'];
// setto il database di default
R::setup($dsn, $username, $password);
if (isset($arguments_parsed['import-asa'])) {
if ('' == $arguments_parsed['import-asa']) {
$arguments_parsed['import-asa'] = getcwd().'/resources/uploads/';
}
$dir = $arguments_parsed['import-asa'];
if (!is_dir($dir)) {
\cli\out('invalid import directory : '.$dir."\n");
exit -1;
}
$log->addInfo('Inizio scansione directory '.$dir);
$drivers = [];
$filesSqlite = scandir($dir);
foreach ($filesSqlite as $filename) {
if ($filename != '.' && $filename != '..') {
$fullpath = $dir.'/'.$filename;
$pos = strpos($filename, '.sqlite');
if ($pos > 0) {
$drivers[] = new SqliteDriver($log, $fullpath);
} else {
$pos = strpos($filename, '.ods');
if ($pos > 0) {
$drivers[] = new OdsDriver($log, $fullpath);
}
}
}
}
$log->addInfo('Trovati '.count($drivers).' driver da importare');
$R = new RedBean_Facade();
$importer = new \BitPrepared\Asa\Importer($log, $R);
foreach ($drivers as $driver) {
try {
$importer->load($driver);
} catch (Exception $e) {
$message = $e->getMessage();
$log->addError($message);
$log->addError($e->getTraceAsString());
}
}
try {
if (isset($arguments_parsed['update-db'])) {
$log->addInfo('Abilitata la sovra-scrittura su database');
$importer->setUpdate(true);
}
$importer->syncAsaToRescue();
} catch (Exception $e) {
}
}
if (isset($arguments_parsed['export-csv'])) {
if ('' == $arguments_parsed['export-csv']) {
$arguments_parsed['export-csv'] = getcwd();
}
echo 'export : '.$arguments_parsed['export-csv'];
}