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 |
||
| 76 | class CalDavBackend extends AbstractBackend implements SyncSupport, SubscriptionSupport, SchedulingSupport { |
||
| 77 | |||
| 78 | const PERSONAL_CALENDAR_URI = 'personal'; |
||
| 79 | const PERSONAL_CALENDAR_NAME = 'Personal'; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * We need to specify a max date, because we need to stop *somewhere* |
||
| 83 | * |
||
| 84 | * On 32 bit system the maximum for a signed integer is 2147483647, so |
||
| 85 | * MAX_DATE cannot be higher than date('Y-m-d', 2147483647) which results |
||
| 86 | * in 2038-01-19 to avoid problems when the date is converted |
||
| 87 | * to a unix timestamp. |
||
| 88 | */ |
||
| 89 | const MAX_DATE = '2038-01-01'; |
||
| 90 | |||
| 91 | const ACCESS_PUBLIC = 4; |
||
| 92 | const CLASSIFICATION_PUBLIC = 0; |
||
| 93 | const CLASSIFICATION_PRIVATE = 1; |
||
| 94 | const CLASSIFICATION_CONFIDENTIAL = 2; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * List of CalDAV properties, and how they map to database field names |
||
| 98 | * Add your own properties by simply adding on to this array. |
||
| 99 | * |
||
| 100 | * Note that only string-based properties are supported here. |
||
| 101 | * |
||
| 102 | * @var array |
||
| 103 | */ |
||
| 104 | public $propertyMap = [ |
||
| 105 | '{DAV:}displayname' => 'displayname', |
||
| 106 | '{urn:ietf:params:xml:ns:caldav}calendar-description' => 'description', |
||
| 107 | '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => 'timezone', |
||
| 108 | '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
||
| 109 | '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
||
| 110 | ]; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * List of subscription properties, and how they map to database field names. |
||
| 114 | * |
||
| 115 | * @var array |
||
| 116 | */ |
||
| 117 | public $subscriptionPropertyMap = [ |
||
| 118 | '{DAV:}displayname' => 'displayname', |
||
| 119 | '{http://apple.com/ns/ical/}refreshrate' => 'refreshrate', |
||
| 120 | '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
||
| 121 | '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
||
| 122 | '{http://calendarserver.org/ns/}subscribed-strip-todos' => 'striptodos', |
||
| 123 | '{http://calendarserver.org/ns/}subscribed-strip-alarms' => 'stripalarms', |
||
| 124 | '{http://calendarserver.org/ns/}subscribed-strip-attachments' => 'stripattachments', |
||
| 125 | ]; |
||
| 126 | |||
| 127 | /** @var array properties to index */ |
||
| 128 | public static $indexProperties = ['CATEGORIES', 'COMMENT', 'DESCRIPTION', |
||
| 129 | 'LOCATION', 'RESOURCES', 'STATUS', 'SUMMARY', 'ATTENDEE', 'CONTACT', |
||
| 130 | 'ORGANIZER']; |
||
| 131 | |||
| 132 | /** @var array parameters to index */ |
||
| 133 | public static $indexParameters = [ |
||
| 134 | 'ATTENDEE' => ['CN'], |
||
| 135 | 'ORGANIZER' => ['CN'], |
||
| 136 | ]; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var string[] Map of uid => display name |
||
| 140 | */ |
||
| 141 | protected $userDisplayNames; |
||
| 142 | |||
| 143 | /** @var IDBConnection */ |
||
| 144 | private $db; |
||
| 145 | |||
| 146 | /** @var Backend */ |
||
| 147 | private $sharingBackend; |
||
| 148 | |||
| 149 | /** @var Principal */ |
||
| 150 | private $principalBackend; |
||
| 151 | |||
| 152 | /** @var IUserManager */ |
||
| 153 | private $userManager; |
||
| 154 | |||
| 155 | /** @var ISecureRandom */ |
||
| 156 | private $random; |
||
| 157 | |||
| 158 | /** @var ILogger */ |
||
| 159 | private $logger; |
||
| 160 | |||
| 161 | /** @var EventDispatcherInterface */ |
||
| 162 | private $dispatcher; |
||
| 163 | |||
| 164 | /** @var bool */ |
||
| 165 | private $legacyEndpoint; |
||
| 166 | |||
| 167 | /** @var string */ |
||
| 168 | private $dbObjectPropertiesTable = 'calendarobjects_props'; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * CalDavBackend constructor. |
||
| 172 | * |
||
| 173 | * @param IDBConnection $db |
||
| 174 | * @param Principal $principalBackend |
||
| 175 | * @param IUserManager $userManager |
||
| 176 | * @param IGroupManager $groupManager |
||
| 177 | * @param ISecureRandom $random |
||
| 178 | * @param ILogger $logger |
||
| 179 | * @param EventDispatcherInterface $dispatcher |
||
| 180 | * @param bool $legacyEndpoint |
||
| 181 | */ |
||
| 182 | public function __construct(IDBConnection $db, |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Return the number of calendars for a principal |
||
| 202 | * |
||
| 203 | * By default this excludes the automatically generated birthday calendar |
||
| 204 | * |
||
| 205 | * @param $principalUri |
||
| 206 | * @param bool $excludeBirthday |
||
| 207 | * @return int |
||
| 208 | */ |
||
| 209 | View Code Duplication | public function getCalendarsForUserCount($principalUri, $excludeBirthday = true) { |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Returns a list of calendars for a principal. |
||
| 225 | * |
||
| 226 | * Every project is an array with the following keys: |
||
| 227 | * * id, a unique id that will be used by other functions to modify the |
||
| 228 | * calendar. This can be the same as the uri or a database key. |
||
| 229 | * * uri, which the basename of the uri with which the calendar is |
||
| 230 | * accessed. |
||
| 231 | * * principaluri. The owner of the calendar. Almost always the same as |
||
| 232 | * principalUri passed to this method. |
||
| 233 | * |
||
| 234 | * Furthermore it can contain webdav properties in clark notation. A very |
||
| 235 | * common one is '{DAV:}displayname'. |
||
| 236 | * |
||
| 237 | * Many clients also require: |
||
| 238 | * {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
||
| 239 | * For this property, you can just return an instance of |
||
| 240 | * Sabre\CalDAV\Property\SupportedCalendarComponentSet. |
||
| 241 | * |
||
| 242 | * If you return {http://sabredav.org/ns}read-only and set the value to 1, |
||
| 243 | * ACL will automatically be put in read-only mode. |
||
| 244 | * |
||
| 245 | * @param string $principalUri |
||
| 246 | * @return array |
||
| 247 | */ |
||
| 248 | function getCalendarsForUser($principalUri) { |
||
|
|
|||
| 249 | $principalUriOriginal = $principalUri; |
||
| 250 | $principalUri = $this->convertPrincipal($principalUri, true); |
||
| 251 | $fields = array_values($this->propertyMap); |
||
| 252 | $fields[] = 'id'; |
||
| 253 | $fields[] = 'uri'; |
||
| 254 | $fields[] = 'synctoken'; |
||
| 255 | $fields[] = 'components'; |
||
| 256 | $fields[] = 'principaluri'; |
||
| 257 | $fields[] = 'transparent'; |
||
| 258 | |||
| 259 | // Making fields a comma-delimited list |
||
| 260 | $query = $this->db->getQueryBuilder(); |
||
| 261 | $query->select($fields)->from('calendars') |
||
| 262 | ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
||
| 263 | ->orderBy('calendarorder', 'ASC'); |
||
| 264 | $stmt = $query->execute(); |
||
| 265 | |||
| 266 | $calendars = []; |
||
| 267 | while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
||
| 268 | |||
| 269 | $components = []; |
||
| 270 | if ($row['components']) { |
||
| 271 | $components = explode(',',$row['components']); |
||
| 272 | } |
||
| 273 | |||
| 274 | $calendar = [ |
||
| 275 | 'id' => $row['id'], |
||
| 276 | 'uri' => $row['uri'], |
||
| 277 | 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
||
| 278 | '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
||
| 279 | '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
||
| 280 | '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
||
| 281 | '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
||
| 282 | '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
||
| 283 | ]; |
||
| 284 | |||
| 285 | foreach($this->propertyMap as $xmlName=>$dbName) { |
||
| 286 | $calendar[$xmlName] = $row[$dbName]; |
||
| 287 | } |
||
| 288 | |||
| 289 | $this->addOwnerPrincipal($calendar); |
||
| 290 | |||
| 291 | if (!isset($calendars[$calendar['id']])) { |
||
| 292 | $calendars[$calendar['id']] = $calendar; |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | $stmt->closeCursor(); |
||
| 297 | |||
| 298 | // query for shared calendars |
||
| 299 | $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
||
| 300 | $principals = array_map(function($principal) { |
||
| 301 | return urldecode($principal); |
||
| 302 | }, $principals); |
||
| 303 | $principals[]= $principalUri; |
||
| 304 | |||
| 305 | $fields = array_values($this->propertyMap); |
||
| 306 | $fields[] = 'a.id'; |
||
| 307 | $fields[] = 'a.uri'; |
||
| 308 | $fields[] = 'a.synctoken'; |
||
| 309 | $fields[] = 'a.components'; |
||
| 310 | $fields[] = 'a.principaluri'; |
||
| 311 | $fields[] = 'a.transparent'; |
||
| 312 | $fields[] = 's.access'; |
||
| 313 | $query = $this->db->getQueryBuilder(); |
||
| 314 | $result = $query->select($fields) |
||
| 315 | ->from('dav_shares', 's') |
||
| 316 | ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
||
| 317 | ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
||
| 318 | ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
||
| 319 | ->setParameter('type', 'calendar') |
||
| 320 | ->setParameter('principaluri', $principals, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY) |
||
| 321 | ->execute(); |
||
| 322 | |||
| 323 | $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
||
| 324 | while($row = $result->fetch()) { |
||
| 325 | if ($row['principaluri'] === $principalUri) { |
||
| 326 | continue; |
||
| 327 | } |
||
| 328 | |||
| 329 | $readOnly = (int) $row['access'] === Backend::ACCESS_READ; |
||
| 330 | View Code Duplication | if (isset($calendars[$row['id']])) { |
|
| 331 | if ($readOnly) { |
||
| 332 | // New share can not have more permissions then the old one. |
||
| 333 | continue; |
||
| 334 | } |
||
| 335 | if (isset($calendars[$row['id']][$readOnlyPropertyName]) && |
||
| 336 | $calendars[$row['id']][$readOnlyPropertyName] === 0) { |
||
| 337 | // Old share is already read-write, no more permissions can be gained |
||
| 338 | continue; |
||
| 339 | } |
||
| 340 | } |
||
| 341 | |||
| 342 | list(, $name) = Uri\split($row['principaluri']); |
||
| 343 | $uri = $row['uri'] . '_shared_by_' . $name; |
||
| 344 | $row['displayname'] = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')'; |
||
| 345 | $components = []; |
||
| 346 | if ($row['components']) { |
||
| 347 | $components = explode(',',$row['components']); |
||
| 348 | } |
||
| 349 | $calendar = [ |
||
| 350 | 'id' => $row['id'], |
||
| 351 | 'uri' => $uri, |
||
| 352 | 'principaluri' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
||
| 353 | '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
||
| 354 | '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
||
| 355 | '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
||
| 356 | '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp('transparent'), |
||
| 357 | '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
||
| 358 | $readOnlyPropertyName => $readOnly, |
||
| 359 | ]; |
||
| 360 | |||
| 361 | foreach($this->propertyMap as $xmlName=>$dbName) { |
||
| 362 | $calendar[$xmlName] = $row[$dbName]; |
||
| 363 | } |
||
| 364 | |||
| 365 | $this->addOwnerPrincipal($calendar); |
||
| 366 | |||
| 367 | $calendars[$calendar['id']] = $calendar; |
||
| 368 | } |
||
| 369 | $result->closeCursor(); |
||
| 370 | |||
| 371 | return array_values($calendars); |
||
| 372 | } |
||
| 373 | |||
| 374 | public function getUsersOwnCalendars($principalUri) { |
||
| 417 | |||
| 418 | |||
| 419 | View Code Duplication | private function getUserDisplayName($uid) { |
|
| 432 | |||
| 433 | /** |
||
| 434 | * @return array |
||
| 435 | */ |
||
| 436 | public function getPublicCalendars() { |
||
| 489 | |||
| 490 | /** |
||
| 491 | * @param string $uri |
||
| 492 | * @return array |
||
| 493 | * @throws NotFound |
||
| 494 | */ |
||
| 495 | public function getPublicCalendar($uri) { |
||
| 550 | |||
| 551 | /** |
||
| 552 | * @param string $principal |
||
| 553 | * @param string $uri |
||
| 554 | * @return array|null |
||
| 555 | */ |
||
| 556 | public function getCalendarByUri($principal, $uri) { |
||
| 602 | |||
| 603 | public function getCalendarById($calendarId) { |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Creates a new calendar for a principal. |
||
| 651 | * |
||
| 652 | * If the creation was a success, an id must be returned that can be used to reference |
||
| 653 | * this calendar in other methods, such as updateCalendar. |
||
| 654 | * |
||
| 655 | * @param string $principalUri |
||
| 656 | * @param string $calendarUri |
||
| 657 | * @param array $properties |
||
| 658 | * @return int |
||
| 659 | * @suppress SqlInjectionChecker |
||
| 660 | */ |
||
| 661 | function createCalendar($principalUri, $calendarUri, array $properties) { |
||
| 707 | |||
| 708 | /** |
||
| 709 | * Updates properties for a calendar. |
||
| 710 | * |
||
| 711 | * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
||
| 712 | * To do the actual updates, you must tell this object which properties |
||
| 713 | * you're going to process with the handle() method. |
||
| 714 | * |
||
| 715 | * Calling the handle method is like telling the PropPatch object "I |
||
| 716 | * promise I can handle updating this property". |
||
| 717 | * |
||
| 718 | * Read the PropPatch documentation for more info and examples. |
||
| 719 | * |
||
| 720 | * @param mixed $calendarId |
||
| 721 | * @param PropPatch $propPatch |
||
| 722 | * @return void |
||
| 723 | */ |
||
| 724 | function updateCalendar($calendarId, PropPatch $propPatch) { |
||
| 769 | |||
| 770 | /** |
||
| 771 | * Delete a calendar and all it's objects |
||
| 772 | * |
||
| 773 | * @param mixed $calendarId |
||
| 774 | * @return void |
||
| 775 | */ |
||
| 776 | function deleteCalendar($calendarId) { |
||
| 801 | |||
| 802 | /** |
||
| 803 | * Delete all of an user's shares |
||
| 804 | * |
||
| 805 | * @param string $principaluri |
||
| 806 | * @return void |
||
| 807 | */ |
||
| 808 | function deleteAllSharesByUser($principaluri) { |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Returns all calendar objects within a calendar. |
||
| 814 | * |
||
| 815 | * Every item contains an array with the following keys: |
||
| 816 | * * calendardata - The iCalendar-compatible calendar data |
||
| 817 | * * uri - a unique key which will be used to construct the uri. This can |
||
| 818 | * be any arbitrary string, but making sure it ends with '.ics' is a |
||
| 819 | * good idea. This is only the basename, or filename, not the full |
||
| 820 | * path. |
||
| 821 | * * lastmodified - a timestamp of the last modification time |
||
| 822 | * * etag - An arbitrary string, surrounded by double-quotes. (e.g.: |
||
| 823 | * '"abcdef"') |
||
| 824 | * * size - The size of the calendar objects, in bytes. |
||
| 825 | * * component - optional, a string containing the type of object, such |
||
| 826 | * as 'vevent' or 'vtodo'. If specified, this will be used to populate |
||
| 827 | * the Content-Type header. |
||
| 828 | * |
||
| 829 | * Note that the etag is optional, but it's highly encouraged to return for |
||
| 830 | * speed reasons. |
||
| 831 | * |
||
| 832 | * The calendardata is also optional. If it's not returned |
||
| 833 | * 'getCalendarObject' will be called later, which *is* expected to return |
||
| 834 | * calendardata. |
||
| 835 | * |
||
| 836 | * If neither etag or size are specified, the calendardata will be |
||
| 837 | * used/fetched to determine these numbers. If both are specified the |
||
| 838 | * amount of times this is needed is reduced by a great degree. |
||
| 839 | * |
||
| 840 | * @param mixed $calendarId |
||
| 841 | * @return array |
||
| 842 | */ |
||
| 843 | function getCalendarObjects($calendarId) { |
||
| 866 | |||
| 867 | /** |
||
| 868 | * Returns information from a single calendar object, based on it's object |
||
| 869 | * uri. |
||
| 870 | * |
||
| 871 | * The object uri is only the basename, or filename and not a full path. |
||
| 872 | * |
||
| 873 | * The returned array must have the same keys as getCalendarObjects. The |
||
| 874 | * 'calendardata' object is required here though, while it's not required |
||
| 875 | * for getCalendarObjects. |
||
| 876 | * |
||
| 877 | * This method must return null if the object did not exist. |
||
| 878 | * |
||
| 879 | * @param mixed $calendarId |
||
| 880 | * @param string $objectUri |
||
| 881 | * @return array|null |
||
| 882 | */ |
||
| 883 | function getCalendarObject($calendarId, $objectUri) { |
||
| 907 | |||
| 908 | /** |
||
| 909 | * Returns a list of calendar objects. |
||
| 910 | * |
||
| 911 | * This method should work identical to getCalendarObject, but instead |
||
| 912 | * return all the calendar objects in the list as an array. |
||
| 913 | * |
||
| 914 | * If the backend supports this, it may allow for some speed-ups. |
||
| 915 | * |
||
| 916 | * @param mixed $calendarId |
||
| 917 | * @param string[] $uris |
||
| 918 | * @return array |
||
| 919 | */ |
||
| 920 | function getMultipleCalendarObjects($calendarId, array $uris) { |
||
| 955 | |||
| 956 | /** |
||
| 957 | * Creates a new calendar object. |
||
| 958 | * |
||
| 959 | * The object uri is only the basename, or filename and not a full path. |
||
| 960 | * |
||
| 961 | * It is possible return an etag from this function, which will be used in |
||
| 962 | * the response to this PUT request. Note that the ETag must be surrounded |
||
| 963 | * by double-quotes. |
||
| 964 | * |
||
| 965 | * However, you should only really return this ETag if you don't mangle the |
||
| 966 | * calendar-data. If the result of a subsequent GET to this object is not |
||
| 967 | * the exact same as this request body, you should omit the ETag. |
||
| 968 | * |
||
| 969 | * @param mixed $calendarId |
||
| 970 | * @param string $objectUri |
||
| 971 | * @param string $calendarData |
||
| 972 | * @return string |
||
| 973 | */ |
||
| 974 | function createCalendarObject($calendarId, $objectUri, $calendarData) { |
||
| 1023 | |||
| 1024 | /** |
||
| 1025 | * Updates an existing calendarobject, based on it's uri. |
||
| 1026 | * |
||
| 1027 | * The object uri is only the basename, or filename and not a full path. |
||
| 1028 | * |
||
| 1029 | * It is possible return an etag from this function, which will be used in |
||
| 1030 | * the response to this PUT request. Note that the ETag must be surrounded |
||
| 1031 | * by double-quotes. |
||
| 1032 | * |
||
| 1033 | * However, you should only really return this ETag if you don't mangle the |
||
| 1034 | * calendar-data. If the result of a subsequent GET to this object is not |
||
| 1035 | * the exact same as this request body, you should omit the ETag. |
||
| 1036 | * |
||
| 1037 | * @param mixed $calendarId |
||
| 1038 | * @param string $objectUri |
||
| 1039 | * @param string $calendarData |
||
| 1040 | * @return string |
||
| 1041 | */ |
||
| 1042 | function updateCalendarObject($calendarId, $objectUri, $calendarData) { |
||
| 1078 | |||
| 1079 | /** |
||
| 1080 | * @param int $calendarObjectId |
||
| 1081 | * @param int $classification |
||
| 1082 | */ |
||
| 1083 | public function setClassification($calendarObjectId, $classification) { |
||
| 1095 | |||
| 1096 | /** |
||
| 1097 | * Deletes an existing calendar object. |
||
| 1098 | * |
||
| 1099 | * The object uri is only the basename, or filename and not a full path. |
||
| 1100 | * |
||
| 1101 | * @param mixed $calendarId |
||
| 1102 | * @param string $objectUri |
||
| 1103 | * @return void |
||
| 1104 | */ |
||
| 1105 | function deleteCalendarObject($calendarId, $objectUri) { |
||
| 1126 | |||
| 1127 | /** |
||
| 1128 | * Performs a calendar-query on the contents of this calendar. |
||
| 1129 | * |
||
| 1130 | * The calendar-query is defined in RFC4791 : CalDAV. Using the |
||
| 1131 | * calendar-query it is possible for a client to request a specific set of |
||
| 1132 | * object, based on contents of iCalendar properties, date-ranges and |
||
| 1133 | * iCalendar component types (VTODO, VEVENT). |
||
| 1134 | * |
||
| 1135 | * This method should just return a list of (relative) urls that match this |
||
| 1136 | * query. |
||
| 1137 | * |
||
| 1138 | * The list of filters are specified as an array. The exact array is |
||
| 1139 | * documented by Sabre\CalDAV\CalendarQueryParser. |
||
| 1140 | * |
||
| 1141 | * Note that it is extremely likely that getCalendarObject for every path |
||
| 1142 | * returned from this method will be called almost immediately after. You |
||
| 1143 | * may want to anticipate this to speed up these requests. |
||
| 1144 | * |
||
| 1145 | * This method provides a default implementation, which parses *all* the |
||
| 1146 | * iCalendar objects in the specified calendar. |
||
| 1147 | * |
||
| 1148 | * This default may well be good enough for personal use, and calendars |
||
| 1149 | * that aren't very large. But if you anticipate high usage, big calendars |
||
| 1150 | * or high loads, you are strongly advised to optimize certain paths. |
||
| 1151 | * |
||
| 1152 | * The best way to do so is override this method and to optimize |
||
| 1153 | * specifically for 'common filters'. |
||
| 1154 | * |
||
| 1155 | * Requests that are extremely common are: |
||
| 1156 | * * requests for just VEVENTS |
||
| 1157 | * * requests for just VTODO |
||
| 1158 | * * requests with a time-range-filter on either VEVENT or VTODO. |
||
| 1159 | * |
||
| 1160 | * ..and combinations of these requests. It may not be worth it to try to |
||
| 1161 | * handle every possible situation and just rely on the (relatively |
||
| 1162 | * easy to use) CalendarQueryValidator to handle the rest. |
||
| 1163 | * |
||
| 1164 | * Note that especially time-range-filters may be difficult to parse. A |
||
| 1165 | * time-range filter specified on a VEVENT must for instance also handle |
||
| 1166 | * recurrence rules correctly. |
||
| 1167 | * A good example of how to interprete all these filters can also simply |
||
| 1168 | * be found in Sabre\CalDAV\CalendarQueryFilter. This class is as correct |
||
| 1169 | * as possible, so it gives you a good idea on what type of stuff you need |
||
| 1170 | * to think of. |
||
| 1171 | * |
||
| 1172 | * @param mixed $calendarId |
||
| 1173 | * @param array $filters |
||
| 1174 | * @return array |
||
| 1175 | */ |
||
| 1176 | function calendarQuery($calendarId, array $filters) { |
||
| 1258 | |||
| 1259 | /** |
||
| 1260 | * custom Nextcloud search extension for CalDAV |
||
| 1261 | * |
||
| 1262 | * @param string $principalUri |
||
| 1263 | * @param array $filters |
||
| 1264 | * @param integer|null $limit |
||
| 1265 | * @param integer|null $offset |
||
| 1266 | * @return array |
||
| 1267 | */ |
||
| 1268 | public function calendarSearch($principalUri, array $filters, $limit=null, $offset=null) { |
||
| 1377 | |||
| 1378 | /** |
||
| 1379 | * used for Nextcloud's calendar API |
||
| 1380 | * |
||
| 1381 | * @param array $calendarInfo |
||
| 1382 | * @param string $pattern |
||
| 1383 | * @param array $searchProperties |
||
| 1384 | * @param array $options |
||
| 1385 | * @param integer|null $limit |
||
| 1386 | * @param integer|null $offset |
||
| 1387 | * |
||
| 1388 | * @return array |
||
| 1389 | */ |
||
| 1390 | public function search(array $calendarInfo, $pattern, array $searchProperties, |
||
| 1483 | |||
| 1484 | /** |
||
| 1485 | * @param Component $comp |
||
| 1486 | * @return array |
||
| 1487 | */ |
||
| 1488 | private function transformSearchData(Component $comp) { |
||
| 1526 | |||
| 1527 | /** |
||
| 1528 | * @param Property $prop |
||
| 1529 | * @return array |
||
| 1530 | */ |
||
| 1531 | private function transformSearchProperty(Property $prop) { |
||
| 1544 | |||
| 1545 | /** |
||
| 1546 | * Searches through all of a users calendars and calendar objects to find |
||
| 1547 | * an object with a specific UID. |
||
| 1548 | * |
||
| 1549 | * This method should return the path to this object, relative to the |
||
| 1550 | * calendar home, so this path usually only contains two parts: |
||
| 1551 | * |
||
| 1552 | * calendarpath/objectpath.ics |
||
| 1553 | * |
||
| 1554 | * If the uid is not found, return null. |
||
| 1555 | * |
||
| 1556 | * This method should only consider * objects that the principal owns, so |
||
| 1557 | * any calendars owned by other principals that also appear in this |
||
| 1558 | * collection should be ignored. |
||
| 1559 | * |
||
| 1560 | * @param string $principalUri |
||
| 1561 | * @param string $uid |
||
| 1562 | * @return string|null |
||
| 1563 | */ |
||
| 1564 | function getCalendarObjectByUID($principalUri, $uid) { |
||
| 1581 | |||
| 1582 | /** |
||
| 1583 | * The getChanges method returns all the changes that have happened, since |
||
| 1584 | * the specified syncToken in the specified calendar. |
||
| 1585 | * |
||
| 1586 | * This function should return an array, such as the following: |
||
| 1587 | * |
||
| 1588 | * [ |
||
| 1589 | * 'syncToken' => 'The current synctoken', |
||
| 1590 | * 'added' => [ |
||
| 1591 | * 'new.txt', |
||
| 1592 | * ], |
||
| 1593 | * 'modified' => [ |
||
| 1594 | * 'modified.txt', |
||
| 1595 | * ], |
||
| 1596 | * 'deleted' => [ |
||
| 1597 | * 'foo.php.bak', |
||
| 1598 | * 'old.txt' |
||
| 1599 | * ] |
||
| 1600 | * ); |
||
| 1601 | * |
||
| 1602 | * The returned syncToken property should reflect the *current* syncToken |
||
| 1603 | * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
||
| 1604 | * property This is * needed here too, to ensure the operation is atomic. |
||
| 1605 | * |
||
| 1606 | * If the $syncToken argument is specified as null, this is an initial |
||
| 1607 | * sync, and all members should be reported. |
||
| 1608 | * |
||
| 1609 | * The modified property is an array of nodenames that have changed since |
||
| 1610 | * the last token. |
||
| 1611 | * |
||
| 1612 | * The deleted property is an array with nodenames, that have been deleted |
||
| 1613 | * from collection. |
||
| 1614 | * |
||
| 1615 | * The $syncLevel argument is basically the 'depth' of the report. If it's |
||
| 1616 | * 1, you only have to report changes that happened only directly in |
||
| 1617 | * immediate descendants. If it's 2, it should also include changes from |
||
| 1618 | * the nodes below the child collections. (grandchildren) |
||
| 1619 | * |
||
| 1620 | * The $limit argument allows a client to specify how many results should |
||
| 1621 | * be returned at most. If the limit is not specified, it should be treated |
||
| 1622 | * as infinite. |
||
| 1623 | * |
||
| 1624 | * If the limit (infinite or not) is higher than you're willing to return, |
||
| 1625 | * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
||
| 1626 | * |
||
| 1627 | * If the syncToken is expired (due to data cleanup) or unknown, you must |
||
| 1628 | * return null. |
||
| 1629 | * |
||
| 1630 | * The limit is 'suggestive'. You are free to ignore it. |
||
| 1631 | * |
||
| 1632 | * @param string $calendarId |
||
| 1633 | * @param string $syncToken |
||
| 1634 | * @param int $syncLevel |
||
| 1635 | * @param int $limit |
||
| 1636 | * @return array |
||
| 1637 | */ |
||
| 1638 | View Code Duplication | function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null) { |
|
| 1702 | |||
| 1703 | /** |
||
| 1704 | * Returns a list of subscriptions for a principal. |
||
| 1705 | * |
||
| 1706 | * Every subscription is an array with the following keys: |
||
| 1707 | * * id, a unique id that will be used by other functions to modify the |
||
| 1708 | * subscription. This can be the same as the uri or a database key. |
||
| 1709 | * * uri. This is just the 'base uri' or 'filename' of the subscription. |
||
| 1710 | * * principaluri. The owner of the subscription. Almost always the same as |
||
| 1711 | * principalUri passed to this method. |
||
| 1712 | * |
||
| 1713 | * Furthermore, all the subscription info must be returned too: |
||
| 1714 | * |
||
| 1715 | * 1. {DAV:}displayname |
||
| 1716 | * 2. {http://apple.com/ns/ical/}refreshrate |
||
| 1717 | * 3. {http://calendarserver.org/ns/}subscribed-strip-todos (omit if todos |
||
| 1718 | * should not be stripped). |
||
| 1719 | * 4. {http://calendarserver.org/ns/}subscribed-strip-alarms (omit if alarms |
||
| 1720 | * should not be stripped). |
||
| 1721 | * 5. {http://calendarserver.org/ns/}subscribed-strip-attachments (omit if |
||
| 1722 | * attachments should not be stripped). |
||
| 1723 | * 6. {http://calendarserver.org/ns/}source (Must be a |
||
| 1724 | * Sabre\DAV\Property\Href). |
||
| 1725 | * 7. {http://apple.com/ns/ical/}calendar-color |
||
| 1726 | * 8. {http://apple.com/ns/ical/}calendar-order |
||
| 1727 | * 9. {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
||
| 1728 | * (should just be an instance of |
||
| 1729 | * Sabre\CalDAV\Property\SupportedCalendarComponentSet, with a bunch of |
||
| 1730 | * default components). |
||
| 1731 | * |
||
| 1732 | * @param string $principalUri |
||
| 1733 | * @return array |
||
| 1734 | */ |
||
| 1735 | function getSubscriptionsForUser($principalUri) { |
||
| 1775 | |||
| 1776 | /** |
||
| 1777 | * Creates a new subscription for a principal. |
||
| 1778 | * |
||
| 1779 | * If the creation was a success, an id must be returned that can be used to reference |
||
| 1780 | * this subscription in other methods, such as updateSubscription. |
||
| 1781 | * |
||
| 1782 | * @param string $principalUri |
||
| 1783 | * @param string $uri |
||
| 1784 | * @param array $properties |
||
| 1785 | * @return mixed |
||
| 1786 | */ |
||
| 1787 | function createSubscription($principalUri, $uri, array $properties) { |
||
| 1825 | |||
| 1826 | /** |
||
| 1827 | * Updates a subscription |
||
| 1828 | * |
||
| 1829 | * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
||
| 1830 | * To do the actual updates, you must tell this object which properties |
||
| 1831 | * you're going to process with the handle() method. |
||
| 1832 | * |
||
| 1833 | * Calling the handle method is like telling the PropPatch object "I |
||
| 1834 | * promise I can handle updating this property". |
||
| 1835 | * |
||
| 1836 | * Read the PropPatch documentation for more info and examples. |
||
| 1837 | * |
||
| 1838 | * @param mixed $subscriptionId |
||
| 1839 | * @param PropPatch $propPatch |
||
| 1840 | * @return void |
||
| 1841 | */ |
||
| 1842 | function updateSubscription($subscriptionId, PropPatch $propPatch) { |
||
| 1875 | |||
| 1876 | /** |
||
| 1877 | * Deletes a subscription. |
||
| 1878 | * |
||
| 1879 | * @param mixed $subscriptionId |
||
| 1880 | * @return void |
||
| 1881 | */ |
||
| 1882 | function deleteSubscription($subscriptionId) { |
||
| 1888 | |||
| 1889 | /** |
||
| 1890 | * Returns a single scheduling object for the inbox collection. |
||
| 1891 | * |
||
| 1892 | * The returned array should contain the following elements: |
||
| 1893 | * * uri - A unique basename for the object. This will be used to |
||
| 1894 | * construct a full uri. |
||
| 1895 | * * calendardata - The iCalendar object |
||
| 1896 | * * lastmodified - The last modification date. Can be an int for a unix |
||
| 1897 | * timestamp, or a PHP DateTime object. |
||
| 1898 | * * etag - A unique token that must change if the object changed. |
||
| 1899 | * * size - The size of the object, in bytes. |
||
| 1900 | * |
||
| 1901 | * @param string $principalUri |
||
| 1902 | * @param string $objectUri |
||
| 1903 | * @return array |
||
| 1904 | */ |
||
| 1905 | function getSchedulingObject($principalUri, $objectUri) { |
||
| 1927 | |||
| 1928 | /** |
||
| 1929 | * Returns all scheduling objects for the inbox collection. |
||
| 1930 | * |
||
| 1931 | * These objects should be returned as an array. Every item in the array |
||
| 1932 | * should follow the same structure as returned from getSchedulingObject. |
||
| 1933 | * |
||
| 1934 | * The main difference is that 'calendardata' is optional. |
||
| 1935 | * |
||
| 1936 | * @param string $principalUri |
||
| 1937 | * @return array |
||
| 1938 | */ |
||
| 1939 | function getSchedulingObjects($principalUri) { |
||
| 1959 | |||
| 1960 | /** |
||
| 1961 | * Deletes a scheduling object from the inbox collection. |
||
| 1962 | * |
||
| 1963 | * @param string $principalUri |
||
| 1964 | * @param string $objectUri |
||
| 1965 | * @return void |
||
| 1966 | */ |
||
| 1967 | function deleteSchedulingObject($principalUri, $objectUri) { |
||
| 1974 | |||
| 1975 | /** |
||
| 1976 | * Creates a new scheduling object. This should land in a users' inbox. |
||
| 1977 | * |
||
| 1978 | * @param string $principalUri |
||
| 1979 | * @param string $objectUri |
||
| 1980 | * @param string $objectData |
||
| 1981 | * @return void |
||
| 1982 | */ |
||
| 1983 | function createSchedulingObject($principalUri, $objectUri, $objectData) { |
||
| 1996 | |||
| 1997 | /** |
||
| 1998 | * Adds a change record to the calendarchanges table. |
||
| 1999 | * |
||
| 2000 | * @param mixed $calendarId |
||
| 2001 | * @param string $objectUri |
||
| 2002 | * @param int $operation 1 = add, 2 = modify, 3 = delete. |
||
| 2003 | * @return void |
||
| 2004 | */ |
||
| 2005 | View Code Duplication | protected function addChange($calendarId, $objectUri, $operation) { |
|
| 2020 | |||
| 2021 | /** |
||
| 2022 | * Parses some information from calendar objects, used for optimized |
||
| 2023 | * calendar-queries. |
||
| 2024 | * |
||
| 2025 | * Returns an array with the following keys: |
||
| 2026 | * * etag - An md5 checksum of the object without the quotes. |
||
| 2027 | * * size - Size of the object in bytes |
||
| 2028 | * * componentType - VEVENT, VTODO or VJOURNAL |
||
| 2029 | * * firstOccurence |
||
| 2030 | * * lastOccurence |
||
| 2031 | * * uid - value of the UID property |
||
| 2032 | * |
||
| 2033 | * @param string $calendarData |
||
| 2034 | * @return array |
||
| 2035 | */ |
||
| 2036 | public function getDenormalizedData($calendarData) { |
||
| 2112 | |||
| 2113 | private function readBlob($cardData) { |
||
| 2120 | |||
| 2121 | /** |
||
| 2122 | * @param IShareable $shareable |
||
| 2123 | * @param array $add |
||
| 2124 | * @param array $remove |
||
| 2125 | */ |
||
| 2126 | public function updateShares($shareable, $add, $remove) { |
||
| 2139 | |||
| 2140 | /** |
||
| 2141 | * @param int $resourceId |
||
| 2142 | * @return array |
||
| 2143 | */ |
||
| 2144 | public function getShares($resourceId) { |
||
| 2147 | |||
| 2148 | /** |
||
| 2149 | * @param boolean $value |
||
| 2150 | * @param \OCA\DAV\CalDAV\Calendar $calendar |
||
| 2151 | * @return string|null |
||
| 2152 | */ |
||
| 2153 | public function setPublishStatus($value, $calendar) { |
||
| 2184 | |||
| 2185 | /** |
||
| 2186 | * @param \OCA\DAV\CalDAV\Calendar $calendar |
||
| 2187 | * @return mixed |
||
| 2188 | */ |
||
| 2189 | View Code Duplication | public function getPublishStatus($calendar) { |
|
| 2201 | |||
| 2202 | /** |
||
| 2203 | * @param int $resourceId |
||
| 2204 | * @param array $acl |
||
| 2205 | * @return array |
||
| 2206 | */ |
||
| 2207 | public function applyShareAcl($resourceId, $acl) { |
||
| 2210 | |||
| 2211 | |||
| 2212 | |||
| 2213 | /** |
||
| 2214 | * update properties table |
||
| 2215 | * |
||
| 2216 | * @param int $calendarId |
||
| 2217 | * @param string $objectUri |
||
| 2218 | * @param string $calendarData |
||
| 2219 | */ |
||
| 2220 | public function updateProperties($calendarId, $objectUri, $calendarData) { |
||
| 2286 | |||
| 2287 | /** |
||
| 2288 | * deletes all birthday calendars |
||
| 2289 | */ |
||
| 2290 | public function deleteAllBirthdayCalendars() { |
||
| 2302 | |||
| 2303 | /** |
||
| 2304 | * read VCalendar data into a VCalendar object |
||
| 2305 | * |
||
| 2306 | * @param string $objectData |
||
| 2307 | * @return VCalendar |
||
| 2308 | */ |
||
| 2309 | protected function readCalendarData($objectData) { |
||
| 2312 | |||
| 2313 | /** |
||
| 2314 | * delete all properties from a given calendar object |
||
| 2315 | * |
||
| 2316 | * @param int $calendarId |
||
| 2317 | * @param int $objectId |
||
| 2318 | */ |
||
| 2319 | View Code Duplication | protected function purgeProperties($calendarId, $objectId) { |
|
| 2326 | |||
| 2327 | /** |
||
| 2328 | * get ID from a given calendar object |
||
| 2329 | * |
||
| 2330 | * @param int $calendarId |
||
| 2331 | * @param string $uri |
||
| 2332 | * @return int |
||
| 2333 | */ |
||
| 2334 | View Code Duplication | protected function getCalendarObjectId($calendarId, $uri) { |
|
| 2350 | |||
| 2351 | View Code Duplication | private function convertPrincipal($principalUri, $toV2) { |
|
| 2361 | |||
| 2362 | View Code Duplication | private function addOwnerPrincipal(&$calendarInfo) { |
|
| 2376 | } |
||
| 2377 |
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.