Fix purge tool

This commit is contained in:
Laurent Destailleur 2020-12-18 13:24:02 +01:00
parent b740d79f8e
commit 90d8299c92
3 changed files with 99 additions and 94 deletions

View File

@ -91,7 +91,7 @@ print '<table class="border centpercent">';
print '<tr class="border"><td style="padding: 4px">'; print '<tr class="border"><td style="padding: 4px">';
if (!empty($conf->syslog->enabled)) { if (!empty($conf->syslog->enabled)) {
print '<input type="radio" name="choice" value="logfile"'; print '<input type="radio" name="choice" id="choicelogfile" value="logfile"';
print ($choice && $choice == 'logfile') ? ' checked' : ''; print ($choice && $choice == 'logfile') ? ' checked' : '';
$filelogparam = $filelog; $filelogparam = $filelog;
if ($user->admin && preg_match('/^dolibarr.*\.log$/', basename($filelog))) { if ($user->admin && preg_match('/^dolibarr.*\.log$/', basename($filelog))) {
@ -101,17 +101,17 @@ if (!empty($conf->syslog->enabled)) {
} }
$desc = $langs->trans("PurgeDeleteLogFile", '{filelogparam}'); $desc = $langs->trans("PurgeDeleteLogFile", '{filelogparam}');
$desc = str_replace('{filelogparam}', $filelogparam, $desc); $desc = str_replace('{filelogparam}', $filelogparam, $desc);
print '> '.$desc; print '> <label for="choicelogfile">'.$desc.'</label>';
print '<br><br>'; print '<br><br>';
} }
print '<input type="radio" name="choice" value="tempfiles"'; print '<input type="radio" name="choice" id="choicetempfiles" value="tempfiles"';
print (!$choice || $choice == 'tempfiles' || $choice == 'allfiles') ? ' checked' : ''; print (!$choice || $choice == 'tempfiles' || $choice == 'allfiles') ? ' checked' : '';
print '> '.$langs->trans("PurgeDeleteTemporaryFiles").'<br><br>'; print '> <label for="choicetempfiles">'.$langs->trans("PurgeDeleteTemporaryFiles").'</label><br><br>';
print '<input type="radio" name="choice" value="confirm_allfiles"'; print '<input type="radio" name="choice" id="choiceallfiles" value="confirm_allfiles"';
print ($choice && $choice == 'confirm_allfiles') ? ' checked' : ''; print ($choice && $choice == 'confirm_allfiles') ? ' checked' : '';
print '> '.$langs->trans("PurgeDeleteAllFilesInDocumentsDir", $dolibarr_main_data_root).'<br>'; print '> <label for="choiceallfiles">'.$langs->trans("PurgeDeleteAllFilesInDocumentsDir", $dolibarr_main_data_root).'</label><br>';
print '</td></tr></table>'; print '</td></tr></table>';

View File

@ -50,11 +50,11 @@ class Utils
* Purge files into directory of data files. * Purge files into directory of data files.
* CAN BE A CRON TASK * CAN BE A CRON TASK
* *
* @param string $choice Choice of purge mode ('tempfiles', '' or 'tempfilesold' to purge temp older than $nbsecondsold seconds, 'allfiles', 'logfile') * @param string $choices Choice of purge mode ('tempfiles', '' or 'tempfilesold' to purge temp older than $nbsecondsold seconds, 'allfiles', 'logfile')
* @param int $nbsecondsold Nb of seconds old to accept deletion of a directory if $choice is 'tempfilesold' * @param int $nbsecondsold Nb of seconds old to accept deletion of a directory if $choice is 'tempfilesold'
* @return int 0 if OK, < 0 if KO (this function is used also by cron so only 0 is OK) * @return int 0 if OK, < 0 if KO (this function is used also by cron so only 0 is OK)
*/ */
public function purgeFiles($choice = 'tempfilesold', $nbsecondsold = 86400) public function purgeFiles($choices = 'tempfilesold,logfile', $nbsecondsold = 86400)
{ {
global $conf, $langs, $dolibarr_main_data_root; global $conf, $langs, $dolibarr_main_data_root;
@ -62,10 +62,17 @@ class Utils
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
$filesarray = array(); if (empty($choices)) $choices = 'tempfilesold,logfile';
if (empty($choice)) $choice = 'tempfilesold';
dol_syslog("Utils::purgeFiles choice=".$choice, LOG_DEBUG); dol_syslog("Utils::purgeFiles choice=".$choices, LOG_DEBUG);
$count = 0;
$countdeleted = 0;
$counterror = 0;
$choicesarray = explode(',', $choices);
foreach ($choicesarray as $choice) {
$filesarray = array();
if ($choice == 'tempfiles' || $choice == 'tempfilesold') if ($choice == 'tempfiles' || $choice == 'tempfilesold')
{ {
@ -117,24 +124,21 @@ class Utils
} }
} }
$count = 0; if (is_array($filesarray) && count($filesarray)) {
$countdeleted = 0;
$counterror = 0;
if (count($filesarray))
{
foreach ($filesarray as $key => $value) foreach ($filesarray as $key => $value)
{ {
//print "x ".$filesarray[$key]['fullname']."-".$filesarray[$key]['type']."<br>\n"; //print "x ".$filesarray[$key]['fullname']."-".$filesarray[$key]['type']."<br>\n";
if ($filesarray[$key]['type'] == 'dir') if ($filesarray[$key]['type'] == 'dir') {
{
$startcount = 0; $startcount = 0;
$tmpcountdeleted = 0; $tmpcountdeleted = 0;
$result = dol_delete_dir_recursive($filesarray[$key]['fullname'], $startcount, 1, 0, $tmpcountdeleted); $result = dol_delete_dir_recursive($filesarray[$key]['fullname'], $startcount, 1, 0, $tmpcountdeleted);
if (!in_array($filesarray[$key]['fullname'], array($conf->api->dir_temp, $conf->user->dir_temp))) { // The 2 directories $conf->api->dir_temp and $conf->user->dir_temp are recreated at end, so we do not count them
$count += $result; $count += $result;
$countdeleted += $tmpcountdeleted; $countdeleted += $tmpcountdeleted;
} elseif ($filesarray[$key]['type'] == 'file') }
{ } elseif ($filesarray[$key]['type'] == 'file') {
// If (file that is not logfile) or (if mode is logfile) // If (file that is not logfile) or (if mode is logfile)
if ($filesarray[$key]['fullname'] != $filelog || $choice == 'logfile') if ($filesarray[$key]['fullname'] != $filelog || $choice == 'logfile')
{ {
@ -158,16 +162,17 @@ class Utils
$result = $ecmdirstatic->refreshcachenboffile(1); $result = $ecmdirstatic->refreshcachenboffile(1);
} }
} }
}
if ($count > 0) if ($count > 0) {
{
$this->output = $langs->trans("PurgeNDirectoriesDeleted", $countdeleted); $this->output = $langs->trans("PurgeNDirectoriesDeleted", $countdeleted);
if ($count > $countdeleted) $this->output .= '<br>'.$langs->trans("PurgeNDirectoriesFailed", ($count - $countdeleted)); if ($count > $countdeleted) $this->output .= '<br>'.$langs->trans("PurgeNDirectoriesFailed", ($count - $countdeleted));
} else $this->output = $langs->trans("PurgeNothingToDelete").($choice == 'tempfilesold' ? ' (older than 24h)' : ''); } else {
$this->output = $langs->trans("PurgeNothingToDelete").($choice == 'tempfilesold' ? ' (older than 24h)' : '');
}
// Recreate temp dir that are not automatically recreated by core code for performance purpose, we need them // Recreate temp dir that are not automatically recreated by core code for performance purpose, we need them
if (!empty($conf->api->enabled)) if (!empty($conf->api->enabled)) {
{
dol_mkdir($conf->api->dir_temp); dol_mkdir($conf->api->dir_temp);
} }
dol_mkdir($conf->user->dir_temp); dol_mkdir($conf->user->dir_temp);

View File

@ -155,8 +155,8 @@ SystemToolsAreaDesc=This area provides administration functions. Use the menu to
Purge=Purge Purge=Purge
PurgeAreaDesc=This page allows you to delete all files generated or stored by Dolibarr (temporary files or all files in <b>%s</b> directory). Using this feature is not normally necessary. It is provided as a workaround for users whose Dolibarr is hosted by a provider that does not offer permissions to delete files generated by the web server. PurgeAreaDesc=This page allows you to delete all files generated or stored by Dolibarr (temporary files or all files in <b>%s</b> directory). Using this feature is not normally necessary. It is provided as a workaround for users whose Dolibarr is hosted by a provider that does not offer permissions to delete files generated by the web server.
PurgeDeleteLogFile=Delete log files, including <b>%s</b> defined for Syslog module (no risk of losing data) PurgeDeleteLogFile=Delete log files, including <b>%s</b> defined for Syslog module (no risk of losing data)
PurgeDeleteTemporaryFiles=Delete all temporary files (no risk of losing data). Note: Deletion is done only if the temp directory was created 24 hours ago. PurgeDeleteTemporaryFiles=Delete all log and temporary files (no risk of losing data). Note: Deletion of temporary files is done only if the temp directory was created more than 24 hours ago.
PurgeDeleteTemporaryFilesShort=Delete temporary files PurgeDeleteTemporaryFilesShort=Delete log and temporary files
PurgeDeleteAllFilesInDocumentsDir=Delete all files in directory: <b>%s</b>.<br>This will delete all generated documents related to elements (third parties, invoices etc...), files uploaded into the ECM module, database backup dumps and temporary files. PurgeDeleteAllFilesInDocumentsDir=Delete all files in directory: <b>%s</b>.<br>This will delete all generated documents related to elements (third parties, invoices etc...), files uploaded into the ECM module, database backup dumps and temporary files.
PurgeRunNow=Purge now PurgeRunNow=Purge now
PurgeNothingToDelete=No directory or files to delete. PurgeNothingToDelete=No directory or files to delete.