Passed
Push — master ( 8ee621...33a572 )
by Daimona
01:38
created

StartVote::updateVotePage()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 54
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 35
nc 6
nop 1
dl 0
loc 54
rs 9.0488
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme\Task;
4
5
use BotRiconferme\Exception\TaskException;
6
use BotRiconferme\TaskResult;
7
use BotRiconferme\WikiController;
8
9
/**
10
 * Start a vote if there are >= 15 opposing comments
11
 */
12
class StartVote extends Task {
13
	/**
14
	 * @inheritDoc
15
	 */
16
	public function run() : TaskResult {
17
		$this->getLogger()->info( 'Starting task StartVote' );
18
19
		$pages = $this->getDataProvider()->getOpenPages();
20
21
		if ( $pages ) {
22
			$this->processPages( $pages );
23
		} else {
24
			$this->getLogger()->info( 'No open procedures.' );
25
		}
26
27
		$this->getLogger()->info( 'Task StartVote completed successfully' );
28
		return new TaskResult( self::STATUS_OK );
29
	}
30
31
	/**
32
	 * @param string[] $pages
33
	 */
34
	protected function processPages( array $pages ) {
35
		$actualPages = [];
36
		foreach ( $pages as $page ) {
37
			if ( WikiController::hasOpposition( $page ) ) {
38
				try {
39
					$this->openVote( $page );
40
					$actualPages[] = $page;
41
				} catch ( TaskException $e ) {
42
					$this->getLogger()->warning( $e->getMessage() );
43
				}
44
			}
45
		}
46
47
		if ( $actualPages ) {
48
			$this->updateVotePage( $actualPages );
49
			$this->updateNews( count( $actualPages ) );
50
		} else {
51
			$this->getLogger()->info( 'No votes to open' );
52
		}
53
	}
54
55
	/**
56
	 * Start the vote for the given page
57
	 *
58
	 * @param string $title
59
	 * @throws TaskException
60
	 */
61
	protected function openVote( string $title ) {
62
		$this->getLogger()->info( "Starting vote on $title" );
63
64
		$content = $this->getController()->getPageContent( $title );
65
66
		if ( preg_match( '/<!-- SEZIONE DA UTILIZZARE PER/', $content ) === false ) {
67
			throw new TaskException( "Vote already opened in $title" );
68
		}
69
70
		$newContent = preg_replace(
71
			'!^La procedura di riconferma tacita .+!m',
72
			'<del>$0</del>',
73
			$content
74
		);
75
76
		$newContent = preg_replace(
77
			'/<!-- SEZIONE DA UTILIZZARE PER L\'EVENTUALE VOTAZIONE DI RICONFERMA.*\n/',
78
			'',
79
			$newContent
80
		);
81
82
		$newContent = preg_replace(
83
			'!(==== *Favorevoli alla riconferma *====\n\#[\s.]+)\n-->!',
84
			'$1',
85
			$newContent
86
		);
87
88
		$params = [
89
			'title' => $title,
90
			'text' => $newContent,
91
			'summary' => $this->getConfig()->get( 'vote-start-summary' )
92
		];
93
94
		$this->getController()->editPage( $params );
95
	}
96
97
	/**
98
	 * Update [[WP:Wikipediano/Votazioni]]
99
	 *
100
	 * @param string[] $titles
101
	 * @see ClosePages::updateVote()
102
	 * @see UpdatesAround::addVote()
103
	 */
104
	protected function updateVotePage( array $titles ) {
105
		$votePage = $this->getConfig()->get( 'ric-vote-page' );
106
		$content = $this->getController()->getPageContent( $votePage );
107
108
		$titleReg = implode( '|', array_map( 'preg_quote', $titles ) );
109
		$search = "!^\*.+ La \[\[($titleReg)\|procedura]] termina.+\n!gm";
110
111
		$newContent = preg_replace( $search, '', $content );
112
		// Make sure the last line ends with a full stop
113
		$sectionReg = '!(^;È in corso.+riconferma tacita.+amministratori.+\n(?:\*.+[;\.]\n)+\*.+)[\.;]!m';
114
		$newContent = preg_replace( $sectionReg, '$1.', $newContent );
115
116
		$newLines = '';
117
		$time = WikiController::getTimeWithArticle( time() + ( 60 * 60 * 24 * 14 ) );
118
		foreach ( $titles as $title ) {
119
			$user = explode( '/', $title )[2];
120
			$newLines .= "*[[Utente:$user|]]. La [[$title|votazione]] termina $time;\n";
121
		}
122
123
		$introReg = '!^Si vota per la \[\[Wikipedia:Amministratori/Riconferma annuale.+!m';
124
		if ( preg_match( $introReg, strip_tags( $content ) ) ) {
125
			// Put before the existing ones, if they're found outside comments
126
			$newContent = preg_replace( $introReg, '$0' . "\n$newLines", $newContent, 1 );
127
		} else {
128
			// Start section
129
			$matches = [];
130
			if ( preg_match( $introReg, $content, $matches ) === false ) {
131
				throw new TaskException( 'Intro not found in vote page' );
132
			}
133
			$beforeReg = '!INSERIRE LA NOTIZIA PIÙ NUOVA IN CIMA.+!m';
134
			// Replace semicolon with full stop
135
			$newLines = substr( $newLines, 0, -2 ) . ".\n";
136
			$newContent = preg_replace( $beforeReg, '$0' . "\n{$matches[0]}\n$newLines", $newContent, 1 );
137
		}
138
139
		$summary = strtr(
140
			$this->getConfig()->get( 'vote-start-vote-page-summary' ),
141
			[ '$num' => count( $titles ) ]
142
		);
143
		$summary = preg_replace_callback(
144
			'!\{\{$plur|(\d+)|([^|]+)|([^|]+)}}!',
145
			function ( $matches ) {
146
				return intval( $matches[1] ) > 1 ? trim( $matches[3] ) : trim( $matches[2] );
147
			},
148
			$summary
149
		);
150
151
		$params = [
152
			'title' => $votePage,
153
			'text' => $newContent,
154
			'summary' => $summary
155
		];
156
157
		$this->getController()->editPage( $params );
158
	}
159
160
	/**
161
	 * Template:VotazioniRCnews
162
	 *
163
	 * @param int $amount Of pages to move
164
	 * @see UpdatesAround::addNews()
165
	 * @see ClosePages::updateNews()
166
	 */
167
	protected function updateNews( int $amount ) {
168
		$this->getLogger()->info( "Turning $amount pages into votes" );
169
		$newsPage = $this->getConfig()->get( 'ric-news-page' );
170
171
		$content = $this->getController()->getPageContent( $newsPage );
172
		$regTac = '!(\| *riconferme[ _]tacite[ _]amministratori *= *)(\d+)!';
173
		$regVot = '!(\| *riconferme[ _]voto[ _]amministratori *= *)(\d+)!';
174
175
		$tacMatches = $votMatches = [];
176
		if ( preg_match( $regTac, $content, $tacMatches ) === false ) {
177
			throw new TaskException( 'Param "tacite" not found in news page' );
178
		}
179
		if ( preg_match( $regVot, $content, $votMatches ) === false ) {
180
			throw new TaskException( 'Param "voto" not found in news page' );
181
		}
182
183
		$newTac = (int)$tacMatches[2] - $amount;
184
		$newVot = (int)$votMatches[2] + $amount;
185
186
		$newContent = preg_replace( $regTac, '${1}' . $newTac, $content );
187
		$newContent = preg_replace( $regVot, '${1}' . $newVot, $newContent );
188
189
		$summary = strtr(
190
			$this->getConfig()->get( 'vote-start-news-page-summary' ),
191
			[ '$num' => $amount ]
192
		);
193
		$summary = preg_replace_callback(
194
			'!\{\{$plur|(\d+)|([^|]+)|([^|]+)}}!',
195
			function ( $matches ) {
196
				return intval( $matches[1] ) > 1 ? trim( $matches[3] ) : trim( $matches[2] );
197
			},
198
			$summary
199
		);
200
201
		$params = [
202
			'title' => $newsPage,
203
			'text' => $newContent,
204
			'summary' => $summary
205
		];
206
207
		$this->getController()->editPage( $params );
208
	}
209
}
210