| Total Complexity | 24 |
| Total Lines | 225 |
| Duplicated Lines | 0 % |
| Coverage | 83.87% |
| Changes | 0 | ||
| 1 | <?php |
||
| 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 | 55 | public function __construct($username) |
|
| 34 | { |
||
| 35 | 55 | $this->username = ucfirst(str_replace('_', ' ', trim($username))); |
|
| 36 | 55 | } |
|
| 37 | |||
| 38 | /** |
||
| 39 | * Get the username. |
||
| 40 | * @return string |
||
| 41 | */ |
||
| 42 | 16 | public function getUsername() |
|
| 43 | { |
||
| 44 | 16 | 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) |
|
| 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) |
|
| 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( |
|
|
1 ignored issue
–
show
|
|||
| 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(); |
|
|
1 ignored issue
–
show
|
|||
| 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()); |
|
|
1 ignored issue
–
show
|
|||
| 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); |
||
|
1 ignored issue
–
show
|
|||
| 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) |
|
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Is this user an anonymous user (IP)? |
||
| 158 | * @return bool |
||
| 159 | */ |
||
| 160 | 4 | public function isAnon() |
|
| 161 | { |
||
| 162 | 4 | 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) |
|
| 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 | public function countEdits(Project $project, $namespace = 'all', $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() |
|
| 241 |