Passed
Push — master ( e9dfe5...fa6a28 )
by MusikAnimal
05:21
created

User::isAnon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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