webservice/scripts/jobs/sql2csv.php
2014-11-07 15:04:02 +00:00

107 lines
3.3 KiB
PHP

<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
//Use classmap autoloader - useful with opcode and realpath cache
require_once 'Zend/Loader/AutoloaderFactory.php';
require_once 'Zend/Loader/ClassMapAutoloader.php';
Zend_Loader_AutoloaderFactory::factory(array(
'Zend_Loader_ClassMapAutoloader' => array(
__DIR__ . '/../../library/Zend/autoload_classmap.php',
__DIR__ . '/../../library/Application/autoload_classmap.php',
__DIR__ . '/../../library/Scores/autoload_classmap.php',
__DIR__ . '/../../application/autoload_classmap.php',
),
'Zend_Loader_StandardAutoloader' => array(
'prefixes' => array(
'Zend' => __DIR__ . '/../../library/Zend',
'Application' => __DIR__ . '/../../library/Application',
'Scores' => __DIR__ . '/../../library/Scores',
'SdMetier' => __DIR__ . '/../../library/SdMetier',
'Metier' => __DIR__ . '/../../library/Metier',
),
'fallback_autoloader' => true
)
));
// Zend_Application - Use it if you don't have autoloaders
//require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
try {
$opts = new Zend_Console_Getopt(
//Options
array(
'help|?' => "Displays usage information.",
'sqlfile=s' => "",
'csvfile=s' => "",
)
);
$opts->parse();
} catch (Zend_Console_Getopt_Exception $e) {
echo $e->getUsageMessage();
exit;
}
//Usage
if(isset($opts->help))
{
echo "\n";
echo $opts->getUsageMessage();
echo "Génération d'un fichier CSV à partir d'un fichier SQL.";
echo "\n";
exit;
}
$c = new Zend_Config($application->getOptions());
try {
//$dbMaster = Zend_Db::factory($c->resources->db);
$dbSlave = Zend_Db::factory(new Zend_Config(array(
'adapter' => 'mysqli',
'params' => array(
'host' => '195.154.170.169:53336',
'username' => 'root',
'password' => 'scores',
'dbname' => 'sdv1',
'driver_options' => array(
MYSQLI_INIT_COMMAND => "SET NAMES utf8",
),
)
)));
} catch (Zend_Db_Exception $e) {
$e->getMessage();
}
$sql = file_get_contents($opts->sqlfile);
$rows = $dbSlave->fetchAll($sql);
if (count($rows) > 0) {
$fp = fopen($opts->csvfile.'.tmp', 'w');
//Write header
$headers = array_keys($rows[0]);
fputcsv($fp, $headers, ',', '"');
//Write content
foreach($rows as $fields) {
fputcsv($fp, $fields, ',', '"');
}
fclose($fp);
rename($opts->csvfile.'.tmp', $opts->csvfile);
} else {
file_put_contents($opts->csvfile, '');
}