Completed
Pull Request — master (#5695)
by Joas
18:31
created
lib/private/Repair/Owncloud/SaveAccountsTableData.php 1 patch
Indentation   +137 added lines, -137 removed lines patch added patch discarded remove patch
@@ -38,142 +38,142 @@
 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
-		$query = $this->db->getQueryBuilder();
90
-		$query->select('*')
91
-			->from('accounts')
92
-			->where($query->expr()->isNotNull('user_id'))
93
-			->setMaxResults(1);
94
-
95
-		try {
96
-			$query->execute();
97
-			return true;
98
-		} catch (InvalidFieldNameException $e) {
99
-			return false;
100
-		} catch (TableNotFoundException $e) {
101
-			return false;
102
-		}
103
-	}
104
-
105
-	/**
106
-	 * @param int $offset
107
-	 * @return int Number of copied users
108
-	 */
109
-	protected function runStep($offset) {
110
-		$query = $this->db->getQueryBuilder();
111
-		$query->select('*')
112
-			->from('accounts')
113
-			->orderBy('id')
114
-			->setMaxResults(self::BATCH_SIZE);
115
-
116
-		if ($offset > 0) {
117
-			$query->setFirstResult($offset);
118
-		}
119
-
120
-		$result = $query->execute();
121
-
122
-		$update = $this->db->getQueryBuilder();
123
-		$update->update('users')
124
-			->set('displayname', $update->createParameter('displayname'))
125
-			->where($update->expr()->eq('uid', $update->createParameter('userid')));
126
-
127
-		$updatedUsers = 0;
128
-		while ($row = $result->fetch()) {
129
-			try {
130
-				$this->migrateUserInfo($update, $row);
131
-			} catch (PreConditionNotMetException $e) {
132
-				// Ignore and continue
133
-			} catch (\UnexpectedValueException $e) {
134
-				// Ignore and continue
135
-			}
136
-			$updatedUsers++;
137
-		}
138
-		$result->closeCursor();
139
-
140
-		return $updatedUsers;
141
-	}
142
-
143
-	/**
144
-	 * @param IQueryBuilder $update
145
-	 * @param array $userdata
146
-	 * @throws PreConditionNotMetException
147
-	 * @throws \UnexpectedValueException
148
-	 */
149
-	protected function migrateUserInfo(IQueryBuilder $update, $userdata) {
150
-		$state = (int) $userdata['state'];
151
-		if ($state === 3) {
152
-			// Deleted user, ignore
153
-			return;
154
-		}
155
-
156
-		if ($userdata['email'] !== null) {
157
-			$this->config->setUserValue($userdata['user_id'], 'settings', 'email', $userdata['email']);
158
-		}
159
-		if ($userdata['quota'] !== null) {
160
-			$this->config->setUserValue($userdata['user_id'], 'files', 'quota', $userdata['quota']);
161
-		}
162
-		if ($userdata['last_login'] !== null) {
163
-			$this->config->setUserValue($userdata['user_id'], 'login', 'lastLogin', $userdata['last_login']);
164
-		}
165
-		if ($state === 1) {
166
-			$this->config->setUserValue($userdata['user_id'], 'core', 'enabled', 'true');
167
-		} else if ($state === 2) {
168
-			$this->config->setUserValue($userdata['user_id'], 'core', 'enabled', 'false');
169
-		}
170
-
171
-		if ($userdata['display_name'] !== null) {
172
-			$update->setParameter('displayname', $userdata['display_name'])
173
-				->setParameter('userid', $userdata['user_id']);
174
-			$update->execute();
175
-		}
176
-
177
-	}
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
+        $query = $this->db->getQueryBuilder();
90
+        $query->select('*')
91
+            ->from('accounts')
92
+            ->where($query->expr()->isNotNull('user_id'))
93
+            ->setMaxResults(1);
94
+
95
+        try {
96
+            $query->execute();
97
+            return true;
98
+        } catch (InvalidFieldNameException $e) {
99
+            return false;
100
+        } catch (TableNotFoundException $e) {
101
+            return false;
102
+        }
103
+    }
104
+
105
+    /**
106
+     * @param int $offset
107
+     * @return int Number of copied users
108
+     */
109
+    protected function runStep($offset) {
110
+        $query = $this->db->getQueryBuilder();
111
+        $query->select('*')
112
+            ->from('accounts')
113
+            ->orderBy('id')
114
+            ->setMaxResults(self::BATCH_SIZE);
115
+
116
+        if ($offset > 0) {
117
+            $query->setFirstResult($offset);
118
+        }
119
+
120
+        $result = $query->execute();
121
+
122
+        $update = $this->db->getQueryBuilder();
123
+        $update->update('users')
124
+            ->set('displayname', $update->createParameter('displayname'))
125
+            ->where($update->expr()->eq('uid', $update->createParameter('userid')));
126
+
127
+        $updatedUsers = 0;
128
+        while ($row = $result->fetch()) {
129
+            try {
130
+                $this->migrateUserInfo($update, $row);
131
+            } catch (PreConditionNotMetException $e) {
132
+                // Ignore and continue
133
+            } catch (\UnexpectedValueException $e) {
134
+                // Ignore and continue
135
+            }
136
+            $updatedUsers++;
137
+        }
138
+        $result->closeCursor();
139
+
140
+        return $updatedUsers;
141
+    }
142
+
143
+    /**
144
+     * @param IQueryBuilder $update
145
+     * @param array $userdata
146
+     * @throws PreConditionNotMetException
147
+     * @throws \UnexpectedValueException
148
+     */
149
+    protected function migrateUserInfo(IQueryBuilder $update, $userdata) {
150
+        $state = (int) $userdata['state'];
151
+        if ($state === 3) {
152
+            // Deleted user, ignore
153
+            return;
154
+        }
155
+
156
+        if ($userdata['email'] !== null) {
157
+            $this->config->setUserValue($userdata['user_id'], 'settings', 'email', $userdata['email']);
158
+        }
159
+        if ($userdata['quota'] !== null) {
160
+            $this->config->setUserValue($userdata['user_id'], 'files', 'quota', $userdata['quota']);
161
+        }
162
+        if ($userdata['last_login'] !== null) {
163
+            $this->config->setUserValue($userdata['user_id'], 'login', 'lastLogin', $userdata['last_login']);
164
+        }
165
+        if ($state === 1) {
166
+            $this->config->setUserValue($userdata['user_id'], 'core', 'enabled', 'true');
167
+        } else if ($state === 2) {
168
+            $this->config->setUserValue($userdata['user_id'], 'core', 'enabled', 'false');
169
+        }
170
+
171
+        if ($userdata['display_name'] !== null) {
172
+            $update->setParameter('displayname', $userdata['display_name'])
173
+                ->setParameter('userid', $userdata['user_id']);
174
+            $update->execute();
175
+        }
176
+
177
+    }
178 178
 }
179 179
 
Please login to merge, or discard this patch.