-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_functions.php
More file actions
222 lines (203 loc) · 7.1 KB
/
Copy pathdb_functions.php
File metadata and controls
222 lines (203 loc) · 7.1 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/* this was written using http://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059 */
function connectToDb($host,$database,$user,$pass){
try {
$db = new PDO("mysql:host=$host;dbname=$database;charset=latin1", $user, $pass, array(PDO::ATTR_PERSISTENT => true)); // open db connection and cache it
// print "databse opened\n";
return $db;
} catch (PDOException $pdoex) {
die($pdoex->getMessage());
}
}
/* assures the existence of the config table */
function checkConfigTable($db){
$results=$db->query("SHOW TABLES LIKE 'config'");
if (!$results){
die(print_r($dbh->errorInfo(), TRUE));
}
if ($results->rowCount()<1){
// echo "table doesn't exist\n";
$sql = 'CREATE TABLE config (keyname VARCHAR(100) PRIMARY KEY, value TEXT NOT NULL);';
$db->exec($sql);
$sql = 'INSERT INTO config (keyname,value) VALUES ("dbversion","1")';
$db->exec($sql);
// } else {
// echo "table exists\n";
}
}
/* assures the existence of the urls table */
function checkUrlsTable($db){
$results=$db->query("SHOW TABLES LIKE 'urls'");
if (!$results){
die(print_r($dbh->errorInfo(), TRUE));
}
if ($results->rowCount()<1){
// echo "table doesn't exist\n";
$sql = 'CREATE TABLE urls (uid INT PRIMARY KEY AUTO_INCREMENT, url TEXT NOT NULL);';
$db->exec($sql);
// } else {
// echo "table exists\n";
}
}
/* assures the existence of the tags table */
function checkTagsTable($db){
$results=$db->query("SHOW TABLES LIKE 'tags'");
if (!$results){
die(print_r($dbh->errorInfo(), TRUE));
}
if ($results->rowCount()<1){
// echo "table doesn't exist\n";
$sql = 'CREATE TABLE tags (tid INT PRIMARY KEY AUTO_INCREMENT, keyword VARCHAR(100) NOT NULL);';
$db->exec($sql);
// } else {
// echo "table exists\n";
}
}
/* assures the existence of the sessions table */
function checkSessionsTable($db){
$results=$db->query("SHOW TABLES LIKE 'sessions'");
if (!$results){
die(print_r($dbh->errorInfo(), TRUE));
}
if ($results->rowCount()<1){
// echo "table doesn't exist\n";
$sql = 'CREATE TABLE sessions (sid INT PRIMARY KEY AUTO_INCREMENT, aid INT NOT NULL REFERENCES appointment(aid), description TEXT NOT NULL, start DATETIME NOT NULL, end DATETIME);';
$db->exec($sql);
// } else {
// echo "table exists\n";
}
}
/* assures the existence of the appointments table */
function checkAppointmentsTable($db){
$results=$db->query("SHOW TABLES LIKE 'appointments'");
if (!$results){
die(print_r($dbh->errorInfo(), TRUE));
}
if ($results->rowCount()<1){
// echo "table doesn't exist\n";
$sql = 'CREATE TABLE appointments (aid INT PRIMARY KEY AUTO_INCREMENT, title TEXT NOT NULL,description TEXT, start DATETIME NOT NULL, end DATETIME, location TEXT, coords TEXT);';
$db->exec($sql);
// } else {
// echo "table exists\n";
}
}
/* assures the existence of the appointment_urls table */
function checkAppointmentAttachmentsTable($db){
$results=$db->query("SHOW TABLES LIKE 'appointment_attachments'");
if (!$results){
die(print_r($dbh->errorInfo(), TRUE));
}
if ($results->rowCount()<1){
// echo "table doesn't exist\n";
$sql = 'CREATE TABLE appointment_attachments (aid INT NOT NULL REFERENCES appointments(aid),uid INT NOT NULL REFERENCES urls(uid), mime TEXT, PRIMARY KEY (aid,uid));';
$db->exec($sql);
// } else {
// echo "table exists\n";
}
}
/* assures the existence of the appointment_urls table */
function checkAppointmentUrlsTable($db){
$results=$db->query("SHOW TABLES LIKE 'appointment_urls'");
if (!$results){
die(print_r($dbh->errorInfo(), TRUE));
}
if ($results->rowCount()<1){
// echo "table doesn't exist\n";
$sql = 'CREATE TABLE appointment_urls (aid INT NOT NULL REFERENCES appointments(aid),uid INT NOT NULL REFERENCES urls(uid), description TEXT, PRIMARY KEY (aid,uid));';
$db->exec($sql);
// } else {
// echo "table exists\n";
}
}
/* assures the existence of the appointment_sessions table */
function checkAppointmentSessionsTable($db){
$results=$db->query("SHOW TABLES LIKE 'appointment_sessions'");
if (!$results){
die(print_r($dbh->errorInfo(), TRUE));
}
if ($results->rowCount()<1){
// echo "table doesn't exist\n";
$sql = 'CREATE TABLE appointment_sessions (aid INT NOT NULL REFERENCES appointments(aid),sid INT NOT NULL REFERENCES sessions(sid), PRIMARY KEY (aid,sid));';
$db->exec($sql);
// } else {
// echo "table exists\n";
}
}
/* assures the existence of the appointment_tags table */
function checkAppointmentTagsTable($db){
$results=$db->query("SHOW TABLES LIKE 'appointment_tags'");
if (!$results){
die(print_r($dbh->errorInfo(), TRUE));
}
if ($results->rowCount()<1){
// echo "table doesn't exist\n";
$sql = 'CREATE TABLE appointment_tags (aid INT NOT NULL REFERENCES appointments(aid),tid INT NOT NULL REFERENCES tags(tid), PRIMARY KEY (aid,tid));';
$db->exec($sql);
// } else {
// echo "table exists\n";
}
}
/* assures the existence of the appointment_tags table */
function checkImportedAppointmentsTable($db){
$results=$db->query("SHOW TABLES LIKE 'imported_appointments'");
if (!$results){
die(print_r($dbh->errorInfo(), TRUE));
}
if ($results->rowCount()<1){
// echo "table doesn't exist\n";
$sql = 'CREATE TABLE imported_appointments (md5hash BINARY(32) PRIMARY KEY, aid INT NOT NULL REFERENCES appointments(aid));';
$db->exec($sql);
// } else {
// echo "table exists\n";
}
}
/* assures the existence of all required database tables */
function checkTables($db){
try {
checkConfigTable($db);
checkUrlsTable($db);
checkTagsTable($db);
checkAppointmentsTable($db);
checkSessionsTable($db);
checkAppointmentUrlsTable($db);
checkAppointmentAttachmentsTable($db);
checkAppointmentTagsTable($db);
checkImportedAppointmentsTable($db);
} catch (PDOException $pdoex){
echo $pdoex->getMessage();
}
}
function clear_imported($db){
$stm=$db->prepare('SELECT aid FROM imported_appointments');
$aids=array();
if ($stm->execute()){
$results=$stm->fetchAll();
foreach ($results as $r){
$aids[]=$r['aid'];
}
}
$stm=$db->prepare('SELECT aid FROM appointment_tags NATURAL JOIN tags WHERE keyword = :key');
if ($stm->execute(array(':key'=>loc('imported')))){
$results=$stm->fetchAll();
foreach ($results as $r){
$aids[]=$r['aid'];
}
}
$aids=array_unique($aids,SORT_NUMERIC);
sort($aids);
$aid_string = implode(', ', $aids);
$stm=$db->prepare('DELETE FROM appointment_tags WHERE aid IN ('.$aid_string.')');
$stm->execute();
$stm=$db->prepare('DELETE FROM appointment_attachments WHERE aid IN ('.$aid_string.')');
$stm->execute();
$stm=$db->prepare('DELETE FROM appointment_urls WHERE aid IN ('.$aid_string.')');
$stm->execute();
$stm=$db->prepare('DELETE FROM sessoins WHERE aid IN ('.$aid_string.')');
$stm->execute();
$stm=$db->prepare('DELETE FROM appointments WHERE aid IN ('.$aid_string.')');
$stm->execute();
$stm=$db->prepare('DELETE FROM appointments WHERE aid NOT IN (SELECT DISTINCT aid FROM appointment_tags)');
$stm->execute(); // remove untagged appointments
$stm=$db->prepare('DROP TABLE imported_appointments');
$stm->execute();
}