]> git.mxchange.org Git - friendica-addons.git/blob - dav/database-init.inc.php
Merge pull request #73 from CatoTH/master
[friendica-addons.git] / dav / database-init.inc.php
1 <?php
2
3
4 /**
5  * @param int $from_version
6  * @return array|string[]
7  */
8 function dav_get_update_statements($from_version)
9 {
10         $stms = array();
11
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` ,
25                         `dav_caldav_log` ,
26                         `dav_calendars` ,
27                         `dav_cal_virtual_object_cache` ,
28                         `dav_cards` ,
29                         `dav_jqcalendar` ,
30                         `dav_locks` ,
31                         `dav_notifications` ;";
32
33                 $stms = array_merge($stms, dav_get_create_statements(array("dav_calendarobjects")));
34
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));
37
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`";
41
42                 $stms[] = "ALTER TABLE `dav_calendarobjects` DROP `namespace`, DROP `namespace_id`, DROP `user_temp`";
43
44         }
45
46         return $stms;
47 }
48
49 /**
50  * @param array $except
51  * @return array
52  */
53 function dav_get_create_statements($except = array())
54 {
55         $arr = array();
56
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',
60   PRIMARY KEY (`uid`)
61 ) ENGINE=MyISAM DEFAULT CHARSET=utf8";
62
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',
71   PRIMARY KEY (`id`),
72   UNIQUE KEY `principaluri` (`principaluri`,`uri`)
73 ) ENGINE=MyISAM  DEFAULT CHARSET=utf8";
74
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,
83   PRIMARY KEY (`id`),
84   KEY `mitglied` (`uid`)
85 ) ENGINE=MyISAM DEFAULT CHARSET=utf8";
86
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,
90   `calendardata` text,
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,
98   PRIMARY KEY (`id`),
99   UNIQUE KEY `uri` (`uri`),
100   KEY `calendar_id` (`calendar_id`)
101 ) ENGINE=MyISAM  DEFAULT CHARSET=utf8";
102
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,
116   PRIMARY KEY (`id`),
117   UNIQUE KEY (`namespace` , `namespace_id` , `uri`),
118   KEY `uri` (`uri`)
119 ) ENGINE=MyISAM  DEFAULT CHARSET=utf8";
120
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,
135   PRIMARY KEY (`id`),
136   KEY `data_uri` (`data_uri`),
137   KEY `ref_type` (`calendar_id`,`data_end`)
138 ) ENGINE=MyISAM  DEFAULT CHARSET=utf8";
139
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";
145
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,
158   PRIMARY KEY (`id`),
159   UNIQUE KEY `namespace` (`namespace`,`namespace_id`,`contact`),
160   KEY `contact` (`contact`)
161 ) ENGINE=InnoDB  DEFAULT CHARSET=utf8";
162
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,
175   PRIMARY KEY (`id`),
176   KEY `calendarByStart` (`calendar_id`,`StartTime`),
177   KEY `calendarobject_id` (`calendarobject_id`,`ical_recurr_uri`)
178 ) ENGINE=InnoDB  DEFAULT CHARSET=utf8";
179
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',
188   PRIMARY KEY (`id`),
189   KEY `notified` (`notified`,`alert_date`),
190   KEY `calendar_id` (`calendar_id`,`calendarobject_id`)
191 ) ENGINE=InnoDB  DEFAULT CHARSET=utf8";
192
193         return $arr;
194 }
195
196 /**
197  * @return int
198  */
199 function dav_check_tables()
200 {
201         $x = q("DESCRIBE %s%scalendars", CALDAV_SQL_DB, CALDAV_SQL_PREFIX);
202         if (!$x) return -1;
203         if (count($x) == 9) return 1; // Version 0.1
204         if (count($x) == 12) return 0; // Correct
205         return -2; // Unknown version
206 }
207
208
209 /**
210  * @return array
211  */
212 function dav_create_tables()
213 {
214         $stms   = dav_get_create_statements();
215         $errors = array();
216
217         global $db;
218         foreach ($stms as $st) {
219                 $db->q($st);
220                 if ($db->error) $errors[] = $db->error;
221         }
222
223         return $errors;
224 }
225
226 /**
227  * @return array
228  */
229 function dav_upgrade_tables()
230 {
231         $ver = dav_check_tables();
232         if (!in_array($ver, array(1) )) return array("Unknown error");
233
234         $stms   = dav_get_update_statements($ver);
235         $errors = array();
236
237         global $db;
238         foreach ($stms as $st) {
239                 $db->q($st);
240                 if ($db->error) $errors[] = $db->error;
241         }
242
243         return $errors;
244 }