@@ -36,157 +36,157 @@ |
||
| 36 | 36 | */ |
| 37 | 37 | class SaveAccountsTableData implements IRepairStep { |
| 38 | 38 | |
| 39 | - const BATCH_SIZE = 75; |
|
| 40 | - |
|
| 41 | - /** @var IDBConnection */ |
|
| 42 | - protected $db; |
|
| 43 | - |
|
| 44 | - /** @var IConfig */ |
|
| 45 | - protected $config; |
|
| 46 | - |
|
| 47 | - protected $hasForeignKeyOnPersistentLocks = false; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * @param IDBConnection $db |
|
| 51 | - * @param IConfig $config |
|
| 52 | - */ |
|
| 53 | - public function __construct(IDBConnection $db, IConfig $config) { |
|
| 54 | - $this->db = $db; |
|
| 55 | - $this->config = $config; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @return string |
|
| 60 | - */ |
|
| 61 | - public function getName() { |
|
| 62 | - return 'Copy data from accounts table when migrating from ownCloud'; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @param IOutput $output |
|
| 67 | - */ |
|
| 68 | - public function run(IOutput $output) { |
|
| 69 | - if (!$this->shouldRun()) { |
|
| 70 | - return; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - $offset = 0; |
|
| 74 | - $numUsers = $this->runStep($offset); |
|
| 75 | - |
|
| 76 | - while ($numUsers === self::BATCH_SIZE) { |
|
| 77 | - $offset += $numUsers; |
|
| 78 | - $numUsers = $this->runStep($offset); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - // Remove the table |
|
| 82 | - if ($this->hasForeignKeyOnPersistentLocks) { |
|
| 83 | - $this->db->dropTable('persistent_locks'); |
|
| 84 | - } |
|
| 85 | - $this->db->dropTable('accounts'); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @return bool |
|
| 90 | - */ |
|
| 91 | - protected function shouldRun() { |
|
| 92 | - $schema = $this->db->createSchema(); |
|
| 93 | - $prefix = $this->config->getSystemValue('dbtableprefix', 'oc_'); |
|
| 94 | - |
|
| 95 | - $tableName = $prefix . 'accounts'; |
|
| 96 | - if (!$schema->hasTable($tableName)) { |
|
| 97 | - return false; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - $table = $schema->getTable($tableName); |
|
| 101 | - if (!$table->hasColumn('user_id')) { |
|
| 102 | - return false; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - if ($schema->hasTable($prefix . 'persistent_locks')) { |
|
| 106 | - $locksTable = $schema->getTable($prefix . 'persistent_locks'); |
|
| 107 | - $foreignKeys = $locksTable->getForeignKeys(); |
|
| 108 | - foreach ($foreignKeys as $foreignKey) { |
|
| 109 | - if ($tableName === $foreignKey->getForeignTableName()) { |
|
| 110 | - $this->hasForeignKeyOnPersistentLocks = true; |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - return true; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * @param int $offset |
|
| 120 | - * @return int Number of copied users |
|
| 121 | - */ |
|
| 122 | - protected function runStep($offset) { |
|
| 123 | - $query = $this->db->getQueryBuilder(); |
|
| 124 | - $query->select('*') |
|
| 125 | - ->from('accounts') |
|
| 126 | - ->orderBy('id') |
|
| 127 | - ->setMaxResults(self::BATCH_SIZE); |
|
| 128 | - |
|
| 129 | - if ($offset > 0) { |
|
| 130 | - $query->setFirstResult($offset); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - $result = $query->execute(); |
|
| 134 | - |
|
| 135 | - $update = $this->db->getQueryBuilder(); |
|
| 136 | - $update->update('users') |
|
| 137 | - ->set('displayname', $update->createParameter('displayname')) |
|
| 138 | - ->where($update->expr()->eq('uid', $update->createParameter('userid'))); |
|
| 139 | - |
|
| 140 | - $updatedUsers = 0; |
|
| 141 | - while ($row = $result->fetch()) { |
|
| 142 | - try { |
|
| 143 | - $this->migrateUserInfo($update, $row); |
|
| 144 | - } catch (PreConditionNotMetException $e) { |
|
| 145 | - // Ignore and continue |
|
| 146 | - } catch (\UnexpectedValueException $e) { |
|
| 147 | - // Ignore and continue |
|
| 148 | - } |
|
| 149 | - $updatedUsers++; |
|
| 150 | - } |
|
| 151 | - $result->closeCursor(); |
|
| 152 | - |
|
| 153 | - return $updatedUsers; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * @param IQueryBuilder $update |
|
| 158 | - * @param array $userdata |
|
| 159 | - * @throws PreConditionNotMetException |
|
| 160 | - * @throws \UnexpectedValueException |
|
| 161 | - */ |
|
| 162 | - protected function migrateUserInfo(IQueryBuilder $update, $userdata) { |
|
| 163 | - $state = (int) $userdata['state']; |
|
| 164 | - if ($state === 3) { |
|
| 165 | - // Deleted user, ignore |
|
| 166 | - return; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - if ($userdata['email'] !== null) { |
|
| 170 | - $this->config->setUserValue($userdata['user_id'], 'settings', 'email', $userdata['email']); |
|
| 171 | - } |
|
| 172 | - if ($userdata['quota'] !== null) { |
|
| 173 | - $this->config->setUserValue($userdata['user_id'], 'files', 'quota', $userdata['quota']); |
|
| 174 | - } |
|
| 175 | - if ($userdata['last_login'] !== null) { |
|
| 176 | - $this->config->setUserValue($userdata['user_id'], 'login', 'lastLogin', $userdata['last_login']); |
|
| 177 | - } |
|
| 178 | - if ($state === 1) { |
|
| 179 | - $this->config->setUserValue($userdata['user_id'], 'core', 'enabled', 'true'); |
|
| 180 | - } else if ($state === 2) { |
|
| 181 | - $this->config->setUserValue($userdata['user_id'], 'core', 'enabled', 'false'); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - if ($userdata['display_name'] !== null) { |
|
| 185 | - $update->setParameter('displayname', $userdata['display_name']) |
|
| 186 | - ->setParameter('userid', $userdata['user_id']); |
|
| 187 | - $update->execute(); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - } |
|
| 39 | + const BATCH_SIZE = 75; |
|
| 40 | + |
|
| 41 | + /** @var IDBConnection */ |
|
| 42 | + protected $db; |
|
| 43 | + |
|
| 44 | + /** @var IConfig */ |
|
| 45 | + protected $config; |
|
| 46 | + |
|
| 47 | + protected $hasForeignKeyOnPersistentLocks = false; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * @param IDBConnection $db |
|
| 51 | + * @param IConfig $config |
|
| 52 | + */ |
|
| 53 | + public function __construct(IDBConnection $db, IConfig $config) { |
|
| 54 | + $this->db = $db; |
|
| 55 | + $this->config = $config; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @return string |
|
| 60 | + */ |
|
| 61 | + public function getName() { |
|
| 62 | + return 'Copy data from accounts table when migrating from ownCloud'; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @param IOutput $output |
|
| 67 | + */ |
|
| 68 | + public function run(IOutput $output) { |
|
| 69 | + if (!$this->shouldRun()) { |
|
| 70 | + return; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + $offset = 0; |
|
| 74 | + $numUsers = $this->runStep($offset); |
|
| 75 | + |
|
| 76 | + while ($numUsers === self::BATCH_SIZE) { |
|
| 77 | + $offset += $numUsers; |
|
| 78 | + $numUsers = $this->runStep($offset); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + // Remove the table |
|
| 82 | + if ($this->hasForeignKeyOnPersistentLocks) { |
|
| 83 | + $this->db->dropTable('persistent_locks'); |
|
| 84 | + } |
|
| 85 | + $this->db->dropTable('accounts'); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @return bool |
|
| 90 | + */ |
|
| 91 | + protected function shouldRun() { |
|
| 92 | + $schema = $this->db->createSchema(); |
|
| 93 | + $prefix = $this->config->getSystemValue('dbtableprefix', 'oc_'); |
|
| 94 | + |
|
| 95 | + $tableName = $prefix . 'accounts'; |
|
| 96 | + if (!$schema->hasTable($tableName)) { |
|
| 97 | + return false; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + $table = $schema->getTable($tableName); |
|
| 101 | + if (!$table->hasColumn('user_id')) { |
|
| 102 | + return false; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + if ($schema->hasTable($prefix . 'persistent_locks')) { |
|
| 106 | + $locksTable = $schema->getTable($prefix . 'persistent_locks'); |
|
| 107 | + $foreignKeys = $locksTable->getForeignKeys(); |
|
| 108 | + foreach ($foreignKeys as $foreignKey) { |
|
| 109 | + if ($tableName === $foreignKey->getForeignTableName()) { |
|
| 110 | + $this->hasForeignKeyOnPersistentLocks = true; |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + return true; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * @param int $offset |
|
| 120 | + * @return int Number of copied users |
|
| 121 | + */ |
|
| 122 | + protected function runStep($offset) { |
|
| 123 | + $query = $this->db->getQueryBuilder(); |
|
| 124 | + $query->select('*') |
|
| 125 | + ->from('accounts') |
|
| 126 | + ->orderBy('id') |
|
| 127 | + ->setMaxResults(self::BATCH_SIZE); |
|
| 128 | + |
|
| 129 | + if ($offset > 0) { |
|
| 130 | + $query->setFirstResult($offset); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + $result = $query->execute(); |
|
| 134 | + |
|
| 135 | + $update = $this->db->getQueryBuilder(); |
|
| 136 | + $update->update('users') |
|
| 137 | + ->set('displayname', $update->createParameter('displayname')) |
|
| 138 | + ->where($update->expr()->eq('uid', $update->createParameter('userid'))); |
|
| 139 | + |
|
| 140 | + $updatedUsers = 0; |
|
| 141 | + while ($row = $result->fetch()) { |
|
| 142 | + try { |
|
| 143 | + $this->migrateUserInfo($update, $row); |
|
| 144 | + } catch (PreConditionNotMetException $e) { |
|
| 145 | + // Ignore and continue |
|
| 146 | + } catch (\UnexpectedValueException $e) { |
|
| 147 | + // Ignore and continue |
|
| 148 | + } |
|
| 149 | + $updatedUsers++; |
|
| 150 | + } |
|
| 151 | + $result->closeCursor(); |
|
| 152 | + |
|
| 153 | + return $updatedUsers; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * @param IQueryBuilder $update |
|
| 158 | + * @param array $userdata |
|
| 159 | + * @throws PreConditionNotMetException |
|
| 160 | + * @throws \UnexpectedValueException |
|
| 161 | + */ |
|
| 162 | + protected function migrateUserInfo(IQueryBuilder $update, $userdata) { |
|
| 163 | + $state = (int) $userdata['state']; |
|
| 164 | + if ($state === 3) { |
|
| 165 | + // Deleted user, ignore |
|
| 166 | + return; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + if ($userdata['email'] !== null) { |
|
| 170 | + $this->config->setUserValue($userdata['user_id'], 'settings', 'email', $userdata['email']); |
|
| 171 | + } |
|
| 172 | + if ($userdata['quota'] !== null) { |
|
| 173 | + $this->config->setUserValue($userdata['user_id'], 'files', 'quota', $userdata['quota']); |
|
| 174 | + } |
|
| 175 | + if ($userdata['last_login'] !== null) { |
|
| 176 | + $this->config->setUserValue($userdata['user_id'], 'login', 'lastLogin', $userdata['last_login']); |
|
| 177 | + } |
|
| 178 | + if ($state === 1) { |
|
| 179 | + $this->config->setUserValue($userdata['user_id'], 'core', 'enabled', 'true'); |
|
| 180 | + } else if ($state === 2) { |
|
| 181 | + $this->config->setUserValue($userdata['user_id'], 'core', 'enabled', 'false'); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + if ($userdata['display_name'] !== null) { |
|
| 185 | + $update->setParameter('displayname', $userdata['display_name']) |
|
| 186 | + ->setParameter('userid', $userdata['user_id']); |
|
| 187 | + $update->execute(); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + } |
|
| 191 | 191 | } |
| 192 | 192 | |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | $schema = $this->db->createSchema(); |
| 93 | 93 | $prefix = $this->config->getSystemValue('dbtableprefix', 'oc_'); |
| 94 | 94 | |
| 95 | - $tableName = $prefix . 'accounts'; |
|
| 95 | + $tableName = $prefix.'accounts'; |
|
| 96 | 96 | if (!$schema->hasTable($tableName)) { |
| 97 | 97 | return false; |
| 98 | 98 | } |
@@ -102,8 +102,8 @@ discard block |
||
| 102 | 102 | return false; |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | - if ($schema->hasTable($prefix . 'persistent_locks')) { |
|
| 106 | - $locksTable = $schema->getTable($prefix . 'persistent_locks'); |
|
| 105 | + if ($schema->hasTable($prefix.'persistent_locks')) { |
|
| 106 | + $locksTable = $schema->getTable($prefix.'persistent_locks'); |
|
| 107 | 107 | $foreignKeys = $locksTable->getForeignKeys(); |
| 108 | 108 | foreach ($foreignKeys as $foreignKey) { |
| 109 | 109 | if ($tableName === $foreignKey->getForeignTableName()) { |