Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Database / DatabaseLogBase.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Database\DatabaseLogBase.
6  */
7
8 namespace Drupal\Console\Command\Database;
9
10 use Drupal\Console\Core\Command\Shared\CommandTrait;
11 use Drupal\Core\Database\Connection;
12 use Drupal\Core\Datetime\DateFormatterInterface;
13 use Drupal\Core\Entity\EntityTypeManagerInterface;
14 use Drupal\Core\StringTranslation\TranslatableMarkup;
15 use Drupal\Core\StringTranslation\TranslationInterface;
16 use Drupal\user\UserStorageInterface;
17 use Symfony\Component\Console\Command\Command;
18 use Drupal\Core\Logger\RfcLogLevel;
19 use Drupal\user\Entity\User;
20 use Drupal\Component\Utility\Unicode;
21 use Drupal\Component\Utility\Html;
22 use Symfony\Component\Console\Input\InputOption;
23 use Symfony\Component\Console\Input\InputInterface;
24 use Drupal\Console\Core\Style\DrupalStyle;
25
26 /**
27  * Class DatabaseLogBase
28  *
29  * @package Drupal\Console\Command\Database
30  */
31 abstract class DatabaseLogBase extends Command
32 {
33     use CommandTrait;
34
35     /**
36      * @var Connection
37      */
38     protected $database;
39
40     /**
41      * @var DateFormatterInterface
42      */
43     protected $dateFormatter;
44
45     /**
46      * @var EntityTypeManagerInterface
47      */
48     protected $entityTypeManager;
49
50     /**
51      * @var TranslationInterface
52      */
53     protected $stringTranslation;
54
55     /**
56      * @var UserStorageInterface
57      */
58     protected $userStorage;
59
60     /**
61      * @var TranslatableMarkup[]
62      */
63     protected $severityList;
64
65     /**
66      * @var null|string
67      */
68     protected $eventType;
69
70     /**
71      * @var null|string
72      */
73     protected $eventSeverity;
74
75     /**
76      * @var null|string
77      */
78     protected $userId;
79
80     /**
81      * LogDebugCommand constructor.
82      *
83      * @param Connection                 $database
84      * @param DateFormatterInterface     $dateFormatter
85      * @param EntityTypeManagerInterface $entityTypeManager
86      * @param TranslationInterface       $stringTranslation
87      */
88     public function __construct(
89         Connection $database,
90         DateFormatterInterface $dateFormatter,
91         EntityTypeManagerInterface $entityTypeManager,
92         TranslationInterface $stringTranslation
93     ) {
94         $this->database = $database;
95         $this->dateFormatter = $dateFormatter;
96         $this->entityTypeManager = $entityTypeManager;
97         $this->stringTranslation = $stringTranslation;
98         $this->userStorage = $this->entityTypeManager->getStorage('user');
99         $this->severityList = RfcLogLevel::getLevels();
100         parent::__construct();
101     }
102
103     /**
104      * addDefaultLoggingOptions.
105      */
106     protected function addDefaultLoggingOptions()
107     {
108         $this
109             ->addOption(
110                 'type',
111                 null,
112                 InputOption::VALUE_OPTIONAL,
113                 $this->trans('commands.database.log.common.options.type')
114             )
115             ->addOption(
116                 'severity',
117                 null,
118                 InputOption::VALUE_OPTIONAL,
119                 $this->trans('commands.database.log.common.options.severity')
120             )
121             ->addOption(
122                 'user-id',
123                 null,
124                 InputOption::VALUE_OPTIONAL,
125                 $this->trans('commands.database.log.common.options.user-id')
126             );
127     }
128
129     /**
130      * @param InputInterface $input
131      */
132     protected function getDefaultOptions(InputInterface $input)
133     {
134         $this->eventType = $input->getOption('type');
135         $this->eventSeverity = $input->getOption('severity');
136         $this->userId = $input->getOption('user-id');
137     }
138
139     /**
140      * @param DrupalStyle $io
141      * @param null        $offset
142      * @param int         $range
143      * @return bool|\Drupal\Core\Database\Query\SelectInterface
144      */
145     protected function makeQuery(DrupalStyle $io, $offset = null, $range = 1000)
146     {
147         $query = $this->database->select('watchdog', 'w');
148         $query->fields(
149             'w',
150             [
151                 'wid',
152                 'uid',
153                 'severity',
154                 'type',
155                 'timestamp',
156                 'message',
157                 'variables',
158             ]
159         );
160
161         if ($this->eventType) {
162             $query->condition('type', $this->eventType);
163         }
164
165         if ($this->eventSeverity) {
166             if (!in_array($this->eventSeverity, $this->severityList)) {
167                 $io->error(
168                     sprintf(
169                         $this->trans('database.log.common.messages.invalid-severity'),
170                         $this->eventSeverity
171                     )
172                 );
173                 return false;
174             }
175             $query->condition(
176                 'severity',
177                 array_search(
178                     $this->eventSeverity,
179                     $this->severityList
180                 )
181             );
182         }
183
184         if ($this->userId) {
185             $query->condition('uid', $this->userId);
186         }
187
188         $query->orderBy('wid', 'ASC');
189
190         if ($offset) {
191             $query->range($offset, $range);
192         }
193
194         return $query;
195     }
196
197     /**
198      * Generic logging table header
199      *
200      * @return array
201      */
202     protected function createTableHeader()
203     {
204         return [
205         $this->trans('commands.database.log.common.messages.event-id'),
206         $this->trans('commands.database.log.common.messages.type'),
207         $this->trans('commands.database.log.common.messages.date'),
208         $this->trans('commands.database.log.common.messages.message'),
209         $this->trans('commands.database.log.common.messages.user'),
210         $this->trans('commands.database.log.common.messages.severity'),
211         ];
212     }
213
214     /**
215      * @param \stdClass $dblog
216      * @return array
217      */
218     protected function createTableRow(\stdClass $dblog)
219     {
220
221         /**
222          * @var User $user
223          */
224         $user = $this->userStorage->load($dblog->uid);
225
226         return [
227             $dblog->wid,
228             $dblog->type,
229             $this->dateFormatter->format($dblog->timestamp, 'short'),
230             Unicode::truncate(
231                 Html::decodeEntities(strip_tags($this->formatMessage($dblog))),
232                 500,
233                 true,
234                 true
235             ),
236             $user->getUsername() . ' (' . $user->id() . ')',
237             $this->severityList[$dblog->severity]->render(),
238         ];
239     }
240
241     /**
242      * Formats a database log message.
243      *
244      * @param $event
245      *   The record from the watchdog table. The object properties are: wid, uid,
246      *   severity, type, timestamp, message, variables, link, name.
247      *
248      * @return string|false
249      *   The formatted log message or FALSE if the message or variables properties
250      *   are not set.
251      */
252     protected function formatMessage(\stdClass $event)
253     {
254         $message = false;
255
256         // Check for required properties.
257         if (isset($event->message, $event->variables)) {
258             // Messages without variables or user specified text.
259             if ($event->variables === 'N;') {
260                 return $event->message;
261             }
262
263             return $this->stringTranslation->translate(
264                 $event->message,
265                 unserialize($event->variables)
266             );
267         }
268
269         return $message;
270     }
271
272     /**
273      * @param $dblog
274      * @return array
275      */
276     protected function formatSingle($dblog)
277     {
278         return array_combine(
279             $this->createTableHeader(),
280             $this->createTableRow($dblog)
281         );
282     }
283 }