Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like CalDavBackend often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CalDavBackend, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 69 | class CalDavBackend extends AbstractBackend implements SyncSupport, SubscriptionSupport, SchedulingSupport { |
||
| 70 | |||
| 71 | const PERSONAL_CALENDAR_URI = 'personal'; |
||
| 72 | const PERSONAL_CALENDAR_NAME = 'Personal'; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * We need to specify a max date, because we need to stop *somewhere* |
||
| 76 | * |
||
| 77 | * On 32 bit system the maximum for a signed integer is 2147483647, so |
||
| 78 | * MAX_DATE cannot be higher than date('Y-m-d', 2147483647) which results |
||
| 79 | * in 2038-01-19 to avoid problems when the date is converted |
||
| 80 | * to a unix timestamp. |
||
| 81 | */ |
||
| 82 | const MAX_DATE = '2038-01-01'; |
||
| 83 | |||
| 84 | const ACCESS_PUBLIC = 4; |
||
| 85 | const CLASSIFICATION_PUBLIC = 0; |
||
| 86 | const CLASSIFICATION_PRIVATE = 1; |
||
| 87 | const CLASSIFICATION_CONFIDENTIAL = 2; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * List of CalDAV properties, and how they map to database field names |
||
| 91 | * Add your own properties by simply adding on to this array. |
||
| 92 | * |
||
| 93 | * Note that only string-based properties are supported here. |
||
| 94 | * |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | public $propertyMap = [ |
||
| 98 | '{DAV:}displayname' => 'displayname', |
||
| 99 | '{urn:ietf:params:xml:ns:caldav}calendar-description' => 'description', |
||
| 100 | '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => 'timezone', |
||
| 101 | '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
||
| 102 | '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
||
| 103 | ]; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * List of subscription properties, and how they map to database field names. |
||
| 107 | * |
||
| 108 | * @var array |
||
| 109 | */ |
||
| 110 | public $subscriptionPropertyMap = [ |
||
| 111 | '{DAV:}displayname' => 'displayname', |
||
| 112 | '{http://apple.com/ns/ical/}refreshrate' => 'refreshrate', |
||
| 113 | '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
||
| 114 | '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
||
| 115 | '{http://calendarserver.org/ns/}subscribed-strip-todos' => 'striptodos', |
||
| 116 | '{http://calendarserver.org/ns/}subscribed-strip-alarms' => 'stripalarms', |
||
| 117 | '{http://calendarserver.org/ns/}subscribed-strip-attachments' => 'stripattachments', |
||
| 118 | ]; |
||
| 119 | |||
| 120 | /** @var array properties to index */ |
||
| 121 | public static $indexProperties = ['CATEGORIES', 'COMMENT', 'DESCRIPTION', |
||
| 122 | 'LOCATION', 'RESOURCES', 'STATUS', 'SUMMARY', 'ATTENDEE', 'CONTACT', |
||
| 123 | 'ORGANIZER']; |
||
| 124 | |||
| 125 | /** @var array parameters to index */ |
||
| 126 | public static $indexParameters = [ |
||
| 127 | 'ATTENDEE' => ['CN'], |
||
| 128 | 'ORGANIZER' => ['CN'], |
||
| 129 | ]; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var string[] Map of uid => display name |
||
| 133 | */ |
||
| 134 | protected $userDisplayNames; |
||
| 135 | |||
| 136 | /** @var IDBConnection */ |
||
| 137 | private $db; |
||
| 138 | |||
| 139 | /** @var Backend */ |
||
| 140 | private $sharingBackend; |
||
| 141 | |||
| 142 | /** @var Principal */ |
||
| 143 | private $principalBackend; |
||
| 144 | |||
| 145 | /** @var IUserManager */ |
||
| 146 | private $userManager; |
||
| 147 | |||
| 148 | /** @var ISecureRandom */ |
||
| 149 | private $random; |
||
| 150 | |||
| 151 | /** @var EventDispatcherInterface */ |
||
| 152 | private $dispatcher; |
||
| 153 | |||
| 154 | /** @var bool */ |
||
| 155 | private $legacyEndpoint; |
||
| 156 | |||
| 157 | /** @var string */ |
||
| 158 | private $dbObjectPropertiesTable = 'calendarobjects_props'; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * CalDavBackend constructor. |
||
| 162 | * |
||
| 163 | * @param IDBConnection $db |
||
| 164 | * @param Principal $principalBackend |
||
| 165 | * @param IUserManager $userManager |
||
| 166 | * @param IGroupManager $groupManager |
||
| 167 | * @param ISecureRandom $random |
||
| 168 | * @param EventDispatcherInterface $dispatcher |
||
| 169 | * @param bool $legacyEndpoint |
||
| 170 | */ |
||
| 171 | public function __construct(IDBConnection $db, |
||
| 172 | Principal $principalBackend, |
||
| 173 | IUserManager $userManager, |
||
| 174 | IGroupManager $groupManager, |
||
| 175 | ISecureRandom $random, |
||
| 176 | EventDispatcherInterface $dispatcher, |
||
| 177 | $legacyEndpoint = false) { |
||
| 178 | $this->db = $db; |
||
| 179 | $this->principalBackend = $principalBackend; |
||
| 180 | $this->userManager = $userManager; |
||
| 181 | $this->sharingBackend = new Backend($this->db, $this->userManager, $groupManager, $principalBackend, 'calendar'); |
||
| 182 | $this->random = $random; |
||
| 183 | $this->dispatcher = $dispatcher; |
||
| 184 | $this->legacyEndpoint = $legacyEndpoint; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Return the number of calendars for a principal |
||
| 189 | * |
||
| 190 | * By default this excludes the automatically generated birthday calendar |
||
| 191 | * |
||
| 192 | * @param $principalUri |
||
| 193 | * @param bool $excludeBirthday |
||
| 194 | * @return int |
||
| 195 | */ |
||
| 196 | View Code Duplication | public function getCalendarsForUserCount($principalUri, $excludeBirthday = true) { |
|
| 197 | $principalUri = $this->convertPrincipal($principalUri, true); |
||
| 198 | $query = $this->db->getQueryBuilder(); |
||
| 199 | $query->select($query->createFunction('COUNT(*)')) |
||
| 200 | ->from('calendars') |
||
| 201 | ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
||
| 202 | |||
| 203 | if ($excludeBirthday) { |
||
| 204 | $query->andWhere($query->expr()->neq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI))); |
||
| 205 | } |
||
| 206 | |||
| 207 | return (int)$query->execute()->fetchColumn(); |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Returns a list of calendars for a principal. |
||
| 212 | * |
||
| 213 | * Every project is an array with the following keys: |
||
| 214 | * * id, a unique id that will be used by other functions to modify the |
||
| 215 | * calendar. This can be the same as the uri or a database key. |
||
| 216 | * * uri, which the basename of the uri with which the calendar is |
||
| 217 | * accessed. |
||
| 218 | * * principaluri. The owner of the calendar. Almost always the same as |
||
| 219 | * principalUri passed to this method. |
||
| 220 | * |
||
| 221 | * Furthermore it can contain webdav properties in clark notation. A very |
||
| 222 | * common one is '{DAV:}displayname'. |
||
| 223 | * |
||
| 224 | * Many clients also require: |
||
| 225 | * {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
||
| 226 | * For this property, you can just return an instance of |
||
| 227 | * Sabre\CalDAV\Property\SupportedCalendarComponentSet. |
||
| 228 | * |
||
| 229 | * If you return {http://sabredav.org/ns}read-only and set the value to 1, |
||
| 230 | * ACL will automatically be put in read-only mode. |
||
| 231 | * |
||
| 232 | * @param string $principalUri |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | function getCalendarsForUser($principalUri) { |
||
|
|
|||
| 236 | $principalUriOriginal = $principalUri; |
||
| 237 | $principalUri = $this->convertPrincipal($principalUri, true); |
||
| 238 | $fields = array_values($this->propertyMap); |
||
| 239 | $fields[] = 'id'; |
||
| 240 | $fields[] = 'uri'; |
||
| 241 | $fields[] = 'synctoken'; |
||
| 242 | $fields[] = 'components'; |
||
| 243 | $fields[] = 'principaluri'; |
||
| 244 | $fields[] = 'transparent'; |
||
| 245 | |||
| 246 | // Making fields a comma-delimited list |
||
| 247 | $query = $this->db->getQueryBuilder(); |
||
| 248 | $query->select($fields)->from('calendars') |
||
| 249 | ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
||
| 250 | ->orderBy('calendarorder', 'ASC'); |
||
| 251 | $stmt = $query->execute(); |
||
| 252 | |||
| 253 | $calendars = []; |
||
| 254 | while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
||
| 255 | |||
| 256 | $components = []; |
||
| 257 | if ($row['components']) { |
||
| 258 | $components = explode(',',$row['components']); |
||
| 259 | } |
||
| 260 | |||
| 261 | $calendar = [ |
||
| 262 | 'id' => $row['id'], |
||
| 263 | 'uri' => $row['uri'], |
||
| 264 | 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
||
| 265 | '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
||
| 266 | '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
||
| 267 | '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
||
| 268 | '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
||
| 269 | '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
||
| 270 | ]; |
||
| 271 | |||
| 272 | foreach($this->propertyMap as $xmlName=>$dbName) { |
||
| 273 | $calendar[$xmlName] = $row[$dbName]; |
||
| 274 | } |
||
| 275 | |||
| 276 | $this->addOwnerPrincipal($calendar); |
||
| 277 | |||
| 278 | if (!isset($calendars[$calendar['id']])) { |
||
| 279 | $calendars[$calendar['id']] = $calendar; |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | $stmt->closeCursor(); |
||
| 284 | |||
| 285 | // query for shared calendars |
||
| 286 | $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
||
| 287 | $principals = array_map(function($principal) { |
||
| 288 | return urldecode($principal); |
||
| 289 | }, $principals); |
||
| 290 | $principals[]= $principalUri; |
||
| 291 | |||
| 292 | $fields = array_values($this->propertyMap); |
||
| 293 | $fields[] = 'a.id'; |
||
| 294 | $fields[] = 'a.uri'; |
||
| 295 | $fields[] = 'a.synctoken'; |
||
| 296 | $fields[] = 'a.components'; |
||
| 297 | $fields[] = 'a.principaluri'; |
||
| 298 | $fields[] = 'a.transparent'; |
||
| 299 | $fields[] = 's.access'; |
||
| 300 | $query = $this->db->getQueryBuilder(); |
||
| 301 | $result = $query->select($fields) |
||
| 302 | ->from('dav_shares', 's') |
||
| 303 | ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
||
| 304 | ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
||
| 305 | ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
||
| 306 | ->setParameter('type', 'calendar') |
||
| 307 | ->setParameter('principaluri', $principals, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY) |
||
| 308 | ->execute(); |
||
| 309 | |||
| 310 | $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
||
| 311 | while($row = $result->fetch()) { |
||
| 312 | if ($row['principaluri'] === $principalUri) { |
||
| 313 | continue; |
||
| 314 | } |
||
| 315 | |||
| 316 | $readOnly = (int) $row['access'] === Backend::ACCESS_READ; |
||
| 317 | View Code Duplication | if (isset($calendars[$row['id']])) { |
|
| 318 | if ($readOnly) { |
||
| 319 | // New share can not have more permissions then the old one. |
||
| 320 | continue; |
||
| 321 | } |
||
| 322 | if (isset($calendars[$row['id']][$readOnlyPropertyName]) && |
||
| 323 | $calendars[$row['id']][$readOnlyPropertyName] === 0) { |
||
| 324 | // Old share is already read-write, no more permissions can be gained |
||
| 325 | continue; |
||
| 326 | } |
||
| 327 | } |
||
| 328 | |||
| 329 | list(, $name) = Uri\split($row['principaluri']); |
||
| 330 | $uri = $row['uri'] . '_shared_by_' . $name; |
||
| 331 | $row['displayname'] = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')'; |
||
| 332 | $components = []; |
||
| 333 | if ($row['components']) { |
||
| 334 | $components = explode(',',$row['components']); |
||
| 335 | } |
||
| 336 | $calendar = [ |
||
| 337 | 'id' => $row['id'], |
||
| 338 | 'uri' => $uri, |
||
| 339 | 'principaluri' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
||
| 340 | '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
||
| 341 | '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
||
| 342 | '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
||
| 343 | '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
||
| 344 | '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
||
| 345 | $readOnlyPropertyName => $readOnly, |
||
| 346 | ]; |
||
| 347 | |||
| 348 | foreach($this->propertyMap as $xmlName=>$dbName) { |
||
| 349 | $calendar[$xmlName] = $row[$dbName]; |
||
| 350 | } |
||
| 351 | |||
| 352 | $this->addOwnerPrincipal($calendar); |
||
| 353 | |||
| 354 | $calendars[$calendar['id']] = $calendar; |
||
| 355 | } |
||
| 356 | $result->closeCursor(); |
||
| 357 | |||
| 358 | return array_values($calendars); |
||
| 359 | } |
||
| 360 | |||
| 361 | public function getUsersOwnCalendars($principalUri) { |
||
| 404 | |||
| 405 | |||
| 406 | View Code Duplication | private function getUserDisplayName($uid) { |
|
| 419 | |||
| 420 | /** |
||
| 421 | * @return array |
||
| 422 | */ |
||
| 423 | public function getPublicCalendars() { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @param string $uri |
||
| 479 | * @return array |
||
| 480 | * @throws NotFound |
||
| 481 | */ |
||
| 482 | public function getPublicCalendar($uri) { |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @param string $principal |
||
| 540 | * @param string $uri |
||
| 541 | * @return array|null |
||
| 542 | */ |
||
| 543 | public function getCalendarByUri($principal, $uri) { |
||
| 589 | |||
| 590 | public function getCalendarById($calendarId) { |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Creates a new calendar for a principal. |
||
| 638 | * |
||
| 639 | * If the creation was a success, an id must be returned that can be used to reference |
||
| 640 | * this calendar in other methods, such as updateCalendar. |
||
| 641 | * |
||
| 642 | * @param string $principalUri |
||
| 643 | * @param string $calendarUri |
||
| 644 | * @param array $properties |
||
| 645 | * @return int |
||
| 646 | * @suppress SqlInjectionChecker |
||
| 647 | */ |
||
| 648 | function createCalendar($principalUri, $calendarUri, array $properties) { |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Updates properties for a calendar. |
||
| 697 | * |
||
| 698 | * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
||
| 699 | * To do the actual updates, you must tell this object which properties |
||
| 700 | * you're going to process with the handle() method. |
||
| 701 | * |
||
| 702 | * Calling the handle method is like telling the PropPatch object "I |
||
| 703 | * promise I can handle updating this property". |
||
| 704 | * |
||
| 705 | * Read the PropPatch documentation for more info and examples. |
||
| 706 | * |
||
| 707 | * @param mixed $calendarId |
||
| 708 | * @param PropPatch $propPatch |
||
| 709 | * @return void |
||
| 710 | */ |
||
| 711 | function updateCalendar($calendarId, PropPatch $propPatch) { |
||
| 756 | |||
| 757 | /** |
||
| 758 | * Delete a calendar and all it's objects |
||
| 759 | * |
||
| 760 | * @param mixed $calendarId |
||
| 761 | * @return void |
||
| 762 | */ |
||
| 763 | function deleteCalendar($calendarId) { |
||
| 788 | |||
| 789 | /** |
||
| 790 | * Delete all of an user's shares |
||
| 791 | * |
||
| 792 | * @param string $principaluri |
||
| 793 | * @return void |
||
| 794 | */ |
||
| 795 | function deleteAllSharesByUser($principaluri) { |
||
| 798 | |||
| 799 | /** |
||
| 800 | * Returns all calendar objects within a calendar. |
||
| 801 | * |
||
| 802 | * Every item contains an array with the following keys: |
||
| 803 | * * calendardata - The iCalendar-compatible calendar data |
||
| 804 | * * uri - a unique key which will be used to construct the uri. This can |
||
| 805 | * be any arbitrary string, but making sure it ends with '.ics' is a |
||
| 806 | * good idea. This is only the basename, or filename, not the full |
||
| 807 | * path. |
||
| 808 | * * lastmodified - a timestamp of the last modification time |
||
| 809 | * * etag - An arbitrary string, surrounded by double-quotes. (e.g.: |
||
| 810 | * '"abcdef"') |
||
| 811 | * * size - The size of the calendar objects, in bytes. |
||
| 812 | * * component - optional, a string containing the type of object, such |
||
| 813 | * as 'vevent' or 'vtodo'. If specified, this will be used to populate |
||
| 814 | * the Content-Type header. |
||
| 815 | * |
||
| 816 | * Note that the etag is optional, but it's highly encouraged to return for |
||
| 817 | * speed reasons. |
||
| 818 | * |
||
| 819 | * The calendardata is also optional. If it's not returned |
||
| 820 | * 'getCalendarObject' will be called later, which *is* expected to return |
||
| 821 | * calendardata. |
||
| 822 | * |
||
| 823 | * If neither etag or size are specified, the calendardata will be |
||
| 824 | * used/fetched to determine these numbers. If both are specified the |
||
| 825 | * amount of times this is needed is reduced by a great degree. |
||
| 826 | * |
||
| 827 | * @param mixed $calendarId |
||
| 828 | * @return array |
||
| 829 | */ |
||
| 830 | function getCalendarObjects($calendarId) { |
||
| 853 | |||
| 854 | /** |
||
| 855 | * Returns information from a single calendar object, based on it's object |
||
| 856 | * uri. |
||
| 857 | * |
||
| 858 | * The object uri is only the basename, or filename and not a full path. |
||
| 859 | * |
||
| 860 | * The returned array must have the same keys as getCalendarObjects. The |
||
| 861 | * 'calendardata' object is required here though, while it's not required |
||
| 862 | * for getCalendarObjects. |
||
| 863 | * |
||
| 864 | * This method must return null if the object did not exist. |
||
| 865 | * |
||
| 866 | * @param mixed $calendarId |
||
| 867 | * @param string $objectUri |
||
| 868 | * @return array|null |
||
| 869 | */ |
||
| 870 | function getCalendarObject($calendarId, $objectUri) { |
||
| 894 | |||
| 895 | /** |
||
| 896 | * Returns a list of calendar objects. |
||
| 897 | * |
||
| 898 | * This method should work identical to getCalendarObject, but instead |
||
| 899 | * return all the calendar objects in the list as an array. |
||
| 900 | * |
||
| 901 | * If the backend supports this, it may allow for some speed-ups. |
||
| 902 | * |
||
| 903 | * @param mixed $calendarId |
||
| 904 | * @param string[] $uris |
||
| 905 | * @return array |
||
| 906 | */ |
||
| 907 | function getMultipleCalendarObjects($calendarId, array $uris) { |
||
| 942 | |||
| 943 | /** |
||
| 944 | * Creates a new calendar object. |
||
| 945 | * |
||
| 946 | * The object uri is only the basename, or filename and not a full path. |
||
| 947 | * |
||
| 948 | * It is possible return an etag from this function, which will be used in |
||
| 949 | * the response to this PUT request. Note that the ETag must be surrounded |
||
| 950 | * by double-quotes. |
||
| 951 | * |
||
| 952 | * However, you should only really return this ETag if you don't mangle the |
||
| 953 | * calendar-data. If the result of a subsequent GET to this object is not |
||
| 954 | * the exact same as this request body, you should omit the ETag. |
||
| 955 | * |
||
| 956 | * @param mixed $calendarId |
||
| 957 | * @param string $objectUri |
||
| 958 | * @param string $calendarData |
||
| 959 | * @return string |
||
| 960 | */ |
||
| 961 | function createCalendarObject($calendarId, $objectUri, $calendarData) { |
||
| 1010 | |||
| 1011 | /** |
||
| 1012 | * Updates an existing calendarobject, based on it's uri. |
||
| 1013 | * |
||
| 1014 | * The object uri is only the basename, or filename and not a full path. |
||
| 1015 | * |
||
| 1016 | * It is possible return an etag from this function, which will be used in |
||
| 1017 | * the response to this PUT request. Note that the ETag must be surrounded |
||
| 1018 | * by double-quotes. |
||
| 1019 | * |
||
| 1020 | * However, you should only really return this ETag if you don't mangle the |
||
| 1021 | * calendar-data. If the result of a subsequent GET to this object is not |
||
| 1022 | * the exact same as this request body, you should omit the ETag. |
||
| 1023 | * |
||
| 1024 | * @param mixed $calendarId |
||
| 1025 | * @param string $objectUri |
||
| 1026 | * @param string $calendarData |
||
| 1027 | * @return string |
||
| 1028 | */ |
||
| 1029 | function updateCalendarObject($calendarId, $objectUri, $calendarData) { |
||
| 1065 | |||
| 1066 | /** |
||
| 1067 | * @param int $calendarObjectId |
||
| 1068 | * @param int $classification |
||
| 1069 | */ |
||
| 1070 | public function setClassification($calendarObjectId, $classification) { |
||
| 1082 | |||
| 1083 | /** |
||
| 1084 | * Deletes an existing calendar object. |
||
| 1085 | * |
||
| 1086 | * The object uri is only the basename, or filename and not a full path. |
||
| 1087 | * |
||
| 1088 | * @param mixed $calendarId |
||
| 1089 | * @param string $objectUri |
||
| 1090 | * @return void |
||
| 1091 | */ |
||
| 1092 | function deleteCalendarObject($calendarId, $objectUri) { |
||
| 1113 | |||
| 1114 | /** |
||
| 1115 | * Performs a calendar-query on the contents of this calendar. |
||
| 1116 | * |
||
| 1117 | * The calendar-query is defined in RFC4791 : CalDAV. Using the |
||
| 1118 | * calendar-query it is possible for a client to request a specific set of |
||
| 1119 | * object, based on contents of iCalendar properties, date-ranges and |
||
| 1120 | * iCalendar component types (VTODO, VEVENT). |
||
| 1121 | * |
||
| 1122 | * This method should just return a list of (relative) urls that match this |
||
| 1123 | * query. |
||
| 1124 | * |
||
| 1125 | * The list of filters are specified as an array. The exact array is |
||
| 1126 | * documented by Sabre\CalDAV\CalendarQueryParser. |
||
| 1127 | * |
||
| 1128 | * Note that it is extremely likely that getCalendarObject for every path |
||
| 1129 | * returned from this method will be called almost immediately after. You |
||
| 1130 | * may want to anticipate this to speed up these requests. |
||
| 1131 | * |
||
| 1132 | * This method provides a default implementation, which parses *all* the |
||
| 1133 | * iCalendar objects in the specified calendar. |
||
| 1134 | * |
||
| 1135 | * This default may well be good enough for personal use, and calendars |
||
| 1136 | * that aren't very large. But if you anticipate high usage, big calendars |
||
| 1137 | * or high loads, you are strongly advised to optimize certain paths. |
||
| 1138 | * |
||
| 1139 | * The best way to do so is override this method and to optimize |
||
| 1140 | * specifically for 'common filters'. |
||
| 1141 | * |
||
| 1142 | * Requests that are extremely common are: |
||
| 1143 | * * requests for just VEVENTS |
||
| 1144 | * * requests for just VTODO |
||
| 1145 | * * requests with a time-range-filter on either VEVENT or VTODO. |
||
| 1146 | * |
||
| 1147 | * ..and combinations of these requests. It may not be worth it to try to |
||
| 1148 | * handle every possible situation and just rely on the (relatively |
||
| 1149 | * easy to use) CalendarQueryValidator to handle the rest. |
||
| 1150 | * |
||
| 1151 | * Note that especially time-range-filters may be difficult to parse. A |
||
| 1152 | * time-range filter specified on a VEVENT must for instance also handle |
||
| 1153 | * recurrence rules correctly. |
||
| 1154 | * A good example of how to interprete all these filters can also simply |
||
| 1155 | * be found in Sabre\CalDAV\CalendarQueryFilter. This class is as correct |
||
| 1156 | * as possible, so it gives you a good idea on what type of stuff you need |
||
| 1157 | * to think of. |
||
| 1158 | * |
||
| 1159 | * @param mixed $calendarId |
||
| 1160 | * @param array $filters |
||
| 1161 | * @return array |
||
| 1162 | */ |
||
| 1163 | function calendarQuery($calendarId, array $filters) { |
||
| 1227 | |||
| 1228 | /** |
||
| 1229 | * custom Nextcloud search extension for CalDAV |
||
| 1230 | * |
||
| 1231 | * @param string $principalUri |
||
| 1232 | * @param array $filters |
||
| 1233 | * @param integer|null $limit |
||
| 1234 | * @param integer|null $offset |
||
| 1235 | * @return array |
||
| 1236 | */ |
||
| 1237 | public function calendarSearch($principalUri, array $filters, $limit=null, $offset=null) { |
||
| 1346 | |||
| 1347 | /** |
||
| 1348 | * Searches through all of a users calendars and calendar objects to find |
||
| 1349 | * an object with a specific UID. |
||
| 1350 | * |
||
| 1351 | * This method should return the path to this object, relative to the |
||
| 1352 | * calendar home, so this path usually only contains two parts: |
||
| 1353 | * |
||
| 1354 | * calendarpath/objectpath.ics |
||
| 1355 | * |
||
| 1356 | * If the uid is not found, return null. |
||
| 1357 | * |
||
| 1358 | * This method should only consider * objects that the principal owns, so |
||
| 1359 | * any calendars owned by other principals that also appear in this |
||
| 1360 | * collection should be ignored. |
||
| 1361 | * |
||
| 1362 | * @param string $principalUri |
||
| 1363 | * @param string $uid |
||
| 1364 | * @return string|null |
||
| 1365 | */ |
||
| 1366 | function getCalendarObjectByUID($principalUri, $uid) { |
||
| 1383 | |||
| 1384 | /** |
||
| 1385 | * The getChanges method returns all the changes that have happened, since |
||
| 1386 | * the specified syncToken in the specified calendar. |
||
| 1387 | * |
||
| 1388 | * This function should return an array, such as the following: |
||
| 1389 | * |
||
| 1390 | * [ |
||
| 1391 | * 'syncToken' => 'The current synctoken', |
||
| 1392 | * 'added' => [ |
||
| 1393 | * 'new.txt', |
||
| 1394 | * ], |
||
| 1395 | * 'modified' => [ |
||
| 1396 | * 'modified.txt', |
||
| 1397 | * ], |
||
| 1398 | * 'deleted' => [ |
||
| 1399 | * 'foo.php.bak', |
||
| 1400 | * 'old.txt' |
||
| 1401 | * ] |
||
| 1402 | * ); |
||
| 1403 | * |
||
| 1404 | * The returned syncToken property should reflect the *current* syncToken |
||
| 1405 | * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
||
| 1406 | * property This is * needed here too, to ensure the operation is atomic. |
||
| 1407 | * |
||
| 1408 | * If the $syncToken argument is specified as null, this is an initial |
||
| 1409 | * sync, and all members should be reported. |
||
| 1410 | * |
||
| 1411 | * The modified property is an array of nodenames that have changed since |
||
| 1412 | * the last token. |
||
| 1413 | * |
||
| 1414 | * The deleted property is an array with nodenames, that have been deleted |
||
| 1415 | * from collection. |
||
| 1416 | * |
||
| 1417 | * The $syncLevel argument is basically the 'depth' of the report. If it's |
||
| 1418 | * 1, you only have to report changes that happened only directly in |
||
| 1419 | * immediate descendants. If it's 2, it should also include changes from |
||
| 1420 | * the nodes below the child collections. (grandchildren) |
||
| 1421 | * |
||
| 1422 | * The $limit argument allows a client to specify how many results should |
||
| 1423 | * be returned at most. If the limit is not specified, it should be treated |
||
| 1424 | * as infinite. |
||
| 1425 | * |
||
| 1426 | * If the limit (infinite or not) is higher than you're willing to return, |
||
| 1427 | * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
||
| 1428 | * |
||
| 1429 | * If the syncToken is expired (due to data cleanup) or unknown, you must |
||
| 1430 | * return null. |
||
| 1431 | * |
||
| 1432 | * The limit is 'suggestive'. You are free to ignore it. |
||
| 1433 | * |
||
| 1434 | * @param string $calendarId |
||
| 1435 | * @param string $syncToken |
||
| 1436 | * @param int $syncLevel |
||
| 1437 | * @param int $limit |
||
| 1438 | * @return array |
||
| 1439 | */ |
||
| 1440 | View Code Duplication | function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null) { |
|
| 1504 | |||
| 1505 | /** |
||
| 1506 | * Returns a list of subscriptions for a principal. |
||
| 1507 | * |
||
| 1508 | * Every subscription is an array with the following keys: |
||
| 1509 | * * id, a unique id that will be used by other functions to modify the |
||
| 1510 | * subscription. This can be the same as the uri or a database key. |
||
| 1511 | * * uri. This is just the 'base uri' or 'filename' of the subscription. |
||
| 1512 | * * principaluri. The owner of the subscription. Almost always the same as |
||
| 1513 | * principalUri passed to this method. |
||
| 1514 | * |
||
| 1515 | * Furthermore, all the subscription info must be returned too: |
||
| 1516 | * |
||
| 1517 | * 1. {DAV:}displayname |
||
| 1518 | * 2. {http://apple.com/ns/ical/}refreshrate |
||
| 1519 | * 3. {http://calendarserver.org/ns/}subscribed-strip-todos (omit if todos |
||
| 1520 | * should not be stripped). |
||
| 1521 | * 4. {http://calendarserver.org/ns/}subscribed-strip-alarms (omit if alarms |
||
| 1522 | * should not be stripped). |
||
| 1523 | * 5. {http://calendarserver.org/ns/}subscribed-strip-attachments (omit if |
||
| 1524 | * attachments should not be stripped). |
||
| 1525 | * 6. {http://calendarserver.org/ns/}source (Must be a |
||
| 1526 | * Sabre\DAV\Property\Href). |
||
| 1527 | * 7. {http://apple.com/ns/ical/}calendar-color |
||
| 1528 | * 8. {http://apple.com/ns/ical/}calendar-order |
||
| 1529 | * 9. {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
||
| 1530 | * (should just be an instance of |
||
| 1531 | * Sabre\CalDAV\Property\SupportedCalendarComponentSet, with a bunch of |
||
| 1532 | * default components). |
||
| 1533 | * |
||
| 1534 | * @param string $principalUri |
||
| 1535 | * @return array |
||
| 1536 | */ |
||
| 1537 | function getSubscriptionsForUser($principalUri) { |
||
| 1577 | |||
| 1578 | /** |
||
| 1579 | * Creates a new subscription for a principal. |
||
| 1580 | * |
||
| 1581 | * If the creation was a success, an id must be returned that can be used to reference |
||
| 1582 | * this subscription in other methods, such as updateSubscription. |
||
| 1583 | * |
||
| 1584 | * @param string $principalUri |
||
| 1585 | * @param string $uri |
||
| 1586 | * @param array $properties |
||
| 1587 | * @return mixed |
||
| 1588 | */ |
||
| 1589 | function createSubscription($principalUri, $uri, array $properties) { |
||
| 1627 | |||
| 1628 | /** |
||
| 1629 | * Updates a subscription |
||
| 1630 | * |
||
| 1631 | * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
||
| 1632 | * To do the actual updates, you must tell this object which properties |
||
| 1633 | * you're going to process with the handle() method. |
||
| 1634 | * |
||
| 1635 | * Calling the handle method is like telling the PropPatch object "I |
||
| 1636 | * promise I can handle updating this property". |
||
| 1637 | * |
||
| 1638 | * Read the PropPatch documentation for more info and examples. |
||
| 1639 | * |
||
| 1640 | * @param mixed $subscriptionId |
||
| 1641 | * @param PropPatch $propPatch |
||
| 1642 | * @return void |
||
| 1643 | */ |
||
| 1644 | function updateSubscription($subscriptionId, PropPatch $propPatch) { |
||
| 1677 | |||
| 1678 | /** |
||
| 1679 | * Deletes a subscription. |
||
| 1680 | * |
||
| 1681 | * @param mixed $subscriptionId |
||
| 1682 | * @return void |
||
| 1683 | */ |
||
| 1684 | function deleteSubscription($subscriptionId) { |
||
| 1690 | |||
| 1691 | /** |
||
| 1692 | * Returns a single scheduling object for the inbox collection. |
||
| 1693 | * |
||
| 1694 | * The returned array should contain the following elements: |
||
| 1695 | * * uri - A unique basename for the object. This will be used to |
||
| 1696 | * construct a full uri. |
||
| 1697 | * * calendardata - The iCalendar object |
||
| 1698 | * * lastmodified - The last modification date. Can be an int for a unix |
||
| 1699 | * timestamp, or a PHP DateTime object. |
||
| 1700 | * * etag - A unique token that must change if the object changed. |
||
| 1701 | * * size - The size of the object, in bytes. |
||
| 1702 | * |
||
| 1703 | * @param string $principalUri |
||
| 1704 | * @param string $objectUri |
||
| 1705 | * @return array |
||
| 1706 | */ |
||
| 1707 | function getSchedulingObject($principalUri, $objectUri) { |
||
| 1729 | |||
| 1730 | /** |
||
| 1731 | * Returns all scheduling objects for the inbox collection. |
||
| 1732 | * |
||
| 1733 | * These objects should be returned as an array. Every item in the array |
||
| 1734 | * should follow the same structure as returned from getSchedulingObject. |
||
| 1735 | * |
||
| 1736 | * The main difference is that 'calendardata' is optional. |
||
| 1737 | * |
||
| 1738 | * @param string $principalUri |
||
| 1739 | * @return array |
||
| 1740 | */ |
||
| 1741 | function getSchedulingObjects($principalUri) { |
||
| 1761 | |||
| 1762 | /** |
||
| 1763 | * Deletes a scheduling object from the inbox collection. |
||
| 1764 | * |
||
| 1765 | * @param string $principalUri |
||
| 1766 | * @param string $objectUri |
||
| 1767 | * @return void |
||
| 1768 | */ |
||
| 1769 | function deleteSchedulingObject($principalUri, $objectUri) { |
||
| 1776 | |||
| 1777 | /** |
||
| 1778 | * Creates a new scheduling object. This should land in a users' inbox. |
||
| 1779 | * |
||
| 1780 | * @param string $principalUri |
||
| 1781 | * @param string $objectUri |
||
| 1782 | * @param string $objectData |
||
| 1783 | * @return void |
||
| 1784 | */ |
||
| 1785 | function createSchedulingObject($principalUri, $objectUri, $objectData) { |
||
| 1798 | |||
| 1799 | /** |
||
| 1800 | * Adds a change record to the calendarchanges table. |
||
| 1801 | * |
||
| 1802 | * @param mixed $calendarId |
||
| 1803 | * @param string $objectUri |
||
| 1804 | * @param int $operation 1 = add, 2 = modify, 3 = delete. |
||
| 1805 | * @return void |
||
| 1806 | */ |
||
| 1807 | View Code Duplication | protected function addChange($calendarId, $objectUri, $operation) { |
|
| 1822 | |||
| 1823 | /** |
||
| 1824 | * Parses some information from calendar objects, used for optimized |
||
| 1825 | * calendar-queries. |
||
| 1826 | * |
||
| 1827 | * Returns an array with the following keys: |
||
| 1828 | * * etag - An md5 checksum of the object without the quotes. |
||
| 1829 | * * size - Size of the object in bytes |
||
| 1830 | * * componentType - VEVENT, VTODO or VJOURNAL |
||
| 1831 | * * firstOccurence |
||
| 1832 | * * lastOccurence |
||
| 1833 | * * uid - value of the UID property |
||
| 1834 | * |
||
| 1835 | * @param string $calendarData |
||
| 1836 | * @return array |
||
| 1837 | */ |
||
| 1838 | public function getDenormalizedData($calendarData) { |
||
| 1914 | |||
| 1915 | private function readBlob($cardData) { |
||
| 1922 | |||
| 1923 | /** |
||
| 1924 | * @param IShareable $shareable |
||
| 1925 | * @param array $add |
||
| 1926 | * @param array $remove |
||
| 1927 | */ |
||
| 1928 | public function updateShares($shareable, $add, $remove) { |
||
| 1941 | |||
| 1942 | /** |
||
| 1943 | * @param int $resourceId |
||
| 1944 | * @return array |
||
| 1945 | */ |
||
| 1946 | public function getShares($resourceId) { |
||
| 1949 | |||
| 1950 | /** |
||
| 1951 | * @param boolean $value |
||
| 1952 | * @param \OCA\DAV\CalDAV\Calendar $calendar |
||
| 1953 | * @return string|null |
||
| 1954 | */ |
||
| 1955 | public function setPublishStatus($value, $calendar) { |
||
| 1976 | |||
| 1977 | /** |
||
| 1978 | * @param \OCA\DAV\CalDAV\Calendar $calendar |
||
| 1979 | * @return mixed |
||
| 1980 | */ |
||
| 1981 | View Code Duplication | public function getPublishStatus($calendar) { |
|
| 1993 | |||
| 1994 | /** |
||
| 1995 | * @param int $resourceId |
||
| 1996 | * @param array $acl |
||
| 1997 | * @return array |
||
| 1998 | */ |
||
| 1999 | public function applyShareAcl($resourceId, $acl) { |
||
| 2002 | |||
| 2003 | |||
| 2004 | |||
| 2005 | /** |
||
| 2006 | * update properties table |
||
| 2007 | * |
||
| 2008 | * @param int $calendarId |
||
| 2009 | * @param string $objectUri |
||
| 2010 | * @param string $calendarData |
||
| 2011 | */ |
||
| 2012 | public function updateProperties($calendarId, $objectUri, $calendarData) { |
||
| 2078 | |||
| 2079 | /** |
||
| 2080 | * read VCalendar data into a VCalendar object |
||
| 2081 | * |
||
| 2082 | * @param string $objectData |
||
| 2083 | * @return VCalendar |
||
| 2084 | */ |
||
| 2085 | protected function readCalendarData($objectData) { |
||
| 2088 | |||
| 2089 | /** |
||
| 2090 | * delete all properties from a given calendar object |
||
| 2091 | * |
||
| 2092 | * @param int $calendarId |
||
| 2093 | * @param int $objectId |
||
| 2094 | */ |
||
| 2095 | View Code Duplication | protected function purgeProperties($calendarId, $objectId) { |
|
| 2102 | |||
| 2103 | /** |
||
| 2104 | * get ID from a given calendar object |
||
| 2105 | * |
||
| 2106 | * @param int $calendarId |
||
| 2107 | * @param string $uri |
||
| 2108 | * @return int |
||
| 2109 | */ |
||
| 2110 | View Code Duplication | protected function getCalendarObjectId($calendarId, $uri) { |
|
| 2126 | |||
| 2127 | View Code Duplication | private function convertPrincipal($principalUri, $toV2) { |
|
| 2137 | |||
| 2138 | View Code Duplication | private function addOwnerPrincipal(&$calendarInfo) { |
|
| 2152 | } |
||
| 2153 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.