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