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