Passed
Push — master ( 0d0277...cfeed2 )
by Daimona
02:12
created

FailedUpdates::updateTimeline()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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