Add now-timestamp parameter to simpleSaveChangeHistory#814
Add now-timestamp parameter to simpleSaveChangeHistory#814
Conversation
There was a problem hiding this comment.
Pull request overview
This PR refactors the simpleSaveChangeHistory function to accept a timestamp parameter instead of using SQL's NOW() function directly. This change allows for better control over timestamp generation and ensures consistency when multiple operations need to use the same timestamp.
Changes:
- Modified
simpleSaveChangeHistoryfunction signature to accept a$nowparameter before the optional$fromand$toparameters - Updated all call sites in the changed regions to generate timestamps using PHP's
date('Y-m-d H:i:s')and pass them to the function - Replaced SQL
NOW()calls with parameterized:nowplaceholders in UPDATE and INSERT queries across multiple files
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| library/lib/tools.php | Updated function signature and SQL query to use parameterized timestamp |
| library/lib/list.php | Updated listDeleteAction and listUnDeleteAction to generate and pass timestamps |
| library/functions.php | Updated move_boxes to generate timestamp once per transaction and use it consistently |
| library/ajax/deleteprofile.php | Updated user deletion to use parameterized timestamp |
| cron/dailyroutine.php | Updated daily routine deletions to use parameterized timestamp |
Comments suppressed due to low confidence (1)
library/lib/list.php:298
- When
listUnDeleteActionis called recursively for child records (line 298), each recursive call generates a new timestamp. This means parent and child records will have different timestamps in the history table, even though they're part of the same logical operation. Consider passing the$nowtimestamp as a parameter through the recursive calls to ensure all related records have consistent timestamps.
if ($recursive) {
$childs = db_array('SELECT id FROM '.$table.' WHERE parent_id = :id', ['id' => $id]);
foreach ($childs as $child) {
$count += listUnDeleteAction($table, $child['id'], $count, true);
18a995c to
8d86bae
Compare
8d86bae to
fa3d114
Compare
fa3d114 to
89b0c55
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.
Comments suppressed due to low confidence (1)
library/lib/list.php:276
- The
listUnDeleteActionfunction generates a new$nowtimestamp at line 266, but when it recursively calls itself at line 276, it doesn't pass$nowas a parameter. This means each recursive call will generate a different timestamp, resulting in inconsistent timestamps between parent and child records. This differs from the pattern used inlistDeleteAction(line 230) which correctly passes$nowto recursive calls. Consider modifyinglistUnDeleteActionto accept$nowas a parameter and pass it to recursive calls for consistency.
$now = date('Y-m-d H:i:s');
$result = db_query('UPDATE '.$table.' SET deleted = '.($null ? 'NULL' : '0').', modified = :now, modified_by = :user_id WHERE id = :id', ['now' => $now, 'id' => $id, 'user_id' => $_SESSION['user']['id']]);
$count += $result->rowCount();
if ($result->rowCount()) {
simpleSaveChangeHistory($table, $id, 'Record recovered', $now);
}
if ($recursive) {
$childs = db_array('SELECT id FROM '.$table.' WHERE parent_id = :id', ['id' => $id]);
foreach ($childs as $child) {
$count += listUnDeleteAction($table, $child['id'], $count, true);
| if (null === $now) { | ||
| $now = date('Y-m-d H:i:s'); | ||
| } | ||
| db_query('INSERT INTO history (tablename, record_id, changes, user_id, ip, changedate, from_int, from_float, to_int, to_float) VALUES (:table,:id,:change,:user_id,:ip,:now, :from_int, :from_float, :to_int, :to_float)', ['table' => $table, 'id' => $record, 'change' => $changes, 'user_id' => $_SESSION['user']['id'], 'ip' => $_SERVER['REMOTE_ADDR'], 'now' => $now, 'from_int' => $from['int'], 'from_float' => $from['float'], 'to_int' => $to['int'], 'to_float' => $to['float']]); |
There was a problem hiding this comment.
When simpleSaveChangeHistory is called without the $from and $to parameters (using the default empty arrays), accessing $from['int'], $from['float'], $to['int'], and $to['float'] at line 298 will trigger "Undefined array key" warnings in PHP 8+. Consider adding null coalescing operators to handle these cases gracefully, such as $from['int'] ?? null instead of $from['int']. This affects all calls to this function that don't provide these parameters, including the new calls added in this PR.
listBulkRealDelete is a better version anyways
89b0c55 to
0979fa6
Compare
https://trello.com/c/sONNWuHc