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
|
<?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 <t.laqua@gmail.com>
* @author Thomas Gries <mail@tgries.de>
* @author Matthew April <Matthew.April@tbs-sct.gc.ca>
*
*/
class SpecialUserMerge extends FormSpecialPage {
function __construct() {
parent::__construct( 'UserMerge', 'usermerge' );
}
/**
* @return array
*/
protected function getFormFields() {
$us = $this;
return array(
'olduser' => array(
'type' => 'text',
'label-message' => 'usermerge-olduser',
'required' => true,
'validation-callback' => function( $val ) use ( $us ) {
$key = $us->validateOldUser( $val );
if ( is_string( $key ) || is_array( $key ) ) {
return $us->msg( $key )->escaped();
}
return true;
},
),
'newuser' => array(
'type' => 'text',
'required' => true,
'label-message' => 'usermerge-newuser',
'validation-callback' => function( $val ) use ( $us ) {
$key = $us->validateNewUser( $val );
if ( is_string( $key ) ) {
return $us->msg( $key )->escaped();
}
return true;
},
),
'delete' => array(
'type' => 'check',
'label-message' => 'usermerge-deleteolduser',
),
);
}
/**
* @param $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 validateOldUser( $val ) {
global $wgUserMergeProtectedGroups;
$oldUser = User::newFromName( $val );
if ( !$oldUser || $oldUser->getId() === 0 ) {
return 'usermerge-badolduser';
}
if ( $this->getUser()->getId() === $oldUser->getId() ) {
return array( 'usermerge-noselfdelete', $this->getUser()->getName() );
}
if ( count( array_intersect( $oldUser->getGroups(), $wgUserMergeProtectedGroups ) ) ) {
return array( 'usermerge-protectedgroup', $oldUser->getName() );
}
return true;
}
/**
* @param $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' );
$form->setWrapperLegendMsg( 'usermerge-fieldset' );
}
/**
* @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() );
$out = $this->getOutput();
$out->addWikiMsg(
'usermerge-success',
$oldUser->getName(), $oldUser->getId(),
$newUser->getName(), $newUser->getId()
);
if ( $data['delete'] ) {
$failed = $um->delete( $this->getUser(), array( $this, 'msg' ) );
$out->addWikiMsg(
'usermerge-userdeleted', $oldUser->getName(), $oldUser->getId()
);
if ( $failed ) {
// Output an error message for failed moves
$out->addHTML( Html::openElement( 'ul' ) );
foreach ( $failed as $oldTitleText => $newTitle ) {
$oldTitle = Title::newFromText( $oldTitleText );
$out->addHTML(
Html::rawElement( 'li', array(),
$this->msg( 'usermerge-page-unmoved' )->rawParams(
Linker::link( $oldTitle ),
Linker::link( $newTitle )
)->escaped()
)
);
}
$out->addHTML( Html::closeElement( 'ul' ) );
}
}
return Status::newGood();
}
}
/**
* Former class name, for backwards compatability
* @deprecated
*/
class UserMerge extends SpecialUserMerge {}
|