Skip to content

Commit d9fcd35

Browse files
committed
Remove filter_ts_* global variables
Replaces them with $dateFrom and $dateTo variables. Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent 3992ca8 commit d9fcd35

File tree

4 files changed

+73
-117
lines changed

4 files changed

+73
-117
lines changed

libraries/classes/Controllers/Table/TrackingController.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use function in_array;
2424
use function is_array;
2525
use function sprintf;
26-
use function strtotime;
2726

2827
final class TrackingController extends AbstractController
2928
{
@@ -46,8 +45,6 @@ public function __invoke(ServerRequest $request): void
4645
$GLOBALS['msg'] = $GLOBALS['msg'] ?? null;
4746
$GLOBALS['errorUrl'] = $GLOBALS['errorUrl'] ?? null;
4847
$GLOBALS['entries'] = $GLOBALS['entries'] ?? null;
49-
$GLOBALS['filter_ts_from'] = $GLOBALS['filter_ts_from'] ?? null;
50-
$GLOBALS['filter_ts_to'] = $GLOBALS['filter_ts_to'] ?? null;
5148
$GLOBALS['filter_users'] = $GLOBALS['filter_users'] ?? null;
5249

5350
$this->addScriptFiles(['vendor/jquery/jquery.tablesorter.js', 'table/tracking.js']);
@@ -86,8 +83,6 @@ public function __invoke(ServerRequest $request): void
8683

8784
$trackedData = [];
8885
$GLOBALS['entries'] = [];
89-
$GLOBALS['filter_ts_from'] = null;
90-
$GLOBALS['filter_ts_to'] = null;
9186
$GLOBALS['filter_users'] = [];
9287

9388
$report = $request->hasBodyParam('report');
@@ -117,19 +112,17 @@ public function __invoke(ServerRequest $request): void
117112
/** @var string $users */
118113
$users = $request->getParsedBodyParam('users', '*');
119114

120-
$GLOBALS['filter_ts_from'] = strtotime($dateFrom);
121-
$GLOBALS['filter_ts_to'] = strtotime($dateTo);
122115
$GLOBALS['filter_users'] = array_map('trim', explode(',', $users));
123116
}
124117

125118
// Prepare export
126119
if ($reportExport !== null) {
127120
$GLOBALS['entries'] = $this->tracking->getEntries(
128121
$trackedData,
129-
(int) $GLOBALS['filter_ts_from'],
130-
(int) $GLOBALS['filter_ts_to'],
131122
$GLOBALS['filter_users'],
132-
$logType
123+
$logType,
124+
$dateFrom,
125+
$dateTo
133126
);
134127
}
135128

@@ -239,8 +232,6 @@ public function __invoke(ServerRequest $request): void
239232
$trackedData,
240233
$GLOBALS['urlParams'],
241234
$logType,
242-
(int) $GLOBALS['filter_ts_to'],
243-
(int) $GLOBALS['filter_ts_from'],
244235
$GLOBALS['filter_users'],
245236
$versionParam,
246237
$dateFrom,

libraries/classes/Tracking.php

Lines changed: 47 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,23 @@ public function __construct(
6363
/**
6464
* Filters tracking entries
6565
*
66-
* @param array $data the entries to filter
67-
* @param int $filter_ts_from "from" date
68-
* @param int $filter_ts_to "to" date
69-
* @param array $filter_users users
66+
* @param array $data the entries to filter
67+
* @param array $filter_users users
7068
*
7169
* @return array filtered entries
7270
*/
73-
public function filter(
74-
array $data,
75-
$filter_ts_from,
76-
$filter_ts_to,
77-
array $filter_users
78-
): array {
71+
public function filter(array $data, array $filter_users, string $dateFrom, string $dateTo): array
72+
{
73+
$dateFromTimestamp = strtotime($dateFrom);
74+
$dateToTimestamp = strtotime($dateTo);
7975
$tmp_entries = [];
8076
$id = 0;
8177
foreach ($data as $entry) {
8278
$timestamp = strtotime($entry['date']);
8379
$filtered_user = in_array($entry['username'], $filter_users);
8480
if (
85-
$timestamp >= $filter_ts_from
86-
&& $timestamp <= $filter_ts_to
81+
$timestamp >= $dateFromTimestamp
82+
&& $timestamp <= $dateToTimestamp
8783
&& (in_array('*', $filter_users) || $filtered_user)
8884
) {
8985
$tmp_entries[] = [
@@ -209,11 +205,9 @@ public function getSqlResultForSelectableTables(string $db)
209205
/**
210206
* Function to get html for tracking report and tracking report export
211207
*
212-
* @param array $data data
213-
* @param array $url_params url params
214-
* @param int $filter_ts_to filter time stamp from
215-
* @param int $filter_ts_from filter time stamp tp
216-
* @param array $filter_users filter users
208+
* @param array $data data
209+
* @param array $url_params url params
210+
* @param array $filter_users filter users
217211
* @psalm-param 'schema'|'data'|'schema_and_data' $logType
218212
*
219213
* @return string
@@ -222,8 +216,6 @@ public function getHtmlForTrackingReport(
222216
array $data,
223217
array $url_params,
224218
string $logType,
225-
$filter_ts_to,
226-
$filter_ts_from,
227219
array $filter_users,
228220
string $version,
229221
string $dateFrom,
@@ -268,16 +260,16 @@ public function getHtmlForTrackingReport(
268260
$data,
269261
$url_params,
270262
$logType,
271-
$filter_ts_to,
272-
$filter_ts_from,
273263
$filter_users,
274264
$str1,
275265
$str2,
276266
$str3,
277267
$str4,
278268
$str5,
279269
$drop_image_or_text,
280-
$version
270+
$version,
271+
$dateFrom,
272+
$dateTo
281273
);
282274

283275
$html .= $this->getHtmlForTrackingReportExportForm2(
@@ -346,8 +338,6 @@ public function getHtmlForElementsOfTrackingReport(
346338
*
347339
* @param array $data data
348340
* @param array $url_params url params
349-
* @param int $filter_ts_to filter time stamp from
350-
* @param int $filter_ts_from filter time stamp tp
351341
* @param array $filter_users filter users
352342
* @param string $str1 HTML for log_type select
353343
* @param string $str2 HTML for "from date"
@@ -363,16 +353,16 @@ public function getHtmlForTrackingReportExportForm1(
363353
array $data,
364354
array $url_params,
365355
string $logType,
366-
$filter_ts_to,
367-
$filter_ts_from,
368356
array $filter_users,
369357
$str1,
370358
$str2,
371359
$str3,
372360
$str4,
373361
$str5,
374362
$drop_image_or_text,
375-
string $version
363+
string $version,
364+
string $dateFrom,
365+
string $dateTo
376366
) {
377367
$ddlog_count = 0;
378368

@@ -395,11 +385,11 @@ public function getHtmlForTrackingReportExportForm1(
395385
[$temp, $ddlog_count] = $this->getHtmlForDataDefinitionStatements(
396386
$data,
397387
$filter_users,
398-
$filter_ts_from,
399-
$filter_ts_to,
400388
$url_params,
401389
$drop_image_or_text,
402-
$version
390+
$version,
391+
$dateFrom,
392+
$dateTo
403393
);
404394
$html .= $temp;
405395
unset($temp);
@@ -410,12 +400,12 @@ public function getHtmlForTrackingReportExportForm1(
410400
$html .= $this->getHtmlForDataManipulationStatements(
411401
$data,
412402
$filter_users,
413-
$filter_ts_from,
414-
$filter_ts_to,
415403
$url_params,
416404
$ddlog_count,
417405
$drop_image_or_text,
418-
$version
406+
$version,
407+
$dateFrom,
408+
$dateTo
419409
);
420410
}
421411

@@ -501,8 +491,6 @@ public function getHtmlForTrackingReportExportForm2(
501491
*
502492
* @param array $data data
503493
* @param array $filter_users filter users
504-
* @param int $filter_ts_from filter time staml from
505-
* @param int $filter_ts_to filter time stamp to
506494
* @param array $url_params url parameters
507495
* @param int $ddlog_count data definition log count
508496
* @param string $drop_image_or_text drop image or text
@@ -512,26 +500,26 @@ public function getHtmlForTrackingReportExportForm2(
512500
public function getHtmlForDataManipulationStatements(
513501
array $data,
514502
array $filter_users,
515-
$filter_ts_from,
516-
$filter_ts_to,
517503
array $url_params,
518504
$ddlog_count,
519505
$drop_image_or_text,
520-
string $version
506+
string $version,
507+
string $dateFrom,
508+
string $dateTo
521509
) {
522510
// no need for the second returned parameter
523511
[$html] = $this->getHtmlForDataStatements(
524512
$data,
525513
$filter_users,
526-
$filter_ts_from,
527-
$filter_ts_to,
528514
$url_params,
529515
$drop_image_or_text,
530516
'dmlog',
531517
__('Data manipulation statement'),
532518
$ddlog_count,
533519
'dml_versions',
534-
$version
520+
$version,
521+
$dateFrom,
522+
$dateTo
535523
);
536524

537525
return $html;
@@ -542,8 +530,6 @@ public function getHtmlForDataManipulationStatements(
542530
*
543531
* @param array $data data
544532
* @param array $filter_users filter users
545-
* @param int $filter_ts_from filter time stamp from
546-
* @param int $filter_ts_to filter time stamp to
547533
* @param array $url_params url parameters
548534
* @param string $drop_image_or_text drop image or text
549535
*
@@ -552,24 +538,24 @@ public function getHtmlForDataManipulationStatements(
552538
public function getHtmlForDataDefinitionStatements(
553539
array $data,
554540
array $filter_users,
555-
$filter_ts_from,
556-
$filter_ts_to,
557541
array $url_params,
558542
$drop_image_or_text,
559-
string $version
543+
string $version,
544+
string $dateFrom,
545+
string $dateTo
560546
) {
561547
[$html, $line_number] = $this->getHtmlForDataStatements(
562548
$data,
563549
$filter_users,
564-
$filter_ts_from,
565-
$filter_ts_to,
566550
$url_params,
567551
$drop_image_or_text,
568552
'ddlog',
569553
__('Data definition statement'),
570554
1,
571555
'ddl_versions',
572-
$version
556+
$version,
557+
$dateFrom,
558+
$dateTo
573559
);
574560

575561
return [
@@ -583,8 +569,6 @@ public function getHtmlForDataDefinitionStatements(
583569
*
584570
* @param array $data data
585571
* @param array $filterUsers filter users
586-
* @param int $filterTsFrom filter time stamp from
587-
* @param int $filterTsTo filter time stamp to
588572
* @param array $urlParams url parameters
589573
* @param string $dropImageOrText drop image or text
590574
* @param string $whichLog dmlog|ddlog
@@ -597,23 +581,25 @@ public function getHtmlForDataDefinitionStatements(
597581
private function getHtmlForDataStatements(
598582
array $data,
599583
array $filterUsers,
600-
$filterTsFrom,
601-
$filterTsTo,
602584
array $urlParams,
603585
$dropImageOrText,
604586
$whichLog,
605587
$headerMessage,
606588
$lineNumber,
607589
$tableId,
608-
string $version
590+
string $version,
591+
string $dateFrom,
592+
string $dateTo
609593
) {
594+
$dateFromTimestamp = strtotime($dateFrom);
595+
$dateToTimestamp = strtotime($dateTo);
610596
$offset = $lineNumber;
611597
$entries = [];
612598
foreach ($data[$whichLog] as $entry) {
613599
$timestamp = strtotime($entry['date']);
614600
if (
615-
$timestamp >= $filterTsFrom
616-
&& $timestamp <= $filterTsTo
601+
$timestamp >= $dateFromTimestamp
602+
&& $timestamp <= $dateToTimestamp
617603
&& (in_array('*', $filterUsers)
618604
|| in_array($entry['username'], $filterUsers))
619605
) {
@@ -1070,40 +1056,28 @@ public function createTrackingForMultipleTables(string $db, array $selected, str
10701056
/**
10711057
* Function to get the entries
10721058
*
1073-
* @param array $data data
1074-
* @param int $filter_ts_from filter time stamp from
1075-
* @param int $filter_ts_to filter time stamp to
1076-
* @param array $filter_users filter users
1059+
* @param array $data data
1060+
* @param array $filter_users filter users
10771061
* @phpstan-param 'schema'|'data'|'schema_and_data' $logType
10781062
*
10791063
* @return array
10801064
*/
1081-
public function getEntries(array $data, $filter_ts_from, $filter_ts_to, array $filter_users, string $logType)
1065+
public function getEntries(array $data, array $filter_users, string $logType, string $dateFrom, string $dateTo)
10821066
{
10831067
$entries = [];
10841068
// Filtering data definition statements
10851069
if ($logType === 'schema' || $logType === 'schema_and_data') {
10861070
$entries = array_merge(
10871071
$entries,
1088-
$this->filter(
1089-
$data['ddlog'],
1090-
$filter_ts_from,
1091-
$filter_ts_to,
1092-
$filter_users
1093-
)
1072+
$this->filter($data['ddlog'], $filter_users, $dateFrom, $dateTo)
10941073
);
10951074
}
10961075

10971076
// Filtering data manipulation statements
10981077
if ($logType === 'data' || $logType === 'schema_and_data') {
10991078
$entries = array_merge(
11001079
$entries,
1101-
$this->filter(
1102-
$data['dmlog'],
1103-
$filter_ts_from,
1104-
$filter_ts_to,
1105-
$filter_users
1106-
)
1080+
$this->filter($data['dmlog'], $filter_users, $dateFrom, $dateTo)
11071081
);
11081082
}
11091083

psalm-baseline.xml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<files psalm-version="4.29.0@7ec5ffbd5f68ae03782d7fd33fff0c45a69f95b3">
2+
<files psalm-version="4.30.0@d0bc6e25d89f649e4f36a534f330f8bb4643dd69">
33
<file src="libraries/classes/Advisory/Advisor.php">
44
<MixedArgument occurrences="8">
55
<code>$matches[1]</code>
@@ -4234,11 +4234,9 @@
42344234
<MixedArgument occurrences="1">
42354235
<code>$version</code>
42364236
</MixedArgument>
4237-
<MixedAssignment occurrences="11">
4237+
<MixedAssignment occurrences="9">
42384238
<code>$GLOBALS['entries']</code>
42394239
<code>$GLOBALS['errorUrl']</code>
4240-
<code>$GLOBALS['filter_ts_from']</code>
4241-
<code>$GLOBALS['filter_ts_to']</code>
42424240
<code>$GLOBALS['filter_users']</code>
42434241
<code>$GLOBALS['msg']</code>
42444242
<code>$reportExport</code>
@@ -4540,8 +4538,7 @@
45404538
<PossiblyInvalidArgument occurrences="1">
45414539
<code>$post_key</code>
45424540
</PossiblyInvalidArgument>
4543-
<PossiblyInvalidCast occurrences="2">
4544-
<code>$_ENV[$var_name]</code>
4541+
<PossiblyInvalidCast occurrences="1">
45454542
<code>$_SERVER[$var_name]</code>
45464543
</PossiblyInvalidCast>
45474544
<PossiblyNullReference occurrences="1">

0 commit comments

Comments
 (0)