Passed
Push — master ( 1a47f4...ba2710 )
by Daimona
01:58
created

UpdatesAround::addToVotazioni()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 41
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 26
nc 6
nop 1
dl 0
loc 41
rs 9.504
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\Page\Page;
7
use BotRiconferme\Page\PageRiconferma;
8
use BotRiconferme\Exception\TaskException;
9
use BotRiconferme\TaskResult;
10
11
/**
12
 * Do some updates around to notify people of the newly created pages
13
 */
14
class UpdatesAround extends Subtask {
15
	/**
16
	 * @inheritDoc
17
	 */
18
	public function runInternal() : int {
19
		$pages = $this->getDataProvider()->getCreatedPages();
20
21
		if ( !$pages ) {
22
			return TaskResult::STATUS_NOTHING;
23
		}
24
25
		// Wikipedia:Amministratori/Riconferma annuale
26
		$this->addToMainPage( $pages );
27
		// WP:Wikipediano/Votazioni
28
		$this->addToVotazioni( $pages );
29
		// Template:VotazioniRCnews
30
		$this->addToNews( count( $pages ) );
31
32
		return TaskResult::STATUS_GOOD;
33
	}
34
35
	/**
36
	 * Add created pages to Wikipedia:Amministratori/Riconferma annuale
37
	 *
38
	 * @param PageRiconferma[] $pages
39
	 */
40
	protected function addToMainPage( array $pages ) {
41
		$this->getLogger()->info(
42
			'Adding the following to main: ' . implode( ', ', array_map( 'strval', $pages ) )
43
		);
44
45
		$append = '';
46
		foreach ( $pages as $page ) {
47
			$append .= '{{' . $page->getTitle() . "}}\n";
48
		}
49
50
		$summary = $this->msg( 'main-page-summary' )
51
			->params( [ '$num' => count( $pages ) ] )->text();
52
53
		$params = [
54
			'title' => $this->getConfig()->get( 'main-page-title' ),
55
			'appendtext' => $append,
56
			'summary' => $summary
57
		];
58
59
		$this->getController()->editPage( $params );
60
	}
61
62
	/**
63
	 * Add a line in Wikipedia:Wikipediano/Votazioni
64
	 *
65
	 * @param PageRiconferma[] $pages
66
	 */
67
	protected function addToVotazioni( array $pages ) {
68
		$this->getLogger()->info(
69
			'Adding the following to votes: ' . implode( ', ', array_map( 'strval', $pages ) )
70
		);
71
		$votePage = new Page( $this->getConfig()->get( 'vote-page-title' ) );
72
73
		$content = $votePage->getContent();
74
75
		$time = Message::getTimeWithArticle( time() + ( 60 * 60 * 24 * 7 ) );
76
		$newLines = '';
77
		foreach ( $pages as $page ) {
78
			$newLines .= '*[[Utente:' . $page->getUser() . '|]]. ' .
79
				'La [[' . $page->getTitle() . "|procedura]] termina $time;\n";
80
		}
81
82
		$introReg = '!^;È in corso la .*riconferma tacita.* degli .*amministratori.+!m';
83
		if ( preg_match( $introReg, strip_tags( $content ) ) ) {
84
			// Put before the existing ones, if they're found outside comments
85
			$newContent = preg_replace( $introReg, '$0' . "\n$newLines", $content, 1 );
86
		} else {
87
			// Start section
88
			try {
89
				$matches = $votePage->getMatch( $introReg );
90
			} catch ( \Exception $e ) {
91
				throw new TaskException( 'Intro not found in vote page' );
92
			}
93
			$beforeReg = '!INSERIRE LA NOTIZIA PIÙ NUOVA IN CIMA.+!m';
94
			// Replace semicolon with full stop
95
			$newLines = substr( $newLines, 0, -2 ) . ".\n";
96
			$newContent = preg_replace( $beforeReg, '$0' . "\n{$matches[0]}\n$newLines", $content, 1 );
97
		}
98
99
		$summary = $this->msg( 'vote-page-summary' )
100
			->params( [ '$num' => count( $pages ) ] )->text();
101
102
		$params = [
103
			'text' => $newContent,
104
			'summary' => $summary
105
		];
106
107
		$votePage->edit( $params );
108
	}
109
110
	/**
111
	 * Update the counter on Template:VotazioniRCnews
112
	 *
113
	 * @param int $amount
114
	 */
115
	protected function addToNews( int $amount ) {
116
		$this->getLogger()->info( "Increasing the news counter by $amount" );
117
		$newsPage = new Page( $this->getConfig()->get( 'news-page-title' ) );
118
119
		$content = $newsPage->getContent();
120
		$reg = '!(\| *riconferme[ _]tacite[ _]amministratori *= *)(\d+)!';
121
122
		try {
123
			$matches = $newsPage->getMatch( $reg );
124
		} catch ( \Exception $e ) {
125
			throw new TaskException( 'Param not found in news page' );
126
		}
127
128
		$newNum = (int)$matches[2] + $amount;
129
		$newContent = preg_replace( $reg, '${1}' . $newNum, $content );
130
131
		$summary = $this->msg( 'news-page-summary' )
132
			->params( [ '$num' => $amount ] )
133
			->text();
134
135
		$newsPage->edit( [
136
			'text' => $newContent,
137
			'summary' => $summary
138
		] );
139
	}
140
}
141