Passed
Branch master (a4e902)
by Daimona
01:39
created

SimpleUpdates::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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