Passed
Push — master ( 935a55...baa252 )
by Daimona
01:37
created

CreatePages::getLastPageNum()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 20
c 0
b 0
f 0
nc 8
nop 1
dl 0
loc 33
rs 9.2888
1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme\Task\Subtask;
4
5
use BotRiconferme\Wiki\Page\Page;
6
use BotRiconferme\Wiki\Page\PageRiconferma;
7
use BotRiconferme\Request\RequestBase;
8
use BotRiconferme\Exception\TaskException;
9
use BotRiconferme\TaskResult;
10
use BotRiconferme\Wiki\User;
11
12
/**
13
 * For each user, create the WP:A/Riconferma_annuale/USERNAME/XXX page and add it to its base page
14
 */
15
class CreatePages extends Subtask {
16
	/**
17
	 * @inheritDoc
18
	 */
19
	public function runInternal() : int {
20
		$users = $this->getDataProvider()->getUsersToProcess();
21
22
		if ( !$users ) {
23
			return TaskResult::STATUS_NOTHING;
24
		}
25
26
		foreach ( $users as $user ) {
27
			$this->processUser( $user );
28
		}
29
30
		return TaskResult::STATUS_GOOD;
31
	}
32
33
	/**
34
	 * Determine what pages we need to create for a single user.
35
	 *
36
	 * @param User $user
37
	 */
38
	protected function processUser( User $user ) {
39
		try {
40
			$num = $this->getLastPageNum( $user ) + 1;
41
		} catch ( TaskException $e ) {
42
			// The page was already created today. PLZ let this poor bot work!
43
			$this->getDataProvider()->removeUser( $user->getName() );
44
			$this->getLogger()->warning( $e->getMessage() . " - User $user won't be processed." );
45
			return;
46
		}
47
48
		$basePage = $user->getBasePage();
49
		// This should always use the new username
50
		$pageTitle = "$basePage/$num";
51
		$this->createPage( $pageTitle, $user );
52
		$ricPage = new PageRiconferma( $pageTitle, $this->getWiki() );
53
54
		$newText = $this->msg( 'base-page-text' )->params( [ '$title' => $pageTitle ] )->text();
55
		if ( $num === 1 ) {
56
			$this->createBasePage( $basePage, $newText );
57
		} else {
58
			$basePage = $user->getExistingBasePage();
59
			$this->updateBasePage( $basePage, $newText );
60
		}
61
62
		$this->getDataProvider()->addCreatedPage( $ricPage );
63
	}
64
65
	/**
66
	 * Get the number of last page for the given user
67
	 *
68
	 * @param User $user
69
	 * @return int
70
	 * @throws TaskException
71
	 */
72
	protected function getLastPageNum( User $user ) : int {
73
		$this->getLogger()->debug( "Retrieving previous pages for $user" );
74
75
		$unprefixedTitle = explode( ':', $this->getOpt( 'main-page-title' ), 2 )[1];
76
77
		$prefixes = [ "$unprefixedTitle/$user/" ];
78
		foreach ( $user->getAliases() as $alias ) {
79
			$prefixes[] = "$unprefixedTitle/$alias/";
80
		}
81
82
		$params = [
83
			'action' => 'query',
84
			'list' => 'allpages',
85
			'apnamespace' => 4,
86
			'apprefix' => implode( '|', $prefixes ),
87
			'aplimit' => 'max'
88
		];
89
90
		$res = RequestBase::newFromParams( $params )->execute();
91
92
		$last = 0;
93
		foreach ( $res->query->allpages as $resPage ) {
94
			$page = new PageRiconferma( $resPage->title, $this->getWiki() );
95
96
			if ( date( 'z/Y', $page->getCreationTimestamp() ) === date( 'z/Y' ) ) {
97
				throw new TaskException( "Page $page was already created today!" );
98
			}
99
			if ( $page->getNum() > $last ) {
100
				$last = $page->getNum();
101
			}
102
		}
103
104
		return $last;
105
	}
106
107
	/**
108
	 * Really creates the page WP:A/Riconferma_annuale/USERNAME/XXX
109
	 *
110
	 * @param string $title
111
	 * @param User $user
112
	 */
113
	protected function createPage( string $title, User $user ) {
114
		$this->getLogger()->info( "Creating page $title" );
115
		$groups = $user->getGroups();
116
		$textParams = [
117
			'$user' => $user->getName(),
118
			'$date' => $groups['sysop'],
119
			'$burocrate' => $groups['bureaucrat'] ?? '',
120
			'$checkuser' => $groups['checkuser'] ?? ''
121
		];
122
123
		$params = [
124
			'title' => $title,
125
			'text' => $this->msg( 'ric-page-text' )->params( $textParams )->text(),
126
			'summary' => $this->msg( 'ric-page-summary' )->text()
127
		];
128
129
		$this->getWiki()->editPage( $params );
130
	}
131
132
	/**
133
	 * Creates the page WP:A/Riconferma_annuale/USERNAME if it doesn't exist
134
	 *
135
	 * @param Page $basePage
136
	 * @param string $newText
137
	 */
138
	protected function createBasePage( Page $basePage, string $newText ) {
139
		$this->getLogger()->info( "Creating base page $basePage" );
140
141
		$params = [
142
			'text' => $newText,
143
			'summary' => $this->msg( 'base-page-summary' )->text()
144
		];
145
146
		$basePage->edit( $params );
147
	}
148
149
	/**
150
	 * Updates the page WP:A/Riconferma_annuale/USERNAME if it already exists
151
	 * @param Page $basePage
152
	 * @param string $newText
153
	 */
154
	protected function updateBasePage( Page $basePage, string $newText ) {
155
		$this->getLogger()->info( "Updating base page $basePage" );
156
157
		$params = [
158
			'appendtext' => "\n$newText",
159
			'summary' => $this->msg( 'base-page-summary-update' )->text()
160
		];
161
162
		$basePage->edit( $params );
163
	}
164
}
165