Passed
Push — master ( 35639c...18326f )
by Daimona
01:42
created

UpdatesAround::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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