| Total Complexity | 41 |
| Total Lines | 608 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like EditCounterRepository 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 EditCounterRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class EditCounterRepository extends UserRightsRepository |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Get data about revisions, pages, etc. |
||
| 23 | * @param Project $project The project. |
||
| 24 | * @param User $user The user. |
||
| 25 | * @return string[] With keys: 'deleted', 'live', 'total', '24h', '7d', '30d', |
||
| 26 | * '365d', 'small', 'large', 'with_comments', and 'minor_edits', ... |
||
| 27 | */ |
||
| 28 | public function getPairData(Project $project, User $user): array |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Get log totals for a user. |
||
| 98 | * @param Project $project The project. |
||
| 99 | * @param User $user The user. |
||
| 100 | * @return integer[] Keys are "<log>-<action>" strings, values are counts. |
||
| 101 | */ |
||
| 102 | public function getLogCounts(Project $project, User $user): array |
||
| 103 | { |
||
| 104 | // Set up cache. |
||
| 105 | $cacheKey = $this->getCacheKey(func_get_args(), 'ec_logcounts'); |
||
| 106 | if ($this->cache->hasItem($cacheKey)) { |
||
| 107 | return $this->cache->getItem($cacheKey)->get(); |
||
| 108 | } |
||
| 109 | |||
| 110 | // Query. |
||
| 111 | $loggingTable = $this->getTableName($project->getDatabaseName(), 'logging'); |
||
| 112 | $sql = " |
||
| 113 | (SELECT CONCAT(log_type, '-', log_action) AS source, COUNT(log_id) AS value |
||
| 114 | FROM $loggingTable |
||
| 115 | WHERE log_actor = :actorId |
||
| 116 | GROUP BY log_type, log_action |
||
| 117 | )"; |
||
| 118 | |||
| 119 | $results = $this->executeProjectsQuery($sql, [ |
||
| 120 | 'actorId' => $user->getActorId($project), |
||
| 121 | ])->fetchAll(); |
||
| 122 | |||
| 123 | $logCounts = array_combine( |
||
| 124 | array_map(function ($e) { |
||
| 125 | return $e['source']; |
||
| 126 | }, $results), |
||
| 127 | array_map(function ($e) { |
||
| 128 | return (int)$e['value']; |
||
| 129 | }, $results) |
||
| 130 | ); |
||
| 131 | |||
| 132 | // Make sure there is some value for each of the wanted counts. |
||
| 133 | $requiredCounts = [ |
||
| 134 | 'thanks-thank', |
||
| 135 | 'review-approve', |
||
| 136 | 'newusers-create2', |
||
| 137 | 'newusers-byemail', |
||
| 138 | 'patrol-patrol', |
||
| 139 | 'block-block', |
||
| 140 | 'block-reblock', |
||
| 141 | 'block-unblock', |
||
| 142 | 'protect-protect', |
||
| 143 | 'protect-modify', |
||
| 144 | 'protect-unprotect', |
||
| 145 | 'rights-rights', |
||
| 146 | 'move-move', |
||
| 147 | 'delete-delete', |
||
| 148 | 'delete-revision', |
||
| 149 | 'delete-restore', |
||
| 150 | 'import-import', |
||
| 151 | 'import-interwiki', |
||
| 152 | 'import-upload', |
||
| 153 | 'upload-upload', |
||
| 154 | 'upload-overwrite', |
||
| 155 | 'abusefilter-modify', |
||
| 156 | 'merge-merge', |
||
| 157 | ]; |
||
| 158 | foreach ($requiredCounts as $req) { |
||
| 159 | if (!isset($logCounts[$req])) { |
||
| 160 | $logCounts[$req] = 0; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | // Add Commons upload count, if applicable. |
||
| 165 | $logCounts['files_uploaded_commons'] = 0; |
||
| 166 | if ($this->isLabs() && !$user->isAnon()) { |
||
| 167 | $sql = "SELECT COUNT(log_id) |
||
| 168 | FROM commonswiki_p.logging_userindex |
||
| 169 | JOIN commonswiki_p.actor ON actor_id = log_actor |
||
| 170 | WHERE log_type = 'upload' AND log_action = 'upload' |
||
| 171 | AND actor_name = :username"; |
||
| 172 | $resultQuery = $this->executeProjectsQuery($sql, [ |
||
| 173 | 'username' => $user->getUsername(), |
||
| 174 | ]); |
||
| 175 | $logCounts['files_uploaded_commons'] = (int)$resultQuery->fetchColumn(); |
||
| 176 | } |
||
| 177 | |||
| 178 | // Cache and return. |
||
| 179 | return $this->setCache($cacheKey, $logCounts); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Get the IDs and timestamps of the latest edit and logged action by the given user. |
||
| 184 | * @param Project $project |
||
| 185 | * @param User $user |
||
| 186 | * @return string[] With keys 'rev_first', 'rev_latest', 'log_latest'. |
||
| 187 | */ |
||
| 188 | public function getFirstAndLatestActions(Project $project, User $user): array |
||
| 189 | { |
||
| 190 | $loggingTable = $project->getTableName('logging', 'userindex'); |
||
| 191 | $revisionTable = $project->getTableName('revision'); |
||
| 192 | |||
| 193 | $sql = "( |
||
| 194 | SELECT 'rev_first' AS `key`, rev_id AS `id`, |
||
| 195 | rev_timestamp AS `timestamp`, NULL as `type` |
||
| 196 | FROM $revisionTable |
||
| 197 | WHERE rev_actor = :actorId |
||
| 198 | LIMIT 1 |
||
| 199 | ) UNION ( |
||
| 200 | SELECT 'rev_latest' AS `key`, rev_id AS `id`, |
||
| 201 | rev_timestamp AS `timestamp`, NULL as `type` |
||
| 202 | FROM $revisionTable |
||
| 203 | WHERE rev_actor = :actorId |
||
| 204 | ORDER BY rev_timestamp DESC LIMIT 1 |
||
| 205 | ) UNION ( |
||
| 206 | SELECT 'log_latest' AS `key`, log_id AS `id`, |
||
| 207 | log_timestamp AS `timestamp`, log_type AS `type` |
||
| 208 | FROM $loggingTable |
||
| 209 | WHERE log_actor = :actorId |
||
| 210 | ORDER BY log_timestamp DESC LIMIT 1 |
||
| 211 | )"; |
||
| 212 | |||
| 213 | $resultQuery = $this->executeProjectsQuery($sql, [ |
||
| 214 | 'actorId' => $user->getActorId($project), |
||
| 215 | ]); |
||
| 216 | |||
| 217 | $actions = []; |
||
| 218 | while ($result = $resultQuery->fetch()) { |
||
| 219 | $actions[$result['key']] = [ |
||
| 220 | 'id' => $result['id'], |
||
| 221 | 'timestamp' => $result['timestamp'], |
||
| 222 | 'type' => $result['type'], |
||
| 223 | ]; |
||
| 224 | } |
||
| 225 | |||
| 226 | return $actions; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Get data for all blocks set on the given user. |
||
| 231 | * @param Project $project |
||
| 232 | * @param User $user |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | public function getBlocksReceived(Project $project, User $user): array |
||
| 236 | { |
||
| 237 | $loggingTable = $this->getTableName($project->getDatabaseName(), 'logging', 'logindex'); |
||
| 238 | $sql = "SELECT log_action, log_timestamp, log_params FROM $loggingTable |
||
| 239 | WHERE log_type = 'block' |
||
| 240 | AND log_action IN ('block', 'reblock', 'unblock') |
||
| 241 | AND log_timestamp > 0 |
||
| 242 | AND log_title = :username |
||
| 243 | AND log_namespace = 2 |
||
| 244 | ORDER BY log_timestamp ASC"; |
||
| 245 | $username = str_replace(' ', '_', $user->getUsername()); |
||
| 246 | |||
| 247 | return $this->executeProjectsQuery($sql, [ |
||
| 248 | 'username' => $username, |
||
| 249 | ])->fetchAll(); |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Get a user's edit count for each project. |
||
| 254 | * @see EditCounterRepository::globalEditCountsFromCentralAuth() |
||
| 255 | * @see EditCounterRepository::globalEditCountsFromDatabases() |
||
| 256 | * @param User $user The user. |
||
| 257 | * @param Project $project The project to start from. |
||
| 258 | * @return mixed[] Elements are arrays with 'project' (Project), and 'total' (int). |
||
| 259 | */ |
||
| 260 | public function globalEditCounts(User $user, Project $project): array |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Get a user's total edit count on one or more project. |
||
| 284 | * Requires the CentralAuth extension to be installed on the project. |
||
| 285 | * |
||
| 286 | * @param User $user The user. |
||
| 287 | * @param Project $project The project to start from. |
||
| 288 | * @return mixed[]|false Elements are arrays with 'dbName' (string), and 'total' (int). False for logged out users. |
||
| 289 | */ |
||
| 290 | protected function globalEditCountsFromCentralAuth(User $user, Project $project) |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Get total edit counts from all projects for this user. |
||
| 332 | * @param User $user The user. |
||
| 333 | * @param Project $project The project to start from. |
||
| 334 | * @return mixed[] Elements are arrays with 'dbName' (string), and 'total' (int). |
||
| 335 | * @see EditCounterRepository::globalEditCountsFromCentralAuth() |
||
| 336 | */ |
||
| 337 | protected function globalEditCountsFromDatabases(User $user, Project $project): array |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Needed because we can't cache objects (Project in this case). |
||
| 365 | * @see self::getProjectsWikiEdits() |
||
| 366 | * @param string[] $databases |
||
| 367 | * @return mixed[] Keys are database names, values are Project objects. |
||
| 368 | */ |
||
| 369 | private function formatProjectsWikiEdits(array $databases): array |
||
| 370 | { |
||
| 371 | $projects = []; |
||
| 372 | foreach ($databases as $database) { |
||
| 373 | $projects[$database] = ProjectRepository::getProject($database, $this->container); |
||
| 374 | } |
||
| 375 | return $projects; |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Get Projects that the user has made at least one edit on. |
||
| 380 | * @param User $user |
||
| 381 | * @return mixed[] Keys are database names, values are Projects. |
||
| 382 | */ |
||
| 383 | public function getProjectsWithEdits(User $user): array |
||
| 384 | { |
||
| 385 | $cacheKey = $this->getCacheKey(func_get_args(), 'ec_projects_with_edits'); |
||
| 386 | if ($this->cache->hasItem($cacheKey)) { |
||
| 387 | return $this->formatProjectsWikiEdits($this->cache->getItem($cacheKey)->get()); |
||
| 388 | } |
||
| 389 | |||
| 390 | $projectRepo = new ProjectRepository(); |
||
| 391 | $projectRepo->setContainer($this->container); |
||
| 392 | $allProjects = $projectRepo->getAll(); |
||
| 393 | $databases = []; |
||
| 394 | |||
| 395 | foreach ($allProjects as $projectMeta) { |
||
| 396 | $actorTable = $this->getTableName($projectMeta['dbName'], 'actor', 'revision'); |
||
| 397 | $sql = "SELECT 1 FROM $actorTable WHERE actor_name = :actor"; |
||
| 398 | |||
| 399 | $resultQuery = $this->executeProjectsQuery($sql, [ |
||
| 400 | 'actor' => $user->getUsername(), |
||
| 401 | ]); |
||
| 402 | if ($resultQuery->fetch()) { |
||
| 403 | $databases[] = $projectMeta['dbName']; |
||
| 404 | } |
||
| 405 | } |
||
| 406 | |||
| 407 | $projects = $this->setCache($cacheKey, $databases); |
||
| 408 | |||
| 409 | return $this->formatProjectsWikiEdits($projects); |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Get the given user's total edit counts per namespace on the given project. |
||
| 414 | * @param Project $project The project. |
||
| 415 | * @param User $user The user. |
||
| 416 | * @return array Array keys are namespace IDs, values are the edit counts. |
||
| 417 | */ |
||
| 418 | public function getNamespaceTotals(Project $project, User $user): array |
||
| 419 | { |
||
| 420 | // Cache? |
||
| 421 | $cacheKey = $this->getCacheKey(func_get_args(), 'ec_namespacetotals'); |
||
| 422 | if ($this->cache->hasItem($cacheKey)) { |
||
| 423 | return $this->cache->getItem($cacheKey)->get(); |
||
| 424 | } |
||
| 425 | |||
| 426 | // Query. |
||
| 427 | $revisionTable = $this->getTableName($project->getDatabaseName(), 'revision'); |
||
| 428 | $pageTable = $this->getTableName($project->getDatabaseName(), 'page'); |
||
| 429 | $sql = "SELECT page_namespace, COUNT(rev_id) AS total |
||
| 430 | FROM $pageTable p JOIN $revisionTable r ON (r.rev_page = p.page_id) |
||
| 431 | WHERE r.rev_actor = :actorId |
||
| 432 | GROUP BY page_namespace"; |
||
| 433 | |||
| 434 | $results = $this->executeProjectsQuery($sql, [ |
||
| 435 | 'actorId' => $user->getActorId($project), |
||
| 436 | ])->fetchAll(); |
||
| 437 | |||
| 438 | $namespaceTotals = array_combine(array_map(function ($e) { |
||
| 439 | return $e['page_namespace']; |
||
| 440 | }, $results), array_map(function ($e) { |
||
| 441 | return (int)$e['total']; |
||
| 442 | }, $results)); |
||
| 443 | |||
| 444 | // Cache and return. |
||
| 445 | return $this->setCache($cacheKey, $namespaceTotals); |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Get revisions by this user across the given Projects. |
||
| 450 | * @param Project[] $projects The projects. |
||
| 451 | * @param User $user The user. |
||
| 452 | * @param int $limit The maximum number of revisions to fetch from each project. |
||
| 453 | * @param int $offset Offset results by this number of rows. |
||
| 454 | * @return array|mixed |
||
| 455 | */ |
||
| 456 | public function getRevisions(array $projects, User $user, int $limit = 30, int $offset = 0) |
||
| 457 | { |
||
| 458 | // Check cache. |
||
| 459 | $cacheKey = $this->getCacheKey('ec_globalcontribs.'.$user->getCacheKey().'.'.$limit.'.'.$offset); |
||
| 460 | if ($this->cache->hasItem($cacheKey)) { |
||
| 461 | return $this->cache->getItem($cacheKey)->get(); |
||
| 462 | } |
||
| 463 | |||
| 464 | $username = $this->getProjectsConnection()->quote($user->getUsername(), \PDO::PARAM_STR); |
||
| 465 | |||
| 466 | // Assemble queries. |
||
| 467 | $queries = []; |
||
| 468 | foreach ($projects as $project) { |
||
| 469 | $revisionTable = $project->getTableName('revision'); |
||
| 470 | $pageTable = $project->getTableName('page'); |
||
| 471 | $commentTable = $project->getTableName('comment', 'revision'); |
||
| 472 | $actorId = $user->getActorId($project); |
||
| 473 | $sql = "SELECT |
||
| 474 | '".$project->getDatabaseName()."' AS project_name, |
||
| 475 | revs.rev_id AS id, |
||
| 476 | revs.rev_timestamp AS timestamp, |
||
| 477 | UNIX_TIMESTAMP(revs.rev_timestamp) AS unix_timestamp, |
||
| 478 | revs.rev_minor_edit AS minor, |
||
| 479 | revs.rev_deleted AS deleted, |
||
| 480 | revs.rev_len AS length, |
||
| 481 | (CAST(revs.rev_len AS SIGNED) - IFNULL(parentrevs.rev_len, 0)) AS length_change, |
||
| 482 | revs.rev_parent_id AS parent_id, |
||
| 483 | $username AS username, |
||
| 484 | page.page_title, |
||
| 485 | page.page_namespace, |
||
| 486 | comment_text AS `comment` |
||
| 487 | FROM $revisionTable AS revs |
||
| 488 | JOIN $pageTable AS page ON (rev_page = page_id) |
||
| 489 | LEFT JOIN $revisionTable AS parentrevs ON (revs.rev_parent_id = parentrevs.rev_id) |
||
| 490 | LEFT OUTER JOIN $commentTable ON revs.rev_comment_id = comment_id |
||
| 491 | WHERE revs.rev_actor = $actorId"; |
||
| 492 | $queries[] = $sql; |
||
| 493 | } |
||
| 494 | $sql = "SELECT * FROM ((\n" . join("\n) UNION (\n", $queries) . ")) a ORDER BY timestamp DESC LIMIT $limit"; |
||
| 495 | |||
| 496 | if (is_numeric($offset)) { |
||
| 497 | $sql .= " OFFSET $offset"; |
||
| 498 | } |
||
| 499 | |||
| 500 | $revisions = $this->executeProjectsQuery($sql)->fetchAll(); |
||
| 501 | |||
| 502 | // Cache and return. |
||
| 503 | return $this->setCache($cacheKey, $revisions); |
||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Get data for a bar chart of monthly edit totals per namespace. |
||
| 508 | * @param Project $project The project. |
||
| 509 | * @param User $user The user. |
||
| 510 | * @return string[] [ |
||
| 511 | * [ |
||
| 512 | * 'year' => <year>, |
||
| 513 | * 'month' => <month>, |
||
| 514 | * 'page_namespace' => <namespace>, |
||
| 515 | * 'count' => <count>, |
||
| 516 | * ], |
||
| 517 | * ... |
||
| 518 | * ] |
||
| 519 | */ |
||
| 520 | public function getMonthCounts(Project $project, User $user): array |
||
| 521 | { |
||
| 522 | $cacheKey = $this->getCacheKey(func_get_args(), 'ec_monthcounts'); |
||
| 523 | if ($this->cache->hasItem($cacheKey)) { |
||
| 524 | return $this->cache->getItem($cacheKey)->get(); |
||
| 525 | } |
||
| 526 | |||
| 527 | $revisionTable = $project->getTableName('revision'); |
||
| 528 | $pageTable = $project->getTableName('page'); |
||
| 529 | $sql = |
||
| 530 | "SELECT " |
||
| 531 | . " YEAR(rev_timestamp) AS `year`," |
||
| 532 | . " MONTH(rev_timestamp) AS `month`," |
||
| 533 | . " page_namespace," |
||
| 534 | . " COUNT(rev_id) AS `count` " |
||
| 535 | . " FROM $revisionTable JOIN $pageTable ON (rev_page = page_id)" |
||
| 536 | . " WHERE rev_actor = :actorId" |
||
| 537 | . " GROUP BY YEAR(rev_timestamp), MONTH(rev_timestamp), page_namespace"; |
||
| 538 | |||
| 539 | $totals = $this->executeProjectsQuery($sql, [ |
||
| 540 | 'actorId' => $user->getActorId($project), |
||
| 541 | ])->fetchAll(); |
||
| 542 | |||
| 543 | // Cache and return. |
||
| 544 | return $this->setCache($cacheKey, $totals); |
||
| 545 | } |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Get data for the timecard chart, with totals grouped by day and to the nearest two-hours. |
||
| 549 | * @param Project $project |
||
| 550 | * @param User $user |
||
| 551 | * @return string[] |
||
| 552 | */ |
||
| 553 | public function getTimeCard(Project $project, User $user): array |
||
| 554 | { |
||
| 555 | $cacheKey = $this->getCacheKey(func_get_args(), 'ec_timecard'); |
||
| 556 | if ($this->cache->hasItem($cacheKey)) { |
||
| 557 | return $this->cache->getItem($cacheKey)->get(); |
||
| 558 | } |
||
| 559 | |||
| 560 | $hourInterval = 2; |
||
| 561 | $xCalc = "ROUND(HOUR(rev_timestamp)/$hourInterval) * $hourInterval"; |
||
| 562 | $revisionTable = $this->getTableName($project->getDatabaseName(), 'revision'); |
||
| 563 | $sql = "SELECT " |
||
| 564 | . " DAYOFWEEK(rev_timestamp) AS `day_of_week`, " |
||
| 565 | . " $xCalc AS `hour`, " |
||
| 566 | . " COUNT(rev_id) AS `value` " |
||
| 567 | . " FROM $revisionTable" |
||
| 568 | . " WHERE rev_actor = :actorId" |
||
| 569 | . " GROUP BY DAYOFWEEK(rev_timestamp), $xCalc "; |
||
| 570 | |||
| 571 | $totals = $this->executeProjectsQuery($sql, [ |
||
| 572 | 'actorId' => $user->getActorId($project), |
||
| 573 | ])->fetchAll(); |
||
| 574 | |||
| 575 | // Cache and return. |
||
| 576 | return $this->setCache($cacheKey, $totals); |
||
| 577 | } |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Get various data about edit sizes of the past 5,000 edits. |
||
| 581 | * Will cache the result for 10 minutes. |
||
| 582 | * @param Project $project The project. |
||
| 583 | * @param User $user The user. |
||
| 584 | * @return string[] Values with for keys 'average_size', |
||
| 585 | * 'small_edits' and 'large_edits' |
||
| 586 | */ |
||
| 587 | public function getEditSizeData(Project $project, User $user): array |
||
| 614 | } |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Get the number of edits this user made using semi-automated tools. |
||
| 618 | * @param Project $project |
||
| 619 | * @param User $user |
||
| 620 | * @return int Result of query, see below. |
||
| 621 | */ |
||
| 622 | public function countAutomatedEdits(Project $project, User $user): int |
||
| 627 | } |
||
| 628 | } |
||
| 629 |