Completed
Pull Request — master (#3887)
by Joas
91:25 queued 74:34
created
apps/files_sharing/lib/Command/CleanupRemoteStorages.php 2 patches
Indentation   +143 added lines, -143 removed lines patch added patch discarded remove patch
@@ -34,147 +34,147 @@
 block discarded – undo
34 34
  */
35 35
 class CleanupRemoteStorages extends Command {
36 36
 
37
-	/**
38
-	 * @var IDBConnection
39
-	 */
40
-	protected $connection;
41
-
42
-	public function __construct(IDBConnection $connection) {
43
-		$this->connection = $connection;
44
-		parent::__construct();
45
-	}
46
-
47
-	protected function configure() {
48
-		$this
49
-			->setName('sharing:cleanup-remote-storages')
50
-			->setDescription('Cleanup shared storage entries that have no matching entry in the shares_external table')
51
-			->addOption(
52
-				'dry-run',
53
-				null,
54
-				InputOption::VALUE_NONE,
55
-				'only show which storages would be deleted'
56
-			);
57
-	}
58
-
59
-	public function execute(InputInterface $input, OutputInterface $output) {
60
-
61
-		$remoteStorages = $this->getRemoteStorages();
62
-
63
-		$output->writeln(count($remoteStorages) . " remote storage(s) need(s) to be checked");
64
-
65
-		$remoteShareIds = $this->getRemoteShareIds();
66
-
67
-		$output->writeln(count($remoteShareIds) . " remote share(s) exist");
68
-
69
-		foreach ($remoteShareIds as $id => $remoteShareId) {
70
-			if (isset($remoteStorages[$remoteShareId])) {
71
-				if ($input->getOption('dry-run') || $output->isVerbose()) {
72
-					$output->writeln("<info>$remoteShareId belongs to remote share $id</info>");
73
-				}
74
-
75
-				unset($remoteStorages[$remoteShareId]);
76
-			} else {
77
-				$output->writeln("<comment>$remoteShareId for share $id has no matching storage, yet</comment>");
78
-			}
79
-		}
80
-
81
-		if (empty($remoteStorages)) {
82
-			$output->writeln("<info>no storages deleted</info>");
83
-		} else {
84
-			$dryRun = $input->getOption('dry-run');
85
-			foreach ($remoteStorages as $id => $numericId) {
86
-				if ($dryRun) {
87
-					$output->writeln("<error>$id [$numericId] can be deleted</error>");
88
-					$this->countFiles($numericId, $output);
89
-				} else {
90
-					$this->deleteStorage($id, $numericId, $output);
91
-				}
92
-			}
93
-		}
94
-	}
95
-
96
-	public function countFiles($numericId, OutputInterface $output) {
97
-		$queryBuilder = $this->connection->getQueryBuilder();
98
-		$queryBuilder->select($queryBuilder->createFunction('count(fileid)'))
99
-			->from('filecache')
100
-			->where($queryBuilder->expr()->eq(
101
-				'storage',
102
-				$queryBuilder->createNamedParameter($numericId, IQueryBuilder::PARAM_STR),
103
-				IQueryBuilder::PARAM_STR)
104
-			);
105
-		$result = $queryBuilder->execute();
106
-		$count = $result->fetchColumn();
107
-		$output->writeln("$count files can be deleted for storage $numericId");
108
-	}
109
-
110
-	public function deleteStorage($id, $numericId, OutputInterface $output) {
111
-		$queryBuilder = $this->connection->getQueryBuilder();
112
-		$queryBuilder->delete('storages')
113
-			->where($queryBuilder->expr()->eq(
114
-				'id',
115
-				$queryBuilder->createNamedParameter($id, IQueryBuilder::PARAM_STR),
116
-				IQueryBuilder::PARAM_STR)
117
-			);
118
-		$output->write("deleting $id [$numericId] ... ");
119
-		$count = $queryBuilder->execute();
120
-		$output->writeln("deleted $count storage");
121
-		$this->deleteFiles($numericId, $output);
122
-	}
123
-
124
-	public function deleteFiles($numericId, OutputInterface $output) {
125
-		$queryBuilder = $this->connection->getQueryBuilder();
126
-		$queryBuilder->delete('filecache')
127
-			->where($queryBuilder->expr()->eq(
128
-				'storage',
129
-				$queryBuilder->createNamedParameter($numericId, IQueryBuilder::PARAM_STR),
130
-				IQueryBuilder::PARAM_STR)
131
-			);
132
-		$output->write("deleting files for storage $numericId ... ");
133
-		$count = $queryBuilder->execute();
134
-		$output->writeln("deleted $count files");
135
-	}
136
-
137
-	public function getRemoteStorages() {
138
-
139
-		$queryBuilder = $this->connection->getQueryBuilder();
140
-		$queryBuilder->select(['id', 'numeric_id'])
141
-			->from('storages')
142
-			->where($queryBuilder->expr()->like(
143
-				'id',
144
-				// match all 'shared::' + 32 characters storages
145
-				$queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::') . str_repeat('_', 32)),
146
-				IQueryBuilder::PARAM_STR)
147
-			)
148
-			->andWhere($queryBuilder->expr()->notLike(
149
-				'id',
150
-				// but not the ones starting with a '/', they are for normal shares
151
-				$queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::/') . '%'),
152
-				IQueryBuilder::PARAM_STR)
153
-			)->orderBy('numeric_id');
154
-		$query = $queryBuilder->execute();
155
-
156
-		$remoteStorages = [];
157
-
158
-		while ($row = $query->fetch()) {
159
-			$remoteStorages[$row['id']] = $row['numeric_id'];
160
-		}
161
-
162
-		return $remoteStorages;
163
-	}
164
-
165
-	public function getRemoteShareIds() {
166
-
167
-		$queryBuilder = $this->connection->getQueryBuilder();
168
-		$queryBuilder->select(['id', 'share_token', 'remote'])
169
-			->from('share_external');
170
-		$query = $queryBuilder->execute();
171
-
172
-		$remoteShareIds = [];
173
-
174
-		while ($row = $query->fetch()) {
175
-			$remoteShareIds[$row['id']] = 'shared::' . md5($row['share_token'] . '@' . $row['remote']);
176
-		}
177
-
178
-		return $remoteShareIds;
179
-	}
37
+    /**
38
+     * @var IDBConnection
39
+     */
40
+    protected $connection;
41
+
42
+    public function __construct(IDBConnection $connection) {
43
+        $this->connection = $connection;
44
+        parent::__construct();
45
+    }
46
+
47
+    protected function configure() {
48
+        $this
49
+            ->setName('sharing:cleanup-remote-storages')
50
+            ->setDescription('Cleanup shared storage entries that have no matching entry in the shares_external table')
51
+            ->addOption(
52
+                'dry-run',
53
+                null,
54
+                InputOption::VALUE_NONE,
55
+                'only show which storages would be deleted'
56
+            );
57
+    }
58
+
59
+    public function execute(InputInterface $input, OutputInterface $output) {
60
+
61
+        $remoteStorages = $this->getRemoteStorages();
62
+
63
+        $output->writeln(count($remoteStorages) . " remote storage(s) need(s) to be checked");
64
+
65
+        $remoteShareIds = $this->getRemoteShareIds();
66
+
67
+        $output->writeln(count($remoteShareIds) . " remote share(s) exist");
68
+
69
+        foreach ($remoteShareIds as $id => $remoteShareId) {
70
+            if (isset($remoteStorages[$remoteShareId])) {
71
+                if ($input->getOption('dry-run') || $output->isVerbose()) {
72
+                    $output->writeln("<info>$remoteShareId belongs to remote share $id</info>");
73
+                }
74
+
75
+                unset($remoteStorages[$remoteShareId]);
76
+            } else {
77
+                $output->writeln("<comment>$remoteShareId for share $id has no matching storage, yet</comment>");
78
+            }
79
+        }
80
+
81
+        if (empty($remoteStorages)) {
82
+            $output->writeln("<info>no storages deleted</info>");
83
+        } else {
84
+            $dryRun = $input->getOption('dry-run');
85
+            foreach ($remoteStorages as $id => $numericId) {
86
+                if ($dryRun) {
87
+                    $output->writeln("<error>$id [$numericId] can be deleted</error>");
88
+                    $this->countFiles($numericId, $output);
89
+                } else {
90
+                    $this->deleteStorage($id, $numericId, $output);
91
+                }
92
+            }
93
+        }
94
+    }
95
+
96
+    public function countFiles($numericId, OutputInterface $output) {
97
+        $queryBuilder = $this->connection->getQueryBuilder();
98
+        $queryBuilder->select($queryBuilder->createFunction('count(fileid)'))
99
+            ->from('filecache')
100
+            ->where($queryBuilder->expr()->eq(
101
+                'storage',
102
+                $queryBuilder->createNamedParameter($numericId, IQueryBuilder::PARAM_STR),
103
+                IQueryBuilder::PARAM_STR)
104
+            );
105
+        $result = $queryBuilder->execute();
106
+        $count = $result->fetchColumn();
107
+        $output->writeln("$count files can be deleted for storage $numericId");
108
+    }
109
+
110
+    public function deleteStorage($id, $numericId, OutputInterface $output) {
111
+        $queryBuilder = $this->connection->getQueryBuilder();
112
+        $queryBuilder->delete('storages')
113
+            ->where($queryBuilder->expr()->eq(
114
+                'id',
115
+                $queryBuilder->createNamedParameter($id, IQueryBuilder::PARAM_STR),
116
+                IQueryBuilder::PARAM_STR)
117
+            );
118
+        $output->write("deleting $id [$numericId] ... ");
119
+        $count = $queryBuilder->execute();
120
+        $output->writeln("deleted $count storage");
121
+        $this->deleteFiles($numericId, $output);
122
+    }
123
+
124
+    public function deleteFiles($numericId, OutputInterface $output) {
125
+        $queryBuilder = $this->connection->getQueryBuilder();
126
+        $queryBuilder->delete('filecache')
127
+            ->where($queryBuilder->expr()->eq(
128
+                'storage',
129
+                $queryBuilder->createNamedParameter($numericId, IQueryBuilder::PARAM_STR),
130
+                IQueryBuilder::PARAM_STR)
131
+            );
132
+        $output->write("deleting files for storage $numericId ... ");
133
+        $count = $queryBuilder->execute();
134
+        $output->writeln("deleted $count files");
135
+    }
136
+
137
+    public function getRemoteStorages() {
138
+
139
+        $queryBuilder = $this->connection->getQueryBuilder();
140
+        $queryBuilder->select(['id', 'numeric_id'])
141
+            ->from('storages')
142
+            ->where($queryBuilder->expr()->like(
143
+                'id',
144
+                // match all 'shared::' + 32 characters storages
145
+                $queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::') . str_repeat('_', 32)),
146
+                IQueryBuilder::PARAM_STR)
147
+            )
148
+            ->andWhere($queryBuilder->expr()->notLike(
149
+                'id',
150
+                // but not the ones starting with a '/', they are for normal shares
151
+                $queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::/') . '%'),
152
+                IQueryBuilder::PARAM_STR)
153
+            )->orderBy('numeric_id');
154
+        $query = $queryBuilder->execute();
155
+
156
+        $remoteStorages = [];
157
+
158
+        while ($row = $query->fetch()) {
159
+            $remoteStorages[$row['id']] = $row['numeric_id'];
160
+        }
161
+
162
+        return $remoteStorages;
163
+    }
164
+
165
+    public function getRemoteShareIds() {
166
+
167
+        $queryBuilder = $this->connection->getQueryBuilder();
168
+        $queryBuilder->select(['id', 'share_token', 'remote'])
169
+            ->from('share_external');
170
+        $query = $queryBuilder->execute();
171
+
172
+        $remoteShareIds = [];
173
+
174
+        while ($row = $query->fetch()) {
175
+            $remoteShareIds[$row['id']] = 'shared::' . md5($row['share_token'] . '@' . $row['remote']);
176
+        }
177
+
178
+        return $remoteShareIds;
179
+    }
180 180
 }
Please login to merge, or discard this patch.
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -60,11 +60,11 @@  discard block
 block discarded – undo
60 60
 
61 61
 		$remoteStorages = $this->getRemoteStorages();
62 62
 
63
-		$output->writeln(count($remoteStorages) . " remote storage(s) need(s) to be checked");
63
+		$output->writeln(count($remoteStorages)." remote storage(s) need(s) to be checked");
64 64
 
65 65
 		$remoteShareIds = $this->getRemoteShareIds();
66 66
 
67
-		$output->writeln(count($remoteShareIds) . " remote share(s) exist");
67
+		$output->writeln(count($remoteShareIds)." remote share(s) exist");
68 68
 
69 69
 		foreach ($remoteShareIds as $id => $remoteShareId) {
70 70
 			if (isset($remoteStorages[$remoteShareId])) {
@@ -142,13 +142,13 @@  discard block
 block discarded – undo
142 142
 			->where($queryBuilder->expr()->like(
143 143
 				'id',
144 144
 				// match all 'shared::' + 32 characters storages
145
-				$queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::') . str_repeat('_', 32)),
145
+				$queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::').str_repeat('_', 32)),
146 146
 				IQueryBuilder::PARAM_STR)
147 147
 			)
148 148
 			->andWhere($queryBuilder->expr()->notLike(
149 149
 				'id',
150 150
 				// but not the ones starting with a '/', they are for normal shares
151
-				$queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::/') . '%'),
151
+				$queryBuilder->createNamedParameter($this->connection->escapeLikeParameter('shared::/').'%'),
152 152
 				IQueryBuilder::PARAM_STR)
153 153
 			)->orderBy('numeric_id');
154 154
 		$query = $queryBuilder->execute();
@@ -172,7 +172,7 @@  discard block
 block discarded – undo
172 172
 		$remoteShareIds = [];
173 173
 
174 174
 		while ($row = $query->fetch()) {
175
-			$remoteShareIds[$row['id']] = 'shared::' . md5($row['share_token'] . '@' . $row['remote']);
175
+			$remoteShareIds[$row['id']] = 'shared::'.md5($row['share_token'].'@'.$row['remote']);
176 176
 		}
177 177
 
178 178
 		return $remoteShareIds;
Please login to merge, or discard this patch.