1
|
|
|
<?php declare( strict_types=1 ); |
2
|
|
|
|
3
|
|
|
namespace BotRiconferme\Wiki; |
4
|
|
|
|
5
|
|
|
use BotRiconferme\Config; |
6
|
|
|
use BotRiconferme\Exception\MissingPageException; |
7
|
|
|
use BotRiconferme\Wiki\Page\Page; |
8
|
|
|
use BotRiconferme\Wiki\Page\PageBotList; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class representing a single user. NOTE: this can only represent users stored in the JSON list |
12
|
|
|
*/ |
13
|
|
|
class User extends Element { |
14
|
|
|
/** @var string */ |
15
|
|
|
private $name; |
16
|
|
|
/** @var string[]|null Info contained in the JSON page */ |
17
|
|
|
private $info; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string $name |
21
|
|
|
* @param Wiki $wiki |
22
|
|
|
*/ |
23
|
|
|
public function __construct( string $name, Wiki $wiki ) { |
24
|
|
|
parent::__construct( $wiki ); |
25
|
|
|
$this->name = $name; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
public function getName() : string { |
32
|
|
|
return $this->name; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get a list of groups this user belongs to |
37
|
|
|
* |
38
|
|
|
* @return string[] |
39
|
|
|
*/ |
40
|
|
|
public function getGroups() : array { |
41
|
|
|
return array_diff( array_keys( $this->getUserInfo() ), PageBotList::NON_GROUP_KEYS ); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Like getGroups(), but includes flag dates. |
46
|
|
|
* |
47
|
|
|
* @return string[] [ group => date ] |
48
|
|
|
*/ |
49
|
|
|
public function getGroupsWithDates() : array { |
50
|
|
|
return array_intersect_key( $this->getUserInfo(), array_fill_keys( $this->getGroups(), 1 ) ); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get some info about this user, including flag dates. |
55
|
|
|
* |
56
|
|
|
* @return string[] |
57
|
|
|
*/ |
58
|
|
|
public function getUserInfo() : array { |
59
|
|
|
if ( $this->info === null ) { |
60
|
|
|
$usersList = PageBotList::get( $this->wiki )->getAdminsList(); |
61
|
|
|
$this->info = $usersList[ $this->name ]->getUserInfo(); |
62
|
|
|
} |
63
|
|
|
return $this->info; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param array|null $info |
68
|
|
|
*/ |
69
|
|
|
public function setInfo( ?array $info ) : void { |
70
|
|
|
$this->info = $info; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Whether the user is in the given group |
75
|
|
|
* |
76
|
|
|
* @param string $groupName |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
public function inGroup( string $groupName ) : bool { |
80
|
|
|
return in_array( $groupName, $this->getGroups() ); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns a regex for matching the name of this user |
85
|
|
|
* |
86
|
|
|
* @inheritDoc |
87
|
|
|
*/ |
88
|
|
|
public function getRegex() : string { |
89
|
|
|
$bits = $this->getAliases(); |
90
|
|
|
$bits[] = $this->name; |
91
|
|
|
$regexify = function ( $el ) { |
92
|
|
|
return str_replace( ' ', '[ _]', preg_quote( $el ) ); |
93
|
|
|
}; |
94
|
|
|
return '(?:' . implode( '|', array_map( $regexify, $bits ) ) . ')'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get a list of aliases for this user. |
99
|
|
|
* |
100
|
|
|
* @return string[] |
101
|
|
|
*/ |
102
|
|
|
public function getAliases() : array { |
103
|
|
|
return $this->getUserInfo()['aliases'] ?? []; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return Page |
108
|
|
|
*/ |
109
|
|
|
public function getTalkPage() : Page { |
110
|
|
|
return new Page( "User talk:{$this->name}", $this->wiki ); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get the default base page, e.g. WP:A/Riconferma annuale/XXX |
115
|
|
|
* @return Page |
116
|
|
|
*/ |
117
|
|
|
public function getBasePage() : Page { |
118
|
|
|
$prefix = Config::getInstance()->get( 'main-page-title' ); |
119
|
|
|
return new Page( "$prefix/$this", $this->wiki ); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get an *existing* base page for this user. If no existing page is found, this will throw. |
124
|
|
|
* Don't use this method if the page is allowed not to exist. |
125
|
|
|
* |
126
|
|
|
* @throws MissingPageException |
127
|
|
|
* @return Page |
128
|
|
|
*/ |
129
|
|
|
public function getExistingBasePage() : Page { |
130
|
|
|
$basePage = $this->getBasePage(); |
131
|
|
|
if ( !$basePage->exists() ) { |
132
|
|
|
$basePage = null; |
133
|
|
|
$prefix = Config::getInstance()->get( 'main-page-title' ); |
134
|
|
|
foreach ( $this->getAliases() as $alias ) { |
135
|
|
|
$altTitle = "$prefix/$alias"; |
136
|
|
|
$altPage = new Page( $altTitle, $this->wiki ); |
137
|
|
|
if ( $altPage->exists() ) { |
138
|
|
|
$basePage = $altPage; |
139
|
|
|
break; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
if ( $basePage === null ) { |
143
|
|
|
// We've tried hard enough. |
144
|
|
|
throw new MissingPageException( "Couldn't find base page for $this" ); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
return $basePage; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
public function __toString() { |
154
|
|
|
return $this->name; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|