ClosePages   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
c 0
b 0
f 0
dl 0
loc 62
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A runInternal() 0 17 4
A updateBasePage() 0 21 4
A addVoteCloseText() 0 8 1
1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme\Task\Subtask;
4
5
use BotRiconferme\TaskHelper\TaskResult;
6
use BotRiconferme\Wiki\Page\PageRiconferma;
7
8
/**
9
 * For each open page, protect it, add a closing text if it was a vote, and
10
 * update the text in the base page
11
 */
12
class ClosePages extends Subtask {
13
	/**
14
	 * @inheritDoc
15
	 */
16
	public function runInternal(): int {
17
		$pages = $this->getDataProvider()->getPagesToClose();
18
19
		if ( !$pages ) {
20
			return TaskResult::STATUS_NOTHING;
21
		}
22
23
		$protectReason = $this->msg( 'close-protect-summary' )->text();
24
		foreach ( $pages as $page ) {
25
			if ( $page->isVote() ) {
26
				$this->addVoteCloseText( $page );
27
			}
28
			$this->getWiki()->protectPage( $page->getTitle(), $protectReason );
29
			$this->updateBasePage( $page );
30
		}
31
32
		return TaskResult::STATUS_GOOD;
33
	}
34
35
	/**
36
	 * @param PageRiconferma $page
37
	 */
38
	protected function addVoteCloseText( PageRiconferma $page ): void {
39
		$content = $page->getContent();
40
		$beforeReg = '!è necessario ottenere una maggioranza .+ votanti\.!u';
41
		$newContent = preg_replace( $beforeReg, '$0' . "\n" . $page->getOutcomeText(), $content );
42
43
		$page->edit( [
44
			'text' => $newContent,
45
			'summary' => $this->msg( 'close-result-summary' )->text()
46
		] );
47
	}
48
49
	/**
50
	 * @param PageRiconferma $page
51
	 * @see CreatePages::updateBasePage()
52
	 */
53
	protected function updateBasePage( PageRiconferma $page ): void {
54
		$this->getLogger()->info( "Updating base page for $page" );
55
56
		if ( $page->getNum() === 1 ) {
57
			$basePage = $this->getUser( $page->getUserName() )->getBasePage();
58
		} else {
59
			$basePage = $this->getUser( $page->getUserName() )->getExistingBasePage();
60
		}
61
62
		$current = $basePage->getContent();
63
64
		$outcomeText = ( $page->getOutcome() & PageRiconferma::OUTCOME_FAIL ) ?
65
			'non riconfermato' :
66
			'riconfermato';
67
		$text = $page->isVote() ? "votazione di riconferma: $outcomeText" : 'riconferma tacita';
68
69
		$newContent = preg_replace( '/^(#: *)(votazione di )?riconferma in corso/m', '$1' . $text, $current );
70
71
		$basePage->edit( [
72
			'text' => $newContent,
73
			'summary' => $this->msg( 'close-base-page-summary-update' )->text()
74
		] );
75
	}
76
}
77