Passed
Push — master ( 42aaff...766431 )
by Daimona
01:42
created

SimpleUpdates::updateAdminList()   B

Complexity

Conditions 7
Paths 27

Size

Total Lines 53
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 38
nc 27
nop 1
dl 0
loc 53
rs 8.3786
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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