Completed
Pull Request — master (#5770)
by Robin
17:50
created
lib/private/Repair/NC13/RepairInvalidPaths.php 2 patches
Doc Comments   +6 added lines patch added patch discarded remove patch
@@ -79,6 +79,9 @@  discard block
 block discarded – undo
79 79
 		} while (count($rows) > 0);
80 80
 	}
81 81
 
82
+	/**
83
+	 * @param string $path
84
+	 */
82 85
 	private function getId($storage, $path) {
83 86
 		if (!$this->getIdQuery) {
84 87
 			$builder = $this->connection->getQueryBuilder();
@@ -95,6 +98,9 @@  discard block
 block discarded – undo
95 98
 		return $this->getIdQuery->execute()->fetchColumn();
96 99
 	}
97 100
 
101
+	/**
102
+	 * @param string $newPath
103
+	 */
98 104
 	private function update($fileid, $newPath) {
99 105
 		if (!$this->updateQuery) {
100 106
 			$builder = $this->connection->getQueryBuilder();
Please login to merge, or discard this patch.
Indentation   +138 added lines, -138 removed lines patch added patch discarded remove patch
@@ -29,142 +29,142 @@
 block discarded – undo
29 29
 use OCP\Migration\IRepairStep;
30 30
 
31 31
 class RepairInvalidPaths implements IRepairStep {
32
-	const MAX_ROWS = 1000;
33
-
34
-	/** @var IDBConnection */
35
-	private $connection;
36
-	/** @var IConfig */
37
-	private $config;
38
-
39
-	private $getIdQuery;
40
-	private $updateQuery;
41
-	private $reparentQuery;
42
-	private $deleteQuery;
43
-
44
-	public function __construct(IDBConnection $connection, IConfig $config) {
45
-		$this->connection = $connection;
46
-		$this->config = $config;
47
-	}
48
-
49
-
50
-	public function getName() {
51
-		return 'Repair invalid paths in file cache';
52
-	}
53
-
54
-	private function getInvalidEntries() {
55
-		$builder = $this->connection->getQueryBuilder();
56
-
57
-		$computedPath = $builder->func()->concat(
58
-			'p.path',
59
-			$builder->func()->concat($builder->createNamedParameter('/'), 'f.name')
60
-		);
61
-
62
-		//select f.path, f.parent,p.path from oc_filecache f inner join oc_filecache p on f.parent=p.fileid and p.path!='' where f.path != p.path || '/' || f.name;
63
-		$query = $builder->select('f.fileid', 'f.path', 'p.path AS parent_path', 'f.name', 'f.parent', 'f.storage')
64
-			->from('filecache', 'f')
65
-			->innerJoin('f', 'filecache', 'p', $builder->expr()->andX(
66
-				$builder->expr()->eq('f.parent', 'p.fileid'),
67
-				$builder->expr()->neq('p.name', $builder->createNamedParameter(''))
68
-			))
69
-			->where($builder->expr()->neq('f.path', $computedPath))
70
-			->setMaxResults(self::MAX_ROWS);
71
-
72
-		do {
73
-			$result = $query->execute();
74
-			$rows = $result->fetchAll();
75
-			foreach ($rows as $row) {
76
-				yield $row;
77
-			}
78
-			$result->closeCursor();
79
-		} while (count($rows) > 0);
80
-	}
81
-
82
-	private function getId($storage, $path) {
83
-		if (!$this->getIdQuery) {
84
-			$builder = $this->connection->getQueryBuilder();
85
-
86
-			$this->getIdQuery = $builder->select('fileid')
87
-				->from('filecache')
88
-				->where($builder->expr()->eq('storage', $builder->createParameter('storage')))
89
-				->andWhere($builder->expr()->eq('path', $builder->createParameter('path')));
90
-		}
91
-
92
-		$this->getIdQuery->setParameter('storage', $storage, IQueryBuilder::PARAM_INT);
93
-		$this->getIdQuery->setParameter('path', $path);
94
-
95
-		return $this->getIdQuery->execute()->fetchColumn();
96
-	}
97
-
98
-	private function update($fileid, $newPath) {
99
-		if (!$this->updateQuery) {
100
-			$builder = $this->connection->getQueryBuilder();
101
-
102
-			$this->updateQuery = $builder->update('filecache')
103
-				->set('path', $builder->createParameter('newpath'))
104
-				->set('path_hash', $builder->func()->md5($builder->createParameter('newpath')))
105
-				->where($builder->expr()->eq('fileid', $builder->createParameter('fileid')));
106
-		}
107
-
108
-		$this->updateQuery->setParameter('newpath', $newPath);
109
-		$this->updateQuery->setParameter('fileid', $fileid, IQueryBuilder::PARAM_INT);
110
-
111
-		$this->updateQuery->execute();
112
-	}
113
-
114
-	private function reparent($from, $to) {
115
-		if (!$this->reparentQuery) {
116
-			$builder = $this->connection->getQueryBuilder();
117
-
118
-			$this->reparentQuery = $builder->update('filecache')
119
-				->set('parent', $builder->createParameter('to'))
120
-				->where($builder->expr()->eq('fileid', $builder->createParameter('from')));
121
-		}
122
-
123
-		$this->reparentQuery->setParameter('from', $from);
124
-		$this->reparentQuery->setParameter('to', $to);
125
-
126
-		$this->reparentQuery->execute();
127
-	}
128
-
129
-	private function delete($fileid) {
130
-		if (!$this->deleteQuery) {
131
-			$builder = $this->connection->getQueryBuilder();
132
-
133
-			$this->deleteQuery = $builder->delete('filecache')
134
-				->where($builder->expr()->eq('fileid', $builder->createParameter('fileid')));
135
-		}
136
-
137
-		$this->deleteQuery->setParameter('fileid', $fileid, IQueryBuilder::PARAM_INT);
138
-
139
-		$this->deleteQuery->execute();
140
-	}
141
-
142
-	private function repair() {
143
-		$this->connection->beginTransaction();
144
-		$entries = $this->getInvalidEntries();
145
-		$count = 0;
146
-		foreach ($entries as $entry) {
147
-			$count++;
148
-			$calculatedPath = $entry['parent_path'] . '/' . $entry['name'];
149
-			if ($newId = $this->getId($entry['storage'], $calculatedPath)) {
150
-				// a new entry with the correct path has already been created, reuse that one and delete the incorrect entry
151
-				$this->reparent($entry['fileid'], $newId);
152
-				$this->delete($entry['fileid']);
153
-			} else {
154
-				$this->update($entry['fileid'], $calculatedPath);
155
-			}
156
-		}
157
-		$this->connection->commit();
158
-		return $count;
159
-	}
160
-
161
-	public function run(IOutput $output) {
162
-		$versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0');
163
-		// was added to 12.0.0.30 and 13.0.0.1
164
-		if (version_compare($versionFromBeforeUpdate, '12.0.0.30', '<') || version_compare($versionFromBeforeUpdate, '13.0.0.0', '==')) {
165
-			$count = $this->repair();
166
-
167
-			$output->info('Repaired ' . $count . ' paths');
168
-		}
169
-	}
32
+    const MAX_ROWS = 1000;
33
+
34
+    /** @var IDBConnection */
35
+    private $connection;
36
+    /** @var IConfig */
37
+    private $config;
38
+
39
+    private $getIdQuery;
40
+    private $updateQuery;
41
+    private $reparentQuery;
42
+    private $deleteQuery;
43
+
44
+    public function __construct(IDBConnection $connection, IConfig $config) {
45
+        $this->connection = $connection;
46
+        $this->config = $config;
47
+    }
48
+
49
+
50
+    public function getName() {
51
+        return 'Repair invalid paths in file cache';
52
+    }
53
+
54
+    private function getInvalidEntries() {
55
+        $builder = $this->connection->getQueryBuilder();
56
+
57
+        $computedPath = $builder->func()->concat(
58
+            'p.path',
59
+            $builder->func()->concat($builder->createNamedParameter('/'), 'f.name')
60
+        );
61
+
62
+        //select f.path, f.parent,p.path from oc_filecache f inner join oc_filecache p on f.parent=p.fileid and p.path!='' where f.path != p.path || '/' || f.name;
63
+        $query = $builder->select('f.fileid', 'f.path', 'p.path AS parent_path', 'f.name', 'f.parent', 'f.storage')
64
+            ->from('filecache', 'f')
65
+            ->innerJoin('f', 'filecache', 'p', $builder->expr()->andX(
66
+                $builder->expr()->eq('f.parent', 'p.fileid'),
67
+                $builder->expr()->neq('p.name', $builder->createNamedParameter(''))
68
+            ))
69
+            ->where($builder->expr()->neq('f.path', $computedPath))
70
+            ->setMaxResults(self::MAX_ROWS);
71
+
72
+        do {
73
+            $result = $query->execute();
74
+            $rows = $result->fetchAll();
75
+            foreach ($rows as $row) {
76
+                yield $row;
77
+            }
78
+            $result->closeCursor();
79
+        } while (count($rows) > 0);
80
+    }
81
+
82
+    private function getId($storage, $path) {
83
+        if (!$this->getIdQuery) {
84
+            $builder = $this->connection->getQueryBuilder();
85
+
86
+            $this->getIdQuery = $builder->select('fileid')
87
+                ->from('filecache')
88
+                ->where($builder->expr()->eq('storage', $builder->createParameter('storage')))
89
+                ->andWhere($builder->expr()->eq('path', $builder->createParameter('path')));
90
+        }
91
+
92
+        $this->getIdQuery->setParameter('storage', $storage, IQueryBuilder::PARAM_INT);
93
+        $this->getIdQuery->setParameter('path', $path);
94
+
95
+        return $this->getIdQuery->execute()->fetchColumn();
96
+    }
97
+
98
+    private function update($fileid, $newPath) {
99
+        if (!$this->updateQuery) {
100
+            $builder = $this->connection->getQueryBuilder();
101
+
102
+            $this->updateQuery = $builder->update('filecache')
103
+                ->set('path', $builder->createParameter('newpath'))
104
+                ->set('path_hash', $builder->func()->md5($builder->createParameter('newpath')))
105
+                ->where($builder->expr()->eq('fileid', $builder->createParameter('fileid')));
106
+        }
107
+
108
+        $this->updateQuery->setParameter('newpath', $newPath);
109
+        $this->updateQuery->setParameter('fileid', $fileid, IQueryBuilder::PARAM_INT);
110
+
111
+        $this->updateQuery->execute();
112
+    }
113
+
114
+    private function reparent($from, $to) {
115
+        if (!$this->reparentQuery) {
116
+            $builder = $this->connection->getQueryBuilder();
117
+
118
+            $this->reparentQuery = $builder->update('filecache')
119
+                ->set('parent', $builder->createParameter('to'))
120
+                ->where($builder->expr()->eq('fileid', $builder->createParameter('from')));
121
+        }
122
+
123
+        $this->reparentQuery->setParameter('from', $from);
124
+        $this->reparentQuery->setParameter('to', $to);
125
+
126
+        $this->reparentQuery->execute();
127
+    }
128
+
129
+    private function delete($fileid) {
130
+        if (!$this->deleteQuery) {
131
+            $builder = $this->connection->getQueryBuilder();
132
+
133
+            $this->deleteQuery = $builder->delete('filecache')
134
+                ->where($builder->expr()->eq('fileid', $builder->createParameter('fileid')));
135
+        }
136
+
137
+        $this->deleteQuery->setParameter('fileid', $fileid, IQueryBuilder::PARAM_INT);
138
+
139
+        $this->deleteQuery->execute();
140
+    }
141
+
142
+    private function repair() {
143
+        $this->connection->beginTransaction();
144
+        $entries = $this->getInvalidEntries();
145
+        $count = 0;
146
+        foreach ($entries as $entry) {
147
+            $count++;
148
+            $calculatedPath = $entry['parent_path'] . '/' . $entry['name'];
149
+            if ($newId = $this->getId($entry['storage'], $calculatedPath)) {
150
+                // a new entry with the correct path has already been created, reuse that one and delete the incorrect entry
151
+                $this->reparent($entry['fileid'], $newId);
152
+                $this->delete($entry['fileid']);
153
+            } else {
154
+                $this->update($entry['fileid'], $calculatedPath);
155
+            }
156
+        }
157
+        $this->connection->commit();
158
+        return $count;
159
+    }
160
+
161
+    public function run(IOutput $output) {
162
+        $versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0');
163
+        // was added to 12.0.0.30 and 13.0.0.1
164
+        if (version_compare($versionFromBeforeUpdate, '12.0.0.30', '<') || version_compare($versionFromBeforeUpdate, '13.0.0.0', '==')) {
165
+            $count = $this->repair();
166
+
167
+            $output->info('Repaired ' . $count . ' paths');
168
+        }
169
+    }
170 170
 }
Please login to merge, or discard this patch.