Log and continue on Dav reader failure

Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
This commit is contained in:
John Molakvoæ (skjnldsv) 2018-12-05 19:41:19 +01:00
parent 5e44699139
commit 2be63bcb6a
No known key found for this signature in database
GPG key ID: 60C25B8C072916CF
3 changed files with 32 additions and 12 deletions

View file

@ -147,7 +147,7 @@ class Repair implements IOutput {
new AddPreviewBackgroundCleanupJob(\OC::$server->getJobList()), new AddPreviewBackgroundCleanupJob(\OC::$server->getJobList()),
new AddCleanupUpdaterBackupsJob(\OC::$server->getJobList()), new AddCleanupUpdaterBackupsJob(\OC::$server->getJobList()),
new RepairPendingCronJobs(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()), new RepairPendingCronJobs(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()),
new SetVcardDatabaseUID(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()) new SetVcardDatabaseUID(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig(), \OC::$server->getLogger())
]; ];
} }

View file

@ -25,9 +25,11 @@ namespace OC\Repair\NC15;
use OCP\IConfig; use OCP\IConfig;
use OCP\IDBConnection; use OCP\IDBConnection;
use OCP\ILogger;
use OCP\Migration\IOutput; use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep; use OCP\Migration\IRepairStep;
use Sabre\VObject\Reader; use Sabre\VObject\Reader;
use Sabre\VObject\ParseException;
class SetVcardDatabaseUID implements IRepairStep { class SetVcardDatabaseUID implements IRepairStep {
const MAX_ROWS = 1000; const MAX_ROWS = 1000;
@ -38,11 +40,15 @@ class SetVcardDatabaseUID implements IRepairStep {
/** @var IConfig */ /** @var IConfig */
private $config; private $config;
/** @var ILogger */
private $logger;
private $updateQuery; private $updateQuery;
public function __construct(IDBConnection $connection, IConfig $config) { public function __construct(IDBConnection $connection, IConfig $config, ILogger $logger) {
$this->connection = $connection; $this->connection = $connection;
$this->config = $config; $this->config = $config;
$this->logger = $logger;
} }
public function getName() { public function getName() {
@ -75,13 +81,20 @@ class SetVcardDatabaseUID implements IRepairStep {
* Extract UID from vcard * Extract UID from vcard
* *
* @param string $cardData the vcard raw data * @param string $cardData the vcard raw data
* @param IOutput $output the output logger
* @return string the uid or empty if none * @return string the uid or empty if none
*/ */
private function getUID(string $cardData): string { private function getUID(string $cardData, IOutput $output): string {
$vCard = Reader::read($cardData); try {
if ($vCard->UID) { $vCard = Reader::read($cardData);
$uid = $vCard->UID->getValue(); if ($vCard->UID) {
return $uid; $uid = $vCard->UID->getValue();
return $uid;
}
} catch (ParseException $e) {
$output->warning('One vCard is broken. We logged the exception and will continue the repair.');
$this->logger->logException($e);
} }
return ''; return '';
@ -106,7 +119,7 @@ class SetVcardDatabaseUID implements IRepairStep {
$this->updateQuery->execute(); $this->updateQuery->execute();
} }
private function repair(): int { private function repair(IOutput $output): int {
$this->connection->beginTransaction(); $this->connection->beginTransaction();
$entries = $this->getInvalidEntries(); $entries = $this->getInvalidEntries();
$count = 0; $count = 0;
@ -116,7 +129,7 @@ class SetVcardDatabaseUID implements IRepairStep {
if (is_resource($cardData)) { if (is_resource($cardData)) {
$cardData = stream_get_contents($cardData); $cardData = stream_get_contents($cardData);
} }
$uid = $this->getUID($cardData); $uid = $this->getUID($cardData, $output);
$this->update($entry['id'], $uid); $this->update($entry['id'], $uid);
} }
$this->connection->commit(); $this->connection->commit();
@ -133,7 +146,7 @@ class SetVcardDatabaseUID implements IRepairStep {
public function run(IOutput $output) { public function run(IOutput $output) {
if ($this->shouldRun()) { if ($this->shouldRun()) {
$count = $this->repair(); $count = $this->repair($output);
$output->info('Fixed ' . $count . ' vcards'); $output->info('Fixed ' . $count . ' vcards');
} }

View file

@ -24,6 +24,8 @@
namespace Test\Repair; namespace Test\Repair;
use OCP\IConfig; use OCP\IConfig;
use OCP\ILogger;
use OCP\Migration\IOutput;
use OC\Repair\NC15\SetVcardDatabaseUID; use OC\Repair\NC15\SetVcardDatabaseUID;
use Test\TestCase; use Test\TestCase;
@ -38,11 +40,15 @@ class SetVcardDatabaseUIDTest extends TestCase {
/** @var IConfig */ /** @var IConfig */
private $config; private $config;
/** @var Ilogger */
private $logger;
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$this->config = $this->createMock(IConfig::class); $this->config = $this->createMock(IConfig::class);
$this->repair = new SetVcardDatabaseUID(\OC::$server->getDatabaseConnection(), $this->config); $this->logger = $this->createMock(Ilogger::class);
$this->repair = new SetVcardDatabaseUID(\OC::$server->getDatabaseConnection(), $this->config, $this->logger);
} }
protected function tearDown() { protected function tearDown() {
@ -86,7 +92,8 @@ class SetVcardDatabaseUIDTest extends TestCase {
* @param string|boolean $expected * @param string|boolean $expected
*/ */
public function testExtractUIDFromVcard($from, $expected) { public function testExtractUIDFromVcard($from, $expected) {
$uid = $this->invokePrivate($this->repair, 'getUid', ['carddata' => $from]); $output = $this->createMock(IOutput::class);
$uid = $this->invokePrivate($this->repair, 'getUid', ['carddata' => $from, 'output' => $output]);
$this->assertEquals($expected, $uid); $this->assertEquals($expected, $uid);
} }