Completed
Push — master ( e73989...0274a4 )
by Daniel
28:31
created
apps/files/lib/Command/RepairTree.php 1 patch
Indentation   +96 added lines, -96 removed lines patch added patch discarded remove patch
@@ -14,100 +14,100 @@
 block discarded – undo
14 14
 use Symfony\Component\Console\Output\OutputInterface;
15 15
 
16 16
 class RepairTree extends Command {
17
-	public const CHUNK_SIZE = 200;
18
-
19
-	public function __construct(
20
-		protected IDBConnection $connection,
21
-	) {
22
-		parent::__construct();
23
-	}
24
-
25
-	protected function configure(): void {
26
-		$this
27
-			->setName('files:repair-tree')
28
-			->setDescription('Try and repair malformed filesystem tree structures (may be necessary to run multiple times for nested malformations)')
29
-			->addOption('dry-run');
30
-	}
31
-
32
-	public function execute(InputInterface $input, OutputInterface $output): int {
33
-		$rows = $this->findBrokenTreeBits();
34
-		$fix = !$input->getOption('dry-run');
35
-
36
-		$output->writeln('Found ' . count($rows) . ' file entries with an invalid path');
37
-
38
-		if ($fix) {
39
-			$this->connection->beginTransaction();
40
-		}
41
-
42
-		$query = $this->connection->getQueryBuilder();
43
-		$query->update('filecache')
44
-			->set('path', $query->createParameter('path'))
45
-			->set('path_hash', $query->func()->md5($query->createParameter('path')))
46
-			->set('storage', $query->createParameter('storage'))
47
-			->where($query->expr()->eq('fileid', $query->createParameter('fileid')));
48
-
49
-		foreach ($rows as $row) {
50
-			$output->writeln("Path of file {$row['fileid']} is {$row['path']} but should be {$row['parent_path']}/{$row['name']} based on its parent", OutputInterface::VERBOSITY_VERBOSE);
51
-
52
-			if ($fix) {
53
-				$fileId = $this->getFileId((int)$row['parent_storage'], $row['parent_path'] . '/' . $row['name']);
54
-				if ($fileId > 0) {
55
-					$output->writeln("Cache entry has already be recreated with id $fileId, deleting instead");
56
-					$this->deleteById((int)$row['fileid']);
57
-				} else {
58
-					$query->setParameters([
59
-						'fileid' => $row['fileid'],
60
-						'path' => $row['parent_path'] . '/' . $row['name'],
61
-						'storage' => $row['parent_storage'],
62
-					]);
63
-					$query->executeStatement();
64
-				}
65
-			}
66
-		}
67
-
68
-		if ($fix) {
69
-			$this->connection->commit();
70
-		}
71
-
72
-		return self::SUCCESS;
73
-	}
74
-
75
-	private function getFileId(int $storage, string $path) {
76
-		$query = $this->connection->getQueryBuilder();
77
-		$query->select('fileid')
78
-			->from('filecache')
79
-			->where($query->expr()->eq('storage', $query->createNamedParameter($storage)))
80
-			->andWhere($query->expr()->eq('path_hash', $query->createNamedParameter(md5($path))));
81
-		return $query->executeQuery()->fetch(\PDO::FETCH_COLUMN);
82
-	}
83
-
84
-	private function deleteById(int $fileId): void {
85
-		$query = $this->connection->getQueryBuilder();
86
-		$query->delete('filecache')
87
-			->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId)));
88
-		$query->executeStatement();
89
-	}
90
-
91
-	private function findBrokenTreeBits(): array {
92
-		$query = $this->connection->getQueryBuilder();
93
-
94
-		$query->select('f.fileid', 'f.path', 'f.parent', 'f.name')
95
-			->selectAlias('p.path', 'parent_path')
96
-			->selectAlias('p.storage', 'parent_storage')
97
-			->from('filecache', 'f')
98
-			->innerJoin('f', 'filecache', 'p', $query->expr()->eq('f.parent', 'p.fileid'))
99
-			->where($query->expr()->orX(
100
-				$query->expr()->andX(
101
-					$query->expr()->neq('p.path_hash', $query->createNamedParameter(md5(''))),
102
-					$query->expr()->neq('f.path', $query->func()->concat('p.path', $query->func()->concat($query->createNamedParameter('/'), 'f.name')))
103
-				),
104
-				$query->expr()->andX(
105
-					$query->expr()->eq('p.path_hash', $query->createNamedParameter(md5(''))),
106
-					$query->expr()->neq('f.path', 'f.name')
107
-				),
108
-				$query->expr()->neq('f.storage', 'p.storage')
109
-			));
110
-
111
-		return $query->executeQuery()->fetchAll();
112
-	}
17
+    public const CHUNK_SIZE = 200;
18
+
19
+    public function __construct(
20
+        protected IDBConnection $connection,
21
+    ) {
22
+        parent::__construct();
23
+    }
24
+
25
+    protected function configure(): void {
26
+        $this
27
+            ->setName('files:repair-tree')
28
+            ->setDescription('Try and repair malformed filesystem tree structures (may be necessary to run multiple times for nested malformations)')
29
+            ->addOption('dry-run');
30
+    }
31
+
32
+    public function execute(InputInterface $input, OutputInterface $output): int {
33
+        $rows = $this->findBrokenTreeBits();
34
+        $fix = !$input->getOption('dry-run');
35
+
36
+        $output->writeln('Found ' . count($rows) . ' file entries with an invalid path');
37
+
38
+        if ($fix) {
39
+            $this->connection->beginTransaction();
40
+        }
41
+
42
+        $query = $this->connection->getQueryBuilder();
43
+        $query->update('filecache')
44
+            ->set('path', $query->createParameter('path'))
45
+            ->set('path_hash', $query->func()->md5($query->createParameter('path')))
46
+            ->set('storage', $query->createParameter('storage'))
47
+            ->where($query->expr()->eq('fileid', $query->createParameter('fileid')));
48
+
49
+        foreach ($rows as $row) {
50
+            $output->writeln("Path of file {$row['fileid']} is {$row['path']} but should be {$row['parent_path']}/{$row['name']} based on its parent", OutputInterface::VERBOSITY_VERBOSE);
51
+
52
+            if ($fix) {
53
+                $fileId = $this->getFileId((int)$row['parent_storage'], $row['parent_path'] . '/' . $row['name']);
54
+                if ($fileId > 0) {
55
+                    $output->writeln("Cache entry has already be recreated with id $fileId, deleting instead");
56
+                    $this->deleteById((int)$row['fileid']);
57
+                } else {
58
+                    $query->setParameters([
59
+                        'fileid' => $row['fileid'],
60
+                        'path' => $row['parent_path'] . '/' . $row['name'],
61
+                        'storage' => $row['parent_storage'],
62
+                    ]);
63
+                    $query->executeStatement();
64
+                }
65
+            }
66
+        }
67
+
68
+        if ($fix) {
69
+            $this->connection->commit();
70
+        }
71
+
72
+        return self::SUCCESS;
73
+    }
74
+
75
+    private function getFileId(int $storage, string $path) {
76
+        $query = $this->connection->getQueryBuilder();
77
+        $query->select('fileid')
78
+            ->from('filecache')
79
+            ->where($query->expr()->eq('storage', $query->createNamedParameter($storage)))
80
+            ->andWhere($query->expr()->eq('path_hash', $query->createNamedParameter(md5($path))));
81
+        return $query->executeQuery()->fetch(\PDO::FETCH_COLUMN);
82
+    }
83
+
84
+    private function deleteById(int $fileId): void {
85
+        $query = $this->connection->getQueryBuilder();
86
+        $query->delete('filecache')
87
+            ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId)));
88
+        $query->executeStatement();
89
+    }
90
+
91
+    private function findBrokenTreeBits(): array {
92
+        $query = $this->connection->getQueryBuilder();
93
+
94
+        $query->select('f.fileid', 'f.path', 'f.parent', 'f.name')
95
+            ->selectAlias('p.path', 'parent_path')
96
+            ->selectAlias('p.storage', 'parent_storage')
97
+            ->from('filecache', 'f')
98
+            ->innerJoin('f', 'filecache', 'p', $query->expr()->eq('f.parent', 'p.fileid'))
99
+            ->where($query->expr()->orX(
100
+                $query->expr()->andX(
101
+                    $query->expr()->neq('p.path_hash', $query->createNamedParameter(md5(''))),
102
+                    $query->expr()->neq('f.path', $query->func()->concat('p.path', $query->func()->concat($query->createNamedParameter('/'), 'f.name')))
103
+                ),
104
+                $query->expr()->andX(
105
+                    $query->expr()->eq('p.path_hash', $query->createNamedParameter(md5(''))),
106
+                    $query->expr()->neq('f.path', 'f.name')
107
+                ),
108
+                $query->expr()->neq('f.storage', 'p.storage')
109
+            ));
110
+
111
+        return $query->executeQuery()->fetchAll();
112
+    }
113 113
 }
Please login to merge, or discard this patch.