OpenUpdates   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 58
dl 0
loc 119
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

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