Passed
Push — master ( d72fa1...8ee621 )
by Daimona
01:55
created

UpdatesAround::getTimeWithArticle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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