Passed
Push — master ( e4a382...56087c )
by Daimona
01:45
created

SimpleUpdates::updateCUList()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 21
nc 3
nop 1
dl 0
loc 28
rs 9.584
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\Wiki\Element;
7
use BotRiconferme\Wiki\Page\Page;
8
use BotRiconferme\Wiki\Page\PageRiconferma;
9
use BotRiconferme\TaskResult;
10
use BotRiconferme\Wiki\User;
11
12
/**
13
 * Update various pages around, to be done for all closed procedures
14
 */
15
class SimpleUpdates extends Subtask {
16
	/**
17
	 * @inheritDoc
18
	 */
19
	public function runInternal() : int {
20
		$pages = $this->getDataProvider()->getPagesToClose();
21
22
		if ( !$pages ) {
23
			return TaskResult::STATUS_NOTHING;
24
		}
25
26
		$this->updateVotazioni( $pages );
27
		$this->updateNews( $pages );
28
		$this->updateAdminList( $this->getDataProvider()->getGroupOutcomes( 'sysop', $pages ) );
29
		$checkUsers = $this->getDataProvider()->getGroupOutcomes( 'checkuser', $pages );
30
		if ( $checkUsers ) {
31
			$this->updateCUList( $checkUsers );
32
		}
33
34
		return TaskResult::STATUS_GOOD;
35
	}
36
37
	/**
38
	 * @param PageRiconferma[] $pages
39
	 * @see UpdatesAround::addToVotazioni()
40
	 */
41
	protected function updateVotazioni( array $pages ) {
42
		$this->getLogger()->info(
43
			'Updating votazioni: ' . implode( ', ', $pages )
44
		);
45
		$votePage = new Page( $this->getConfig()->get( 'vote-page-title' ) );
46
47
		$titleReg = Element::regexFromArray( $pages );
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*)(?=\s*[}|])!';
91
		$voteReg = '!(\| *riconferme[ _]voto[ _]amministratori *= *)(\d*)(?=\s*[}|])!';
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 bool[] $outcomes
114
	 */
115
	protected function updateAdminList( array $outcomes ) {
116
		$this->getLogger()->info( 'Updating admin list' );
117
118
		$adminsPage = new Page( $this->getConfig()->get( 'admins-list-title' ) );
119
		$newContent = $adminsPage->getContent();
120
121
		$riconfNames = $removeNames = [];
122
		foreach ( $outcomes as $user => $confirmed ) {
123
			$userReg = ( new User( $user ) )->getRegex();
124
			$reg = "!(\{\{Amministratore\/riga\|$userReg.+\| *)\d+( *\|[ \w]*\}\}.*\n)!";
125
			if ( $confirmed ) {
126
				$newContent = preg_replace( $reg, '${1}{{subst:#time:Ymd|+1 year}}$2', $newContent );
127
				$riconfNames[] = $user;
128
			} else {
129
				$newContent = preg_replace( $reg, '', $newContent );
130
				$removeNames[] = $user;
131
			}
132
		}
133
134
		$summary = $this->msg( 'close-update-list-summary' )
135
			->params( [
136
				'$riconf' => Message::commaList( $riconfNames ),
137
				'$remove' => Message::commaList( $removeNames )
138
			] )
139
			->text();
140
141
		$adminsPage->edit( [
142
			'text' => $newContent,
143
			'summary' => $summary
144
		] );
145
	}
146
147
	/**
148
	 * @param bool[] $outcomes
149
	 */
150
	protected function updateCUList( array $outcomes ) {
151
		$this->getLogger()->info( 'Updating CU list.' );
152
		$cuList = new Page( $this->getConfig()->get( 'cu-list-title' ) );
153
		$newContent = $cuList->getContent();
154
155
		$riconfNames = $removeNames = [];
156
		foreach ( $outcomes as $user => $confirmed ) {
157
			$userReg = ( new User( $user ) )->getRegex();
158
			$reg = "!(\{\{ *Checkuser *\| *$userReg *\|[^}]+\| *)[\w \d]+(}}.*\n)!";
159
			if ( $confirmed ) {
160
				$newContent = preg_replace( $reg, '$1{{subst:#time:j F Y}}$2', $newContent );
161
				$riconfNames[] = $user;
162
			} else {
163
				$newContent = preg_replace( $reg, '', $newContent );
164
				$removeNames[] = $user;
165
			}
166
		}
167
168
		$summary = $this->msg( 'cu-list-update-summary' )
169
			->params( [
170
				'$riconf' => Message::commaList( $riconfNames ),
171
				'$remove' => Message::commaList( $removeNames )
172
			] )
173
			->text();
174
175
		$cuList->edit( [
176
			'text' => $newContent,
177
			'summary' => $summary
178
		] );
179
	}
180
}
181