Passed
Push — master ( 42aaff...766431 )
by Daimona
01:42
created

ArchivePages::addToArchive()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 1
dl 0
loc 19
rs 9.8666
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
9
/**
10
 * Remove pages from the main page and add them to the archive
11
 */
12
class ArchivePages extends Subtask {
13
	/**
14
	 * @inheritDoc
15
	 */
16
	public function run() : TaskResult {
17
		$this->getLogger()->info( 'Starting task ArchivePages' );
18
19
		$pages = $this->getDataProvider()->getPagesToClose();
20
		$this->removeFromMainPage( $pages );
21
		$this->addToArchive( $pages );
22
23
		$this->getLogger()->info( 'Task ArchivePages completed successfully' );
24
		return new TaskResult( self::STATUS_OK );
25
	}
26
27
	/**
28
	 * Removes pages from WP:A/Riconferme annuali
29
	 *
30
	 * @param PageRiconferma[] $pages
31
	 * @see UpdatesAround::addToMainPage()
32
	 */
33
	protected function removeFromMainPage( array $pages ) {
34
		$this->getLogger()->info(
35
			'Removing from main: ' . implode( ', ', array_map( 'strval', $pages ) )
36
		);
37
38
		$mainPage = new Page( $this->getConfig()->get( 'ric-main-page' ) );
39
		$translations = [];
40
		foreach ( $pages as $page ) {
41
			$translations[ '{{' . $page->getTitle() . '}}' ] = '';
42
		}
43
44
		$params = [
45
			'title' => $mainPage,
46
			'text' => strtr( $mainPage->getContent(), $translations ),
47
			'summary' => $this->getConfig()->get( 'close-main-summary' )
48
		];
49
		$this->getController()->editPage( $params );
50
	}
51
52
	/**
53
	 * Adds closed pages to the current archive
54
	 *
55
	 * @param PageRiconferma[] $pages
56
	 */
57
	protected function addToArchive( array $pages ) {
58
		$this->getLogger()->info(
59
			'Adding to archive: ' . implode( ', ', array_map( 'strval', $pages ) )
60
		);
61
62
		$simple = $votes = [];
63
		foreach ( $pages as $page ) {
64
			if ( $page->isVote() ) {
65
				$votes[] = $page;
66
			} else {
67
				$simple[] = $page;
68
			}
69
		}
70
71
		$simpleTitle = $this->getConfig()->get( 'close-simple-archive-title' );
72
		$voteTitle = $this->getConfig()->get( 'close-vote-archive-title' );
73
74
		$this->reallyAddToArchive( $simpleTitle, $simple );
75
		$this->reallyAddToArchive( $voteTitle, $votes );
76
	}
77
78
	/**
79
	 * Really add $pages to the given archive
80
	 *
81
	 * @param string $archiveTitle
82
	 * @param array $pages
83
	 */
84
	private function reallyAddToArchive( string $archiveTitle, array $pages ) {
85
		$curTitle = "$archiveTitle/" . date( 'Y' );
86
87
		$append = '';
88
		$archivedList = [];
89
		foreach ( $pages as $page ) {
90
			$append .= '{{' . $page->getTitle() . "}}\n";
91
			$archivedList[] = $page->getUserNum();
92
		}
93
94
		if ( count( $archivedList ) > 1 ) {
95
			$last = array_pop( $archivedList );
96
			$userNums = implode( ', ', $archivedList ) . " e $last";
97
		} else {
98
			$userNums = $archivedList[0];
99
		}
100
101
		$summary = $this->msg( 'close-archive-summary' )
102
			->params( [ '$usernums' => $userNums ] )->text();
103
104
		$params = [
105
			'title' => $curTitle,
106
			'appendtext' => $append,
107
			'summary' => $summary
108
		];
109
110
		$this->getController()->editPage( $params );
111
	}
112
}
113