@@ -52,178 +52,178 @@ |
||
| 52 | 52 | use Symfony\Component\EventDispatcher\GenericEvent; |
| 53 | 53 | |
| 54 | 54 | class Repair implements IOutput{ |
| 55 | - /* @var IRepairStep[] */ |
|
| 56 | - private $repairSteps; |
|
| 57 | - /** @var EventDispatcher */ |
|
| 58 | - private $dispatcher; |
|
| 59 | - /** @var string */ |
|
| 60 | - private $currentStep; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Creates a new repair step runner |
|
| 64 | - * |
|
| 65 | - * @param IRepairStep[] $repairSteps array of RepairStep instances |
|
| 66 | - * @param EventDispatcher $dispatcher |
|
| 67 | - */ |
|
| 68 | - public function __construct($repairSteps = [], EventDispatcher $dispatcher = null) { |
|
| 69 | - $this->repairSteps = $repairSteps; |
|
| 70 | - $this->dispatcher = $dispatcher; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Run a series of repair steps for common problems |
|
| 75 | - */ |
|
| 76 | - public function run() { |
|
| 77 | - if (count($this->repairSteps) === 0) { |
|
| 78 | - $this->emit('\OC\Repair', 'info', array('No repair steps available')); |
|
| 79 | - return; |
|
| 80 | - } |
|
| 81 | - // run each repair step |
|
| 82 | - foreach ($this->repairSteps as $step) { |
|
| 83 | - $this->currentStep = $step->getName(); |
|
| 84 | - $this->emit('\OC\Repair', 'step', [$this->currentStep]); |
|
| 85 | - $step->run($this); |
|
| 86 | - } |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Add repair step |
|
| 91 | - * |
|
| 92 | - * @param IRepairStep|string $repairStep repair step |
|
| 93 | - * @throws \Exception |
|
| 94 | - */ |
|
| 95 | - public function addStep($repairStep) { |
|
| 96 | - if (is_string($repairStep)) { |
|
| 97 | - try { |
|
| 98 | - $s = \OC::$server->query($repairStep); |
|
| 99 | - } catch (QueryException $e) { |
|
| 100 | - if (class_exists($repairStep)) { |
|
| 101 | - $s = new $repairStep(); |
|
| 102 | - } else { |
|
| 103 | - throw new \Exception("Repair step '$repairStep' is unknown"); |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - if ($s instanceof IRepairStep) { |
|
| 108 | - $this->repairSteps[] = $s; |
|
| 109 | - } else { |
|
| 110 | - throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep"); |
|
| 111 | - } |
|
| 112 | - } else { |
|
| 113 | - $this->repairSteps[] = $repairStep; |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Returns the default repair steps to be run on the |
|
| 119 | - * command line or after an upgrade. |
|
| 120 | - * |
|
| 121 | - * @return IRepairStep[] |
|
| 122 | - */ |
|
| 123 | - public static function getRepairSteps() { |
|
| 124 | - return [ |
|
| 125 | - new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->getDatabaseConnection(), false), |
|
| 126 | - new RepairMimeTypes(\OC::$server->getConfig()), |
|
| 127 | - new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()), |
|
| 128 | - new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()), |
|
| 129 | - new RemoveRootShares(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager(), \OC::$server->getLazyRootFolder()), |
|
| 130 | - new MoveUpdaterStepFile(\OC::$server->getConfig()), |
|
| 131 | - new MoveAvatars( |
|
| 132 | - \OC::$server->getJobList(), |
|
| 133 | - \OC::$server->getConfig() |
|
| 134 | - ), |
|
| 135 | - new CleanPreviews( |
|
| 136 | - \OC::$server->getJobList(), |
|
| 137 | - \OC::$server->getUserManager(), |
|
| 138 | - \OC::$server->getConfig() |
|
| 139 | - ), |
|
| 140 | - new FixMountStorages(\OC::$server->getDatabaseConnection()), |
|
| 141 | - new UpdateLanguageCodes(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()), |
|
| 142 | - new InstallCoreBundle( |
|
| 143 | - \OC::$server->query(BundleFetcher::class), |
|
| 144 | - \OC::$server->getConfig(), |
|
| 145 | - \OC::$server->query(Installer::class) |
|
| 146 | - ) |
|
| 147 | - ]; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Returns expensive repair steps to be run on the |
|
| 152 | - * command line with a special option. |
|
| 153 | - * |
|
| 154 | - * @return IRepairStep[] |
|
| 155 | - */ |
|
| 156 | - public static function getExpensiveRepairSteps() { |
|
| 157 | - return [ |
|
| 158 | - new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager()), |
|
| 159 | - ]; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Returns the repair steps to be run before an |
|
| 164 | - * upgrade. |
|
| 165 | - * |
|
| 166 | - * @return IRepairStep[] |
|
| 167 | - */ |
|
| 168 | - public static function getBeforeUpgradeRepairSteps() { |
|
| 169 | - $connection = \OC::$server->getDatabaseConnection(); |
|
| 170 | - $config = \OC::$server->getConfig(); |
|
| 171 | - $steps = [ |
|
| 172 | - new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), $connection, true), |
|
| 173 | - new SqliteAutoincrement($connection), |
|
| 174 | - new SaveAccountsTableData($connection, $config), |
|
| 175 | - ]; |
|
| 176 | - |
|
| 177 | - return $steps; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * @param string $scope |
|
| 182 | - * @param string $method |
|
| 183 | - * @param array $arguments |
|
| 184 | - */ |
|
| 185 | - public function emit($scope, $method, array $arguments = []) { |
|
| 186 | - if (!is_null($this->dispatcher)) { |
|
| 187 | - $this->dispatcher->dispatch("$scope::$method", |
|
| 188 | - new GenericEvent("$scope::$method", $arguments)); |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - public function info($string) { |
|
| 193 | - // for now just emit as we did in the past |
|
| 194 | - $this->emit('\OC\Repair', 'info', array($string)); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * @param string $message |
|
| 199 | - */ |
|
| 200 | - public function warning($message) { |
|
| 201 | - // for now just emit as we did in the past |
|
| 202 | - $this->emit('\OC\Repair', 'warning', [$message]); |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * @param int $max |
|
| 207 | - */ |
|
| 208 | - public function startProgress($max = 0) { |
|
| 209 | - // for now just emit as we did in the past |
|
| 210 | - $this->emit('\OC\Repair', 'startProgress', [$max, $this->currentStep]); |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * @param int $step |
|
| 215 | - * @param string $description |
|
| 216 | - */ |
|
| 217 | - public function advance($step = 1, $description = '') { |
|
| 218 | - // for now just emit as we did in the past |
|
| 219 | - $this->emit('\OC\Repair', 'advance', [$step, $description]); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * @param int $max |
|
| 224 | - */ |
|
| 225 | - public function finishProgress() { |
|
| 226 | - // for now just emit as we did in the past |
|
| 227 | - $this->emit('\OC\Repair', 'finishProgress', []); |
|
| 228 | - } |
|
| 55 | + /* @var IRepairStep[] */ |
|
| 56 | + private $repairSteps; |
|
| 57 | + /** @var EventDispatcher */ |
|
| 58 | + private $dispatcher; |
|
| 59 | + /** @var string */ |
|
| 60 | + private $currentStep; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Creates a new repair step runner |
|
| 64 | + * |
|
| 65 | + * @param IRepairStep[] $repairSteps array of RepairStep instances |
|
| 66 | + * @param EventDispatcher $dispatcher |
|
| 67 | + */ |
|
| 68 | + public function __construct($repairSteps = [], EventDispatcher $dispatcher = null) { |
|
| 69 | + $this->repairSteps = $repairSteps; |
|
| 70 | + $this->dispatcher = $dispatcher; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Run a series of repair steps for common problems |
|
| 75 | + */ |
|
| 76 | + public function run() { |
|
| 77 | + if (count($this->repairSteps) === 0) { |
|
| 78 | + $this->emit('\OC\Repair', 'info', array('No repair steps available')); |
|
| 79 | + return; |
|
| 80 | + } |
|
| 81 | + // run each repair step |
|
| 82 | + foreach ($this->repairSteps as $step) { |
|
| 83 | + $this->currentStep = $step->getName(); |
|
| 84 | + $this->emit('\OC\Repair', 'step', [$this->currentStep]); |
|
| 85 | + $step->run($this); |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Add repair step |
|
| 91 | + * |
|
| 92 | + * @param IRepairStep|string $repairStep repair step |
|
| 93 | + * @throws \Exception |
|
| 94 | + */ |
|
| 95 | + public function addStep($repairStep) { |
|
| 96 | + if (is_string($repairStep)) { |
|
| 97 | + try { |
|
| 98 | + $s = \OC::$server->query($repairStep); |
|
| 99 | + } catch (QueryException $e) { |
|
| 100 | + if (class_exists($repairStep)) { |
|
| 101 | + $s = new $repairStep(); |
|
| 102 | + } else { |
|
| 103 | + throw new \Exception("Repair step '$repairStep' is unknown"); |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + if ($s instanceof IRepairStep) { |
|
| 108 | + $this->repairSteps[] = $s; |
|
| 109 | + } else { |
|
| 110 | + throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep"); |
|
| 111 | + } |
|
| 112 | + } else { |
|
| 113 | + $this->repairSteps[] = $repairStep; |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Returns the default repair steps to be run on the |
|
| 119 | + * command line or after an upgrade. |
|
| 120 | + * |
|
| 121 | + * @return IRepairStep[] |
|
| 122 | + */ |
|
| 123 | + public static function getRepairSteps() { |
|
| 124 | + return [ |
|
| 125 | + new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->getDatabaseConnection(), false), |
|
| 126 | + new RepairMimeTypes(\OC::$server->getConfig()), |
|
| 127 | + new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()), |
|
| 128 | + new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()), |
|
| 129 | + new RemoveRootShares(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager(), \OC::$server->getLazyRootFolder()), |
|
| 130 | + new MoveUpdaterStepFile(\OC::$server->getConfig()), |
|
| 131 | + new MoveAvatars( |
|
| 132 | + \OC::$server->getJobList(), |
|
| 133 | + \OC::$server->getConfig() |
|
| 134 | + ), |
|
| 135 | + new CleanPreviews( |
|
| 136 | + \OC::$server->getJobList(), |
|
| 137 | + \OC::$server->getUserManager(), |
|
| 138 | + \OC::$server->getConfig() |
|
| 139 | + ), |
|
| 140 | + new FixMountStorages(\OC::$server->getDatabaseConnection()), |
|
| 141 | + new UpdateLanguageCodes(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()), |
|
| 142 | + new InstallCoreBundle( |
|
| 143 | + \OC::$server->query(BundleFetcher::class), |
|
| 144 | + \OC::$server->getConfig(), |
|
| 145 | + \OC::$server->query(Installer::class) |
|
| 146 | + ) |
|
| 147 | + ]; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Returns expensive repair steps to be run on the |
|
| 152 | + * command line with a special option. |
|
| 153 | + * |
|
| 154 | + * @return IRepairStep[] |
|
| 155 | + */ |
|
| 156 | + public static function getExpensiveRepairSteps() { |
|
| 157 | + return [ |
|
| 158 | + new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager()), |
|
| 159 | + ]; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Returns the repair steps to be run before an |
|
| 164 | + * upgrade. |
|
| 165 | + * |
|
| 166 | + * @return IRepairStep[] |
|
| 167 | + */ |
|
| 168 | + public static function getBeforeUpgradeRepairSteps() { |
|
| 169 | + $connection = \OC::$server->getDatabaseConnection(); |
|
| 170 | + $config = \OC::$server->getConfig(); |
|
| 171 | + $steps = [ |
|
| 172 | + new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), $connection, true), |
|
| 173 | + new SqliteAutoincrement($connection), |
|
| 174 | + new SaveAccountsTableData($connection, $config), |
|
| 175 | + ]; |
|
| 176 | + |
|
| 177 | + return $steps; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * @param string $scope |
|
| 182 | + * @param string $method |
|
| 183 | + * @param array $arguments |
|
| 184 | + */ |
|
| 185 | + public function emit($scope, $method, array $arguments = []) { |
|
| 186 | + if (!is_null($this->dispatcher)) { |
|
| 187 | + $this->dispatcher->dispatch("$scope::$method", |
|
| 188 | + new GenericEvent("$scope::$method", $arguments)); |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + public function info($string) { |
|
| 193 | + // for now just emit as we did in the past |
|
| 194 | + $this->emit('\OC\Repair', 'info', array($string)); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * @param string $message |
|
| 199 | + */ |
|
| 200 | + public function warning($message) { |
|
| 201 | + // for now just emit as we did in the past |
|
| 202 | + $this->emit('\OC\Repair', 'warning', [$message]); |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * @param int $max |
|
| 207 | + */ |
|
| 208 | + public function startProgress($max = 0) { |
|
| 209 | + // for now just emit as we did in the past |
|
| 210 | + $this->emit('\OC\Repair', 'startProgress', [$max, $this->currentStep]); |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * @param int $step |
|
| 215 | + * @param string $description |
|
| 216 | + */ |
|
| 217 | + public function advance($step = 1, $description = '') { |
|
| 218 | + // for now just emit as we did in the past |
|
| 219 | + $this->emit('\OC\Repair', 'advance', [$step, $description]); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * @param int $max |
|
| 224 | + */ |
|
| 225 | + public function finishProgress() { |
|
| 226 | + // for now just emit as we did in the past |
|
| 227 | + $this->emit('\OC\Repair', 'finishProgress', []); |
|
| 228 | + } |
|
| 229 | 229 | } |
@@ -37,140 +37,140 @@ |
||
| 37 | 37 | */ |
| 38 | 38 | class SaveAccountsTableData implements IRepairStep { |
| 39 | 39 | |
| 40 | - const BATCH_SIZE = 75; |
|
| 41 | - |
|
| 42 | - /** @var IDBConnection */ |
|
| 43 | - protected $db; |
|
| 44 | - |
|
| 45 | - /** @var IConfig */ |
|
| 46 | - protected $config; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * @param IDBConnection $db |
|
| 50 | - * @param IConfig $config |
|
| 51 | - */ |
|
| 52 | - public function __construct(IDBConnection $db, IConfig $config) { |
|
| 53 | - $this->db = $db; |
|
| 54 | - $this->config = $config; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @return string |
|
| 59 | - */ |
|
| 60 | - public function getName() { |
|
| 61 | - return 'Copy data from accounts table when migrating from ownCloud'; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * @param IOutput $output |
|
| 66 | - */ |
|
| 67 | - public function run(IOutput $output) { |
|
| 68 | - if (!$this->shouldRun()) { |
|
| 69 | - return; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - $offset = 0; |
|
| 73 | - $numUsers = $this->runStep($offset); |
|
| 74 | - |
|
| 75 | - while ($numUsers === self::BATCH_SIZE) { |
|
| 76 | - $offset += $numUsers; |
|
| 77 | - $numUsers = $this->runStep($offset); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - // Remove the table |
|
| 81 | - $this->db->dropTable('accounts'); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * @return bool |
|
| 86 | - */ |
|
| 87 | - protected function shouldRun() { |
|
| 88 | - $query = $this->db->getQueryBuilder(); |
|
| 89 | - $query->select('*') |
|
| 90 | - ->from('accounts') |
|
| 91 | - ->where($query->expr()->isNotNull('user_id')) |
|
| 92 | - ->setMaxResults(1); |
|
| 93 | - |
|
| 94 | - try { |
|
| 95 | - $query->execute(); |
|
| 96 | - return true; |
|
| 97 | - } catch (InvalidFieldNameException $e) { |
|
| 98 | - return false; |
|
| 99 | - } |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * @param int $offset |
|
| 104 | - * @return int Number of copied users |
|
| 105 | - */ |
|
| 106 | - protected function runStep($offset) { |
|
| 107 | - $query = $this->db->getQueryBuilder(); |
|
| 108 | - $query->select('*') |
|
| 109 | - ->from('accounts') |
|
| 110 | - ->orderBy('id') |
|
| 111 | - ->setMaxResults(self::BATCH_SIZE); |
|
| 112 | - |
|
| 113 | - if ($offset > 0) { |
|
| 114 | - $query->setFirstResult($offset); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - $result = $query->execute(); |
|
| 118 | - |
|
| 119 | - $update = $this->db->getQueryBuilder(); |
|
| 120 | - $update->update('users') |
|
| 121 | - ->set('displayname', $update->createParameter('displayname')) |
|
| 122 | - ->where($update->expr()->eq('uid', $update->createParameter('userid'))); |
|
| 123 | - |
|
| 124 | - $updatedUsers = 0; |
|
| 125 | - while ($row = $result->fetch()) { |
|
| 126 | - try { |
|
| 127 | - $this->migrateUserInfo($update, $row); |
|
| 128 | - } catch (PreConditionNotMetException $e) { |
|
| 129 | - // Ignore and continue |
|
| 130 | - } catch (\UnexpectedValueException $e) { |
|
| 131 | - // Ignore and continue |
|
| 132 | - } |
|
| 133 | - $updatedUsers++; |
|
| 134 | - } |
|
| 135 | - $result->closeCursor(); |
|
| 136 | - |
|
| 137 | - return $updatedUsers; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * @param IQueryBuilder $update |
|
| 142 | - * @param array $userdata |
|
| 143 | - * @throws PreConditionNotMetException |
|
| 144 | - * @throws \UnexpectedValueException |
|
| 145 | - */ |
|
| 146 | - protected function migrateUserInfo(IQueryBuilder $update, $userdata) { |
|
| 147 | - if ($userdata['state'] === '3') { |
|
| 148 | - // Deleted user, ignore |
|
| 149 | - return; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - if ($userdata['email'] !== null) { |
|
| 153 | - $this->config->setUserValue($userdata['user_id'], 'settings', 'email', $userdata['email']); |
|
| 154 | - } |
|
| 155 | - if ($userdata['quota'] !== null) { |
|
| 156 | - $this->config->setUserValue($userdata['user_id'], 'files', 'quota', $userdata['quota']); |
|
| 157 | - } |
|
| 158 | - if ($userdata['last_login'] !== null) { |
|
| 159 | - $this->config->setUserValue($userdata['user_id'], 'login', 'lastLogin', $userdata['last_login']); |
|
| 160 | - } |
|
| 161 | - if ($userdata['state'] === '1') { |
|
| 162 | - $this->config->setUserValue($userdata['user_id'], 'core', 'enabled', 'true'); |
|
| 163 | - } |
|
| 164 | - if ($userdata['state'] === '2') { |
|
| 165 | - $this->config->setUserValue($userdata['user_id'], 'core', 'enabled', 'false'); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - if ($userdata['display_name'] !== null) { |
|
| 169 | - $update->setParameter('displayname', $userdata['display_name']) |
|
| 170 | - ->setParameter('userid', $userdata['user_id']); |
|
| 171 | - $update->execute(); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - } |
|
| 40 | + const BATCH_SIZE = 75; |
|
| 41 | + |
|
| 42 | + /** @var IDBConnection */ |
|
| 43 | + protected $db; |
|
| 44 | + |
|
| 45 | + /** @var IConfig */ |
|
| 46 | + protected $config; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @param IDBConnection $db |
|
| 50 | + * @param IConfig $config |
|
| 51 | + */ |
|
| 52 | + public function __construct(IDBConnection $db, IConfig $config) { |
|
| 53 | + $this->db = $db; |
|
| 54 | + $this->config = $config; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @return string |
|
| 59 | + */ |
|
| 60 | + public function getName() { |
|
| 61 | + return 'Copy data from accounts table when migrating from ownCloud'; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * @param IOutput $output |
|
| 66 | + */ |
|
| 67 | + public function run(IOutput $output) { |
|
| 68 | + if (!$this->shouldRun()) { |
|
| 69 | + return; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + $offset = 0; |
|
| 73 | + $numUsers = $this->runStep($offset); |
|
| 74 | + |
|
| 75 | + while ($numUsers === self::BATCH_SIZE) { |
|
| 76 | + $offset += $numUsers; |
|
| 77 | + $numUsers = $this->runStep($offset); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + // Remove the table |
|
| 81 | + $this->db->dropTable('accounts'); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * @return bool |
|
| 86 | + */ |
|
| 87 | + protected function shouldRun() { |
|
| 88 | + $query = $this->db->getQueryBuilder(); |
|
| 89 | + $query->select('*') |
|
| 90 | + ->from('accounts') |
|
| 91 | + ->where($query->expr()->isNotNull('user_id')) |
|
| 92 | + ->setMaxResults(1); |
|
| 93 | + |
|
| 94 | + try { |
|
| 95 | + $query->execute(); |
|
| 96 | + return true; |
|
| 97 | + } catch (InvalidFieldNameException $e) { |
|
| 98 | + return false; |
|
| 99 | + } |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * @param int $offset |
|
| 104 | + * @return int Number of copied users |
|
| 105 | + */ |
|
| 106 | + protected function runStep($offset) { |
|
| 107 | + $query = $this->db->getQueryBuilder(); |
|
| 108 | + $query->select('*') |
|
| 109 | + ->from('accounts') |
|
| 110 | + ->orderBy('id') |
|
| 111 | + ->setMaxResults(self::BATCH_SIZE); |
|
| 112 | + |
|
| 113 | + if ($offset > 0) { |
|
| 114 | + $query->setFirstResult($offset); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + $result = $query->execute(); |
|
| 118 | + |
|
| 119 | + $update = $this->db->getQueryBuilder(); |
|
| 120 | + $update->update('users') |
|
| 121 | + ->set('displayname', $update->createParameter('displayname')) |
|
| 122 | + ->where($update->expr()->eq('uid', $update->createParameter('userid'))); |
|
| 123 | + |
|
| 124 | + $updatedUsers = 0; |
|
| 125 | + while ($row = $result->fetch()) { |
|
| 126 | + try { |
|
| 127 | + $this->migrateUserInfo($update, $row); |
|
| 128 | + } catch (PreConditionNotMetException $e) { |
|
| 129 | + // Ignore and continue |
|
| 130 | + } catch (\UnexpectedValueException $e) { |
|
| 131 | + // Ignore and continue |
|
| 132 | + } |
|
| 133 | + $updatedUsers++; |
|
| 134 | + } |
|
| 135 | + $result->closeCursor(); |
|
| 136 | + |
|
| 137 | + return $updatedUsers; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * @param IQueryBuilder $update |
|
| 142 | + * @param array $userdata |
|
| 143 | + * @throws PreConditionNotMetException |
|
| 144 | + * @throws \UnexpectedValueException |
|
| 145 | + */ |
|
| 146 | + protected function migrateUserInfo(IQueryBuilder $update, $userdata) { |
|
| 147 | + if ($userdata['state'] === '3') { |
|
| 148 | + // Deleted user, ignore |
|
| 149 | + return; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + if ($userdata['email'] !== null) { |
|
| 153 | + $this->config->setUserValue($userdata['user_id'], 'settings', 'email', $userdata['email']); |
|
| 154 | + } |
|
| 155 | + if ($userdata['quota'] !== null) { |
|
| 156 | + $this->config->setUserValue($userdata['user_id'], 'files', 'quota', $userdata['quota']); |
|
| 157 | + } |
|
| 158 | + if ($userdata['last_login'] !== null) { |
|
| 159 | + $this->config->setUserValue($userdata['user_id'], 'login', 'lastLogin', $userdata['last_login']); |
|
| 160 | + } |
|
| 161 | + if ($userdata['state'] === '1') { |
|
| 162 | + $this->config->setUserValue($userdata['user_id'], 'core', 'enabled', 'true'); |
|
| 163 | + } |
|
| 164 | + if ($userdata['state'] === '2') { |
|
| 165 | + $this->config->setUserValue($userdata['user_id'], 'core', 'enabled', 'false'); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + if ($userdata['display_name'] !== null) { |
|
| 169 | + $update->setParameter('displayname', $userdata['display_name']) |
|
| 170 | + ->setParameter('userid', $userdata['user_id']); |
|
| 171 | + $update->execute(); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + } |
|
| 175 | 175 | } |
| 176 | 176 | |