| Total Complexity | 59 |
| Total Lines | 569 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ClosePages 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 ClosePages, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare( strict_types=1 ); |
||
| 13 | class ClosePages extends Subtask { |
||
| 14 | /** |
||
| 15 | * @inheritDoc |
||
| 16 | */ |
||
| 17 | public function run() : TaskResult { |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Extract the list of failed votes from the given list of pages |
||
| 51 | * |
||
| 52 | * @param PageRiconferma[] $pages |
||
| 53 | * @return PageRiconferma[] |
||
| 54 | */ |
||
| 55 | private function getFailures( array $pages ) : array { |
||
| 56 | $ret = []; |
||
| 57 | foreach ( $pages as $page ) { |
||
| 58 | if ( $page->getOutcome() & PageRiconferma::OUTCOME_FAIL ) { |
||
| 59 | $ret[] = $page; |
||
| 60 | } |
||
| 61 | } |
||
| 62 | return $ret; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param PageRiconferma $page |
||
| 67 | */ |
||
| 68 | protected function addVoteCloseText( PageRiconferma $page ) { |
||
| 69 | $content = $page->getContent(); |
||
| 70 | $beforeReg = '!è necessario ottenere una maggioranza .+ votanti\.!'; |
||
| 71 | $newContent = preg_replace( $beforeReg, '$0' . "\n" . $page->getOutcomeText(), $content ); |
||
| 72 | |||
| 73 | $params = [ |
||
| 74 | 'text' => $newContent, |
||
| 75 | 'summary' => $this->getConfig()->get( 'close-result-summary' ) |
||
| 76 | ]; |
||
| 77 | $page->edit( $params ); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Removes pages from WP:A/Riconferme annuali |
||
| 82 | * |
||
| 83 | * @param PageRiconferma[] $pages |
||
| 84 | * @see UpdatesAround::addToMainPage() |
||
| 85 | */ |
||
| 86 | protected function removeFromMainPage( array $pages ) { |
||
| 87 | $this->getLogger()->info( |
||
| 88 | 'Removing from main: ' . implode( ', ', array_map( 'strval', $pages ) ) |
||
| 89 | ); |
||
| 90 | |||
| 91 | $mainPage = new Page( $this->getConfig()->get( 'ric-main-page' ) ); |
||
| 92 | $translations = []; |
||
| 93 | foreach ( $pages as $page ) { |
||
| 94 | $translations[ '{{' . $page->getTitle() . '}}' ] = ''; |
||
| 95 | } |
||
| 96 | |||
| 97 | $params = [ |
||
| 98 | 'title' => $mainPage, |
||
| 99 | 'text' => strtr( $mainPage->getContent(), $translations ), |
||
| 100 | 'summary' => $this->getConfig()->get( 'close-main-summary' ) |
||
| 101 | ]; |
||
| 102 | $this->getController()->editPage( $params ); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Adds closed pages to the current archive |
||
| 107 | * |
||
| 108 | * @param PageRiconferma[] $pages |
||
| 109 | */ |
||
| 110 | protected function addToArchive( array $pages ) { |
||
| 111 | $this->getLogger()->info( |
||
| 112 | 'Adding to archive: ' . implode( ', ', array_map( 'strval', $pages ) ) |
||
| 113 | ); |
||
| 114 | |||
| 115 | $simple = $votes = []; |
||
| 116 | foreach ( $pages as $page ) { |
||
| 117 | if ( $page->isVote() ) { |
||
| 118 | $votes[] = $page; |
||
| 119 | } else { |
||
| 120 | $simple[] = $page; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | $simpleTitle = $this->getConfig()->get( 'close-simple-archive-title' ); |
||
| 125 | $voteTitle = $this->getConfig()->get( 'close-vote-archive-title' ); |
||
| 126 | |||
| 127 | $this->reallyAddToArchive( $simpleTitle, $simple ); |
||
| 128 | $this->reallyAddToArchive( $voteTitle, $votes ); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Really add $pages to the given archive |
||
| 133 | * |
||
| 134 | * @param string $archiveTitle |
||
| 135 | * @param array $pages |
||
| 136 | */ |
||
| 137 | private function reallyAddToArchive( string $archiveTitle, array $pages ) { |
||
| 138 | $curTitle = "$archiveTitle/" . date( 'Y' ); |
||
| 139 | |||
| 140 | $append = ''; |
||
| 141 | $archivedList = []; |
||
| 142 | foreach ( $pages as $page ) { |
||
| 143 | $append .= '{{' . $page->getTitle() . "}}\n"; |
||
| 144 | $archivedList[] = $page->getUserNum(); |
||
| 145 | } |
||
| 146 | |||
| 147 | if ( count( $archivedList ) > 1 ) { |
||
| 148 | $last = array_pop( $archivedList ); |
||
| 149 | $userNums = implode( ', ', $archivedList ) . " e $last"; |
||
| 150 | } else { |
||
| 151 | $userNums = $archivedList[0]; |
||
| 152 | } |
||
| 153 | |||
| 154 | $summary = $this->msg( 'close-archive-summary' ) |
||
| 155 | ->params( [ '$usernums' => $userNums ] )->text(); |
||
| 156 | |||
| 157 | $params = [ |
||
| 158 | 'title' => $curTitle, |
||
| 159 | 'appendtext' => $append, |
||
| 160 | 'summary' => $summary |
||
| 161 | ]; |
||
| 162 | |||
| 163 | $this->getController()->editPage( $params ); |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param PageRiconferma $page |
||
| 168 | * @see CreatePages::updateBasePage() |
||
| 169 | */ |
||
| 170 | protected function updateBasePage( PageRiconferma $page ) { |
||
| 171 | $this->getLogger()->info( "Updating base page for $page" ); |
||
| 172 | |||
| 173 | $basePage = new Page( $page->getBaseTitle() ); |
||
| 174 | $current = $basePage->getContent(); |
||
| 175 | |||
| 176 | $outcomeText = $page->getOutcome() & PageRiconferma::OUTCOME_FAIL ? |
||
| 177 | 'non riconfermato' : |
||
| 178 | 'riconfermato'; |
||
| 179 | $text = $page->isVote() ? "votazione: $outcomeText" : 'riconferma tacita'; |
||
| 180 | |||
| 181 | $newContent = str_replace( 'riconferma in corso', $text, $current ); |
||
| 182 | $params = [ |
||
| 183 | 'text' => $newContent, |
||
| 184 | 'summary' => $this->getConfig()->get( 'close-base-page-summary-update' ) |
||
| 185 | ]; |
||
| 186 | |||
| 187 | $basePage->edit( $params ); |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param PageRiconferma[] $pages |
||
| 192 | * @see UpdatesAround::addVote() |
||
| 193 | */ |
||
| 194 | protected function updateVote( array $pages ) { |
||
| 195 | $this->getLogger()->info( |
||
| 196 | 'Updating votazioni: ' . implode( ', ', array_map( 'strval', $pages ) ) |
||
| 197 | ); |
||
| 198 | $votePage = new Page( $this->getConfig()->get( 'ric-vote-page' ) ); |
||
| 199 | $content = $votePage->getContent(); |
||
| 200 | |||
| 201 | $titles = []; |
||
| 202 | foreach ( $pages as $page ) { |
||
| 203 | $titles[] = preg_quote( $page->getTitle() ); |
||
| 204 | } |
||
| 205 | |||
| 206 | $titleReg = implode( '|', array_map( 'preg_quote', $titles ) ); |
||
| 207 | $search = "!^\*.+ La \[\[($titleReg)\|procedura]] termina.+\n!gm"; |
||
| 208 | |||
| 209 | $newContent = preg_replace( $search, '', $content ); |
||
| 210 | // Make sure the last line ends with a full stop in every section |
||
| 211 | $simpleSectReg = '!(^;È in corso.+riconferma tacita.+amministrat.+\n(?:\*.+[;\.]\n)+\*.+)[\.;]!m'; |
||
| 212 | $voteSectReg = '!(^;Si vota per la .+riconferma .+amministratori.+\n(?:\*.+[;\.]\n)+\*.+)[\.;]!m'; |
||
| 213 | $newContent = preg_replace( $simpleSectReg, '$1.', $newContent ); |
||
| 214 | $newContent = preg_replace( $voteSectReg, '$1.', $newContent ); |
||
| 215 | |||
| 216 | // @fixme Remove empty sections, and add the "''Nessuna riconferma o votazione in corso''" message |
||
| 217 | // if the page is empty! Or just wait for the page to be restyled... |
||
| 218 | |||
| 219 | $summary = $this->msg( 'close-vote-page-summary' ) |
||
| 220 | ->params( [ '$num' => count( $pages ) ] )->text(); |
||
| 221 | |||
| 222 | $params = [ |
||
| 223 | 'text' => $newContent, |
||
| 224 | 'summary' => $summary |
||
| 225 | ]; |
||
| 226 | |||
| 227 | $votePage->edit( $params ); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param array $pages |
||
| 232 | * @see UpdatesAround::addNews() |
||
| 233 | */ |
||
| 234 | protected function updateNews( array $pages ) { |
||
| 235 | $simpleAmount = $voteAmount = 0; |
||
| 236 | foreach ( $pages as $page ) { |
||
| 237 | if ( $page->isVote() ) { |
||
| 238 | $voteAmount++; |
||
| 239 | } else { |
||
| 240 | $simpleAmount++; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | $this->getLogger()->info( |
||
| 245 | "Decreasing the news counter: $simpleAmount simple, $voteAmount votes." |
||
| 246 | ); |
||
| 247 | |||
| 248 | $newsPage = new Page( $this->getConfig()->get( 'ric-news-page' ) ); |
||
| 249 | |||
| 250 | $content = $newsPage->getContent(); |
||
| 251 | $simpleReg = '!(\| *riconferme[ _]tacite[ _]amministratori *= *)(\d+)!'; |
||
| 252 | $voteReg = '!(\| *riconferme[ _]voto[ _]amministratori *= *)(\d+)!'; |
||
| 253 | |||
| 254 | $simpleMatches = $voteMatches = []; |
||
| 255 | preg_match( $simpleReg, $content, $simpleMatches ); |
||
| 256 | preg_match( $voteReg, $content, $voteMatches ); |
||
| 257 | |||
| 258 | $newSimp = (int)$simpleMatches[2] - $simpleAmount ?: ''; |
||
| 259 | $newVote = (int)$voteMatches[2] - $voteAmount ?: ''; |
||
| 260 | $newContent = preg_replace( $simpleReg, '${1}' . $newSimp, $content ); |
||
| 261 | $newContent = preg_replace( $voteReg, '${1}' . $newVote, $newContent ); |
||
| 262 | |||
| 263 | $summary = $this->msg( 'close-news-page-summary' ) |
||
| 264 | ->params( [ '$num' => count( $pages ) ] )->text(); |
||
| 265 | |||
| 266 | $params = [ |
||
| 267 | 'text' => $newContent, |
||
| 268 | 'summary' => $summary |
||
| 269 | ]; |
||
| 270 | |||
| 271 | $newsPage->edit( $params ); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Update date on WP:Amministratori/Lista |
||
| 276 | * |
||
| 277 | * @param PageRiconferma[] $pages |
||
| 278 | */ |
||
| 279 | protected function updateAdminList( array $pages ) { |
||
| 280 | $this->getLogger()->info( |
||
| 281 | 'Updating admin list: ' . implode( ', ', array_map( 'strval', $pages ) ) |
||
| 282 | ); |
||
| 283 | $adminsPage = new Page( $this->getConfig()->get( 'admins-list' ) ); |
||
| 284 | $newContent = $adminsPage->getContent(); |
||
| 285 | $newDate = date( 'Ymd', strtotime( '+1 year' ) ); |
||
| 286 | |||
| 287 | $riconfNames = $removeNames = []; |
||
| 288 | foreach ( $pages as $page ) { |
||
| 289 | $user = $page->getUser(); |
||
| 290 | $reg = "!(\{\{Amministratore\/riga\|$user.+\| *)\d+( *\|(?: *pausa)? *\}\}\n)!"; |
||
| 291 | if ( $page->getOutcome() & PageRiconferma::OUTCOME_FAIL ) { |
||
| 292 | // Remove the line |
||
| 293 | $newContent = preg_replace( $reg, '', $newContent ); |
||
| 294 | $removeNames[] = $user; |
||
| 295 | } else { |
||
| 296 | $newContent = preg_replace( $reg, '$1' . $newDate . '$2', $newContent ); |
||
| 297 | $riconfNames[] = $user; |
||
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | if ( count( $riconfNames ) > 1 ) { |
||
| 302 | $lastUser = array_pop( $riconfNames ); |
||
| 303 | $riconfList = implode( ', ', $riconfNames ) . " e $lastUser"; |
||
| 304 | } elseif ( $riconfNames ) { |
||
| 305 | $riconfList = $riconfNames[0]; |
||
| 306 | } else { |
||
| 307 | $riconfList = 'nessuno'; |
||
| 308 | } |
||
| 309 | |||
| 310 | if ( count( $removeNames ) > 1 ) { |
||
| 311 | $lastUser = array_pop( $removeNames ); |
||
| 312 | $removeList = implode( ', ', $removeNames ) . " e $lastUser"; |
||
| 313 | } elseif ( $removeNames ) { |
||
| 314 | $removeList = $removeNames[0]; |
||
| 315 | } else { |
||
| 316 | $removeList = 'nessuno'; |
||
| 317 | } |
||
| 318 | |||
| 319 | $summary = $this->msg( 'close-update-list-summary' ) |
||
| 320 | ->params( [ |
||
| 321 | '$riconf' => $riconfList, |
||
| 322 | '$remove' => $removeList |
||
| 323 | ] ) |
||
| 324 | ->text(); |
||
| 325 | |||
| 326 | $params = [ |
||
| 327 | 'text' => $newContent, |
||
| 328 | 'summary' => $summary |
||
| 329 | ]; |
||
| 330 | |||
| 331 | $adminsPage->edit( $params ); |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param PageRiconferma[] $pages |
||
| 336 | */ |
||
| 337 | protected function updateCUList( array $pages ) { |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @param PageRiconferma[] $pages |
||
| 401 | */ |
||
| 402 | protected function updateBurList( array $pages ) { |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Request the removal of the flag on meta |
||
| 447 | * |
||
| 448 | * @param PageRiconferma[] $pages |
||
| 449 | */ |
||
| 450 | protected function requestRemoval( array $pages ) { |
||
| 451 | $this->getLogger()->info( |
||
| 452 | 'Requesting removal on meta for: ' . implode( ', ', array_map( 'strval', $pages ) ) |
||
| 453 | ); |
||
| 454 | $admins = $this->getDataProvider()->getUsersList(); |
||
| 455 | |||
| 456 | $flagRemPage = new Page( |
||
| 457 | $this->getConfig()->get( 'flag-removal-page' ), |
||
| 458 | 'https://meta.wikimedia.org/w/api.php' |
||
| 459 | ); |
||
| 460 | $section = $this->getConfig()->get( 'flag-removal-section' ); |
||
| 461 | $baseText = $this->getConfig()->get( 'flag-removal-text' ); |
||
| 462 | |||
| 463 | $newContent = $flagRemPage->getContent( $section ); |
||
| 464 | foreach ( $pages as $page ) { |
||
| 465 | $curText = strtr( |
||
| 466 | $baseText, |
||
| 467 | [ |
||
| 468 | '$username' => $page->getUser(), |
||
| 469 | '$link' => '[[:it:' . $page->getTitle() . ']]', |
||
| 470 | '$groups' => implode( ', ', array_keys( $admins[ $page->getUser() ] ) ) |
||
| 471 | ] |
||
| 472 | ); |
||
| 473 | $newContent .= $curText; |
||
| 474 | } |
||
| 475 | |||
| 476 | $summary = $this->msg( 'flag-removal-summary' ) |
||
| 477 | ->params( [ '$num' => count( $pages ) ] ) |
||
| 478 | ->text(); |
||
| 479 | |||
| 480 | $params = [ |
||
| 481 | 'section' => $section, |
||
| 482 | 'text' => $newContent, |
||
| 483 | 'summary' => $summary |
||
| 484 | ]; |
||
| 485 | $flagRemPage->edit( $params ); |
||
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Update [[Wikipedia:Wikipediano/Annunci]] |
||
| 490 | * |
||
| 491 | * @param PageRiconferma[] $pages |
||
| 492 | */ |
||
| 493 | protected function updateAnnunci( array $pages ) { |
||
| 535 | } |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Update [[Wikipedia:Ultime notizie]] |
||
| 539 | * |
||
| 540 | * @param PageRiconferma[] $pages |
||
| 541 | */ |
||
| 542 | protected function updateUltimeNotizie( array $pages ) { |
||
| 584 |