forked from arjaneising/My-Movie-List
/
update.php
203 lines (170 loc) · 5.65 KB
/
update.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
error_reporting(E_ALL ^ E_STRICT);
ini_set('display_errors', true);
date_default_timezone_set('Europe/Amsterdam');
require_once('_includes/main.php');
class Update
{
public function __construct($from_version, $to_version, array $statements)
{
$this->from = $from_version;
$this->to = $to_version;
$this->statements = $statements;
}
public function apply($conn, &$error = null)
{
foreach ($this->statements as $statement)
{
if (!mysql_query($statement, $conn))
{
$error = mysql_error($conn) . "\n\nQuery:\n$statement";
return false;
}
}
return true;
}
}
function backup_database($db, $conn)
{
if (!is_writable('backups'))
die('Please make sure the backups dir is writable, or if you are really really daring or made a backup yourself, <a href="update.php?skip-backups">continue without making a backup</a>.');
require_once '_includes/backup.class.php';
$backup_creator = new MDB_Backup($conn);
$backup_creator->dump(sprintf('backups/%s-%s-%s-%s.sql',
DB_NAME, date('Y-m-d_his'), $db->getVersion(), uniqid()));
}
function update_database($db, $conn, array $updates)
{
while (true)
{
$current_version = $db->getVersion();
foreach ($updates as $update)
{
// Check if this update is of any use now
if ($update->from != $current_version)
continue;
// When successfully applied…
if ($update->apply($conn, $error))
{
$db->setVersion($update->to);
continue 2; // …continue while loop
}
// Otherwise bail out.
else
{
die(sprintf('Error while updating from %f to %f:<br><pre>%s</pre>',
$update->from, $update->to, $error));
}
}
// When no update was applied, stop trying. We are probably up to date
// or stuck with an unupgradable version.
break;
}
}
$updates = array();
$updates[] = new Update(null, 1.1, array(
"CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "cast` (
`movie_id` int(7) NOT NULL,
`name_id` int(7) NOT NULL,
`order` int(3) NOT NULL,
PRIMARY KEY (`movie_id`,`name_id`),
KEY `name_id` (`name_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8",
"CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "crew` (
`movie_id` int(7) NOT NULL,
`name_id` int(7) NOT NULL,
`order` int(3) NOT NULL,
PRIMARY KEY (`movie_id`,`name_id`),
KEY `name_id` (`name_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8",
"CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "imdbtop250` (
`id` int(7) NOT NULL,
`order` int(3) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`),
UNIQUE KEY `order` (`order`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8",
"CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "movies` (
`id` int(7) NOT NULL,
`title` varchar(255) NOT NULL,
`title_english` varchar(255) NOT NULL,
`language` varchar(2) NOT NULL,
`genre` set('Action','Adventure','Animation','Biography','Comedy','Crime',
'Documentary','Drama','Family','History','Horror','Music','Musical',
'Mystery','Sci-Fi','Short','Sport','Talk Show','Thriller','War',
'Western') NOT NULL,
`year` int(4) NOT NULL,
`runtime` int(3) NOT NULL,
`rating` tinyint(2) NOT NULL,
`date_added` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8",
"CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "names` (
`id` int(7) NOT NULL,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8",
"CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "options` (
`name` varchar(255) NOT NULL,
`value` varchar(255) NOT NULL,
PRIMARY KEY (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8"));
$updates[] = new Update(1.1, 1.2, array(
"ALTER TABLE `" . DB_PREFIX . "movies` CHANGE `genre` `genre`
SET('Action','Adventure','Animation','Biography','Comedy','Crime',
'Documentary','Drama','Family','Fantasy','Film-Noir','Game-Show','History',
'Horror','Music','Musical','Mystery','News','Reality-TV','Romance','Sci-Fi',
'Sport','Talk-Show','Thriller','War','Western') NOT NULL DEFAULT ''"));
$ignored_prefix_sql_updates = array(
// Create a trigger to automagically create the sort title
"DROP TRIGGER IF EXISTS `" . DB_PREFIX ."_sort_title`",
"CREATE TRIGGER `" . DB_PREFIX ."_sort_title` BEFORE INSERT ON `" . DB_PREFIX . "movies`
FOR EACH ROW
BEGIN
CASE
" . generate_prefix_remove_sql("
WHEN LEFT(NEW.title, %d) = '%s' THEN
SET NEW.sort_title = SUBSTRING(NEW.title FROM %d);
", $ignoredPrefixes) . "
ELSE
SET NEW.sort_title = NEW.title;
END CASE;
END",
// Create the sort_title value for all existing movies
"UPDATE `" . DB_PREFIX . "movies`
SET sort_title = CASE
" . generate_prefix_remove_sql("
WHEN LEFT(title, %d) = '%s' THEN
SUBSTRING(title FROM %d)
", $ignoredPrefixes) . "
ELSE
title
END");
$updates[] = new Update(1.2, 1.3, array_merge(array(
// Add sort-title column and index
"ALTER TABLE `" . DB_PREFIX . "movies`
ADD `sort_title` VARCHAR(255) NULL DEFAULT NULL AFTER `title`,
ADD INDEX `sort_index` (`sort_title`)"),
$ignored_prefix_sql_updates
));
function generate_prefix_remove_sql($format, $prefixes)
{
$sql = '';
foreach ($prefixes as $prefix)
{
// WHEN LEFT(NEW.title, 4) = 'The ' THEN
// SET NEW.sort_title = SUBSTRING(NEW.title FROM 5);
$sql .= sprintf($format,
strlen($prefix) + 1, $prefix . ' ', strlen($prefix) + 2);
}
return $sql;
}
$conn = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die('Could not connect: ' . mysql_error());
mysql_select_db(DB_NAME, $conn) or die('Could not select database');
$db = new MDB($conn);
if (!isset($_GET['skip-backups']))
backup_database($db, $conn);
update_database($db, $conn, $updates);
if (!error_get_last())
header('Location: ./?message=update-success');
?>