1
|
|
|
<?php declare( strict_types=1 ); |
2
|
|
|
|
3
|
|
|
namespace BotRiconferme\Task\Subtask; |
4
|
|
|
|
5
|
|
|
use BotRiconferme\Message\Message; |
6
|
|
|
use BotRiconferme\TaskHelper\TaskResult; |
7
|
|
|
use BotRiconferme\Utils\RegexUtils; |
8
|
|
|
use BotRiconferme\Wiki\Page\PageRiconferma; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Update various pages around, to be done for all closed procedures |
12
|
|
|
*/ |
13
|
|
|
class SimpleUpdates extends Subtask { |
14
|
|
|
/** |
15
|
|
|
* @inheritDoc |
16
|
|
|
*/ |
17
|
|
|
public function runInternal(): int { |
18
|
|
|
$pages = $this->getDataProvider()->getPagesToClose(); |
19
|
|
|
|
20
|
|
|
if ( !$pages ) { |
21
|
|
|
return TaskResult::STATUS_NOTHING; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
$this->updateVotazioni( $pages ); |
25
|
|
|
$this->updateNews( $pages ); |
26
|
|
|
$this->updateAdminList( $this->getGroupOutcomes( 'sysop', $pages ) ); |
27
|
|
|
$checkUsers = $this->getGroupOutcomes( 'checkuser', $pages ); |
28
|
|
|
if ( $checkUsers ) { |
29
|
|
|
$this->updateCUList( $checkUsers ); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return TaskResult::STATUS_GOOD; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param PageRiconferma[] $pages |
37
|
|
|
* @see OpenUpdates::addToVotazioni() |
38
|
|
|
*/ |
39
|
|
|
protected function updateVotazioni( array $pages ): void { |
40
|
|
|
$this->getLogger()->info( |
41
|
|
|
'Updating votazioni: ' . implode( ', ', $pages ) |
42
|
|
|
); |
43
|
|
|
$votePage = $this->getPage( $this->getOpt( 'vote-page-title' ) ); |
44
|
|
|
|
45
|
|
|
$users = []; |
46
|
|
|
foreach ( $pages as $page ) { |
47
|
|
|
$users[] = $this->getUser( $page->getUserName() ); |
48
|
|
|
} |
49
|
|
|
$usersReg = RegexUtils::regexFromArray( '!', ...$users ); |
50
|
|
|
|
51
|
|
|
$search = "!^.+\{\{[^|}]*\/Riga\|[^|]*riconferma[^|]*\|utente=$usersReg\|.+\n!m"; |
52
|
|
|
|
53
|
|
|
$newContent = preg_replace( $search, '', $votePage->getContent() ); |
54
|
|
|
|
55
|
|
|
$summary = $this->msg( 'close-vote-page-summary' ) |
56
|
|
|
->params( [ '$num' => count( $pages ) ] )->text(); |
57
|
|
|
|
58
|
|
|
$votePage->edit( [ |
59
|
|
|
'text' => $newContent, |
60
|
|
|
'summary' => $summary |
61
|
|
|
] ); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param PageRiconferma[] $pages |
66
|
|
|
* @see OpenUpdates::addToNews() |
67
|
|
|
*/ |
68
|
|
|
protected function updateNews( array $pages ): void { |
69
|
|
|
$simpleAmount = $voteAmount = 0; |
70
|
|
|
foreach ( $pages as $page ) { |
71
|
|
|
if ( $page->isVote() ) { |
72
|
|
|
$voteAmount++; |
73
|
|
|
} else { |
74
|
|
|
$simpleAmount++; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->getLogger()->info( "Updating news counter: -$simpleAmount simple, -$voteAmount votes." ); |
79
|
|
|
|
80
|
|
|
$newsPage = $this->getPage( $this->getOpt( 'news-page-title' ) ); |
81
|
|
|
|
82
|
|
|
$simpleReg = '!(\| *riconferme[ _]tacite[ _]amministratori *= *)(\d*)(?=\s*[}|])!'; |
83
|
|
|
$voteReg = '!(\| *riconferme[ _]voto[ _]amministratori *= *)(\d*)(?=\s*[}|])!'; |
84
|
|
|
|
85
|
|
|
$simpleMatches = $newsPage->getMatch( $simpleReg ); |
86
|
|
|
$voteMatches = $newsPage->getMatch( $voteReg ); |
87
|
|
|
|
88
|
|
|
$newSimp = ( (int)$simpleMatches[2] - $simpleAmount ) ?: ''; |
89
|
|
|
$newVote = ( (int)$voteMatches[2] - $voteAmount ) ?: ''; |
90
|
|
|
$newContent = preg_replace( $simpleReg, '${1}' . $newSimp, $newsPage->getContent() ); |
91
|
|
|
$newContent = preg_replace( $voteReg, '${1}' . $newVote, $newContent ); |
92
|
|
|
|
93
|
|
|
$summary = $this->msg( 'close-news-page-summary' ) |
94
|
|
|
->params( [ '$num' => count( $pages ) ] )->text(); |
95
|
|
|
|
96
|
|
|
$newsPage->edit( [ |
97
|
|
|
'text' => $newContent, |
98
|
|
|
'summary' => $summary |
99
|
|
|
] ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Update date on WP:Amministratori/Lista |
104
|
|
|
* |
105
|
|
|
* @param bool[] $outcomes |
106
|
|
|
*/ |
107
|
|
|
protected function updateAdminList( array $outcomes ): void { |
108
|
|
|
$this->getLogger()->info( 'Updating admin list' ); |
109
|
|
|
$adminsPage = $this->getPage( $this->getOpt( 'admins-list-title' ) ); |
110
|
|
|
$newContent = $adminsPage->getContent(); |
111
|
|
|
|
112
|
|
|
$riconfNames = $removeNames = []; |
113
|
|
|
foreach ( $outcomes as $username => $confirmed ) { |
114
|
|
|
$user = $this->getUser( $username ); |
115
|
|
|
$userReg = $user->getRegex( '!' ); |
116
|
|
|
$reg = "!({{Ammini\w+\/riga\|$userReg\|\D+\|\d{8}\|)(?:\d{8})?\|\d{8}((?:\|[a-z]*)?}}.*\n)!"; |
117
|
|
|
if ( $confirmed ) { |
118
|
|
|
$nextDate = date( |
119
|
|
|
'Ymd', |
120
|
|
|
$this->getBotList()->getNextTimestamp( $user->getName() ) |
121
|
|
|
); |
122
|
|
|
$newContent = preg_replace( |
123
|
|
|
$reg, |
124
|
|
|
'${1}{{subst:#timel:Ymd}}|' . $nextDate . '$2', |
125
|
|
|
$newContent |
126
|
|
|
); |
127
|
|
|
$riconfNames[] = $username; |
128
|
|
|
} else { |
129
|
|
|
$newContent = preg_replace( $reg, '', $newContent ); |
130
|
|
|
$removeNames[] = $username; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$summary = ''; |
135
|
|
|
if ( $riconfNames ) { |
136
|
|
|
$summary .= $this->msg( 'close-update-list-summary-riconf' ) |
137
|
|
|
->params( [ '$riconf' => Message::commaList( $riconfNames ) ] ) |
138
|
|
|
->text(); |
139
|
|
|
$summary .= ' '; |
140
|
|
|
} |
141
|
|
|
if ( $removeNames ) { |
142
|
|
|
$summary .= $this->msg( 'close-update-list-summary-remove' ) |
143
|
|
|
->params( [ '$remove' => Message::commaList( $removeNames ) ] ) |
144
|
|
|
->text(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$adminsPage->edit( [ |
148
|
|
|
'text' => $newContent, |
149
|
|
|
'summary' => rtrim( $summary ) |
150
|
|
|
] ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param bool[] $outcomes |
155
|
|
|
*/ |
156
|
|
|
protected function updateCUList( array $outcomes ): void { |
157
|
|
|
$this->getLogger()->info( 'Updating CU list.' ); |
158
|
|
|
$cuList = $this->getPage( $this->getOpt( 'cu-list-title' ) ); |
159
|
|
|
$newContent = $cuList->getContent(); |
160
|
|
|
|
161
|
|
|
$riconfNames = $removeNames = []; |
162
|
|
|
foreach ( $outcomes as $user => $confirmed ) { |
163
|
|
|
$userReg = $this->getUser( $user )->getRegex( '!' ); |
164
|
|
|
$reg = "!(\{\{ *Checkuser *\| *$userReg *\|[^}]+\| *)[\w \d]+(}}.*\n)!"; |
165
|
|
|
if ( $confirmed ) { |
166
|
|
|
$newContent = preg_replace( $reg, '${1}{{subst:#time:j F Y}}$2', $newContent ); |
167
|
|
|
$riconfNames[] = $user; |
168
|
|
|
} else { |
169
|
|
|
$newContent = preg_replace( $reg, '', $newContent ); |
170
|
|
|
$removeNames[] = $user; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$summary = ''; |
175
|
|
|
if ( $riconfNames ) { |
176
|
|
|
$summary .= $this->msg( 'cu-list-update-summary-riconf' ) |
177
|
|
|
->params( [ '$riconf' => Message::commaList( $riconfNames ) ] ) |
178
|
|
|
->text(); |
179
|
|
|
$summary .= ' '; |
180
|
|
|
} |
181
|
|
|
if ( $removeNames ) { |
182
|
|
|
$summary .= $this->msg( 'cu-list-update-summary-remove' ) |
183
|
|
|
->params( [ '$remove' => Message::commaList( $removeNames ) ] ) |
184
|
|
|
->text(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$cuList->edit( [ |
188
|
|
|
'text' => $newContent, |
189
|
|
|
'summary' => rtrim( $summary ) |
190
|
|
|
] ); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Given a user group and an array of PageRiconferma, get an array of users from $pages |
195
|
|
|
* which are in the given groups and the outcome of the procedure (true = confirmed) |
196
|
|
|
* @param string $group |
197
|
|
|
* @param PageRiconferma[] $pages |
198
|
|
|
* @return bool[] |
199
|
|
|
*/ |
200
|
|
|
private function getGroupOutcomes( string $group, array $pages ): array { |
201
|
|
|
$ret = []; |
202
|
|
|
foreach ( $pages as $page ) { |
203
|
|
|
$user = $this->getUser( $page->getUserName() ); |
204
|
|
|
if ( $user->inGroup( $group ) ) { |
205
|
|
|
$ret[ $user->getName() ] = !( $page->getOutcome() & PageRiconferma::OUTCOME_FAIL ); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
return $ret; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|