@@ -34,118 +34,118 @@ |
||
34 | 34 | |
35 | 35 | class CalDAVRemoveEmptyValue implements IRepairStep { |
36 | 36 | |
37 | - /** @var IDBConnection */ |
|
38 | - private $db; |
|
39 | - |
|
40 | - /** @var CalDavBackend */ |
|
41 | - private $calDavBackend; |
|
42 | - |
|
43 | - /** @var ILogger */ |
|
44 | - private $logger; |
|
45 | - |
|
46 | - /** |
|
47 | - * @param IDBConnection $db |
|
48 | - * @param CalDavBackend $calDavBackend |
|
49 | - * @param ILogger $logger |
|
50 | - */ |
|
51 | - public function __construct(IDBConnection $db, CalDavBackend $calDavBackend, ILogger $logger) { |
|
52 | - $this->db = $db; |
|
53 | - $this->calDavBackend = $calDavBackend; |
|
54 | - $this->logger = $logger; |
|
55 | - } |
|
56 | - |
|
57 | - public function getName() { |
|
58 | - return 'Fix broken values of calendar objects'; |
|
59 | - } |
|
60 | - |
|
61 | - public function run(IOutput $output) { |
|
62 | - $pattern = ';VALUE=:'; |
|
63 | - $count = $warnings = 0; |
|
64 | - |
|
65 | - $objects = $this->getInvalidObjects($pattern); |
|
66 | - |
|
67 | - $output->startProgress(count($objects)); |
|
68 | - foreach ($objects as $row) { |
|
69 | - $calObject = $this->calDavBackend->getCalendarObject((int)$row['calendarid'], $row['uri']); |
|
70 | - $data = preg_replace('/' . $pattern . '/', ':', $calObject['calendardata']); |
|
71 | - |
|
72 | - if ($data !== $calObject['calendardata']) { |
|
73 | - $output->advance(); |
|
74 | - |
|
75 | - try { |
|
76 | - $this->calDavBackend->getDenormalizedData($data); |
|
77 | - } catch (InvalidDataException $e) { |
|
78 | - $this->logger->info('Calendar object for calendar {cal} with uri {uri} still invalid', [ |
|
79 | - 'app' => 'dav', |
|
80 | - 'cal' => (int)$row['calendarid'], |
|
81 | - 'uri' => $row['uri'], |
|
82 | - ]); |
|
83 | - $warnings++; |
|
84 | - continue; |
|
85 | - } |
|
86 | - |
|
87 | - $this->calDavBackend->updateCalendarObject((int)$row['calendarid'], $row['uri'], $data); |
|
88 | - $count++; |
|
89 | - } |
|
90 | - } |
|
91 | - $output->finishProgress(); |
|
92 | - |
|
93 | - if ($warnings > 0) { |
|
94 | - $output->warning(sprintf('%d events could not be updated, see log file for more information', $warnings)); |
|
95 | - } |
|
96 | - if ($count > 0) { |
|
97 | - $output->info(sprintf('Updated %d events', $count)); |
|
98 | - } |
|
99 | - } |
|
100 | - |
|
101 | - protected function getInvalidObjects($pattern) { |
|
102 | - if ($this->db->getDatabasePlatform() instanceof OraclePlatform) { |
|
103 | - $rows = []; |
|
104 | - $chunkSize = 500; |
|
105 | - $query = $this->db->getQueryBuilder(); |
|
106 | - $query->select($query->func()->count('*', 'num_entries')) |
|
107 | - ->from('calendarobjects'); |
|
108 | - $result = $query->execute(); |
|
109 | - $count = $result->fetchColumn(); |
|
110 | - $result->closeCursor(); |
|
111 | - |
|
112 | - $numChunks = ceil($count / $chunkSize); |
|
113 | - |
|
114 | - $query = $this->db->getQueryBuilder(); |
|
115 | - $query->select(['calendarid', 'uri', 'calendardata']) |
|
116 | - ->from('calendarobjects') |
|
117 | - ->setMaxResults($chunkSize); |
|
118 | - for ($chunk = 0; $chunk < $numChunks; $chunk++) { |
|
119 | - $query->setFirstResult($chunk * $chunkSize); |
|
120 | - $result = $query->execute(); |
|
121 | - |
|
122 | - while ($row = $result->fetch()) { |
|
123 | - if (mb_strpos($row['calendardata'], $pattern) !== false) { |
|
124 | - unset($row['calendardata']); |
|
125 | - $rows[] = $row; |
|
126 | - } |
|
127 | - } |
|
128 | - $result->closeCursor(); |
|
129 | - } |
|
130 | - return $rows; |
|
131 | - } |
|
132 | - |
|
133 | - $query = $this->db->getQueryBuilder(); |
|
134 | - $query->select(['calendarid', 'uri']) |
|
135 | - ->from('calendarobjects') |
|
136 | - ->where($query->expr()->like( |
|
137 | - 'calendardata', |
|
138 | - $query->createNamedParameter( |
|
139 | - '%' . $this->db->escapeLikeParameter($pattern) . '%', |
|
140 | - IQueryBuilder::PARAM_STR |
|
141 | - ), |
|
142 | - IQueryBuilder::PARAM_STR |
|
143 | - )); |
|
144 | - |
|
145 | - $result = $query->execute(); |
|
146 | - $rows = $result->fetchAll(); |
|
147 | - $result->closeCursor(); |
|
148 | - |
|
149 | - return $rows; |
|
150 | - } |
|
37 | + /** @var IDBConnection */ |
|
38 | + private $db; |
|
39 | + |
|
40 | + /** @var CalDavBackend */ |
|
41 | + private $calDavBackend; |
|
42 | + |
|
43 | + /** @var ILogger */ |
|
44 | + private $logger; |
|
45 | + |
|
46 | + /** |
|
47 | + * @param IDBConnection $db |
|
48 | + * @param CalDavBackend $calDavBackend |
|
49 | + * @param ILogger $logger |
|
50 | + */ |
|
51 | + public function __construct(IDBConnection $db, CalDavBackend $calDavBackend, ILogger $logger) { |
|
52 | + $this->db = $db; |
|
53 | + $this->calDavBackend = $calDavBackend; |
|
54 | + $this->logger = $logger; |
|
55 | + } |
|
56 | + |
|
57 | + public function getName() { |
|
58 | + return 'Fix broken values of calendar objects'; |
|
59 | + } |
|
60 | + |
|
61 | + public function run(IOutput $output) { |
|
62 | + $pattern = ';VALUE=:'; |
|
63 | + $count = $warnings = 0; |
|
64 | + |
|
65 | + $objects = $this->getInvalidObjects($pattern); |
|
66 | + |
|
67 | + $output->startProgress(count($objects)); |
|
68 | + foreach ($objects as $row) { |
|
69 | + $calObject = $this->calDavBackend->getCalendarObject((int)$row['calendarid'], $row['uri']); |
|
70 | + $data = preg_replace('/' . $pattern . '/', ':', $calObject['calendardata']); |
|
71 | + |
|
72 | + if ($data !== $calObject['calendardata']) { |
|
73 | + $output->advance(); |
|
74 | + |
|
75 | + try { |
|
76 | + $this->calDavBackend->getDenormalizedData($data); |
|
77 | + } catch (InvalidDataException $e) { |
|
78 | + $this->logger->info('Calendar object for calendar {cal} with uri {uri} still invalid', [ |
|
79 | + 'app' => 'dav', |
|
80 | + 'cal' => (int)$row['calendarid'], |
|
81 | + 'uri' => $row['uri'], |
|
82 | + ]); |
|
83 | + $warnings++; |
|
84 | + continue; |
|
85 | + } |
|
86 | + |
|
87 | + $this->calDavBackend->updateCalendarObject((int)$row['calendarid'], $row['uri'], $data); |
|
88 | + $count++; |
|
89 | + } |
|
90 | + } |
|
91 | + $output->finishProgress(); |
|
92 | + |
|
93 | + if ($warnings > 0) { |
|
94 | + $output->warning(sprintf('%d events could not be updated, see log file for more information', $warnings)); |
|
95 | + } |
|
96 | + if ($count > 0) { |
|
97 | + $output->info(sprintf('Updated %d events', $count)); |
|
98 | + } |
|
99 | + } |
|
100 | + |
|
101 | + protected function getInvalidObjects($pattern) { |
|
102 | + if ($this->db->getDatabasePlatform() instanceof OraclePlatform) { |
|
103 | + $rows = []; |
|
104 | + $chunkSize = 500; |
|
105 | + $query = $this->db->getQueryBuilder(); |
|
106 | + $query->select($query->func()->count('*', 'num_entries')) |
|
107 | + ->from('calendarobjects'); |
|
108 | + $result = $query->execute(); |
|
109 | + $count = $result->fetchColumn(); |
|
110 | + $result->closeCursor(); |
|
111 | + |
|
112 | + $numChunks = ceil($count / $chunkSize); |
|
113 | + |
|
114 | + $query = $this->db->getQueryBuilder(); |
|
115 | + $query->select(['calendarid', 'uri', 'calendardata']) |
|
116 | + ->from('calendarobjects') |
|
117 | + ->setMaxResults($chunkSize); |
|
118 | + for ($chunk = 0; $chunk < $numChunks; $chunk++) { |
|
119 | + $query->setFirstResult($chunk * $chunkSize); |
|
120 | + $result = $query->execute(); |
|
121 | + |
|
122 | + while ($row = $result->fetch()) { |
|
123 | + if (mb_strpos($row['calendardata'], $pattern) !== false) { |
|
124 | + unset($row['calendardata']); |
|
125 | + $rows[] = $row; |
|
126 | + } |
|
127 | + } |
|
128 | + $result->closeCursor(); |
|
129 | + } |
|
130 | + return $rows; |
|
131 | + } |
|
132 | + |
|
133 | + $query = $this->db->getQueryBuilder(); |
|
134 | + $query->select(['calendarid', 'uri']) |
|
135 | + ->from('calendarobjects') |
|
136 | + ->where($query->expr()->like( |
|
137 | + 'calendardata', |
|
138 | + $query->createNamedParameter( |
|
139 | + '%' . $this->db->escapeLikeParameter($pattern) . '%', |
|
140 | + IQueryBuilder::PARAM_STR |
|
141 | + ), |
|
142 | + IQueryBuilder::PARAM_STR |
|
143 | + )); |
|
144 | + |
|
145 | + $result = $query->execute(); |
|
146 | + $rows = $result->fetchAll(); |
|
147 | + $result->closeCursor(); |
|
148 | + |
|
149 | + return $rows; |
|
150 | + } |
|
151 | 151 | } |