|
1
|
|
|
<?php declare( strict_types=1 ); |
|
2
|
|
|
|
|
3
|
|
|
namespace BotRiconferme\Task\Subtask; |
|
4
|
|
|
|
|
5
|
|
|
use BotRiconferme\Page\PageRiconferma; |
|
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 Subtask { |
|
13
|
|
|
/** |
|
14
|
|
|
* @inheritDoc |
|
15
|
|
|
*/ |
|
16
|
|
|
public function runInternal() : int { |
|
17
|
|
|
$users = $this->getDataProvider()->getUsersToProcess(); |
|
18
|
|
|
|
|
19
|
|
|
if ( !$users ) { |
|
20
|
|
|
return self::STATUS_NOTHING; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
foreach ( $users as $user => $groups ) { |
|
24
|
|
|
$this->processUser( $user, $groups ); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
return self::STATUS_GOOD ; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Determine what pages we need to create for a single user. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $user |
|
34
|
|
|
* @param array $groups |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function processUser( string $user, array $groups ) { |
|
37
|
|
|
try { |
|
38
|
|
|
$num = $this->getLastPageNum( $user ) + 1; |
|
39
|
|
|
} catch ( TaskException $e ) { |
|
40
|
|
|
// The page was already created today. PLZ let this poor bot work! |
|
41
|
|
|
$this->getDataProvider()->removeUser( $user ); |
|
42
|
|
|
$this->getLogger()->warning( $e->getMessage() . "\nRemoving $user." ); |
|
43
|
|
|
return; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$baseTitle = $this->getConfig()->get( 'ric-main-page' ) . "/$user"; |
|
47
|
|
|
$pageTitle = "$baseTitle/$num"; |
|
48
|
|
|
$this->createPage( $pageTitle, $user, $groups ); |
|
49
|
|
|
|
|
50
|
|
|
$newText = $this->msg( 'ric-base-page-text' )->params( [ '$title' => $pageTitle ] )->text(); |
|
51
|
|
|
if ( $num === 1 ) { |
|
52
|
|
|
$this->createBasePage( $baseTitle, $newText ); |
|
53
|
|
|
} else { |
|
54
|
|
|
$this->updateBasePage( $baseTitle, $newText ); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$pageObj = new PageRiconferma( $pageTitle ); |
|
58
|
|
|
$this->getDataProvider()->addCreatedPages( $pageObj ); |
|
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
|
|
|
|