Passed
Push — master ( 2ef02f...fd3159 )
by Daimona
01:56
created

FailedUpdates   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 110
dl 0
loc 192
rs 10
c 0
b 0
f 0
wmc 18

6 Methods

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