Passed
Branch master (a4e902)
by Daimona
01:39
created

UpdatesAround::runInternal()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 15
rs 10
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
10
/**
11
 * Do some updates around to notify people of the newly created pages
12
 */
13
class UpdatesAround extends Subtask {
14
	/**
15
	 * @inheritDoc
16
	 */
17
	public function runInternal() : int {
18
		$pages = $this->getDataProvider()->getCreatedPages();
19
20
		if ( !$pages ) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $pages of type BotRiconferme\Page\PageRiconferma[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

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