Skip to content
Snippets Groups Projects
Verified Commit 95012978 authored by Florian Azizian's avatar Florian Azizian
Browse files

FEAT #15049 TIME 4 batch purge resources + migration script

parent 64b13e17
No related branches found
No related tags found
No related merge requests found
......@@ -36,6 +36,7 @@
"certificateSSL": "",
"userAgent": "service",
"statusReplyReceived": "REPLY_SEDA",
"statusMailToPurge": "REPLY_SEDA",
"externalSAE": {
"retentionRules": [
{
......
......@@ -162,3 +162,165 @@ function Bt_getReply($args = [])
return ['response' => $curlResponse['response']];
}
function Bt_purgeAll($args = [])
{
if (!empty($args['resources'])) {
$resources = \SrcCore\models\DatabaseModel::select([
'select' => ['d.path_template', 'r.path', 'r.filename'],
'table' => ['res_letterbox r', 'docservers d'],
'left_join' => ['r.docserver_id = d.docserver_id'],
'where' => ['res_id in (?)'],
'data' => [$args['resources']]
]);
foreach ($resources as $resource) {
$pathToDocument = $resource['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $resource['path']) . $resource['filename'];
if (is_file($pathToDocument)) {
unlink($pathToDocument);
}
}
$resources = \SrcCore\models\DatabaseModel::select([
'select' => ['d.path_template', 'r.path', 'r.filename'],
'table' => ['res_attachments r', 'docservers d'],
'left_join' => ['r.docserver_id = d.docserver_id'],
'where' => ['res_id in (?)'],
'data' => [$args['resources']]
]);
foreach ($resources as $resource) {
$pathToDocument = $resource['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $resource['path']) . $resource['filename'];
if (is_file($pathToDocument)) {
unlink($pathToDocument);
}
}
$resources = \SrcCore\models\DatabaseModel::select([
'select' => ['d.path_template', 'adrpath', 'adrfilename'],
'table' => ['adr_letterbox adr', 'docservers d'],
'left_join' => ['adrdocserver_id = d.docserver_id'],
'where' => ['res_id in (?)'],
'data' => [$args['resources']]
]);
foreach ($resources as $resource) {
$pathToDocument = $resource['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $resource['path']) . $resource['filename'];
if (is_file($pathToDocument)) {
unlink($pathToDocument);
}
}
$resources = \SrcCore\models\DatabaseModel::select([
'select' => ['d.path_template', 'adrpath', 'adrfilename'],
'table' => ['adr_attachments', 'docservers d'],
'left_join' => ['adrdocserver_id = d.docserver_id'],
'where' => ['res_id in (?)'],
'data' => [$args['resources']]
]);
foreach ($resources as $resource) {
$pathToDocument = $resource['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $resource['path']) . $resource['filename'];
if (is_file($pathToDocument)) {
unlink($pathToDocument);
}
}
\SrcCore\models\DatabaseModel::delete([
'table' => 'adr_letterbox',
'where' => ['res_id in (?)'],
'data' => [$args['resources']]
]);
\SrcCore\models\DatabaseModel::delete([
'table' => 'acknowledgement_receipts',
'where' => ['res_id in (?)'],
'data' => [$args['resources']]
]);
\SrcCore\models\DatabaseModel::delete([
'table' => 'listinstance',
'where' => ['res_id in (?)'],
'data' => [$args['resources']]
]);
\SrcCore\models\DatabaseModel::delete([
'table' => 'listinstance_history',
'where' => ['res_id in (?)'],
'data' => [$args['resources']]
]);
\SrcCore\models\DatabaseModel::delete([
'table' => 'listinstance_history_details',
'where' => ['res_id in (?)'],
'data' => [$args['resources']]
]);
\SrcCore\models\DatabaseModel::delete([
'table' => 'registered_mail_resources',
'where' => ['res_id in (?)'],
'data' => [$args['resources']]
]);
\SrcCore\models\DatabaseModel::delete([
'table' => 'res_letterbox',
'where' => ['res_id in (?)'],
'data' => [$args['resources']]
]);
\SrcCore\models\DatabaseModel::delete([
'table' => 'res_mark_as_read',
'where' => ['res_id in (?)'],
'data' => [$args['resources']]
]);
\SrcCore\models\DatabaseModel::delete([
'table' => 'resource_contacts',
'where' => ['res_id in (?)'],
'data' => [$args['resources']]
]);
\SrcCore\models\DatabaseModel::delete([
'table' => 'resources_folders',
'where' => ['res_id in (?)'],
'data' => [$args['resources']]
]);
\SrcCore\models\DatabaseModel::delete([
'table' => 'resources_tags',
'where' => ['res_id in (?)'],
'data' => [$args['resources']]
]);
\SrcCore\models\DatabaseModel::delete([
'table' => 'unit_identifier',
'where' => ['res_id in (?)'],
'data' => [$args['resources']]
]);
\SrcCore\models\DatabaseModel::delete([
'table' => 'users_followed_resources',
'where' => ['res_id in (?)'],
'data' => [$args['resources']]
]);
\SrcCore\models\DatabaseModel::delete([
'table' => 'message_exchange',
'where' => ['res_id_master in (?)'],
'data' => [$args['resources']]
]);
\SrcCore\models\DatabaseModel::delete([
'table' => 'res_attachments',
'where' => ['res_id_master in (?)'],
'data' => [$args['resources']]
]);
\SrcCore\models\DatabaseModel::delete([
'table' => 'shippings',
'where' => ['document_id in (?)'],
'data' => [$args['resources']]
]);
\SrcCore\models\DatabaseModel::delete([
'table' => 'notes',
'where' => ['identifier in (?)'],
'data' => [$args['resources']]
]);
\SrcCore\models\DatabaseModel::delete([
'table' => 'note_entities',
'where' => ['note_id in (select id from notes where identifier in (?))'],
'data' => [$args['resources']]
]);
\SrcCore\models\DatabaseModel::delete([
'table' => 'adr_attachments',
'where' => ['res_id in (select res_id from res_attachments where res_id_master in (?))'],
'data' => [$args['resources']]
]);
\SrcCore\models\DatabaseModel::delete([
'table' => 'emails',
'where' => ['document->>\''.$args['resources'].'\''],
'data' => []
]);
}
}
......@@ -114,15 +114,15 @@ Bt_writeLog(['level' => 'INFO', 'message' => 'Retrieve mail sent to archiving sy
$acknowledgements = \Attachment\models\AttachmentModel::get([
'select' => ['res_id_master'],
'where' => ['attachment_type = ?'],
'data' => ['acknowledgement_record_management']
'where' => ['attachment_type = ?', 'status = ?'],
'data' => ['acknowledgement_record_management', 'TRA']
]);
$acknowledgements = array_column($acknowledgements, 'res_id_master');
$replies = \Attachment\models\AttachmentModel::get([
'select' => ['res_id_master'],
'where' => ['attachment_type = ?'],
'data' => ['reply_record_management']
'where' => ['attachment_type = ?', 'status = ?'],
'data' => ['reply_record_management', 'TRA']
]);
$replies = array_column($replies, 'res_id_master');
$pendingResources = array_diff($acknowledgements, $replies);
......
<?php
/**
* Copyright Maarch since 2008 under licence GPLv3.
* See LICENCE.txt file at the root folder for more details.
* This file is part of Maarch software.
*
*/
/**
* @brief Check all replies in record management system
* @author dev@maarch.org
*/
/**
* @brief Class to include the file error
*
*/
class IncludeFileError extends Exception
{
public function __construct($file)
{
$this->file = $file;
parent :: __construct('Include File \'$file\' is missing!', 1);
}
}
// Globals variables definition
$GLOBALS['batchName'] = 'endOfLifeCycle';
$GLOBALS['wb'] = '';
$totalProcessedResources = 0;
// Load tools
include('batch_tools.php');
$options = getopt("c:", ["config:"]);
if (empty($options['c']) && empty($options['config'])) {
print("Configuration file missing\n");
exit(101);
} elseif (!empty($options['c']) && empty($options['config'])) {
$options['config'] = $options['c'];
unset($options['c']);
}
$txt = '';
foreach (array_keys($options) as $key) {
if (isset($options[$key]) && $options[$key] == false) {
$txt .= $key . '=false,';
} else {
$txt .= $key . '=' . $options[$key] . ',';
}
}
print($txt . "\n");
$GLOBALS['configFile'] = $options['config'];
print("Load json config file:" . $GLOBALS['configFile'] . "\n");
// Tests existence of config file
if (!file_exists($GLOBALS['configFile'])) {
print(
"Configuration file " . $GLOBALS['configFile']
. " does not exist\n"
);
exit(102);
}
$file = file_get_contents($GLOBALS['configFile']);
$file = json_decode($file, true);
if (empty($file)) {
print("Error on loading config file:" . $GLOBALS['configFile'] . "\n");
exit(103);
}
// Load config
$config = $file['config'];
$GLOBALS['MaarchDirectory'] = $config['maarchDirectory'];
$GLOBALS['customId'] = $config['customID'];
$GLOBALS['applicationUrl'] = $config['maarchUrl'];
$GLOBALS['batchDirectory'] = $GLOBALS['MaarchDirectory'] . 'bin/exportSeda';
$config = $file['exportSeda'];
$GLOBALS['statusMailToPurge'] = $config['statusMailToPurge'];
chdir($GLOBALS['MaarchDirectory']);
set_include_path(get_include_path() . PATH_SEPARATOR . $GLOBALS['MaarchDirectory']);
try {
Bt_myInclude($GLOBALS['MaarchDirectory'] . 'vendor/autoload.php');
} catch (IncludeFileError $e) {
Bt_writeLog(['level' => 'ERROR', 'message' => 'Problem with the php include path:' .$e .' '. get_include_path()]);
exit();
}
\SrcCore\models\DatabasePDO::reset();
new \SrcCore\models\DatabasePDO(['customId' => $GLOBALS['customId']]);
$GLOBALS['errorLckFile'] = $GLOBALS['batchDirectory'] . DIRECTORY_SEPARATOR . $GLOBALS['batchName'] .'_error.lck';
$GLOBALS['lckFile'] = $GLOBALS['batchDirectory'] . DIRECTORY_SEPARATOR . $GLOBALS['batchName'] . '.lck';
if (file_exists($GLOBALS['errorLckFile'])) {
Bt_writeLog(['level' => 'ERROR', 'message' => 'Error persists, please solve this before launching a new batch']);
exit(13);
}
Bt_getWorkBatch();
Bt_writeLog(['level' => 'INFO', 'message' => 'Retrieve mail to purge']);
$wherePurge = ['retention_frozen = ?', 'duration_current_use is not null', 'creation_date + interval \'1 day\' * duration_current_use < CURRENT_TIMESTAMP'];
$dataPurge = ['false'];
if (!empty($GLOBALS['statusMailToPurge'])) {
$wherePurge[] = 'status = ?';
$dataPurge[] = $GLOBALS['statusMailToPurge'];
}
$tmpWhere = ['binding = null and action_current_use = ?'];
$tmpData = ['delete'];
$bindingDocument = \Parameter\models\ParameterModel::getById(['select' => ['param_value_string'], 'id' => 'bindingDocumentFinalAction']);
if ($bindingDocument == 'delete') {
$tmpWhere[] = 'binding = ?';
$tmpData[] = 'true';
}
$nonBindingDocument = \Parameter\models\ParameterModel::getById(['select' => ['param_value_string'], 'id' => 'nonBindingDocumentFinalAction']);
if ($nonBindingDocument == 'delete') {
$tmpWhere[] = 'binding = ?';
$tmpData[] = 'false';
}
$replies = \Attachment\models\AttachmentModel::get([
'select' => ['res_id', 'res_id_master', 'path', 'filename', 'docserver_id', 'fingerprint'],
'where' => ['attachment_type = ?', 'status = ?'],
'data' => ['reply_record_management', 'TRA']
]);
$resIdMaster = [];
foreach ($replies as $reply) {
$docserver = \Docserver\models\DocserverModel::getByDocserverId(['docserverId' => $reply['docserver_id'], 'select' => ['path_template', 'docserver_type_id']]);
if (empty($docserver['path_template']) || !file_exists($docserver['path_template'])) {
Bt_writeLog(['level' => 'WARNING', 'message' => 'Docserver does not exists (' . $reply['docserver_id'] . ') for attachment res_id : ' . $reply['res_id']]);
continue;
}
$pathToDocument = $docserver['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $reply['path']) . $reply['filename'];
if (!is_file($pathToDocument)) {
Bt_writeLog(['level' => 'WARNING', 'message' => 'File does not exists (' . $pathToDocument . ') for attachment res_id : ' . $reply['res_id']]);
continue;
}
$replyXml = @simplexml_load_file($pathToDocument);
if (empty($replyXml)) {
Bt_writeLog(['level' => 'WARNING', 'message' => 'Reply is not readable for attachment res_id : ' . $reply['res_id']]);
continue;
}
$messageExchange = \MessageExchange\models\MessageExchangeModel::getMessageByReference(['select' => ['message_id'], 'reference' => (string)$replyXml->MessageReceivedIdentifier]);
if (empty($messageExchange)) {
Bt_writeLog(['level' => 'WARNING', 'message' => 'Reply is not readable for this reference : ' . (string)$replyXml->MessageReceivedIdentifier]);
continue;
}
$unitIdentifier = \MessageExchange\models\MessageExchangeModel::getUnitIdentifierByResId(['select' => ['message_id'], 'resId' => $args['resId']]);
if ($unitIdentifier['message_id'] != $messageExchange['message_id']) {
Bt_writeLog(['level' => 'WARNING', 'message' => 'Wrong reply for attachment res_id : ' . $reply['res_id']]);
continue;
}
$resIdMaster[] = $reply['res_id_master'];
}
if (!empty($resIdMaster)) {
$tmpWhere[] = 'res_id in (?)';
$tmpData[] = $resIdMaster;
}
$wherePurge[] = '(' . implode(") or (", $tmpWhere) . ')';
$dataPurge = array_merge($dataPurge, $tmpData);
$resources = \SrcCore\models\DatabaseModel::select([
'select' => ['res_id'],
'table' => ['res_letterbox r', 'doctypes d'],
'left_join' => ['r.type_id = d.type_id'],
'where' => $wherePurge,
'data' => $dataPurge
]);
$resources = array_column($resources, 'res_id');
Bt_purgeAll(['resources' => $resources]);
Bt_writeLog(['level' => 'INFO', 'message' => 'End of process']);
Bt_writeLog(['level' => 'INFO', 'message' => $nbMailsPurge.' document(s) retrieved']);
Bt_logInDataBase($nbMailsPurge, $err, $nbMailsPurge.' mail(s) purge');
Bt_updateWorkBatch();
exit($GLOBALS['exitCode']);
#!/bin/sh
cd /var/www/html/MaarchCourrier/bin/exportSeda/
filePath='/var/www/html/MaarchCourrier/bin/exportSeda/purge.php'
php $filePath -c /var/www/html/MaarchCourrier/apps/maarch_entreprise/xml/config.json
......@@ -48,6 +48,7 @@ foreach ($customs as $custom) {
$exportSeda['certificateSSL'] = (string)$loadedExportSedaXml->CONFIG->certificateSSL;
$exportSeda['userAgent'] = (string)$loadedExportSedaXml->CONFIG->userAgent;
$exportSeda['statusReplyReceived'] = 'REPLY_SEDA';
$exportSeda['statusMailToPurge'] = 'REPLY_SEDA';
$exportSeda['M2M']['gec'] = 'maarch_courrier';
$file['exportSeda'] = $exportSeda;
......@@ -55,6 +56,9 @@ foreach ($customs as $custom) {
fwrite($fp, json_encode($file, JSON_PRETTY_PRINT));
fclose($fp);
createScriptGetAllReplies();
createScriptPurge();
$migrated++;
}
}
......@@ -65,3 +69,63 @@ foreach ($nonReadableFiles as $file) {
}
printf($migrated . " custom(s) avec config.xml (export seda) trouvé(s) et migré(s).\n");
function createScriptGetAllReplies()
{
$corePath = str_replace('migration/20.10', '', __DIR__);
$config = $corePath . \SrcCore\models\CoreConfigModel::getConfigPath();
if (!empty($GLOBALS['customId'])) {
$folderScript = $corePath.'custom/'.$GLOBALS['customId'].'/bin/exportSeda/scripts/';
if (!file_exists($folderScript)) {
mkdir($folderScript, 0777, true);
}
$scriptPath = $folderScript . 'checkAllReplies.sh';
} else {
$scriptPath = $corePath.'bin/exportSeda/scripts/checkAllReplies.sh';
}
$fileToOpen = fopen($scriptPath, 'w+');
fwrite($fileToOpen, '#!/bin/sh');
fwrite($fileToOpen, "\n");
fwrite($fileToOpen, 'cd ' . $corePath . 'bin/exportSeda/');
fwrite($fileToOpen, "\n");
fwrite($fileToOpen, 'filePath=\''.$corePath.'bin/exportSeda/checkAllReplies.php\'');
fwrite($fileToOpen, "\n");
fwrite($fileToOpen, 'php $filePath -c ' . $config);
fwrite($fileToOpen, "\n");
fclose($fileToOpen);
shell_exec('chmod +x '. $scriptPath);
printf("Si le script checkAllReply.php est lancé dans la crontab, il faut modifier le chemin comme ceci : " . $scriptPath . "\n");
}
function createScriptPurge()
{
$corePath = str_replace('migration/20.10', '', __DIR__);
$config = $corePath . \SrcCore\models\CoreConfigModel::getConfigPath();
if (!empty($GLOBALS['customId'])) {
$folderScript = $corePath.'custom/'.$GLOBALS['customId'].'/bin/exportSeda/scripts/';
if (!file_exists($folderScript)) {
mkdir($folderScript, 0777, true);
}
$scriptPath = $folderScript . 'purge.sh';
} else {
$scriptPath = $corePath.'bin/exportSeda/scripts/purge.sh';
}
$fileToOpen = fopen($scriptPath, 'w+');
fwrite($fileToOpen, '#!/bin/sh');
fwrite($fileToOpen, "\n");
fwrite($fileToOpen, 'cd ' . $corePath . 'bin/exportSeda/');
fwrite($fileToOpen, "\n");
fwrite($fileToOpen, 'filePath=\''.$corePath.'bin/exportSeda/purge.php\'');
fwrite($fileToOpen, "\n");
fwrite($fileToOpen, 'php $filePath -c ' . $config);
fwrite($fileToOpen, "\n");
fclose($fileToOpen);
shell_exec('chmod +x '. $scriptPath);
printf("Si le script Purge.php est lancé dans la crontab, il faut modifier le chemin comme ceci : " . $scriptPath . "\n");
}
......@@ -221,25 +221,25 @@ trait ExportSEDATrait
return [];
}
private static function saveMessage($args = [])
public static function saveMessage($args = [])
{
$data = new stdClass();
$data = new \stdClass();
$data->messageId = $args['messageObject']->MessageIdentifier->value;
$data->date = $args['messageObject']->Date;
$data->MessageIdentifier = new stdClass();
$data->MessageIdentifier = new \stdClass();
$data->MessageIdentifier->value = $args['messageObject']->MessageIdentifier->value;
$data->TransferringAgency = new stdClass();
$data->TransferringAgency->Identifier = new stdClass();
$data->TransferringAgency = new \stdClass();
$data->TransferringAgency->Identifier = new \stdClass();
$data->TransferringAgency->Identifier->value = $args['messageObject']->TransferringAgency->Identifier->value;
$data->ArchivalAgency = new stdClass();
$data->ArchivalAgency->Identifier = new stdClass();
$data->ArchivalAgency = new \stdClass();
$data->ArchivalAgency->Identifier = new \stdClass();
$data->ArchivalAgency->Identifier->value = $args['messageObject']->ArchivalAgency->Identifier->value;
$data->ArchivalAgreement = new stdClass();
$data->ArchivalAgreement = new \stdClass();
$data->ArchivalAgreement->value = $args['messageObject']->ArchivalAgreement->value;
$data->ReplyCode = $args['messageObject']->ReplyCode;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment