SetVcardDatabaseUID.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 John Molakvoæ <skjnldsv@protonmail.com>
  4. *
  5. * @author John Molakvoæ <skjnldsv@protonmail.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Repair\NC15;
  24. use OCP\IConfig;
  25. use OCP\IDBConnection;
  26. use OCP\ILogger;
  27. use OCP\Migration\IOutput;
  28. use OCP\Migration\IRepairStep;
  29. use Sabre\VObject\Reader;
  30. use Sabre\VObject\ParseException;
  31. class SetVcardDatabaseUID implements IRepairStep {
  32. const MAX_ROWS = 1000;
  33. /** @var IDBConnection */
  34. private $connection;
  35. /** @var IConfig */
  36. private $config;
  37. /** @var ILogger */
  38. private $logger;
  39. private $updateQuery;
  40. public function __construct(IDBConnection $connection, IConfig $config, ILogger $logger) {
  41. $this->connection = $connection;
  42. $this->config = $config;
  43. $this->logger = $logger;
  44. }
  45. public function getName() {
  46. return 'Extract the vcard uid and store it in the db';
  47. }
  48. /**
  49. * @return \Generator
  50. * @suppress SqlInjectionChecker
  51. */
  52. private function getInvalidEntries() {
  53. $builder = $this->connection->getQueryBuilder();
  54. $builder->select('id', 'carddata')
  55. ->from('cards')
  56. ->where($builder->expr()->isNull('uid'))
  57. ->setMaxResults(self::MAX_ROWS);
  58. do {
  59. $result = $builder->execute();
  60. $rows = $result->fetchAll();
  61. foreach ($rows as $row) {
  62. yield $row;
  63. }
  64. $result->closeCursor();
  65. } while (count($rows) > 0);
  66. }
  67. /**
  68. * Extract UID from vcard
  69. *
  70. * @param string $cardData the vcard raw data
  71. * @param IOutput $output the output logger
  72. * @return string the uid or empty if none
  73. */
  74. private function getUID(string $cardData, IOutput $output): string {
  75. try {
  76. $vCard = Reader::read($cardData);
  77. if ($vCard->UID) {
  78. $uid = $vCard->UID->getValue();
  79. return $uid;
  80. }
  81. } catch (ParseException $e) {
  82. $output->warning('One vCard is broken. We logged the exception and will continue the repair.');
  83. $this->logger->logException($e);
  84. }
  85. return '';
  86. }
  87. /**
  88. * @param int $id
  89. * @param string $uid
  90. */
  91. private function update(int $id, string $uid) {
  92. if (!$this->updateQuery) {
  93. $builder = $this->connection->getQueryBuilder();
  94. $this->updateQuery = $builder->update('cards')
  95. ->set('uid', $builder->createParameter('uid'))
  96. ->where($builder->expr()->eq('id', $builder->createParameter('id')));
  97. }
  98. $this->updateQuery->setParameter('id', $id);
  99. $this->updateQuery->setParameter('uid', $uid);
  100. $this->updateQuery->execute();
  101. }
  102. private function repair(IOutput $output): int {
  103. $this->connection->beginTransaction();
  104. $entries = $this->getInvalidEntries();
  105. $count = 0;
  106. foreach ($entries as $entry) {
  107. $count++;
  108. $cardData = $entry['carddata'];
  109. if (is_resource($cardData)) {
  110. $cardData = stream_get_contents($cardData);
  111. }
  112. $uid = $this->getUID($cardData, $output);
  113. $this->update($entry['id'], $uid);
  114. }
  115. $this->connection->commit();
  116. return $count;
  117. }
  118. private function shouldRun() {
  119. $versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0.0');
  120. // was added to 15.0.0.2
  121. return version_compare($versionFromBeforeUpdate, '15.0.0.2', '<=');
  122. }
  123. public function run(IOutput $output) {
  124. if ($this->shouldRun()) {
  125. $count = $this->repair($output);
  126. $output->info('Fixed ' . $count . ' vcards');
  127. }
  128. }
  129. }