Total Complexity | 55 |
Total Lines | 502 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like LogHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LogHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class LogHelper |
||
33 | { |
||
34 | /** |
||
35 | * @param int $requestId |
||
36 | * |
||
37 | * @return DataObject[] |
||
38 | */ |
||
39 | public static function getRequestLogsWithComments( |
||
40 | $requestId, |
||
41 | PdoDatabase $db, |
||
42 | ISecurityManager $securityManager |
||
43 | ): array { |
||
44 | // FIXME: domains |
||
45 | $logs = LogSearchHelper::get($db, 1)->byObjectType('Request')->byObjectId($requestId)->fetch(); |
||
46 | |||
47 | $currentUser = User::getCurrent($db); |
||
48 | $showRestrictedComments = $securityManager->allows('RequestData', 'seeRestrictedComments', $currentUser) === ISecurityManager::ALLOWED; |
||
49 | $showCheckuserComments = $securityManager->allows('RequestData', 'seeCheckuserComments', $currentUser) === ISecurityManager::ALLOWED; |
||
50 | |||
51 | $comments = Comment::getForRequest($requestId, $db, $showRestrictedComments, $showCheckuserComments, $currentUser->getId()); |
||
52 | |||
53 | $items = array_merge($logs, $comments); |
||
54 | |||
55 | $sortKey = function(DataObject $item): int { |
||
56 | if ($item instanceof Log) { |
||
57 | return $item->getTimestamp()->getTimestamp(); |
||
58 | } |
||
59 | |||
60 | if ($item instanceof Comment) { |
||
61 | return $item->getTime()->getTimestamp(); |
||
62 | } |
||
63 | |||
64 | return 0; |
||
65 | }; |
||
66 | |||
67 | do { |
||
68 | $flag = false; |
||
69 | |||
70 | $loopLimit = (count($items) - 1); |
||
71 | for ($i = 0; $i < $loopLimit; $i++) { |
||
72 | // are these two items out of order? |
||
73 | if ($sortKey($items[$i]) > $sortKey($items[$i + 1])) { |
||
74 | // swap them |
||
75 | $swap = $items[$i]; |
||
76 | $items[$i] = $items[$i + 1]; |
||
77 | $items[$i + 1] = $swap; |
||
78 | |||
79 | // set a flag to say we've modified the array this time around |
||
80 | $flag = true; |
||
81 | } |
||
82 | } |
||
83 | } |
||
84 | while ($flag); |
||
85 | |||
86 | return $items; |
||
87 | } |
||
88 | |||
89 | public static function getLogDescription(Log $entry): string |
||
90 | { |
||
91 | $text = "Deferred to "; |
||
92 | if (substr($entry->getAction(), 0, strlen($text)) == $text) { |
||
93 | // Deferred to a different queue |
||
94 | // This is exactly what we want to display. |
||
95 | return $entry->getAction(); |
||
96 | } |
||
97 | |||
98 | $text = "Closed custom-n"; |
||
99 | if ($entry->getAction() == $text) { |
||
100 | // Custom-closed |
||
101 | return "closed (custom reason - account not created)"; |
||
102 | } |
||
103 | |||
104 | $text = "Closed custom-y"; |
||
105 | if ($entry->getAction() == $text) { |
||
106 | // Custom-closed |
||
107 | return "closed (custom reason - account created)"; |
||
108 | } |
||
109 | |||
110 | $text = "Closed 0"; |
||
111 | if ($entry->getAction() == $text) { |
||
112 | // Dropped the request - short-circuit the lookup |
||
113 | return "dropped request"; |
||
114 | } |
||
115 | |||
116 | $text = "Closed "; |
||
117 | if (substr($entry->getAction(), 0, strlen($text)) == $text) { |
||
118 | // Closed with a reason - do a lookup here. |
||
119 | $id = substr($entry->getAction(), strlen($text)); |
||
120 | /** @var EmailTemplate|false $template */ |
||
121 | $template = EmailTemplate::getById((int)$id, $entry->getDatabase()); |
||
122 | |||
123 | if ($template !== false) { |
||
|
|||
124 | return 'closed (' . $template->getName() . ')'; |
||
125 | } |
||
126 | } |
||
127 | |||
128 | // Fall back to the basic stuff |
||
129 | $lookup = array( |
||
130 | 'Reserved' => 'reserved', |
||
131 | 'Email Confirmed' => 'email-confirmed', |
||
132 | 'Manually Confirmed' => 'manually confirmed the request', |
||
133 | 'Unreserved' => 'unreserved', |
||
134 | 'Approved' => 'approved', |
||
135 | 'Suspended' => 'suspended', |
||
136 | 'RoleChange' => 'changed roles', |
||
137 | 'GlobalRoleChange' => 'changed global roles', |
||
138 | 'Banned' => 'banned', |
||
139 | 'Edited' => 'edited interface message', |
||
140 | 'Declined' => 'declined', |
||
141 | 'EditComment-c' => 'edited a comment', |
||
142 | 'EditComment-r' => 'edited a comment', |
||
143 | 'FlaggedComment' => 'flagged a comment', |
||
144 | 'UnflaggedComment' => 'unflagged a comment', |
||
145 | 'Unbanned' => 'unbanned', |
||
146 | 'BanReplaced' => 'replaced ban', |
||
147 | 'Promoted' => 'promoted to tool admin', |
||
148 | 'BreakReserve' => 'forcibly broke the reservation', |
||
149 | 'Prefchange' => 'changed user preferences', |
||
150 | 'Renamed' => 'renamed', |
||
151 | 'Demoted' => 'demoted from tool admin', |
||
152 | 'ReceiveReserved' => 'received the reservation', |
||
153 | 'SendReserved' => 'sent the reservation', |
||
154 | 'EditedEmail' => 'edited email', |
||
155 | 'DeletedTemplate' => 'deleted template', |
||
156 | 'EditedTemplate' => 'edited template', |
||
157 | 'CreatedEmail' => 'created email', |
||
158 | 'CreatedTemplate' => 'created template', |
||
159 | 'SentMail' => 'sent an email to the requester', |
||
160 | 'Registered' => 'registered a tool account', |
||
161 | 'JobIssue' => 'ran a background job unsuccessfully', |
||
162 | 'JobCompleted' => 'completed a background job', |
||
163 | 'JobAcknowledged' => 'acknowledged a job failure', |
||
164 | 'JobRequeued' => 'requeued a job for re-execution', |
||
165 | 'JobCancelled' => 'cancelled execution of a job', |
||
166 | 'EnqueuedJobQueue' => 'scheduled for creation', |
||
167 | 'Hospitalised' => 'sent to the hospital', |
||
168 | 'QueueCreated' => 'created a request queue', |
||
169 | 'QueueEdited' => 'edited a request queue', |
||
170 | 'DomainCreated' => 'created a domain', |
||
171 | 'DomainEdited' => 'edited a domain', |
||
172 | 'RequestFormCreated' => 'created a request form', |
||
173 | 'RequestFormEdited' => 'edited a request form', |
||
174 | ); |
||
175 | |||
176 | if (array_key_exists($entry->getAction(), $lookup)) { |
||
177 | return $lookup[$entry->getAction()]; |
||
178 | } |
||
179 | |||
180 | // OK, I don't know what this is. Fall back to something sane. |
||
181 | return "performed an unknown action ({$entry->getAction()})"; |
||
182 | } |
||
183 | |||
184 | public static function getLogActions(PdoDatabase $database): array |
||
185 | { |
||
186 | $lookup = array( |
||
187 | "Requests" => [ |
||
188 | 'Reserved' => 'reserved', |
||
189 | 'Email Confirmed' => 'email-confirmed', |
||
190 | 'Manually Confirmed' => 'manually confirmed', |
||
191 | 'Unreserved' => 'unreserved', |
||
192 | 'EditComment-c' => 'edited a comment (by comment ID)', |
||
193 | 'EditComment-r' => 'edited a comment (by request)', |
||
194 | 'FlaggedComment' => 'flagged a comment', |
||
195 | 'UnflaggedComment' => 'unflagged a comment', |
||
196 | 'BreakReserve' => 'forcibly broke the reservation', |
||
197 | 'ReceiveReserved' => 'received the reservation', |
||
198 | 'SendReserved' => 'sent the reservation', |
||
199 | 'SentMail' => 'sent an email to the requester', |
||
200 | 'Closed 0' => 'dropped request', |
||
201 | 'Closed custom-y' => 'closed (custom reason - account created)', |
||
202 | 'Closed custom-n' => 'closed (custom reason - account not created)', |
||
203 | ], |
||
204 | 'Users' => [ |
||
205 | 'Approved' => 'approved', |
||
206 | 'Suspended' => 'suspended', |
||
207 | 'RoleChange' => 'changed roles', |
||
208 | 'GlobalRoleChange' => 'changed global roles', |
||
209 | 'Declined' => 'declined', |
||
210 | 'Prefchange' => 'changed user preferences', |
||
211 | 'Renamed' => 'renamed', |
||
212 | 'Promoted' => 'promoted to tool admin', |
||
213 | 'Demoted' => 'demoted from tool admin', |
||
214 | 'Registered' => 'registered a tool account', |
||
215 | ], |
||
216 | "Bans" => [ |
||
217 | 'Banned' => 'banned', |
||
218 | 'Unbanned' => 'unbanned', |
||
219 | 'BanReplaced' => 'replaced ban', |
||
220 | ], |
||
221 | "Site notice" => [ |
||
222 | 'Edited' => 'edited interface message', |
||
223 | ], |
||
224 | "Email close templates" => [ |
||
225 | 'EditedEmail' => 'edited email', |
||
226 | 'CreatedEmail' => 'created email', |
||
227 | ], |
||
228 | "Welcome templates" => [ |
||
229 | 'DeletedTemplate' => 'deleted template', |
||
230 | 'EditedTemplate' => 'edited template', |
||
231 | 'CreatedTemplate' => 'created template', |
||
232 | ], |
||
233 | "Job queue" => [ |
||
234 | 'JobIssue' => 'ran a background job unsuccessfully', |
||
235 | 'JobCompleted' => 'completed a background job', |
||
236 | 'JobAcknowledged' => 'acknowledged a job failure', |
||
237 | 'JobRequeued' => 'requeued a job for re-execution', |
||
238 | 'JobCancelled' => 'cancelled execution of a job', |
||
239 | 'EnqueuedJobQueue' => 'scheduled for creation', |
||
240 | 'Hospitalised' => 'sent to the hospital', |
||
241 | ], |
||
242 | "Request queues" => [ |
||
243 | 'QueueCreated' => 'created a request queue', |
||
244 | 'QueueEdited' => 'edited a request queue', |
||
245 | ], |
||
246 | "Domains" => [ |
||
247 | 'DomainCreated' => 'created a domain', |
||
248 | 'DomainEdited' => 'edited a domain', |
||
249 | ], |
||
250 | "Request forms" => [ |
||
251 | 'RequestFormCreated' => 'created a request form', |
||
252 | 'RequestFormEdited' => 'edited a request form', |
||
253 | ], |
||
254 | ); |
||
255 | |||
256 | $databaseDrivenLogKeys = $database->query(<<<SQL |
||
257 | SELECT CONCAT('Closed ', id) AS k, CONCAT('closed (',name,')') AS v FROM emailtemplate |
||
258 | UNION ALL |
||
259 | SELECT CONCAT('Deferred to ', logname) AS k, CONCAT('deferred to ', displayname) AS v FROM requestqueue; |
||
260 | SQL |
||
261 | ); |
||
262 | foreach ($databaseDrivenLogKeys->fetchAll(PDO::FETCH_ASSOC) as $row) { |
||
263 | $lookup["Requests"][$row['k']] = $row['v']; |
||
264 | } |
||
265 | |||
266 | return $lookup; |
||
267 | } |
||
268 | |||
269 | public static function getObjectTypes(): array |
||
270 | { |
||
271 | return array( |
||
272 | 'Ban' => 'Ban', |
||
273 | 'Comment' => 'Comment', |
||
274 | 'EmailTemplate' => 'Email template', |
||
275 | 'JobQueue' => 'Job queue item', |
||
276 | 'Request' => 'Request', |
||
277 | 'SiteNotice' => 'Site notice', |
||
278 | 'User' => 'User', |
||
279 | 'WelcomeTemplate' => 'Welcome template', |
||
280 | 'RequestQueue' => 'Request queue', |
||
281 | 'Domain' => 'Domain', |
||
282 | 'RequestForm' => 'Request form' |
||
283 | ); |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * This returns an HTML representation of the object |
||
288 | * |
||
289 | * @param int $objectId |
||
290 | * @param string $objectType |
||
291 | * |
||
292 | * @category Security-Critical |
||
293 | */ |
||
294 | private static function getObjectDescription( |
||
295 | $objectId, |
||
296 | $objectType, |
||
297 | PdoDatabase $database, |
||
298 | SiteConfiguration $configuration |
||
299 | ): ?string { |
||
300 | if ($objectType == '') { |
||
301 | return null; |
||
302 | } |
||
303 | |||
304 | $baseurl = $configuration->getBaseUrl(); |
||
305 | |||
306 | switch ($objectType) { |
||
307 | case 'Ban': |
||
308 | /** @var Ban $ban */ |
||
309 | $ban = Ban::getById($objectId, $database); |
||
310 | |||
311 | if ($ban === false) { |
||
312 | return 'Ban #' . $objectId; |
||
313 | } |
||
314 | |||
315 | return <<<HTML |
||
316 | <a href="{$baseurl}/internal.php/bans/show?id={$objectId}">Ban #{$objectId}</a> |
||
317 | HTML; |
||
318 | case 'EmailTemplate': |
||
319 | /** @var EmailTemplate $emailTemplate */ |
||
320 | $emailTemplate = EmailTemplate::getById($objectId, $database); |
||
321 | |||
322 | if ($emailTemplate === false) { |
||
323 | return 'Email Template #' . $objectId; |
||
324 | } |
||
325 | |||
326 | $name = htmlentities($emailTemplate->getName(), ENT_COMPAT, 'UTF-8'); |
||
327 | |||
328 | return <<<HTML |
||
329 | <a href="{$baseurl}/internal.php/emailManagement/view?id={$objectId}">Email Template #{$objectId} ({$name})</a> |
||
330 | HTML; |
||
331 | case 'SiteNotice': |
||
332 | return "<a href=\"{$baseurl}/internal.php/siteNotice\">the site notice</a>"; |
||
333 | case 'Request': |
||
334 | /** @var Request $request */ |
||
335 | $request = Request::getById($objectId, $database); |
||
336 | |||
337 | if ($request === false) { |
||
338 | return 'Request #' . $objectId; |
||
339 | } |
||
340 | |||
341 | $name = htmlentities($request->getName(), ENT_COMPAT, 'UTF-8'); |
||
342 | |||
343 | return <<<HTML |
||
344 | <a href="{$baseurl}/internal.php/viewRequest?id={$objectId}">Request #{$objectId} ({$name})</a> |
||
345 | HTML; |
||
346 | case 'User': |
||
347 | /** @var User $user */ |
||
348 | $user = User::getById($objectId, $database); |
||
349 | |||
350 | // Some users were merged out of existence |
||
351 | if ($user === false) { |
||
352 | return 'User #' . $objectId; |
||
353 | } |
||
354 | |||
355 | $username = htmlentities($user->getUsername(), ENT_COMPAT, 'UTF-8'); |
||
356 | |||
357 | return "<a href=\"{$baseurl}/internal.php/statistics/users/detail?user={$objectId}\">{$username}</a>"; |
||
358 | case 'WelcomeTemplate': |
||
359 | /** @var WelcomeTemplate $welcomeTemplate */ |
||
360 | $welcomeTemplate = WelcomeTemplate::getById($objectId, $database); |
||
361 | |||
362 | // some old templates have been completely deleted and lost to the depths of time. |
||
363 | if ($welcomeTemplate === false) { |
||
364 | return "Welcome template #{$objectId}"; |
||
365 | } |
||
366 | else { |
||
367 | $userCode = htmlentities($welcomeTemplate->getUserCode(), ENT_COMPAT, 'UTF-8'); |
||
368 | |||
369 | return "<a href=\"{$baseurl}/internal.php/welcomeTemplates/view?template={$objectId}\">{$userCode}</a>"; |
||
370 | } |
||
371 | case 'JobQueue': |
||
372 | /** @var JobQueue $job */ |
||
373 | $job = JobQueue::getById($objectId, $database); |
||
374 | |||
375 | $taskDescriptions = JobQueue::getTaskDescriptions(); |
||
376 | |||
377 | if ($job === false) { |
||
378 | return 'Job Queue Task #' . $objectId; |
||
379 | } |
||
380 | |||
381 | $task = $job->getTask(); |
||
382 | if (isset($taskDescriptions[$task])) { |
||
383 | $description = $taskDescriptions[$task]; |
||
384 | } |
||
385 | else { |
||
386 | $description = 'Unknown task'; |
||
387 | } |
||
388 | |||
389 | return "<a href=\"{$baseurl}/internal.php/jobQueue/view?id={$objectId}\">Job #{$job->getId()} ({$description})</a>"; |
||
390 | case 'RequestQueue': |
||
391 | /** @var RequestQueue $queue */ |
||
392 | $queue = RequestQueue::getById($objectId, $database); |
||
393 | |||
394 | if ($queue === false) { |
||
395 | return "Request Queue #{$objectId}"; |
||
396 | } |
||
397 | |||
398 | $queueHeader = htmlentities($queue->getHeader(), ENT_COMPAT, 'UTF-8'); |
||
399 | |||
400 | return "<a href=\"{$baseurl}/internal.php/queueManagement/edit?queue={$objectId}\">{$queueHeader}</a>"; |
||
401 | case 'Domain': |
||
402 | /** @var Domain $domain */ |
||
403 | $domain = Domain::getById($objectId, $database); |
||
404 | |||
405 | if ($domain === false) { |
||
406 | return "Domain #{$objectId}"; |
||
407 | } |
||
408 | |||
409 | $domainName = htmlentities($domain->getShortName(), ENT_COMPAT, 'UTF-8'); |
||
410 | return "<a href=\"{$baseurl}/internal.php/domainManagement/edit?domain={$objectId}\">{$domainName}</a>"; |
||
411 | case 'RequestForm': |
||
412 | /** @var RequestForm $queue */ |
||
413 | $queue = RequestForm::getById($objectId, $database); |
||
414 | |||
415 | if ($queue === false) { |
||
416 | return "Request Form #{$objectId}"; |
||
417 | } |
||
418 | |||
419 | $formName = htmlentities($queue->getName(), ENT_COMPAT, 'UTF-8'); |
||
420 | |||
421 | return "<a href=\"{$baseurl}/internal.php/requestFormManagement/edit?form={$objectId}\">{$formName}</a>"; |
||
422 | case 'Comment': |
||
423 | /** @var Comment $comment */ |
||
424 | $comment = Comment::getById($objectId, $database); |
||
425 | /** @var Request $request */ |
||
426 | $request = Request::getById($comment->getRequest(), $database); |
||
427 | $requestName = htmlentities($request->getName(), ENT_COMPAT, 'UTF-8'); |
||
428 | |||
429 | return "<a href=\"{$baseurl}/internal.php/editComment?id={$objectId}\">Comment {$objectId}</a> on request <a href=\"{$baseurl}/internal.php/viewRequest?id={$comment->getRequest()}#comment-{$objectId}\">#{$comment->getRequest()} ({$requestName})</a>"; |
||
430 | default: |
||
431 | return '[' . $objectType . " " . $objectId . ']'; |
||
432 | } |
||
433 | } |
||
434 | |||
435 | /** |
||
436 | * @param Log[] $logs |
||
437 | * |
||
438 | * @throws Exception |
||
439 | */ |
||
440 | public static function prepareLogsForTemplate(array $logs, PdoDatabase $database, SiteConfiguration $configuration): array |
||
534 | } |
||
535 | } |
||
536 |