|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file contains only the User class. |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
declare(strict_types = 1); |
|
7
|
|
|
|
|
8
|
|
|
namespace AppBundle\Model; |
|
9
|
|
|
|
|
10
|
|
|
use DateTime; |
|
11
|
|
|
use Exception; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* A User is a wiki user who has the same username across all projects in an XTools installation. |
|
15
|
|
|
*/ |
|
16
|
|
|
class User extends Model |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var string The user's username. */ |
|
19
|
|
|
protected $username; |
|
20
|
|
|
|
|
21
|
|
|
/** @var int Quick cache of edit counts, keyed by project domain. */ |
|
22
|
|
|
protected $editCounts = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Create a new User given a username. |
|
26
|
|
|
* @param string $username |
|
27
|
|
|
*/ |
|
28
|
62 |
|
public function __construct(string $username) |
|
29
|
|
|
{ |
|
30
|
62 |
|
$this->username = ucfirst(str_replace('_', ' ', trim($username))); |
|
31
|
|
|
|
|
32
|
|
|
// IPv6 address are stored as uppercase in the database. |
|
33
|
62 |
|
if ($this->isAnon()) { |
|
34
|
6 |
|
$this->username = strtoupper($this->username); |
|
35
|
|
|
} |
|
36
|
62 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Get the username. |
|
40
|
|
|
* @return string |
|
41
|
|
|
*/ |
|
42
|
16 |
|
public function getUsername(): string |
|
43
|
|
|
{ |
|
44
|
16 |
|
return $this->username; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Unique identifier for this User, to be used in cache keys. Use of md5 ensures the cache key does not contain |
|
49
|
|
|
* reserved characters. You could also use the ID, but that may require an unnecessary DB query. |
|
50
|
|
|
* @see Repository::getCacheKey() |
|
51
|
|
|
* @return string |
|
52
|
|
|
*/ |
|
53
|
1 |
|
public function getCacheKey(): string |
|
54
|
|
|
{ |
|
55
|
1 |
|
return md5($this->username); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get the user's ID on the given project. |
|
60
|
|
|
* @param Project $project |
|
61
|
|
|
* @return int |
|
62
|
|
|
*/ |
|
63
|
1 |
|
public function getId(Project $project): int |
|
64
|
|
|
{ |
|
65
|
1 |
|
$ret = $this->getRepository()->getIdAndRegistration( |
|
|
|
|
|
|
66
|
1 |
|
$project->getDatabaseName(), |
|
67
|
1 |
|
$this->getUsername() |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
1 |
|
return (int)$ret['userId']; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get the user's actor ID on the given project. |
|
75
|
|
|
* @param Project $project |
|
76
|
|
|
* @return int |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getActorId(Project $project): int |
|
79
|
|
|
{ |
|
80
|
|
|
return (int)$this->getRepository()->getActorId( |
|
|
|
|
|
|
81
|
|
|
$project->getDatabaseName(), |
|
82
|
|
|
$this->getUsername() |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get the user's registration date on the given project. |
|
88
|
|
|
* @param Project $project |
|
89
|
|
|
* @return DateTime|null null if no registration date was found. |
|
90
|
|
|
*/ |
|
91
|
1 |
|
public function getRegistrationDate(Project $project): ?DateTime |
|
92
|
|
|
{ |
|
93
|
1 |
|
$ret = $this->getRepository()->getIdAndRegistration( |
|
94
|
1 |
|
$project->getDatabaseName(), |
|
95
|
1 |
|
$this->getUsername() |
|
96
|
|
|
); |
|
97
|
|
|
|
|
98
|
1 |
|
return null !== $ret['regDate'] |
|
99
|
1 |
|
? DateTime::createFromFormat('YmdHis', $ret['regDate']) |
|
100
|
1 |
|
: null; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Get a user's local user rights on the given Project. |
|
105
|
|
|
* @param Project $project |
|
106
|
|
|
* @return string[] |
|
107
|
|
|
*/ |
|
108
|
3 |
|
public function getUserRights(Project $project): array |
|
109
|
|
|
{ |
|
110
|
3 |
|
return $this->getRepository()->getUserRights($project, $this); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get a list of this user's global rights. |
|
115
|
|
|
* @param Project|null $project A project to query; if not provided, the default will be used. |
|
116
|
|
|
* @return string[] |
|
117
|
|
|
*/ |
|
118
|
1 |
|
public function getGlobalUserRights(?Project $project = null): array |
|
119
|
|
|
{ |
|
120
|
1 |
|
return $this->getRepository()->getGlobalUserRights($this->getUsername(), $project); |
|
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Get the user's (system) edit count. |
|
125
|
|
|
* @param Project $project |
|
126
|
|
|
* @return int |
|
127
|
|
|
*/ |
|
128
|
2 |
|
public function getEditCount(Project $project): int |
|
129
|
|
|
{ |
|
130
|
2 |
|
$domain = $project->getDomain(); |
|
131
|
2 |
|
if (isset($this->editCounts[$domain])) { |
|
132
|
1 |
|
return $this->editCounts[$domain]; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
2 |
|
$this->editCounts[$domain] = (int)$this->getRepository()->getEditCount( |
|
|
|
|
|
|
136
|
2 |
|
$project->getDatabaseName(), |
|
137
|
2 |
|
$this->getUsername() |
|
138
|
|
|
); |
|
139
|
|
|
|
|
140
|
2 |
|
return $this->editCounts[$domain]; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Maximum number of edits to process, based on configuration. |
|
145
|
|
|
* @return int |
|
146
|
|
|
*/ |
|
147
|
1 |
|
public function maxEdits(): int |
|
148
|
|
|
{ |
|
149
|
1 |
|
return $this->getRepository()->maxEdits(); |
|
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Does this user exist on the given project. |
|
154
|
|
|
* @param Project $project |
|
155
|
|
|
* @return bool |
|
156
|
|
|
*/ |
|
157
|
|
|
public function existsOnProject(Project $project): bool |
|
158
|
|
|
{ |
|
159
|
|
|
return $this->getId($project) > 0; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Is this user an Administrator on the given project? |
|
164
|
|
|
* @param Project $project The project. |
|
165
|
|
|
* @return bool |
|
166
|
|
|
*/ |
|
167
|
2 |
|
public function isAdmin(Project $project): bool |
|
168
|
|
|
{ |
|
169
|
2 |
|
return false !== array_search('sysop', $this->getUserRights($project)); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Is this user an anonymous user (IP)? |
|
174
|
|
|
* @return bool |
|
175
|
|
|
*/ |
|
176
|
62 |
|
public function isAnon(): bool |
|
177
|
|
|
{ |
|
178
|
62 |
|
return (bool)filter_var($this->username, FILTER_VALIDATE_IP); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Get the expiry of the current block on the user |
|
183
|
|
|
* @param Project $project The project. |
|
184
|
|
|
* @return DateTime|bool Expiry as DateTime, true if indefinite, or false if they are not blocked. |
|
185
|
|
|
*/ |
|
186
|
2 |
|
public function getBlockExpiry(Project $project) |
|
187
|
|
|
{ |
|
188
|
2 |
|
$expiry = $this->getRepository()->getBlockExpiry( |
|
|
|
|
|
|
189
|
2 |
|
$project->getDatabaseName(), |
|
190
|
2 |
|
$this->getUsername() |
|
191
|
|
|
); |
|
192
|
|
|
|
|
193
|
2 |
|
if ('infinity' === $expiry) { |
|
194
|
1 |
|
return true; |
|
195
|
1 |
|
} elseif (false === $expiry) { |
|
196
|
|
|
return false; |
|
197
|
|
|
} else { |
|
198
|
1 |
|
return new DateTime($expiry); |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Is this user currently blocked on the given project? |
|
204
|
|
|
* @param Project $project The project. |
|
205
|
|
|
* @return bool |
|
206
|
|
|
*/ |
|
207
|
1 |
|
public function isBlocked(Project $project): bool |
|
208
|
|
|
{ |
|
209
|
1 |
|
return false !== $this->getBlockExpiry($project); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Does the user have more edits than maximum amount allowed for processing? |
|
214
|
|
|
* @param Project $project |
|
215
|
|
|
* @return bool |
|
216
|
|
|
*/ |
|
217
|
1 |
|
public function hasTooManyEdits(Project $project): bool |
|
218
|
|
|
{ |
|
219
|
1 |
|
$editCount = $this->getEditCount($project); |
|
220
|
1 |
|
return $this->maxEdits() > 0 && $editCount > $this->maxEdits(); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Get edit count within given timeframe and namespace |
|
225
|
|
|
* @param Project $project |
|
226
|
|
|
* @param int|string $namespace Namespace ID or 'all' for all namespaces |
|
227
|
|
|
* @param string $start Start date in a format accepted by strtotime() |
|
228
|
|
|
* @param string $end End date in a format accepted by strtotime() |
|
229
|
|
|
* @return int |
|
230
|
|
|
*/ |
|
231
|
2 |
|
public function countEdits(Project $project, $namespace = 'all', $start = '', $end = ''): int |
|
232
|
|
|
{ |
|
233
|
2 |
|
return (int) $this->getRepository()->countEdits($project, $this, $namespace, $start, $end); |
|
|
|
|
|
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Is this user the same as the current XTools user? |
|
238
|
|
|
* @return bool |
|
239
|
|
|
*/ |
|
240
|
2 |
|
public function isCurrentlyLoggedIn(): bool |
|
241
|
|
|
{ |
|
242
|
|
|
try { |
|
243
|
2 |
|
$ident = $this->getRepository()->getXtoolsUserInfo(); |
|
|
|
|
|
|
244
|
2 |
|
} catch (Exception $exception) { |
|
245
|
2 |
|
return false; |
|
246
|
|
|
} |
|
247
|
|
|
return isset($ident->username) && $ident->username === $this->getUsername(); |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|