Passed
Push — master ( cfeed2...9edbb3 )
by Daimona
01:56
created

FailedUpdates::updateCronologia()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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