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