| Total Complexity | 28 |
| Total Lines | 310 |
| Duplicated Lines | 0 % |
| Coverage | 83.52% |
| 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 | 52 | public function __construct($username) |
|
| 34 | { |
||
| 35 | 52 | $this->username = ucfirst(str_replace('_', ' ', trim($username))); |
|
| 36 | 52 | } |
|
| 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) |
|
| 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 | 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) |
|
| 172 | { |
||
| 173 | 2 | if (isset($this->blockExpiry)) { |
|
| 174 | return $this->blockExpiry; |
||
| 175 | } |
||
| 176 | |||
| 177 | 2 | $expiry = $this->getRepository()->getBlockExpiry( |
|
|
1 ignored issue
–
show
|
|||
| 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 | public function countEdits(Project $project, $namespace = 'all', $start = '', $end = '') |
||
| 223 | { |
||
| 224 | return (int) $this->getRepository()->countEdits($project, $this, $namespace, $start, $end); |
||
|
1 ignored issue
–
show
|
|||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Get the number of edits this user made using semi-automated tools. |
||
| 229 | * @param Project $project |
||
| 230 | * @param string|int $namespace Namespace ID or 'all' |
||
| 231 | * @param string $start Start date in a format accepted by strtotime() |
||
| 232 | * @param string $end End date in a format accepted by strtotime() |
||
| 233 | * @return int Result of query, see below. |
||
| 234 | */ |
||
| 235 | public function countAutomatedEdits(Project $project, $namespace = 'all', $start = '', $end = '') |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Get non-automated contributions for this user. |
||
| 242 | * @param Project $project |
||
| 243 | * @param string|int $namespace Namespace ID or 'all' |
||
| 244 | * @param string $start Start date in a format accepted by strtotime() |
||
| 245 | * @param string $end End date in a format accepted by strtotime() |
||
| 246 | * @param int $offset Used for pagination, offset results by N edits |
||
| 247 | * @return array[] Result of query, with columns (string) 'full_page_title' including namespace, |
||
| 248 | * (string) 'page_title', (int) 'page_namespace', (int) 'rev_id', (DateTime) 'timestamp', |
||
| 249 | * (bool) 'minor', (int) 'length', (int) 'length_change', (string) 'comment' |
||
| 250 | */ |
||
| 251 | 1 | public function getNonAutomatedEdits( |
|
| 252 | Project $project, |
||
| 253 | $namespace = 'all', |
||
| 254 | $start = '', |
||
| 255 | $end = '', |
||
| 256 | $offset = 0 |
||
| 257 | ) { |
||
| 258 | 1 | $revs = $this->getRepository()->getNonAutomatedEdits( |
|
|
1 ignored issue
–
show
|
|||
| 259 | 1 | $project, |
|
| 260 | 1 | $this, |
|
| 261 | 1 | $namespace, |
|
| 262 | 1 | $start, |
|
| 263 | 1 | $end, |
|
| 264 | 1 | $offset |
|
| 265 | ); |
||
| 266 | |||
| 267 | 1 | $namespaces = $project->getNamespaces(); |
|
| 268 | |||
| 269 | 1 | return array_map(function ($rev) use ($namespaces) { |
|
| 270 | 1 | $pageTitle = $rev['page_title']; |
|
| 271 | 1 | $fullPageTitle = ''; |
|
| 272 | |||
| 273 | 1 | if ($rev['page_namespace'] !== '0') { |
|
| 274 | 1 | $fullPageTitle = $namespaces[$rev['page_namespace']] . ":$pageTitle"; |
|
| 275 | } else { |
||
| 276 | $fullPageTitle = $pageTitle; |
||
| 277 | } |
||
| 278 | |||
| 279 | return [ |
||
| 280 | 1 | 'full_page_title' => $fullPageTitle, |
|
| 281 | 1 | 'page_title' => $pageTitle, |
|
| 282 | 1 | 'page_namespace' => (int) $rev['page_namespace'], |
|
| 283 | 1 | 'rev_id' => (int) $rev['rev_id'], |
|
| 284 | 1 | 'timestamp' => DateTime::createFromFormat('YmdHis', $rev['timestamp']), |
|
| 285 | 1 | 'minor' => (bool) $rev['minor'], |
|
| 286 | 1 | 'length' => (int) $rev['length'], |
|
| 287 | 1 | 'length_change' => (int) $rev['length_change'], |
|
| 288 | 1 | 'comment' => $rev['comment'], |
|
| 289 | ]; |
||
| 290 | 1 | }, $revs); |
|
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Get counts of known automated tools used by the given user. |
||
| 295 | * @param Project $project |
||
| 296 | * @param string|int $namespace Namespace ID or 'all' |
||
| 297 | * @param string $start Start date in a format accepted by strtotime() |
||
| 298 | * @param string $end End date in a format accepted by strtotime() |
||
| 299 | * @return string[] Each tool that they used along with the count and link: |
||
| 300 | * [ |
||
| 301 | * 'Twinkle' => [ |
||
| 302 | * 'count' => 50, |
||
| 303 | * 'link' => 'Wikipedia:Twinkle', |
||
| 304 | * ], |
||
| 305 | * ] |
||
| 306 | */ |
||
| 307 | public function getAutomatedCounts(Project $project, $namespace = 'all', $start = '', $end = '') |
||
| 308 | { |
||
| 309 | return $this->getRepository()->getAutomatedCounts($project, $this, $namespace, $start, $end); |
||
|
1 ignored issue
–
show
|
|||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Is this user the same as the current XTools user? |
||
| 314 | * @return bool |
||
| 315 | */ |
||
| 316 | 2 | public function isCurrentlyLoggedIn() |
|
| 324 | } |
||
| 325 | } |
||
| 326 |