Completed
Push — master ( f27131...1e7b45 )
by
unknown
32:55
created
lib/private/Repair/Owncloud/SaveAccountsTableData.php 1 patch
Indentation   +161 added lines, -161 removed lines patch added patch discarded remove patch
@@ -18,165 +18,165 @@
 block discarded – undo
18 18
  * before the data structure is changed and the information is gone
19 19
  */
20 20
 class SaveAccountsTableData implements IRepairStep {
21
-	public const BATCH_SIZE = 75;
22
-
23
-	/** @var IDBConnection */
24
-	protected $db;
25
-
26
-	/** @var IConfig */
27
-	protected $config;
28
-
29
-	protected $hasForeignKeyOnPersistentLocks = false;
30
-
31
-	/**
32
-	 * @param IDBConnection $db
33
-	 * @param IConfig $config
34
-	 */
35
-	public function __construct(IDBConnection $db, IConfig $config) {
36
-		$this->db = $db;
37
-		$this->config = $config;
38
-	}
39
-
40
-	/**
41
-	 * @return string
42
-	 */
43
-	public function getName() {
44
-		return 'Copy data from accounts table when migrating from ownCloud';
45
-	}
46
-
47
-	/**
48
-	 * @param IOutput $output
49
-	 */
50
-	public function run(IOutput $output) {
51
-		if (!$this->shouldRun()) {
52
-			return;
53
-		}
54
-
55
-		$offset = 0;
56
-		$numUsers = $this->runStep($offset);
57
-
58
-		while ($numUsers === self::BATCH_SIZE) {
59
-			$offset += $numUsers;
60
-			$numUsers = $this->runStep($offset);
61
-		}
62
-
63
-		// oc_persistent_locks will be removed later on anyways so we can just drop and ignore any foreign key constraints here
64
-		$tableName = $this->config->getSystemValueString('dbtableprefix', 'oc_') . 'persistent_locks';
65
-		$schema = $this->db->createSchema();
66
-		$table = $schema->getTable($tableName);
67
-		foreach ($table->getForeignKeys() as $foreignKey) {
68
-			$table->removeForeignKey($foreignKey->getName());
69
-		}
70
-		$this->db->migrateToSchema($schema);
71
-
72
-		// Remove the table
73
-		if ($this->hasForeignKeyOnPersistentLocks) {
74
-			$this->db->dropTable('persistent_locks');
75
-		}
76
-		$this->db->dropTable('accounts');
77
-	}
78
-
79
-	/**
80
-	 * @return bool
81
-	 */
82
-	protected function shouldRun() {
83
-		$schema = $this->db->createSchema();
84
-		$prefix = $this->config->getSystemValueString('dbtableprefix', 'oc_');
85
-
86
-		$tableName = $prefix . 'accounts';
87
-		if (!$schema->hasTable($tableName)) {
88
-			return false;
89
-		}
90
-
91
-		$table = $schema->getTable($tableName);
92
-		if (!$table->hasColumn('user_id')) {
93
-			return false;
94
-		}
95
-
96
-		if ($schema->hasTable($prefix . 'persistent_locks')) {
97
-			$locksTable = $schema->getTable($prefix . 'persistent_locks');
98
-			$foreignKeys = $locksTable->getForeignKeys();
99
-			foreach ($foreignKeys as $foreignKey) {
100
-				if ($tableName === $foreignKey->getForeignTableName()) {
101
-					$this->hasForeignKeyOnPersistentLocks = true;
102
-				}
103
-			}
104
-		}
105
-
106
-		return true;
107
-	}
108
-
109
-	/**
110
-	 * @param int $offset
111
-	 * @return int Number of copied users
112
-	 */
113
-	protected function runStep($offset) {
114
-		$query = $this->db->getQueryBuilder();
115
-		$query->select('*')
116
-			->from('accounts')
117
-			->orderBy('id')
118
-			->setMaxResults(self::BATCH_SIZE);
119
-
120
-		if ($offset > 0) {
121
-			$query->setFirstResult($offset);
122
-		}
123
-
124
-		$result = $query->executeQuery();
125
-
126
-		$update = $this->db->getQueryBuilder();
127
-		$update->update('users')
128
-			->set('displayname', $update->createParameter('displayname'))
129
-			->where($update->expr()->eq('uid', $update->createParameter('userid')));
130
-
131
-		$updatedUsers = 0;
132
-		while ($row = $result->fetch()) {
133
-			try {
134
-				$this->migrateUserInfo($update, $row);
135
-			} catch (PreConditionNotMetException $e) {
136
-				// Ignore and continue
137
-			} catch (\UnexpectedValueException $e) {
138
-				// Ignore and continue
139
-			}
140
-			$updatedUsers++;
141
-		}
142
-		$result->closeCursor();
143
-
144
-		return $updatedUsers;
145
-	}
146
-
147
-	/**
148
-	 * @param IQueryBuilder $update
149
-	 * @param array $userdata
150
-	 * @throws PreConditionNotMetException
151
-	 * @throws \UnexpectedValueException
152
-	 */
153
-	protected function migrateUserInfo(IQueryBuilder $update, $userdata) {
154
-		$state = (int)$userdata['state'];
155
-		if ($state === 3) {
156
-			// Deleted user, ignore
157
-			return;
158
-		}
159
-
160
-		if ($userdata['email'] !== null) {
161
-			$this->config->setUserValue($userdata['user_id'], 'settings', 'email', $userdata['email']);
162
-		}
163
-		if ($userdata['quota'] !== null) {
164
-			$this->config->setUserValue($userdata['user_id'], 'files', 'quota', $userdata['quota']);
165
-		}
166
-		if ($userdata['last_login'] !== null) {
167
-			$this->config->setUserValue($userdata['user_id'], 'login', 'lastLogin', $userdata['last_login']);
168
-		}
169
-		if ($state === 1) {
170
-			$this->config->setUserValue($userdata['user_id'], 'core', 'enabled', 'true');
171
-		} elseif ($state === 2) {
172
-			$this->config->setUserValue($userdata['user_id'], 'core', 'enabled', 'false');
173
-		}
174
-
175
-		if ($userdata['display_name'] !== null) {
176
-			// user.displayname only allows 64 characters but old accounts.display_name allowed 255 characters
177
-			$update->setParameter('displayname', mb_substr($userdata['display_name'], 0, 64))
178
-				->setParameter('userid', $userdata['user_id']);
179
-			$update->executeStatement();
180
-		}
181
-	}
21
+    public const BATCH_SIZE = 75;
22
+
23
+    /** @var IDBConnection */
24
+    protected $db;
25
+
26
+    /** @var IConfig */
27
+    protected $config;
28
+
29
+    protected $hasForeignKeyOnPersistentLocks = false;
30
+
31
+    /**
32
+     * @param IDBConnection $db
33
+     * @param IConfig $config
34
+     */
35
+    public function __construct(IDBConnection $db, IConfig $config) {
36
+        $this->db = $db;
37
+        $this->config = $config;
38
+    }
39
+
40
+    /**
41
+     * @return string
42
+     */
43
+    public function getName() {
44
+        return 'Copy data from accounts table when migrating from ownCloud';
45
+    }
46
+
47
+    /**
48
+     * @param IOutput $output
49
+     */
50
+    public function run(IOutput $output) {
51
+        if (!$this->shouldRun()) {
52
+            return;
53
+        }
54
+
55
+        $offset = 0;
56
+        $numUsers = $this->runStep($offset);
57
+
58
+        while ($numUsers === self::BATCH_SIZE) {
59
+            $offset += $numUsers;
60
+            $numUsers = $this->runStep($offset);
61
+        }
62
+
63
+        // oc_persistent_locks will be removed later on anyways so we can just drop and ignore any foreign key constraints here
64
+        $tableName = $this->config->getSystemValueString('dbtableprefix', 'oc_') . 'persistent_locks';
65
+        $schema = $this->db->createSchema();
66
+        $table = $schema->getTable($tableName);
67
+        foreach ($table->getForeignKeys() as $foreignKey) {
68
+            $table->removeForeignKey($foreignKey->getName());
69
+        }
70
+        $this->db->migrateToSchema($schema);
71
+
72
+        // Remove the table
73
+        if ($this->hasForeignKeyOnPersistentLocks) {
74
+            $this->db->dropTable('persistent_locks');
75
+        }
76
+        $this->db->dropTable('accounts');
77
+    }
78
+
79
+    /**
80
+     * @return bool
81
+     */
82
+    protected function shouldRun() {
83
+        $schema = $this->db->createSchema();
84
+        $prefix = $this->config->getSystemValueString('dbtableprefix', 'oc_');
85
+
86
+        $tableName = $prefix . 'accounts';
87
+        if (!$schema->hasTable($tableName)) {
88
+            return false;
89
+        }
90
+
91
+        $table = $schema->getTable($tableName);
92
+        if (!$table->hasColumn('user_id')) {
93
+            return false;
94
+        }
95
+
96
+        if ($schema->hasTable($prefix . 'persistent_locks')) {
97
+            $locksTable = $schema->getTable($prefix . 'persistent_locks');
98
+            $foreignKeys = $locksTable->getForeignKeys();
99
+            foreach ($foreignKeys as $foreignKey) {
100
+                if ($tableName === $foreignKey->getForeignTableName()) {
101
+                    $this->hasForeignKeyOnPersistentLocks = true;
102
+                }
103
+            }
104
+        }
105
+
106
+        return true;
107
+    }
108
+
109
+    /**
110
+     * @param int $offset
111
+     * @return int Number of copied users
112
+     */
113
+    protected function runStep($offset) {
114
+        $query = $this->db->getQueryBuilder();
115
+        $query->select('*')
116
+            ->from('accounts')
117
+            ->orderBy('id')
118
+            ->setMaxResults(self::BATCH_SIZE);
119
+
120
+        if ($offset > 0) {
121
+            $query->setFirstResult($offset);
122
+        }
123
+
124
+        $result = $query->executeQuery();
125
+
126
+        $update = $this->db->getQueryBuilder();
127
+        $update->update('users')
128
+            ->set('displayname', $update->createParameter('displayname'))
129
+            ->where($update->expr()->eq('uid', $update->createParameter('userid')));
130
+
131
+        $updatedUsers = 0;
132
+        while ($row = $result->fetch()) {
133
+            try {
134
+                $this->migrateUserInfo($update, $row);
135
+            } catch (PreConditionNotMetException $e) {
136
+                // Ignore and continue
137
+            } catch (\UnexpectedValueException $e) {
138
+                // Ignore and continue
139
+            }
140
+            $updatedUsers++;
141
+        }
142
+        $result->closeCursor();
143
+
144
+        return $updatedUsers;
145
+    }
146
+
147
+    /**
148
+     * @param IQueryBuilder $update
149
+     * @param array $userdata
150
+     * @throws PreConditionNotMetException
151
+     * @throws \UnexpectedValueException
152
+     */
153
+    protected function migrateUserInfo(IQueryBuilder $update, $userdata) {
154
+        $state = (int)$userdata['state'];
155
+        if ($state === 3) {
156
+            // Deleted user, ignore
157
+            return;
158
+        }
159
+
160
+        if ($userdata['email'] !== null) {
161
+            $this->config->setUserValue($userdata['user_id'], 'settings', 'email', $userdata['email']);
162
+        }
163
+        if ($userdata['quota'] !== null) {
164
+            $this->config->setUserValue($userdata['user_id'], 'files', 'quota', $userdata['quota']);
165
+        }
166
+        if ($userdata['last_login'] !== null) {
167
+            $this->config->setUserValue($userdata['user_id'], 'login', 'lastLogin', $userdata['last_login']);
168
+        }
169
+        if ($state === 1) {
170
+            $this->config->setUserValue($userdata['user_id'], 'core', 'enabled', 'true');
171
+        } elseif ($state === 2) {
172
+            $this->config->setUserValue($userdata['user_id'], 'core', 'enabled', 'false');
173
+        }
174
+
175
+        if ($userdata['display_name'] !== null) {
176
+            // user.displayname only allows 64 characters but old accounts.display_name allowed 255 characters
177
+            $update->setParameter('displayname', mb_substr($userdata['display_name'], 0, 64))
178
+                ->setParameter('userid', $userdata['user_id']);
179
+            $update->executeStatement();
180
+        }
181
+    }
182 182
 }
Please login to merge, or discard this patch.