|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Phinx\Db\Table\Column; |
|
4
|
|
|
use Phinx\Migration\AbstractMigration; |
|
5
|
|
|
|
|
6
|
|
|
class Maps extends AbstractMigration |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* Change Method. |
|
10
|
|
|
* |
|
11
|
|
|
* Write your reversible migrations using this method. |
|
12
|
|
|
* |
|
13
|
|
|
* More information on writing migrations is available here: |
|
14
|
|
|
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class |
|
15
|
|
|
* |
|
16
|
|
|
* The following commands can be used in this method and Phinx will |
|
17
|
|
|
* automatically reverse them when rolling back: |
|
18
|
|
|
* |
|
19
|
|
|
* createTable |
|
20
|
|
|
* renameTable |
|
21
|
|
|
* addColumn |
|
22
|
|
|
* renameColumn |
|
23
|
|
|
* addIndex |
|
24
|
|
|
* addForeignKey |
|
25
|
|
|
* |
|
26
|
|
|
* Remember to call "create()" or "update()" and NOT "save()" when working |
|
27
|
|
|
* with the Table class. |
|
28
|
|
|
*/ |
|
29
|
|
|
public function change() |
|
30
|
|
|
{ |
|
31
|
|
|
$maps = $this->table('maps', array('id' => false, 'primary_key' => 'id')); |
|
32
|
|
|
$maps->addColumn('id', 'integer', array('limit' => 10, 'signed' => false, 'identity' => true)) |
|
33
|
|
|
->addColumn('name', 'string', array('limit' => 42, 'null' => false, 'comment' => 'The name of the map')) |
|
34
|
|
|
->addColumn('alias', 'string', array('limit' => 42, 'null' => true, 'comment' => 'The alias of the map')) |
|
35
|
|
|
->addColumn('avatar', 'string', array('limit' => 200, 'null' => true, 'comment' => 'The path to the map\'s image')) |
|
36
|
|
|
->addColumn('description', 'text', array('null' => false, 'default' => '', 'comment' => 'A description of the map')) |
|
37
|
|
|
->addColumn('status', 'set', array('values' => array('active', 'hidden', 'disabled', 'deleted'), 'null' => false, |
|
38
|
|
|
'default' => 'active', 'comment' => 'The status of the map')) |
|
39
|
|
|
->addIndex('alias', array('unique' => true)) |
|
40
|
|
|
->create(); |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
$permissions = $this->table('permissions'); |
|
44
|
|
|
$data = [ |
|
45
|
|
|
[ 'name' => 'add_map', 'description' => 'The ability to add a map' ], |
|
46
|
|
|
[ 'name' => 'edit_map', 'description' => 'The ability to edit a map' ], |
|
47
|
|
|
[ 'name' => 'del_map', 'description' => 'The ability to mark a map as deleted' ], |
|
48
|
|
|
[ 'name' => 'wipe_map', 'description' => 'The ability to wipe a map from the database' ], |
|
49
|
|
|
]; |
|
50
|
|
|
$permissions->insert($data)->save(); |
|
51
|
|
|
|
|
52
|
|
|
$ids = $this->getPermissionIDs(); |
|
53
|
|
|
$rolePermissions = $this->table('role_permission'); |
|
54
|
|
|
$data = [ // We use hardcoded role IDs because class constants might change |
|
55
|
|
|
[ 'role_id' => 1, 'perm_id' => $ids['add']], // Developers |
|
56
|
|
|
[ 'role_id' => 1, 'perm_id' => $ids['edit']], |
|
57
|
|
|
[ 'role_id' => 1, 'perm_id' => $ids['del']], |
|
58
|
|
|
[ 'role_id' => 1, 'perm_id' => $ids['wipe']], |
|
59
|
|
|
|
|
60
|
|
|
[ 'role_id' => 5, 'perm_id' => $ids['add']], // System Admins |
|
61
|
|
|
[ 'role_id' => 5, 'perm_id' => $ids['edit']], |
|
62
|
|
|
[ 'role_id' => 5, 'perm_id' => $ids['del']], |
|
63
|
|
|
[ 'role_id' => 5, 'perm_id' => $ids['wipe']], |
|
64
|
|
|
]; |
|
65
|
|
|
$rolePermissions->insert($data)->save(); |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
$matches = $this->table('matches'); |
|
70
|
|
|
$matches |
|
71
|
|
|
->addColumn('map', 'integer', array('limit' => 10, 'signed' => false, 'null' => true, 'comment' => 'The map that was played')) |
|
72
|
|
|
->addForeignKey('map', 'maps', 'id', array('delete' => 'SET_NULL')) |
|
73
|
|
|
->save(); |
|
74
|
|
|
|
|
75
|
|
|
$this->updateMatches(); |
|
76
|
|
|
|
|
77
|
|
|
// TODO: Uncomment this (commented to prevent loss of data in case of error) |
|
78
|
|
|
// $matches->removeColumn('map_played') |
|
79
|
|
|
// ->save(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get the IDs of the newly entered permissions |
|
84
|
|
|
* |
|
85
|
|
|
* @return array |
|
86
|
|
|
*/ |
|
87
|
|
|
private function getPermissionIDs() |
|
88
|
|
|
{ |
|
89
|
|
|
$return = array(); |
|
90
|
|
|
|
|
91
|
|
|
foreach(array('add', 'edit', 'del', 'wipe') as $permission) { |
|
92
|
|
|
$row = $this->fetchRow("SELECT id FROM permissions WHERE name = '{$permission}_map'"); |
|
93
|
|
|
$return[$permission] = $row['id']; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $return; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
private function updateMatches() |
|
101
|
|
|
{ |
|
102
|
|
|
$matches = $this->table('matches'); |
|
|
|
|
|
|
103
|
|
|
$maps = $this->table('maps'); |
|
104
|
|
|
|
|
105
|
|
|
$insert = array(); |
|
106
|
|
|
|
|
107
|
|
|
$storedMaps = $this->fetchAll("SELECT DISTINCT(map_played) AS map FROM matches WHERE map_played REGEXP '^[A-Za-z0-9]+$'"); |
|
108
|
|
|
foreach($storedMaps as $map) { |
|
109
|
|
|
$insert[] = [ 'name' => $map[0], 'alias' => null, 'avatar' => null ]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$maps->insert($insert)->save(); |
|
113
|
|
|
|
|
114
|
|
|
foreach($storedMaps as $map) { |
|
115
|
|
|
$name = $map[0]; |
|
116
|
|
|
// $name contains only alphanumeric characters, meaning there can't be any MySQL injection |
|
117
|
|
|
$row = $this->fetchRow("SELECT id FROM maps WHERE name = '$name'"); |
|
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.