forked from unl/UNL_ENews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupgrade.php
172 lines (150 loc) · 5.88 KB
/
upgrade.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
<?php
if (file_exists(dirname(__FILE__).'/www/config.inc.php')) {
require_once dirname(__FILE__).'/www/config.inc.php';
} else {
require dirname(__FILE__).'/www/config.sample.php';
}
echo 'Connecting to the database…';
$mysqli = UNL_ENews_Controller::getDB();
echo 'connected successfully!<br />'.PHP_EOL;
echo 'Initializing database structure…';
foreach (array('enews.sql', 'story_tags.sql') as $sql_file) {
$result = $mysqli->multi_query(file_get_contents(dirname(__FILE__).'/data/'.$sql_file));
if (!$result) {
echo 'There was an error initializing the database.<br />'.PHP_EOL;
echo $mysqli->error;
exit();
}
do {
if ($result = $mysqli->use_result()) {
$result->close();
}
} while ($mysqli->next_result());
}
echo 'initialization complete!<br />'.PHP_EOL;
if (UNL_ENews_Newsroom::getByID(1) === false ) {
echo 'Inserting sample data…';
$result = $mysqli->multi_query(file_get_contents(dirname(__FILE__).'/data/enews_sample_data.sql'));
if (!$result) {
echo 'There was an error inserting the sample data!<br />'.PHP_EOL;
echo $mysqli->error;
exit();
}
}
$result = $mysqli->multi_query(file_get_contents(dirname(__FILE__).'/data/story_presentations.sql'));
if (!$result) {
echo 'There was an error updating the story presentation types!<br />'.PHP_EOL;
echo $mysqli->error;
exit();
}
echo 'Adding subtitle field to newsrooms...<br />'.PHP_EOL;
$result = $mysqli->query("ALTER TABLE `newsrooms` ADD `subtitle` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL AFTER `name`;");
if (!$result) {
if (mysqli_errno($mysqli) == 1060) {
echo 'Field already has been added<br />'.PHP_EOL;
}
}
echo 'Adding submit_url field to newsrooms...<br />'.PHP_EOL;
$result = $mysqli->query("ALTER TABLE `newsrooms` ADD `submit_url` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL AFTER `allow_submissions`;");
if (!$result) {
if (mysqli_errno($mysqli) == 1060) {
echo 'Field already has been added<br />'.PHP_EOL;
}
}
$result = $mysqli->query("SELECT id, email_lists FROM newsrooms;");
if ($result) {
while ($row = $result->fetch_assoc()) {
if (!empty($row['email_lists'])) {
UNL_ENews_Newsroom::getByID($row['id'])->addEmail($row['email_lists']);
}
}
// Assume we did a good job, and drop the email_lists field?
}
echo 'Adding from_address field to newsroom table<br />'.PHP_EOL;
$result = $mysqli->query("ALTER TABLE `newsrooms` ADD `from_address` VARCHAR( 255 ) COLLATE utf8_unicode_ci DEFAULT NULL AFTER `website`;");
if (!$result) {
if (mysqli_errno($mysqli) == 1060) {
echo 'Field already exists but that\'s ok!<br />'.PHP_EOL;
} else {
echo $mysqli->error;
exit();
}
}
echo 'Adding footer_text field to newsrooms…<br />'.PHP_EOL;
$result = $mysqli->query("ALTER TABLE `newsrooms` ADD `footer_text` VARCHAR( 300 ) NOT NULL;");
if (!$result) {
if (mysqli_errno($mysqli) == 1060) {
echo 'Field already has been added<br />'.PHP_EOL;
}
}
echo 'Adding presentation_id field to stories table<br />'.PHP_EOL;
$result = $mysqli->query("ALTER TABLE `stories` ADD `presentation_id` INT( 10 ) NOT NULL AFTER `website`;");
if (!$result) {
if (mysqli_errno($mysqli) == 1060) {
echo 'Field already exists but that\'s ok!<br />'.PHP_EOL;
} else {
echo $mysqli->error;
exit();
}
} else {
echo 'Setting existing stories to presentation_id of 1<br />'.PHP_EOL;
$result = $mysqli->query("UPDATE stories SET presentation_id = 1;");
if (!$result) {
echo 'Error setting default presentations on existing stories: ';
echo $mysqli->error;
exit();
}
}
echo 'Adding description field to files table<br />'.PHP_EOL;
$result = $mysqli->query("ALTER TABLE `files` ADD `description` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL AFTER `use_for`;");
if (!$result) {
if (mysqli_errno($mysqli) == 1060) {
echo 'Field already exists but that\'s ok!<br />'.PHP_EOL;
}
}
echo 'Correcting default presentation type field…<br />'.PHP_EOL;
$result = $mysqli->query("ALTER TABLE `story_presentations` CHANGE `default` `isdefault` TINYINT( 1 ) NOT NULL ;");
if (!$result) {
if (mysqli_errno($mysqli) == 1060) {
echo 'Field already has been corrected<br />'.PHP_EOL;
}
}
echo 'Adding presentation_id field to newsletter_stories…<br />'.PHP_EOL;
$result = $mysqli->query("ALTER TABLE `newsletter_stories` ADD `presentation_id` INT( 10 ) NULL AFTER `story_id`;");
if (!$result) {
if (mysqli_errno($mysqli) == 1060) {
echo 'Field already has been added<br />'.PHP_EOL;
}
}
echo 'Adding active field to story_presentations…<br />'.PHP_EOL;
$result = $mysqli->query("ALTER TABLE `story_presentations` ADD `active` TINYINT( 1 ) NOT NULL DEFAULT 1 AFTER `template`;");
if (!$result) {
if (mysqli_errno($mysqli) == 1060) {
echo 'Field already has been added<br />'.PHP_EOL;
}
}
echo 'Removing invalid column from story_presentations…<br />'.PHP_EOL;
$result = $mysqli->query("ALTER TABLE `story_presentations` DROP COLUMN `dependent_selector`;");
if (!$result) {
if (mysqli_errno($mysqli) == 1091) {
echo 'Field does not exist but that\'s ok!<br />'.PHP_EOL;
} else {
echo $mysqli->error;
exit();
}
}
echo 'Updating existing stories with invalid presentation...<br />'.PHP_EOL;
$result = $mysqli->query("UPDATE stories SET presentation_id = 6 WHERE presentation_id = 7;");
if (!$result) {
echo 'Error updating stories with invalid presentation: ';
echo $mysqli->error;
exit();
}
echo 'Removing invalid story_presentations...<br />'.PHP_EOL;
$result = $mysqli->query("DELETE FROM story_presentations WHERE id = 7;");
if (!$result) {
echo 'Error removing invalid story_presentations: ';
echo $mysqli->error;
exit();
}
echo 'Upgrade complete!';