1
|
|
|
<?php declare( strict_types=1 ); |
2
|
|
|
|
3
|
|
|
namespace BotRiconferme\Task; |
4
|
|
|
|
5
|
|
|
use BotRiconferme\Exception\TaskException; |
6
|
|
|
use BotRiconferme\TaskHelper\TaskResult; |
7
|
|
|
use BotRiconferme\Utils\RegexUtils; |
8
|
|
|
use BotRiconferme\Wiki\Page\PageRiconferma; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Start a vote if there are >= PageRiconferma::REQUIRED_OPPOSE opposing comments |
12
|
|
|
*/ |
13
|
|
|
class StartVote extends Task { |
14
|
|
|
/** |
15
|
|
|
* @inheritDoc |
16
|
|
|
*/ |
17
|
|
|
protected function getSubtasksMap(): array { |
18
|
|
|
// Everything is done here. |
19
|
|
|
return []; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @inheritDoc |
24
|
|
|
*/ |
25
|
|
|
public function runInternal(): int { |
26
|
|
|
$pages = $this->getDataProvider()->getOpenPages(); |
27
|
|
|
|
28
|
|
|
if ( !$pages ) { |
29
|
|
|
return TaskResult::STATUS_NOTHING; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return $this->processPages( $pages ); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param PageRiconferma[] $pages |
37
|
|
|
* @return int a STATUS_* constant |
38
|
|
|
*/ |
39
|
|
|
protected function processPages( array $pages ): int { |
40
|
|
|
$donePages = []; |
41
|
|
|
foreach ( $pages as $page ) { |
42
|
|
|
if ( $page->hasOpposition() && !$page->isVote() ) { |
43
|
|
|
$this->openVote( $page ); |
44
|
|
|
$this->updateBasePage( $page ); |
45
|
|
|
$donePages[] = $page; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if ( !$donePages ) { |
50
|
|
|
return TaskResult::STATUS_NOTHING; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->updateVotazioni( $donePages ); |
54
|
|
|
$this->updateNews( count( $donePages ) ); |
55
|
|
|
return TaskResult::STATUS_GOOD; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Start the vote for the given page |
60
|
|
|
* |
61
|
|
|
* @param PageRiconferma $page |
62
|
|
|
*/ |
63
|
|
|
protected function openVote( PageRiconferma $page ): void { |
64
|
|
|
$this->getLogger()->info( "Starting vote on $page" ); |
65
|
|
|
|
66
|
|
|
$content = $page->getContent(); |
67
|
|
|
|
68
|
|
|
$newContent = preg_replace( |
69
|
|
|
'!^La procedura di riconferma tacita .+!m', |
70
|
|
|
'<del>$0</del>', |
71
|
|
|
$content |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
$newContent = preg_replace( |
75
|
|
|
'/<!-- SEZIONE DA UTILIZZARE PER L\'EVENTUALE VOTAZIONE DI RICONFERMA.*\n/', |
76
|
|
|
'', |
77
|
|
|
$newContent |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
$newContent = preg_replace( |
81
|
|
|
'!(==== *Favorevoli alla riconferma *====\n#[\s.]+|maggioranza di.+ dei votanti\.)\n-->!', |
82
|
|
|
'$1', |
83
|
|
|
$newContent, |
84
|
|
|
2 |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
$params = [ |
88
|
|
|
'text' => $newContent, |
89
|
|
|
'summary' => $this->msg( 'vote-start-summary' )->text() |
90
|
|
|
]; |
91
|
|
|
|
92
|
|
|
$page->edit( $params ); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Update [[WP:Wikipediano/Votazioni]] |
97
|
|
|
* |
98
|
|
|
* @param PageRiconferma[] $pages |
99
|
|
|
* @see SimpleUpdates::updateVotazioni() |
100
|
|
|
* @see OpenUpdates::addToVotazioni() |
101
|
|
|
*/ |
102
|
|
|
protected function updateVotazioni( array $pages ): void { |
103
|
|
|
$votePage = $this->getPage( $this->getOpt( 'vote-page-title' ) ); |
104
|
|
|
|
105
|
|
|
$users = []; |
106
|
|
|
foreach ( $pages as $page ) { |
107
|
|
|
$users[] = $this->getUser( $page->getUserName() ); |
108
|
|
|
} |
109
|
|
|
$usersReg = RegexUtils::regexFromArray( '!', ...$users ); |
110
|
|
|
|
111
|
|
|
$search = "!^.+\{\{[^|}]*\/Riga\|riconferma tacita\|utente=$usersReg\|.+\n!m"; |
112
|
|
|
|
113
|
|
|
$newContent = preg_replace( $search, '', $votePage->getContent() ); |
114
|
|
|
|
115
|
|
|
$newLines = ''; |
116
|
|
|
$endDays = PageRiconferma::VOTE_DURATION; |
117
|
|
|
foreach ( $pages as $page ) { |
118
|
|
|
$newLines .= '{{subst:Wikipedia:Wikipediano/Votazioni/RigaCompleta|votazione riconferma' . |
119
|
|
|
'|utente=' . $page->getUserName() . '|numero=' . $page->getNum() . '|giorno=' . |
120
|
|
|
"{{subst:#timel:j F|+ $endDays days}}|ore={{subst:LOCALTIME}}}}\n"; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$newContent = preg_replace( '!\|votazioni[ _]riconferma *= *\n!', '$0' . $newLines, $newContent ); |
124
|
|
|
|
125
|
|
|
$summary = $this->msg( 'vote-start-vote-page-summary' ) |
126
|
|
|
->params( [ '$num' => count( $pages ) ] ) |
127
|
|
|
->text(); |
128
|
|
|
|
129
|
|
|
$params = [ |
130
|
|
|
'text' => $newContent, |
131
|
|
|
'summary' => $summary |
132
|
|
|
]; |
133
|
|
|
|
134
|
|
|
$votePage->edit( $params ); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param PageRiconferma $page |
139
|
|
|
* @see \BotRiconferme\Task\Subtask\ClosePages::updateBasePage() |
140
|
|
|
*/ |
141
|
|
|
protected function updateBasePage( PageRiconferma $page ): void { |
142
|
|
|
$this->getLogger()->info( "Updating base page for $page" ); |
143
|
|
|
|
144
|
|
|
if ( $page->getNum() === 1 ) { |
145
|
|
|
$basePage = $this->getUser( $page->getUserName() )->getBasePage(); |
146
|
|
|
} else { |
147
|
|
|
$basePage = $this->getUser( $page->getUserName() )->getExistingBasePage(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$current = $basePage->getContent(); |
151
|
|
|
|
152
|
|
|
$newContent = preg_replace( '/^(#: *)riconferma in corso/m', '$1votazione di riconferma in corso', $current ); |
153
|
|
|
|
154
|
|
|
$basePage->edit( [ |
155
|
|
|
'text' => $newContent, |
156
|
|
|
'summary' => $this->msg( 'close-base-page-summary-update' )->text() |
157
|
|
|
] ); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Template:VotazioniRCnews |
162
|
|
|
* |
163
|
|
|
* @param int $amount Of pages to move |
164
|
|
|
* @throws TaskException |
165
|
|
|
* @see SimpleUpdates::updateNews() |
166
|
|
|
* @see OpenUpdates::addToNews() |
167
|
|
|
*/ |
168
|
|
|
protected function updateNews( int $amount ): void { |
169
|
|
|
$this->getLogger()->info( "Turning $amount pages into votes" ); |
170
|
|
|
$newsPage = $this->getPage( $this->getOpt( 'news-page-title' ) ); |
171
|
|
|
|
172
|
|
|
$content = $newsPage->getContent(); |
173
|
|
|
$regTac = '!(\| *riconferme[ _]tacite[ _]amministratori *= *)(\d*)(?=\s*[}|])!'; |
174
|
|
|
$regVot = '!(\| *riconferme[ _]voto[ _]amministratori *= *)(\d*)(?=\s*[}|])!'; |
175
|
|
|
|
176
|
|
|
if ( !$newsPage->matches( $regTac ) ) { |
177
|
|
|
throw new TaskException( 'Param "tacite" not found in news page' ); |
178
|
|
|
} |
179
|
|
|
if ( !$newsPage->matches( $regVot ) ) { |
180
|
|
|
throw new TaskException( 'Param "voto" not found in news page' ); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$newTac = ( (int)$newsPage->getMatch( $regTac )[2] - $amount ) ?: ''; |
184
|
|
|
$newVot = ( (int)$newsPage->getMatch( $regVot )[2] + $amount ) ?: ''; |
185
|
|
|
|
186
|
|
|
$newContent = preg_replace( $regTac, '${1}' . $newTac, $content ); |
187
|
|
|
$newContent = preg_replace( $regVot, '${1}' . $newVot, $newContent ); |
188
|
|
|
|
189
|
|
|
$summary = $this->msg( 'vote-start-news-page-summary' ) |
190
|
|
|
->params( [ '$num' => $amount ] ) |
191
|
|
|
->text(); |
192
|
|
|
|
193
|
|
|
$params = [ |
194
|
|
|
'text' => $newContent, |
195
|
|
|
'summary' => $summary |
196
|
|
|
]; |
197
|
|
|
|
198
|
|
|
$newsPage->edit( $params ); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|