Skip to content

Commit 4405b8a

Browse files
Merge pull request #17879 from MauricioFauth/tracking-datetime
Refactor `$dateFrom` and `$dateTo` to use `DateTimeImmutable`
2 parents 3c90ff3 + a1c8769 commit 4405b8a

3 files changed

Lines changed: 78 additions & 45 deletions

File tree

libraries/classes/Controllers/Table/TrackingController.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpMyAdmin\Controllers\Table;
66

7+
use DateTimeImmutable;
78
use PhpMyAdmin\Controllers\AbstractController;
89
use PhpMyAdmin\DbTableExists;
910
use PhpMyAdmin\Http\ServerRequest;
@@ -14,6 +15,8 @@
1415
use PhpMyAdmin\Tracking;
1516
use PhpMyAdmin\Url;
1617
use PhpMyAdmin\Util;
18+
use Throwable;
19+
use Webmozart\Assert\Assert;
1720

1821
use function __;
1922
use function array_map;
@@ -93,8 +96,8 @@ public function __invoke(ServerRequest $request): void
9396

9497
$logType = $this->validateLogTypeParam($request->getParsedBodyParam('log_type'));
9598

96-
$dateFrom = '';
97-
$dateTo = '';
99+
$dateFrom = null;
100+
$dateTo = null;
98101
$users = '';
99102

100103
// Init vars for tracking report
@@ -105,16 +108,20 @@ public function __invoke(ServerRequest $request): void
105108
$versionParam
106109
);
107110

108-
/** @var string $dateFrom */
109-
$dateFrom = $request->getParsedBodyParam('date_from', $trackedData['date_from']);
110-
/** @var string $dateTo */
111-
$dateTo = $request->getParsedBodyParam('date_to', $trackedData['date_to']);
111+
$dateFrom = $this->validateDateTimeParam(
112+
$request->getParsedBodyParam('date_from', $trackedData['date_from'])
113+
);
114+
$dateTo = $this->validateDateTimeParam($request->getParsedBodyParam('date_to', $trackedData['date_to']));
115+
112116
/** @var string $users */
113117
$users = $request->getParsedBodyParam('users', '*');
114118

115119
$GLOBALS['filter_users'] = array_map('trim', explode(',', $users));
116120
}
117121

122+
$dateFrom = $dateFrom ?? new DateTimeImmutable();
123+
$dateTo = $dateTo ?? new DateTimeImmutable();
124+
118125
// Prepare export
119126
if ($reportExport !== null) {
120127
$GLOBALS['entries'] = $this->tracking->getEntries(
@@ -272,4 +279,18 @@ private function validateLogTypeParam($param): string
272279
{
273280
return in_array($param, ['schema', 'data'], true) ? $param : 'schema_and_data';
274281
}
282+
283+
/**
284+
* @param mixed $param
285+
*/
286+
private function validateDateTimeParam($param): DateTimeImmutable
287+
{
288+
try {
289+
Assert::stringNotEmpty($param);
290+
291+
return new DateTimeImmutable($param);
292+
} catch (Throwable $exception) {
293+
return new DateTimeImmutable();
294+
}
295+
}
275296
}

libraries/classes/Tracking.php

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace PhpMyAdmin;
99

10+
use DateTimeImmutable;
1011
use PhpMyAdmin\ConfigStorage\Relation;
1112
use PhpMyAdmin\Dbal\ResultInterface;
1213
use PhpMyAdmin\Html\Generator;
@@ -68,18 +69,20 @@ public function __construct(
6869
*
6970
* @return array filtered entries
7071
*/
71-
public function filter(array $data, array $filter_users, string $dateFrom, string $dateTo): array
72-
{
73-
$dateFromTimestamp = strtotime($dateFrom);
74-
$dateToTimestamp = strtotime($dateTo);
72+
public function filter(
73+
array $data,
74+
array $filter_users,
75+
DateTimeImmutable $dateFrom,
76+
DateTimeImmutable $dateTo
77+
): array {
7578
$tmp_entries = [];
7679
$id = 0;
7780
foreach ($data as $entry) {
7881
$timestamp = strtotime($entry['date']);
7982
$filtered_user = in_array($entry['username'], $filter_users);
8083
if (
81-
$timestamp >= $dateFromTimestamp
82-
&& $timestamp <= $dateToTimestamp
84+
$timestamp >= $dateFrom->getTimestamp()
85+
&& $timestamp <= $dateTo->getTimestamp()
8386
&& (in_array('*', $filter_users) || $filtered_user)
8487
) {
8588
$tmp_entries[] = [
@@ -218,8 +221,8 @@ public function getHtmlForTrackingReport(
218221
string $logType,
219222
array $filter_users,
220223
string $version,
221-
string $dateFrom,
222-
string $dateTo,
224+
DateTimeImmutable $dateFrom,
225+
DateTimeImmutable $dateTo,
223226
string $users
224227
) {
225228
$html = '<h3>' . __('Tracking report')
@@ -300,8 +303,8 @@ public function getHtmlForTrackingReport(
300303
*/
301304
public function getHtmlForElementsOfTrackingReport(
302305
string $logType,
303-
string $dateFrom,
304-
string $dateTo,
306+
DateTimeImmutable $dateFrom,
307+
DateTimeImmutable $dateTo,
305308
string $users
306309
): array {
307310
$str1 = '<select name="log_type">'
@@ -316,9 +319,9 @@ public function getHtmlForElementsOfTrackingReport(
316319
. __('Structure and data') . '</option>'
317320
. '</select>';
318321
$str2 = '<input type="text" name="date_from" value="'
319-
. htmlspecialchars($dateFrom) . '" size="19">';
322+
. htmlspecialchars($dateFrom->format('Y-m-d H:i:s')) . '" size="19">';
320323
$str3 = '<input type="text" name="date_to" value="'
321-
. htmlspecialchars($dateTo) . '" size="19">';
324+
. htmlspecialchars($dateTo->format('Y-m-d H:i:s')) . '" size="19">';
322325
$str4 = '<input type="text" name="users" value="'
323326
. htmlspecialchars($users) . '">';
324327
$str5 = '<input type="hidden" name="list_report" value="1">'
@@ -361,8 +364,8 @@ public function getHtmlForTrackingReportExportForm1(
361364
$str5,
362365
$drop_image_or_text,
363366
string $version,
364-
string $dateFrom,
365-
string $dateTo
367+
DateTimeImmutable $dateFrom,
368+
DateTimeImmutable $dateTo
366369
) {
367370
$ddlog_count = 0;
368371

@@ -436,8 +439,8 @@ public function getHtmlForTrackingReportExportForm2(
436439
$str5,
437440
string $logType,
438441
string $version,
439-
string $dateFrom,
440-
string $dateTo,
442+
DateTimeImmutable $dateFrom,
443+
DateTimeImmutable $dateTo,
441444
string $users
442445
) {
443446
$html = '<form method="post" action="' . Url::getFromRoute('/table/tracking') . '">';
@@ -461,8 +464,8 @@ public function getHtmlForTrackingReportExportForm2(
461464
'report' => 'true',
462465
'version' => $version,
463466
'log_type' => $logType,
464-
'date_from' => $dateFrom,
465-
'date_to' => $dateTo,
467+
'date_from' => $dateFrom->format('Y-m-d H:i:s'),
468+
'date_to' => $dateTo->format('Y-m-d H:i:s'),
466469
'users' => $users,
467470
'report_export' => 'true',
468471
]);
@@ -504,8 +507,8 @@ public function getHtmlForDataManipulationStatements(
504507
$ddlog_count,
505508
$drop_image_or_text,
506509
string $version,
507-
string $dateFrom,
508-
string $dateTo
510+
DateTimeImmutable $dateFrom,
511+
DateTimeImmutable $dateTo
509512
) {
510513
// no need for the second returned parameter
511514
[$html] = $this->getHtmlForDataStatements(
@@ -541,8 +544,8 @@ public function getHtmlForDataDefinitionStatements(
541544
array $url_params,
542545
$drop_image_or_text,
543546
string $version,
544-
string $dateFrom,
545-
string $dateTo
547+
DateTimeImmutable $dateFrom,
548+
DateTimeImmutable $dateTo
546549
) {
547550
[$html, $line_number] = $this->getHtmlForDataStatements(
548551
$data,
@@ -588,18 +591,16 @@ private function getHtmlForDataStatements(
588591
$lineNumber,
589592
$tableId,
590593
string $version,
591-
string $dateFrom,
592-
string $dateTo
594+
DateTimeImmutable $dateFrom,
595+
DateTimeImmutable $dateTo
593596
) {
594-
$dateFromTimestamp = strtotime($dateFrom);
595-
$dateToTimestamp = strtotime($dateTo);
596597
$offset = $lineNumber;
597598
$entries = [];
598599
foreach ($data[$whichLog] as $entry) {
599600
$timestamp = strtotime($entry['date']);
600601
if (
601-
$timestamp >= $dateFromTimestamp
602-
&& $timestamp <= $dateToTimestamp
602+
$timestamp >= $dateFrom->getTimestamp()
603+
&& $timestamp <= $dateTo->getTimestamp()
603604
&& (in_array('*', $filterUsers)
604605
|| in_array($entry['username'], $filterUsers))
605606
) {
@@ -1062,8 +1063,13 @@ public function createTrackingForMultipleTables(string $db, array $selected, str
10621063
*
10631064
* @return array
10641065
*/
1065-
public function getEntries(array $data, array $filter_users, string $logType, string $dateFrom, string $dateTo)
1066-
{
1066+
public function getEntries(
1067+
array $data,
1068+
array $filter_users,
1069+
string $logType,
1070+
DateTimeImmutable $dateFrom,
1071+
DateTimeImmutable $dateTo
1072+
) {
10671073
$entries = [];
10681074
// Filtering data definition statements
10691075
if ($logType === 'schema' || $logType === 'schema_and_data') {

test/classes/TrackingTest.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpMyAdmin\Tests;
66

7+
use DateTimeImmutable;
78
use PhpMyAdmin\ConfigStorage\Relation;
89
use PhpMyAdmin\ConfigStorage\RelationParameters;
910
use PhpMyAdmin\SqlQueryForm;
@@ -77,7 +78,12 @@ public function testFilter(): void
7778
];
7879
$filter_users = ['username1'];
7980

80-
$ret = $this->tracking->filter($data, $filter_users, '2010-01-01 12:34:56', '2020-01-01 12:34:56');
81+
$ret = $this->tracking->filter(
82+
$data,
83+
$filter_users,
84+
new DateTimeImmutable('2010-01-01 12:34:56'),
85+
new DateTimeImmutable('2020-01-01 12:34:56')
86+
);
8187

8288
$this->assertEquals('username1', $ret[0]['username']);
8389
$this->assertEquals('statement1', $ret[0]['statement']);
@@ -280,8 +286,8 @@ public function testGetHtmlForTrackingReportr(): void
280286
'schema_and_data',
281287
$filter_users,
282288
'10',
283-
'2022-11-03 22:15:24',
284-
'2022-11-04 22:15:24',
289+
new DateTimeImmutable('2022-11-03 22:15:24'),
290+
new DateTimeImmutable('2022-11-04 22:15:24'),
285291
'users'
286292
);
287293

@@ -354,8 +360,8 @@ public function testGetHtmlForDataManipulationStatements(): void
354360
$ddlog_count,
355361
$drop_image_or_text,
356362
'10',
357-
'2010-01-01 12:34:56',
358-
'2020-01-01 12:34:56'
363+
new DateTimeImmutable('2010-01-01 12:34:56'),
364+
new DateTimeImmutable('2020-01-01 12:34:56')
359365
);
360366

361367
$this->assertStringContainsString(
@@ -404,8 +410,8 @@ public function testGetHtmlForDataDefinitionStatements(): void
404410
$url_params,
405411
$drop_image_or_text,
406412
'10',
407-
'2010-01-01 12:34:56',
408-
'2020-01-01 12:34:56'
413+
new DateTimeImmutable('2010-01-01 12:34:56'),
414+
new DateTimeImmutable('2020-01-01 12:34:56')
409415
);
410416

411417
$this->assertStringContainsString(
@@ -572,8 +578,8 @@ public function testGetEntries(): void
572578
$data,
573579
$filter_users,
574580
'schema',
575-
'2010-01-01 12:34:56',
576-
'2020-01-01 12:34:56'
581+
new DateTimeImmutable('2010-01-01 12:34:56'),
582+
new DateTimeImmutable('2020-01-01 12:34:56')
577583
);
578584
$this->assertEquals('username3', $entries[0]['username']);
579585
$this->assertEquals('statement1', $entries[0]['statement']);

0 commit comments

Comments
 (0)