Passed
Push — master ( 6ce606...5a8341 )
by Daimona
01:34
created

ArchivePages::removeFromMainPage()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 19
nc 4
nop 1
dl 0
loc 29
rs 9.6333
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\Wiki\Page\Page;
7
use BotRiconferme\Wiki\Page\PageRiconferma;
8
use BotRiconferme\TaskResult;
9
10
/**
11
 * Remove pages from the main page and add them to the archive
12
 */
13
class ArchivePages extends Subtask {
14
	/**
15
	 * @inheritDoc
16
	 */
17
	public function runInternal() : int {
18
		$pages = $this->getDataProvider()->getPagesToClose();
19
20
		if ( !$pages ) {
21
			return TaskResult::STATUS_NOTHING;
22
		}
23
24
		$this->removeFromMainPage( $pages );
25
		$this->addToArchive( $pages );
26
27
		return TaskResult::STATUS_GOOD;
28
	}
29
30
	/**
31
	 * Removes pages from WP:A/Riconferme annuali
32
	 *
33
	 * @param PageRiconferma[] $pages
34
	 * @see UpdatesAround::addToMainPage()
35
	 */
36
	protected function removeFromMainPage( array $pages ) {
37
		$this->getLogger()->info(
38
			'Removing from main: ' . implode( ', ', $pages )
39
		);
40
41
		$mainPage = new Page( $this->getConfig()->get( 'main-page-title' ) );
42
		$remove = [];
43
		foreach ( $pages as $page ) {
44
			$remove[] = '{{' . $page->getTitle() . '}}';
45
		}
46
47
		$newContent = str_replace( $remove, '', $mainPage->getContent() );
48
49
		$reg = '!\{\{(?:Wikipedia:Amministratori\/Riconferma annuale)?\/[^/}]+\/\d!';
50
		if ( preg_match_all( $reg, $newContent ) === 1 ) {
51
			$newContent = preg_replace(
52
				"/<!-- (:''Nessuna riconferma in corso\.'') -->/",
53
				'$1',
54
				$newContent
55
			);
56
		}
57
58
		$summary = $this->msg( 'close-main-summary' )
59
			->params( [ '$num' => count( $pages ) ] )
60
			->text();
61
62
		$mainPage->edit( [
63
			'text' => $newContent,
64
			'summary' => $summary
65
		] );
66
	}
67
68
	/**
69
	 * Adds closed pages to the current archive
70
	 *
71
	 * @param PageRiconferma[] $pages
72
	 */
73
	protected function addToArchive( array $pages ) {
74
		$this->getLogger()->info(
75
			'Adding to archive: ' . implode( ', ', $pages )
76
		);
77
78
		$simple = $votes = [];
79
		foreach ( $pages as $page ) {
80
			if ( $page->isVote() ) {
81
				$votes[] = $page;
82
			} else {
83
				$simple[] = $page;
84
			}
85
		}
86
87
		$simpleTitle = $this->getConfig()->get( 'close-simple-archive-title' );
88
		$voteTitle = $this->getConfig()->get( 'close-vote-archive-title' );
89
90
		if ( $simple ) {
91
			$this->reallyAddToArchive( $simpleTitle, $simple );
92
		}
93
		if ( $votes ) {
94
			$this->reallyAddToArchive( $voteTitle, $votes );
95
		}
96
	}
97
98
	/**
99
	 * Really add $pages to the given archive
100
	 *
101
	 * @param string $archiveTitle
102
	 * @param array $pages
103
	 */
104
	private function reallyAddToArchive( string $archiveTitle, array $pages ) {
105
		$archivePage = new Page( "$archiveTitle/" . date( 'Y' ) );
106
		$exists = $archivePage->exists();
107
108
		$append = "\n";
109
		$archivedList = [];
110
		foreach ( $pages as $page ) {
111
			$append .= '{{' . $page->getTitle() . "}}\n";
112
			$archivedList[] = $page->getUserNum();
113
		}
114
115
		$summary = $this->msg( 'close-archive-summary' )
116
			->params( [ '$usernums' => Message::commaList( $archivedList ) ] )->text();
117
118
		$archivePage->edit( [
119
			'appendtext' => $append,
120
			'summary' => $summary
121
		] );
122
123
		if ( !$exists ) {
124
			$this->addArchiveYear( $archiveTitle );
125
		}
126
	}
127
128
	/**
129
	 * Add a link to the newly-created archive for this year to the main archive page
130
	 *
131
	 * @param string $archiveTitle
132
	 */
133
	private function addArchiveYear( string $archiveTitle ) {
134
		$page = new Page( $archiveTitle );
135
		$year = date( 'Y' );
136
137
		$summary = $this->msg( 'new-archive-summary' )
138
			->params( [ '$year' => $year ] )->text();
139
140
		$page->edit( [
141
			'appendtext' => "\n*[[/$year|$year]]",
142
			'summary' => $summary
143
		] );
144
	}
145
}
146