stwalkerster /
waca
| 1 | <?php |
||||
| 2 | /****************************************************************************** |
||||
| 3 | * Wikipedia Account Creation Assistance tool * |
||||
| 4 | * * |
||||
| 5 | * All code in this file is released into the public domain by the ACC * |
||||
| 6 | * Development Team. Please see team.json for a list of contributors. * |
||||
| 7 | ******************************************************************************/ |
||||
| 8 | |||||
| 9 | namespace Waca\Helpers; |
||||
| 10 | |||||
| 11 | use Exception; |
||||
| 12 | use PDO; |
||||
| 13 | use Waca\DataObject; |
||||
| 14 | use Waca\DataObjects\Ban; |
||||
| 15 | use Waca\DataObjects\Comment; |
||||
| 16 | use Waca\DataObjects\Domain; |
||||
| 17 | use Waca\DataObjects\EmailTemplate; |
||||
| 18 | use Waca\DataObjects\JobQueue; |
||||
| 19 | use Waca\DataObjects\Log; |
||||
| 20 | use Waca\DataObjects\Request; |
||||
| 21 | use Waca\DataObjects\RequestForm; |
||||
| 22 | use Waca\DataObjects\RequestQueue; |
||||
| 23 | use Waca\DataObjects\User; |
||||
| 24 | use Waca\DataObjects\WelcomeTemplate; |
||||
| 25 | use Waca\Helpers\SearchHelpers\LogSearchHelper; |
||||
| 26 | use Waca\Helpers\SearchHelpers\UserSearchHelper; |
||||
| 27 | use Waca\PdoDatabase; |
||||
| 28 | use Waca\Security\SecurityManager; |
||||
| 29 | use Waca\SiteConfiguration; |
||||
| 30 | |||||
| 31 | class LogHelper |
||||
| 32 | { |
||||
| 33 | /** |
||||
| 34 | * Summary of getRequestLogsWithComments |
||||
| 35 | * |
||||
| 36 | * @param int $requestId |
||||
| 37 | * @param PdoDatabase $db |
||||
| 38 | * @param SecurityManager $securityManager |
||||
| 39 | * |
||||
| 40 | * @return DataObject[] |
||||
| 41 | */ |
||||
| 42 | public static function getRequestLogsWithComments($requestId, PdoDatabase $db, SecurityManager $securityManager) |
||||
| 43 | { |
||||
| 44 | $logs = LogSearchHelper::get($db)->byObjectType('Request')->byObjectId($requestId)->fetch(); |
||||
| 45 | |||||
| 46 | $currentUser = User::getCurrent($db); |
||||
| 47 | $showRestrictedComments = $securityManager->allows('RequestData', 'seeRestrictedComments', $currentUser) === SecurityManager::ALLOWED; |
||||
| 48 | $showCheckuserComments = $securityManager->allows('RequestData', 'seeCheckuserComments', $currentUser) === SecurityManager::ALLOWED; |
||||
| 49 | |||||
| 50 | $comments = Comment::getForRequest($requestId, $db, $showRestrictedComments, $showCheckuserComments, $currentUser->getId()); |
||||
| 51 | |||||
| 52 | $items = array_merge($logs, $comments); |
||||
| 53 | |||||
| 54 | /** |
||||
| 55 | * @param DataObject $item |
||||
| 56 | * |
||||
| 57 | * @return int |
||||
| 58 | */ |
||||
| 59 | $sortKey = function(DataObject $item) { |
||||
| 60 | if ($item instanceof Log) { |
||||
| 61 | return $item->getTimestamp()->getTimestamp(); |
||||
| 62 | } |
||||
| 63 | |||||
| 64 | if ($item instanceof Comment) { |
||||
| 65 | return $item->getTime()->getTimestamp(); |
||||
| 66 | } |
||||
| 67 | |||||
| 68 | return 0; |
||||
| 69 | }; |
||||
| 70 | |||||
| 71 | do { |
||||
| 72 | $flag = false; |
||||
| 73 | |||||
| 74 | $loopLimit = (count($items) - 1); |
||||
| 75 | for ($i = 0; $i < $loopLimit; $i++) { |
||||
| 76 | // are these two items out of order? |
||||
| 77 | if ($sortKey($items[$i]) > $sortKey($items[$i + 1])) { |
||||
| 78 | // swap them |
||||
| 79 | $swap = $items[$i]; |
||||
| 80 | $items[$i] = $items[$i + 1]; |
||||
| 81 | $items[$i + 1] = $swap; |
||||
| 82 | |||||
| 83 | // set a flag to say we've modified the array this time around |
||||
| 84 | $flag = true; |
||||
| 85 | } |
||||
| 86 | } |
||||
| 87 | } |
||||
| 88 | while ($flag); |
||||
| 89 | |||||
| 90 | return $items; |
||||
| 91 | } |
||||
| 92 | |||||
| 93 | /** |
||||
| 94 | * Summary of getLogDescription |
||||
| 95 | * |
||||
| 96 | * @param Log $entry |
||||
| 97 | * |
||||
| 98 | * @return string |
||||
| 99 | */ |
||||
| 100 | public static function getLogDescription(Log $entry) |
||||
| 101 | { |
||||
| 102 | $text = "Deferred to "; |
||||
| 103 | if (substr($entry->getAction(), 0, strlen($text)) == $text) { |
||||
| 104 | // Deferred to a different queue |
||||
| 105 | // This is exactly what we want to display. |
||||
| 106 | return $entry->getAction(); |
||||
| 107 | } |
||||
| 108 | |||||
| 109 | $text = "Closed custom-n"; |
||||
| 110 | if ($entry->getAction() == $text) { |
||||
| 111 | // Custom-closed |
||||
| 112 | return "closed (custom reason - account not created)"; |
||||
| 113 | } |
||||
| 114 | |||||
| 115 | $text = "Closed custom-y"; |
||||
| 116 | if ($entry->getAction() == $text) { |
||||
| 117 | // Custom-closed |
||||
| 118 | return "closed (custom reason - account created)"; |
||||
| 119 | } |
||||
| 120 | |||||
| 121 | $text = "Closed 0"; |
||||
| 122 | if ($entry->getAction() == $text) { |
||||
| 123 | // Dropped the request - short-circuit the lookup |
||||
| 124 | return "dropped request"; |
||||
| 125 | } |
||||
| 126 | |||||
| 127 | $text = "Closed "; |
||||
| 128 | if (substr($entry->getAction(), 0, strlen($text)) == $text) { |
||||
| 129 | // Closed with a reason - do a lookup here. |
||||
| 130 | $id = substr($entry->getAction(), strlen($text)); |
||||
| 131 | /** @var EmailTemplate $template */ |
||||
| 132 | $template = EmailTemplate::getById((int)$id, $entry->getDatabase()); |
||||
| 133 | |||||
| 134 | if ($template != false) { |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 135 | return "closed (" . $template->getName() . ")"; |
||||
| 136 | } |
||||
| 137 | } |
||||
| 138 | |||||
| 139 | // Fall back to the basic stuff |
||||
| 140 | $lookup = array( |
||||
| 141 | 'Reserved' => 'reserved', |
||||
| 142 | 'Email Confirmed' => 'email-confirmed', |
||||
| 143 | 'Manually Confirmed' => 'manually confirmed the request', |
||||
| 144 | 'Unreserved' => 'unreserved', |
||||
| 145 | 'Approved' => 'approved', |
||||
| 146 | 'Suspended' => 'suspended', |
||||
| 147 | 'RoleChange' => 'changed roles', |
||||
| 148 | 'Banned' => 'banned', |
||||
| 149 | 'Edited' => 'edited interface message', |
||||
| 150 | 'Declined' => 'declined', |
||||
| 151 | 'EditComment-c' => 'edited a comment', |
||||
| 152 | 'EditComment-r' => 'edited a comment', |
||||
| 153 | 'FlaggedComment' => 'flagged a comment', |
||||
| 154 | 'UnflaggedComment' => 'unflagged a comment', |
||||
| 155 | 'Unbanned' => 'unbanned', |
||||
| 156 | 'Promoted' => 'promoted to tool admin', |
||||
| 157 | 'BreakReserve' => 'forcibly broke the reservation', |
||||
| 158 | 'Prefchange' => 'changed user preferences', |
||||
| 159 | 'Renamed' => 'renamed', |
||||
| 160 | 'Demoted' => 'demoted from tool admin', |
||||
| 161 | 'ReceiveReserved' => 'received the reservation', |
||||
| 162 | 'SendReserved' => 'sent the reservation', |
||||
| 163 | 'EditedEmail' => 'edited email', |
||||
| 164 | 'DeletedTemplate' => 'deleted template', |
||||
| 165 | 'EditedTemplate' => 'edited template', |
||||
| 166 | 'CreatedEmail' => 'created email', |
||||
| 167 | 'CreatedTemplate' => 'created template', |
||||
| 168 | 'SentMail' => 'sent an email to the requester', |
||||
| 169 | 'Registered' => 'registered a tool account', |
||||
| 170 | 'JobIssue' => 'ran a background job unsuccessfully', |
||||
| 171 | 'JobCompleted' => 'completed a background job', |
||||
| 172 | 'JobAcknowledged' => 'acknowledged a job failure', |
||||
| 173 | 'JobRequeued' => 'requeued a job for re-execution', |
||||
| 174 | 'JobCancelled' => 'cancelled execution of a job', |
||||
| 175 | 'EnqueuedJobQueue' => 'scheduled for creation', |
||||
| 176 | 'Hospitalised' => 'sent to the hospital', |
||||
| 177 | 'QueueCreated' => 'created a request queue', |
||||
| 178 | 'QueueEdited' => 'edited a request queue', |
||||
| 179 | 'DomainCreated' => 'created a domain', |
||||
| 180 | 'DomainEdited' => 'edited a domain', |
||||
| 181 | 'RequestFormCreated' => 'created a request form', |
||||
| 182 | 'RequestFormEdited' => 'edited a request form', |
||||
| 183 | ); |
||||
| 184 | |||||
| 185 | if (array_key_exists($entry->getAction(), $lookup)) { |
||||
| 186 | return $lookup[$entry->getAction()]; |
||||
| 187 | } |
||||
| 188 | |||||
| 189 | // OK, I don't know what this is. Fall back to something sane. |
||||
| 190 | return "performed an unknown action ({$entry->getAction()})"; |
||||
| 191 | } |
||||
| 192 | |||||
| 193 | /** |
||||
| 194 | * @param PdoDatabase $database |
||||
| 195 | * |
||||
| 196 | * @return array |
||||
| 197 | */ |
||||
| 198 | public static function getLogActions(PdoDatabase $database) |
||||
| 199 | { |
||||
| 200 | $lookup = array( |
||||
| 201 | "Requests" => [ |
||||
| 202 | 'Reserved' => 'reserved', |
||||
| 203 | 'Email Confirmed' => 'email-confirmed', |
||||
| 204 | 'Manually Confirmed' => 'manually confirmed', |
||||
| 205 | 'Unreserved' => 'unreserved', |
||||
| 206 | 'EditComment-c' => 'edited a comment (by comment ID)', |
||||
| 207 | 'EditComment-r' => 'edited a comment (by request)', |
||||
| 208 | 'FlaggedComment' => 'flagged a comment', |
||||
| 209 | 'UnflaggedComment' => 'unflagged a comment', |
||||
| 210 | 'BreakReserve' => 'forcibly broke the reservation', |
||||
| 211 | 'ReceiveReserved' => 'received the reservation', |
||||
| 212 | 'SendReserved' => 'sent the reservation', |
||||
| 213 | 'SentMail' => 'sent an email to the requester', |
||||
| 214 | 'Closed 0' => 'dropped request', |
||||
| 215 | 'Closed custom-y' => 'closed (custom reason - account created)', |
||||
| 216 | 'Closed custom-n' => 'closed (custom reason - account not created)', |
||||
| 217 | ], |
||||
| 218 | 'Users' => [ |
||||
| 219 | 'Approved' => 'approved', |
||||
| 220 | 'Suspended' => 'suspended', |
||||
| 221 | 'RoleChange' => 'changed roles', |
||||
| 222 | 'Declined' => 'declined', |
||||
| 223 | 'Prefchange' => 'changed user preferences', |
||||
| 224 | 'Renamed' => 'renamed', |
||||
| 225 | 'Promoted' => 'promoted to tool admin', |
||||
| 226 | 'Demoted' => 'demoted from tool admin', |
||||
| 227 | 'Registered' => 'registered a tool account', |
||||
| 228 | ], |
||||
| 229 | "Bans" => [ |
||||
| 230 | 'Banned' => 'banned', |
||||
| 231 | 'Unbanned' => 'unbanned', |
||||
| 232 | ], |
||||
| 233 | "Site notice" => [ |
||||
| 234 | 'Edited' => 'edited interface message', |
||||
| 235 | ], |
||||
| 236 | "Email close templates" => [ |
||||
| 237 | 'EditedEmail' => 'edited email', |
||||
| 238 | 'CreatedEmail' => 'created email', |
||||
| 239 | ], |
||||
| 240 | "Welcome templates" => [ |
||||
| 241 | 'DeletedTemplate' => 'deleted template', |
||||
| 242 | 'EditedTemplate' => 'edited template', |
||||
| 243 | 'CreatedTemplate' => 'created template', |
||||
| 244 | ], |
||||
| 245 | "Job queue" => [ |
||||
| 246 | 'JobIssue' => 'ran a background job unsuccessfully', |
||||
| 247 | 'JobCompleted' => 'completed a background job', |
||||
| 248 | 'JobAcknowledged' => 'acknowledged a job failure', |
||||
| 249 | 'JobRequeued' => 'requeued a job for re-execution', |
||||
| 250 | 'JobCancelled' => 'cancelled execution of a job', |
||||
| 251 | 'EnqueuedJobQueue' => 'scheduled for creation', |
||||
| 252 | 'Hospitalised' => 'sent to the hospital', |
||||
| 253 | ], |
||||
| 254 | "Request queues" => [ |
||||
| 255 | 'QueueCreated' => 'created a request queue', |
||||
| 256 | 'QueueEdited' => 'edited a request queue', |
||||
| 257 | ], |
||||
| 258 | "Domains" => [ |
||||
| 259 | 'DomainCreated' => 'created a domain', |
||||
| 260 | 'DomainEdited' => 'edited a domain', |
||||
| 261 | ], |
||||
| 262 | "Request forms" => [ |
||||
| 263 | 'RequestFormCreated' => 'created a request form', |
||||
| 264 | 'RequestFormEdited' => 'edited a request form', |
||||
| 265 | ], |
||||
| 266 | ); |
||||
| 267 | |||||
| 268 | $databaseDrivenLogKeys = $database->query(<<<SQL |
||||
| 269 | SELECT CONCAT('Closed ', id) AS k, CONCAT('closed (',name,')') AS v FROM emailtemplate |
||||
| 270 | UNION ALL |
||||
| 271 | SELECT CONCAT('Deferred to ', logname) AS k, CONCAT('deferred to ', displayname) AS v FROM requestqueue; |
||||
| 272 | SQL |
||||
| 273 | ); |
||||
| 274 | foreach ($databaseDrivenLogKeys->fetchAll(PDO::FETCH_ASSOC) as $row) { |
||||
| 275 | $lookup["Requests"][$row['k']] = $row['v']; |
||||
| 276 | } |
||||
| 277 | |||||
| 278 | return $lookup; |
||||
| 279 | } |
||||
| 280 | |||||
| 281 | public static function getObjectTypes() |
||||
| 282 | { |
||||
| 283 | return array( |
||||
| 284 | 'Ban' => 'Ban', |
||||
| 285 | 'Comment' => 'Comment', |
||||
| 286 | 'EmailTemplate' => 'Email template', |
||||
| 287 | 'JobQueue' => 'Job queue item', |
||||
| 288 | 'Request' => 'Request', |
||||
| 289 | 'SiteNotice' => 'Site notice', |
||||
| 290 | 'User' => 'User', |
||||
| 291 | 'WelcomeTemplate' => 'Welcome template', |
||||
| 292 | 'RequestQueue' => 'Request queue', |
||||
| 293 | 'Domain' => 'Domain', |
||||
| 294 | 'RequestForm' => 'Request form' |
||||
| 295 | ); |
||||
| 296 | } |
||||
| 297 | |||||
| 298 | /** |
||||
| 299 | * This returns a HTML |
||||
| 300 | * |
||||
| 301 | * @param string $objectId |
||||
| 302 | * @param string $objectType |
||||
| 303 | * @param PdoDatabase $database |
||||
| 304 | * @param SiteConfiguration $configuration |
||||
| 305 | * |
||||
| 306 | * @return null|string |
||||
| 307 | * @category Security-Critical |
||||
| 308 | */ |
||||
| 309 | private static function getObjectDescription( |
||||
| 310 | $objectId, |
||||
| 311 | $objectType, |
||||
| 312 | PdoDatabase $database, |
||||
| 313 | SiteConfiguration $configuration |
||||
| 314 | ) { |
||||
| 315 | if ($objectType == '') { |
||||
| 316 | return null; |
||||
| 317 | } |
||||
| 318 | |||||
| 319 | $baseurl = $configuration->getBaseUrl(); |
||||
| 320 | |||||
| 321 | switch ($objectType) { |
||||
| 322 | case 'Ban': |
||||
| 323 | /** @var Ban $ban */ |
||||
| 324 | $ban = Ban::getById($objectId, $database); |
||||
|
0 ignored issues
–
show
$objectId of type string is incompatible with the type integer expected by parameter $id of Waca\DataObject::getById().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 325 | |||||
| 326 | if ($ban === false) { |
||||
|
0 ignored issues
–
show
|
|||||
| 327 | return 'Ban #' . $objectId; |
||||
| 328 | } |
||||
| 329 | |||||
| 330 | return <<<HTML |
||||
| 331 | <a href="{$baseurl}/internal.php/bans/show?id={$objectId}">Ban #{$objectId}</a> |
||||
| 332 | HTML; |
||||
| 333 | case 'EmailTemplate': |
||||
| 334 | /** @var EmailTemplate $emailTemplate */ |
||||
| 335 | $emailTemplate = EmailTemplate::getById($objectId, $database); |
||||
| 336 | |||||
| 337 | if ($emailTemplate === false) { |
||||
|
0 ignored issues
–
show
|
|||||
| 338 | return 'Email Template #' . $objectId; |
||||
| 339 | } |
||||
| 340 | |||||
| 341 | $name = htmlentities($emailTemplate->getName(), ENT_COMPAT, 'UTF-8'); |
||||
| 342 | |||||
| 343 | return <<<HTML |
||||
| 344 | <a href="{$baseurl}/internal.php/emailManagement/view?id={$objectId}">Email Template #{$objectId} ({$name})</a> |
||||
| 345 | HTML; |
||||
| 346 | case 'SiteNotice': |
||||
| 347 | return "<a href=\"{$baseurl}/internal.php/siteNotice\">the site notice</a>"; |
||||
| 348 | case 'Request': |
||||
| 349 | /** @var Request $request */ |
||||
| 350 | $request = Request::getById($objectId, $database); |
||||
| 351 | |||||
| 352 | if ($request === false) { |
||||
|
0 ignored issues
–
show
|
|||||
| 353 | return 'Request #' . $objectId; |
||||
| 354 | } |
||||
| 355 | |||||
| 356 | $name = htmlentities($request->getName(), ENT_COMPAT, 'UTF-8'); |
||||
| 357 | |||||
| 358 | return <<<HTML |
||||
| 359 | <a href="{$baseurl}/internal.php/viewRequest?id={$objectId}">Request #{$objectId} ({$name})</a> |
||||
| 360 | HTML; |
||||
| 361 | case 'User': |
||||
| 362 | /** @var User $user */ |
||||
| 363 | $user = User::getById($objectId, $database); |
||||
|
0 ignored issues
–
show
$objectId of type string is incompatible with the type integer|null expected by parameter $id of Waca\DataObjects\User::getById().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 364 | |||||
| 365 | // Some users were merged out of existence |
||||
| 366 | if ($user === false) { |
||||
|
0 ignored issues
–
show
|
|||||
| 367 | return 'User #' . $objectId; |
||||
| 368 | } |
||||
| 369 | |||||
| 370 | $username = htmlentities($user->getUsername(), ENT_COMPAT, 'UTF-8'); |
||||
| 371 | |||||
| 372 | return "<a href=\"{$baseurl}/internal.php/statistics/users/detail?user={$objectId}\">{$username}</a>"; |
||||
| 373 | case 'WelcomeTemplate': |
||||
| 374 | /** @var WelcomeTemplate $welcomeTemplate */ |
||||
| 375 | $welcomeTemplate = WelcomeTemplate::getById($objectId, $database); |
||||
| 376 | |||||
| 377 | // some old templates have been completely deleted and lost to the depths of time. |
||||
| 378 | if ($welcomeTemplate === false) { |
||||
|
0 ignored issues
–
show
|
|||||
| 379 | return "Welcome template #{$objectId}"; |
||||
| 380 | } |
||||
| 381 | else { |
||||
| 382 | $userCode = htmlentities($welcomeTemplate->getUserCode(), ENT_COMPAT, 'UTF-8'); |
||||
| 383 | |||||
| 384 | return "<a href=\"{$baseurl}/internal.php/welcomeTemplates/view?template={$objectId}\">{$userCode}</a>"; |
||||
| 385 | } |
||||
| 386 | case 'JobQueue': |
||||
| 387 | /** @var JobQueue $job */ |
||||
| 388 | $job = JobQueue::getById($objectId, $database); |
||||
| 389 | |||||
| 390 | $taskDescriptions = JobQueue::getTaskDescriptions(); |
||||
| 391 | |||||
| 392 | if ($job === false) { |
||||
|
0 ignored issues
–
show
|
|||||
| 393 | return 'Job Queue Task #' . $objectId; |
||||
| 394 | } |
||||
| 395 | |||||
| 396 | $task = $job->getTask(); |
||||
| 397 | if (isset($taskDescriptions[$task])) { |
||||
| 398 | $description = $taskDescriptions[$task]; |
||||
| 399 | } |
||||
| 400 | else { |
||||
| 401 | $description = 'Unknown task'; |
||||
| 402 | } |
||||
| 403 | |||||
| 404 | return "<a href=\"{$baseurl}/internal.php/jobQueue/view?id={$objectId}\">Job #{$job->getId()} ({$description})</a>"; |
||||
| 405 | case 'RequestQueue': |
||||
| 406 | /** @var RequestQueue $queue */ |
||||
| 407 | $queue = RequestQueue::getById($objectId, $database); |
||||
| 408 | |||||
| 409 | if ($queue === false) { |
||||
|
0 ignored issues
–
show
|
|||||
| 410 | return "Request Queue #{$objectId}"; |
||||
| 411 | } |
||||
| 412 | |||||
| 413 | $queueHeader = htmlentities($queue->getHeader(), ENT_COMPAT, 'UTF-8'); |
||||
| 414 | |||||
| 415 | return "<a href=\"{$baseurl}/internal.php/queueManagement/edit?queue={$objectId}\">{$queueHeader}</a>"; |
||||
| 416 | case 'Domain': |
||||
| 417 | /** @var Domain $domain */ |
||||
| 418 | $domain = Domain::getById($objectId, $database); |
||||
| 419 | |||||
| 420 | if ($domain === false) { |
||||
|
0 ignored issues
–
show
|
|||||
| 421 | return "Domain #{$objectId}"; |
||||
| 422 | } |
||||
| 423 | |||||
| 424 | $domainName = htmlentities($domain->getShortName(), ENT_COMPAT, 'UTF-8'); |
||||
| 425 | return "<a href=\"{$baseurl}/internal.php/domainManagement/edit?domain={$objectId}\">{$domainName}</a>"; |
||||
| 426 | case 'RequestForm': |
||||
| 427 | /** @var RequestForm $queue */ |
||||
| 428 | $queue = RequestForm::getById($objectId, $database); |
||||
| 429 | |||||
| 430 | if ($queue === false) { |
||||
|
0 ignored issues
–
show
|
|||||
| 431 | return "Request Form #{$objectId}"; |
||||
| 432 | } |
||||
| 433 | |||||
| 434 | $formName = htmlentities($queue->getName(), ENT_COMPAT, 'UTF-8'); |
||||
| 435 | |||||
| 436 | return "<a href=\"{$baseurl}/internal.php/requestFormManagement/edit?form={$objectId}\">{$formName}</a>"; |
||||
| 437 | |||||
| 438 | default: |
||||
| 439 | return '[' . $objectType . " " . $objectId . ']'; |
||||
| 440 | } |
||||
| 441 | } |
||||
| 442 | |||||
| 443 | /** |
||||
| 444 | * @param Log[] $logs |
||||
| 445 | * @param PdoDatabase $database |
||||
| 446 | * @param SiteConfiguration $configuration |
||||
| 447 | * |
||||
| 448 | * @return array |
||||
| 449 | * @throws Exception |
||||
| 450 | */ |
||||
| 451 | public static function prepareLogsForTemplate($logs, PdoDatabase $database, SiteConfiguration $configuration) |
||||
| 452 | { |
||||
| 453 | $userIds = array(); |
||||
| 454 | |||||
| 455 | foreach ($logs as $logEntry) { |
||||
| 456 | if (!$logEntry instanceof Log) { |
||||
| 457 | // if this happens, we've done something wrong with passing back the log data. |
||||
| 458 | throw new Exception('Log entry is not an instance of a Log, this should never happen.'); |
||||
| 459 | } |
||||
| 460 | |||||
| 461 | $user = $logEntry->getUser(); |
||||
| 462 | if ($user === -1) { |
||||
| 463 | continue; |
||||
| 464 | } |
||||
| 465 | |||||
| 466 | if (!array_search($user, $userIds)) { |
||||
| 467 | $userIds[] = $user; |
||||
| 468 | } |
||||
| 469 | } |
||||
| 470 | |||||
| 471 | $users = UserSearchHelper::get($database)->inIds($userIds)->fetchMap('username'); |
||||
| 472 | $users[-1] = User::getCommunity()->getUsername(); |
||||
| 473 | |||||
| 474 | $logData = array(); |
||||
| 475 | |||||
| 476 | foreach ($logs as $logEntry) { |
||||
| 477 | $objectDescription = self::getObjectDescription($logEntry->getObjectId(), $logEntry->getObjectType(), |
||||
| 478 | $database, $configuration); |
||||
| 479 | |||||
| 480 | // initialise to sane default |
||||
| 481 | $comment = null; |
||||
| 482 | |||||
| 483 | switch ($logEntry->getAction()) { |
||||
| 484 | case 'Renamed': |
||||
| 485 | $renameData = unserialize($logEntry->getComment()); |
||||
|
0 ignored issues
–
show
It seems like
$logEntry->getComment() can also be of type null; however, parameter $data of unserialize() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 486 | $oldName = htmlentities($renameData['old'], ENT_COMPAT, 'UTF-8'); |
||||
| 487 | $newName = htmlentities($renameData['new'], ENT_COMPAT, 'UTF-8'); |
||||
| 488 | $comment = 'Renamed \'' . $oldName . '\' to \'' . $newName . '\'.'; |
||||
| 489 | break; |
||||
| 490 | case 'RoleChange': |
||||
| 491 | $roleChangeData = unserialize($logEntry->getComment()); |
||||
| 492 | |||||
| 493 | $removed = array(); |
||||
| 494 | foreach ($roleChangeData['removed'] as $r) { |
||||
| 495 | $removed[] = htmlentities($r, ENT_COMPAT, 'UTF-8'); |
||||
| 496 | } |
||||
| 497 | |||||
| 498 | $added = array(); |
||||
| 499 | foreach ($roleChangeData['added'] as $r) { |
||||
| 500 | $added[] = htmlentities($r, ENT_COMPAT, 'UTF-8'); |
||||
| 501 | } |
||||
| 502 | |||||
| 503 | $reason = htmlentities($roleChangeData['reason'], ENT_COMPAT, 'UTF-8'); |
||||
| 504 | |||||
| 505 | $roleDelta = 'Removed [' . implode(', ', $removed) . '], Added [' . implode(', ', $added) . ']'; |
||||
| 506 | $comment = $roleDelta . ' with comment: ' . $reason; |
||||
| 507 | break; |
||||
| 508 | case 'JobIssue': |
||||
| 509 | $jobIssueData = unserialize($logEntry->getComment()); |
||||
| 510 | $errorMessage = $jobIssueData['error']; |
||||
| 511 | $status = $jobIssueData['status']; |
||||
| 512 | |||||
| 513 | $comment = 'Job ' . htmlentities($status, ENT_COMPAT, 'UTF-8') . ': '; |
||||
| 514 | $comment .= htmlentities($errorMessage, ENT_COMPAT, 'UTF-8'); |
||||
| 515 | break; |
||||
| 516 | case 'JobIssueRequest': |
||||
| 517 | case 'JobCompletedRequest': |
||||
| 518 | $jobData = unserialize($logEntry->getComment()); |
||||
| 519 | |||||
| 520 | /** @var JobQueue $job */ |
||||
| 521 | $job = JobQueue::getById($jobData['job'], $database); |
||||
| 522 | $descs = JobQueue::getTaskDescriptions(); |
||||
| 523 | $comment = htmlentities($descs[$job->getTask()], ENT_COMPAT, 'UTF-8'); |
||||
| 524 | break; |
||||
| 525 | |||||
| 526 | case 'JobCompleted': |
||||
| 527 | break; |
||||
| 528 | default: |
||||
| 529 | $comment = $logEntry->getComment(); |
||||
| 530 | break; |
||||
| 531 | } |
||||
| 532 | |||||
| 533 | $logData[] = array( |
||||
| 534 | 'timestamp' => $logEntry->getTimestamp(), |
||||
| 535 | 'userid' => $logEntry->getUser(), |
||||
| 536 | 'username' => $users[$logEntry->getUser()], |
||||
| 537 | 'description' => self::getLogDescription($logEntry), |
||||
| 538 | 'objectdescription' => $objectDescription, |
||||
| 539 | 'comment' => $comment, |
||||
| 540 | ); |
||||
| 541 | } |
||||
| 542 | |||||
| 543 | return array($users, $logData); |
||||
| 544 | } |
||||
| 545 | } |
||||
| 546 |