From 353f5bd46952d25abadc329092c353e64758fab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Molakvo=C3=A6=20=28skjnldsv=29?= Date: Tue, 30 Oct 2018 14:03:41 +0100 Subject: [PATCH] fixup! move migration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: John Molakvoæ (skjnldsv) --- apps/dav/lib/CardDAV/CardDavBackend.php | 15 ++++++++-- .../Repair/NC15/SetVcardDatabaseUID.php | 30 +++++++++++-------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/apps/dav/lib/CardDAV/CardDavBackend.php b/apps/dav/lib/CardDAV/CardDavBackend.php index a9d65f2f0f..8ef0e0baf5 100644 --- a/apps/dav/lib/CardDAV/CardDavBackend.php +++ b/apps/dav/lib/CardDAV/CardDavBackend.php @@ -1130,9 +1130,20 @@ class CardDavBackend implements BackendInterface, SyncSupport { } } + /** + * Extract UID from vcard + * + * @param string $cardData the vcard raw data + * @return string the uid or empty if none + * @throws BadRequest + */ private function getUID($cardData) { $vCard = Reader::read($cardData); - $uid = $vCard->UID->getValue(); - return $uid; + if ($vCard->UID) { + $uid = $vCard->UID->getValue(); + return $uid; + } + // should already be handled, but just in case + throw new BadRequest('vCards on CardDAV servers MUST have a UID property'); } } diff --git a/lib/private/Repair/NC15/SetVcardDatabaseUID.php b/lib/private/Repair/NC15/SetVcardDatabaseUID.php index 4d2805247b..1b188e1d96 100644 --- a/lib/private/Repair/NC15/SetVcardDatabaseUID.php +++ b/lib/private/Repair/NC15/SetVcardDatabaseUID.php @@ -27,6 +27,7 @@ use OCP\IConfig; use OCP\IDBConnection; use OCP\Migration\IOutput; use OCP\Migration\IRepairStep; +use Sabre\VObject\Reader; class SetVcardDatabaseUID implements IRepairStep { const MAX_ROWS = 1000; @@ -70,20 +71,27 @@ class SetVcardDatabaseUID implements IRepairStep { } while (count($rows) > 0); } - private function getUid($carddata) { - preg_match('/UID:(.*)$/m', $carddata, $matches); - if (count($matches) > 1) { - return $matches[1]; + /** + * Extract UID from vcard + * + * @param string $cardData the vcard raw data + * @return string the uid or empty if none + */ + private function getUID(string $cardData): string { + $vCard = Reader::read($cardData); + if ($vCard->UID) { + $uid = $vCard->UID->getValue(); + return $uid; } - return false; + return ''; } /** * @param int $id * @param string $uid */ - private function update($id, $uid) { + private function update(int $id, string $uid) { if (!$this->updateQuery) { $builder = $this->connection->getQueryBuilder(); @@ -98,16 +106,14 @@ class SetVcardDatabaseUID implements IRepairStep { $this->updateQuery->execute(); } - private function repair() { + private function repair(): int { $this->connection->beginTransaction(); $entries = $this->getInvalidEntries(); $count = 0; foreach ($entries as $entry) { $count++; - $uid = $this->getUid($entry['carddata']); - if ($uid !== false) { - $this->update($entry['id'], $uid); - } + $uid = $this->getUID($entry['carddata']); + $this->update($entry['id'], $uid); } $this->connection->commit(); @@ -118,7 +124,7 @@ class SetVcardDatabaseUID implements IRepairStep { $versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0.0'); // was added to 15.0.0.2 - return version_compare($versionFromBeforeUpdate, '15.0.0.2', '<='); + return version_compare($versionFromBeforeUpdate, '15.0.0.3', '<='); } public function run(IOutput $output) {