Passed
Push — master ( 35639c...18326f )
by Daimona
01:42
created

CreatePages::createPage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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