Completed
Pull Request — master (#6416)
by Joas
22:20 queued 08:23
created
lib/private/Repair/Owncloud/SaveAccountsTableData.php 2 patches
Indentation   +132 added lines, -132 removed lines patch added patch discarded remove patch
@@ -38,137 +38,137 @@
 block discarded – undo
38 38
  */
39 39
 class SaveAccountsTableData implements IRepairStep {
40 40
 
41
-	const BATCH_SIZE = 75;
42
-
43
-	/** @var IDBConnection */
44
-	protected $db;
45
-
46
-	/** @var IConfig */
47
-	protected $config;
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
-		$this->db->dropTable('accounts');
83
-	}
84
-
85
-	/**
86
-	 * @return bool
87
-	 */
88
-	protected function shouldRun() {
89
-		$schema = $this->db->createSchema();
90
-
91
-		$tableName = $this->config->getSystemValue('dbtableprefix', 'oc_') . 'accounts';
92
-		if (!$schema->hasTable($tableName)) {
93
-			return false;
94
-		}
95
-
96
-		$table = $schema->getTable($tableName);
97
-		return $table->hasColumn('user_id');
98
-	}
99
-
100
-	/**
101
-	 * @param int $offset
102
-	 * @return int Number of copied users
103
-	 */
104
-	protected function runStep($offset) {
105
-		$query = $this->db->getQueryBuilder();
106
-		$query->select('*')
107
-			->from('accounts')
108
-			->orderBy('id')
109
-			->setMaxResults(self::BATCH_SIZE);
110
-
111
-		if ($offset > 0) {
112
-			$query->setFirstResult($offset);
113
-		}
114
-
115
-		$result = $query->execute();
116
-
117
-		$update = $this->db->getQueryBuilder();
118
-		$update->update('users')
119
-			->set('displayname', $update->createParameter('displayname'))
120
-			->where($update->expr()->eq('uid', $update->createParameter('userid')));
121
-
122
-		$updatedUsers = 0;
123
-		while ($row = $result->fetch()) {
124
-			try {
125
-				$this->migrateUserInfo($update, $row);
126
-			} catch (PreConditionNotMetException $e) {
127
-				// Ignore and continue
128
-			} catch (\UnexpectedValueException $e) {
129
-				// Ignore and continue
130
-			}
131
-			$updatedUsers++;
132
-		}
133
-		$result->closeCursor();
134
-
135
-		return $updatedUsers;
136
-	}
137
-
138
-	/**
139
-	 * @param IQueryBuilder $update
140
-	 * @param array $userdata
141
-	 * @throws PreConditionNotMetException
142
-	 * @throws \UnexpectedValueException
143
-	 */
144
-	protected function migrateUserInfo(IQueryBuilder $update, $userdata) {
145
-		$state = (int) $userdata['state'];
146
-		if ($state === 3) {
147
-			// Deleted user, ignore
148
-			return;
149
-		}
150
-
151
-		if ($userdata['email'] !== null) {
152
-			$this->config->setUserValue($userdata['user_id'], 'settings', 'email', $userdata['email']);
153
-		}
154
-		if ($userdata['quota'] !== null) {
155
-			$this->config->setUserValue($userdata['user_id'], 'files', 'quota', $userdata['quota']);
156
-		}
157
-		if ($userdata['last_login'] !== null) {
158
-			$this->config->setUserValue($userdata['user_id'], 'login', 'lastLogin', $userdata['last_login']);
159
-		}
160
-		if ($state === 1) {
161
-			$this->config->setUserValue($userdata['user_id'], 'core', 'enabled', 'true');
162
-		} else if ($state === 2) {
163
-			$this->config->setUserValue($userdata['user_id'], 'core', 'enabled', 'false');
164
-		}
165
-
166
-		if ($userdata['display_name'] !== null) {
167
-			$update->setParameter('displayname', $userdata['display_name'])
168
-				->setParameter('userid', $userdata['user_id']);
169
-			$update->execute();
170
-		}
171
-
172
-	}
41
+    const BATCH_SIZE = 75;
42
+
43
+    /** @var IDBConnection */
44
+    protected $db;
45
+
46
+    /** @var IConfig */
47
+    protected $config;
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
+        $this->db->dropTable('accounts');
83
+    }
84
+
85
+    /**
86
+     * @return bool
87
+     */
88
+    protected function shouldRun() {
89
+        $schema = $this->db->createSchema();
90
+
91
+        $tableName = $this->config->getSystemValue('dbtableprefix', 'oc_') . 'accounts';
92
+        if (!$schema->hasTable($tableName)) {
93
+            return false;
94
+        }
95
+
96
+        $table = $schema->getTable($tableName);
97
+        return $table->hasColumn('user_id');
98
+    }
99
+
100
+    /**
101
+     * @param int $offset
102
+     * @return int Number of copied users
103
+     */
104
+    protected function runStep($offset) {
105
+        $query = $this->db->getQueryBuilder();
106
+        $query->select('*')
107
+            ->from('accounts')
108
+            ->orderBy('id')
109
+            ->setMaxResults(self::BATCH_SIZE);
110
+
111
+        if ($offset > 0) {
112
+            $query->setFirstResult($offset);
113
+        }
114
+
115
+        $result = $query->execute();
116
+
117
+        $update = $this->db->getQueryBuilder();
118
+        $update->update('users')
119
+            ->set('displayname', $update->createParameter('displayname'))
120
+            ->where($update->expr()->eq('uid', $update->createParameter('userid')));
121
+
122
+        $updatedUsers = 0;
123
+        while ($row = $result->fetch()) {
124
+            try {
125
+                $this->migrateUserInfo($update, $row);
126
+            } catch (PreConditionNotMetException $e) {
127
+                // Ignore and continue
128
+            } catch (\UnexpectedValueException $e) {
129
+                // Ignore and continue
130
+            }
131
+            $updatedUsers++;
132
+        }
133
+        $result->closeCursor();
134
+
135
+        return $updatedUsers;
136
+    }
137
+
138
+    /**
139
+     * @param IQueryBuilder $update
140
+     * @param array $userdata
141
+     * @throws PreConditionNotMetException
142
+     * @throws \UnexpectedValueException
143
+     */
144
+    protected function migrateUserInfo(IQueryBuilder $update, $userdata) {
145
+        $state = (int) $userdata['state'];
146
+        if ($state === 3) {
147
+            // Deleted user, ignore
148
+            return;
149
+        }
150
+
151
+        if ($userdata['email'] !== null) {
152
+            $this->config->setUserValue($userdata['user_id'], 'settings', 'email', $userdata['email']);
153
+        }
154
+        if ($userdata['quota'] !== null) {
155
+            $this->config->setUserValue($userdata['user_id'], 'files', 'quota', $userdata['quota']);
156
+        }
157
+        if ($userdata['last_login'] !== null) {
158
+            $this->config->setUserValue($userdata['user_id'], 'login', 'lastLogin', $userdata['last_login']);
159
+        }
160
+        if ($state === 1) {
161
+            $this->config->setUserValue($userdata['user_id'], 'core', 'enabled', 'true');
162
+        } else if ($state === 2) {
163
+            $this->config->setUserValue($userdata['user_id'], 'core', 'enabled', 'false');
164
+        }
165
+
166
+        if ($userdata['display_name'] !== null) {
167
+            $update->setParameter('displayname', $userdata['display_name'])
168
+                ->setParameter('userid', $userdata['user_id']);
169
+            $update->execute();
170
+        }
171
+
172
+    }
173 173
 }
174 174
 
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -88,7 +88,7 @@
 block discarded – undo
88 88
 	protected function shouldRun() {
89 89
 		$schema = $this->db->createSchema();
90 90
 
91
-		$tableName = $this->config->getSystemValue('dbtableprefix', 'oc_') . 'accounts';
91
+		$tableName = $this->config->getSystemValue('dbtableprefix', 'oc_').'accounts';
92 92
 		if (!$schema->hasTable($tableName)) {
93 93
 			return false;
94 94
 		}
Please login to merge, or discard this patch.