db->autoCommit(false);
$f->addToLog("app/om_filestorage_migrate.php: db->autocommit(false);", VERBOSE_MODE);
//
$files = array(
"commission" => array(
"pkey" => array(
"field" => "commission",
"type" => "N",
),
"file_fields" => array("om_fichier_commission_ordre_jour" => "NULL", "om_fichier_commission_compte_rendu" => "NULL", ),
),
"consultation" => array(
"pkey" => array(
"field" => "consultation",
"type" => "N",
),
"file_fields" => array("om_fichier_consultation" => "NULL", "fichier" => "NULL", ),
),
"document_numerise" => array(
"pkey" => array(
"field" => "document_numerise",
"type" => "N",
),
"file_fields" => array("uid" => "NULL", )
),
"instruction" => array(
"pkey" => array(
"field" => "instruction",
"type" => "N",
),
"file_fields" => array("om_fichier_instruction" => "NULL"),
),
"om_logo" => array(
"pkey" => array(
"field" => "om_logo",
"type" => "N",
),
"file_fields" => array("fichier" => "NULL", ),
),
"rapport_instruction" => array(
"pkey" => array(
"field" => "rapport_instruction",
"type" => "N",
),
"file_fields" => array("om_fichier_rapport_instruction" => "NULL", ),
),
);
//
$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);
//
$uids_created = array();
//
$old_files_to_delete = array();
//
foreach ($files as $table => $params) {
//
$error = false;
//
$uids_created_table = array();
//
$old_files_to_delete_table = array();
foreach ($params["file_fields"] as $field => $constraint ) {
//
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)) {
//Si cet identifiant de fichier existe ailleurs
if ( in_array($row[$field], $old_files_to_delete)){
$key = array_search($row[$field], $old_files_to_delete);
$uids_created[] = $uids_created[$key];
$uids_created_table[] = $uids_created[$key];
continue;
}
// On essaye de récupérer le fichier source, si le champ est non null ou
// qu'il est obligatoire, pour vérifier qu'il n'y a pas d'erreur
if ( strcmp($constraint, "NOT NULL") === 0 ||
!empty($row[$field]) ){
$get = $storage_source->get($row[$field]);
}
else{
continue;
}
//
echo "- ";
//
echo $row[$params["pkey"]["field"]];
echo "-";
echo $row[$field];
//
if ($get === NULL) {
continue;
}
$path = $storage_source->getPath($row[$field]);
$base_metadata = array(
"dossier" => "",
"dossier_version" => '',
"numDemandeAutor" => "",
"anneemoisDemandeAutor" => "",
"typeInstruction" => "",
"statutAutorisation" => "",
"typeAutorisation" => "",
"dateEvenementDocument" => date("Y-m-d"),
"groupeInstruction" => '',
"title" => '',
"codeProduit" => "OpenADS",
);
$metadatas = $storage_source->getInfo($row[$field]);
$metadatas = array_merge($base_metadata, $metadatas);
$old_files_to_delete[] = $row[$field];
$old_files_to_delete_table[] = $row[$field];
echo "-";
echo $path;
// On crée le nouveau fichier
$new_uid = $storage_destination->create($path, $metadatas, "from_path");
if ( $new_uid === NULL||$new_uid==="" ){
echo _("La creation a echouee");
continue;
}
$uids_created[] = $new_uid;
$uids_created_table[] = $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_table as $uid_created_table) {
//
$storage_destination->delete($uid_created_table);
}
} else {
//
$f->db->commit();
$f->addToLog("app/om_filestorage_migrate.php: db->commit();", VERBOSE_MODE);
//
$old_files_to_delete_table = array_unique($old_files_to_delete_table);
foreach($old_files_to_delete_table as $old_file_to_delete_table) {
//
$storage_source->delete($old_file_to_delete_table);
}
}
}
?>