Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add 2024 Universal Code of Conduct Coordinating Committee elections
- Loading branch information
1 parent
8204a79
commit e33caa9
Showing
3 changed files
with
139 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
tool-labs/accounteligibility/framework/rules/AccountAgeRule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
/** | ||
* A rule which checks that the account is at least the given number of days old. | ||
*/ | ||
class AccountAgeRule implements Rule | ||
{ | ||
########## | ||
## Properties | ||
########## | ||
/** | ||
* The minimum number of days for which the account must be registered. | ||
* @var int | ||
*/ | ||
private $minDaysOld; | ||
|
||
/** | ||
* The maximum date up to which to count the age, or null for no maximum. | ||
* @var DateWrapper|null | ||
*/ | ||
private $maxDate; | ||
|
||
|
||
########## | ||
## Public methods | ||
########## | ||
/** | ||
* Construct an instance. | ||
* @param int $minDaysOld The minimum number of days for which the account must be registered. | ||
* @param string|null $maxDate The maximum date up to which to count the age in a format recognised by {@see DateWrapper::__construct}, or null for no maximum. | ||
*/ | ||
public function __construct($minDaysOld, $maxDate = null) | ||
{ | ||
$this->minDaysOld = $minDaysOld; | ||
$this->maxDate = $maxDate ? new DateWrapper($maxDate) : null; | ||
} | ||
|
||
/** | ||
* Collect information from a wiki and return whether the rule has been met. | ||
* @param Toolserver $db The database wrapper. | ||
* @param Wiki $wiki The current wiki. | ||
* @param LocalUser $user The local user account. | ||
* @return ResultInfo|null The eligibility check result, or null if the rule doesn't apply to this wiki. | ||
*/ | ||
public function accumulate($db, $wiki, $user) | ||
{ | ||
// get result | ||
$daysOld = $this->getAccountAge($db, $user); | ||
$isOldEnough = is_null($daysOld) || $daysOld >= $this->minDaysOld; | ||
$result = $isOldEnough ? Result::PASS : Result::ACCUMULATING; | ||
|
||
// get message | ||
$message = | ||
($isOldEnough ? "was" : "was not") | ||
. " registered at least {$this->minDaysOld} days " | ||
. ($this->maxDate | ||
? "before {$this->maxDate->readable} " | ||
: "ago " | ||
) | ||
. (is_null($daysOld) | ||
? " (registered before 2005)" | ||
: ( | ||
" (registered {$daysOld} days " | ||
. ($this->maxDate ? "before" : "ago") | ||
. ")" | ||
) | ||
) | ||
. ($isOldEnough | ||
? "." | ||
: "..." | ||
); | ||
|
||
// build result | ||
return new ResultInfo($result, $message); | ||
} | ||
|
||
|
||
########## | ||
## Private methods | ||
########## | ||
/** | ||
* Get the local account age in days. | ||
* @param Toolserver $db The database wrapper. | ||
* @param LocalUser $user The local user account. | ||
* @return int|null Returns the number of days between the user's registration and the max (or current) date, or null if the user was registered before registration dates started being tracked in 2005. | ||
*/ | ||
private function getAccountAge($db, $user) | ||
{ | ||
$registered = $user->registered; | ||
if (!$user->registered) | ||
$registered = $db->getRegistrationDate($user->id, $user->actorID, "d F Y", true)[0]; | ||
|
||
if (!$registered) | ||
return null; // before 2005 | ||
|
||
$start = DateTime::createFromFormat('YmdHis', $registered); | ||
$end = $this->maxDate | ||
? $this->maxDate->date | ||
: new DateTime(); | ||
|
||
return $start->diff($end)->days; | ||
} | ||
} |