|
1
|
|
|
<?php declare( strict_types=1 ); |
|
2
|
|
|
|
|
3
|
|
|
namespace BotRiconferme\Task\Subtask; |
|
4
|
|
|
|
|
5
|
|
|
use BotRiconferme\Page\Page; |
|
6
|
|
|
use BotRiconferme\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
|
|
|
$protectReason = $this->getConfig()->get( 'close-protect-summary' ); |
|
19
|
|
|
foreach ( $pages as $page ) { |
|
20
|
|
|
if ( $page->isVote() ) { |
|
21
|
|
|
$this->addVoteCloseText( $page ); |
|
22
|
|
|
} |
|
23
|
|
|
$this->getController()->protectPage( $page->getTitle(), $protectReason ); |
|
24
|
|
|
$this->updateBasePage( $page ); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
return self::STATUS_GOOD ; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param PageRiconferma $page |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function addVoteCloseText( PageRiconferma $page ) { |
|
34
|
|
|
$content = $page->getContent(); |
|
35
|
|
|
$beforeReg = '!è necessario ottenere una maggioranza .+ votanti\.!'; |
|
36
|
|
|
$newContent = preg_replace( $beforeReg, '$0' . "\n" . $page->getOutcomeText(), $content ); |
|
37
|
|
|
|
|
38
|
|
|
$page->edit( [ |
|
39
|
|
|
'text' => $newContent, |
|
40
|
|
|
'summary' => $this->getConfig()->get( 'close-result-summary' ) |
|
41
|
|
|
] ); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param PageRiconferma $page |
|
46
|
|
|
* @see CreatePages::updateBasePage() |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function updateBasePage( PageRiconferma $page ) { |
|
49
|
|
|
$this->getLogger()->info( "Updating base page for $page" ); |
|
50
|
|
|
|
|
51
|
|
|
$basePage = new Page( $page->getBaseTitle() ); |
|
52
|
|
|
$current = $basePage->getContent(); |
|
53
|
|
|
|
|
54
|
|
|
$outcomeText = $page->getOutcome() & PageRiconferma::OUTCOME_FAIL ? |
|
55
|
|
|
'non riconfermato' : |
|
56
|
|
|
'riconfermato'; |
|
57
|
|
|
$text = $page->isVote() ? "votazione: $outcomeText" : 'riconferma tacita'; |
|
58
|
|
|
|
|
59
|
|
|
$newContent = str_replace( 'riconferma in corso', $text, $current ); |
|
60
|
|
|
|
|
61
|
|
|
$basePage->edit( [ |
|
62
|
|
|
'text' => $newContent, |
|
63
|
|
|
'summary' => $this->getConfig()->get( 'close-base-page-summary-update' ) |
|
64
|
|
|
] ); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|