Total Complexity | 46 |
Total Lines | 328 |
Duplicated Lines | 0 % |
Changes | 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 NON_GROUP_KEYS = [ 'override', 'override-perm', 'aliases' ]; |
||
14 | |||
15 | /** |
||
16 | * @var array The JSON list |
||
17 | * @phan-var array<string,array<string,string|string[]>> |
||
18 | */ |
||
19 | private $botList; |
||
20 | /** @var array[] The list from the API request */ |
||
21 | private $actualList; |
||
22 | |||
23 | /** |
||
24 | * @inheritDoc |
||
25 | */ |
||
26 | protected function getSubtasksMap(): array { |
||
27 | // Everything is done here. |
||
28 | return []; |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * @inheritDoc |
||
33 | */ |
||
34 | public function runInternal(): int { |
||
35 | $this->actualList = $this->getActualAdmins(); |
||
36 | $pageBotList = $this->getBotList(); |
||
37 | $this->botList = $pageBotList->getDecodedContent(); |
||
38 | |||
39 | $missing = $this->getMissingGroups(); |
||
40 | $extra = $this->getExtraGroups(); |
||
41 | |||
42 | $newContent = $this->getNewContent( $missing, $extra ); |
||
43 | |||
44 | if ( $newContent === $this->botList ) { |
||
45 | return TaskResult::STATUS_NOTHING; |
||
46 | } |
||
47 | |||
48 | $this->getLogger()->info( 'Updating admin list' ); |
||
49 | |||
50 | $pageBotList->edit( [ |
||
51 | 'text' => json_encode( $newContent ), |
||
52 | 'summary' => $this->msg( 'list-update-summary' )->text() |
||
53 | ] ); |
||
54 | |||
55 | return $this->errors ? TaskResult::STATUS_ERROR : TaskResult::STATUS_GOOD; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @return string[][] |
||
60 | * @phan-return array<string,string[]> |
||
61 | */ |
||
62 | protected function getActualAdmins(): array { |
||
63 | $params = [ |
||
64 | 'action' => 'query', |
||
65 | 'list' => 'allusers', |
||
66 | 'augroup' => 'sysop', |
||
67 | 'auprop' => 'groups', |
||
68 | 'aulimit' => 'max', |
||
69 | ]; |
||
70 | |||
71 | $req = $this->getWiki()->getRequestFactory()->createStandaloneRequest( $params ); |
||
72 | return $this->extractAdmins( $req->executeAsQuery() ); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param Generator $data |
||
77 | * @return string[][] |
||
78 | * @phan-return array<string,string[]> |
||
79 | */ |
||
80 | protected function extractAdmins( Generator $data ): array { |
||
81 | $ret = []; |
||
82 | $blacklist = $this->getOpt( 'exclude-admins' ); |
||
83 | foreach ( $data as $u ) { |
||
84 | if ( in_array( $u->name, $blacklist, true ) ) { |
||
85 | continue; |
||
86 | } |
||
87 | $interestingGroups = array_intersect( $u->groups, [ 'sysop', 'bureaucrat', 'checkuser' ] ); |
||
88 | $ret[ $u->name ] = array_values( $interestingGroups ); |
||
89 | } |
||
90 | return $ret; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Populate a list of new admins missing from the JSON list and their groups |
||
95 | * |
||
96 | * @return string[][] |
||
97 | */ |
||
98 | protected function getMissingGroups(): array { |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Get the flag date for the given admin and group. |
||
130 | * |
||
131 | * @param string $admin |
||
132 | * @param string $group |
||
133 | * @return string|null |
||
134 | */ |
||
135 | protected function getFlagDate( string $admin, string $group ): ?string { |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Find the actual timestamp when the user was given the searched group |
||
161 | * |
||
162 | * @param Generator $data |
||
163 | * @param string $group |
||
164 | * @return string|null |
||
165 | */ |
||
166 | private function extractTimestamp( Generator $data, string $group ): ?string { |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Get a list of admins who are in the JSON page but don't have the listed privileges anymore |
||
182 | * |
||
183 | * @return string[][] |
||
184 | */ |
||
185 | protected function getExtraGroups(): array { |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @param string[] $names |
||
200 | * @return Generator |
||
201 | */ |
||
202 | private function getRenameEntries( array $names ): Generator { |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Given a list of (old) usernames, check if these people have been renamed recently. |
||
222 | * |
||
223 | * @param string[] $names |
||
224 | * @return string[] [ old_name => new_name ] |
||
225 | */ |
||
226 | protected function getRenamedUsers( array $names ): array { |
||
227 | if ( !$names ) { |
||
228 | return []; |
||
229 | } |
||
230 | $this->getLogger()->info( 'Checking rename for ' . implode( ', ', $names ) ); |
||
231 | |||
232 | $data = $this->getRenameEntries( $names ); |
||
233 | $ret = []; |
||
234 | foreach ( $data as $entry ) { |
||
235 | // 1 month is arbitrary |
||
236 | if ( strtotime( $entry->timestamp ) > strtotime( '-1 month' ) ) { |
||
237 | $par = $entry->params; |
||
238 | $ret[ $par->olduser ] = $par->newuser; |
||
239 | } |
||
240 | } |
||
241 | $this->getLogger()->info( 'Renames found: ' . var_export( $ret, true ) ); |
||
242 | return $ret; |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Update aliases and overrides for renamed users |
||
247 | * |
||
248 | * @param array &$newContent |
||
249 | * @phan-param array<string,array<string,string|string[]>> $newContent |
||
250 | * @param string[][] $removed |
||
251 | */ |
||
252 | private function handleRenames( array &$newContent, array $removed ): void { |
||
267 | } |
||
268 | } |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * @param array &$newContent |
||
273 | * @phan-param array<string,array<string,string|string[]>> $newContent |
||
274 | * @param string[][] $missing |
||
275 | * @param string[][] $extra |
||
276 | * @return string[][] Removed users |
||
277 | */ |
||
278 | private function handleExtraAndMissing( |
||
279 | array &$newContent, |
||
280 | array $missing, |
||
281 | array $extra |
||
282 | ): array { |
||
283 | $removed = []; |
||
284 | foreach ( $newContent as $user => $data ) { |
||
285 | if ( isset( $missing[ $user ] ) ) { |
||
286 | $newContent[ $user ] = array_merge( $data, $missing[ $user ] ); |
||
287 | unset( $missing[ $user ] ); |
||
288 | } elseif ( isset( $extra[ $user ] ) ) { |
||
289 | $newGroups = array_diff_key( $data, $extra[ $user ] ); |
||
290 | if ( array_diff_key( $newGroups, array_fill_keys( self::NON_GROUP_KEYS, 1 ) ) ) { |
||
291 | $newContent[ $user ] = $newGroups; |
||
292 | } else { |
||
293 | $removed[$user] = $data; |
||
294 | unset( $newContent[ $user ] ); |
||
295 | } |
||
296 | } |
||
297 | } |
||
298 | // Add users which don't have an entry at all |
||
299 | $newContent = array_merge( $newContent, $missing ); |
||
300 | return $removed; |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Get the new content for the list |
||
305 | * |
||
306 | * @param string[][] $missing |
||
307 | * @param string[][] $extra |
||
308 | * @return array[] |
||
309 | */ |
||
310 | protected function getNewContent( array $missing, array $extra ): array { |
||
311 | $newContent = $this->botList; |
||
312 | |||
313 | $removed = $this->handleExtraAndMissing( $newContent, $missing, $extra ); |
||
314 | |||
315 | $this->handleRenames( $newContent, $removed ); |
||
316 | |||
317 | $this->removeOverrides( $newContent ); |
||
318 | |||
319 | ksort( $newContent, SORT_STRING | SORT_FLAG_CASE ); |
||
320 | |||
321 | return $newContent; |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Remove expired overrides. |
||
326 | * |
||
327 | * @param array[] &$newContent |
||
328 | */ |
||
329 | protected function removeOverrides( array &$newContent ): void { |
||
340 | } |
||
341 | } |
||
342 | } |
||
343 |