1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
<?php
namespace MediaWiki\User\TempUser;
use BadMethodCallException;
use InvalidArgumentException;
use MediaWiki\Permissions\Authority;
use MediaWiki\Utils\MWTimestamp;
use Wikimedia\Rdbms\IExpression;
use Wikimedia\Rdbms\IReadableDatabase;
/**
* The real TempUserConfig including internal methods used by TempUserCreator.
*
* @since 1.39
*/
class RealTempUserConfig implements TempUserConfig {
/** @var bool */
private $known = false;
/** @var bool */
private $enabled = false;
/** @var array */
private $serialProviderConfig = [];
/** @var array */
private $serialMappingConfig = [];
/** @var string[] */
private $autoCreateActions;
/** @var Pattern|null */
private $genPattern;
/** @var Pattern[]|null */
private $matchPatterns;
/** @var Pattern|null */
private $reservedPattern;
/** @var int|null */
private $expireAfterDays;
/** @var int|null */
private $notifyBeforeExpirationDays;
/**
* @param array $config See the documentation of $wgAutoCreateTempUser.
* - known: bool
* - enabled: bool
* - actions: array
* - genPattern: string
* - matchPattern: string|string[], optional
* - reservedPattern: string, optional
* - serialProvider: array
* - serialMapping: array
* - expireAfterDays: int, optional
* - notifyBeforeExpirationDays: int, optional
*/
public function __construct( $config ) {
$this->enabled = $config['enabled'] ?? false;
$this->known = $this->enabled || ( $config['known'] ?? false );
// Configuration related to creating new temporary accounts for some actions
if ( $this->enabled ) {
$this->autoCreateActions = $config['actions'];
$this->serialProviderConfig = $config['serialProvider'];
$this->serialMappingConfig = $config['serialMapping'];
}
// Configuration related to managing and identifying existing temporary accounts,
// regardless of whether new temp accounts are being actively created via the
// 'enabled' config flag.
if ( $this->known || $this->enabled ) {
$this->genPattern = new Pattern( 'genPattern', $config['genPattern'] );
$this->expireAfterDays = $config['expireAfterDays'] ?? null;
$this->notifyBeforeExpirationDays = $config['notifyBeforeExpirationDays'] ?? null;
if ( isset( $config['matchPattern'] ) ) {
$matchPatterns = $config['matchPattern'];
if ( !is_array( $config['matchPattern'] ) ) {
$matchPatterns = [ $matchPatterns ];
}
foreach ( $matchPatterns as &$pattern ) {
$pattern = new Pattern( 'matchPattern', $pattern );
}
$this->matchPatterns = $matchPatterns;
} else {
$this->matchPatterns = [ $this->genPattern ];
}
}
// Configuration that is set regardless of whether the feature is enabled or known.
if ( isset( $config['reservedPattern'] ) ) {
$this->reservedPattern = new Pattern( 'reservedPattern', $config['reservedPattern'] );
}
}
public function isEnabled() {
return $this->enabled;
}
public function isKnown() {
return $this->known;
}
public function isAutoCreateAction( string $action ) {
if ( $action === 'create' ) {
$action = 'edit';
}
return $this->isEnabled()
&& in_array( $action, $this->autoCreateActions, true );
}
public function shouldAutoCreate( Authority $authority, string $action ) {
return $this->isAutoCreateAction( $action )
&& !$authority->isRegistered()
&& $authority->isAllowed( 'createaccount' );
}
public function isTempName( string $name ) {
if ( !$this->isKnown() ) {
return false;
}
foreach ( $this->matchPatterns as $pattern ) {
if ( $pattern->isMatch( $name ) ) {
return true;
}
}
return false;
}
public function isReservedName( string $name ) {
return $this->isTempName( $name ) || ( $this->reservedPattern && $this->reservedPattern->isMatch( $name ) );
}
public function getPlaceholderName(): string {
$year = null;
if ( $this->serialProviderConfig['useYear'] ?? false ) {
$year = MWTimestamp::getInstance()->format( 'Y' );
}
if ( $this->isEnabled() ) {
return $this->genPattern->generate( '*', $year );
} else {
throw new BadMethodCallException( __METHOD__ . ' is disabled' );
}
}
/**
* @deprecated since 1.42.
*/
public function getMatchPattern(): Pattern {
wfDeprecated( __METHOD__, '1.42' );
if ( $this->isKnown() ) {
// This method is deprecated to allow time for callers to update.
// This method only returns one Pattern, so just return the first one.
return $this->getMatchPatterns()[0];
} else {
throw new BadMethodCallException( __METHOD__ . ' is disabled' );
}
}
public function getMatchPatterns(): array {
if ( $this->isKnown() ) {
return $this->matchPatterns;
} else {
throw new BadMethodCallException( __METHOD__ . ' is disabled' );
}
}
public function getMatchCondition( IReadableDatabase $db, string $field, string $op ): IExpression {
if ( $this->isKnown() ) {
$exprs = [];
foreach ( $this->getMatchPatterns() as $pattern ) {
$exprs[] = $db->expr( $field, $op, $pattern->toLikeValue( $db ) );
}
if ( count( $exprs ) === 1 ) {
return $exprs[0];
}
if ( $op === IExpression::LIKE ) {
return $db->orExpr( $exprs );
} elseif ( $op === IExpression::NOT_LIKE ) {
return $db->andExpr( $exprs );
} else {
throw new InvalidArgumentException( "Invalid operator $op" );
}
} else {
throw new BadMethodCallException( __METHOD__ . ' is disabled' );
}
}
public function getExpireAfterDays(): ?int {
return $this->expireAfterDays;
}
public function getNotifyBeforeExpirationDays(): ?int {
return $this->notifyBeforeExpirationDays;
}
/**
* @internal For TempUserCreator only
* @return Pattern
*/
public function getGeneratorPattern(): Pattern {
if ( $this->isEnabled() ) {
return $this->genPattern;
} else {
throw new BadMethodCallException( __METHOD__ . ' is disabled' );
}
}
/**
* @internal For TempUserCreator only
* @return array
*/
public function getSerialProviderConfig(): array {
return $this->serialProviderConfig;
}
/**
* @internal For TempUserCreator only
* @return array
*/
public function getSerialMappingConfig(): array {
return $this->serialMappingConfig;
}
}
|