Compare commits

..

4 commits

Author SHA1 Message Date
Benjamin Renard
341f555e0c
LSformElement::jsonCompositeAttribute: fix checking non-multiple components value 2023-05-24 13:28:32 +02:00
Benjamin Renard
27ad049ac7
LSio: improve handling time & memory limits and allow before_import hook to set them 2023-05-24 13:28:31 +02:00
Benjamin Renard
99f83f326d
LSformElement::supannCompositeAttribute: fix checking components value
LSformRule :: validate_values() expected an array of values and not a 
unique value
2023-05-24 12:00:51 +02:00
Benjamin Renard
c97d47ac00
LSformElement::supannEtuInscription: fix cursusann component regex 2023-05-24 11:58:13 +02:00
5 changed files with 139 additions and 8 deletions

View file

@ -398,7 +398,12 @@ class LSformElement_jsonCompositeAttribute extends LSformElement {
// Apply check data rules // Apply check data rules
LSsession :: loadLSclass('LSformRule', null, true); LSsession :: loadLSclass('LSformRule', null, true);
foreach($this -> getComponentConfig($c, 'check_data', array(), 'array') as $ruleType => $rconf) { foreach($this -> getComponentConfig($c, 'check_data', array(), 'array') as $ruleType => $rconf) {
$errors = LSformRule :: validate_values($ruleType, $value, $rconf, $this); $errors = LSformRule :: validate_values(
$ruleType,
$this -> getComponentConfig($c, 'multiple', false, 'bool')?$value:array($value),
$rconf,
$this
);
if (is_array($errors)) { if (is_array($errors)) {
$retval = false; $retval = false;
foreach ($errors as $error) { foreach ($errors as $error) {

View file

@ -472,7 +472,7 @@ class LSformElement_supannCompositeAttribute extends LSformElement {
if (isset($cconf['check_data']) && is_array($cconf['check_data'])) { if (isset($cconf['check_data']) && is_array($cconf['check_data'])) {
LSsession :: loadLSclass('LSformRule', null, true); LSsession :: loadLSclass('LSformRule', null, true);
foreach($cconf['check_data'] as $ruleType => $rconf) { foreach($cconf['check_data'] as $ruleType => $rconf) {
$cerrors = LSformRule :: validate_values($ruleType, $value, $rconf, $this); $cerrors = LSformRule :: validate_values($ruleType, array($value), $rconf, $this);
if (is_array($cerrors)) if (is_array($cerrors))
foreach ($cerrors as $cerror) foreach ($cerrors as $cerror)
$errors[] = getFData( $errors[] = getFData(

View file

@ -81,7 +81,7 @@ class LSformElement_supannEtuInscription extends LSformElement_supannCompositeAt
'check_data' => array ( 'check_data' => array (
'regex' => array ( 'regex' => array (
'params' => array ( 'params' => array (
'regex' => '/^[LMDXB][0-9]?$/' 'regex' => '/^\{SUPANN\}[LMDXB][0-9]?$/'
), ),
), ),
), ),

View file

@ -227,10 +227,6 @@ class LSio extends LSlog_staticLoggerClass {
$objectsInError = array(); $objectsInError = array();
self :: log_trace("import(): objects data=".varDump($objectsData)); self :: log_trace("import(): objects data=".varDump($objectsData));
// Reset & increase time limit: allow one seconds by object to handle,
// with a minimum of 30 seconds
set_time_limit((count($objectsData)>30?count($objectsData):30));
// Trigger before_import event // Trigger before_import event
if ( if (
!$ioFormat -> fireEvent( !$ioFormat -> fireEvent(
@ -248,8 +244,79 @@ class LSio extends LSlog_staticLoggerClass {
return $return; return $return;
} }
// Increase time limit: allow at least one seconds by object to handle,
// with a minimum of 30 seconds
$time_limit = intval(ini_get('max_execution_time'));
$new_time_limit = (count($objectsData)>30?count($objectsData):30);
if ($time_limit == 0) {
self :: log_debug("import(): time limit = 0 (no limit)");
}
else if ($new_time_limit > $time_limit) {
self :: log_debug(
sprintf(
"import(): increase time limit to %s seconds (current: %s seconds)",
$new_time_limit, $time_limit
)
);
if (set_time_limit($new_time_limit) === false) {
self :: log_warning(
sprintf(
"import(): fail to increase time limit to %s seconds (current: %s seconds)",
$new_time_limit, $time_limit
)
);
}
else {
$time_limit = $new_time_limit;
}
}
else {
self :: log_debug("import(): time limit = $time_limit seconds");
}
// Increase memory limit: allow at least 10M by object to handle
$mem_limit = php_ini_unit_to_bytes(ini_get('memory_limit'));
$new_mem_limit = 10485760 * count($objectsData);
if ($mem_limit == -1) {
self :: log_debug("import(): memory limit = -1 (no limit)");
}
else if ($new_mem_limit > $mem_limit) {
self :: log_debug(
sprintf(
"import(): increase memory limit to %s (current: %s)",
format_size($new_mem_limit), format_size($mem_limit)
)
);
if (ini_set('memory_limit', $new_mem_limit."B") === false) {
self :: log_warning(
sprintf(
"import(): fail to increase memory limit to %s (current: %s)",
format_size($new_mem_limit), format_size($mem_limit)
)
);
}
else {
$mem_limit = $new_mem_limit;
}
}
else {
self :: log_debug("import(): memory limit = ".format_size($mem_limit));
}
// Browse inputed objects // Browse inputed objects
foreach($objectsData as $objData) { foreach($objectsData as $idx => $objData) {
// Force execution of PHP garbage collector on each object
gc_collect_cycles();
self :: log_debug(
sprintf(
'import() - #%s: memory usage = %s (max = %s) on %s',
$idx,
format_size(memory_get_usage()),
format_size(memory_get_peak_usage()),
$mem_limit==-1?'no limit':format_size($mem_limit)
)
);
$globalErrors = array(); $globalErrors = array();
// Instanciate an LSobject // Instanciate an LSobject
$object = new $LSobject(); $object = new $LSobject();
@ -350,6 +417,17 @@ class LSio extends LSlog_staticLoggerClass {
$return['errors'] = $objectsInError; $return['errors'] = $objectsInError;
$return['success'] = empty($objectsInError); $return['success'] = empty($objectsInError);
// Force execution of PHP garbage collector after import
gc_collect_cycles();
self :: log_debug(
sprintf(
'import(): memory usage after import = %s (max = %s) on %s',
format_size(memory_get_usage()),
format_size(memory_get_peak_usage()),
$mem_limit==-1?'no limit':format_size($mem_limit)
)
);
// Trigger after_import event // Trigger after_import event
if ( if (
!$ioFormat -> fireEvent( !$ioFormat -> fireEvent(

View file

@ -841,3 +841,51 @@ function generate_uuid() {
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
); );
} }
/**
* Format a number
* @param float|int $number
* @return string
*/
function format_number($number) {
if ((int)$number == $number) return strval($number);
return number_format($number, 2, ',', '.');
}
/**
* Format a size (in bytes)
* @param int $value
* @param boolean|string $unit Unit of the provided value (optional, default=bytes)
* @return string|false
*/
function format_size($value, $unit=false) {
$units = array(
___('TB') => 1099511627776,
___('GB') => 1073741824,
___('MB') => 1048576,
___('KB') => 1024,
___('B') => 1,
);
if (!$unit) $unit = 'B';
if (!array_key_exists($unit, $units)) return false;
$value = $value * $units[$unit];
foreach ($units as $unit => $factor) {
if ($value >= $factor)
return format_number($value / $factor)._($unit);
}
// 0 ?
return strval($value);
}
/**
* Convert PHP ini value with unit as bytes
* @param string $value
* @return int
*/
function php_ini_unit_to_bytes($value) {
if ($value == "-1") return -1;
return (int)preg_replace_callback('/(\-?\d+)(.?)/', function ($m) {
$factor = $m[2]?strpos('BKMG', $m[2]):null;
return $factor?intval($m[1]) * pow(1024, $factor):$m[0];
}, strtoupper($value));
}