Passed
Push — master ( 45294c...e80866 )
by Morris
10:23 queued 10s
created
lib/private/Repair/NC15/SetVcardDatabaseUID.php 2 patches
Indentation   +119 added lines, -119 removed lines patch added patch discarded remove patch
@@ -32,123 +32,123 @@
 block discarded – undo
32 32
 use Sabre\VObject\ParseException;
33 33
 
34 34
 class SetVcardDatabaseUID implements IRepairStep {
35
-	const MAX_ROWS = 1000;
36
-
37
-	/** @var IDBConnection */
38
-	private $connection;
39
-
40
-	/** @var IConfig */
41
-	private $config;
42
-
43
-	/** @var ILogger */
44
-	private $logger;
45
-
46
-	private $updateQuery;
47
-
48
-	public function __construct(IDBConnection $connection, IConfig $config, ILogger $logger) {
49
-		$this->connection = $connection;
50
-		$this->config     = $config;
51
-		$this->logger     = $logger;
52
-	}
53
-
54
-	public function getName() {
55
-		return 'Extract the vcard uid and store it in the db';
56
-	}
57
-
58
-	/**
59
-	 * @return \Generator
60
-	 * @suppress SqlInjectionChecker
61
-	 */
62
-	private function getInvalidEntries() {
63
-		$builder = $this->connection->getQueryBuilder();
64
-
65
-		$builder->select('id', 'carddata')
66
-		        ->from('cards')
67
-		        ->where($builder->expr()->isNull('uid'))
68
-		        ->setMaxResults(self::MAX_ROWS);
69
-
70
-		do {
71
-			$result = $builder->execute();
72
-			$rows   = $result->fetchAll();
73
-			foreach ($rows as $row) {
74
-				yield $row;
75
-			}
76
-			$result->closeCursor();
77
-		} while (count($rows) > 0);
78
-	}
79
-
80
-	/**
81
-	 * Extract UID from vcard
82
-	 *
83
-	 * @param string $cardData the vcard raw data
84
-	 * @param IOutput $output the output logger
85
-	 * @return string the uid or empty if none
86
-	 */
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);
98
-		}
99
-
100
-		return '';
101
-	}
102
-
103
-	/**
104
-	 * @param int $id
105
-	 * @param string $uid
106
-	 */
107
-	private function update(int $id, string $uid) {
108
-		if (!$this->updateQuery) {
109
-			$builder = $this->connection->getQueryBuilder();
110
-
111
-			$this->updateQuery = $builder->update('cards')
112
-			                             ->set('uid', $builder->createParameter('uid'))
113
-			                             ->where($builder->expr()->eq('id', $builder->createParameter('id')));
114
-		}
115
-
116
-		$this->updateQuery->setParameter('id', $id);
117
-		$this->updateQuery->setParameter('uid', $uid);
118
-
119
-		$this->updateQuery->execute();
120
-	}
121
-
122
-	private function repair(IOutput $output): int {
123
-		$this->connection->beginTransaction();
124
-		$entries = $this->getInvalidEntries();
125
-		$count   = 0;
126
-		foreach ($entries as $entry) {
127
-			$count++;
128
-			$cardData = $entry['carddata'];
129
-			if (is_resource($cardData)) {
130
-				$cardData = stream_get_contents($cardData);
131
-			}
132
-			$uid = $this->getUID($cardData, $output);
133
-			$this->update($entry['id'], $uid);
134
-		}
135
-		$this->connection->commit();
136
-
137
-		return $count;
138
-	}
139
-
140
-	private function shouldRun() {
141
-		$versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0.0');
142
-
143
-		// was added to 15.0.0.2
144
-		return version_compare($versionFromBeforeUpdate, '15.0.0.2', '<=');
145
-	}
146
-
147
-	public function run(IOutput $output) {
148
-		if ($this->shouldRun()) {
149
-			$count = $this->repair($output);
150
-
151
-			$output->info('Fixed ' . $count . ' vcards');
152
-		}
153
-	}
35
+    const MAX_ROWS = 1000;
36
+
37
+    /** @var IDBConnection */
38
+    private $connection;
39
+
40
+    /** @var IConfig */
41
+    private $config;
42
+
43
+    /** @var ILogger */
44
+    private $logger;
45
+
46
+    private $updateQuery;
47
+
48
+    public function __construct(IDBConnection $connection, IConfig $config, ILogger $logger) {
49
+        $this->connection = $connection;
50
+        $this->config     = $config;
51
+        $this->logger     = $logger;
52
+    }
53
+
54
+    public function getName() {
55
+        return 'Extract the vcard uid and store it in the db';
56
+    }
57
+
58
+    /**
59
+     * @return \Generator
60
+     * @suppress SqlInjectionChecker
61
+     */
62
+    private function getInvalidEntries() {
63
+        $builder = $this->connection->getQueryBuilder();
64
+
65
+        $builder->select('id', 'carddata')
66
+                ->from('cards')
67
+                ->where($builder->expr()->isNull('uid'))
68
+                ->setMaxResults(self::MAX_ROWS);
69
+
70
+        do {
71
+            $result = $builder->execute();
72
+            $rows   = $result->fetchAll();
73
+            foreach ($rows as $row) {
74
+                yield $row;
75
+            }
76
+            $result->closeCursor();
77
+        } while (count($rows) > 0);
78
+    }
79
+
80
+    /**
81
+     * Extract UID from vcard
82
+     *
83
+     * @param string $cardData the vcard raw data
84
+     * @param IOutput $output the output logger
85
+     * @return string the uid or empty if none
86
+     */
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);
98
+        }
99
+
100
+        return '';
101
+    }
102
+
103
+    /**
104
+     * @param int $id
105
+     * @param string $uid
106
+     */
107
+    private function update(int $id, string $uid) {
108
+        if (!$this->updateQuery) {
109
+            $builder = $this->connection->getQueryBuilder();
110
+
111
+            $this->updateQuery = $builder->update('cards')
112
+                                            ->set('uid', $builder->createParameter('uid'))
113
+                                            ->where($builder->expr()->eq('id', $builder->createParameter('id')));
114
+        }
115
+
116
+        $this->updateQuery->setParameter('id', $id);
117
+        $this->updateQuery->setParameter('uid', $uid);
118
+
119
+        $this->updateQuery->execute();
120
+    }
121
+
122
+    private function repair(IOutput $output): int {
123
+        $this->connection->beginTransaction();
124
+        $entries = $this->getInvalidEntries();
125
+        $count   = 0;
126
+        foreach ($entries as $entry) {
127
+            $count++;
128
+            $cardData = $entry['carddata'];
129
+            if (is_resource($cardData)) {
130
+                $cardData = stream_get_contents($cardData);
131
+            }
132
+            $uid = $this->getUID($cardData, $output);
133
+            $this->update($entry['id'], $uid);
134
+        }
135
+        $this->connection->commit();
136
+
137
+        return $count;
138
+    }
139
+
140
+    private function shouldRun() {
141
+        $versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0.0');
142
+
143
+        // was added to 15.0.0.2
144
+        return version_compare($versionFromBeforeUpdate, '15.0.0.2', '<=');
145
+    }
146
+
147
+    public function run(IOutput $output) {
148
+        if ($this->shouldRun()) {
149
+            $count = $this->repair($output);
150
+
151
+            $output->info('Fixed ' . $count . ' vcards');
152
+        }
153
+    }
154 154
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -148,7 +148,7 @@
 block discarded – undo
148 148
 		if ($this->shouldRun()) {
149 149
 			$count = $this->repair($output);
150 150
 
151
-			$output->info('Fixed ' . $count . ' vcards');
151
+			$output->info('Fixed '.$count.' vcards');
152 152
 		}
153 153
 	}
154 154
 }
Please login to merge, or discard this patch.
lib/private/Repair.php 1 patch
Indentation   +171 added lines, -171 removed lines patch added patch discarded remove patch
@@ -60,175 +60,175 @@
 block discarded – undo
60 60
 
61 61
 class Repair implements IOutput {
62 62
 
63
-	/** @var IRepairStep[] */
64
-	private $repairSteps;
65
-
66
-	/** @var EventDispatcher */
67
-	private $dispatcher;
68
-
69
-	/** @var string */
70
-	private $currentStep;
71
-
72
-	/**
73
-	 * Creates a new repair step runner
74
-	 *
75
-	 * @param IRepairStep[] $repairSteps array of RepairStep instances
76
-	 * @param EventDispatcher $dispatcher
77
-	 */
78
-	public function __construct($repairSteps = [], EventDispatcher $dispatcher = null) {
79
-		$this->repairSteps = $repairSteps;
80
-		$this->dispatcher  = $dispatcher;
81
-	}
82
-
83
-	/**
84
-	 * Run a series of repair steps for common problems
85
-	 */
86
-	public function run() {
87
-		if (count($this->repairSteps) === 0) {
88
-			$this->emit('\OC\Repair', 'info', array('No repair steps available'));
89
-
90
-			return;
91
-		}
92
-		// run each repair step
93
-		foreach ($this->repairSteps as $step) {
94
-			$this->currentStep = $step->getName();
95
-			$this->emit('\OC\Repair', 'step', [$this->currentStep]);
96
-			$step->run($this);
97
-		}
98
-	}
99
-
100
-	/**
101
-	 * Add repair step
102
-	 *
103
-	 * @param IRepairStep|string $repairStep repair step
104
-	 * @throws \Exception
105
-	 */
106
-	public function addStep($repairStep) {
107
-		if (is_string($repairStep)) {
108
-			try {
109
-				$s = \OC::$server->query($repairStep);
110
-			} catch (QueryException $e) {
111
-				if (class_exists($repairStep)) {
112
-					$s = new $repairStep();
113
-				} else {
114
-					throw new \Exception("Repair step '$repairStep' is unknown");
115
-				}
116
-			}
117
-
118
-			if ($s instanceof IRepairStep) {
119
-				$this->repairSteps[] = $s;
120
-			} else {
121
-				throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep");
122
-			}
123
-		} else {
124
-			$this->repairSteps[] = $repairStep;
125
-		}
126
-	}
127
-
128
-	/**
129
-	 * Returns the default repair steps to be run on the
130
-	 * command line or after an upgrade.
131
-	 *
132
-	 * @return IRepairStep[]
133
-	 */
134
-	public static function getRepairSteps() {
135
-		return [
136
-			new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->getDatabaseConnection(), false),
137
-			new RepairMimeTypes(\OC::$server->getConfig()),
138
-			new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()),
139
-			new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
140
-			new RemoveRootShares(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager(), \OC::$server->getLazyRootFolder()),
141
-			new MoveUpdaterStepFile(\OC::$server->getConfig()),
142
-			new FixMountStorages(\OC::$server->getDatabaseConnection()),
143
-			new RepairInvalidPaths(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()),
144
-			new AddLogRotateJob(\OC::$server->getJobList()),
145
-			new ClearFrontendCaches(\OC::$server->getMemCacheFactory(), \OC::$server->query(SCSSCacher::class), \OC::$server->query(JSCombiner::class)),
146
-			new ClearGeneratedAvatarCache(\OC::$server->getConfig(), \OC::$server->query(AvatarManager::class)),
147
-			new AddPreviewBackgroundCleanupJob(\OC::$server->getJobList()),
148
-			new AddCleanupUpdaterBackupsJob(\OC::$server->getJobList()),
149
-			new RepairPendingCronJobs(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()),
150
-			new SetVcardDatabaseUID(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig(), \OC::$server->getLogger())
151
-		];
152
-	}
153
-
154
-	/**
155
-	 * Returns expensive repair steps to be run on the
156
-	 * command line with a special option.
157
-	 *
158
-	 * @return IRepairStep[]
159
-	 */
160
-	public static function getExpensiveRepairSteps() {
161
-		return [
162
-			new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager())
163
-		];
164
-	}
165
-
166
-	/**
167
-	 * Returns the repair steps to be run before an
168
-	 * upgrade.
169
-	 *
170
-	 * @return IRepairStep[]
171
-	 */
172
-	public static function getBeforeUpgradeRepairSteps() {
173
-		$connection = \OC::$server->getDatabaseConnection();
174
-		$config     = \OC::$server->getConfig();
175
-		$steps      = [
176
-			new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), $connection, true),
177
-			new SqliteAutoincrement($connection),
178
-			new SaveAccountsTableData($connection, $config),
179
-			new DropAccountTermsTable($connection)
180
-		];
181
-
182
-		return $steps;
183
-	}
184
-
185
-	/**
186
-	 * @param string $scope
187
-	 * @param string $method
188
-	 * @param array $arguments
189
-	 */
190
-	public function emit($scope, $method, array $arguments = []) {
191
-		if (!is_null($this->dispatcher)) {
192
-			$this->dispatcher->dispatch("$scope::$method",
193
-				new GenericEvent("$scope::$method", $arguments));
194
-		}
195
-	}
196
-
197
-	public function info($string) {
198
-		// for now just emit as we did in the past
199
-		$this->emit('\OC\Repair', 'info', array($string));
200
-	}
201
-
202
-	/**
203
-	 * @param string $message
204
-	 */
205
-	public function warning($message) {
206
-		// for now just emit as we did in the past
207
-		$this->emit('\OC\Repair', 'warning', [$message]);
208
-	}
209
-
210
-	/**
211
-	 * @param int $max
212
-	 */
213
-	public function startProgress($max = 0) {
214
-		// for now just emit as we did in the past
215
-		$this->emit('\OC\Repair', 'startProgress', [$max, $this->currentStep]);
216
-	}
217
-
218
-	/**
219
-	 * @param int $step
220
-	 * @param string $description
221
-	 */
222
-	public function advance($step = 1, $description = '') {
223
-		// for now just emit as we did in the past
224
-		$this->emit('\OC\Repair', 'advance', [$step, $description]);
225
-	}
226
-
227
-	/**
228
-	 * @param int $max
229
-	 */
230
-	public function finishProgress() {
231
-		// for now just emit as we did in the past
232
-		$this->emit('\OC\Repair', 'finishProgress', []);
233
-	}
63
+    /** @var IRepairStep[] */
64
+    private $repairSteps;
65
+
66
+    /** @var EventDispatcher */
67
+    private $dispatcher;
68
+
69
+    /** @var string */
70
+    private $currentStep;
71
+
72
+    /**
73
+     * Creates a new repair step runner
74
+     *
75
+     * @param IRepairStep[] $repairSteps array of RepairStep instances
76
+     * @param EventDispatcher $dispatcher
77
+     */
78
+    public function __construct($repairSteps = [], EventDispatcher $dispatcher = null) {
79
+        $this->repairSteps = $repairSteps;
80
+        $this->dispatcher  = $dispatcher;
81
+    }
82
+
83
+    /**
84
+     * Run a series of repair steps for common problems
85
+     */
86
+    public function run() {
87
+        if (count($this->repairSteps) === 0) {
88
+            $this->emit('\OC\Repair', 'info', array('No repair steps available'));
89
+
90
+            return;
91
+        }
92
+        // run each repair step
93
+        foreach ($this->repairSteps as $step) {
94
+            $this->currentStep = $step->getName();
95
+            $this->emit('\OC\Repair', 'step', [$this->currentStep]);
96
+            $step->run($this);
97
+        }
98
+    }
99
+
100
+    /**
101
+     * Add repair step
102
+     *
103
+     * @param IRepairStep|string $repairStep repair step
104
+     * @throws \Exception
105
+     */
106
+    public function addStep($repairStep) {
107
+        if (is_string($repairStep)) {
108
+            try {
109
+                $s = \OC::$server->query($repairStep);
110
+            } catch (QueryException $e) {
111
+                if (class_exists($repairStep)) {
112
+                    $s = new $repairStep();
113
+                } else {
114
+                    throw new \Exception("Repair step '$repairStep' is unknown");
115
+                }
116
+            }
117
+
118
+            if ($s instanceof IRepairStep) {
119
+                $this->repairSteps[] = $s;
120
+            } else {
121
+                throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep");
122
+            }
123
+        } else {
124
+            $this->repairSteps[] = $repairStep;
125
+        }
126
+    }
127
+
128
+    /**
129
+     * Returns the default repair steps to be run on the
130
+     * command line or after an upgrade.
131
+     *
132
+     * @return IRepairStep[]
133
+     */
134
+    public static function getRepairSteps() {
135
+        return [
136
+            new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->getDatabaseConnection(), false),
137
+            new RepairMimeTypes(\OC::$server->getConfig()),
138
+            new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()),
139
+            new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
140
+            new RemoveRootShares(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager(), \OC::$server->getLazyRootFolder()),
141
+            new MoveUpdaterStepFile(\OC::$server->getConfig()),
142
+            new FixMountStorages(\OC::$server->getDatabaseConnection()),
143
+            new RepairInvalidPaths(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()),
144
+            new AddLogRotateJob(\OC::$server->getJobList()),
145
+            new ClearFrontendCaches(\OC::$server->getMemCacheFactory(), \OC::$server->query(SCSSCacher::class), \OC::$server->query(JSCombiner::class)),
146
+            new ClearGeneratedAvatarCache(\OC::$server->getConfig(), \OC::$server->query(AvatarManager::class)),
147
+            new AddPreviewBackgroundCleanupJob(\OC::$server->getJobList()),
148
+            new AddCleanupUpdaterBackupsJob(\OC::$server->getJobList()),
149
+            new RepairPendingCronJobs(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()),
150
+            new SetVcardDatabaseUID(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig(), \OC::$server->getLogger())
151
+        ];
152
+    }
153
+
154
+    /**
155
+     * Returns expensive repair steps to be run on the
156
+     * command line with a special option.
157
+     *
158
+     * @return IRepairStep[]
159
+     */
160
+    public static function getExpensiveRepairSteps() {
161
+        return [
162
+            new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager())
163
+        ];
164
+    }
165
+
166
+    /**
167
+     * Returns the repair steps to be run before an
168
+     * upgrade.
169
+     *
170
+     * @return IRepairStep[]
171
+     */
172
+    public static function getBeforeUpgradeRepairSteps() {
173
+        $connection = \OC::$server->getDatabaseConnection();
174
+        $config     = \OC::$server->getConfig();
175
+        $steps      = [
176
+            new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), $connection, true),
177
+            new SqliteAutoincrement($connection),
178
+            new SaveAccountsTableData($connection, $config),
179
+            new DropAccountTermsTable($connection)
180
+        ];
181
+
182
+        return $steps;
183
+    }
184
+
185
+    /**
186
+     * @param string $scope
187
+     * @param string $method
188
+     * @param array $arguments
189
+     */
190
+    public function emit($scope, $method, array $arguments = []) {
191
+        if (!is_null($this->dispatcher)) {
192
+            $this->dispatcher->dispatch("$scope::$method",
193
+                new GenericEvent("$scope::$method", $arguments));
194
+        }
195
+    }
196
+
197
+    public function info($string) {
198
+        // for now just emit as we did in the past
199
+        $this->emit('\OC\Repair', 'info', array($string));
200
+    }
201
+
202
+    /**
203
+     * @param string $message
204
+     */
205
+    public function warning($message) {
206
+        // for now just emit as we did in the past
207
+        $this->emit('\OC\Repair', 'warning', [$message]);
208
+    }
209
+
210
+    /**
211
+     * @param int $max
212
+     */
213
+    public function startProgress($max = 0) {
214
+        // for now just emit as we did in the past
215
+        $this->emit('\OC\Repair', 'startProgress', [$max, $this->currentStep]);
216
+    }
217
+
218
+    /**
219
+     * @param int $step
220
+     * @param string $description
221
+     */
222
+    public function advance($step = 1, $description = '') {
223
+        // for now just emit as we did in the past
224
+        $this->emit('\OC\Repair', 'advance', [$step, $description]);
225
+    }
226
+
227
+    /**
228
+     * @param int $max
229
+     */
230
+    public function finishProgress() {
231
+        // for now just emit as we did in the past
232
+        $this->emit('\OC\Repair', 'finishProgress', []);
233
+    }
234 234
 }
Please login to merge, or discard this patch.