2525
2626use OCP \IConfig ;
2727use OCP \IDBConnection ;
28+ use OCP \ILogger ;
2829use OCP \Migration \IOutput ;
2930use OCP \Migration \IRepairStep ;
3031use Sabre \VObject \Reader ;
32+ use Sabre \VObject \ParseException ;
3133
3234class 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 }
0 commit comments