forked from wikimedia/mediawiki-extensions-UserMerge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpecialUserMerge.php
181 lines (161 loc) · 4.74 KB
/
SpecialUserMerge.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
<?php
/** \file
* \brief Contains code for the UserMerge Class (extends SpecialPage).
*/
/**
* Special page class for the User Merge and Delete extension
* allows sysops to merge references from one user to another user.
* It also supports deleting users following merge.
*
* @ingroup Extensions
* @author Tim Laqua <[email protected]>
* @author Thomas Gries <[email protected]>
* @author Matthew April <[email protected]>
*
*/
class SpecialUserMerge extends FormSpecialPage {
public function __construct() {
parent::__construct( 'UserMerge', 'usermerge' );
}
/**
* @return array
*/
protected function getFormFields() {
$us = $this;
return [
'olduser' => [
'type' => 'text',
'label-message' => 'usermerge-olduser',
'required' => true,
'validation-callback' => function ( $val ) use ( $us ) {
// only pass strings to User::newFromName
if ( !is_string( $val ) ) {
return true;
}
$key = $us->validateOldUser( $val );
if ( is_string( $key ) || is_array( $key ) ) {
return $us->msg( $key )->escaped();
}
return true;
},
],
'newuser' => [
'type' => 'text',
'required' => true,
'label-message' => 'usermerge-newuser',
'validation-callback' => function ( $val ) use ( $us ) {
// only pass strings to User::newFromName
if ( !is_string( $val ) ) {
return true;
}
$key = $us->validateNewUser( $val );
if ( is_string( $key ) ) {
return $us->msg( $key )->escaped();
}
return true;
},
],
'delete' => [
'type' => 'check',
'label-message' => 'usermerge-deleteolduser',
],
];
}
/**
* @param string $val user's input for username
* @return bool|string|string[] true if valid, a string or string[] of the error's message key
* if validation failed
*/
public function validateOldUser( $val ) {
global $wgUserMergeProtectedGroups;
$oldUser = User::newFromName( $val );
if ( !$oldUser || $oldUser->getId() === 0 ) {
return 'usermerge-badolduser';
}
if ( $this->getUser()->getId() === $oldUser->getId() ) {
return [ 'usermerge-noselfdelete', $this->getUser()->getName() ];
}
if ( count( array_intersect( $oldUser->getGroups(), $wgUserMergeProtectedGroups ) ) ) {
return [ 'usermerge-protectedgroup', $oldUser->getName() ];
}
return true;
}
/**
* @param string $val user's input for username
* @return bool|string true if valid, a string of the error's message key if validation failed
*/
public function validateNewUser( $val ) {
global $wgUserMergeEnableDelete;
if ( $wgUserMergeEnableDelete && $val === 'Anonymous' ) {
return true; // Special case
}
$newUser = User::newFromName( $val );
if ( !$newUser || $newUser->getId() === 0 ) {
return 'usermerge-badnewuser';
}
return true;
}
/**
* @param HTMLForm $form
*/
protected function alterForm( HTMLForm $form ) {
$form->setSubmitTextMsg( 'usermerge-submit' );
}
/**
* @param array $data
* @return Status
*/
public function onSubmit( array $data ) {
global $wgUserMergeEnableDelete;
// Most of the data has been validated using callbacks
// still need to check if the users are different
$newUser = User::newFromName( $data['newuser'] );
// Handle "Anonymous" as a special case for user deletion
if ( $wgUserMergeEnableDelete && $data['newuser'] === 'Anonymous' ) {
$newUser->mId = 0;
}
$oldUser = User::newFromName( $data['olduser'] );
if ( $newUser->getName() === $oldUser->getName() ) {
return Status::newFatal( 'usermerge-same-old-and-new-user' );
}
// Validation passed, let's merge the user now.
$um = new MergeUser( $oldUser, $newUser, new UserMergeLogger() );
$um->merge( $this->getUser(), __METHOD__ );
$out = $this->getOutput();
$out->addWikiMsg(
'usermerge-success',
$oldUser->getName(), $oldUser->getId(),
$newUser->getName(), $newUser->getId()
);
if ( $data['delete'] ) {
$failed = $um->delete( $this->getUser(), [ $this, 'msg' ] );
$out->addWikiMsg(
'usermerge-userdeleted', $oldUser->getName(), $oldUser->getId()
);
if ( $failed ) {
// Output an error message for failed moves
$out->addHTML( Html::openElement( 'ul' ) );
$linkRenderer = $this->getLinkRenderer();
foreach ( $failed as $oldTitleText => $newTitle ) {
$oldTitle = Title::newFromText( $oldTitleText );
$out->addHTML(
Html::rawElement( 'li', [],
$this->msg( 'usermerge-page-unmoved' )->rawParams(
$linkRenderer->makeLink( $oldTitle ),
$linkRenderer->makeLink( $newTitle )
)->escaped()
)
);
}
$out->addHTML( Html::closeElement( 'ul' ) );
}
}
return Status::newGood();
}
protected function getDisplayFormat() {
return 'ooui';
}
protected function getGroupName() {
return 'users';
}
}