1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics) |
6
|
|
|
* |
7
|
|
|
* This program is free software; you can redistribute it and/or |
8
|
|
|
* modify it under the terms of the GNU General Public License |
9
|
|
|
* as published by the Free Software Foundation; either version 2 |
10
|
|
|
* of the License, or (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License |
18
|
|
|
* along with this program; if not, write to the Free Software |
19
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace App\Command; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
use App\Entity\Base\NamedDBElement; |
26
|
|
|
use App\Entity\LogSystem\AbstractLogEntry; |
27
|
|
|
use App\Services\ElementTypeNameGenerator; |
28
|
|
|
use App\Services\LogSystem\LogEntryExtraFormatter; |
29
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
30
|
|
|
use Symfony\Component\Console\Command\Command; |
31
|
|
|
use Symfony\Component\Console\Helper\Table; |
32
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
33
|
|
|
use Symfony\Component\Console\Input\InputOption; |
34
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
35
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
36
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
37
|
|
|
|
38
|
|
|
class ShowEventLogCommand extends Command |
39
|
|
|
{ |
40
|
|
|
protected static $defaultName = 'app:show-logs'; |
41
|
|
|
protected $entityManager; |
42
|
|
|
protected $translator; |
43
|
|
|
protected $elementTypeNameGenerator; |
44
|
|
|
protected $repo; |
45
|
|
|
protected $formatter; |
46
|
|
|
|
47
|
|
|
public function __construct(EntityManagerInterface $entityManager, |
48
|
|
|
TranslatorInterface $translator, ElementTypeNameGenerator $elementTypeNameGenerator, LogEntryExtraFormatter $formatter) |
49
|
|
|
{ |
50
|
|
|
$this->entityManager = $entityManager; |
51
|
|
|
$this->translator = $translator; |
52
|
|
|
$this->elementTypeNameGenerator = $elementTypeNameGenerator; |
53
|
|
|
$this->formatter = $formatter; |
54
|
|
|
|
55
|
|
|
$this->repo = $this->entityManager->getRepository(AbstractLogEntry::class); |
56
|
|
|
parent::__construct(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function configure() |
60
|
|
|
{ |
61
|
|
|
$this |
62
|
|
|
->setDescription('List the last event log entries.') |
63
|
|
|
->addOption('count', 'c', InputOption::VALUE_REQUIRED, 'How many log entries should be shown per page.', 50 ) |
64
|
|
|
->addOption('oldest_first', null, InputOption::VALUE_NONE,'Show older entries first.') |
65
|
|
|
->addOption('page', 'p', InputOption::VALUE_REQUIRED, 'Which page should be shown?', 1) |
66
|
|
|
->addOption('onePage', null, InputOption::VALUE_NONE, 'Show only one page (dont ask to go to next).') |
67
|
|
|
->addOption('showExtra', 'x', InputOption::VALUE_NONE, 'Show a column with the extra data.'); |
68
|
|
|
; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
72
|
|
|
{ |
73
|
|
|
$io = new SymfonyStyle($input, $output); |
74
|
|
|
|
75
|
|
|
$onePage = $input->getOption('onePage'); |
76
|
|
|
|
77
|
|
|
$desc = $input->getOption('oldest_first'); |
78
|
|
|
$limit = $input->getOption('count'); |
79
|
|
|
$page = $input->getOption('page'); |
80
|
|
|
$showExtra = $input->getOption('showExtra'); |
81
|
|
|
|
82
|
|
|
$total_count = $this->repo->count([]); |
83
|
|
|
$max_page = ceil($total_count / $limit); |
84
|
|
|
|
85
|
|
|
if ($page > $max_page) { |
86
|
|
|
$io->error("There is no page $page! The maximum page is $max_page."); |
87
|
|
|
return 1; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$io->note("There are a total of $total_count log entries in the DB."); |
91
|
|
|
|
92
|
|
|
$continue = true; |
93
|
|
|
while ($continue && $page <= $max_page) { |
94
|
|
|
$this->showPage($output, $desc, $limit, $page, $max_page, $showExtra); |
95
|
|
|
|
96
|
|
|
if ($onePage) { |
97
|
|
|
return 0; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$continue = $io->confirm('Do you want to show the next page?'); |
101
|
|
|
$page++; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return 0; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function showPage(OutputInterface $output, bool $desc, int $limit, int $page, int $max_page, bool $showExtra): void |
108
|
|
|
{ |
109
|
|
|
$sorting = $desc ? 'ASC' : 'DESC'; |
110
|
|
|
$offset = ($page - 1) * $limit; |
111
|
|
|
|
112
|
|
|
/** @var AbstractLogEntry[] $entries */ |
113
|
|
|
$entries = $this->repo->getLogsOrderedByTimestamp($sorting, $limit, $offset); |
114
|
|
|
|
115
|
|
|
$table = new Table($output); |
116
|
|
|
$table->setHeaderTitle("Page $page / $max_page"); |
117
|
|
|
$headers = ['ID', 'Timestamp', 'Type', 'User', 'Target Type', 'Target']; |
118
|
|
|
if ($showExtra) { |
119
|
|
|
$headers[] = 'Extra data'; |
120
|
|
|
} |
121
|
|
|
$table->setHeaders($headers); |
122
|
|
|
|
123
|
|
|
foreach ($entries as $entry) { |
124
|
|
|
$this->addTableRow($table, $entry, $showExtra); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$table->render(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
protected function addTableRow(Table $table, AbstractLogEntry $entry, bool $showExtra): void |
131
|
|
|
{ |
132
|
|
|
$target = $this->repo->getTargetElement($entry); |
133
|
|
|
$target_name = ""; |
134
|
|
|
if ($target instanceof NamedDBElement) { |
135
|
|
|
$target_name = $target->getName() . ' <info>(' . $target->getID() . ')</info>'; |
136
|
|
|
} elseif ($entry->getTargetID()) { |
|
|
|
|
137
|
|
|
$target_name = '<info>(' . $entry->getTargetID() . ')</info>'; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$target_class = ""; |
141
|
|
|
if ($entry->getTargetClass() !== null) { |
142
|
|
|
$target_class = $this->elementTypeNameGenerator->getLocalizedTypeLabel($entry->getTargetClass()); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$row = [ |
146
|
|
|
$entry->getID(), |
147
|
|
|
$entry->getTimestamp()->format('Y-m-d H:i:s'), |
148
|
|
|
$entry->getType(), |
149
|
|
|
$entry->getUser()->getFullName(true), |
150
|
|
|
$target_class, |
151
|
|
|
$target_name |
152
|
|
|
]; |
153
|
|
|
|
154
|
|
|
if ($showExtra) { |
155
|
|
|
$row[] = $this->formatter->formatConsole($entry); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$table->addRow($row); |
159
|
|
|
} |
160
|
|
|
} |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: