Passed
Push — master ( db42f9...1e692f )
by Daimona
02:07
created

OpenUpdates::addToVotazioni()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 19
nc 2
nop 1
dl 0
loc 29
rs 9.6333
c 0
b 0
f 0
1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme\Task\Subtask;
4
5
use BotRiconferme\Wiki\Page\Page;
6
use BotRiconferme\Wiki\Page\PageRiconferma;
7
use BotRiconferme\Exception\TaskException;
8
use BotRiconferme\TaskResult;
9
10
/**
11
 * Do some updates around to notify people of the newly created pages
12
 */
13
class OpenUpdates extends Subtask {
14
	/**
15
	 * @inheritDoc
16
	 */
17
	public function runInternal() : int {
18
		$pages = $this->getDataProvider()->getCreatedPages();
19
20
		if ( !$pages ) {
21
			return TaskResult::STATUS_NOTHING;
22
		}
23
24
		// Wikipedia:Amministratori/Riconferma annuale
25
		$this->addToMainPage( $pages );
26
		// WP:Wikipediano/Votazioni
27
		$this->addToVotazioni( $pages );
28
		// Template:VotazioniRCnews
29
		$this->addToNews( count( $pages ) );
30
31
		return TaskResult::STATUS_GOOD;
32
	}
33
34
	/**
35
	 * Add created pages to Wikipedia:Amministratori/Riconferma annuale
36
	 *
37
	 * @param PageRiconferma[] $pages
38
	 */
39
	protected function addToMainPage( array $pages ) {
40
		$this->getLogger()->info(
41
			'Adding the following to main: ' . implode( ', ', $pages )
42
		);
43
44
		$mainPage = new Page( $this->getOpt( 'main-page-title' ) );
45
46
		$append = "\n";
47
		foreach ( $pages as $page ) {
48
			$append .= '{{' . $page->getTitle() . "}}\n";
49
		}
50
51
		$newContent = $mainPage->getContent() . $append;
52
		$newContent = preg_replace(
53
			"/^:''Nessuna riconferma in corso\.''/m",
54
			'<!-- $0 -->',
55
			$newContent
56
		);
57
58
		$summary = $this->msg( 'main-page-summary' )
59
			->params( [ '$num' => count( $pages ) ] )->text();
60
61
		$mainPage->edit( [
62
			'text' => $newContent,
63
			'summary' => $summary
64
		] );
65
	}
66
67
	/**
68
	 * Add a line in Wikipedia:Wikipediano/Votazioni
69
	 *
70
	 * @param PageRiconferma[] $pages
71
	 */
72
	protected function addToVotazioni( array $pages ) {
73
		$this->getLogger()->info(
74
			'Adding the following to votes: ' . implode( ', ', $pages )
75
		);
76
		$votePage = new Page( $this->getOpt( 'vote-page-title' ) );
77
78
		$endDays = PageRiconferma::SIMPLE_DURATION;
79
		$newLines = '';
80
		foreach ( $pages as $page ) {
81
			$newLines .= '{{subst:Wikipedia:Wikipediano/Votazioni/RigaCompleta|riconferma tacita' .
82
				'|utente=' . $page->getUser() . '|numero=' . $page->getNum() . '|giorno=' .
83
				"{{subst:#timel:j F|+ $endDays days}}|ore={{subst:LOCALTIME}}}}\n";
84
		}
85
86
		$newContent = preg_replace(
87
			'!\|riconferme[ _]tacite *= *\n!',
88
			'$0' . $newLines,
89
			$votePage->getContent()
90
		);
91
92
		$summary = $this->msg( 'vote-page-summary' )
93
			->params( [ '$num' => count( $pages ) ] )->text();
94
95
		$params = [
96
			'text' => $newContent,
97
			'summary' => $summary
98
		];
99
100
		$votePage->edit( $params );
101
	}
102
103
	/**
104
	 * Update the counter on Template:VotazioniRCnews
105
	 *
106
	 * @param int $amount
107
	 * @throws TaskException
108
	 */
109
	protected function addToNews( int $amount ) {
110
		$this->getLogger()->info( "Increasing the news counter by $amount" );
111
		$newsPage = new Page( $this->getOpt( 'news-page-title' ) );
112
113
		$content = $newsPage->getContent();
114
		$reg = '!(\| *riconferme[ _]tacite[ _]amministratori *= *)(\d*)(?=\s*[}|])!';
115
116
		try {
117
			$matches = $newsPage->getMatch( $reg );
118
		} catch ( \Exception $e ) {
119
			throw new TaskException( 'Param not found in news page' );
120
		}
121
122
		$newNum = (int)$matches[2] + $amount;
123
		$newContent = preg_replace( $reg, '${1}' . $newNum, $content );
124
125
		$summary = $this->msg( 'news-page-summary' )
126
			->params( [ '$num' => $amount ] )
127
			->text();
128
129
		$newsPage->edit( [
130
			'text' => $newContent,
131
			'summary' => $summary
132
		] );
133
	}
134
}
135