PageBotList::getUserInfo()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme\Wiki\Page;
4
5
use BotRiconferme\Wiki\UserInfo;
6
use BotRiconferme\Wiki\Wiki;
7
use DateTime;
8
9
/**
10
 * Singleton class representing the JSON list of admins
11
 */
12
class PageBotList extends Page {
13
	/** @var UserInfo[]|null */
14
	private $adminsList;
15
16
	/**
17
	 * Use self::get() instead
18
	 * @param string $listTitle
19
	 * @param Wiki $wiki
20
	 */
21
	private function __construct( string $listTitle, Wiki $wiki ) {
22
		parent::__construct( $listTitle, $wiki );
23
	}
24
25
	/**
26
	 * Instance getter
27
	 *
28
	 * @param Wiki $wiki
29
	 * @param string $listTitle
30
	 * @return self
31
	 */
32
	public static function get( Wiki $wiki, string $listTitle ): self {
33
		static $instance = null;
34
		if ( $instance === null ) {
35
			$instance = new self( $listTitle, $wiki );
36
		}
37
		return $instance;
38
	}
39
40
	/**
41
	 * @param UserInfo $ui
42
	 * @return int|null
43
	 */
44
	public function getOverrideTimestamp( UserInfo $ui ): ?int {
45
		$info = $ui->getInfo();
46
		if ( !array_intersect_key( $info, [ 'override-perm' => true, 'override' => true ] ) ) {
47
			return null;
48
		}
49
50
		// A one-time override takes precedence
51
		if ( array_key_exists( 'override', $info ) ) {
52
			$date = $info['override'];
53
		} else {
54
			$date = $info['override-prem'] . '/' . date( 'Y' );
55
		}
56
		return DateTime::createFromFormat( 'd/m/Y', $date )->getTimestamp();
57
	}
58
59
	/**
60
	 * Get the next valid timestamp for the given user
61
	 *
62
	 * @param string $user
63
	 * @return int
64
	 * @suppress PhanPluginComparisonObjectOrdering DateTime objects can be compared (phan issue #2907)
65
	 */
66
	public function getNextTimestamp( string $user ): int {
67
		$userInfo = $this->getUserInfo( $user )->getInfo();
68
		$now = new DateTime();
69
		if ( isset( $userInfo['override-perm'] ) ) {
70
			$date = DateTime::createFromFormat(
71
				'd/m/Y',
72
				$userInfo['override-perm'] . '/' . date( 'Y' )
73
			);
74
		} else {
75
			$date = null;
76
			if ( isset( $userInfo['override'] ) ) {
77
				$date = DateTime::createFromFormat( 'd/m/Y', $userInfo['override'] );
78
			}
79
			if ( !$date || $date <= $now ) {
80
				$ts = self::getValidFlagTimestamp( $userInfo );
81
				$date = ( new DateTime )->setTimestamp( $ts );
82
				$date->modify( '+1 year' );
83
			}
84
		}
85
		// @phan-suppress-next-line PhanPossiblyInfiniteLoop
86
		while ( $date <= $now ) {
87
			$date->modify( '+1 year' );
88
		}
89
		return $date->getTimestamp();
90
	}
91
92
	/**
93
	 * Get the valid timestamp for the given groups
94
	 *
95
	 * @param string[] $groups
96
	 * @return int
97
	 */
98
	public static function getValidFlagTimestamp( array $groups ): int {
99
		$checkuser = isset( $groups['checkuser'] ) ?
100
			DateTime::createFromFormat( 'd/m/Y', $groups['checkuser'] )->getTimestamp() :
101
			0;
102
		$bureaucrat = isset( $groups['bureaucrat'] ) ?
103
			DateTime::createFromFormat( 'd/m/Y', $groups['bureaucrat'] )->getTimestamp() :
104
			0;
105
106
		$timestamp = max( $bureaucrat, $checkuser );
107
		if ( $timestamp === 0 ) {
108
			$timestamp = DateTime::createFromFormat( 'd/m/Y', $groups['sysop'] )->getTimestamp();
109
		}
110
		return $timestamp;
111
	}
112
113
	/**
114
	 * An override is considered expired if:
115
	 * - The override date has passed (that's the point of having an override), AND
116
	 * - The "normal" date has passed (otherwise we'd use two different dates for the same year)
117
	 * For decreased risk, we add an additional delay of 3 days.
118
	 *
119
	 * @param string[] $groups
120
	 * @return bool
121
	 */
122
	public static function isOverrideExpired( array $groups ): bool {
123
		if ( !isset( $groups['override'] ) ) {
124
			return false;
125
		}
126
127
		$flagTS = self::getValidFlagTimestamp( $groups );
128
		$usualTS = strtotime( date( 'Y' ) . '-' . date( 'm-d', $flagTS ) );
129
		$overrideTS = DateTime::createFromFormat( 'd/m/Y', $groups['override'] )->getTimestamp();
130
		$delay = 60 * 60 * 24 * 3;
131
132
		return time() > $usualTS + $delay && time() > $overrideTS + $delay;
133
	}
134
135
	/**
136
	 * Get the actual list of admins
137
	 *
138
	 * @return UserInfo[]
139
	 */
140
	public function getAdminsList(): array {
141
		if ( $this->adminsList === null ) {
142
			$this->adminsList = [];
143
			foreach ( $this->getDecodedContent() as $user => $info ) {
144
				$this->adminsList[ $user ] = new UserInfo( $user, $info );
145
			}
146
		}
147
		return $this->adminsList;
148
	}
149
150
	/**
151
	 * @param string $user
152
	 * @return UserInfo
153
	 */
154
	public function getUserInfo( string $user ): UserInfo {
155
		return $this->getAdminsList()[$user];
156
	}
157
158
	/**
159
	 * Get the JSON-decoded content of the list
160
	 *
161
	 * @return array[]
162
	 * @phan-return array<string,array{sysop:string,checkuser?:string,bureaucrat?:string,override?:string,override-perm?:string,aliases?:list<string>}>
163
	 */
164
	public function getDecodedContent(): array {
165
		return json_decode( $this->getContent(), true );
166
	}
167
}
168