5 * @param int $from_version
6 * @return array|string[]
8 function dav_get_update_statements($from_version)
12 if ($from_version == 1) {
13 $stms[] = "ALTER TABLE `dav_calendarobjects`
14 ADD `calendar_id` INT NOT NULL AFTER `namespace_id` ,
15 ADD `user_temp` INT NOT NULL AFTER `calendar_id` ";
16 $stms[] = "ALTER TABLE `dav_calendarobjects`
17 ADD `componentType` ENUM( 'VEVENT', 'VTODO' ) NOT NULL DEFAULT 'VEVENT' AFTER `lastmodified` ,
18 ADD `firstOccurence` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER `componentType` ,
19 ADD `lastOccurence` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER `firstOccurence`";
20 $stms[] = "UPDATE dav_calendarobjects a JOIN dav_calendars b ON a.namespace = b.namespace AND a.namespace_id = b.namespace_id SET a.user_temp = b.uid";
21 $stms[] = "DROP TABLE IF EXISTS
22 `dav_addressbooks_community` ,
23 `dav_addressbooks_phone` ,
24 `dav_cache_synchronized` ,
27 `dav_cal_virtual_object_cache` ,
31 `dav_notifications` ;";
33 $stms = array_merge($stms, dav_get_create_statements(array("dav_calendarobjects")));
35 $user_ids = q("SELECT DISTINCT `uid` FROM %s%scalendars", CALDAV_SQL_DB, CALDAV_SQL_PREFIX);
36 foreach ($user_ids as $user) $stms = array_merge($stms, wdcal_create_std_calendars_get_statements($user["uid"], false));
38 $stms[] = "UPDATE dav_calendarobjects a JOIN dav_calendars b
39 ON b.`namespace` = " . CALDAV_NAMESPACE_PRIVATE . " AND a.`user_temp` = b.`namespace_id` AND b.`uri` = 'private'
40 SET a.`calendar_id` = b.`id`";
42 $stms[] = "ALTER TABLE `dav_calendarobjects` DROP `namespace`, DROP `namespace_id`, DROP `user_temp`";
46 if (in_array($from_version, array(1, 2))) {
47 $stms[] = "DROP TABLE `dav_addressbooks_phone`";
48 $stms[] = "DROP TABLE `dav_addressbooks_community`";
49 $stms[] = "DROP TABLE `dav_cards`";
51 $stms[] = "CREATE TABLE IF NOT EXISTS `dav_addressbooks` (
52 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
53 `namespace` mediumint(9) NOT NULL,
54 `namespace_id` int(11) unsigned NOT NULL,
55 `displayname` varchar(200) NOT NULL,
56 `description` varchar(500) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
57 `uri` varchar(50) NOT NULL,
58 `ctag` int(11) unsigned NOT NULL DEFAULT '1',
60 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
62 $stms[] = "CREATE TABLE IF NOT EXISTS `dav_addressbookobjects` (
63 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
64 `addressbook_id` int(11) unsigned NOT NULL,
65 `contact` int(11) DEFAULT NULL,
66 `carddata` mediumtext CHARACTER SET utf8 COLLATE utf8_unicode_ci,
67 `uri` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
68 `lastmodified` timestamp NULL DEFAULT NULL,
69 `manually_edited` tinyint(4) NOT NULL DEFAULT '0',
70 `manually_deleted` tinyint(4) NOT NULL DEFAULT '0',
71 `etag` varchar(15) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
72 `size` int(10) unsigned NOT NULL,
74 UNIQUE KEY `namespace` (`addressbook_id`,`contact`),
75 KEY `contact` (`contact`)
76 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
83 * @param array $except
86 function dav_get_create_statements($except = array())
90 if (!in_array("dav_caldav_log", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_caldav_log` (
91 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
92 `uid` mediumint(9) NOT NULL,
93 `ip` varchar(15) NOT NULL,
94 `user_agent` varchar(100) NOT NULL,
95 `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
96 `method` varchar(10) NOT NULL,
97 `url` varchar(100) NOT NULL,
99 KEY `mitglied` (`uid`)
100 ) ENGINE=InnoDB DEFAULT CHARSET=utf8";
102 if (!in_array("dav_calendarobjects", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_calendarobjects` (
103 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
104 `calendar_id` int(11) NOT NULL,
106 `uri` varchar(200) NOT NULL,
107 `lastmodified` timestamp NULL DEFAULT NULL,
108 `componentType` enum('VEVENT','VTODO') NOT NULL DEFAULT 'VEVENT',
109 `firstOccurence` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
110 `lastOccurence` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
111 `etag` varchar(15) NOT NULL,
112 `size` int(10) unsigned NOT NULL,
114 UNIQUE KEY `uri` (`uri`),
115 KEY `calendar_id` (`calendar_id`)
116 ) ENGINE=InnoDB DEFAULT CHARSET=utf8";
118 if (!in_array("dav_calendars", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_calendars` (
119 `id` int(11) NOT NULL AUTO_INCREMENT,
120 `namespace` mediumint(9) NOT NULL,
121 `namespace_id` int(10) unsigned NOT NULL,
122 `calendarorder` int(11) NOT NULL DEFAULT '1',
123 `calendarcolor` char(6) NOT NULL DEFAULT '5858FF',
124 `displayname` varchar(200) NOT NULL,
125 `timezone` text NOT NULL,
126 `description` varchar(500) NOT NULL,
127 `uri` varchar(50) NOT NULL DEFAULT '',
128 `has_vevent` tinyint(4) NOT NULL DEFAULT '1',
129 `has_vtodo` tinyint(4) NOT NULL DEFAULT '1',
130 `ctag` int(10) unsigned NOT NULL,
132 UNIQUE KEY (`namespace` , `namespace_id` , `uri`),
134 ) ENGINE=InnoDB DEFAULT CHARSET=utf8";
136 if (!in_array("dav_cal_virtual_object_cache", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_cal_virtual_object_cache` (
137 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
138 `calendar_id` int(10) unsigned NOT NULL,
139 `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
140 `data_uri` char(80) NOT NULL,
141 `data_summary` varchar(1000) NOT NULL,
142 `data_location` varchar(1000) NOT NULL,
143 `data_start` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
144 `data_end` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
145 `data_allday` tinyint(4) NOT NULL,
146 `data_type` varchar(20) NOT NULL,
147 `calendardata` text NOT NULL,
148 `size` int(11) NOT NULL,
149 `etag` varchar(15) NOT NULL,
151 KEY `data_uri` (`data_uri`),
152 KEY `ref_type` (`calendar_id`,`data_end`)
153 ) ENGINE=InnoDB DEFAULT CHARSET=utf8";
155 if (!in_array("dav_cal_virtual_object_sync", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_cal_virtual_object_sync` (
156 `calendar_id` int(10) unsigned NOT NULL,
157 `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
158 PRIMARY KEY (`calendar_id`)
159 ) ENGINE=InnoDB DEFAULT CHARSET=utf8";
161 if (!in_array("dav_jqcalendar", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_jqcalendar` (
162 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
163 `ical_recurr_uri` varchar(100) DEFAULT NULL,
164 `calendar_id` int(10) unsigned NOT NULL,
165 `calendarobject_id` int(10) unsigned NOT NULL,
166 `Summary` varchar(100) NOT NULL,
167 `StartTime` timestamp NULL DEFAULT NULL,
168 `EndTime` timestamp NULL DEFAULT NULL,
169 `IsEditable` tinyint(3) unsigned NOT NULL,
170 `IsAllDayEvent` tinyint(4) NOT NULL,
171 `IsRecurring` tinyint(4) NOT NULL,
172 `Color` char(6) DEFAULT NULL,
174 KEY `calendarByStart` (`calendar_id`,`StartTime`),
175 KEY `calendarobject_id` (`calendarobject_id`,`ical_recurr_uri`)
176 ) ENGINE=InnoDB DEFAULT CHARSET=utf8";
178 if (!in_array("dav_notifications", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_notifications` (
179 `id` int(11) NOT NULL AUTO_INCREMENT,
180 `ical_recurr_uri` varchar(100) DEFAULT NULL,
181 `calendar_id` int(11) NOT NULL,
182 `calendarobject_id` int(10) unsigned NOT NULL,
183 `action` enum('email','display') NOT NULL DEFAULT 'email',
184 `alert_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
185 `notified` tinyint(4) NOT NULL DEFAULT '0',
187 KEY `notified` (`notified`,`alert_date`),
188 KEY `calendar_id` (`calendar_id`,`calendarobject_id`)
189 ) ENGINE=InnoDB DEFAULT CHARSET=utf8";
191 if (!in_array("dav_addressbooks", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_addressbooks` (
192 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
193 `namespace` mediumint(9) NOT NULL,
194 `namespace_id` int(11) unsigned NOT NULL,
195 `displayname` varchar(200) NOT NULL,
196 `description` varchar(500) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
197 `uri` varchar(50) NOT NULL,
198 `ctag` int(11) unsigned NOT NULL DEFAULT '1',
200 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
202 if (!in_array("dav_addressbookobjects", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_addressbookobjects` (
203 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
204 `addressbook_id` int(11) unsigned NOT NULL,
205 `contact` int(11) DEFAULT NULL,
206 `carddata` mediumtext CHARACTER SET utf8 COLLATE utf8_unicode_ci,
207 `uri` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
208 `lastmodified` timestamp NULL DEFAULT NULL,
209 `manually_edited` tinyint(4) NOT NULL DEFAULT '0',
210 `manually_deleted` tinyint(4) NOT NULL DEFAULT '0',
211 `etag` varchar(15) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
212 `size` int(10) unsigned NOT NULL,
214 UNIQUE KEY `namespace` (`addressbook_id`,`contact`),
215 KEY `contact` (`contact`)
216 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
224 function dav_check_tables()
226 $x = q("DESCRIBE %s%scalendars", CALDAV_SQL_DB, CALDAV_SQL_PREFIX);
228 if (count($x) == 9) return 1; // Version 0.1
229 // @TODO Detect Version 0.2
230 if (count($x) == 12) return 0; // Correct
231 return -2; // Unknown version
238 function dav_create_tables()
240 $stms = dav_get_create_statements();
244 foreach ($stms as $st) {
246 if ($db->error) $errors[] = $db->error;
255 function dav_upgrade_tables()
257 $ver = dav_check_tables();
258 if (!in_array($ver, array(1))) return array("Unknown error");
260 $stms = dav_get_update_statements($ver);
264 foreach ($stms as $st) {
266 if ($db->error) $errors[] = $db->error;