| Total Complexity | 54 |
| Total Lines | 517 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AutoEditsRepository 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 AutoEditsRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class AutoEditsRepository extends UserRepository |
||
| 19 | { |
||
| 20 | /** @var array List of automated tools, used for fetching the tool list and filtering it. */ |
||
| 21 | private $aeTools; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Method to give the repository access to the AutomatedEditsHelper and fetch the list of semi-automated tools. |
||
| 25 | * @param Project $project |
||
| 26 | * @param int|string $namespace Namespace ID or 'all'. |
||
| 27 | * @return array |
||
| 28 | */ |
||
| 29 | public function getTools(Project $project, $namespace = 'all'): array |
||
| 30 | { |
||
| 31 | if (!isset($this->aeTools)) { |
||
| 32 | $this->aeTools = $this->container |
||
| 33 | ->get('app.automated_edits_helper') |
||
| 34 | ->getTools($project); |
||
| 35 | } |
||
| 36 | |||
| 37 | if ('all' !== $namespace) { |
||
| 38 | // Limit by namespace. |
||
| 39 | return array_filter($this->aeTools, function (array $tool) use ($namespace) { |
||
| 40 | return empty($tool['namespaces']) || |
||
| 41 | in_array((int)$namespace, $tool['namespaces']) || |
||
| 42 | ( |
||
| 43 | 1 === $namespace % 2 && |
||
| 44 | isset($tool['talk_namespaces']) |
||
| 45 | ); |
||
| 46 | }); |
||
| 47 | } |
||
| 48 | |||
| 49 | return $this->aeTools; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Is the tag for given tool intended to be counted by itself? For instance, |
||
| 54 | * when counting Rollback edits we don't want to also count Huggle edits (which are tagged as Rollback). |
||
| 55 | * @param Project $project |
||
| 56 | * @param string|null $tool |
||
| 57 | * @return bool |
||
| 58 | */ |
||
| 59 | private function usesSingleTag(Project $project, ?string $tool): bool |
||
| 60 | { |
||
| 61 | return isset($this->getTools($project)[$tool]['single_tag']); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Get the number of edits this user made using semi-automated tools. |
||
| 66 | * @param Project $project |
||
| 67 | * @param User $user |
||
| 68 | * @param string|int $namespace Namespace ID or 'all' |
||
| 69 | * @param string $start Start date in a format accepted by strtotime() |
||
| 70 | * @param string $end End date in a format accepted by strtotime() |
||
| 71 | * @return int Result of query, see below. |
||
| 72 | */ |
||
| 73 | public function countAutomatedEdits(Project $project, User $user, $namespace = 'all', $start = '', $end = ''): int |
||
| 74 | { |
||
| 75 | $cacheKey = $this->getCacheKey(func_get_args(), 'user_autoeditcount'); |
||
| 76 | if ($this->cache->hasItem($cacheKey)) { |
||
| 77 | return $this->cache->getItem($cacheKey)->get(); |
||
| 78 | } |
||
| 79 | |||
| 80 | [$condBegin, $condEnd] = $this->getRevTimestampConditions($start, $end); |
||
| 81 | |||
| 82 | // Get the combined regex and tags for the tools |
||
| 83 | [$regex, $tagIds] = $this->getToolRegexAndTags($project, false, null, $namespace); |
||
| 84 | |||
| 85 | [$pageJoin, $condNamespace] = $this->getPageAndNamespaceSql($project, $namespace); |
||
| 86 | |||
| 87 | $revisionTable = $project->getTableName('revision'); |
||
| 88 | $commentTable = $project->getTableName('comment'); |
||
| 89 | $tagTable = $project->getTableName('change_tag'); |
||
| 90 | $commentJoin = ''; |
||
| 91 | $tagJoin = ''; |
||
| 92 | |||
| 93 | $params = []; |
||
| 94 | |||
| 95 | // Build SQL for detecting autoedits via regex and/or tags |
||
| 96 | $condTools = []; |
||
| 97 | if ('' != $regex) { |
||
| 98 | $commentJoin = "LEFT OUTER JOIN $commentTable ON rev_comment_id = comment_id"; |
||
| 99 | $condTools[] = "comment_text REGEXP :tools"; |
||
| 100 | $params['tools'] = $regex; |
||
| 101 | } |
||
| 102 | if ('' != $tagIds) { |
||
| 103 | $tagJoin = "LEFT OUTER JOIN $tagTable ON ct_rev_id = rev_id"; |
||
| 104 | $condTools[] = "ct_tag_id IN ($tagIds)"; |
||
| 105 | } |
||
| 106 | $condTool = 'AND (' . implode(' OR ', $condTools) . ')'; |
||
| 107 | $userClause = $user->isAnon() ? 'rev_user_text = :username' : 'rev_user = :userId'; |
||
| 108 | |||
| 109 | $sql = "SELECT COUNT(DISTINCT(rev_id)) |
||
| 110 | FROM $revisionTable |
||
| 111 | $pageJoin |
||
| 112 | $commentJoin |
||
| 113 | $tagJoin |
||
| 114 | WHERE $userClause |
||
| 115 | $condNamespace |
||
| 116 | $condTool |
||
| 117 | $condBegin |
||
| 118 | $condEnd"; |
||
| 119 | |||
| 120 | $resultQuery = $this->executeQuery($sql, $project, $user, $namespace, $start, $end, $params); |
||
| 121 | $result = (int)$resultQuery->fetchColumn(); |
||
| 122 | |||
| 123 | // Cache and return. |
||
| 124 | return $this->setCache($cacheKey, $result); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get non-automated contributions for the given user. |
||
| 129 | * @param Project $project |
||
| 130 | * @param User $user |
||
| 131 | * @param string|int $namespace Namespace ID or 'all'. |
||
| 132 | * @param string $start Start date in a format accepted by strtotime(). |
||
| 133 | * @param string $end End date in a format accepted by strtotime(). |
||
| 134 | * @param int $offset Used for pagination, offset results by N edits. |
||
| 135 | * @return string[] Result of query, with columns 'page_title', 'page_namespace', 'rev_id', 'timestamp', 'minor', |
||
| 136 | * 'length', 'length_change', 'comment'. |
||
| 137 | */ |
||
| 138 | public function getNonAutomatedEdits( |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Get (semi-)automated contributions for the given user, and optionally for a given tool. |
||
| 198 | * @param Project $project |
||
| 199 | * @param User $user |
||
| 200 | * @param string|int $namespace Namespace ID or 'all'. |
||
| 201 | * @param string $start Start date in a format accepted by strtotime(). |
||
| 202 | * @param string $end End date in a format accepted by strtotime(). |
||
| 203 | * @param string|null $tool Only get edits made with this tool. Must match the keys in semi_automated.yml. |
||
| 204 | * @param int $offset Used for pagination, offset results by N edits. |
||
| 205 | * @return string[] Result of query, with columns 'page_title', 'page_namespace', 'rev_id', 'timestamp', 'minor', |
||
| 206 | * 'length', 'length_change', 'comment'. |
||
| 207 | */ |
||
| 208 | public function getAutomatedEdits( |
||
| 209 | Project $project, |
||
| 210 | User $user, |
||
| 211 | $namespace = 'all', |
||
| 212 | $start = '', |
||
| 213 | $end = '', |
||
| 214 | ?string $tool = null, |
||
| 215 | int $offset = 0 |
||
| 216 | ): array { |
||
| 217 | $cacheKey = $this->getCacheKey(func_get_args(), 'user_autoedits'); |
||
| 218 | if ($this->cache->hasItem($cacheKey)) { |
||
| 219 | return $this->cache->getItem($cacheKey)->get(); |
||
| 220 | } |
||
| 221 | |||
| 222 | [$condBegin, $condEnd] = $this->getRevTimestampConditions($start, $end, 'revs.'); |
||
| 223 | |||
| 224 | // In this case there is a slight performance improvement we can make if we're not given a start date. |
||
| 225 | if ('' == $condBegin && '' == $condEnd) { |
||
| 226 | $condBegin = 'AND revs.rev_timestamp > 0'; |
||
| 227 | } |
||
| 228 | |||
| 229 | // Get the combined regex and tags for the tools |
||
| 230 | [$regex, $tagIds] = $this->getToolRegexAndTags($project, false, $tool); |
||
| 231 | |||
| 232 | $pageTable = $project->getTableName('page'); |
||
| 233 | $revisionTable = $project->getTableName('revision'); |
||
| 234 | $commentTable = $project->getTableName('comment'); |
||
| 235 | $tagTable = $project->getTableName('change_tag'); |
||
| 236 | $condNamespace = 'all' === $namespace ? '' : 'AND page_namespace = :namespace'; |
||
| 237 | $userClause = $user->isAnon() ? 'rev_user_text = :username' : 'rev_user = :userId'; |
||
| 238 | $tagJoin = ''; |
||
| 239 | $condsTool = []; |
||
| 240 | |||
| 241 | if ('' != $regex) { |
||
| 242 | $condsTool[] = 'comment_text RLIKE :tools'; |
||
| 243 | } |
||
| 244 | |||
| 245 | if ('' != $tagIds) { |
||
| 246 | $tagJoin = "LEFT OUTER JOIN $tagTable ON (ct_rev_id = revs.rev_id)"; |
||
| 247 | if ($this->usesSingleTag($project, $tool)) { |
||
| 248 | // Only show edits made with the tool that don't overlap with other tools. |
||
| 249 | // For instance, Huggle edits are also tagged as Rollback, but when viewing |
||
| 250 | // Rollback edits we don't want to show Huggle edits. |
||
| 251 | $condsTool[] = " |
||
| 252 | EXISTS ( |
||
| 253 | SELECT COUNT(ct_tag_id) AS tag_count |
||
| 254 | FROM $tagTable |
||
| 255 | WHERE ct_rev_id = revs.rev_id |
||
| 256 | HAVING tag_count = 1 AND ct_tag_id = $tagIds |
||
| 257 | )"; |
||
| 258 | } else { |
||
| 259 | $condsTool[] = "ct_tag_id IN ($tagIds)"; |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | $sql = "SELECT |
||
| 264 | page_title, |
||
| 265 | page_namespace, |
||
| 266 | revs.rev_id AS rev_id, |
||
| 267 | revs.rev_timestamp AS timestamp, |
||
| 268 | revs.rev_minor_edit AS minor, |
||
| 269 | revs.rev_len AS length, |
||
| 270 | (CAST(revs.rev_len AS SIGNED) - IFNULL(parentrevs.rev_len, 0)) AS length_change, |
||
| 271 | comment_text AS comment |
||
| 272 | FROM $pageTable |
||
| 273 | JOIN $revisionTable AS revs ON (page_id = revs.rev_page) |
||
| 274 | LEFT JOIN $revisionTable AS parentrevs ON (revs.rev_parent_id = parentrevs.rev_id) |
||
| 275 | LEFT OUTER JOIN $commentTable ON (revs.rev_comment_id = comment_id) |
||
| 276 | $tagJoin |
||
| 277 | WHERE revs.{$userClause} |
||
| 278 | $condBegin |
||
| 279 | $condEnd |
||
| 280 | $condNamespace |
||
| 281 | AND (".implode(' OR ', $condsTool).") |
||
| 282 | GROUP BY revs.rev_id |
||
| 283 | ORDER BY revs.rev_timestamp DESC |
||
| 284 | LIMIT 50 |
||
| 285 | OFFSET $offset"; |
||
| 286 | |||
| 287 | $resultQuery = $this->executeQuery($sql, $project, $user, $namespace, $start, $end, ['tools' => $regex]); |
||
| 288 | $result = $resultQuery->fetchAll(); |
||
| 289 | |||
| 290 | // Cache and return. |
||
| 291 | return $this->setCache($cacheKey, $result); |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Get counts of known automated tools used by the given user. |
||
| 296 | * @param Project $project |
||
| 297 | * @param User $user |
||
| 298 | * @param string|int $namespace Namespace ID or 'all'. |
||
| 299 | * @param string $start Start date in a format accepted by strtotime(). |
||
| 300 | * @param string $end End date in a format accepted by strtotime(). |
||
| 301 | * @return string[] Each tool that they used along with the count and link: |
||
| 302 | * [ |
||
| 303 | * 'Twinkle' => [ |
||
| 304 | * 'count' => 50, |
||
| 305 | * 'link' => 'Wikipedia:Twinkle', |
||
| 306 | * ], |
||
| 307 | * ] |
||
| 308 | */ |
||
| 309 | public function getToolCounts( |
||
| 310 | Project $project, |
||
| 311 | User $user, |
||
| 312 | $namespace = 'all', |
||
| 313 | $start = '', |
||
| 314 | $end = '' |
||
| 315 | ): array { |
||
| 316 | $cacheKey = $this->getCacheKey(func_get_args(), 'user_autotoolcounts'); |
||
| 317 | if ($this->cache->hasItem($cacheKey)) { |
||
| 318 | return $this->cache->getItem($cacheKey)->get(); |
||
| 319 | } |
||
| 320 | |||
| 321 | $sql = $this->getAutomatedCountsSql($project, $user, $namespace, $start, $end); |
||
| 322 | $resultQuery = $this->executeQuery($sql, $project, $user, $namespace, $start, $end); |
||
| 323 | |||
| 324 | $tools = $this->getTools($project, $namespace); |
||
| 325 | |||
| 326 | // handling results |
||
| 327 | $results = []; |
||
| 328 | |||
| 329 | while ($row = $resultQuery->fetch()) { |
||
| 330 | // Only track tools that they've used at least once |
||
| 331 | $tool = $row['toolname']; |
||
| 332 | if ($row['count'] > 0) { |
||
| 333 | $results[$tool] = [ |
||
| 334 | 'link' => $tools[$tool]['link'], |
||
| 335 | 'label' => $tools[$tool]['label'] ?? $tool, |
||
| 336 | 'count' => $row['count'], |
||
| 337 | ]; |
||
| 338 | } |
||
| 339 | } |
||
| 340 | |||
| 341 | // Sort the array by count |
||
| 342 | uasort($results, function ($a, $b) { |
||
| 343 | return $b['count'] - $a['count']; |
||
| 344 | }); |
||
| 345 | |||
| 346 | // Cache and return. |
||
| 347 | return $this->setCache($cacheKey, $results); |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Get SQL for getting counts of known automated tools used by the given user. |
||
| 352 | * @see self::getAutomatedCounts() |
||
| 353 | * @param Project $project |
||
| 354 | * @param User $user |
||
| 355 | * @param string|int $namespace Namespace ID or 'all'. |
||
| 356 | * @param string $start Start date in a format accepted by strtotime() |
||
| 357 | * @param string $end End date in a format accepted by strtotime() |
||
| 358 | * @return string The SQL. |
||
| 359 | */ |
||
| 360 | private function getAutomatedCountsSql(Project $project, User $user, $namespace, $start, $end): string |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Get some of the inner SQL for self::getAutomatedCountsSql(). |
||
| 407 | * @param Project $project |
||
| 408 | * @param string[] $values Values as defined in semi_automated.yml |
||
| 409 | * @return string[] [Equality clause, JOIN clause] |
||
| 410 | */ |
||
| 411 | private function getInnerAutomatedCountsSql(Project $project, array $values): array |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Get the combined regex and tags for all semi-automated tools, or the given tool, ready to be used in a query. |
||
| 456 | * @param Project $project |
||
| 457 | * @param bool $nonAutoEdits Set to true to exclude tools with the 'contribs' flag. |
||
| 458 | * @param string|null $tool |
||
| 459 | * @param int|string|null $namespace Tools only used in given namespace ID, or 'all' for all namespaces. |
||
| 460 | * @return array In the format: ['combined|regex', [1,2,3]] where the second element contains the tag IDs. |
||
| 461 | */ |
||
| 462 | private function getToolRegexAndTags( |
||
| 463 | Project $project, |
||
| 464 | bool $nonAutoEdits = false, |
||
| 465 | ?string $tool = null, |
||
| 466 | $namespace = null |
||
| 467 | ): array { |
||
| 468 | $tools = $this->getTools($project); |
||
| 469 | $regexes = []; |
||
| 470 | $allTagIds = $this->getTags($project); |
||
| 471 | $tagIds = []; |
||
| 472 | |||
| 473 | if ('' != $tool) { |
||
| 474 | $tools = [$tools[$tool]]; |
||
| 475 | } |
||
| 476 | |||
| 477 | foreach (array_values($tools) as $values) { |
||
| 478 | if ($nonAutoEdits && isset($values['contribs'])) { |
||
| 479 | continue; |
||
| 480 | } |
||
| 481 | |||
| 482 | if (is_numeric($namespace) && |
||
| 483 | !empty($values['namespaces']) && |
||
| 484 | !in_array((int)$namespace, $values['namespaces']) |
||
| 485 | ) { |
||
| 486 | continue; |
||
| 487 | } |
||
| 488 | |||
| 489 | if (isset($values['regex'])) { |
||
| 490 | $regexes[] = $values['regex']; |
||
| 491 | } |
||
| 492 | if (isset($values['tag']) && isset($allTagIds[$values['tag']])) { |
||
| 493 | $tagIds[] = $allTagIds[$values['tag']]; |
||
| 494 | } |
||
| 495 | } |
||
| 496 | |||
| 497 | return [ |
||
| 498 | implode('|', $regexes), |
||
| 499 | implode(',', $tagIds), |
||
| 500 | ]; |
||
| 501 | } |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Get the IDs of tags for given Project, which are used in the IN clauses of other queries above. |
||
| 505 | * This join decomposition is actually faster than JOIN'ing on change_tag_def all in one query. |
||
| 506 | * @param Project $project |
||
| 507 | * @return int[] |
||
| 508 | */ |
||
| 509 | public function getTags(Project $project): array |
||
| 535 | } |
||
| 536 | } |
||
| 537 |