Passed
Push — master ( 2ef02f...fd3159 )
by Daimona
01:56
created

SimpleUpdates::updateAdminList()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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