Passed
Push — master ( 9edbb3...aa10e3 )
by Daimona
02:01
created

FailedUpdates::updateUltimeNotizie()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 29
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 22
c 2
b 0
f 0
nc 4
nop 1
dl 0
loc 29
rs 9.568
1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme\Task\Subtask;
4
5
use BotRiconferme\Message\Message;
6
use BotRiconferme\TaskHelper\TaskResult;
7
use BotRiconferme\Utils\RegexUtils;
8
use BotRiconferme\Wiki\Page\Page;
9
use BotRiconferme\Wiki\Page\PageRiconferma;
10
use BotRiconferme\Wiki\User;
11
12
/**
13
 * Update various pages around, to be done for all failed procedures
14
 */
15
class FailedUpdates extends Subtask {
16
	/**
17
	 * @inheritDoc
18
	 */
19
	public function runInternal() : int {
20
		$failed = $this->getFailures();
21
		if ( !$failed ) {
22
			return TaskResult::STATUS_NOTHING;
23
		}
24
25
		$bureaucrats = $this->getFailedBureaucrats( $failed );
26
		if ( $bureaucrats ) {
27
			$this->updateBurList( $bureaucrats );
28
		}
29
		$this->requestRemoval( $failed );
30
		$this->updateAnnunci( $failed );
31
		$this->updateUltimeNotizie( $failed );
32
		$this->updateTimeline( $failed );
33
		$this->updateCronologia( $failed );
34
		$this->blockOnPrivate( $failed );
35
36
		return TaskResult::STATUS_GOOD;
37
	}
38
39
	/**
40
	 * Get the list of failed votes
41
	 *
42
	 * @return PageRiconferma[]
43
	 */
44
	private function getFailures() : array {
45
		$ret = [];
46
		$allPages = $this->getDataProvider()->getPagesToClose();
47
		foreach ( $allPages as $page ) {
48
			if ( $page->getOutcome() & PageRiconferma::OUTCOME_FAIL ) {
49
				$ret[] = $page;
50
			}
51
		}
52
		return $ret;
53
	}
54
55
	/**
56
	 * @param User[] $users
57
	 */
58
	protected function updateBurList( array $users ) : void {
59
		$this->getLogger()->info( 'Updating bur list. Removing: ' . implode( ', ', $users ) );
60
		$remList = RegexUtils::regexFromArray( '!', ...$users );
61
		$burList = $this->getPage( $this->getOpt( 'bur-list-title' ) );
62
		$content = $burList->getContent();
63
		$reg = "!^\#\{\{ *Burocrate *\| *$remList.+\n!m";
64
		$newContent = preg_replace( $reg, '', $content );
65
66
		$summary = $this->msg( 'bur-list-update-summary' )
67
			->params( [ '$remove' => Message::commaList( $users ) ] )
68
			->text();
69
70
		$burList->edit( [
71
			'text' => $newContent,
72
			'summary' => $summary
73
		] );
74
	}
75
76
	/**
77
	 * Request the removal of the flag on meta
78
	 *
79
	 * @param PageRiconferma[] $pages
80
	 */
81
	protected function requestRemoval( array $pages ) : void {
82
		$this->getLogger()->info( 'Requesting flag removal for: ' . implode( ', ', $pages ) );
83
84
		$metaWiki = $this->getWikiGroup()->getCentralWiki();
85
		$flagRemPage = new Page(
86
			$this->getOpt( 'flag-removal-page-title' ),
87
			$metaWiki
88
		);
89
		$baseText = $this->msg( 'flag-removal-text' );
90
91
		$append = '';
92
		foreach ( $pages as $page ) {
93
			$append .=
94
				$baseText->params( [
95
					'$username' => $page->getUserName(),
96
					'$link' => '[[:it:' . $page->getTitle() . ']]',
97
					'$groups' => implode( ', ', $this->getUser( $page->getUserName() )->getGroups() )
98
				] )->text();
99
		}
100
101
		$after = '=== Miscellaneous requests ===';
102
		$newContent = str_replace( $after, "$append\n$after", $flagRemPage->getContent() );
103
		$summary = $this->msg( 'flag-removal-summary' )
104
			->params( [ '$num' => count( $pages ) ] )
105
			->text();
106
107
		$flagRemPage->edit( [
108
			'text' => $newContent,
109
			'summary' => $summary
110
		] );
111
	}
112
113
	/**
114
	 * Update [[Wikipedia:Wikipediano/Annunci]]
115
	 *
116
	 * @param PageRiconferma[] $pages
117
	 */
118
	protected function updateAnnunci( array $pages ) : void {
119
		$this->getLogger()->info( 'Updating annunci' );
120
		$section = 1;
121
122
		$names = [];
123
		$text = '';
124
		foreach ( $pages as $page ) {
125
			$user = $page->getUserName();
126
			$names[] = $user;
127
			$text .= $this->msg( 'annunci-text' )->params( [ '$user' => $user ] )->text();
128
		}
129
130
		$month = ucfirst( Message::MONTHS[ date( 'F' ) ] );
131
132
		$annunciPage = $this->getPage( $this->getOpt( 'annunci-page-title' ) );
133
		$content = $annunciPage->getContent( $section );
134
		$secReg = "!=== *$month *===!";
135
		if ( $annunciPage->matches( $secReg ) ) {
136
			$newContent = preg_replace( $secReg, '$0' . "\n" . $text, $content );
137
		} else {
138
			$before = '!</div>\s*}}\s*</includeonly>!';
139
			$newContent = preg_replace( $before, '$0' . "\n=== $month ===\n" . $text, $content );
140
		}
141
142
		$summary = $this->msg( 'annunci-summary' )
143
			->params( [ '$names' => Message::commaList( $names ) ] )
144
			->text();
145
146
		$annunciPage->edit( [
147
			'section' => $section,
148
			'text' => $newContent,
149
			'summary' => $summary
150
		] );
151
	}
152
153
	/**
154
	 * Update [[Wikipedia:Ultime notizie]]
155
	 *
156
	 * @param PageRiconferma[] $pages
157
	 */
158
	protected function updateUltimeNotizie( array $pages ) : void {
159
		$this->getLogger()->info( 'Updating ultime notizie' );
160
		$notiziePage = $this->getPage( $this->getOpt( 'ultimenotizie-page-title' ) );
161
162
		$names = [];
163
		$text = '';
164
		$msg = $this->msg( 'ultimenotizie-text' );
165
		foreach ( $pages as $page ) {
166
			$user = $page->getUserName();
167
			$names[] = $user;
168
			$text .= $msg->params( [ '$user' => $user, '$title' => $page->getTitle() ] )->text();
169
		}
170
171
		$content = $notiziePage->getContent();
172
		$year = date( 'Y' );
173
		$secReg = "!== *$year *==!";
174
		if ( preg_match( $secReg, $content ) ) {
175
			$newContent = preg_replace( $secReg, '$0' . "\n" . $text, $content );
176
		} else {
177
			$reg = '!si veda la \[\[[^\]+relativa discussione]]\.\n!';
178
			$newContent = preg_replace( $reg, '$0' . "\n== $year ==\n" . $text, $content );
179
		}
180
181
		$summary = $this->msg( 'ultimenotizie-summary' )
182
			->params( [ '$names' => Message::commaList( $names ) ] )->text();
183
184
		$notiziePage->edit( [
185
			'text' => $newContent,
186
			'summary' => $summary
187
		] );
188
	}
189
190
	/**
191
	 * Update [[Wikipedia:Amministratori/Timeline]]
192
	 *
193
	 * @param PageRiconferma[] $pages
194
	 */
195
	protected function updateTimeline( array $pages ) : void {
196
		$this->getLogger()->info( 'Updating timeline' );
197
		$timelinePage = $this->getPage( $this->getOpt( 'timeline-page-title' ) );
198
		$content = $timelinePage->getContent();
199
200
		$today = date( 'm/d/Y' );
201
		foreach ( $pages as $page ) {
202
			$name = $page->getUserName();
203
			$content = preg_replace(
204
				"/(?<=color:)current( *from:\d+/\d+/\d+ till:)end(?= text:\"\[\[(User|Utente):$name|$name]]\")/",
205
				'nonriconf$1' . $today,
206
				$content
207
			);
208
		}
209
210
		$summary = $this->msg( 'timeline-summary' )->text();
211
212
		$timelinePage->edit( [
213
			'text' => $content,
214
			'summary' => $summary
215
		] );
216
	}
217
218
	/**
219
	 * Update [[Wikipedia:Amministratori/Cronologia]]
220
	 *
221
	 * @param PageRiconferma[] $pages
222
	 */
223
	private function updateCronologia( array $pages ) : void {
224
		$this->getLogger()->info( 'Updating cronologia' );
225
		$timelinePage = $this->getPage( $this->getOpt( 'cronologia-page-title' ) );
226
		$content = $timelinePage->getContent();
227
228
		foreach ( $pages as $page ) {
229
			$name = $page->getUserName();
230
			$content = preg_replace(
231
				"/(\* *)'''(\[\[(Utente|User):$name|$name]])'''( <small>dal \d+ \w+ \d+)(</small>)/",
232
				'$1$2$3' . " al {{subst:#timel:j F Y}} (non riconfermato)" . '$4',
233
				$content
234
			);
235
		}
236
237
		$summary = $this->msg( 'cronologia-summary' )->text();
238
239
		$timelinePage->edit( [
240
			'text' => $content,
241
			'summary' => $summary
242
		] );
243
	}
244
245
	/**
246
	 * Block the user on the private wiki
247
	 *
248
	 * @param PageRiconferma[] $pages
249
	 */
250
	private function blockOnPrivate( array $pages ) : void {
251
		$this->getLogger()->info( 'Blocking on private wiki: ' . implode( ', ', $pages ) );
252
253
		$privWiki = $this->getWikiGroup()->getPrivateWiki();
254
		$reason = $this->msg( 'private-block-reason' )->text();
255
256
		foreach ( $pages as $page ) {
257
			$privWiki->blockUser( $page->getUserName(), $reason );
258
		}
259
	}
260
261
	/**
262
	 * Get a list of bureaucrats from the given $pages
263
	 *
264
	 * @param PageRiconferma[] $pages
265
	 * @return User[]
266
	 */
267
	private function getFailedBureaucrats( array $pages ) : array {
268
		$ret = [];
269
		foreach ( $pages as $page ) {
270
			$user = $this->getUser( $page->getUserName() );
271
			if ( $user->inGroup( 'bureaucrat' ) ) {
272
				$ret[] = $user;
273
			}
274
		}
275
		return $ret;
276
	}
277
}
278