Passed
Push — master ( 585e03...f7d4e3 )
by MusikAnimal
05:22
created

User::getGlobalGroups()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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