N°5279 - PHP 8.1: Migrate usages of deprecated strftime() function

In the end we made an adapter to keep using the strftime() format (https://www.php.net/manual/fr/function.strftime.php); not to ease migration but because we couldn't use \DateTime::format().
We can't use \DateTime::format() directly on the whole filename as it would also format characters that are not supposed to be. eg. "__DB__-Y-m-d-production" would become "itopdb-2023-02-09-+01:00Thu, 09 Feb 2023 11:34:01 +0100202309", mind the "production" part being converted.
This commit is contained in:
Molkobain
2023-02-10 21:49:30 +01:00
parent 35b0b16e20
commit f7ee21f1d7
10 changed files with 147 additions and 26 deletions

View File

@@ -146,18 +146,35 @@ class DBBackup
/**
* Create a normalized backup name, depending on the current date/time and Database
*
* @param string sNameSpec Name and path, eventually containing itop placeholders + time formatting specs
* @param string sNameSpec Name and path, eventually containing itop placeholders + time formatting following the strftime() format {@link https://www.php.net/manual/fr/function.strftime.php}
* @param \DateTime|null $oDateTime Date time to use for the name
*
* @return string
* @since 3.1.0 N°5279 Add $oDtaeaTime parameter
*/
public function MakeName($sNameSpec = "__DB__-%Y-%m-%d")
public function MakeName($sNameSpec = "__DB__-%Y-%m-%d", DateTime $oDateTime = null)
{
if ($oDateTime === null) {
$oDateTime = new DateTime();
}
$sFileName = $sNameSpec;
$sFileName = str_replace('__HOST__', $this->sDBHost, $sFileName);
$sFileName = str_replace('__DB__', $this->sDBName, $sFileName);
$sFileName = str_replace('__SUBNAME__', $this->sDBSubName, $sFileName);
// Transform %Y, etc.
$sFileName = strftime($sFileName);
// Transform date/time placeholders (%Y, %m, etc)
// N°5279 - As of PHP 8.1 strftime() is deprecated so we use \DateTime::format() instead
//
// IMPORTANT: We can't use \DateTime::format() directly on the whole filename as it would also format characters that are not supposed to be. eg. "__DB__-Y-m-d-production" would become "itopdb-2023-02-09-+01:00Thu, 09 Feb 2023 11:34:01 +0100202309"
$sFileName = preg_replace_callback(
'/(%[a-zA-Z])/',
function ($aMatches) use ($oDateTime) {
$sDateTimeFormatPlaceholder = utils::StrftimeFormatToDateTimeFormat($aMatches[0]);
return $oDateTime->format($sDateTimeFormatPlaceholder);
},
$sFileName,
);
return $sFileName;
}