Passed
Push — master ( 1d252d...bd204f )
by Daimona
01:55
created

PageBotList::__construct()   A

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 2
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
8
/**
9
 * Singleton class representing the JSON list of admins
10
 */
11
class PageBotList extends Page {
12
	public const NON_GROUP_KEYS = [ 'override', 'override-perm', 'aliases' ];
13
14
	/** @var UserInfo[]|null */
15
	private $adminsList;
16
17
	/**
18
	 * @private Use self::get()
19
	 * @param string $listTitle
20
	 * @param Wiki $wiki
21
	 */
22
	public function __construct( string $listTitle, Wiki $wiki ) {
23
		parent::__construct( $listTitle, $wiki );
24
	}
25
26
	/**
27
	 * Instance getter
28
	 *
29
	 * @param Wiki $wiki
30
	 * @param string $listTitle
31
	 * @return self
32
	 */
33
	public static function get( Wiki $wiki, string $listTitle ) : self {
34
		static $instance = null;
35
		if ( $instance === null ) {
36
			$instance = new self( $listTitle, $wiki );
37
		}
38
		return $instance;
39
	}
40
41
	/**
42
	 * @param UserInfo $ui
43
	 * @return int|null
44
	 */
45
	public function getOverrideTimestamp( UserInfo $ui ) : ?int {
46
		$info = $ui->getInfo();
47
		if ( !array_intersect_key( $info, [ 'override-perm' => true, 'override' => true ] ) ) {
48
			return null;
49
		}
50
51
		// A one-time override takes precedence
52
		if ( array_key_exists( 'override', $info ) ) {
53
			$date = $info['override'];
54
		} else {
55
			$date = $info['override-prem'] . '/' . date( 'Y' );
56
		}
57
		return \DateTime::createFromFormat( 'd/m/Y', $date )->getTimestamp();
58
	}
59
60
	/**
61
	 * Get the next valid timestamp for the given user
62
	 *
63
	 * @param string $user
64
	 * @return int
65
	 */
66
	public function getNextTimestamp( string $user ) : int {
67
		$userInfo = $this->getUserInfo( $user )->getInfo();
68
		if ( isset( $userInfo['override-perm'] ) ) {
69
			$date = \DateTime::createFromFormat(
70
				'd/m/Y',
71
				$userInfo['override-perm'] . '/' . date( 'Y' )
72
			);
73
		} else {
74
			$date = null;
75
			if ( isset( $userInfo['override'] ) ) {
76
				$date = \DateTime::createFromFormat( 'd/m/Y', $userInfo['override'] );
77
			}
78
			if ( !$date || $date <= new \DateTime ) {
79
				$ts = self::getValidFlagTimestamp( $userInfo );
80
				$date = ( new \DateTime )->setTimestamp( $ts );
81
			}
82
		}
83
		while ( $date <= new \DateTime ) {
84
			$date->modify( '+1 year' );
85
		}
86
		return $date->getTimestamp();
87
	}
88
89
	/**
90
	 * Get the valid timestamp for the given groups
91
	 *
92
	 * @param array $groups
93
	 * @return int
94
	 */
95
	public static function getValidFlagTimestamp( array $groups ): int {
96
		$checkuser = isset( $groups['checkuser'] ) ?
97
			\DateTime::createFromFormat( 'd/m/Y', $groups['checkuser'] )->getTimestamp() :
98
			0;
99
		$bureaucrat = isset( $groups['bureaucrat'] ) ?
100
			\DateTime::createFromFormat( 'd/m/Y', $groups['bureaucrat'] )->getTimestamp() :
101
			0;
102
103
		$timestamp = max( $bureaucrat, $checkuser );
104
		if ( $timestamp === 0 ) {
105
			$timestamp = \DateTime::createFromFormat( 'd/m/Y', $groups['sysop'] )->getTimestamp();
106
		}
107
		return $timestamp;
108
	}
109
110
	/**
111
	 * @param array $groups
112
	 * @return bool
113
	 */
114
	public static function isOverrideExpired( array $groups ) : bool {
115
		if ( !isset( $groups['override'] ) ) {
116
			return false;
117
		}
118
119
		$flagTS = self::getValidFlagTimestamp( $groups );
120
		$usualTS = strtotime( date( 'Y' ) . '-' . date( 'm-d', $flagTS ) );
121
		$overrideTS = \DateTime::createFromFormat( 'd/m/Y', $groups['override'] )->getTimestamp();
122
		$delay = 60 * 60 * 24 * 3;
123
124
		return time() > $usualTS + $delay && time() > $overrideTS + $delay;
125
	}
126
127
	/**
128
	 * Get the actual list of admins
129
	 *
130
	 * @return UserInfo[]
131
	 */
132
	public function getAdminsList() : array {
133
		if ( $this->adminsList === null ) {
134
			$this->adminsList = [];
135
			foreach ( $this->getDecodedContent() as $user => $info ) {
136
				$this->adminsList[ $user ] = new UserInfo( $user, $info );
137
			}
138
		}
139
		return $this->adminsList;
140
	}
141
142
	/**
143
	 * @param string $user
144
	 * @return UserInfo
145
	 */
146
	public function getUserInfo( string $user ) : UserInfo {
147
		return $this->getAdminsList()[$user];
148
	}
149
150
	/**
151
	 * Get the JSON-decoded content of the list
152
	 *
153
	 * @return array[]
154
	 */
155
	public function getDecodedContent() : array {
156
		return json_decode( $this->getContent(), true );
157
	}
158
}
159