ArchivePages   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 62
dl 0
loc 133
rs 10
c 0
b 0
f 0
wmc 14

5 Methods

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