1
|
|
|
<?php declare( strict_types=1 ); |
2
|
|
|
|
3
|
|
|
namespace BotRiconferme\Task; |
4
|
|
|
|
5
|
|
|
use BotRiconferme\Exception\TaskException; |
6
|
|
|
use BotRiconferme\Wiki\Element; |
7
|
|
|
use BotRiconferme\Wiki\Page\Page; |
8
|
|
|
use BotRiconferme\Wiki\Page\PageRiconferma; |
9
|
|
|
use BotRiconferme\TaskResult; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Start a vote if there are >= PageRiconferma::REQUIRED_OPPOSE opposing comments |
13
|
|
|
*/ |
14
|
|
|
class StartVote extends Task { |
15
|
|
|
/** |
16
|
|
|
* @inheritDoc |
17
|
|
|
*/ |
18
|
|
|
protected function getSubtasksMap(): array { |
19
|
|
|
// Everything is done here. |
20
|
|
|
return []; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @inheritDoc |
25
|
|
|
*/ |
26
|
|
|
public function runInternal() : int { |
27
|
|
|
$pages = $this->getDataProvider()->getOpenPages(); |
28
|
|
|
|
29
|
|
|
if ( !$pages ) { |
30
|
|
|
return TaskResult::STATUS_NOTHING; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $this->processPages( $pages ); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param PageRiconferma[] $pages |
38
|
|
|
* @return int a STATUS_* constant |
39
|
|
|
*/ |
40
|
|
|
protected function processPages( array $pages ) : int { |
41
|
|
|
$actualPages = []; |
42
|
|
|
foreach ( $pages as $page ) { |
43
|
|
|
if ( $page->hasOpposition() && !$page->isVote() ) { |
44
|
|
|
$this->openVote( $page ); |
45
|
|
|
$actualPages[] = $page; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if ( $actualPages ) { |
50
|
|
|
$this->updateVotazioni( $actualPages ); |
51
|
|
|
$this->updateNews( count( $actualPages ) ); |
52
|
|
|
return TaskResult::STATUS_GOOD; |
53
|
|
|
} else { |
54
|
|
|
return TaskResult::STATUS_NOTHING; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Start the vote for the given page |
60
|
|
|
* |
61
|
|
|
* @param PageRiconferma $page |
62
|
|
|
*/ |
63
|
|
|
protected function openVote( PageRiconferma $page ) { |
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 UpdatesAround::addToVotazioni() |
101
|
|
|
*/ |
102
|
|
|
protected function updateVotazioni( array $pages ) { |
103
|
|
|
$votePage = new Page( $this->getConfig()->get( 'vote-page-title' ) ); |
104
|
|
|
|
105
|
|
|
$users = []; |
106
|
|
|
foreach ( $pages as $page ) { |
107
|
|
|
$users[] = $page->getUser(); |
108
|
|
|
} |
109
|
|
|
$usersReg = Element::regexFromArray( $users ); |
110
|
|
|
|
111
|
|
|
$search = "!^.+\{\{Wikipedia:Wikipediano\/Votazioni\/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|utente=' . |
119
|
|
|
$page->getUser() . '|numero=' . $page->getNum() . "|giorno={{subst:#timel:j F|+ $endDays days}}" . |
120
|
|
|
"|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
|
|
|
* Template:VotazioniRCnews |
139
|
|
|
* |
140
|
|
|
* @param int $amount Of pages to move |
141
|
|
|
* @see UpdatesAround::addToNews() |
142
|
|
|
* @see SimpleUpdates::updateNews() |
143
|
|
|
* @throws TaskException |
144
|
|
|
*/ |
145
|
|
|
protected function updateNews( int $amount ) { |
146
|
|
|
$this->getLogger()->info( "Turning $amount pages into votes" ); |
147
|
|
|
$newsPage = new Page( $this->getConfig()->get( 'news-page-title' ) ); |
148
|
|
|
|
149
|
|
|
$content = $newsPage->getContent(); |
150
|
|
|
$regTac = '!(\| *riconferme[ _]tacite[ _]amministratori *= *)(\d*)(?=\s*[}|])!'; |
151
|
|
|
$regVot = '!(\| *riconferme[ _]voto[ _]amministratori *= *)(\d*)(?=\s*[}|])!'; |
152
|
|
|
|
153
|
|
|
if ( !$newsPage->matches( $regTac ) ) { |
154
|
|
|
throw new TaskException( 'Param "tacite" not found in news page' ); |
155
|
|
|
} |
156
|
|
|
if ( !$newsPage->matches( $regVot ) ) { |
157
|
|
|
throw new TaskException( 'Param "voto" not found in news page' ); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$newTac = intval( $newsPage->getMatch( $regTac )[2] ) - $amount ?: ''; |
161
|
|
|
$newVot = intval( $newsPage->getMatch( $regVot )[2] ) + $amount ?: ''; |
162
|
|
|
|
163
|
|
|
$newContent = preg_replace( $regTac, '${1}' . $newTac, $content ); |
164
|
|
|
$newContent = preg_replace( $regVot, '${1}' . $newVot, $newContent ); |
165
|
|
|
|
166
|
|
|
$summary = $this->msg( 'vote-start-news-page-summary' ) |
167
|
|
|
->params( [ '$num' => $amount ] ) |
168
|
|
|
->text(); |
169
|
|
|
|
170
|
|
|
$params = [ |
171
|
|
|
'text' => $newContent, |
172
|
|
|
'summary' => $summary |
173
|
|
|
]; |
174
|
|
|
|
175
|
|
|
$newsPage->edit( $params ); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|