Skip to content

Commit f4497bb

Browse files
authored
Merge pull request #12867 from nextcloud/backport/12854/stable15
[stable15] Log and continue on Dav reader failure (repair uid)
2 parents 61c17b5 + 7028362 commit f4497bb

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

lib/private/Repair.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public static function getRepairSteps() {
147147
new AddPreviewBackgroundCleanupJob(\OC::$server->getJobList()),
148148
new AddCleanupUpdaterBackupsJob(\OC::$server->getJobList()),
149149
new RepairPendingCronJobs(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()),
150-
new SetVcardDatabaseUID(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig())
150+
new SetVcardDatabaseUID(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig(), \OC::$server->getLogger())
151151
];
152152
}
153153

lib/private/Repair/NC15/SetVcardDatabaseUID.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525

2626
use OCP\IConfig;
2727
use OCP\IDBConnection;
28+
use OCP\ILogger;
2829
use OCP\Migration\IOutput;
2930
use OCP\Migration\IRepairStep;
3031
use Sabre\VObject\Reader;
32+
use Sabre\VObject\ParseException;
3133

3234
class SetVcardDatabaseUID implements IRepairStep {
3335
const MAX_ROWS = 1000;
@@ -38,11 +40,15 @@ class SetVcardDatabaseUID implements IRepairStep {
3840
/** @var IConfig */
3941
private $config;
4042

43+
/** @var ILogger */
44+
private $logger;
45+
4146
private $updateQuery;
4247

43-
public function __construct(IDBConnection $connection, IConfig $config) {
48+
public function __construct(IDBConnection $connection, IConfig $config, ILogger $logger) {
4449
$this->connection = $connection;
4550
$this->config = $config;
51+
$this->logger = $logger;
4652
}
4753

4854
public function getName() {
@@ -75,13 +81,20 @@ private function getInvalidEntries() {
7581
* Extract UID from vcard
7682
*
7783
* @param string $cardData the vcard raw data
84+
* @param IOutput $output the output logger
7885
* @return string the uid or empty if none
7986
*/
80-
private function getUID(string $cardData): string {
81-
$vCard = Reader::read($cardData);
82-
if ($vCard->UID) {
83-
$uid = $vCard->UID->getValue();
84-
return $uid;
87+
private function getUID(string $cardData, IOutput $output): string {
88+
try {
89+
$vCard = Reader::read($cardData);
90+
if ($vCard->UID) {
91+
$uid = $vCard->UID->getValue();
92+
93+
return $uid;
94+
}
95+
} catch (ParseException $e) {
96+
$output->warning('One vCard is broken. We logged the exception and will continue the repair.');
97+
$this->logger->logException($e);
8598
}
8699

87100
return '';
@@ -106,7 +119,7 @@ private function update(int $id, string $uid) {
106119
$this->updateQuery->execute();
107120
}
108121

109-
private function repair(): int {
122+
private function repair(IOutput $output): int {
110123
$this->connection->beginTransaction();
111124
$entries = $this->getInvalidEntries();
112125
$count = 0;
@@ -116,7 +129,7 @@ private function repair(): int {
116129
if (is_resource($cardData)) {
117130
$cardData = stream_get_contents($cardData);
118131
}
119-
$uid = $this->getUID($cardData);
132+
$uid = $this->getUID($cardData, $output);
120133
$this->update($entry['id'], $uid);
121134
}
122135
$this->connection->commit();
@@ -133,7 +146,7 @@ private function shouldRun() {
133146

134147
public function run(IOutput $output) {
135148
if ($this->shouldRun()) {
136-
$count = $this->repair();
149+
$count = $this->repair($output);
137150

138151
$output->info('Fixed ' . $count . ' vcards');
139152
}

tests/lib/Repair/SetVcardDatabaseUIDTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
namespace Test\Repair;
2525

2626
use OCP\IConfig;
27+
use OCP\ILogger;
28+
use OCP\Migration\IOutput;
2729
use OC\Repair\NC15\SetVcardDatabaseUID;
2830
use Test\TestCase;
2931

@@ -38,11 +40,15 @@ class SetVcardDatabaseUIDTest extends TestCase {
3840
/** @var IConfig */
3941
private $config;
4042

43+
/** @var Ilogger */
44+
private $logger;
45+
4146
protected function setUp() {
4247
parent::setUp();
4348

4449
$this->config = $this->createMock(IConfig::class);
45-
$this->repair = new SetVcardDatabaseUID(\OC::$server->getDatabaseConnection(), $this->config);
50+
$this->logger = $this->createMock(Ilogger::class);
51+
$this->repair = new SetVcardDatabaseUID(\OC::$server->getDatabaseConnection(), $this->config, $this->logger);
4652
}
4753

4854
protected function tearDown() {
@@ -86,7 +92,8 @@ public function dataTestVcards() {
8692
* @param string|boolean $expected
8793
*/
8894
public function testExtractUIDFromVcard($from, $expected) {
89-
$uid = $this->invokePrivate($this->repair, 'getUid', ['carddata' => $from]);
95+
$output = $this->createMock(IOutput::class);
96+
$uid = $this->invokePrivate($this->repair, 'getUid', ['carddata' => $from, 'output' => $output]);
9097
$this->assertEquals($expected, $uid);
9198
}
9299

0 commit comments

Comments
 (0)