Passed
Push — master ( 1a47f4...ba2710 )
by Daimona
01:58
created

SimpleUpdates::updateVotazioni()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 18
nc 2
nop 1
dl 0
loc 30
rs 9.6666
c 0
b 0
f 0
1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme\Task\Subtask;
4
5
use BotRiconferme\Message;
6
use BotRiconferme\Page\Page;
7
use BotRiconferme\Page\PageRiconferma;
8
use BotRiconferme\TaskResult;
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( $pages );
27
		$this->updateCUList( $pages );
28
29
		return TaskResult::STATUS_GOOD;
30
	}
31
32
	/**
33
	 * @param PageRiconferma[] $pages
34
	 * @see UpdatesAround::addToVotazioni()
35
	 */
36
	protected function updateVotazioni( array $pages ) {
37
		$this->getLogger()->info(
38
			'Updating votazioni: ' . implode( ', ', array_map( 'strval', $pages ) )
39
		);
40
		$votePage = new Page( $this->getConfig()->get( 'vote-page-title' ) );
41
42
		$titles = [];
43
		foreach ( $pages as $page ) {
44
			$titles[] = preg_quote( $page->getTitle() );
45
		}
46
47
		$titleReg = implode( '|', $titles );
48
		$search = "!^\*.+ La \[\[($titleReg)\|procedura]] termina.+\n!m";
49
50
		$newContent = preg_replace( $search, '', $votePage->getContent() );
51
		// Make sure the last line ends with a full stop in every section
52
		$simpleSectReg = '!(^;È in corso.+riconferma tacita.+amministrat.+\n(?:\*.+[;\.]\n)+\*.+)[\.;]!m';
53
		$voteSectReg = '!(^;Si vota per la .+riconferma .+amministratori.+\n(?:\*.+[;\.]\n)+\*.+)[\.;]!m';
54
		$newContent = preg_replace( $simpleSectReg, '$1.', $newContent );
55
		$newContent = preg_replace( $voteSectReg, '$1.', $newContent );
56
57
		// @fixme Remove empty sections, and add the "''Nessuna riconferma o votazione in corso''" message
58
		// if the page is empty! Or just wait for the page to be restyled...
59
60
		$summary = $this->msg( 'close-vote-page-summary' )
61
			->params( [ '$num' => count( $pages ) ] )->text();
62
63
		$votePage->edit( [
64
			'text' => $newContent,
65
			'summary' => $summary
66
		] );
67
	}
68
69
	/**
70
	 * @param array $pages
71
	 * @see UpdatesAround::addToNews()
72
	 */
73
	protected function updateNews( array $pages ) {
74
		$simpleAmount = $voteAmount = 0;
75
		foreach ( $pages as $page ) {
76
			if ( $page->isVote() ) {
77
				$voteAmount++;
78
			} else {
79
				$simpleAmount++;
80
			}
81
		}
82
83
		$this->getLogger()->info(
84
			"Decreasing the news counter: $simpleAmount simple, $voteAmount votes."
85
		);
86
87
		$newsPage = new Page( $this->getConfig()->get( 'news-page-title' ) );
88
89
		$content = $newsPage->getContent();
90
		$simpleReg = '!(\| *riconferme[ _]tacite[ _]amministratori *= *)(\d+)!';
91
		$voteReg = '!(\| *riconferme[ _]voto[ _]amministratori *= *)(\d+)!';
92
93
		$simpleMatches = $newsPage->getMatch( $simpleReg );
94
		$voteMatches = $newsPage->getMatch( $voteReg );
95
96
		$newSimp = (int)$simpleMatches[2] - $simpleAmount ?: '';
97
		$newVote = (int)$voteMatches[2] - $voteAmount ?: '';
98
		$newContent = preg_replace( $simpleReg, '${1}' . $newSimp, $content );
99
		$newContent = preg_replace( $voteReg, '${1}' . $newVote, $newContent );
100
101
		$summary = $this->msg( 'close-news-page-summary' )
102
			->params( [ '$num' => count( $pages ) ] )->text();
103
104
		$newsPage->edit( [
105
			'text' => $newContent,
106
			'summary' => $summary
107
		] );
108
	}
109
110
	/**
111
	 * Update date on WP:Amministratori/Lista
112
	 *
113
	 * @param PageRiconferma[] $pages
114
	 */
115
	protected function updateAdminList( array $pages ) {
116
		$this->getLogger()->info(
117
			'Updating admin list: ' . implode( ', ', array_map( 'strval', $pages ) )
118
		);
119
		$adminsPage = new Page( $this->getConfig()->get( 'admins-list-title' ) );
120
		$newContent = $adminsPage->getContent();
121
		$newDate = date( 'Ymd', strtotime( '+1 year' ) );
122
123
		$riconfNames = $removeNames = [];
124
		foreach ( $pages as $page ) {
125
			$user = $page->getUser();
126
			$reg = "!(\{\{Amministratore\/riga\|$user.+\| *)\d+( *\|(?: *pausa)? *\}\}\n)!";
127
			if ( $page->getOutcome() & PageRiconferma::OUTCOME_FAIL ) {
128
				// Remove the line
129
				$newContent = preg_replace( $reg, '', $newContent );
130
				$removeNames[] = $user;
131
			} else {
132
				$newContent = preg_replace( $reg, '$1' . $newDate . '$2', $newContent );
133
				$riconfNames[] = $user;
134
			}
135
		}
136
137
		$summary = $this->msg( 'close-update-list-summary' )
138
			->params( [
139
				'$riconf' => Message::commaList( $riconfNames ),
140
				'$remove' => Message::commaList( $removeNames )
141
			] )
142
			->text();
143
144
		$adminsPage->edit( [
145
			'text' => $newContent,
146
			'summary' => $summary
147
		] );
148
	}
149
150
	/**
151
	 * @param PageRiconferma[] $pages
152
	 */
153
	protected function updateCUList( array $pages ) {
154
		$this->getLogger()->info( 'Checking if CU list needs updating.' );
155
		$cuList = new Page( $this->getConfig()->get( 'cu-list-title' ) );
156
		$admins = $this->getDataProvider()->getUsersList();
157
		$newContent = $cuList->getContent();
158
159
		$riconfNames = $removeNames = [];
160
		foreach ( $pages as $page ) {
161
			$user = $page->getUser();
162
			if ( array_key_exists( 'checkuser', $admins[ $user ] ) ) {
163
				$reg = "!(\{\{ *Checkuser *\| *$user *\|[^}]+\| *)[\w \d](}}.*\n)!";
164
				if ( $page->getOutcome() & PageRiconferma::OUTCOME_FAIL ) {
165
					// Remove the line
166
					$newContent = preg_replace( $reg, '', $newContent );
167
					$removeNames[] = $user;
168
				} else {
169
					$newContent = preg_replace( $reg, '$1{{subst:#time:j F Y}}$2', $newContent );
170
					$riconfNames[] = $user;
171
				}
172
			}
173
		}
174
175
		if ( !$riconfNames || !$removeNames ) {
0 ignored issues
show
introduced by
$riconfNames is of type array|string[], thus it always evaluated to false.
Loading history...
176
			return;
177
		}
178
179
		$this->getLogger()->info(
180
			'Updating CU list. Riconf: ' . implode( ', ', $riconfNames ) .
181
			'; remove: ' . implode( ', ', $removeNames )
182
		);
183
184
		$summary = $this->msg( 'cu-list-update-summary' )
185
			->params( [
186
				'$riconf' => Message::commaList( $riconfNames ),
187
				'$remove' => Message::commaList( $removeNames )
188
			] )
189
			->text();
190
191
		$cuList->edit( [
192
			'text' => $newContent,
193
			'summary' => $summary
194
		] );
195
	}
196
}
197