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

FailedUpdates   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 218
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 129
dl 0
loc 218
rs 10
c 0
b 0
f 0
wmc 21

6 Methods

Rating   Name   Duplication   Size   Complexity  
A requestRemoval() 0 36 2
A run() 0 13 2
A getFailures() 0 9 3
B updateBurList() 0 41 6
A updateUltimeNotizie() 0 40 4
A updateAnnunci() 0 42 4
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 failed procedures
11
 */
12
class FailedUpdates extends Subtask {
13
	/**
14
	 * @inheritDoc
15
	 */
16
	public function run() : TaskResult {
17
		$this->getLogger()->info( 'Starting task FailedUpdates' );
18
19
		$failed = $this->getFailures();
20
		if ( $failed ) {
21
			$this->updateBurList( $failed );
22
			$this->requestRemoval( $failed );
23
			$this->updateAnnunci( $failed );
24
			$this->updateUltimeNotizie( $failed );
25
		}
26
27
		$this->getLogger()->info( 'Task FailedUpdates completed successfully' );
28
		return new TaskResult( self::STATUS_OK );
29
	}
30
31
	/**
32
	 * Get the list of failed votes
33
	 *
34
	 * @return PageRiconferma[]
35
	 */
36
	private function getFailures() : array {
37
		$ret = [];
38
		$allPages = $this->getDataProvider()->getPagesToClose();
39
		foreach ( $allPages as $page ) {
40
			if ( $page->getOutcome() & PageRiconferma::OUTCOME_FAIL ) {
41
				$ret[] = $page;
42
			}
43
		}
44
		return $ret;
45
	}
46
47
	/**
48
	 * @param PageRiconferma[] $pages
49
	 */
50
	protected function updateBurList( array $pages ) {
51
		$this->getLogger()->info( 'Checking if bur list needs updating.' );
52
		$admins = $this->getDataProvider()->getUsersList();
53
54
		$remove = [];
55
		foreach ( $pages as $page ) {
56
			$user = $page->getUser();
57
			if ( array_key_exists( 'bureaucrat', $admins[ $user ] ) &&
58
				$page->getOutcome() & PageRiconferma::OUTCOME_FAIL
59
			) {
60
				$remove[] = $user;
61
			}
62
		}
63
64
		if ( !$remove ) {
65
			return;
66
		}
67
68
		$this->getLogger()->info( 'Updating bur list. Removing: ' . implode( ', ', $remove ) );
69
		$remList = implode( '|', array_map( 'preg_quote', $remove ) );
70
		$burList = new Page( $this->getConfig()->get( 'bur-list-title' ) );
71
		$content = $burList->getContent();
72
		$reg = "!^\#\{\{ *Burocrate *\| *($remList).+\n!m";
73
		$newContent = preg_replace( $reg, '', $content );
74
75
		if ( count( $remove ) > 1 ) {
76
			$lastUser = array_pop( $remove );
77
			$removeList = implode( ', ', $remove ) . " e $lastUser";
78
		} else {
79
			$removeList = $remove[0];
80
		}
81
82
		$summary = $this->msg( 'bur-list-update-summary' )
83
			->params( [ '$remove' => $removeList ] )
84
			->text();
85
86
		$params = [
87
			'text' => $newContent,
88
			'summary' => $summary
89
		];
90
		$burList->edit( $params );
91
	}
92
93
	/**
94
	 * Request the removal of the flag on meta
95
	 *
96
	 * @param PageRiconferma[] $pages
97
	 */
98
	protected function requestRemoval( array $pages ) {
99
		$this->getLogger()->info(
100
			'Requesting removal on meta for: ' . implode( ', ', array_map( 'strval', $pages ) )
101
		);
102
		$admins = $this->getDataProvider()->getUsersList();
103
104
		$flagRemPage = new Page(
105
			$this->getConfig()->get( 'flag-removal-page' ),
106
			'https://meta.wikimedia.org/w/api.php'
107
		);
108
		$section = $this->getConfig()->get( 'flag-removal-section' );
109
		$baseText = $this->getConfig()->get( 'flag-removal-text' );
110
111
		$newContent = $flagRemPage->getContent( $section );
112
		foreach ( $pages as $page ) {
113
			$curText = strtr(
114
				$baseText,
115
				[
116
					'$username' => $page->getUser(),
117
					'$link' => '[[:it:' . $page->getTitle() . ']]',
118
					'$groups' => implode( ', ', array_keys( $admins[ $page->getUser() ] ) )
119
				]
120
			);
121
			$newContent .= $curText;
122
		}
123
124
		$summary = $this->msg( 'flag-removal-summary' )
125
			->params( [ '$num' => count( $pages ) ] )
126
			->text();
127
128
		$params = [
129
			'section' => $section,
130
			'text' => $newContent,
131
			'summary' => $summary
132
		];
133
		$flagRemPage->edit( $params );
134
	}
135
136
	/**
137
	 * Update [[Wikipedia:Wikipediano/Annunci]]
138
	 *
139
	 * @param PageRiconferma[] $pages
140
	 */
141
	protected function updateAnnunci( array $pages ) {
142
		$this->getLogger()->info( 'Updating annunci' );
143
144
		$names = [];
145
		$text = '';
146
		foreach ( $pages as $page ) {
147
			$user = $page->getUser();
148
			$names[] = $user;
149
			$text .= "{{Breve|admin|{{subst:#time:j}}|[[Utente:$user|]] " .
150
				"non è stato riconfermato [[WP:A|amministratore]].}}\n";
151
		}
152
153
		$oldLoc = setlocale( LC_TIME, 'it_IT', 'Italian_Italy', 'Italian' );
154
		$month = ucfirst( strftime( '%B', time() ) );
155
		setlocale( LC_TIME, $oldLoc );
156
157
		$annunciPage = new Page( $this->getConfig()->get( 'annunci-title' ) );
158
		$content = $annunciPage->getContent( 1 );
159
		$secReg = "!=== *$month *===!";
160
		if ( preg_match( $secReg, $content ) !== false ) {
161
			$newContent = preg_replace( $secReg, '$0' . "\n" . $text, $content );
162
		} else {
163
			$re = '!</div>\s*}}\s*</includeonly>!';
164
			$newContent = preg_replace( $re, '$0' . "\n=== $month ===\n" . $text, $content );
165
		}
166
167
		if ( count( $names ) > 1 ) {
168
			$lastUser = array_pop( $names );
169
			$namesList = implode( ', ', $names ) . " e $lastUser";
170
		} else {
171
			$namesList = $names[0];
172
		}
173
174
		$summary = $this->msg( 'annunci-summary' )
175
			->params( [ '$names' => $namesList ] )
176
			->text();
177
178
		$params = [
179
			'text' => $newContent,
180
			'summary' => $summary
181
		];
182
		$annunciPage->edit( $params );
183
	}
184
185
	/**
186
	 * Update [[Wikipedia:Ultime notizie]]
187
	 *
188
	 * @param PageRiconferma[] $pages
189
	 */
190
	protected function updateUltimeNotizie( array $pages ) {
191
		$this->getLogger()->info( 'Updating ultime notizie' );
192
		$notiziePage = new Page( $this->getConfig()->get( 'ultimenotizie-title' ) );
193
194
		$names = [];
195
		$text = '';
196
		foreach ( $pages as $page ) {
197
			$user = $page->getUser();
198
			$title = $page->getTitle();
199
			$names[] = $user;
200
			$text .= "'''{{subst:#time:j F}}''': [[Utente:$user|]] non è stato [[$title|riconfermato]] " .
201
				'[[WP:A|amministratore]]; ora gli admin sono {{subst:#expr: {{NUMBEROFADMINS}} - 1}}.';
202
		}
203
204
		$content = $notiziePage->getContent();
205
		$year = date( 'Y' );
206
		$secReg = "!== *$year *==!";
207
		if ( preg_match( $secReg, $content ) !== false ) {
208
			$newContent = preg_replace( $secReg, '$0' . "\n" . $text, $content );
209
		} else {
210
			$re = '!si veda la \[\[[^\]+relativa discussione]]\.\n!';
211
			$newContent = preg_replace( $re, '$0' . "\n== $year ==\n" . $text, $content );
212
		}
213
214
		if ( count( $names ) > 1 ) {
215
			$lastUser = array_pop( $names );
216
			$namesList = implode( ', ', $names ) . " e $lastUser";
217
		} else {
218
			$namesList = $names[0];
219
		}
220
221
		$summary = $this->msg( 'ultimenotizie-summary' )
222
			->params( [ '$names' => $namesList ] )
223
			->text();
224
225
		$params = [
226
			'text' => $newContent,
227
			'summary' => $summary
228
		];
229
		$notiziePage->edit( $params );
230
	}
231
}
232