1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* Wikipedia Account Creation Assistance tool * |
4
|
|
|
* * |
5
|
|
|
* All code in this file is released into the public domain by the ACC * |
6
|
|
|
* Development Team. Please see team.json for a list of contributors. * |
7
|
|
|
******************************************************************************/ |
8
|
|
|
|
9
|
|
|
namespace Waca\ConsoleTasks; |
10
|
|
|
|
11
|
|
|
use PDO; |
12
|
|
|
use Waca\DataObjects\Log; |
13
|
|
|
use Waca\DataObjects\User; |
14
|
|
|
use Waca\DataObjects\UserRole; |
15
|
|
|
use Waca\Tasks\ConsoleTaskBase; |
16
|
|
|
|
17
|
|
|
class MigrateToRoles extends ConsoleTaskBase |
18
|
|
|
{ |
19
|
|
|
public function execute() |
20
|
|
|
{ |
21
|
|
|
$communityUser = User::getCommunity(); |
22
|
|
|
|
23
|
|
|
$database = $this->getDatabase(); |
24
|
|
|
$statement = $database->query('SELECT id, status, checkuser FROM user;'); |
25
|
|
|
$update = $database->prepare("UPDATE user SET status = 'Active' WHERE id = :id;"); |
26
|
|
|
|
27
|
|
|
$users = $statement->fetchAll(PDO::FETCH_ASSOC); |
28
|
|
|
|
29
|
|
|
foreach ($users as $user) { |
30
|
|
|
$toAdd = array('user'); |
31
|
|
|
|
32
|
|
|
if($user['status'] === 'Admin'){ |
33
|
|
|
$toAdd[] = 'admin'; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if($user['checkuser'] == 1){ |
37
|
|
|
$toAdd[] = 'checkuser'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
View Code Duplication |
foreach ($toAdd as $x) { |
|
|
|
|
41
|
|
|
$a = new UserRole(); |
42
|
|
|
$a->setUser($user['id']); |
43
|
|
|
$a->setRole($x); |
44
|
|
|
$a->setDatabase($database); |
45
|
|
|
$a->save(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$logData = serialize(array( |
49
|
|
|
'added' => $toAdd, |
50
|
|
|
'removed' => array(), |
51
|
|
|
'reason' => 'Initial migration' |
52
|
|
|
)); |
53
|
|
|
|
54
|
|
|
$log = new Log(); |
55
|
|
|
$log->setDatabase($database); |
56
|
|
|
$log->setAction('RoleChange'); |
57
|
|
|
$log->setObjectId($user['id']); |
58
|
|
|
$log->setObjectType('User'); |
59
|
|
|
$log->setUser($communityUser); |
60
|
|
|
$log->setComment($logData); |
61
|
|
|
$log->save(); |
62
|
|
|
|
63
|
|
|
if($user['status'] === 'Admin' || $user['status'] === 'User'){ |
64
|
|
|
$update->execute(array('id' => $user['id'])); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$database->exec("UPDATE schemaversion SET version = 25;"); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.