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