db->autoCommit(false);
$f->addToLog("app/om_filestorage_migrate.php: db->autocommit(false);", VERBOSE_MODE);
//
$files = array(
"om_logo" => array(
"pkey" => array(
"field" => "om_logo",
"type" => "N",
),
"file_fields" => array("fichier", ),
),
"plans" => array(
"pkey" => array(
"field" => "plans",
"type" => "N",
),
"file_fields" => array("fichier", ),
),
"dossier" => array(
"pkey" => array(
"field" => "dossier",
"type" => "N",
),
"file_fields" => array("fichier", ),
),
);
//
$source_conf = array(
"storage" => "deprecated",
"path" => "../trs/1/",
);
require_once PATH_OPENMAIRIE."om_filestorage_".$source_conf["storage"].".class.php";
$class_name = 'filestorage_'.$source_conf["storage"];
$storage_source = new $class_name($source_conf);
//
$destination_conf = array(
"storage" => "filesystem",
"path" => "../trs/1/",
);
require_once PATH_OPENMAIRIE."om_filestorage_".$destination_conf["storage"].".class.php";
$class_name = 'filestorage_'.$destination_conf["storage"];
$storage_destination = new $class_name($destination_conf);
//
foreach ($files as $table => $params) {
//
$error = false;
//
$uids_created = array();
//
$old_files_to_delete = array();
//
foreach ($params["file_fields"] as $field) {
//
echo "
";
echo $table." => ".$field;
echo "
";
//
echo "";
//
$sql = " SELECT ".$table.".".$params["pkey"]["field"].", ".$table.".".$field." ";
$sql .= " FROM ".DB_PREFIXE.$table." ";
$res = $f->db->query($sql);
$f->addToLog("app/om_filestorage_migrate.php: db->query(\"".$sql."\");", VERBOSE_MODE);
if ($f->isDatabaseError($res, true) === true) {
$error = true;
break;
}
//
while ($row =& $res->fetchRow(DB_FETCHMODE_ASSOC)) {
//
echo "- ";
//
echo $row[$table];
echo "-";
echo $row[$field];
// On essaye de récupérer le fichier source pour vérifier qu'il n'y
// a pas d'erreur
$get = $storage_source->get($row[$field]);
//
if ($get === NULL) {
$error = true;
break;
}
$path = $storage_source->getPath($row[$field]);
$metadatas = $storage_source->getInfo($row[$field]);
$old_files_to_delete[] = $row[$field];
echo "-";
echo $path;
// On crée le nouveau fichier
$new_uid = $storage_destination->create($path, $metadatas, "from_path");
$uids_created[] = $new_uid;
echo "-";
echo $new_uid;
// On met à jour l'enregistrement en question pour mettre à jour
// l'identifiant du fichier
$val = array($field => $new_uid);
$key = "".$table.".".$params["pkey"]["field"]."=";
if ($params["pkey"]["type"] == "A") {
$key .= "'".$row[$table]."'";
} else {
$key .= $row[$table];
}
$res1 = $f->db->autoExecute(DB_PREFIXE.$table, $val, DB_AUTOQUERY_UPDATE, $key);
$f->addToLog("app/om_filestorage_migrate.php: db->autoexecute(\"".DB_PREFIXE.$table."\", ".print_r($val, true).", DB_AUTOQUERY_UPDATE, \"".$key."\");", VERBOSE_MODE);
if ($f->isDatabaseError($res1, true) == true) {
$error = true;
break;
}
//
echo "
";
}
//
echo "
";
}
//
if ($error === true || $error === NULL) {
//
$f->db->rollback();
$f->addToLog("app/om_filestorage_migrate.php: db->rollback();", VERBOSE_MODE);
//
foreach($uids_created as $uid_created) {
//
$storage_destination->delete($uid_created);
}
} else {
//
$f->db->commit();
$f->addToLog("app/om_filestorage_migrate.php: db->commit();", VERBOSE_MODE);
//
$old_files_to_delete = array_unique($old_files_to_delete);
foreach($old_files_to_delete as $old_file_to_delete) {
//
$storage_source->delete($old_file_to_delete);
}
}
}
?>