| Total Complexity | 42 |
| Total Lines | 311 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like UpdateList 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 UpdateList, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare( strict_types=1 ); |
||
| 12 | class UpdateList extends Task { |
||
| 13 | private const RELEVANT_GROUPS = [ 'sysop', 'bureaucrat', 'checkuser' ]; |
||
| 14 | private const NON_GROUP_KEYS = [ 'override' => 1, 'override-perm' => 1, 'aliases' => 1 ]; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var array[] The list from the API request, mapping [ user => group[] ] |
||
| 18 | * @phan-var array<string,list<string>> |
||
| 19 | */ |
||
| 20 | private $actualList; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @inheritDoc |
||
| 24 | */ |
||
| 25 | protected function getSubtasksMap(): array { |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @inheritDoc |
||
| 32 | */ |
||
| 33 | public function runInternal(): int { |
||
| 34 | $this->actualList = $this->computeActualList(); |
||
| 35 | $pageBotList = $this->getBotList(); |
||
| 36 | $botList = $pageBotList->getDecodedContent(); |
||
| 37 | |||
| 38 | $newList = $this->computeNewList( $botList ); |
||
| 39 | |||
| 40 | if ( $newList === $botList ) { |
||
| 41 | return TaskResult::STATUS_NOTHING; |
||
| 42 | } |
||
| 43 | |||
| 44 | $this->getLogger()->info( 'Updating admin list' ); |
||
| 45 | |||
| 46 | $pageBotList->edit( [ |
||
| 47 | 'text' => json_encode( $newList ), |
||
| 48 | 'summary' => $this->msg( 'list-update-summary' )->text() |
||
| 49 | ] ); |
||
| 50 | |||
| 51 | return $this->errors ? TaskResult::STATUS_ERROR : TaskResult::STATUS_GOOD; |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @return string[][] |
||
| 56 | * @phan-return array<string,string[]> |
||
| 57 | */ |
||
| 58 | private function computeActualList(): array { |
||
| 59 | $params = [ |
||
| 60 | 'action' => 'query', |
||
| 61 | 'list' => 'allusers', |
||
| 62 | 'augroup' => 'sysop', |
||
| 63 | 'auprop' => 'groups', |
||
| 64 | 'aulimit' => 'max', |
||
| 65 | ]; |
||
| 66 | |||
| 67 | $req = $this->getWiki()->getRequestFactory()->createStandaloneRequest( $params ); |
||
| 68 | return $this->extractAdminsData( $req->executeAsQuery() ); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param Generator $data |
||
| 73 | * @return string[][] |
||
| 74 | * @phan-return array<string,string[]> |
||
| 75 | */ |
||
| 76 | private function extractAdminsData( Generator $data ): array { |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Get the new content for the list |
||
| 91 | * |
||
| 92 | * @param array[] $curList |
||
| 93 | * @phpcs:ignore Generic.Files.LineLength |
||
| 94 | * @phan-param array<string,array{sysop:string,checkuser?:string,bureaucrat?:string,override?:string,override-perm?:string,aliases?:list<string>}> $curList |
||
| 95 | * @return array[] |
||
| 96 | */ |
||
| 97 | private function computeNewList( array $curList ): array { |
||
| 98 | $newList = $curList; |
||
| 99 | |||
| 100 | $extra = $this->getExtraAdminGroups( $curList ); |
||
| 101 | if ( $extra ) { |
||
| 102 | $renamed = $this->handleRenames( $newList, $extra ); |
||
| 103 | $extra = array_diff_key( $extra, $renamed ); |
||
| 104 | } |
||
| 105 | $this->handleExtraAndMissing( $newList, $extra ); |
||
| 106 | $this->removeOverrides( $newList ); |
||
| 107 | |||
| 108 | ksort( $newList, SORT_STRING | SORT_FLAG_CASE ); |
||
| 109 | return $newList; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param array &$newList |
||
| 114 | * @phan-param array<string,array<string,string|string[]>> &$newList |
||
| 115 | * @param string[][] $extra |
||
| 116 | */ |
||
| 117 | private function handleExtraAndMissing( array &$newList, array $extra ): void { |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Populate a list of new admins missing from the JSON list and their groups |
||
| 144 | * |
||
| 145 | * @param array $botList |
||
| 146 | * @phan-param array<string,array<string,string|string[]>> $botList |
||
| 147 | * @return string[][] |
||
| 148 | */ |
||
| 149 | private function getMissingAdminGroups( array $botList ): array { |
||
| 150 | $missing = []; |
||
| 151 | foreach ( $this->actualList as $admin => $groups ) { |
||
| 152 | $missingGroups = array_diff( $groups, array_keys( $botList[$admin] ?? [] ) ); |
||
| 153 | foreach ( $missingGroups as $group ) { |
||
| 154 | $ts = $this->getFlagDate( $admin, $group ); |
||
| 155 | if ( $ts === null ) { |
||
| 156 | $this->errors[] = "$group flag date unavailable for $admin"; |
||
| 157 | continue; |
||
| 158 | } |
||
| 159 | $missing[$admin][$group] = $ts; |
||
| 160 | } |
||
| 161 | } |
||
| 162 | return $missing; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Get the flag date for the given admin and group. |
||
| 167 | * |
||
| 168 | * @param string $admin |
||
| 169 | * @param string $group |
||
| 170 | * @return string|null |
||
| 171 | */ |
||
| 172 | private function getFlagDate( string $admin, string $group ): ?string { |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Find the actual timestamp when the user was given the searched group |
||
| 198 | * |
||
| 199 | * @param Generator $data |
||
| 200 | * @param string $group |
||
| 201 | * @return string|null |
||
| 202 | */ |
||
| 203 | private function extractTimestamp( Generator $data, string $group ): ?string { |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Get a list of admins who are in the JSON page but don't have the listed privileges anymore |
||
| 219 | * |
||
| 220 | * @param array[] $botList |
||
| 221 | * @phpcs:ignore Generic.Files.LineLength |
||
| 222 | * @phan-param array<string,array{sysop:string,checkuser?:string,bureaucrat?:string,override?:string,override-perm?:string,aliases?:list<string>}> $botList |
||
| 223 | * @return string[][] |
||
| 224 | */ |
||
| 225 | private function getExtraAdminGroups( array $botList ): array { |
||
| 226 | $extra = []; |
||
| 227 | foreach ( $botList as $name => $data ) { |
||
| 228 | $groups = array_diff_key( $data, self::NON_GROUP_KEYS ); |
||
| 229 | if ( !isset( $this->actualList[$name] ) ) { |
||
| 230 | $extra[$name] = $groups; |
||
| 231 | } elseif ( count( $groups ) > count( $this->actualList[$name] ) ) { |
||
| 232 | $extra[$name] = array_diff_key( $groups, $this->actualList[$name] ); |
||
| 233 | } |
||
| 234 | } |
||
| 235 | return $extra; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param string[] $oldNames |
||
| 240 | * @return Generator |
||
| 241 | */ |
||
| 242 | private function getRenameEntries( array $oldNames ): Generator { |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Given a list of (old) usernames, check if these people have been renamed recently. |
||
| 262 | * |
||
| 263 | * @param string[] $oldNames |
||
| 264 | * @return string[] [ old_name => new_name ] |
||
| 265 | */ |
||
| 266 | private function getRenamedUsers( array $oldNames ): array { |
||
| 267 | if ( !$oldNames ) { |
||
| 268 | return []; |
||
| 269 | } |
||
| 270 | $this->getLogger()->info( 'Checking rename for ' . implode( ', ', $oldNames ) ); |
||
| 271 | |||
| 272 | $data = $this->getRenameEntries( $oldNames ); |
||
| 273 | $ret = []; |
||
| 274 | foreach ( $data as $entry ) { |
||
| 275 | // 1 month is arbitrary |
||
| 276 | if ( strtotime( $entry->timestamp ) > strtotime( '-1 month' ) ) { |
||
| 277 | $par = $entry->params; |
||
| 278 | $ret[ $par->olduser ] = $par->newuser; |
||
| 279 | } |
||
| 280 | } |
||
| 281 | $this->getLogger()->info( 'Renames found: ' . var_export( $ret, true ) ); |
||
| 282 | return $ret; |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Checks whether any user that is on the bot list but is not an admin according to MW |
||
| 287 | * was actually renamed, and updates the list accordingly. |
||
| 288 | * |
||
| 289 | * @param array &$newList |
||
| 290 | * @phan-param array<string,array<string,string|string[]>> $newList |
||
| 291 | * @param string[][] $extra |
||
| 292 | * @return array<string,string> Map of renamed users |
||
| 293 | */ |
||
| 294 | private function handleRenames( array &$newList, array $extra ): array { |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Remove expired overrides. |
||
| 309 | * |
||
| 310 | * @param array[] &$newList |
||
| 311 | */ |
||
| 312 | private function removeOverrides( array &$newList ): void { |
||
| 323 | } |
||
| 324 | } |
||
| 325 | } |
||
| 326 |