-
Notifications
You must be signed in to change notification settings - Fork 6
/
grabUserGroups.php
161 lines (142 loc) · 3.91 KB
/
grabUserGroups.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
<?php
/**
* Fetches user groups
*
* @file
* @ingroup Maintenance
* @author Kunal Mehta <[email protected]>
* @version 1.1
*/
require_once 'includes/ExternalWikiGrabber.php';
class GrabUserGroups extends ExternalWikiGrabber {
/**
* Groups we don't want to import...
* @var array
*/
public $badGroups = [
'*',
'user',
'autoconfirmed'
];
/**
* Groups we're going to import
* @var array
*/
public $groups = [];
public function __construct() {
parent::__construct();
$this->addDescription( 'Grabs user group assignments from a pre-existing wiki into a new wiki.' );
$this->addOption( 'groups', 'Get only a specific list of groups (pipe separated list of group names, by default everything except *, user and autoconfirmed)', false, true );
$this->addOption( 'wikia', 'Set this param if the target wiki is on Wikia/Fandom, to automatically skip most of the global user groups that are irrelevant outside there', false, false );
}
public function execute() {
parent::execute();
$providedGroups = $this->getOption( 'groups' );
if ( $providedGroups ) {
$this->groups = explode( '|', $providedGroups );
}
$this->output( "Getting user group information.\n" );
$more = true;
if ( $this->getOption( 'wikia' ) ) {
# They have a few extra usergroups we probably don't want...
$this->badGroups = array_merge( $this->badGroups, [
'authenticated',
'bot-global',
'chatmoderator',
'content-reviewer',
'content-volunteer',
'council',
'fandom-editor',
'fandom-star',
'global-discussions-moderator',
'helper',
'imagereviewer',
'notifications-cms-user',
'request-to-be-forgotten-admin',
'reviewer',
'restricted-login',
'restricted-login-exempt',
'soap',
'staff',
'threadmoderator',
'translator',
'util',
'vanguard',
'voldev',
'vstf',
'wiki-representative',
'wiki-specialist'
] );
}
$params = [
'list' => 'allusers',
'aulimit' => 'max',
'auprop' => 'groups',
'augroup' => implode( '|', $this->getGroups() )
];
$userCount = 0;
do {
$data = $this->bot->query( $params );
$stuff = [];
foreach ( $data['query']['allusers'] as $user ) {
foreach ( $user['groups'] as $group ) {
if ( in_array( $group, $this->groups ) ) {
$stuff[] = [ 'ug_user' => $user['userid'], 'ug_group' => $group ];
}
}
$userCount++;
}
if ( count( $stuff ) ) {
$this->insertRows( $stuff );
}
if ( isset( $data['query-continue'] ) && isset( $data['query-continue']['allusers'] ) ) {
$params = array_merge( $params, $data['query-continue']['allusers'] );
} elseif ( isset( $data['continue'] ) ) {
$params = array_merge( $params, $data['continue'] );
} else {
$more = false;
}
} while ( $more );
$this->output( "Processed $userCount users.\n" );
}
/**
* @return array
*/
public function getGroups() {
$params = [
'action' => 'query',
'meta' => 'siteinfo',
'siprop' => 'usergroups'
];
$data = $this->bot->query( $params );
$groups = [];
foreach ( $data['query']['usergroups'] as $group ) {
if ( !in_array( $group['name'], $this->badGroups ) ) {
$groups[] = $group['name'];
}
}
if ( count( $this->groups ) ) {
# Check in case the user made a typo
$finalGroups = array_intersect( $this->groups, $groups );
$invalidGroups = array_values( array_diff( $this->groups, $groups ) );
if ( count( $invalidGroups ) ) {
$this->fatalError( sprintf( 'Some of the provided groups don\'t exist on the wiki: %s',
implode( '|', $invalidGroups ) ) );
}
$groups = $finalGroups;
}
# Update groups to use outside here
$this->groups = $groups;
return $groups;
}
/**
* Batch insert rows
* @param array $rows
*/
public function insertRows( $rows ) {
$this->dbw->insert( 'user_groups', $rows, __METHOD__, [ 'IGNORE' ] );
$this->dbw->commit();
}
}
$maintClass = 'GrabUserGroups';
require_once RUN_MAINTENANCE_IF_MAIN;