Total Complexity | 42 |
Total Lines | 308 |
Duplicated Lines | 0 % |
Changes | 22 | ||
Bugs | 1 | 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 ); |
||
14 | class UpdateList extends Task { |
||
15 | /** @var array[] The JSON list */ |
||
16 | private $botList; |
||
17 | /** @var array[] The list from the API request */ |
||
18 | private $actualList; |
||
19 | |||
20 | /** |
||
21 | * @inheritDoc |
||
22 | */ |
||
23 | protected function getSubtasksMap(): array { |
||
24 | // Everything is done here. |
||
25 | return []; |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * @inheritDoc |
||
30 | */ |
||
31 | public function runInternal() : int { |
||
32 | $this->actualList = $this->getActualAdmins(); |
||
33 | $pageBotList = PageBotList::get( $this->getWiki() ); |
||
34 | $this->botList = $pageBotList->getDecodedContent(); |
||
35 | |||
36 | $missing = $this->getMissingGroups(); |
||
37 | $extra = $this->getExtraGroups(); |
||
38 | |||
39 | $newContent = $this->getNewContent( $missing, $extra ); |
||
40 | |||
41 | if ( $newContent === $this->botList ) { |
||
42 | return TaskResult::STATUS_NOTHING; |
||
43 | } |
||
44 | |||
45 | $this->getLogger()->info( 'Updating admin list' ); |
||
46 | |||
47 | $pageBotList->edit( [ |
||
48 | 'text' => json_encode( $newContent ), |
||
49 | 'summary' => $this->msg( 'list-update-summary' )->text() |
||
50 | ] ); |
||
51 | |||
52 | return $this->errors ? TaskResult::STATUS_ERROR : TaskResult::STATUS_GOOD; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @return array |
||
57 | */ |
||
58 | protected function getActualAdmins() : array { |
||
59 | $params = [ |
||
60 | 'action' => 'query', |
||
61 | 'list' => 'allusers', |
||
62 | 'augroup' => 'sysop', |
||
63 | 'auprop' => 'groups', |
||
64 | 'aulimit' => 'max', |
||
65 | ]; |
||
66 | |||
67 | $req = RequestBase::newFromParams( $params ); |
||
68 | return $this->extractAdmins( $req->execute() ); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @param \stdClass $data |
||
73 | * @return array |
||
74 | */ |
||
75 | protected function extractAdmins( \stdClass $data ) : array { |
||
76 | $ret = []; |
||
77 | $blacklist = $this->getOpt( 'exclude-admins' ); |
||
78 | foreach ( $data->query->allusers as $u ) { |
||
79 | if ( in_array( $u->name, $blacklist ) ) { |
||
80 | continue; |
||
81 | } |
||
82 | $interestingGroups = array_intersect( $u->groups, [ 'sysop', 'bureaucrat', 'checkuser' ] ); |
||
83 | $ret[ $u->name ] = array_values( $interestingGroups ); |
||
84 | } |
||
85 | return $ret; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Populate a list of new admins missing from the JSON list and their groups |
||
90 | * |
||
91 | * @return array[] |
||
92 | */ |
||
93 | protected function getMissingGroups() : array { |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Get the flag date for the given admin and group. |
||
111 | * |
||
112 | * @param string $admin |
||
113 | * @param string $group |
||
114 | * @return string |
||
115 | * @throws TaskException |
||
116 | */ |
||
117 | protected function getFlagDate( string $admin, string $group ) : string { |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Find the actual timestamp when the user was given the searched group |
||
148 | * |
||
149 | * @param \stdClass $data |
||
150 | * @param string $group |
||
151 | * @return string|null |
||
152 | */ |
||
153 | private function extractTimestamp( \stdClass $data, string $group ) : ?string { |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Get a list of admins who are in the JSON page but don't have the listed privileges anymore |
||
169 | * |
||
170 | * @return array[] |
||
171 | */ |
||
172 | protected function getExtraGroups() : array { |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @param string[] $names |
||
187 | * @return \stdClass |
||
188 | */ |
||
189 | private function getRenameEntries( array $names ) : \stdClass { |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Given a list of (old) usernames, check if these people have been renamed recently. |
||
209 | * |
||
210 | * @param string[] $names |
||
211 | * @return string[] [ old_name => new_name ] |
||
212 | */ |
||
213 | protected function getRenamedUsers( array $names ) : array { |
||
214 | if ( !$names ) { |
||
215 | return []; |
||
216 | } |
||
217 | $this->getLogger()->info( 'Checking rename for ' . implode( ', ', $names ) ); |
||
218 | |||
219 | $data = $this->getRenameEntries( $names ); |
||
220 | $ret = []; |
||
221 | foreach ( $data->query->logevents as $entry ) { |
||
222 | // 1 month is arbitrary |
||
223 | if ( strtotime( $entry->timestamp ) > strtotime( '-1 month' ) ) { |
||
224 | $par = $entry->params; |
||
225 | $ret[ $par->olduser ] = $par->newuser; |
||
226 | } |
||
227 | } |
||
228 | $this->getLogger()->info( 'Renames found: ' . var_export( $ret, true ) ); |
||
229 | return $ret; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Update aliases and overrides for renamed users |
||
234 | * |
||
235 | * @param array &$newContent |
||
236 | * @param array $removed |
||
237 | */ |
||
238 | private function handleRenames( array &$newContent, array $removed ) : void { |
||
253 | } |
||
254 | } |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * @param array[] &$newContent |
||
259 | * @param array[] $missing |
||
260 | * @param array[] $extra |
||
261 | * @return string[] Removed users |
||
262 | */ |
||
263 | private function handleExtraAndMissing( array &$newContent, array $missing, array $extra ) : array { |
||
264 | $removed = []; |
||
265 | foreach ( $newContent as $user => $groups ) { |
||
266 | if ( isset( $missing[ $user ] ) ) { |
||
267 | $newContent[ $user ] = array_merge( $groups, $missing[ $user ] ); |
||
268 | unset( $missing[ $user ] ); |
||
269 | } elseif ( isset( $extra[ $user ] ) ) { |
||
270 | $newGroups = array_diff_key( $groups, $extra[ $user ] ); |
||
271 | if ( array_diff_key( $newGroups, array_fill_keys( PageBotList::NON_GROUP_KEYS, 1 ) ) ) { |
||
272 | $newContent[ $user ] = $newGroups; |
||
273 | } else { |
||
274 | $removed[$user] = $newContent[$user]; |
||
275 | unset( $newContent[ $user ] ); |
||
276 | } |
||
277 | } |
||
278 | } |
||
279 | // Add users which don't have an entry at all |
||
280 | $newContent = array_merge( $newContent, $missing ); |
||
281 | return $removed; |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Get the new content for the list |
||
286 | * |
||
287 | * @param array[] $missing |
||
288 | * @param array[] $extra |
||
289 | * @return array[] |
||
290 | */ |
||
291 | protected function getNewContent( array $missing, array $extra ) : array { |
||
292 | $newContent = $this->botList; |
||
293 | |||
294 | $removed = $this->handleExtraAndMissing( $newContent, $missing, $extra ); |
||
295 | |||
296 | $this->handleRenames( $newContent, $removed ); |
||
297 | |||
298 | $this->removeOverrides( $newContent ); |
||
299 | |||
300 | ksort( $newContent, SORT_STRING | SORT_FLAG_CASE ); |
||
301 | |||
302 | return $newContent; |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * Remove expired overrides. This must happen after the override date has been used AND |
||
307 | * after the "normal" date has passed. We do it 3 days later to be sure. |
||
308 | * |
||
309 | * @param array[] &$newContent |
||
310 | */ |
||
311 | protected function removeOverrides( array &$newContent ) : void { |
||
322 | } |
||
323 | } |
||
324 | } |
||
325 |