/
Mentor.php
236 lines (212 loc) · 6.3 KB
/
Mentor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
namespace GrowthExperiments;
use ConfigException;
use DeferredUpdates;
use IContextSource;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MediaWikiServices;
use MWException;
use ParserOptions;
use Title;
use User;
use UserArray;
use WikiPage;
class Mentor {
const MENTOR_PREF = 'growthexperiments-mentor-id';
const INTRO_TEXT_LENGTH = 240;
/**
* @var User
*/
private $mentorUser;
private function __construct( $mentorUser ) {
$this->mentorUser = $mentorUser;
}
/**
* @param User $mentee
* @param bool $allowSelect Enable selecting a mentor for the user
* @return bool|Mentor
* @throws ConfigException
*/
public static function newFromMentee( User $mentee, $allowSelect = false ) {
$mentorUser = self::loadMentor( $mentee );
if ( $mentorUser ) {
return new static( $mentorUser );
}
if ( $allowSelect ) {
$mentorUser = self::selectMentor( $mentee );
if ( $mentorUser ) {
return new static( $mentorUser );
}
}
return false;
}
/**
* @return User
*/
public function getMentorUser() {
return $this->mentorUser;
}
/**
* Returns all mentors that can be assigned to users
*
* @return string[] Mentors that can be assigned
*/
public static function getMentors() {
$logger = LoggerFactory::getInstance( 'GrowthExperiments' );
$config = MediaWikiServices::getInstance()->getMainConfig();
$mentorsPageName = $config->get( 'GEHomepageMentorsList' );
$title = Title::newFromText( $mentorsPageName );
if ( !$title || !$title->exists() ) {
throw new WikiConfigException( 'wgGEHomepageMentorsList is invalid: ' . $mentorsPageName );
}
$page = WikiPage::factory( $title );
$links = $page->getParserOutput( ParserOptions::newCanonical( 'canonical' ) )->getLinks();
if ( !isset( $links[ NS_USER ] ) ) {
$logger->info( __METHOD__ . ' found zero mentors, no links at {mentorsList}', [
'mentorsList' => $mentorsPageName
] );
return [];
}
$mentorsRaw = array_keys( $links[ NS_USER ] );
for ( $i = 0; $i < count( $mentorsRaw ); $i++ ) {
$canonical = User::getCanonicalName( $mentorsRaw[$i] );
if ( $canonical === false ) {
continue;
}
$mentorsRaw[$i] = $canonical;
}
$userArr = UserArray::newFromNames( $mentorsRaw );
$mentors = [];
foreach ( $userArr as $user ) {
if ( $user->getId() ) {
$mentors[] = $user->getTitleKey();
}
}
return $mentors;
}
/**
* Randomly selects a mentor from a list on a wiki page.
*
* @param User $mentee
* @param User[] $excluded
* @return User The selected mentor
* @throws ConfigException
* @throws MWException
*/
private static function selectMentor( User $mentee, array $excluded = [] ) {
$possibleMentors = self::getMentors();
if ( count( $possibleMentors ) === 0 ) {
throw new MWException(
'Homepage Mentorship module: no mentor available for user ' . $mentee->getName()
);
}
$possibleMentors = array_values( array_diff( $possibleMentors, [ $mentee->getTitleKey() ] ) );
if ( count( $possibleMentors ) === 0 ) {
throw new WikiConfigException(
'Homepage Mentorship module: no mentor available for ' .
$mentee->getName() .
' but themselves'
);
}
$titleKeys = [];
foreach ( $excluded as $user ) {
$titleKeys[] = $user->getTitleKey();
}
$possibleMentors = array_values( array_diff( $possibleMentors, $titleKeys ) );
if ( count( $possibleMentors ) === 0 ) {
throw new MWException(
'Homepage Mentorship module: no mentor available for ' .
$mentee->getName() .
' but excluded users'
);
}
$selectedMentorName = $possibleMentors[ rand( 0, count( $possibleMentors ) - 1 ) ];
$selectedMentor = User::newFromName( $selectedMentorName );
return $selectedMentor;
}
/**
* @param User $mentee
* @return bool|User The current user's mentor or false if they don't have one
*/
private static function loadMentor( User $mentee ) {
$mentorId = $mentee->getIntOption( self::MENTOR_PREF );
return User::whoIs( $mentorId ) ?
User::newFromId( $mentorId ) :
false;
}
/**
* Saves the given $mentor in the $mentee's preferences
*
* @param User $mentee
* @param User $mentor
*/
public static function saveMentor( User $mentee, User $mentor ) {
DeferredUpdates::addCallableUpdate( function () use ( $mentee, $mentor ) {
$user = User::newFromId( $mentee->getId() );
$user->setOption( Mentor::MENTOR_PREF, $mentor->getId() );
$user->saveSettings();
} );
}
/**
* Returns the custom introduction text for a mentor or falls back to a default text
* @param IContextSource $context
* @return mixed|string
* @throws ConfigException
* @throws MWException
*/
public function getIntroText( IContextSource $context ) {
$introText = $this->getCustomMentorIntroText( $context );
if ( $introText === '' ) {
$introText = $this->getDefaultMentorIntroText( $context );
}
return $introText;
}
/**
* @param IContextSource $context
* @return string
* @throws ConfigException
* @throws MWException
*/
private function getCustomMentorIntroText( IContextSource $context ) {
// Use \h (horizontal whitespace) instead of \s (whitespace) to avoid matching newlines (T227535)
preg_match(
sprintf( '/:%s]]\h*\|\h*(.*)/', preg_quote( $this->getMentorUser()->getName(), '/' ) ),
$this->getMentorsPageContent( $context ),
$matches
);
$introText = $matches[1] ?? '';
if ( $introText === '' ) {
return '';
}
return $context->msg( 'quotation-marks' )
->rawParams( $context->getLanguage()->truncateForVisual( $introText, self::INTRO_TEXT_LENGTH ) )
->text();
}
/**
* @param IContextSource $context
* @return string
*/
private function getDefaultMentorIntroText( IContextSource $context ) {
return $context
->msg( 'growthexperiments-homepage-mentorship-intro' )
->params( $this->getMentorUser()->getName() )
->params( $context->getUser()->getName() )
->text();
}
/**
* @param IContextSource $context
* @return string
* @throws ConfigException
* @throws MWException
*/
private function getMentorsPageContent( IContextSource $context ) {
$config = $context->getConfig();
$mentorsPageName = $config->get( 'GEHomepageMentorsList' );
$title = Title::newFromText( $mentorsPageName );
$page = WikiPage::factory( $title );
/** @var $content \WikitextContent */
$content = $page->getContent();
// @phan-suppress-next-line PhanUndeclaredMethod
return $content->getText();
}
}