Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New functionality for save 5 oldest versions of pages #29616

Merged
merged 4 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
65 changes: 65 additions & 0 deletions htdocs/core/lib/files.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3685,3 +3685,68 @@
$out .= "</script>\n";
return $out;
}

/**
* Manage backup versions for a given file, ensuring only a maximum number of versions are kept.
*
* @param string $filetpl The base filename for the backups.
* @param int $max_versions The maximum number of backup versions to keep.
* @return bool Returns true if successful, false otherwise.
*/
function manageFileBackups($filetpl, $max_versions = 5)
{

$base_file_pattern = $filetpl . ".v";
$files_in_directory = glob($base_file_pattern . "*");

// Extract the modification timestamps for each file
$files_with_timestamps = [];
foreach ($files_in_directory as $file) {
$files_with_timestamps[] = [
'file' => $file,
'timestamp' => filemtime($file)
];
}

// Sort the files by modification date
$sorted_files = [];
while (count($files_with_timestamps) > 0) {
$latest_file = null;
$latest_index = null;

// Find the latest file by timestamp
foreach ($files_with_timestamps as $index => $file_info) {
if ($latest_file === null || $file_info['timestamp'] > $latest_file['timestamp']) {

Check warning on line 3719 in htdocs/core/lib/files.lib.php

View workflow job for this annotation

GitHub Actions / phan / Run phan

files.lib.php: PhanTypeArraySuspiciousNull: Suspicious array access to $latest_file of type null
$latest_file = $file_info;
$latest_index = $index;
}
}

// Add the latest file to the sorted list and remove it from the original list
if ($latest_file !== null) {
$sorted_files[] = $latest_file['file'];
unset($files_with_timestamps[$latest_index]);
}
}

// Delete the oldest files to keep only the allowed number of versions
if (count($sorted_files) >= $max_versions) {
$oldest_files = array_slice($sorted_files, $max_versions - 1);
foreach ($oldest_files as $oldest_file) {
dol_delete_file($oldest_file);
}
}


$timestamp = dol_now();
$new_backup = $filetpl . ".v" . $timestamp;

// Move the original file to the new backup with the timestamp
$result = dol_move($filetpl, $new_backup, 0, 1, 0, 0);

if (!$result) {
return false;
}

return true;
}
3 changes: 1 addition & 2 deletions htdocs/core/lib/website2.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ function dolSavePageContent($filetpl, Website $object, WebsitePage $objectpage,

if (dol_is_file($filetpl)) {
if ($backupold) {
dol_delete_file($filetpl.'.old');
$result = dol_move($filetpl, $filetpl.'.old', 0, 1, 0, 0);
$result = manageFileBackups($filetpl);
if (! $result) {
return false;
}
Expand Down