Passed
Branch master (a4e902)
by Daimona
01:39
created

FailedUpdates::runInternal()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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