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