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`";
50 * @param array $except
53 function dav_get_create_statements($except = array())
57 if (!in_array("dav_addressbooks_community", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_addressbooks_community` (
58 `uid` int(11) NOT NULL,
59 `ctag` int(11) unsigned NOT NULL DEFAULT '1',
61 ) ENGINE=MyISAM DEFAULT CHARSET=utf8";
63 if (!in_array("dav_addressbooks_phone", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_addressbooks_phone` (
64 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
65 `uid` int(11) NOT NULL,
66 `principaluri` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
67 `displayname` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
68 `uri` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
69 `description` text CHARACTER SET utf8 COLLATE utf8_unicode_ci,
70 `ctag` int(11) unsigned NOT NULL DEFAULT '1',
72 UNIQUE KEY `principaluri` (`principaluri`,`uri`)
73 ) ENGINE=MyISAM DEFAULT CHARSET=utf8";
75 if (!in_array("dav_caldav_log", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_caldav_log` (
76 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
77 `uid` mediumint(9) NOT NULL,
78 `ip` varchar(15) NOT NULL,
79 `user_agent` varchar(100) NOT NULL,
80 `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
81 `method` varchar(10) NOT NULL,
82 `url` varchar(100) NOT NULL,
84 KEY `mitglied` (`uid`)
85 ) ENGINE=MyISAM DEFAULT CHARSET=utf8";
87 if (!in_array("dav_calendarobjects", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_calendarobjects` (
88 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
89 `calendar_id` int(11) NOT NULL,
91 `uri` varchar(200) NOT NULL,
92 `lastmodified` timestamp NULL DEFAULT NULL,
93 `componentType` enum('VEVENT','VTODO') NOT NULL DEFAULT 'VEVENT',
94 `firstOccurence` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
95 `lastOccurence` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
96 `etag` varchar(15) NOT NULL,
97 `size` int(10) unsigned NOT NULL,
99 UNIQUE KEY `uri` (`uri`),
100 KEY `calendar_id` (`calendar_id`)
101 ) ENGINE=MyISAM DEFAULT CHARSET=utf8";
103 if (!in_array("dav_calendars", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_calendars` (
104 `id` int(11) NOT NULL AUTO_INCREMENT,
105 `namespace` mediumint(9) NOT NULL,
106 `namespace_id` int(10) unsigned NOT NULL,
107 `calendarorder` int(11) NOT NULL DEFAULT '1',
108 `calendarcolor` char(6) NOT NULL DEFAULT '5858FF',
109 `displayname` varchar(200) NOT NULL,
110 `timezone` text NOT NULL,
111 `description` varchar(500) NOT NULL,
112 `uri` varchar(50) NOT NULL DEFAULT '',
113 `has_vevent` tinyint(4) NOT NULL DEFAULT '1',
114 `has_vtodo` tinyint(4) NOT NULL DEFAULT '1',
115 `ctag` int(10) unsigned NOT NULL,
117 UNIQUE KEY (`namespace` , `namespace_id` , `uri`),
119 ) ENGINE=MyISAM DEFAULT CHARSET=utf8";
121 if (!in_array("dav_cal_virtual_object_cache", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_cal_virtual_object_cache` (
122 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
123 `calendar_id` int(10) unsigned NOT NULL,
124 `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
125 `data_uri` char(80) NOT NULL,
126 `data_summary` varchar(1000) NOT NULL,
127 `data_location` varchar(1000) NOT NULL,
128 `data_start` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
129 `data_end` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
130 `data_allday` tinyint(4) NOT NULL,
131 `data_type` varchar(20) NOT NULL,
132 `calendardata` text NOT NULL,
133 `size` int(11) NOT NULL,
134 `etag` varchar(15) NOT NULL,
136 KEY `data_uri` (`data_uri`),
137 KEY `ref_type` (`calendar_id`,`data_end`)
138 ) ENGINE=MyISAM DEFAULT CHARSET=utf8";
140 if (!in_array("dav_cal_virtual_object_sync", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_cal_virtual_object_sync` (
141 `calendar_id` int(10) unsigned NOT NULL,
142 `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
143 PRIMARY KEY (`calendar_id`)
144 ) ENGINE=InnoDB DEFAULT CHARSET=utf8";
146 if (!in_array("dav_cards", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_cards` (
147 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
148 `namespace` tinyint(3) unsigned NOT NULL,
149 `namespace_id` int(11) unsigned NOT NULL,
150 `contact` int(11) DEFAULT NULL,
151 `carddata` mediumtext CHARACTER SET utf8 COLLATE utf8_unicode_ci,
152 `uri` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
153 `lastmodified` int(11) unsigned DEFAULT NULL,
154 `manually_edited` tinyint(4) NOT NULL DEFAULT '0',
155 `manually_deleted` tinyint(4) NOT NULL DEFAULT '0',
156 `etag` varchar(15) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
157 `size` int(10) unsigned NOT NULL,
159 UNIQUE KEY `namespace` (`namespace`,`namespace_id`,`contact`),
160 KEY `contact` (`contact`)
161 ) ENGINE=InnoDB DEFAULT CHARSET=utf8";
163 if (!in_array("dav_jqcalendar", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_jqcalendar` (
164 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
165 `ical_recurr_uri` varchar(100) DEFAULT NULL,
166 `calendar_id` int(10) unsigned NOT NULL,
167 `calendarobject_id` int(10) unsigned NOT NULL,
168 `Summary` varchar(100) NOT NULL,
169 `StartTime` timestamp NULL DEFAULT NULL,
170 `EndTime` timestamp NULL DEFAULT NULL,
171 `IsEditable` tinyint(3) unsigned NOT NULL,
172 `IsAllDayEvent` tinyint(4) NOT NULL,
173 `IsRecurring` tinyint(4) NOT NULL,
174 `Color` char(6) DEFAULT NULL,
176 KEY `calendarByStart` (`calendar_id`,`StartTime`),
177 KEY `calendarobject_id` (`calendarobject_id`,`ical_recurr_uri`)
178 ) ENGINE=InnoDB DEFAULT CHARSET=utf8";
180 if (!in_array("dav_notifications", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_notifications` (
181 `id` int(11) NOT NULL AUTO_INCREMENT,
182 `ical_recurr_uri` varchar(100) DEFAULT NULL,
183 `calendar_id` int(11) NOT NULL,
184 `calendarobject_id` int(10) unsigned NOT NULL,
185 `action` enum('email','display') NOT NULL DEFAULT 'email',
186 `alert_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
187 `notified` tinyint(4) NOT NULL DEFAULT '0',
189 KEY `notified` (`notified`,`alert_date`),
190 KEY `calendar_id` (`calendar_id`,`calendarobject_id`)
191 ) ENGINE=InnoDB DEFAULT CHARSET=utf8";
199 function dav_check_tables()
201 $x = q("DESCRIBE %s%scalendars", CALDAV_SQL_DB, CALDAV_SQL_PREFIX);
203 if (count($x) == 9) return 1; // Version 0.1
204 if (count($x) == 12) return 0; // Correct
205 return -2; // Unknown version
212 function dav_create_tables()
214 $stms = dav_get_create_statements();
218 foreach ($stms as $st) {
220 if ($db->error) $errors[] = $db->error;
229 function dav_upgrade_tables()
231 $ver = dav_check_tables();
232 if (!in_array($ver, array(1) )) return array("Unknown error");
234 $stms = dav_get_update_statements($ver);
238 foreach ($stms as $st) {
240 if ($db->error) $errors[] = $db->error;