Passed
Push — master ( 56087c...78cb26 )
by Daimona
02:23
created

FailedUpdates::getFailedBureaucrats()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 9
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\Wiki\Element;
7
use BotRiconferme\Wiki\Page\Page;
8
use BotRiconferme\Wiki\Page\PageRiconferma;
9
use BotRiconferme\TaskResult;
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
			$bureaucrats = array_keys( $this->getFailedBureaucrats( $failed ) );
23
			if ( $bureaucrats ) {
24
				$this->updateBurList( $bureaucrats );
25
			}
26
			$this->requestRemoval( $failed );
27
			$this->updateAnnunci( $failed );
28
			$this->updateUltimeNotizie( $failed );
29
		}
30
31
		return TaskResult::STATUS_GOOD;
32
	}
33
34
	/**
35
	 * Get the list of failed votes
36
	 *
37
	 * @return PageRiconferma[]
38
	 */
39
	private function getFailures() : array {
40
		$ret = [];
41
		$allPages = $this->getDataProvider()->getPagesToClose();
42
		foreach ( $allPages as $page ) {
43
			if ( $page->getOutcome() & PageRiconferma::OUTCOME_FAIL ) {
44
				$ret[] = $page;
45
			}
46
		}
47
		return $ret;
48
	}
49
50
	/**
51
	 * @param User[] $users
52
	 */
53
	protected function updateBurList( array $users ) {
54
		$this->getLogger()->info( 'Updating bur list. Removing: ' . implode( ', ', $users ) );
55
		$remList = Element::regexFromArray( $users );
56
		$burList = new Page( $this->getConfig()->get( 'bur-list-title' ) );
57
		$content = $burList->getContent();
58
		$reg = "!^\#\{\{ *Burocrate *\| *$remList.+\n!m";
59
		$newContent = preg_replace( $reg, '', $content );
60
61
		$summary = $this->msg( 'bur-list-update-summary' )
62
			->params( [ '$remove' => Message::commaList( $users ) ] )
63
			->text();
64
65
		$burList->edit( [
66
			'text' => $newContent,
67
			'summary' => $summary
68
		] );
69
	}
70
71
	/**
72
	 * Request the removal of the flag on meta
73
	 *
74
	 * @param PageRiconferma[] $pages
75
	 */
76
	protected function requestRemoval( array $pages ) {
77
		$this->getLogger()->info(
78
			'Requesting removal on meta for: ' . implode( ', ', $pages )
79
		);
80
81
		$flagRemPage = new Page(
82
			$this->getConfig()->get( 'flag-removal-page-title' ),
83
			'https://meta.wikimedia.org/w/api.php'
84
		);
85
		$baseText = $this->msg( 'flag-removal-text' );
86
87
		$content = $flagRemPage->getContent();
88
		$append = '';
89
		foreach ( $pages as $page ) {
90
			$append .=
91
				$baseText->params( [
92
					'$username' => $page->getUser()->getName(),
93
					'$link' => '[[:it:' . $page->getTitle() . ']]',
94
					'$groups' => implode( ', ', $page->getUser()->getGroups() )
95
				] )->text();
96
		}
97
98
		$after = '=== Miscellaneous requests ===';
99
		$newContent = str_replace( $after, "$append\n$after", $content );
100
		$summary = $this->msg( 'flag-removal-summary' )
101
			->params( [ '$num' => count( $pages ) ] )
102
			->text();
103
104
		$flagRemPage->edit( [
105
			'text' => $newContent,
106
			'summary' => $summary
107
		] );
108
	}
109
110
	/**
111
	 * Update [[Wikipedia:Wikipediano/Annunci]]
112
	 *
113
	 * @param PageRiconferma[] $pages
114
	 */
115
	protected function updateAnnunci( array $pages ) {
116
		$this->getLogger()->info( 'Updating annunci' );
117
		$section = 1;
118
119
		$names = [];
120
		$text = '';
121
		$msg = $this->msg( 'annunci-text' );
122
		foreach ( $pages as $page ) {
123
			$user = $page->getUser()->getName();
124
			$names[] = $user;
125
			$text .= $msg->params( [ '$user' => $user ] )->text();
126
		}
127
128
		$month = ucfirst( Message::MONTHS[ date( 'F' ) ] );
129
130
		$annunciPage = new Page( $this->getConfig()->get( 'annunci-page-title' ) );
131
		$content = $annunciPage->getContent( $section );
132
		$secReg = "!=== *$month *===!";
133
		if ( preg_match( $secReg, $content ) ) {
134
			$newContent = preg_replace( $secReg, '$0' . "\n" . $text, $content );
135
		} else {
136
			$before = '!</div>\s*}}\s*</includeonly>!';
137
			$newContent = preg_replace( $before, '$0' . "\n=== $month ===\n" . $text, $content );
138
		}
139
140
		$summary = $this->msg( 'annunci-summary' )
141
			->params( [ '$names' => Message::commaList( $names ) ] )
142
			->text();
143
144
		$annunciPage->edit( [
145
			'section' => $section,
146
			'text' => $newContent,
147
			'summary' => $summary
148
		] );
149
	}
150
151
	/**
152
	 * Update [[Wikipedia:Ultime notizie]]
153
	 *
154
	 * @param PageRiconferma[] $pages
155
	 */
156
	protected function updateUltimeNotizie( array $pages ) {
157
		$this->getLogger()->info( 'Updating ultime notizie' );
158
		$notiziePage = new Page( $this->getConfig()->get( 'ultimenotizie-page-title' ) );
159
160
		$names = [];
161
		$text = '';
162
		$msg = $this->msg( 'ultimenotizie-text' );
163
		foreach ( $pages as $page ) {
164
			$user = $page->getUser()->getName();
165
			$title = $page->getTitle();
166
			$names[] = $user;
167
			$text .= $msg->params( [ '$user' => $user, '$title' => $title ] )->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 ) ] )
182
			->text();
183
184
		$notiziePage->edit( [
185
			'text' => $newContent,
186
			'summary' => $summary
187
		] );
188
	}
189
190
	/**
191
	 * Get a list of bureaucrats from the given $pages
192
	 *
193
	 * @param PageRiconferma[] $pages
194
	 * @return User[]
195
	 */
196
	private function getFailedBureaucrats( array $pages ) : array {
197
		$ret = [];
198
		foreach ( $pages as $page ) {
199
			$user = $page->getUser();
200
			if ( $user->inGroup( 'bureaucrat' ) ) {
201
				$ret[] = $user;
202
			}
203
		}
204
		return $ret;
205
	}
206
}
207