Passed
Push — master ( dcfba0...0e0426 )
by MusikAnimal
04:50
created

User::countEdits()   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 4
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 63
    public function __construct($username)
34
    {
35 63
        $this->username = ucfirst(str_replace('_', ' ', trim($username)));
36 63
    }
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 the user's (system) edit count.
86
     * @param Project $project
87
     * @return int
88
     */
89 2
    public function getEditCount(Project $project)
90
    {
91 2
        $domain = $project->getDomain();
92 2
        if (isset($this->editCounts[$domain])) {
93 1
            return $this->editCounts[$domain];
94
        }
95
96 2
        $this->editCounts[$domain] = (int) $this->getRepository()->getEditCount(
97 2
            $project->getDatabaseName(),
98 2
            $this->getUsername()
99
        );
100
101 2
        return $this->editCounts[$domain];
102
    }
103
104
    /**
105
     * Maximum number of edits to process, based on configuration.
106
     * @return int
107
     */
108 1
    public function maxEdits()
109
    {
110 1
        return $this->getRepository()->maxEdits();
111
    }
112
113
    /**
114
     * Get a list of this user's groups on the given project.
115
     * @param Project $project The project.
116
     * @return string[]
117
     */
118 2
    public function getGroups(Project $project)
119
    {
120 2
        $groupsData = $this->getRepository()->getGroups($project, $this->getUsername());
121 2
        $groups = preg_grep('/\*/', $groupsData, PREG_GREP_INVERT);
122 2
        sort($groups);
123 2
        return $groups;
124
    }
125
126
    /**
127
     * Get a list of this user's groups on all projects.
128
     * @param Project $project A project to query; if not provided, the default will be used.
129
     */
130
    public function getGlobalGroups(Project $project = null)
131
    {
132
        return $this->getRepository()->getGlobalGroups($this->getUsername(), $project);
133
    }
134
135
    /**
136
     * Does this user exist on the given project.
137
     * @param Project $project
138
     * @return bool
139
     */
140
    public function existsOnProject(Project $project)
141
    {
142
        $id = $this->getId($project);
143
        return $id > 0;
144
    }
145
146
    /**
147
     * Is this user an Administrator on the given project?
148
     * @param Project $project The project.
149
     * @return bool
150
     */
151 2
    public function isAdmin(Project $project)
152
    {
153 2
        return (false !== array_search('sysop', $this->getGroups($project)));
154
    }
155
156
    /**
157
     * Is this user an anonymous user (IP)?
158
     * @return bool
159
     */
160 5
    public function isAnon()
161
    {
162 5
        return (bool) filter_var($this->username, FILTER_VALIDATE_IP);
163
    }
164
165
    /**
166
     * Get the expiry of the current block on the user
167
     * @param Project $project The project.
168
     * @return DateTime|bool Expiry as DateTime, true if indefinite,
169
     *                       or false if they are not blocked.
170
     */
171 2
    public function getBlockExpiry(Project $project)
172
    {
173 2
        if (isset($this->blockExpiry)) {
174
            return $this->blockExpiry;
175
        }
176
177 2
        $expiry = $this->getRepository()->getBlockExpiry(
178 2
            $project->getDatabaseName(),
179 2
            $this->getId($project)
180
        );
181
182 2
        if ($expiry === 'infinity') {
183 1
            $this->blockExpiry = true;
184 1
        } elseif ($expiry === false) {
185
            $this->blockExpiry = false;
186
        } else {
187 1
            $this->blockExpiry = new DateTime($expiry);
188
        }
189
190 2
        return $this->blockExpiry;
191
    }
192
193
    /**
194
     * Is this user currently blocked on the given project?
195
     * @param Project $project The project.
196
     * @return bool
197
     */
198 1
    public function isBlocked(Project $project)
199
    {
200 1
        return $this->getBlockExpiry($project) !== false;
201
    }
202
203
    /**
204
     * Does the user have more edits than maximum amount allowed for processing?
205
     * @param Project $project
206
     * @return bool
207
     */
208 1
    public function hasTooManyEdits(Project $project)
209
    {
210 1
        $editCount = $this->getEditCount($project);
211 1
        return $this->maxEdits() > 0 && $editCount > $this->maxEdits();
212
    }
213
214
    /**
215
     * Get edit count within given timeframe and namespace
216
     * @param Project $project
217
     * @param int|string $namespace Namespace ID or 'all' for all namespaces
218
     * @param string $start Start date in a format accepted by strtotime()
219
     * @param string $end End date in a format accepted by strtotime()
220
     * @return int
221
     */
222 1
    public function countEdits(Project $project, $namespace = 'all', $start = '', $end = '')
223
    {
224 1
        return (int) $this->getRepository()->countEdits($project, $this, $namespace, $start, $end);
225
    }
226
227
    /**
228
     * Is this user the same as the current XTools user?
229
     * @return bool
230
     */
231 2
    public function isCurrentlyLoggedIn()
232
    {
233
        try {
234 2
            $ident = $this->getRepository()->getXtoolsUserInfo();
235 2
        } catch (Exception $exception) {
236 2
            return false;
237
        }
238
        return isset($ident->username) && $ident->username === $this->getUsername();
239
    }
240
}
241