@@ -30,112 +30,112 @@ |
||
| 30 | 30 | use Sabre\VObject\Reader; |
| 31 | 31 | |
| 32 | 32 | class SetVcardDatabaseUID implements IRepairStep { |
| 33 | - const MAX_ROWS = 1000; |
|
| 34 | - |
|
| 35 | - /** @var IDBConnection */ |
|
| 36 | - private $connection; |
|
| 37 | - |
|
| 38 | - /** @var IConfig */ |
|
| 39 | - private $config; |
|
| 40 | - |
|
| 41 | - private $updateQuery; |
|
| 42 | - |
|
| 43 | - public function __construct(IDBConnection $connection, IConfig $config) { |
|
| 44 | - $this->connection = $connection; |
|
| 45 | - $this->config = $config; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - public function getName() { |
|
| 49 | - return 'Extract the vcard uid and store it in the db'; |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @return \Generator |
|
| 54 | - * @suppress SqlInjectionChecker |
|
| 55 | - */ |
|
| 56 | - private function getInvalidEntries() { |
|
| 57 | - $builder = $this->connection->getQueryBuilder(); |
|
| 58 | - |
|
| 59 | - $builder->select('id', 'carddata') |
|
| 60 | - ->from('cards') |
|
| 61 | - ->where($builder->expr()->isNull('uid')) |
|
| 62 | - ->setMaxResults(self::MAX_ROWS); |
|
| 63 | - |
|
| 64 | - do { |
|
| 65 | - $result = $builder->execute(); |
|
| 66 | - $rows = $result->fetchAll(); |
|
| 67 | - foreach ($rows as $row) { |
|
| 68 | - yield $row; |
|
| 69 | - } |
|
| 70 | - $result->closeCursor(); |
|
| 71 | - } while (count($rows) > 0); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Extract UID from vcard |
|
| 76 | - * |
|
| 77 | - * @param string $cardData the vcard raw data |
|
| 78 | - * @return string the uid or empty if none |
|
| 79 | - */ |
|
| 80 | - private function getUID(string $cardData): string { |
|
| 81 | - $vCard = Reader::read($cardData); |
|
| 82 | - if ($vCard->UID) { |
|
| 83 | - $uid = $vCard->UID->getValue(); |
|
| 84 | - return $uid; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - return ''; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @param int $id |
|
| 92 | - * @param string $uid |
|
| 93 | - */ |
|
| 94 | - private function update(int $id, string $uid) { |
|
| 95 | - if (!$this->updateQuery) { |
|
| 96 | - $builder = $this->connection->getQueryBuilder(); |
|
| 97 | - |
|
| 98 | - $this->updateQuery = $builder->update('cards') |
|
| 99 | - ->set('uid', $builder->createParameter('uid')) |
|
| 100 | - ->where($builder->expr()->eq('id', $builder->createParameter('id'))); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - $this->updateQuery->setParameter('id', $id); |
|
| 104 | - $this->updateQuery->setParameter('uid', $uid); |
|
| 105 | - |
|
| 106 | - $this->updateQuery->execute(); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - private function repair(): int { |
|
| 110 | - $this->connection->beginTransaction(); |
|
| 111 | - $entries = $this->getInvalidEntries(); |
|
| 112 | - $count = 0; |
|
| 113 | - foreach ($entries as $entry) { |
|
| 114 | - $count++; |
|
| 115 | - $cardData = $entry['carddata']; |
|
| 116 | - if (is_resource($cardData)) { |
|
| 117 | - $cardData = stream_get_contents($cardData); |
|
| 118 | - } |
|
| 119 | - $uid = $this->getUID($cardData); |
|
| 120 | - $this->update($entry['id'], $uid); |
|
| 121 | - } |
|
| 122 | - $this->connection->commit(); |
|
| 123 | - |
|
| 124 | - return $count; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - private function shouldRun() { |
|
| 128 | - $versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0.0'); |
|
| 129 | - |
|
| 130 | - // was added to 15.0.0.2 |
|
| 131 | - return version_compare($versionFromBeforeUpdate, '15.0.0.2', '<='); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - public function run(IOutput $output) { |
|
| 135 | - if ($this->shouldRun()) { |
|
| 136 | - $count = $this->repair(); |
|
| 137 | - |
|
| 138 | - $output->info('Fixed ' . $count . ' vcards'); |
|
| 139 | - } |
|
| 140 | - } |
|
| 33 | + const MAX_ROWS = 1000; |
|
| 34 | + |
|
| 35 | + /** @var IDBConnection */ |
|
| 36 | + private $connection; |
|
| 37 | + |
|
| 38 | + /** @var IConfig */ |
|
| 39 | + private $config; |
|
| 40 | + |
|
| 41 | + private $updateQuery; |
|
| 42 | + |
|
| 43 | + public function __construct(IDBConnection $connection, IConfig $config) { |
|
| 44 | + $this->connection = $connection; |
|
| 45 | + $this->config = $config; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + public function getName() { |
|
| 49 | + return 'Extract the vcard uid and store it in the db'; |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @return \Generator |
|
| 54 | + * @suppress SqlInjectionChecker |
|
| 55 | + */ |
|
| 56 | + private function getInvalidEntries() { |
|
| 57 | + $builder = $this->connection->getQueryBuilder(); |
|
| 58 | + |
|
| 59 | + $builder->select('id', 'carddata') |
|
| 60 | + ->from('cards') |
|
| 61 | + ->where($builder->expr()->isNull('uid')) |
|
| 62 | + ->setMaxResults(self::MAX_ROWS); |
|
| 63 | + |
|
| 64 | + do { |
|
| 65 | + $result = $builder->execute(); |
|
| 66 | + $rows = $result->fetchAll(); |
|
| 67 | + foreach ($rows as $row) { |
|
| 68 | + yield $row; |
|
| 69 | + } |
|
| 70 | + $result->closeCursor(); |
|
| 71 | + } while (count($rows) > 0); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Extract UID from vcard |
|
| 76 | + * |
|
| 77 | + * @param string $cardData the vcard raw data |
|
| 78 | + * @return string the uid or empty if none |
|
| 79 | + */ |
|
| 80 | + private function getUID(string $cardData): string { |
|
| 81 | + $vCard = Reader::read($cardData); |
|
| 82 | + if ($vCard->UID) { |
|
| 83 | + $uid = $vCard->UID->getValue(); |
|
| 84 | + return $uid; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + return ''; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @param int $id |
|
| 92 | + * @param string $uid |
|
| 93 | + */ |
|
| 94 | + private function update(int $id, string $uid) { |
|
| 95 | + if (!$this->updateQuery) { |
|
| 96 | + $builder = $this->connection->getQueryBuilder(); |
|
| 97 | + |
|
| 98 | + $this->updateQuery = $builder->update('cards') |
|
| 99 | + ->set('uid', $builder->createParameter('uid')) |
|
| 100 | + ->where($builder->expr()->eq('id', $builder->createParameter('id'))); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + $this->updateQuery->setParameter('id', $id); |
|
| 104 | + $this->updateQuery->setParameter('uid', $uid); |
|
| 105 | + |
|
| 106 | + $this->updateQuery->execute(); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + private function repair(): int { |
|
| 110 | + $this->connection->beginTransaction(); |
|
| 111 | + $entries = $this->getInvalidEntries(); |
|
| 112 | + $count = 0; |
|
| 113 | + foreach ($entries as $entry) { |
|
| 114 | + $count++; |
|
| 115 | + $cardData = $entry['carddata']; |
|
| 116 | + if (is_resource($cardData)) { |
|
| 117 | + $cardData = stream_get_contents($cardData); |
|
| 118 | + } |
|
| 119 | + $uid = $this->getUID($cardData); |
|
| 120 | + $this->update($entry['id'], $uid); |
|
| 121 | + } |
|
| 122 | + $this->connection->commit(); |
|
| 123 | + |
|
| 124 | + return $count; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + private function shouldRun() { |
|
| 128 | + $versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0.0'); |
|
| 129 | + |
|
| 130 | + // was added to 15.0.0.2 |
|
| 131 | + return version_compare($versionFromBeforeUpdate, '15.0.0.2', '<='); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + public function run(IOutput $output) { |
|
| 135 | + if ($this->shouldRun()) { |
|
| 136 | + $count = $this->repair(); |
|
| 137 | + |
|
| 138 | + $output->info('Fixed ' . $count . ' vcards'); |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | 141 | } |