versionecologique/scripts/import_images.php
2017-07-24 10:04:12 +02:00

245 lines
6.5 KiB
PHP

<?php
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
require_once dirname(__FILE__).'/../www/config/config.inc.php';
// Old database
$configOld = array(
'dsn' => 'mysql:dbname=versionecologique_old;host=192.168.0.41;charset=UTF8',
'user' => 'root',
'pass' => '',
);
$dbOld = new PDO($configOld['dsn'], $configOld['user'], $configOld['pass']);
// New database
$configNew = array(
'dsn' => 'mysql:dbname='._DB_NAME_.';host='._DB_SERVER_.';charset=UTF8',
'user' => _DB_USER_,
'pass' => _DB_PASSWD_,
);
$dbNew = new PDO($configNew['dsn'], $configNew['user'], $configNew['pass']);
// Get products id already imported
$sql = "SELECT id_product AS id_product FROM ps_product";
$stmtNew = $dbNew->query($sql);
if ($stmtNew->rowCount() == 0) {
echo "No products ?\n"; exit;
} else {
while($item = $stmtNew->fetch(PDO::FETCH_OBJ)) {
$products[] = $item->id_product;
}
}
// ps_image_type => type des images
// Truncate
$tables = array(
'ps_image',
'ps_image_lang',
'ps_image_shop',
);
foreach($tables as $table) {
$dbNew->query('TRUNCATE TABLE '.$table);
}
$image_ids = array();
// ps_image
$table = 'ps_image';
$cols = array(
'id_image',
'id_product',
'position',
'cover',
);
echo "Importing $table...\n";
$sql = "SELECT * FROM ".$table." WHERE id_product IN (".join(',', $products).")";
$stmtOld = $dbOld->query($sql);
if ($stmtOld->rowCount() == 0) {
echo "No Results ?\n";
} else {
try {
// Construct SQL
$sql = 'INSERT INTO '.$table.' (';
foreach ($cols as $c) {
$sql.= '`'.$c.'`, ';
}
$sql = trim($sql);
$sql = trim($sql, ',');
$sql.= ') VALUES (';
foreach ($cols as $c) {
$sql.= ':'.$c.', ';
}
$sql = trim($sql);
$sql = trim($sql, ',');
$sql.= ')';
// Start transaction
$dbNew->beginTransaction();
$i = 0;
while($item = $stmtOld->fetch(PDO::FETCH_OBJ)) {
$stmtImport = $dbNew->prepare($sql);
foreach ($cols as $c) {
$stmtImport->bindValue($c, $item->{$c});
}
if ($stmtImport->execute() === false) {
echo "Error on $table - $i\n";
exit;
}
$i++;
$image_ids[] = $item->id_image;
}
if ($dbNew->commit() === false) {
echo "Error on $table\n";
exit;
}
} catch(Exception $e) {
$dbNew->rollBack();
echo "Error on $table\n";
exit;
}
echo $stmtOld->rowCount(). " / " . $i . "\n";
}
echo "End of importing $table\n";
// Recreate images
if (count($image_ids) > 0) {
$i = 0;
foreach ($image_ids as $id) {
$image = new Image($id);
$imageFilePath = $image->getPathForCreation().'.'.$image->image_format;
$imageFileOld = realpath(__DIR__).'/../import_images/p/'.$image->getImgFolder().$id.'.'.$image->image_format;
$result = ImageManager::resize($imageFileOld, $imageFilePath);
$imagesTypes = ImageType::getImagesTypes('products');
foreach ($imagesTypes as $imageType) {
$intResult = ImageManager::resize($imageFileOld, $imageFilePath.'-'.stripslashes($imageType['name']).'.'.$image->image_format,
$imageType['width'], $imageType['height'], $image->image_format);
$result = $result & $intResult;
}
if ($result === false) {
echo "Error on image $id - $i/".count($image_ids)."\n";
exit;
}
$i++;
echo $i."/".count($image_ids)."\n";
}
}
// ps_image_shop
$table = 'ps_image_shop';
$cols = array(
'id_product',
'id_image',
'id_shop',
'cover',
);
echo "Importing $table...\n";
$sql = "SELECT * FROM ".$table." WHERE id_product IN (".join(',', $products).")";
$stmtOld = $dbOld->query($sql);
if ($stmtOld->rowCount() == 0) {
echo "No Results ?\n";
} else {
try {
// Construct SQL
$sql = 'INSERT INTO '.$table.' (';
foreach ($cols as $c) {
$sql.= '`'.$c.'`, ';
}
$sql = trim($sql);
$sql = trim($sql, ',');
$sql.= ') VALUES (';
foreach ($cols as $c) {
$sql.= ':'.$c.', ';
}
$sql = trim($sql);
$sql = trim($sql, ',');
$sql.= ')';
// Start transaction
$dbNew->beginTransaction();
$i = 0;
while($item = $stmtOld->fetch(PDO::FETCH_OBJ)) {
$stmtImport = $dbNew->prepare($sql);
foreach ($cols as $c) {
$stmtImport->bindValue($c, $item->{$c});
}
if ($stmtImport->execute() === false) {
echo "Error on $table - $i\n";
exit;
}
$i++;
}
if ($dbNew->commit() === false) {
echo "Error on $table\n";
exit;
}
} catch(Exception $e) {
$dbNew->rollBack();
echo "Error on $table\n";
exit;
}
echo $stmtOld->rowCount(). " / " . $i . "\n";
}
echo "End of importing $table\n";
// ps_image_lang
$table = 'ps_image_lang';
$cols = array(
'id_image',
'id_lang',
'legend',
);
echo "Importing $table...\n";
$sql = "SELECT * FROM ".$table." WHERE id_image IN (".join(',', $image_ids).")";
$stmtOld = $dbOld->query($sql);
if ($stmtOld->rowCount() == 0) {
echo "No Results ?\n";
} else {
try {
// Construct SQL
$sql = 'INSERT INTO '.$table.' (';
foreach ($cols as $c) {
$sql.= '`'.$c.'`, ';
}
$sql = trim($sql);
$sql = trim($sql, ',');
$sql.= ') VALUES (';
foreach ($cols as $c) {
$sql.= ':'.$c.', ';
}
$sql = trim($sql);
$sql = trim($sql, ',');
$sql.= ')';
// Start transaction
$dbNew->beginTransaction();
$i = 0;
while($item = $stmtOld->fetch(PDO::FETCH_OBJ)) {
$stmtImport = $dbNew->prepare($sql);
foreach ($cols as $c) {
$stmtImport->bindValue($c, $item->{$c});
}
if ($stmtImport->execute() === false) {
echo "Error on $table - $i\n";
exit;
}
$i++;
$image_ids[] = $item->id_image;
}
if ($dbNew->commit() === false) {
echo "Error on $table\n";
exit;
}
} catch(Exception $e) {
$dbNew->rollBack();
echo "Error on $table\n";
exit;
}
echo $stmtOld->rowCount(). " / " . $i . "\n";
}
echo "End of importing $table\n";