Passed
Push — master ( b237e3...69f046 )
by Daimona
01:32
created

CreatePages::run()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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