1
|
|
|
<?php |
2
|
|
|
namespace AL\Admin\Common; |
3
|
|
|
|
4
|
|
|
/** A database update script to execute */ |
5
|
|
|
class DatabaseUpdate { |
6
|
|
|
const EXECUTE_ON_SUCCESS = "success"; |
7
|
|
|
const EXECUTE_ON_FAILURE = "failure"; |
8
|
|
|
|
9
|
|
|
/** Permitted values for 'execute_on' */ |
10
|
|
|
const EXECUTE_ON = array(DatabaseUpdate::EXECUTE_ON_SUCCESS, DatabaseUpdate::EXECUTE_ON_FAILURE); |
11
|
|
|
|
12
|
|
|
private $id; |
13
|
|
|
|
14
|
|
|
/** Path to the INI file */ |
15
|
|
|
private $filename; |
16
|
|
|
|
17
|
|
|
private $description; |
18
|
|
|
|
19
|
|
|
/** SQL Condition to check */ |
20
|
|
|
private $condition; |
21
|
|
|
|
22
|
|
|
/** Wether to execute on success or failure */ |
23
|
|
|
private $execute_on; |
24
|
|
|
private $sql; |
25
|
|
|
private $autoexecute; |
26
|
|
|
private $disable_fk; |
27
|
|
|
|
28
|
|
|
/** Wether there's an additional PHP script to run */ |
29
|
|
|
private $has_addition_script; |
30
|
|
|
|
31
|
|
|
public function __construct( |
32
|
|
|
$db_name, |
33
|
|
|
$id, |
34
|
|
|
$filename, |
35
|
|
|
$description, |
36
|
|
|
$condition, |
37
|
|
|
$execute_on, |
38
|
|
|
$sql, |
39
|
|
|
$autoexecute, |
40
|
|
|
$disable_fk, |
41
|
|
|
$has_addition_script |
42
|
|
|
) { |
43
|
|
|
if (!is_int($id)) { |
44
|
|
|
die("id must be an integer, but was: '$id'"); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
$this->id = $id; |
47
|
|
|
$this->filename = $filename; |
48
|
|
|
$this->description = $description; |
49
|
|
|
|
50
|
|
|
if (!in_array($execute_on, DatabaseUpdate::EXECUTE_ON)) { |
51
|
|
|
die("Invalid value '$execute_on' for 'execute_on'. Only " |
|
|
|
|
52
|
|
|
.join(", ", DatabaseUpdate::EXECUTE_ON)." are supported"); |
53
|
|
|
} |
54
|
|
|
$this->execute_on = $execute_on; |
55
|
|
|
$this->condition = str_replace("__DBNAME__", $db_name, $condition); |
56
|
|
|
; |
57
|
|
|
$this->sql = $sql; |
58
|
|
|
|
59
|
|
|
if (!is_bool($autoexecute)) { |
60
|
|
|
die("autoexecute must be a boolean, but was: '$autoexecute'"); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
$this->autoexecute = $autoexecute; |
63
|
|
|
|
64
|
|
|
if (!is_bool($disable_fk)) { |
65
|
|
|
die("disable_fk must be a boolean, but was: '$disable_fk'"); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
$this->disable_fk = $disable_fk; |
68
|
|
|
|
69
|
|
|
if (!is_bool($has_addition_script)) { |
70
|
|
|
die("has_addition_script must be a boolean, but was: '$has_addition_script'"); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
$this->has_addition_script = $has_addition_script; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getId() { |
76
|
|
|
return $this->id; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getFilename() { |
80
|
|
|
return $this->filename; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getDescription() { |
84
|
|
|
return $this->description; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function getCondition() { |
88
|
|
|
return $this->condition; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getExecuteOn() { |
92
|
|
|
return $this->execute_on; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getSql() { |
96
|
|
|
return $this->sql; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getAutoExecute() { |
100
|
|
|
return $this->autoexecute; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function getDisableForeignKeyChecks() { |
104
|
|
|
return $this->disable_fk; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function hasAdditionScript() { |
108
|
|
|
return $this->has_addition_script; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.